106106import android.os.SystemProperties;
107107import android.provider.Settings;
108108import android.util.EventLog;
109+ import android.util.Pair;
109110import android.util.Slog;
110111import android.util.Log;
111112import android.util.PrintWriterPrinter;
@@ -5004,7 +5005,13 @@ public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
50045005 maxNum < N ? maxNum : N);
50055006 for (int i=0; i<N && maxNum > 0; i++) {
50065007 TaskRecord tr = mRecentTasks.get(i);
5007- if (((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
5008+ // Return the entry if desired by the caller. We always return
5009+ // the first entry, because callers always expect this to be the
5010+ // forground app. We may filter others if the caller has
5011+ // not supplied RECENT_WITH_EXCLUDED and there is some reason
5012+ // we should exclude the entry.
5013+ if (i == 0
5014+ || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
50085015 || (tr.intent == null)
50095016 || ((tr.intent.getFlags()
50105017 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
@@ -7546,7 +7553,33 @@ public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
75467553
75477554 return errList;
75487555 }
7549-
7556+
7557+ static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) {
7558+ if (adj >= ProcessList.EMPTY_APP_ADJ) {
7559+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY;
7560+ } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
7561+ if (currApp != null) {
7562+ currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1;
7563+ }
7564+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
7565+ } else if (adj >= ProcessList.HOME_APP_ADJ) {
7566+ if (currApp != null) {
7567+ currApp.lru = 0;
7568+ }
7569+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
7570+ } else if (adj >= ProcessList.SECONDARY_SERVER_ADJ) {
7571+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
7572+ } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
7573+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
7574+ } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
7575+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
7576+ } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
7577+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
7578+ } else {
7579+ return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
7580+ }
7581+ }
7582+
75507583 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
75517584 // Lazy instantiation of list
75527585 List<ActivityManager.RunningAppProcessInfo> runList = null;
@@ -7567,28 +7600,12 @@ public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
75677600 currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT;
75687601 }
75697602 int adj = app.curAdj;
7570- if (adj >= ProcessList.EMPTY_APP_ADJ) {
7571- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY;
7572- } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
7573- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
7574- currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1;
7575- } else if (adj >= ProcessList.HOME_APP_ADJ) {
7576- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
7577- currApp.lru = 0;
7578- } else if (adj >= ProcessList.SECONDARY_SERVER_ADJ) {
7579- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
7580- } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
7581- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
7582- } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
7583- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
7584- } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
7585- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
7586- } else {
7587- currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
7588- }
7603+ currApp.importance = oomAdjToImportance(adj, currApp);
75897604 currApp.importanceReasonCode = app.adjTypeCode;
75907605 if (app.adjSource instanceof ProcessRecord) {
75917606 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
7607+ currApp.importanceReasonImportance = oomAdjToImportance(
7608+ app.adjSourceOom, null);
75927609 } else if (app.adjSource instanceof ActivityRecord) {
75937610 ActivityRecord r = (ActivityRecord)app.adjSource;
75947611 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
@@ -7891,7 +7908,7 @@ boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
78917908 if (mLruProcesses.size() > 0) {
78927909 if (needSep) pw.println(" ");
78937910 needSep = true;
7894- pw.println(" Process LRU list (most recent first ):");
7911+ pw.println(" Process LRU list (sorted by oom_adj ):");
78957912 dumpProcessOomList(pw, this, mLruProcesses, " ",
78967913 "Proc", "PERS", false);
78977914 needSep = true;
@@ -8069,29 +8086,6 @@ boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args,
80698086 boolean needSep = false;
80708087
80718088 if (mLruProcesses.size() > 0) {
8072- ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(mLruProcesses);
8073-
8074- Comparator<ProcessRecord> comparator = new Comparator<ProcessRecord>() {
8075- @Override
8076- public int compare(ProcessRecord object1, ProcessRecord object2) {
8077- if (object1.setAdj != object2.setAdj) {
8078- return object1.setAdj > object2.setAdj ? -1 : 1;
8079- }
8080- if (object1.setSchedGroup != object2.setSchedGroup) {
8081- return object1.setSchedGroup > object2.setSchedGroup ? -1 : 1;
8082- }
8083- if (object1.keeping != object2.keeping) {
8084- return object1.keeping ? -1 : 1;
8085- }
8086- if (object1.pid != object2.pid) {
8087- return object1.pid > object2.pid ? -1 : 1;
8088- }
8089- return 0;
8090- }
8091- };
8092-
8093- Collections.sort(procs, comparator);
8094-
80958089 if (needSep) pw.println(" ");
80968090 needSep = true;
80978091 pw.println(" OOM levels:");
@@ -8110,7 +8104,7 @@ public int compare(ProcessRecord object1, ProcessRecord object2) {
81108104 if (needSep) pw.println(" ");
81118105 needSep = true;
81128106 pw.println(" Process OOM control:");
8113- dumpProcessOomList(pw, this, procs , " ",
8107+ dumpProcessOomList(pw, this, mLruProcesses , " ",
81148108 "Proc", "PERS", true);
81158109 needSep = true;
81168110 }
@@ -8859,18 +8853,41 @@ private static final int dumpProcessList(PrintWriter pw,
88598853 }
88608854
88618855 private static final void dumpProcessOomList(PrintWriter pw,
8862- ActivityManagerService service, List<ProcessRecord> list ,
8856+ ActivityManagerService service, List<ProcessRecord> origList ,
88638857 String prefix, String normalLabel, String persistentLabel,
88648858 boolean inclDetails) {
88658859
8860+ ArrayList<Pair<ProcessRecord, Integer>> list
8861+ = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
8862+ for (int i=0; i<origList.size(); i++) {
8863+ list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
8864+ }
8865+
8866+ Comparator<Pair<ProcessRecord, Integer>> comparator
8867+ = new Comparator<Pair<ProcessRecord, Integer>>() {
8868+ @Override
8869+ public int compare(Pair<ProcessRecord, Integer> object1,
8870+ Pair<ProcessRecord, Integer> object2) {
8871+ if (object1.first.setAdj != object2.first.setAdj) {
8872+ return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
8873+ }
8874+ if (object1.second.intValue() != object2.second.intValue()) {
8875+ return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
8876+ }
8877+ return 0;
8878+ }
8879+ };
8880+
8881+ Collections.sort(list, comparator);
8882+
88668883 final long curRealtime = SystemClock.elapsedRealtime();
88678884 final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
88688885 final long curUptime = SystemClock.uptimeMillis();
88698886 final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
88708887
88718888 final int N = list.size()-1;
88728889 for (int i=N; i>=0; i--) {
8873- ProcessRecord r = list.get(i);
8890+ ProcessRecord r = list.get(i).first ;
88748891 String oomAdj;
88758892 if (r.setAdj >= ProcessList.EMPTY_APP_ADJ) {
88768893 oomAdj = buildOomTag("empty", null, r.setAdj, ProcessList.EMPTY_APP_ADJ);
@@ -8919,7 +8936,7 @@ private static final void dumpProcessOomList(PrintWriter pw,
89198936 }
89208937 pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)",
89218938 prefix, (r.persistent ? persistentLabel : normalLabel),
8922- N-i , oomAdj, schedGroup, foreground, r.trimMemoryLevel,
8939+ N-list.get(i).second , oomAdj, schedGroup, foreground, r.trimMemoryLevel,
89238940 r.toShortString(), r.adjType));
89248941 if (r.adjSource != null || r.adjTarget != null) {
89258942 pw.print(prefix);
@@ -13118,6 +13135,7 @@ private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
1311813135 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
1311913136 .REASON_SERVICE_IN_USE;
1312013137 app.adjSource = cr.binding.client;
13138+ app.adjSourceOom = clientAdj;
1312113139 app.adjTarget = s.name;
1312213140 }
1312313141 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
@@ -13140,6 +13158,7 @@ private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
1314013158 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
1314113159 .REASON_SERVICE_IN_USE;
1314213160 app.adjSource = a;
13161+ app.adjSourceOom = adj;
1314313162 app.adjTarget = s.name;
1314413163 }
1314513164 }
@@ -13201,6 +13220,7 @@ private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
1320113220 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
1320213221 .REASON_PROVIDER_IN_USE;
1320313222 app.adjSource = client;
13223+ app.adjSourceOom = clientAdj;
1320413224 app.adjTarget = cpr.name;
1320513225 }
1320613226 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
@@ -13511,16 +13531,21 @@ private final boolean updateOomAdjLocked(
1351113531 computeOomAdjLocked(app, hiddenAdj, TOP_APP, false);
1351213532
1351313533 if (app.curRawAdj != app.setRawAdj) {
13514- if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ
13515- && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) {
13516- // If this app is transitioning from foreground to
13517- // non-foreground, have it do a gc.
13518- scheduleAppGcLocked(app);
13519- } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
13520- && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) {
13521- // Likewise do a gc when an app is moving in to the
13522- // background (such as a service stopping).
13523- scheduleAppGcLocked(app);
13534+ if (false) {
13535+ // Removing for now. Forcing GCs is not so useful anymore
13536+ // with Dalvik, and the new memory level hint facility is
13537+ // better for what we need to do these days.
13538+ if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ
13539+ && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) {
13540+ // If this app is transitioning from foreground to
13541+ // non-foreground, have it do a gc.
13542+ scheduleAppGcLocked(app);
13543+ } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
13544+ && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) {
13545+ // Likewise do a gc when an app is moving in to the
13546+ // background (such as a service stopping).
13547+ scheduleAppGcLocked(app);
13548+ }
1352413549 }
1352513550
1352613551 if (wasKeeping && !app.keeping) {
0 commit comments