| Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 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 |  | 
| Steven Moreland | 45e25cb | 2021-04-02 04:30:41 +0000 | [diff] [blame] | 17 | #include <processinfo/ProcessInfoService.h> | 
| Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 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 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 39 | if (pis != nullptr) { | 
| Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 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 |  | 
| Emilian Peev | b4f33df | 2017-02-02 09:57:18 +0000 | [diff] [blame] | 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 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 71 | if (pis != nullptr) { | 
| Emilian Peev | b4f33df | 2017-02-02 09:57:18 +0000 | [diff] [blame] | 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 |  | 
| Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 94 | void ProcessInfoService::updateBinderLocked() { | 
|  | 95 | const sp<IServiceManager> sm(defaultServiceManager()); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 96 | if (sm != nullptr) { | 
| Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 97 | const String16 name("processinfo"); | 
|  | 98 | mProcessInfoService = interface_cast<IProcessInfoService>(sm->checkService(name)); | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
| Jooyung Han | c91e3cb | 2020-11-25 06:38:17 +0900 | [diff] [blame] | 102 | ANDROID_SINGLETON_STATIC_INSTANCE(ProcessInfoService) | 
| Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 103 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 104 | } // namespace android |