blob: 19225d3bd90260ca8854e641585b424f58ad8e9b [file] [log] [blame]
Ronghua Wu14bcaca2015-03-16 11:24:30 -07001/*
2 * Copyright (C) 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//#define LOG_NDEBUG 0
18#define LOG_TAG "ProcessInfo"
19#include <utils/Log.h>
20
21#include <media/stagefright/ProcessInfo.h>
22
Ronghua Wud11c43a2016-01-27 16:26:12 -080023#include <binder/IPCThreadState.h>
Ronghua Wu14bcaca2015-03-16 11:24:30 -070024#include <binder/IProcessInfoService.h>
25#include <binder/IServiceManager.h>
Robert Shihc3af31b2019-09-20 21:45:01 -070026#include <private/android_filesystem_config.h>
Ronghua Wu14bcaca2015-03-16 11:24:30 -070027
28namespace android {
29
Chong Zhang97d367b2020-09-16 12:53:14 -070030static constexpr int32_t INVALID_ADJ = -10000;
31static constexpr int32_t NATIVE_ADJ = -1000;
32
Ronghua Wu14bcaca2015-03-16 11:24:30 -070033ProcessInfo::ProcessInfo() {}
34
35bool ProcessInfo::getPriority(int pid, int* priority) {
36 sp<IBinder> binder = defaultServiceManager()->getService(String16("processinfo"));
37 sp<IProcessInfoService> service = interface_cast<IProcessInfoService>(binder);
38
39 size_t length = 1;
Ronghua Wu221761b2015-12-02 11:21:37 -080040 int32_t state;
Ronghua Wu221761b2015-12-02 11:21:37 -080041 int32_t score = INVALID_ADJ;
42 status_t err = service->getProcessStatesAndOomScoresFromPids(length, &pid, &state, &score);
Ronghua Wu14bcaca2015-03-16 11:24:30 -070043 if (err != OK) {
Ronghua Wu221761b2015-12-02 11:21:37 -080044 ALOGE("getProcessStatesAndOomScoresFromPids failed");
Ronghua Wu14bcaca2015-03-16 11:24:30 -070045 return false;
46 }
Ronghua Wu221761b2015-12-02 11:21:37 -080047 ALOGV("pid %d state %d score %d", pid, state, score);
48 if (score <= NATIVE_ADJ) {
Chong Zhang97d367b2020-09-16 12:53:14 -070049 std::scoped_lock lock{mOverrideLock};
50
51 // If this process if not tracked by ActivityManagerService, look for overrides.
52 auto it = mOverrideMap.find(pid);
53 if (it != mOverrideMap.end()) {
54 ALOGI("pid %d invalid OOM score %d, override to %d", pid, score, it->second.oomScore);
55 score = it->second.oomScore;
56 } else {
57 ALOGE("pid %d invalid OOM score %d", pid, score);
58 return false;
59 }
Ronghua Wu14bcaca2015-03-16 11:24:30 -070060 }
61
Ronghua Wu221761b2015-12-02 11:21:37 -080062 // Use OOM adjustments value as the priority. Lower the value, higher the priority.
63 *priority = score;
Ronghua Wu14bcaca2015-03-16 11:24:30 -070064 return true;
65}
66
Ronghua Wud11c43a2016-01-27 16:26:12 -080067bool ProcessInfo::isValidPid(int pid) {
68 int callingPid = IPCThreadState::self()->getCallingPid();
Robert Shihc3af31b2019-09-20 21:45:01 -070069 int callingUid = IPCThreadState::self()->getCallingUid();
Ronghua Wud11c43a2016-01-27 16:26:12 -080070 // Trust it if this is called from the same process otherwise pid has to match the calling pid.
Robert Shihc3af31b2019-09-20 21:45:01 -070071 return (callingPid == getpid()) || (callingPid == pid) || (callingUid == AID_MEDIA);
Ronghua Wud11c43a2016-01-27 16:26:12 -080072}
73
Chong Zhang97d367b2020-09-16 12:53:14 -070074bool ProcessInfo::overrideProcessInfo(int pid, int procState, int oomScore) {
75 std::scoped_lock lock{mOverrideLock};
76
77 mOverrideMap.erase(pid);
78
79 // Disable the override if oomScore is set to NATIVE_ADJ or below.
80 if (oomScore <= NATIVE_ADJ) {
81 return false;
82 }
83
84 mOverrideMap.emplace(pid, ProcessInfoOverride{procState, oomScore});
85 return true;
86}
87
88void ProcessInfo::removeProcessInfoOverride(int pid) {
89 std::scoped_lock lock{mOverrideLock};
90
91 mOverrideMap.erase(pid);
92}
93
Ronghua Wu14bcaca2015-03-16 11:24:30 -070094ProcessInfo::~ProcessInfo() {}
95
96} // namespace android