Android CPU核分配关联进程
系统在应用创建或者退出时,都会进行调整分类,根据分类不同,对应可以运行的CPU核心也有所不同。在 /dev/cpuset 路径下,可以看到相关分类
:/ # cd /dev/cpuset
:/dev/cpuset # ls
audio-app cgroup.procs effective_cpus mem_hardwall memory_spread_page release_agent system-background
background cgroup.sane_behavior effective_mems memory_migrate memory_spread_slab restricted tasks
camera-daemon cpu_exclusive foreground memory_pressure mems sched_load_balance top-app
cgroup.clone_children cpus mem_exclusive memory_pressure_enabled notify_on_release sched_relax_domain_level
常见的可能就是 background foreground top-app ,其中top-app优先级就是最高的一类,top-app默认可以运行在所有CPU核上面。
:/dev/cpuset # cat background/cpus
0-3
:/dev/cpuset # cat system-background/cpus
0-3
:/dev/cpuset # cat foreground/cpus
0-7
:/dev/cpuset # cat top-app/cpus
0-7
默认分配如上,后台的进程只会运行在0-3(小核)上面,foreground top-app运行在0-7,由系统自行调度大小核。如果再激进点,可以只分配(echo 4-7)大核给它,无论什么情形下都执行在大核上面,对应用性能有一点提升。
write /dev/cpuset/top-app/cpus 4-7
进程归纳为什么进程可以在创建或者调整中强制更改。例如如果是系统bin文件so库服务,可以在定义中配置 writepid 如下
service media.swcodec /apex/com.android.media.swcodec/bin/mediaswcodec
class main
user mediacodec
group camera drmrpc mediadrm
ioprio rt 4
writepid /dev/cpuset/foreground/tasks
如果是应用类型的,系统会通过 OomAdjuster.java 动态调整应用的优先级同时会改变应用的分类。可以通过包名判断,调整为TOP APP等类型。如下
@GuardedBy({"mService", "mProcLock"})
private boolean computeOomAdjLSP(ProcessRecord app, int cachedAdj,
ProcessRecord topApp, boolean doingAll, long now, boolean cycleReEval,
boolean computeClients) {
final ProcessStateRecord state = app.mState;
if (state.getMaxAdj() <= FOREGROUND_APP_ADJ) {
... ...
// 强制设置为TOP APP
if ("com.xxx.xxxxxx".equals(app.processName)) {
state.setCurrentSchedulingGroup(SCHED_GROUP_TOP_APP);
}
state.setCurRawProcState(state.getCurProcState());
state.setCurAdj(state.getMaxAdj());
state.setCompletedAdjSeq(state.getAdjSeq());
// if curAdj is less than prevAppAdj, then this process was promoted
return state.getCurAdj() < prevAppAdj || state.getCurProcState() < prevProcState;
}