blob: 27f1a7927f8fb3a0a124b8333a478448a71327cf [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>
26
27namespace android {
28
29ProcessInfo::ProcessInfo() {}
30
31bool ProcessInfo::getPriority(int pid, int* priority) {
32 sp<IBinder> binder = defaultServiceManager()->getService(String16("processinfo"));
33 sp<IProcessInfoService> service = interface_cast<IProcessInfoService>(binder);
34
35 size_t length = 1;
Ronghua Wu221761b2015-12-02 11:21:37 -080036 int32_t state;
37 static const int32_t INVALID_ADJ = -10000;
38 static const int32_t NATIVE_ADJ = -1000;
39 int32_t score = INVALID_ADJ;
40 status_t err = service->getProcessStatesAndOomScoresFromPids(length, &pid, &state, &score);
Ronghua Wu14bcaca2015-03-16 11:24:30 -070041 if (err != OK) {
Ronghua Wu221761b2015-12-02 11:21:37 -080042 ALOGE("getProcessStatesAndOomScoresFromPids failed");
Ronghua Wu14bcaca2015-03-16 11:24:30 -070043 return false;
44 }
Ronghua Wu221761b2015-12-02 11:21:37 -080045 ALOGV("pid %d state %d score %d", pid, state, score);
46 if (score <= NATIVE_ADJ) {
47 ALOGE("pid %d invalid OOM adjustments value %d", pid, score);
Ronghua Wu14bcaca2015-03-16 11:24:30 -070048 return false;
49 }
50
Ronghua Wu221761b2015-12-02 11:21:37 -080051 // Use OOM adjustments value as the priority. Lower the value, higher the priority.
52 *priority = score;
Ronghua Wu14bcaca2015-03-16 11:24:30 -070053 return true;
54}
55
Ronghua Wud11c43a2016-01-27 16:26:12 -080056bool ProcessInfo::isValidPid(int pid) {
57 int callingPid = IPCThreadState::self()->getCallingPid();
58 // Trust it if this is called from the same process otherwise pid has to match the calling pid.
59 return (callingPid == getpid()) || (callingPid == pid);
60}
61
Ronghua Wu14bcaca2015-03-16 11:24:30 -070062ProcessInfo::~ProcessInfo() {}
63
64} // namespace android