1 /*
2  * Copyright 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_PROCESS_INFO_SERVICE_H
18 #define ANDROID_PROCESS_INFO_SERVICE_H
19 
20 #ifndef __ANDROID_VNDK__
21 
22 #include <binder/IProcessInfoService.h>
23 #include <utils/Errors.h>
24 #include <utils/Singleton.h>
25 #include <sys/types.h>
26 
27 namespace android {
28 
29 // ----------------------------------------------------------------------
30 
31 class ProcessInfoService : public Singleton<ProcessInfoService> {
32 
33     friend class Singleton<ProcessInfoService>;
34     sp<IProcessInfoService> mProcessInfoService;
35     Mutex mProcessInfoLock;
36 
37     ProcessInfoService();
38 
39     status_t getProcessStatesImpl(size_t length, /*in*/ int32_t* pids, /*out*/ int32_t* states);
40     status_t getProcessStatesScoresImpl(size_t length, /*in*/ int32_t* pids,
41             /*out*/ int32_t* states, /*out*/ int32_t *scores);
42     void updateBinderLocked();
43 
44     static const int BINDER_ATTEMPT_LIMIT = 5;
45 
46 public:
47 
48     /**
49      * For each PID in the given "pids" input array, write the current process state
50      * for that process into the "states" output array, or
51      * ActivityManager.PROCESS_STATE_NONEXISTENT * to indicate that no process with the given PID
52      * exists.
53      *
54      * Returns NO_ERROR if this operation was successful, or a negative error code otherwise.
55      */
getProcessStatesFromPids(size_t length,int32_t * pids,int32_t * states)56     static status_t getProcessStatesFromPids(size_t length, /*in*/ int32_t* pids,
57             /*out*/ int32_t* states) {
58         return ProcessInfoService::getInstance().getProcessStatesImpl(length, /*in*/ pids,
59                 /*out*/ states);
60     }
61 
62     /**
63      * For each PID in the given "pids" input array, write the current process state
64      * for that process into the "states" output array, or
65      * ActivityManager.PROCESS_STATE_NONEXISTENT * to indicate that no process with the given PID
66      * exists. OoM scores will also be written in the "scores" output array.
67      * Please also note that clients calling this method need to have
68      * "GET_PROCESS_STATE_AND_OOM_SCORE" permission.
69      *
70      * Returns NO_ERROR if this operation was successful, or a negative error code otherwise.
71      */
getProcessStatesScoresFromPids(size_t length,int32_t * pids,int32_t * states,int32_t * scores)72     static status_t getProcessStatesScoresFromPids(size_t length, /*in*/ int32_t* pids,
73             /*out*/ int32_t* states, /*out*/ int32_t *scores) {
74         return ProcessInfoService::getInstance().getProcessStatesScoresImpl(
75                 length, /*in*/ pids, /*out*/ states, /*out*/ scores);
76     }
77 };
78 
79 // ----------------------------------------------------------------------
80 
81 } // namespace android
82 
83 #else // __ANDROID_VNDK__
84 #error "This header is not visible to vendors"
85 #endif // __ANDROID_VNDK__
86 
87 #endif // ANDROID_PROCESS_INFO_SERVICE_H
88 
89