xref: /aosp_15_r20/frameworks/native/libs/binder/ProcessInfoService.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2015 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <processinfo/ProcessInfoService.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <utils/Log.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <utils/String16.h>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker namespace android {
24*38e8c45fSAndroid Build Coastguard Worker 
ProcessInfoService()25*38e8c45fSAndroid Build Coastguard Worker ProcessInfoService::ProcessInfoService() {
26*38e8c45fSAndroid Build Coastguard Worker     updateBinderLocked();
27*38e8c45fSAndroid Build Coastguard Worker }
28*38e8c45fSAndroid Build Coastguard Worker 
getProcessStatesImpl(size_t length,int32_t * pids,int32_t * states)29*38e8c45fSAndroid Build Coastguard Worker status_t ProcessInfoService::getProcessStatesImpl(size_t length, /*in*/ int32_t* pids,
30*38e8c45fSAndroid Build Coastguard Worker         /*out*/ int32_t* states) {
31*38e8c45fSAndroid Build Coastguard Worker     status_t err = NO_ERROR;
32*38e8c45fSAndroid Build Coastguard Worker     sp<IProcessInfoService> pis;
33*38e8c45fSAndroid Build Coastguard Worker     mProcessInfoLock.lock();
34*38e8c45fSAndroid Build Coastguard Worker     pis = mProcessInfoService;
35*38e8c45fSAndroid Build Coastguard Worker     mProcessInfoLock.unlock();
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker     for (int i = 0; i < BINDER_ATTEMPT_LIMIT; i++) {
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker         if (pis != nullptr) {
40*38e8c45fSAndroid Build Coastguard Worker             err = pis->getProcessStatesFromPids(length, /*in*/ pids, /*out*/ states);
41*38e8c45fSAndroid Build Coastguard Worker             if (err == NO_ERROR) return NO_ERROR; // success
42*38e8c45fSAndroid Build Coastguard Worker             if (IInterface::asBinder(pis)->isBinderAlive()) return err;
43*38e8c45fSAndroid Build Coastguard Worker         }
44*38e8c45fSAndroid Build Coastguard Worker         sleep(1);
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker         mProcessInfoLock.lock();
47*38e8c45fSAndroid Build Coastguard Worker         if (pis == mProcessInfoService) {
48*38e8c45fSAndroid Build Coastguard Worker             updateBinderLocked();
49*38e8c45fSAndroid Build Coastguard Worker         }
50*38e8c45fSAndroid Build Coastguard Worker         pis = mProcessInfoService;
51*38e8c45fSAndroid Build Coastguard Worker         mProcessInfoLock.unlock();
52*38e8c45fSAndroid Build Coastguard Worker     }
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker     ALOGW("%s: Could not retrieve process states from ProcessInfoService after %d retries.",
55*38e8c45fSAndroid Build Coastguard Worker             __FUNCTION__, BINDER_ATTEMPT_LIMIT);
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker     return TIMED_OUT;
58*38e8c45fSAndroid Build Coastguard Worker }
59*38e8c45fSAndroid Build Coastguard Worker 
getProcessStatesScoresImpl(size_t length,int32_t * pids,int32_t * states,int32_t * scores)60*38e8c45fSAndroid Build Coastguard Worker status_t ProcessInfoService::getProcessStatesScoresImpl(size_t length,
61*38e8c45fSAndroid Build Coastguard Worker         /*in*/ int32_t* pids, /*out*/ int32_t* states,
62*38e8c45fSAndroid Build Coastguard Worker         /*out*/ int32_t *scores) {
63*38e8c45fSAndroid Build Coastguard Worker     status_t err = NO_ERROR;
64*38e8c45fSAndroid Build Coastguard Worker     sp<IProcessInfoService> pis;
65*38e8c45fSAndroid Build Coastguard Worker     mProcessInfoLock.lock();
66*38e8c45fSAndroid Build Coastguard Worker     pis = mProcessInfoService;
67*38e8c45fSAndroid Build Coastguard Worker     mProcessInfoLock.unlock();
68*38e8c45fSAndroid Build Coastguard Worker 
69*38e8c45fSAndroid Build Coastguard Worker     for (int i = 0; i < BINDER_ATTEMPT_LIMIT; i++) {
70*38e8c45fSAndroid Build Coastguard Worker 
71*38e8c45fSAndroid Build Coastguard Worker         if (pis != nullptr) {
72*38e8c45fSAndroid Build Coastguard Worker             err = pis->getProcessStatesAndOomScoresFromPids(length,
73*38e8c45fSAndroid Build Coastguard Worker                     /*in*/ pids, /*out*/ states, /*out*/ scores);
74*38e8c45fSAndroid Build Coastguard Worker             if (err == NO_ERROR) return NO_ERROR; // success
75*38e8c45fSAndroid Build Coastguard Worker             if (IInterface::asBinder(pis)->isBinderAlive()) return err;
76*38e8c45fSAndroid Build Coastguard Worker         }
77*38e8c45fSAndroid Build Coastguard Worker         sleep(1);
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker         mProcessInfoLock.lock();
80*38e8c45fSAndroid Build Coastguard Worker         if (pis == mProcessInfoService) {
81*38e8c45fSAndroid Build Coastguard Worker             updateBinderLocked();
82*38e8c45fSAndroid Build Coastguard Worker         }
83*38e8c45fSAndroid Build Coastguard Worker         pis = mProcessInfoService;
84*38e8c45fSAndroid Build Coastguard Worker         mProcessInfoLock.unlock();
85*38e8c45fSAndroid Build Coastguard Worker     }
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker     ALOGW("%s: Could not retrieve process states and scores "
88*38e8c45fSAndroid Build Coastguard Worker             "from ProcessInfoService after %d retries.", __FUNCTION__,
89*38e8c45fSAndroid Build Coastguard Worker             BINDER_ATTEMPT_LIMIT);
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker     return TIMED_OUT;
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker 
updateBinderLocked()94*38e8c45fSAndroid Build Coastguard Worker void ProcessInfoService::updateBinderLocked() {
95*38e8c45fSAndroid Build Coastguard Worker     const sp<IServiceManager> sm(defaultServiceManager());
96*38e8c45fSAndroid Build Coastguard Worker     if (sm != nullptr) {
97*38e8c45fSAndroid Build Coastguard Worker         const String16 name("processinfo");
98*38e8c45fSAndroid Build Coastguard Worker         mProcessInfoService = interface_cast<IProcessInfoService>(sm->checkService(name));
99*38e8c45fSAndroid Build Coastguard Worker     }
100*38e8c45fSAndroid Build Coastguard Worker }
101*38e8c45fSAndroid Build Coastguard Worker 
102*38e8c45fSAndroid Build Coastguard Worker ANDROID_SINGLETON_STATIC_INSTANCE(ProcessInfoService)
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker } // namespace android
105