• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 package com.android.server.am;
18 
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 
22 import android.app.ActivityManager;
23 import com.android.internal.util.MemInfoReader;
24 import com.android.server.wm.WindowManagerService;
25 
26 import android.content.res.Resources;
27 import android.graphics.Point;
28 import android.os.SystemProperties;
29 import android.util.Slog;
30 import android.view.Display;
31 
32 /**
33  * Activity manager code dealing with processes.
34  */
35 final class ProcessList {
36     // The minimum time we allow between crashes, for us to consider this
37     // application to be bad and stop and its services and reject broadcasts.
38     static final int MIN_CRASH_INTERVAL = 60*1000;
39 
40     // OOM adjustments for processes in various states:
41 
42     // Adjustment used in certain places where we don't know it yet.
43     // (Generally this is something that is going to be cached, but we
44     // don't know the exact value in the cached range to assign yet.)
45     static final int UNKNOWN_ADJ = 16;
46 
47     // This is a process only hosting activities that are not visible,
48     // so it can be killed without any disruption.
49     static final int CACHED_APP_MAX_ADJ = 15;
50     static final int CACHED_APP_MIN_ADJ = 9;
51 
52     // The B list of SERVICE_ADJ -- these are the old and decrepit
53     // services that aren't as shiny and interesting as the ones in the A list.
54     static final int SERVICE_B_ADJ = 8;
55 
56     // This is the process of the previous application that the user was in.
57     // This process is kept above other things, because it is very common to
58     // switch back to the previous app.  This is important both for recent
59     // task switch (toggling between the two top recent apps) as well as normal
60     // UI flow such as clicking on a URI in the e-mail app to view in the browser,
61     // and then pressing back to return to e-mail.
62     static final int PREVIOUS_APP_ADJ = 7;
63 
64     // This is a process holding the home application -- we want to try
65     // avoiding killing it, even if it would normally be in the background,
66     // because the user interacts with it so much.
67     static final int HOME_APP_ADJ = 6;
68 
69     // This is a process holding an application service -- killing it will not
70     // have much of an impact as far as the user is concerned.
71     static final int SERVICE_ADJ = 5;
72 
73     // This is a process with a heavy-weight application.  It is in the
74     // background, but we want to try to avoid killing it.  Value set in
75     // system/rootdir/init.rc on startup.
76     static final int HEAVY_WEIGHT_APP_ADJ = 4;
77 
78     // This is a process currently hosting a backup operation.  Killing it
79     // is not entirely fatal but is generally a bad idea.
80     static final int BACKUP_APP_ADJ = 3;
81 
82     // This is a process only hosting components that are perceptible to the
83     // user, and we really want to avoid killing them, but they are not
84     // immediately visible. An example is background music playback.
85     static final int PERCEPTIBLE_APP_ADJ = 2;
86 
87     // This is a process only hosting activities that are visible to the
88     // user, so we'd prefer they don't disappear.
89     static final int VISIBLE_APP_ADJ = 1;
90 
91     // This is the process running the current foreground app.  We'd really
92     // rather not kill it!
93     static final int FOREGROUND_APP_ADJ = 0;
94 
95     // This is a system persistent process, such as telephony.  Definitely
96     // don't want to kill it, but doing so is not completely fatal.
97     static final int PERSISTENT_PROC_ADJ = -12;
98 
99     // The system process runs at the default adjustment.
100     static final int SYSTEM_ADJ = -16;
101 
102     // Special code for native processes that are not being managed by the system (so
103     // don't have an oom adj assigned by the system).
104     static final int NATIVE_ADJ = -17;
105 
106     // Memory pages are 4K.
107     static final int PAGE_SIZE = 4*1024;
108 
109     // The minimum number of cached apps we want to be able to keep around,
110     // without empty apps being able to push them out of memory.
111     static final int MIN_CACHED_APPS = 2;
112 
113     // The maximum number of cached processes we will keep around before killing them.
114     // NOTE: this constant is *only* a control to not let us go too crazy with
115     // keeping around processes on devices with large amounts of RAM.  For devices that
116     // are tighter on RAM, the out of memory killer is responsible for killing background
117     // processes as RAM is needed, and we should *never* be relying on this limit to
118     // kill them.  Also note that this limit only applies to cached background processes;
119     // we have no limit on the number of service, visible, foreground, or other such
120     // processes and the number of those processes does not count against the cached
121     // process limit.
122     static final int MAX_CACHED_APPS = 24;
123 
124     // We allow empty processes to stick around for at most 30 minutes.
125     static final long MAX_EMPTY_TIME = 30*60*1000;
126 
127     // The maximum number of empty app processes we will let sit around.
128     private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS);
129 
130     // The number of empty apps at which we don't consider it necessary to do
131     // memory trimming.
132     static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2;
133 
134     // The number of cached at which we don't consider it necessary to do
135     // memory trimming.
136     static final int TRIM_CACHED_APPS = ((MAX_CACHED_APPS-MAX_EMPTY_APPS)*2)/3;
137 
138     // Threshold of number of cached+empty where we consider memory critical.
139     static final int TRIM_CRITICAL_THRESHOLD = 3;
140 
141     // Threshold of number of cached+empty where we consider memory critical.
142     static final int TRIM_LOW_THRESHOLD = 5;
143 
144     // These are the various interesting memory levels that we will give to
145     // the OOM killer.  Note that the OOM killer only supports 6 slots, so we
146     // can't give it a different value for every possible kind of process.
147     private final int[] mOomAdj = new int[] {
148             FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ,
149             BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ
150     };
151     // These are the low-end OOM level limits.  This is appropriate for an
152     // HVGA or smaller phone with less than 512MB.  Values are in KB.
153     private final long[] mOomMinFreeLow = new long[] {
154             8192, 12288, 16384,
155             24576, 28672, 32768
156     };
157     // These are the high-end OOM level limits.  This is appropriate for a
158     // 1280x800 or larger screen with around 1GB RAM.  Values are in KB.
159     private final long[] mOomMinFreeHigh = new long[] {
160             49152, 61440, 73728,
161             86016, 98304, 122880
162     };
163     // The actual OOM killer memory levels we are using.
164     private final long[] mOomMinFree = new long[mOomAdj.length];
165 
166     private final long mTotalMemMb;
167 
168     private long mCachedRestoreLevel;
169 
170     private boolean mHaveDisplaySize;
171 
ProcessList()172     ProcessList() {
173         MemInfoReader minfo = new MemInfoReader();
174         minfo.readMemInfo();
175         mTotalMemMb = minfo.getTotalSize()/(1024*1024);
176         updateOomLevels(0, 0, false);
177     }
178 
applyDisplaySize(WindowManagerService wm)179     void applyDisplaySize(WindowManagerService wm) {
180         if (!mHaveDisplaySize) {
181             Point p = new Point();
182             wm.getBaseDisplaySize(Display.DEFAULT_DISPLAY, p);
183             if (p.x != 0 && p.y != 0) {
184                 updateOomLevels(p.x, p.y, true);
185                 mHaveDisplaySize = true;
186             }
187         }
188     }
189 
updateOomLevels(int displayWidth, int displayHeight, boolean write)190     private void updateOomLevels(int displayWidth, int displayHeight, boolean write) {
191         // Scale buckets from avail memory: at 300MB we use the lowest values to
192         // 700MB or more for the top values.
193         float scaleMem = ((float)(mTotalMemMb-300))/(700-300);
194 
195         // Scale buckets from screen size.
196         int minSize = 480*800;  //  384000
197         int maxSize = 1280*800; // 1024000  230400 870400  .264
198         float scaleDisp = ((float)(displayWidth*displayHeight)-minSize)/(maxSize-minSize);
199         if (false) {
200             Slog.i("XXXXXX", "scaleMem=" + scaleMem);
201             Slog.i("XXXXXX", "scaleDisp=" + scaleDisp + " dw=" + displayWidth
202                     + " dh=" + displayHeight);
203         }
204 
205         StringBuilder adjString = new StringBuilder();
206         StringBuilder memString = new StringBuilder();
207 
208         float scale = scaleMem > scaleDisp ? scaleMem : scaleDisp;
209         if (scale < 0) scale = 0;
210         else if (scale > 1) scale = 1;
211         int minfree_adj = Resources.getSystem().getInteger(
212                 com.android.internal.R.integer.config_lowMemoryKillerMinFreeKbytesAdjust);
213         int minfree_abs = Resources.getSystem().getInteger(
214                 com.android.internal.R.integer.config_lowMemoryKillerMinFreeKbytesAbsolute);
215         if (false) {
216             Slog.i("XXXXXX", "minfree_adj=" + minfree_adj + " minfree_abs=" + minfree_abs);
217         }
218 
219         for (int i=0; i<mOomAdj.length; i++) {
220             long low = mOomMinFreeLow[i];
221             long high = mOomMinFreeHigh[i];
222             mOomMinFree[i] = (long)(low + ((high-low)*scale));
223         }
224 
225         if (minfree_abs >= 0) {
226             for (int i=0; i<mOomAdj.length; i++) {
227                 mOomMinFree[i] = (long)((float)minfree_abs * mOomMinFree[i] / mOomMinFree[mOomAdj.length - 1]);
228             }
229         }
230 
231         if (minfree_adj != 0) {
232             for (int i=0; i<mOomAdj.length; i++) {
233                 mOomMinFree[i] += (long)((float)minfree_adj * mOomMinFree[i] / mOomMinFree[mOomAdj.length - 1]);
234                 if (mOomMinFree[i] < 0) {
235                     mOomMinFree[i] = 0;
236                 }
237             }
238         }
239 
240         // The maximum size we will restore a process from cached to background, when under
241         // memory duress, is 1/3 the size we have reserved for kernel caches and other overhead
242         // before killing background processes.
243         mCachedRestoreLevel = (getMemLevel(ProcessList.CACHED_APP_MAX_ADJ)/1024) / 3;
244 
245         for (int i=0; i<mOomAdj.length; i++) {
246             if (i > 0) {
247                 adjString.append(',');
248                 memString.append(',');
249             }
250             adjString.append(mOomAdj[i]);
251             memString.append((mOomMinFree[i]*1024)/PAGE_SIZE);
252         }
253 
254         // Ask the kernel to try to keep enough memory free to allocate 3 full
255         // screen 32bpp buffers without entering direct reclaim.
256         int reserve = displayWidth * displayHeight * 4 * 3 / 1024;
257         int reserve_adj = Resources.getSystem().getInteger(com.android.internal.R.integer.config_extraFreeKbytesAdjust);
258         int reserve_abs = Resources.getSystem().getInteger(com.android.internal.R.integer.config_extraFreeKbytesAbsolute);
259 
260         if (reserve_abs >= 0) {
261             reserve = reserve_abs;
262         }
263 
264         if (reserve_adj != 0) {
265             reserve += reserve_adj;
266             if (reserve < 0) {
267                 reserve = 0;
268             }
269         }
270 
271         //Slog.i("XXXXXXX", "******************************* MINFREE: " + memString);
272         if (write) {
273             writeFile("/sys/module/lowmemorykiller/parameters/adj", adjString.toString());
274             writeFile("/sys/module/lowmemorykiller/parameters/minfree", memString.toString());
275             SystemProperties.set("sys.sysctl.extra_free_kbytes", Integer.toString(reserve));
276         }
277         // GB: 2048,3072,4096,6144,7168,8192
278         // HC: 8192,10240,12288,14336,16384,20480
279     }
280 
computeEmptyProcessLimit(int totalProcessLimit)281     public static int computeEmptyProcessLimit(int totalProcessLimit) {
282         return (totalProcessLimit*2)/3;
283     }
284 
buildOomTag(String prefix, String space, int val, int base)285     private static String buildOomTag(String prefix, String space, int val, int base) {
286         if (val == base) {
287             if (space == null) return prefix;
288             return prefix + "  ";
289         }
290         return prefix + "+" + Integer.toString(val-base);
291     }
292 
makeOomAdjString(int setAdj)293     public static String makeOomAdjString(int setAdj) {
294         if (setAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
295             return buildOomTag("cch", "  ", setAdj, ProcessList.CACHED_APP_MIN_ADJ);
296         } else if (setAdj >= ProcessList.SERVICE_B_ADJ) {
297             return buildOomTag("svcb ", null, setAdj, ProcessList.SERVICE_B_ADJ);
298         } else if (setAdj >= ProcessList.PREVIOUS_APP_ADJ) {
299             return buildOomTag("prev ", null, setAdj, ProcessList.PREVIOUS_APP_ADJ);
300         } else if (setAdj >= ProcessList.HOME_APP_ADJ) {
301             return buildOomTag("home ", null, setAdj, ProcessList.HOME_APP_ADJ);
302         } else if (setAdj >= ProcessList.SERVICE_ADJ) {
303             return buildOomTag("svc  ", null, setAdj, ProcessList.SERVICE_ADJ);
304         } else if (setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
305             return buildOomTag("hvy  ", null, setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ);
306         } else if (setAdj >= ProcessList.BACKUP_APP_ADJ) {
307             return buildOomTag("bkup ", null, setAdj, ProcessList.BACKUP_APP_ADJ);
308         } else if (setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
309             return buildOomTag("prcp ", null, setAdj, ProcessList.PERCEPTIBLE_APP_ADJ);
310         } else if (setAdj >= ProcessList.VISIBLE_APP_ADJ) {
311             return buildOomTag("vis  ", null, setAdj, ProcessList.VISIBLE_APP_ADJ);
312         } else if (setAdj >= ProcessList.FOREGROUND_APP_ADJ) {
313             return buildOomTag("fore ", null, setAdj, ProcessList.FOREGROUND_APP_ADJ);
314         } else if (setAdj >= ProcessList.PERSISTENT_PROC_ADJ) {
315             return buildOomTag("pers ", null, setAdj, ProcessList.PERSISTENT_PROC_ADJ);
316         } else if (setAdj >= ProcessList.SYSTEM_ADJ) {
317             return buildOomTag("sys  ", null, setAdj, ProcessList.SYSTEM_ADJ);
318         } else if (setAdj >= ProcessList.NATIVE_ADJ) {
319             return buildOomTag("ntv  ", null, setAdj, ProcessList.NATIVE_ADJ);
320         } else {
321             return Integer.toString(setAdj);
322         }
323     }
324 
makeProcStateString(int curProcState)325     public static String makeProcStateString(int curProcState) {
326         String procState;
327         switch (curProcState) {
328             case -1:
329                 procState = "N ";
330                 break;
331             case ActivityManager.PROCESS_STATE_PERSISTENT:
332                 procState = "P ";
333                 break;
334             case ActivityManager.PROCESS_STATE_PERSISTENT_UI:
335                 procState = "PU";
336                 break;
337             case ActivityManager.PROCESS_STATE_TOP:
338                 procState = "T ";
339                 break;
340             case ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND:
341                 procState = "IF";
342                 break;
343             case ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND:
344                 procState = "IB";
345                 break;
346             case ActivityManager.PROCESS_STATE_BACKUP:
347                 procState = "BU";
348                 break;
349             case ActivityManager.PROCESS_STATE_HEAVY_WEIGHT:
350                 procState = "HW";
351                 break;
352             case ActivityManager.PROCESS_STATE_SERVICE:
353                 procState = "S ";
354                 break;
355             case ActivityManager.PROCESS_STATE_RECEIVER:
356                 procState = "R ";
357                 break;
358             case ActivityManager.PROCESS_STATE_HOME:
359                 procState = "HO";
360                 break;
361             case ActivityManager.PROCESS_STATE_LAST_ACTIVITY:
362                 procState = "LA";
363                 break;
364             case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY:
365                 procState = "CA";
366                 break;
367             case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
368                 procState = "Ca";
369                 break;
370             case ActivityManager.PROCESS_STATE_CACHED_EMPTY:
371                 procState = "CE";
372                 break;
373             default:
374                 procState = "??";
375                 break;
376         }
377         return procState;
378     }
379 
appendRamKb(StringBuilder sb, long ramKb)380     public static void appendRamKb(StringBuilder sb, long ramKb) {
381         for (int j=0, fact=10; j<6; j++, fact*=10) {
382             if (ramKb < fact) {
383                 sb.append(' ');
384             }
385         }
386         sb.append(ramKb);
387     }
388 
389     // The minimum amount of time after a state change it is safe ro collect PSS.
390     public static final int PSS_MIN_TIME_FROM_STATE_CHANGE = 15*1000;
391 
392     // The maximum amount of time we want to go between PSS collections.
393     public static final int PSS_MAX_INTERVAL = 30*60*1000;
394 
395     // The minimum amount of time between successive PSS requests for *all* processes.
396     public static final int PSS_ALL_INTERVAL = 10*60*1000;
397 
398     // The minimum amount of time between successive PSS requests for a process.
399     private static final int PSS_SHORT_INTERVAL = 2*60*1000;
400 
401     // The amount of time until PSS when a process first becomes top.
402     private static final int PSS_FIRST_TOP_INTERVAL = 10*1000;
403 
404     // The amount of time until PSS when a process first goes into the background.
405     private static final int PSS_FIRST_BACKGROUND_INTERVAL = 20*1000;
406 
407     // The amount of time until PSS when a process first becomes cached.
408     private static final int PSS_FIRST_CACHED_INTERVAL = 30*1000;
409 
410     // The amount of time until PSS when an important process stays in the same state.
411     private static final int PSS_SAME_IMPORTANT_INTERVAL = 15*60*1000;
412 
413     // The amount of time until PSS when a service process stays in the same state.
414     private static final int PSS_SAME_SERVICE_INTERVAL = 20*60*1000;
415 
416     // The amount of time until PSS when a cached process stays in the same state.
417     private static final int PSS_SAME_CACHED_INTERVAL = 30*60*1000;
418 
419     public static final int PROC_MEM_PERSISTENT = 0;
420     public static final int PROC_MEM_TOP = 1;
421     public static final int PROC_MEM_IMPORTANT = 2;
422     public static final int PROC_MEM_SERVICE = 3;
423     public static final int PROC_MEM_CACHED = 4;
424 
425     private static final int[] sProcStateToProcMem = new int[] {
426         PROC_MEM_PERSISTENT,            // ActivityManager.PROCESS_STATE_PERSISTENT
427         PROC_MEM_PERSISTENT,            // ActivityManager.PROCESS_STATE_PERSISTENT_UI
428         PROC_MEM_TOP,                   // ActivityManager.PROCESS_STATE_TOP
429         PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
430         PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
431         PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_BACKUP
432         PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
433         PROC_MEM_SERVICE,               // ActivityManager.PROCESS_STATE_SERVICE
434         PROC_MEM_CACHED,                // ActivityManager.PROCESS_STATE_RECEIVER
435         PROC_MEM_CACHED,                // ActivityManager.PROCESS_STATE_HOME
436         PROC_MEM_CACHED,                // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
437         PROC_MEM_CACHED,                // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY
438         PROC_MEM_CACHED,                // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT
439         PROC_MEM_CACHED,                // ActivityManager.PROCESS_STATE_CACHED_EMPTY
440     };
441 
442     private static final long[] sFirstAwakePssTimes = new long[] {
443         PSS_SHORT_INTERVAL,             // ActivityManager.PROCESS_STATE_PERSISTENT
444         PSS_SHORT_INTERVAL,             // ActivityManager.PROCESS_STATE_PERSISTENT_UI
445         PSS_FIRST_TOP_INTERVAL,         // ActivityManager.PROCESS_STATE_TOP
446         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
447         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
448         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_BACKUP
449         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
450         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_SERVICE
451         PSS_FIRST_CACHED_INTERVAL,      // ActivityManager.PROCESS_STATE_RECEIVER
452         PSS_FIRST_CACHED_INTERVAL,      // ActivityManager.PROCESS_STATE_HOME
453         PSS_FIRST_CACHED_INTERVAL,      // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
454         PSS_FIRST_CACHED_INTERVAL,      // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY
455         PSS_FIRST_CACHED_INTERVAL,      // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT
456         PSS_FIRST_CACHED_INTERVAL,      // ActivityManager.PROCESS_STATE_CACHED_EMPTY
457     };
458 
459     private static final long[] sSameAwakePssTimes = new long[] {
460         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_PERSISTENT
461         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_PERSISTENT_UI
462         PSS_SHORT_INTERVAL,             // ActivityManager.PROCESS_STATE_TOP
463         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
464         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
465         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_BACKUP
466         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
467         PSS_SAME_SERVICE_INTERVAL,      // ActivityManager.PROCESS_STATE_SERVICE
468         PSS_SAME_SERVICE_INTERVAL,      // ActivityManager.PROCESS_STATE_RECEIVER
469         PSS_SAME_CACHED_INTERVAL,       // ActivityManager.PROCESS_STATE_HOME
470         PSS_SAME_CACHED_INTERVAL,       // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
471         PSS_SAME_CACHED_INTERVAL,       // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY
472         PSS_SAME_CACHED_INTERVAL,       // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT
473         PSS_SAME_CACHED_INTERVAL,       // ActivityManager.PROCESS_STATE_CACHED_EMPTY
474     };
475 
procStatesDifferForMem(int procState1, int procState2)476     public static boolean procStatesDifferForMem(int procState1, int procState2) {
477         return sProcStateToProcMem[procState1] != sProcStateToProcMem[procState2];
478     }
479 
computeNextPssTime(int procState, boolean first, boolean sleeping, long now)480     public static long computeNextPssTime(int procState, boolean first, boolean sleeping,
481             long now) {
482         final long[] table = sleeping
483                 ? (first
484                         ? sFirstAwakePssTimes
485                         : sSameAwakePssTimes)
486                 : (first
487                         ? sFirstAwakePssTimes
488                         : sSameAwakePssTimes);
489         return now + table[procState];
490     }
491 
getMemLevel(int adjustment)492     long getMemLevel(int adjustment) {
493         for (int i=0; i<mOomAdj.length; i++) {
494             if (adjustment <= mOomAdj[i]) {
495                 return mOomMinFree[i] * 1024;
496             }
497         }
498         return mOomMinFree[mOomAdj.length-1] * 1024;
499     }
500 
501     /**
502      * Return the maximum pss size in kb that we consider a process acceptable to
503      * restore from its cached state for running in the background when RAM is low.
504      */
getCachedRestoreThresholdKb()505     long getCachedRestoreThresholdKb() {
506         return mCachedRestoreLevel;
507     }
508 
writeFile(String path, String data)509     private void writeFile(String path, String data) {
510         FileOutputStream fos = null;
511         try {
512             fos = new FileOutputStream(path);
513             fos.write(data.getBytes());
514         } catch (IOException e) {
515             Slog.w(ActivityManagerService.TAG, "Unable to write " + path);
516         } finally {
517             if (fos != null) {
518                 try {
519                     fos.close();
520                 } catch (IOException e) {
521                 }
522             }
523         }
524     }
525 }
526