// 计算adj,返回计算后RawAdj值,和applyOomAdjLocked一起在updateOomAdjLocked中调用 privatefinalintcomputeOomAdjLocked(ProcessRecord app, int cachedAdj, ProcessRecord TOP_APP, boolean doingAll, long now) { ... ... // service情况 booleanmayBeTop=false; // 是否显示在最顶部
// 当adj > 0或schedGroup为后台进程组或procState > 2时执行 for (intis= app.services.size()-1; is >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ || schedGroup == ProcessList.SCHED_GROUP_BACKGROUND || procState > ActivityManager.PROCESS_STATE_TOP); is--) { ServiceRecords= app.services.valueAt(is); ... ... for (intconni= s.connections.size()-1; conni >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ || schedGroup == ProcessList.SCHED_GROUP_BACKGROUND || procState > ActivityManager.PROCESS_STATE_TOP); conni--) { // 获取service所绑定的connections ArrayList<ConnectionRecord> clist = s.connections.valueAt(conni); for (inti=0; i < clist.size() && (adj > ProcessList.FOREGROUND_APP_ADJ || schedGroup == ProcessList.SCHED_GROUP_BACKGROUND || procState > ActivityManager.PROCESS_STATE_TOP); i++) { // XXX should compute this based on the max of // all connected clients. ConnectionRecordcr= clist.get(i); if (cr.binding.client == app) { // Binding to ourself is not interesting. // 当client与当前app同一进程,则continue continue; }
if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { ProcessRecordclient= cr.binding.client; // 计算connections所对应的client进程的adj intclientAdj= computeOomAdjLocked(client, cachedAdj, TOP_APP, doingAll, now); intclientProcState= client.curProcState; // client进程的状态 ... ... // 当绑定的是前台进程的情况 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { // This will treat important bound services identically to // the top app, which may behave differently than generic // foreground work. if (client.curSchedGroup > schedGroup) { if ((cr.flags&Context.BIND_IMPORTANT) != 0) { schedGroup = client.curSchedGroup; } else { schedGroup = ProcessList.SCHED_GROUP_DEFAULT; } } if (clientProcState <= ActivityManager.PROCESS_STATE_TOP) { if (clientProcState == ActivityManager.PROCESS_STATE_TOP) { // Special handling of clients who are in the top state. // We *may* want to consider this process to be in the // top state as well, but only if there is not another // reason for it to be running. Being on the top is a // special state, meaning you are specifically running // for the current top app. If the process is already // running in the background for some other reason, it // is more important to continue considering it to be // in the background state. // 当client进程状态为前台时,则设置mayBeTop=true,并设置client进程procState=16 mayBeTop = true; clientProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; } else { // 当client进程状态 < 2的前提下:若绑定前台service,则clientProcState=3;否则clientProcState=6 ... ... } } ... ...
// 当mayBeTop为true,且procState > 2时 if (mayBeTop && procState > ActivityManager.PROCESS_STATE_TOP) { // A client of one of our services or providers is in the top state. We // *may* want to be in the top state, but not if we are already running in // the background for some other reason. For the decision here, we are going // to pick out a few specific states that we want to remain in when a client // is top (states that tend to be longer-term) and otherwise allow it to go // to the top state. switch (procState) { case ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND: case ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND: case ActivityManager.PROCESS_STATE_SERVICE: // 对于procState = 6, 7, 10时,将procState设置为3 // These all are longer-term states, so pull them up to the top // of the background states, but not all the way to the top state. procState = ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; break; default: // Otherwise, top is a better choice, so take it. procState = ActivityManager.PROCESS_STATE_TOP; break; } }
分析到这里就找到了为什么淘宝进程最后的状态为3了,在调试的过程中发现打开淘宝后会启动一个service:09-05 17:41:27.474 1486 21013 I ActivityManager: Start proc 5692:com.taobao.taobao:channel/u0a198 for service com.taobao.taobao/com.alibaba.analytics.AnalyticsService caller=com.taobao.taobao ,就是这个service在computeOomAdjLocked所绑定的connections所对应的client进程为前台进程,通过在if (cr.binding.client == app)中添加log发现client为ProcessRecord{ffadbcb 21266:com.taobao.taobao/u0a88},app为ProcessRecord{7a416b8 21766:com.taobao.taobao:channel/u0a88},所以出现问题时client为com.taobao.taobao,app为com.taobao.taobao:channel,最终系统把mayBeTop设置为了true,当procState > 2时,就会调整adj,最终把procState为3,而在computeOomAdjLocked的最后,把proState的值赋给了app.curProcState,最终淘宝的state就为3了。