blob: 106ba222e2d8e7be4fdaffd6fae54aecce4fe6a7 [file] [log] [blame]
Dianne Hackborn7d608422011-08-07 16:24:18 -07001/*
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
17package com.android.server.am;
18
Dianne Hackborn7d608422011-08-07 16:24:18 -070019import java.io.FileOutputStream;
20import java.io.IOException;
21
22import com.android.internal.util.MemInfoReader;
23import com.android.server.wm.WindowManagerService;
24
25import android.graphics.Point;
Dianne Hackborn7d608422011-08-07 16:24:18 -070026import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070027import android.view.Display;
Dianne Hackborn7d608422011-08-07 16:24:18 -070028
29/**
30 * Activity manager code dealing with processes.
31 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070032final class ProcessList {
Dianne Hackborn7d608422011-08-07 16:24:18 -070033 // The minimum time we allow between crashes, for us to consider this
34 // application to be bad and stop and its services and reject broadcasts.
35 static final int MIN_CRASH_INTERVAL = 60*1000;
36
37 // OOM adjustments for processes in various states:
38
Dianne Hackbornc8230512013-07-13 21:32:12 -070039 // Adjustment used in certain places where we don't know it yet.
40 // (Generally this is something that is going to be cached, but we
41 // don't know the exact value in the cached range to assign yet.)
42 static final int UNKNOWN_ADJ = 16;
43
Dianne Hackborn7d608422011-08-07 16:24:18 -070044 // This is a process only hosting activities that are not visible,
Dianne Hackborne02c88a2011-10-28 13:58:15 -070045 // so it can be killed without any disruption.
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070046 static final int CACHED_APP_MAX_ADJ = 15;
Dianne Hackborn20cdcee2013-07-10 18:47:04 -070047 static final int CACHED_APP_MIN_ADJ = 9;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070048
49 // The B list of SERVICE_ADJ -- these are the old and decrepit
50 // services that aren't as shiny and interesting as the ones in the A list.
Dianne Hackbornf35fe232011-11-01 19:25:20 -070051 static final int SERVICE_B_ADJ = 8;
52
53 // This is the process of the previous application that the user was in.
54 // This process is kept above other things, because it is very common to
55 // switch back to the previous app. This is important both for recent
56 // task switch (toggling between the two top recent apps) as well as normal
57 // UI flow such as clicking on a URI in the e-mail app to view in the browser,
58 // and then pressing back to return to e-mail.
59 static final int PREVIOUS_APP_ADJ = 7;
Dianne Hackborn7d608422011-08-07 16:24:18 -070060
61 // This is a process holding the home application -- we want to try
62 // avoiding killing it, even if it would normally be in the background,
63 // because the user interacts with it so much.
64 static final int HOME_APP_ADJ = 6;
65
Dianne Hackborne02c88a2011-10-28 13:58:15 -070066 // This is a process holding an application service -- killing it will not
67 // have much of an impact as far as the user is concerned.
68 static final int SERVICE_ADJ = 5;
Dianne Hackborn7d608422011-08-07 16:24:18 -070069
Dianne Hackborn7d608422011-08-07 16:24:18 -070070 // This is a process with a heavy-weight application. It is in the
71 // background, but we want to try to avoid killing it. Value set in
72 // system/rootdir/init.rc on startup.
Dianne Hackbornc8230512013-07-13 21:32:12 -070073 static final int HEAVY_WEIGHT_APP_ADJ = 4;
74
75 // This is a process currently hosting a backup operation. Killing it
76 // is not entirely fatal but is generally a bad idea.
77 static final int BACKUP_APP_ADJ = 3;
Dianne Hackborn7d608422011-08-07 16:24:18 -070078
79 // This is a process only hosting components that are perceptible to the
80 // user, and we really want to avoid killing them, but they are not
Dianne Hackborne02c88a2011-10-28 13:58:15 -070081 // immediately visible. An example is background music playback.
Dianne Hackborn7d608422011-08-07 16:24:18 -070082 static final int PERCEPTIBLE_APP_ADJ = 2;
83
84 // This is a process only hosting activities that are visible to the
Dianne Hackborne02c88a2011-10-28 13:58:15 -070085 // user, so we'd prefer they don't disappear.
Dianne Hackborn7d608422011-08-07 16:24:18 -070086 static final int VISIBLE_APP_ADJ = 1;
87
88 // This is the process running the current foreground app. We'd really
Dianne Hackborne02c88a2011-10-28 13:58:15 -070089 // rather not kill it!
Dianne Hackborn7d608422011-08-07 16:24:18 -070090 static final int FOREGROUND_APP_ADJ = 0;
91
Dianne Hackborne02c88a2011-10-28 13:58:15 -070092 // This is a system persistent process, such as telephony. Definitely
Dianne Hackborn7d608422011-08-07 16:24:18 -070093 // don't want to kill it, but doing so is not completely fatal.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070094 static final int PERSISTENT_PROC_ADJ = -12;
Dianne Hackborn7d608422011-08-07 16:24:18 -070095
96 // The system process runs at the default adjustment.
97 static final int SYSTEM_ADJ = -16;
98
99 // Memory pages are 4K.
100 static final int PAGE_SIZE = 4*1024;
101
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700102 // The minimum number of cached apps we want to be able to keep around,
Dianne Hackborn7d608422011-08-07 16:24:18 -0700103 // without empty apps being able to push them out of memory.
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700104 static final int MIN_CACHED_APPS = 2;
Dianne Hackborn7d608422011-08-07 16:24:18 -0700105
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700106 // The maximum number of cached processes we will keep around before killing them.
107 // NOTE: this constant is *only* a control to not let us go too crazy with
108 // keeping around processes on devices with large amounts of RAM. For devices that
109 // are tighter on RAM, the out of memory killer is responsible for killing background
110 // processes as RAM is needed, and we should *never* be relying on this limit to
111 // kill them. Also note that this limit only applies to cached background processes;
112 // we have no limit on the number of service, visible, foreground, or other such
113 // processes and the number of those processes does not count against the cached
114 // process limit.
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700115 static final int MAX_CACHED_APPS = 24;
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700116
117 // We allow empty processes to stick around for at most 30 minutes.
118 static final long MAX_EMPTY_TIME = 30*60*1000;
119
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700120 // The maximum number of empty app processes we will let sit around.
121 private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700122
123 // The number of empty apps at which we don't consider it necessary to do
124 // memory trimming.
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700125 static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2;
126
127 // The number of cached at which we don't consider it necessary to do
128 // memory trimming.
129 static final int TRIM_CACHED_APPS = (MAX_CACHED_APPS-MAX_EMPTY_APPS)/2;
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700130
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700131 // Threshold of number of cached+empty where we consider memory critical.
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700132 static final int TRIM_CRITICAL_THRESHOLD = 3;
133
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700134 // Threshold of number of cached+empty where we consider memory critical.
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700135 static final int TRIM_LOW_THRESHOLD = 5;
Dianne Hackborn7d608422011-08-07 16:24:18 -0700136
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700137 // We put empty content processes after any cached processes that have
Dianne Hackborn7d608422011-08-07 16:24:18 -0700138 // been idle for less than 15 seconds.
139 static final long CONTENT_APP_IDLE_OFFSET = 15*1000;
140
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700141 // We put empty content processes after any cached processes that have
Dianne Hackborn7d608422011-08-07 16:24:18 -0700142 // been idle for less than 120 seconds.
143 static final long EMPTY_APP_IDLE_OFFSET = 120*1000;
144
145 // These are the various interesting memory levels that we will give to
146 // the OOM killer. Note that the OOM killer only supports 6 slots, so we
147 // can't give it a different value for every possible kind of process.
148 private final int[] mOomAdj = new int[] {
149 FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ,
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700150 BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ
Dianne Hackborn7d608422011-08-07 16:24:18 -0700151 };
152 // These are the low-end OOM level limits. This is appropriate for an
153 // HVGA or smaller phone with less than 512MB. Values are in KB.
154 private final long[] mOomMinFreeLow = new long[] {
155 8192, 12288, 16384,
156 24576, 28672, 32768
157 };
158 // These are the high-end OOM level limits. This is appropriate for a
159 // 1280x800 or larger screen with around 1GB RAM. Values are in KB.
160 private final long[] mOomMinFreeHigh = new long[] {
Ben Chengb5cda422013-03-27 17:36:13 -0700161 49152, 61440, 73728,
162 86016, 98304, 122880
Dianne Hackborn7d608422011-08-07 16:24:18 -0700163 };
164 // The actual OOM killer memory levels we are using.
165 private final long[] mOomMinFree = new long[mOomAdj.length];
166
167 private final long mTotalMemMb;
168
169 private boolean mHaveDisplaySize;
170
171 ProcessList() {
172 MemInfoReader minfo = new MemInfoReader();
173 minfo.readMemInfo();
174 mTotalMemMb = minfo.getTotalSize()/(1024*1024);
175 updateOomLevels(0, 0, false);
176 }
177
178 void applyDisplaySize(WindowManagerService wm) {
179 if (!mHaveDisplaySize) {
180 Point p = new Point();
Colin Cross637dbfc2013-07-18 17:15:15 -0700181 wm.getBaseDisplaySize(Display.DEFAULT_DISPLAY, p);
Dianne Hackborn7d608422011-08-07 16:24:18 -0700182 if (p.x != 0 && p.y != 0) {
183 updateOomLevels(p.x, p.y, true);
184 mHaveDisplaySize = true;
185 }
186 }
187 }
188
189 private void updateOomLevels(int displayWidth, int displayHeight, boolean write) {
190 // Scale buckets from avail memory: at 300MB we use the lowest values to
191 // 700MB or more for the top values.
192 float scaleMem = ((float)(mTotalMemMb-300))/(700-300);
193
194 // Scale buckets from screen size.
195 int minSize = 320*480; // 153600
196 int maxSize = 1280*800; // 1024000 230400 870400 .264
197 float scaleDisp = ((float)(displayWidth*displayHeight)-minSize)/(maxSize-minSize);
Dianne Hackborn295e3c22011-08-25 13:19:08 -0700198 //Slog.i("XXXXXX", "scaleDisp=" + scaleDisp + " dw=" + displayWidth + " dh=" + displayHeight);
Dianne Hackborn7d608422011-08-07 16:24:18 -0700199
200 StringBuilder adjString = new StringBuilder();
201 StringBuilder memString = new StringBuilder();
202
203 float scale = scaleMem > scaleDisp ? scaleMem : scaleDisp;
204 if (scale < 0) scale = 0;
205 else if (scale > 1) scale = 1;
206 for (int i=0; i<mOomAdj.length; i++) {
207 long low = mOomMinFreeLow[i];
208 long high = mOomMinFreeHigh[i];
209 mOomMinFree[i] = (long)(low + ((high-low)*scale));
210
211 if (i > 0) {
212 adjString.append(',');
213 memString.append(',');
214 }
215 adjString.append(mOomAdj[i]);
216 memString.append((mOomMinFree[i]*1024)/PAGE_SIZE);
217 }
218
219 //Slog.i("XXXXXXX", "******************************* MINFREE: " + memString);
220 if (write) {
221 writeFile("/sys/module/lowmemorykiller/parameters/adj", adjString.toString());
222 writeFile("/sys/module/lowmemorykiller/parameters/minfree", memString.toString());
223 }
224 // GB: 2048,3072,4096,6144,7168,8192
225 // HC: 8192,10240,12288,14336,16384,20480
226 }
227
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700228 public static int computeEmptyProcessLimit(int totalProcessLimit) {
229 return (totalProcessLimit*2)/3;
230 }
231
Dianne Hackborn7d608422011-08-07 16:24:18 -0700232 long getMemLevel(int adjustment) {
233 for (int i=0; i<mOomAdj.length; i++) {
234 if (adjustment <= mOomAdj[i]) {
235 return mOomMinFree[i] * 1024;
236 }
237 }
238 return mOomMinFree[mOomAdj.length-1] * 1024;
239 }
240
Dianne Hackborn7d608422011-08-07 16:24:18 -0700241 private void writeFile(String path, String data) {
242 FileOutputStream fos = null;
243 try {
244 fos = new FileOutputStream(path);
245 fos.write(data.getBytes());
246 } catch (IOException e) {
247 Slog.w(ActivityManagerService.TAG, "Unable to write " + path);
248 } finally {
249 if (fos != null) {
250 try {
251 fos.close();
252 } catch (IOException e) {
253 }
254 }
255 }
256 }
257}