Home | History | Annotate | Download | only in binder
      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 #include <binder/ProcessInfoService.h>
     18 #include <binder/IServiceManager.h>
     19 
     20 #include <utils/Log.h>
     21 #include <utils/String16.h>
     22 
     23 namespace android {
     24 
     25 ProcessInfoService::ProcessInfoService() {
     26     updateBinderLocked();
     27 }
     28 
     29 status_t ProcessInfoService::getProcessStatesImpl(size_t length, /*in*/ int32_t* pids,
     30         /*out*/ int32_t* states) {
     31     status_t err = NO_ERROR;
     32     sp<IProcessInfoService> pis;
     33     mProcessInfoLock.lock();
     34     pis = mProcessInfoService;
     35     mProcessInfoLock.unlock();
     36 
     37     for (int i = 0; i < BINDER_ATTEMPT_LIMIT; i++) {
     38 
     39         if (pis != NULL) {
     40             err = pis->getProcessStatesFromPids(length, /*in*/ pids, /*out*/ states);
     41             if (err == NO_ERROR) return NO_ERROR; // success
     42             if (IInterface::asBinder(pis)->isBinderAlive()) return err;
     43         }
     44         sleep(1);
     45 
     46         mProcessInfoLock.lock();
     47         if (pis == mProcessInfoService) {
     48             updateBinderLocked();
     49         }
     50         pis = mProcessInfoService;
     51         mProcessInfoLock.unlock();
     52     }
     53 
     54     ALOGW("%s: Could not retrieve process states from ProcessInfoService after %d retries.",
     55             __FUNCTION__, BINDER_ATTEMPT_LIMIT);
     56 
     57     return TIMED_OUT;
     58 }
     59 
     60 status_t ProcessInfoService::getProcessStatesScoresImpl(size_t length,
     61         /*in*/ int32_t* pids, /*out*/ int32_t* states,
     62         /*out*/ int32_t *scores) {
     63     status_t err = NO_ERROR;
     64     sp<IProcessInfoService> pis;
     65     mProcessInfoLock.lock();
     66     pis = mProcessInfoService;
     67     mProcessInfoLock.unlock();
     68 
     69     for (int i = 0; i < BINDER_ATTEMPT_LIMIT; i++) {
     70 
     71         if (pis != NULL) {
     72             err = pis->getProcessStatesAndOomScoresFromPids(length,
     73                     /*in*/ pids, /*out*/ states, /*out*/ scores);
     74             if (err == NO_ERROR) return NO_ERROR; // success
     75             if (IInterface::asBinder(pis)->isBinderAlive()) return err;
     76         }
     77         sleep(1);
     78 
     79         mProcessInfoLock.lock();
     80         if (pis == mProcessInfoService) {
     81             updateBinderLocked();
     82         }
     83         pis = mProcessInfoService;
     84         mProcessInfoLock.unlock();
     85     }
     86 
     87     ALOGW("%s: Could not retrieve process states and scores "
     88             "from ProcessInfoService after %d retries.", __FUNCTION__,
     89             BINDER_ATTEMPT_LIMIT);
     90 
     91     return TIMED_OUT;
     92 }
     93 
     94 void ProcessInfoService::updateBinderLocked() {
     95     const sp<IServiceManager> sm(defaultServiceManager());
     96     if (sm != NULL) {
     97         const String16 name("processinfo");
     98         mProcessInfoService = interface_cast<IProcessInfoService>(sm->checkService(name));
     99     }
    100 }
    101 
    102 ANDROID_SINGLETON_STATIC_INSTANCE(ProcessInfoService);
    103 
    104 }; // namespace android
    105