梳理Process进程相关知识点,再继续补充点内容。
Linux系统中对进程的管理无非是从调度策略、优先级以及CPU限制三个角度进行配置与管理,那么Android中主要是通过AMS来管理应用程序进程的,是不是也是从这三个方面进行管理的呢?答案是肯定的,那么本篇文章先来看看cpuset负载均衡在AMS中是如何应用的。
一、cpuset介绍
cpuset是Linux cgroup子系统,它为cgroup任务分配单独的CPU和内存。单独分配CPU即表明进程可调度cpu范围。cpu按不同的芯片,大小核数目和频率都有差别,大核频率高处理速度相对比小核快,而Android系统实际上还是响应优先于吞吐的交互型系统,因此Android AMS对进程管理于不同优先级的进程在调度cpu限制上会做有一些策略,以保证更好的交互响应。
二、进程的cpuset设置
还是回到AMS中与adj相关的有三个方法,这三个方法值得看一万遍,每一遍都会有新收获:
- updateOomAdjLocked:在进程组件生命周期变化时更新adj,然后分别执行以下两个方法
- computeOomAdjLocked:计算adj
- applyOomAdjLocked:应用adj
聚焦到computeOomAdjLocked方法,该方法主要是根据进程的四大组件状态决定当前进程的adj优先级。
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
private final int computeOomAdjLocked(ProcessRecord app, int cachedAdj, ProcessRecord TOP_APP, boolean doingAll, long now) {
...
if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) {
...
if (app == TOP_APP) {
… //以top app为例
app.curSchedGroup = ProcessList.SCHED_GROUP_TOP_APP;
...
}
...
}
以TOP_APP为例,这里ProcessRecord 的curSchedGroup属性对应的是cup调度组,而在后续applyOomAdjLocked中会执行Process的setProcessGroup方法。
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
import static android.os.Process.setProcessGroup;
private final boolean applyOomAdjLocked(ProcessRecord app, boolean doingAll, long now,
long nowElapsed) {
...
setProcessGroup(app.pid, processGroup);
...
}
调用Process的setProcessGroup方法
frameworks/base/core/java/android/os/Process.java
public static final int THREAD_GROUP_DEFAULT = -1;
public static final int THREAD_GROUP_BG_NONINTERACTIVE = 0;
private static final int THREAD_GROUP_FOREGROUND = 1;
public static final int THREAD_GROUP_SYSTEM = 2;
public static final int THREAD_GROUP_AUDIO_APP = 3;
public static final int THREAD_GROUP_AUDIO_SYS = 4;
public static final int THREAD_GROUP_TOP_APP = 5;
...
public static final native void setProcessGroup(int pid, int group)
throws IllegalArgumentException, SecurityException;
setProcessGroup是个native方法,并且这里分了若干类型的group,这里看top app优先级是最高的。接着jni到native
frameworks/base/core/jni/android_util_Process.cpp
void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
{
...
if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) {
signalExceptionForGroupError(env, EINVAL, pid);
return;
}
bool isDefault = false;
if (grp < 0) {
grp = SP_FOREGROUND; //SP_FOREGROUND = 1
isDefault = true;
}
SchedPolicy sp = (SchedPolicy) grp;
...
if (cpusets_enabled()) {
// set both cpuset and cgroup for general threads
err = set_cpuset_policy(t_pid, sp);
...
}
...
}
这里直接调用sched_policy.cpp的set_cpuset_policy,并传入对应的pid和SchedPolicy
system/core/libcutils/sched_policy.cpp
int set_cpuset_policy(int tid, SchedPolicy policy)
{
// in the absence of cpusets, use the old sched policy
if (!cpusets_enabled()) {//这里要注意,如果cpusets_enabled为false的话,会走set_sched_policy
return set_sched_policy(tid, policy);
}
if (tid == 0) {
tid = gettid();
}
policy = _policy(policy);
pthread_once(&the_once, __initialize);
int fd = -1;
int boost_fd = -1;
switch (policy) {
case SP_BACKGROUND:
fd = bg_cpuset_fd;
boost_fd = bg_schedboost_fd;
break;
case SP_FOREGROUND:
case SP_AUDIO_APP:
case SP_AUDIO_SYS:
fd = fg_cpuset_fd;
boost_fd = fg_schedboost_fd;
break;
case SP_TOP_APP :
fd = ta_cpuset_fd;
boost_fd = ta_schedboost_fd;
break;
case SP_SYSTEM:
fd = system_bg_cpuset_fd;
break;
default:
boost_fd = fd = -1;
break;
}
if (add_tid_to_cgroup(tid, fd) != 0) {
if (errno != ESRCH && errno != ENOENT)
return -errno;
}
if (schedboost_enabled()) {
if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) {
if (errno != ESRCH && errno != ENOENT)
return -errno;
}
}
return 0;
}
这里主要就是通过policy对应具体的fd句柄,然后通过add_tid_to_cgroup()写cpuset对应节点。这里要注意,如果cpusets_enabled为false的话,会走set_sched_policy,这部分下篇会讲到。
看看对应的fd是什么:
static void __initialize() {
const char* filename;
if (cpusets_enabled()) {
if (!access("/dev/cpuset/tasks", W_OK)) {
filename = "/dev/cpuset/foreground/tasks”; //这里对应的是cpuset路径
fg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
filename = "/dev/cpuset/background/tasks";
bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
filename = "/dev/cpuset/system-background/tasks";
system_bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
filename = "/dev/cpuset/top-app/tasks";
ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
}
}
...
}
那我们来看看对应节点是什么内容:
/dev/cpuset # ls -al
drwxr-xr-x 2 system system 0 1970-05-22 03:35 background
drwxr-xr-x 2 system system 0 1970-05-22 03:35 foreground
drwxr-xr-x 2 system system 0 1970-05-22 03:35 top-app
然后看看对应的cpuset配置:
/dev/cpuset # cat top-app/cpus
0-7
/dev/cpuset # cat foreground/cpus
0-2,4-7
/dev/cpuset # cat background/cpus
0-2
显然,top app 满核随便跑,foreground跑在除了3这个核以外的所有核上, 而background只能跑在小核上。
三、cpuset配置点
不同芯片平台配置会有差别。
# cpuset parameters
echo 0-2 > /dev/cpuset/background/cpus
echo 0-2,4-7 > /dev/cpuset/foreground/cpus
echo 0-7 > /dev/cpuset/top-app/cpus
四、cpuset开关
kernel/msm-4.14/arch/arm64/configs/XXXX
CONFIG_CPUSETS=y //打开cpuset
网友评论