blob: f5920c8db3555b43df60bde381945ba5da6f93ae [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;
Todd Poynor91ecb362013-07-10 19:15:07 -070021import java.io.OutputStream;
22import java.nio.ByteBuffer;
Dianne Hackborn7d608422011-08-07 16:24:18 -070023
Dianne Hackborn8e692572013-09-10 19:06:15 -070024import android.app.ActivityManager;
Dianne Hackborn7d608422011-08-07 16:24:18 -070025import com.android.internal.util.MemInfoReader;
26import com.android.server.wm.WindowManagerService;
27
Colin Crossfcdad6f2013-07-25 15:04:40 -070028import android.content.res.Resources;
Dianne Hackborn7d608422011-08-07 16:24:18 -070029import android.graphics.Point;
Colin Cross59d80a52013-07-25 10:45:05 -070030import android.os.SystemProperties;
Todd Poynor91ecb362013-07-10 19:15:07 -070031import android.net.LocalSocketAddress;
32import android.net.LocalSocket;
Dianne Hackborn7d608422011-08-07 16:24:18 -070033import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070034import android.view.Display;
Dianne Hackborn7d608422011-08-07 16:24:18 -070035
36/**
37 * Activity manager code dealing with processes.
38 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070039final class ProcessList {
Dianne Hackborn7d608422011-08-07 16:24:18 -070040 // The minimum time we allow between crashes, for us to consider this
41 // application to be bad and stop and its services and reject broadcasts.
42 static final int MIN_CRASH_INTERVAL = 60*1000;
43
44 // OOM adjustments for processes in various states:
45
Dianne Hackbornc8230512013-07-13 21:32:12 -070046 // Adjustment used in certain places where we don't know it yet.
47 // (Generally this is something that is going to be cached, but we
48 // don't know the exact value in the cached range to assign yet.)
49 static final int UNKNOWN_ADJ = 16;
50
Dianne Hackborn7d608422011-08-07 16:24:18 -070051 // This is a process only hosting activities that are not visible,
Dianne Hackborne02c88a2011-10-28 13:58:15 -070052 // so it can be killed without any disruption.
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070053 static final int CACHED_APP_MAX_ADJ = 15;
Dianne Hackborn20cdcee2013-07-10 18:47:04 -070054 static final int CACHED_APP_MIN_ADJ = 9;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070055
56 // The B list of SERVICE_ADJ -- these are the old and decrepit
57 // services that aren't as shiny and interesting as the ones in the A list.
Dianne Hackbornf35fe232011-11-01 19:25:20 -070058 static final int SERVICE_B_ADJ = 8;
59
60 // This is the process of the previous application that the user was in.
61 // This process is kept above other things, because it is very common to
62 // switch back to the previous app. This is important both for recent
63 // task switch (toggling between the two top recent apps) as well as normal
64 // UI flow such as clicking on a URI in the e-mail app to view in the browser,
65 // and then pressing back to return to e-mail.
66 static final int PREVIOUS_APP_ADJ = 7;
Dianne Hackborn7d608422011-08-07 16:24:18 -070067
68 // This is a process holding the home application -- we want to try
69 // avoiding killing it, even if it would normally be in the background,
70 // because the user interacts with it so much.
71 static final int HOME_APP_ADJ = 6;
72
Dianne Hackborne02c88a2011-10-28 13:58:15 -070073 // This is a process holding an application service -- killing it will not
74 // have much of an impact as far as the user is concerned.
75 static final int SERVICE_ADJ = 5;
Dianne Hackborn7d608422011-08-07 16:24:18 -070076
Dianne Hackborn7d608422011-08-07 16:24:18 -070077 // This is a process with a heavy-weight application. It is in the
78 // background, but we want to try to avoid killing it. Value set in
79 // system/rootdir/init.rc on startup.
Dianne Hackbornc8230512013-07-13 21:32:12 -070080 static final int HEAVY_WEIGHT_APP_ADJ = 4;
81
82 // This is a process currently hosting a backup operation. Killing it
83 // is not entirely fatal but is generally a bad idea.
84 static final int BACKUP_APP_ADJ = 3;
Dianne Hackborn7d608422011-08-07 16:24:18 -070085
86 // This is a process only hosting components that are perceptible to the
87 // user, and we really want to avoid killing them, but they are not
Dianne Hackborne02c88a2011-10-28 13:58:15 -070088 // immediately visible. An example is background music playback.
Dianne Hackborn7d608422011-08-07 16:24:18 -070089 static final int PERCEPTIBLE_APP_ADJ = 2;
90
91 // This is a process only hosting activities that are visible to the
Dianne Hackborne02c88a2011-10-28 13:58:15 -070092 // user, so we'd prefer they don't disappear.
Dianne Hackborn7d608422011-08-07 16:24:18 -070093 static final int VISIBLE_APP_ADJ = 1;
94
95 // This is the process running the current foreground app. We'd really
Dianne Hackborne02c88a2011-10-28 13:58:15 -070096 // rather not kill it!
Dianne Hackborn7d608422011-08-07 16:24:18 -070097 static final int FOREGROUND_APP_ADJ = 0;
98
Dianne Hackborne02c88a2011-10-28 13:58:15 -070099 // This is a system persistent process, such as telephony. Definitely
Dianne Hackborn7d608422011-08-07 16:24:18 -0700100 // don't want to kill it, but doing so is not completely fatal.
Dianne Hackborne02c88a2011-10-28 13:58:15 -0700101 static final int PERSISTENT_PROC_ADJ = -12;
Dianne Hackborn7d608422011-08-07 16:24:18 -0700102
103 // The system process runs at the default adjustment.
104 static final int SYSTEM_ADJ = -16;
105
Dianne Hackborn8e692572013-09-10 19:06:15 -0700106 // Special code for native processes that are not being managed by the system (so
107 // don't have an oom adj assigned by the system).
108 static final int NATIVE_ADJ = -17;
109
Dianne Hackborn7d608422011-08-07 16:24:18 -0700110 // Memory pages are 4K.
111 static final int PAGE_SIZE = 4*1024;
112
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700113 // The minimum number of cached apps we want to be able to keep around,
Dianne Hackborn7d608422011-08-07 16:24:18 -0700114 // without empty apps being able to push them out of memory.
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700115 static final int MIN_CACHED_APPS = 2;
Dianne Hackborn7d608422011-08-07 16:24:18 -0700116
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700117 // The maximum number of cached processes we will keep around before killing them.
118 // NOTE: this constant is *only* a control to not let us go too crazy with
119 // keeping around processes on devices with large amounts of RAM. For devices that
120 // are tighter on RAM, the out of memory killer is responsible for killing background
121 // processes as RAM is needed, and we should *never* be relying on this limit to
122 // kill them. Also note that this limit only applies to cached background processes;
123 // we have no limit on the number of service, visible, foreground, or other such
124 // processes and the number of those processes does not count against the cached
125 // process limit.
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700126 static final int MAX_CACHED_APPS = 24;
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700127
128 // We allow empty processes to stick around for at most 30 minutes.
129 static final long MAX_EMPTY_TIME = 30*60*1000;
130
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700131 // The maximum number of empty app processes we will let sit around.
132 private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700133
134 // The number of empty apps at which we don't consider it necessary to do
135 // memory trimming.
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700136 static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2;
137
138 // The number of cached at which we don't consider it necessary to do
139 // memory trimming.
Dianne Hackborn2610b7c2013-09-20 18:45:43 -0700140 static final int TRIM_CACHED_APPS = ((MAX_CACHED_APPS-MAX_EMPTY_APPS)*2)/3;
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700141
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700142 // Threshold of number of cached+empty where we consider memory critical.
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700143 static final int TRIM_CRITICAL_THRESHOLD = 3;
144
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700145 // Threshold of number of cached+empty where we consider memory critical.
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700146 static final int TRIM_LOW_THRESHOLD = 5;
Dianne Hackborn7d608422011-08-07 16:24:18 -0700147
Todd Poynor91ecb362013-07-10 19:15:07 -0700148 // Low Memory Killer Daemon command codes.
149 // These must be kept in sync with the definitions in lmkd.c
150 //
151 // LMK_TARGET <minfree> <minkillprio> ... (up to 6 pairs)
152 // LMK_PROCPRIO <pid> <prio>
153 // LMK_PROCREMOVE <pid>
154 static final byte LMK_TARGET = 0;
155 static final byte LMK_PROCPRIO = 1;
156 static final byte LMK_PROCREMOVE = 2;
157
Dianne Hackborn7d608422011-08-07 16:24:18 -0700158 // These are the various interesting memory levels that we will give to
159 // the OOM killer. Note that the OOM killer only supports 6 slots, so we
160 // can't give it a different value for every possible kind of process.
161 private final int[] mOomAdj = new int[] {
162 FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ,
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -0700163 BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ
Dianne Hackborn7d608422011-08-07 16:24:18 -0700164 };
165 // These are the low-end OOM level limits. This is appropriate for an
166 // HVGA or smaller phone with less than 512MB. Values are in KB.
Todd Poynor91ecb362013-07-10 19:15:07 -0700167 private final int[] mOomMinFreeLow = new int[] {
Dianne Hackborn7d608422011-08-07 16:24:18 -0700168 8192, 12288, 16384,
169 24576, 28672, 32768
170 };
171 // These are the high-end OOM level limits. This is appropriate for a
172 // 1280x800 or larger screen with around 1GB RAM. Values are in KB.
Todd Poynor91ecb362013-07-10 19:15:07 -0700173 private final int[] mOomMinFreeHigh = new int[] {
Ben Chengb5cda422013-03-27 17:36:13 -0700174 49152, 61440, 73728,
175 86016, 98304, 122880
Dianne Hackborn7d608422011-08-07 16:24:18 -0700176 };
177 // The actual OOM killer memory levels we are using.
Todd Poynor91ecb362013-07-10 19:15:07 -0700178 private final int[] mOomMinFree = new int[mOomAdj.length];
Dianne Hackborn7d608422011-08-07 16:24:18 -0700179
180 private final long mTotalMemMb;
181
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700182 private long mCachedRestoreLevel;
183
Dianne Hackborn7d608422011-08-07 16:24:18 -0700184 private boolean mHaveDisplaySize;
185
Todd Poynor91ecb362013-07-10 19:15:07 -0700186 private static LocalSocket sLmkdSocket;
187 private static OutputStream sLmkdOutputStream;
188
Dianne Hackborn7d608422011-08-07 16:24:18 -0700189 ProcessList() {
190 MemInfoReader minfo = new MemInfoReader();
191 minfo.readMemInfo();
192 mTotalMemMb = minfo.getTotalSize()/(1024*1024);
193 updateOomLevels(0, 0, false);
194 }
195
196 void applyDisplaySize(WindowManagerService wm) {
197 if (!mHaveDisplaySize) {
198 Point p = new Point();
Colin Cross637dbfc2013-07-18 17:15:15 -0700199 wm.getBaseDisplaySize(Display.DEFAULT_DISPLAY, p);
Dianne Hackborn7d608422011-08-07 16:24:18 -0700200 if (p.x != 0 && p.y != 0) {
201 updateOomLevels(p.x, p.y, true);
202 mHaveDisplaySize = true;
203 }
204 }
205 }
206
207 private void updateOomLevels(int displayWidth, int displayHeight, boolean write) {
208 // Scale buckets from avail memory: at 300MB we use the lowest values to
209 // 700MB or more for the top values.
210 float scaleMem = ((float)(mTotalMemMb-300))/(700-300);
211
212 // Scale buckets from screen size.
Dianne Hackborn635a6d52013-07-29 17:15:38 -0700213 int minSize = 480*800; // 384000
Dianne Hackborn7d608422011-08-07 16:24:18 -0700214 int maxSize = 1280*800; // 1024000 230400 870400 .264
215 float scaleDisp = ((float)(displayWidth*displayHeight)-minSize)/(maxSize-minSize);
Dianne Hackborn635a6d52013-07-29 17:15:38 -0700216 if (false) {
217 Slog.i("XXXXXX", "scaleMem=" + scaleMem);
218 Slog.i("XXXXXX", "scaleDisp=" + scaleDisp + " dw=" + displayWidth
219 + " dh=" + displayHeight);
220 }
Dianne Hackborn7d608422011-08-07 16:24:18 -0700221
Dianne Hackborn7d608422011-08-07 16:24:18 -0700222 float scale = scaleMem > scaleDisp ? scaleMem : scaleDisp;
223 if (scale < 0) scale = 0;
224 else if (scale > 1) scale = 1;
Dianne Hackborn635a6d52013-07-29 17:15:38 -0700225 int minfree_adj = Resources.getSystem().getInteger(
226 com.android.internal.R.integer.config_lowMemoryKillerMinFreeKbytesAdjust);
227 int minfree_abs = Resources.getSystem().getInteger(
228 com.android.internal.R.integer.config_lowMemoryKillerMinFreeKbytesAbsolute);
229 if (false) {
230 Slog.i("XXXXXX", "minfree_adj=" + minfree_adj + " minfree_abs=" + minfree_abs);
231 }
Colin Crossfcdad6f2013-07-25 15:04:40 -0700232
Dianne Hackborn7d608422011-08-07 16:24:18 -0700233 for (int i=0; i<mOomAdj.length; i++) {
Todd Poynor91ecb362013-07-10 19:15:07 -0700234 int low = mOomMinFreeLow[i];
235 int high = mOomMinFreeHigh[i];
236 mOomMinFree[i] = (int)(low + ((high-low)*scale));
Colin Crossfcdad6f2013-07-25 15:04:40 -0700237 }
Dianne Hackborn7d608422011-08-07 16:24:18 -0700238
Colin Crossfcdad6f2013-07-25 15:04:40 -0700239 if (minfree_abs >= 0) {
240 for (int i=0; i<mOomAdj.length; i++) {
Todd Poynor91ecb362013-07-10 19:15:07 -0700241 mOomMinFree[i] = (int)((float)minfree_abs * mOomMinFree[i] / mOomMinFree[mOomAdj.length - 1]);
Colin Crossfcdad6f2013-07-25 15:04:40 -0700242 }
243 }
244
245 if (minfree_adj != 0) {
246 for (int i=0; i<mOomAdj.length; i++) {
Todd Poynor91ecb362013-07-10 19:15:07 -0700247 mOomMinFree[i] += (int)((float)minfree_adj * mOomMinFree[i] / mOomMinFree[mOomAdj.length - 1]);
Colin Crossfcdad6f2013-07-25 15:04:40 -0700248 if (mOomMinFree[i] < 0) {
249 mOomMinFree[i] = 0;
250 }
251 }
252 }
253
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700254 // The maximum size we will restore a process from cached to background, when under
255 // memory duress, is 1/3 the size we have reserved for kernel caches and other overhead
256 // before killing background processes.
257 mCachedRestoreLevel = (getMemLevel(ProcessList.CACHED_APP_MAX_ADJ)/1024) / 3;
258
Colin Cross59d80a52013-07-25 10:45:05 -0700259 // Ask the kernel to try to keep enough memory free to allocate 3 full
260 // screen 32bpp buffers without entering direct reclaim.
261 int reserve = displayWidth * displayHeight * 4 * 3 / 1024;
262 int reserve_adj = Resources.getSystem().getInteger(com.android.internal.R.integer.config_extraFreeKbytesAdjust);
263 int reserve_abs = Resources.getSystem().getInteger(com.android.internal.R.integer.config_extraFreeKbytesAbsolute);
264
265 if (reserve_abs >= 0) {
266 reserve = reserve_abs;
267 }
268
269 if (reserve_adj != 0) {
270 reserve += reserve_adj;
271 if (reserve < 0) {
272 reserve = 0;
273 }
274 }
275
Dianne Hackborn7d608422011-08-07 16:24:18 -0700276 if (write) {
Todd Poynor91ecb362013-07-10 19:15:07 -0700277 ByteBuffer buf = ByteBuffer.allocate(4 * (2*mOomAdj.length + 1));
278 buf.putInt(LMK_TARGET);
279 for (int i=0; i<mOomAdj.length; i++) {
280 buf.putInt((mOomMinFree[i]*1024)/PAGE_SIZE);
281 buf.putInt(mOomAdj[i]);
282 }
283
284 writeLmkd(buf);
Colin Cross59d80a52013-07-25 10:45:05 -0700285 SystemProperties.set("sys.sysctl.extra_free_kbytes", Integer.toString(reserve));
Dianne Hackborn7d608422011-08-07 16:24:18 -0700286 }
287 // GB: 2048,3072,4096,6144,7168,8192
288 // HC: 8192,10240,12288,14336,16384,20480
289 }
290
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700291 public static int computeEmptyProcessLimit(int totalProcessLimit) {
292 return (totalProcessLimit*2)/3;
293 }
294
Dianne Hackborn8e692572013-09-10 19:06:15 -0700295 private static String buildOomTag(String prefix, String space, int val, int base) {
296 if (val == base) {
297 if (space == null) return prefix;
298 return prefix + " ";
299 }
300 return prefix + "+" + Integer.toString(val-base);
301 }
302
303 public static String makeOomAdjString(int setAdj) {
304 if (setAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
305 return buildOomTag("cch", " ", setAdj, ProcessList.CACHED_APP_MIN_ADJ);
306 } else if (setAdj >= ProcessList.SERVICE_B_ADJ) {
307 return buildOomTag("svcb ", null, setAdj, ProcessList.SERVICE_B_ADJ);
308 } else if (setAdj >= ProcessList.PREVIOUS_APP_ADJ) {
309 return buildOomTag("prev ", null, setAdj, ProcessList.PREVIOUS_APP_ADJ);
310 } else if (setAdj >= ProcessList.HOME_APP_ADJ) {
311 return buildOomTag("home ", null, setAdj, ProcessList.HOME_APP_ADJ);
312 } else if (setAdj >= ProcessList.SERVICE_ADJ) {
313 return buildOomTag("svc ", null, setAdj, ProcessList.SERVICE_ADJ);
314 } else if (setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
315 return buildOomTag("hvy ", null, setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ);
316 } else if (setAdj >= ProcessList.BACKUP_APP_ADJ) {
317 return buildOomTag("bkup ", null, setAdj, ProcessList.BACKUP_APP_ADJ);
318 } else if (setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
319 return buildOomTag("prcp ", null, setAdj, ProcessList.PERCEPTIBLE_APP_ADJ);
320 } else if (setAdj >= ProcessList.VISIBLE_APP_ADJ) {
321 return buildOomTag("vis ", null, setAdj, ProcessList.VISIBLE_APP_ADJ);
322 } else if (setAdj >= ProcessList.FOREGROUND_APP_ADJ) {
323 return buildOomTag("fore ", null, setAdj, ProcessList.FOREGROUND_APP_ADJ);
324 } else if (setAdj >= ProcessList.PERSISTENT_PROC_ADJ) {
325 return buildOomTag("pers ", null, setAdj, ProcessList.PERSISTENT_PROC_ADJ);
326 } else if (setAdj >= ProcessList.SYSTEM_ADJ) {
327 return buildOomTag("sys ", null, setAdj, ProcessList.SYSTEM_ADJ);
328 } else if (setAdj >= ProcessList.NATIVE_ADJ) {
329 return buildOomTag("ntv ", null, setAdj, ProcessList.NATIVE_ADJ);
330 } else {
331 return Integer.toString(setAdj);
332 }
333 }
334
Dianne Hackborn8e692572013-09-10 19:06:15 -0700335 public static String makeProcStateString(int curProcState) {
336 String procState;
337 switch (curProcState) {
338 case -1:
339 procState = "N ";
340 break;
341 case ActivityManager.PROCESS_STATE_PERSISTENT:
342 procState = "P ";
343 break;
344 case ActivityManager.PROCESS_STATE_PERSISTENT_UI:
345 procState = "PU";
346 break;
347 case ActivityManager.PROCESS_STATE_TOP:
348 procState = "T ";
349 break;
350 case ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND:
351 procState = "IF";
352 break;
353 case ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND:
354 procState = "IB";
355 break;
356 case ActivityManager.PROCESS_STATE_BACKUP:
357 procState = "BU";
358 break;
359 case ActivityManager.PROCESS_STATE_HEAVY_WEIGHT:
360 procState = "HW";
361 break;
362 case ActivityManager.PROCESS_STATE_SERVICE:
363 procState = "S ";
364 break;
365 case ActivityManager.PROCESS_STATE_RECEIVER:
366 procState = "R ";
367 break;
368 case ActivityManager.PROCESS_STATE_HOME:
369 procState = "HO";
370 break;
371 case ActivityManager.PROCESS_STATE_LAST_ACTIVITY:
372 procState = "LA";
373 break;
374 case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY:
375 procState = "CA";
376 break;
377 case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
378 procState = "Ca";
379 break;
380 case ActivityManager.PROCESS_STATE_CACHED_EMPTY:
381 procState = "CE";
382 break;
383 default:
384 procState = "??";
385 break;
386 }
387 return procState;
388 }
389
390 public static void appendRamKb(StringBuilder sb, long ramKb) {
391 for (int j=0, fact=10; j<6; j++, fact*=10) {
392 if (ramKb < fact) {
393 sb.append(' ');
394 }
395 }
396 sb.append(ramKb);
397 }
398
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700399 // The minimum amount of time after a state change it is safe ro collect PSS.
400 public static final int PSS_MIN_TIME_FROM_STATE_CHANGE = 15*1000;
401
402 // The maximum amount of time we want to go between PSS collections.
403 public static final int PSS_MAX_INTERVAL = 30*60*1000;
404
405 // The minimum amount of time between successive PSS requests for *all* processes.
406 public static final int PSS_ALL_INTERVAL = 10*60*1000;
407
408 // The minimum amount of time between successive PSS requests for a process.
409 private static final int PSS_SHORT_INTERVAL = 2*60*1000;
410
411 // The amount of time until PSS when a process first becomes top.
412 private static final int PSS_FIRST_TOP_INTERVAL = 10*1000;
413
414 // The amount of time until PSS when a process first goes into the background.
415 private static final int PSS_FIRST_BACKGROUND_INTERVAL = 20*1000;
416
417 // The amount of time until PSS when a process first becomes cached.
418 private static final int PSS_FIRST_CACHED_INTERVAL = 30*1000;
419
420 // The amount of time until PSS when an important process stays in the same state.
421 private static final int PSS_SAME_IMPORTANT_INTERVAL = 15*60*1000;
422
423 // The amount of time until PSS when a service process stays in the same state.
424 private static final int PSS_SAME_SERVICE_INTERVAL = 20*60*1000;
425
426 // The amount of time until PSS when a cached process stays in the same state.
427 private static final int PSS_SAME_CACHED_INTERVAL = 30*60*1000;
428
429 public static final int PROC_MEM_PERSISTENT = 0;
430 public static final int PROC_MEM_TOP = 1;
431 public static final int PROC_MEM_IMPORTANT = 2;
432 public static final int PROC_MEM_SERVICE = 3;
433 public static final int PROC_MEM_CACHED = 4;
434
435 private static final int[] sProcStateToProcMem = new int[] {
436 PROC_MEM_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT
437 PROC_MEM_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT_UI
438 PROC_MEM_TOP, // ActivityManager.PROCESS_STATE_TOP
439 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
440 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
441 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_BACKUP
442 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
443 PROC_MEM_SERVICE, // ActivityManager.PROCESS_STATE_SERVICE
444 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_RECEIVER
445 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_HOME
446 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
447 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY
448 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT
449 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_CACHED_EMPTY
450 };
451
452 private static final long[] sFirstAwakePssTimes = new long[] {
453 PSS_SHORT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT
454 PSS_SHORT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT_UI
455 PSS_FIRST_TOP_INTERVAL, // ActivityManager.PROCESS_STATE_TOP
456 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
457 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
458 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_BACKUP
459 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
460 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_SERVICE
461 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_RECEIVER
462 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_HOME
463 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
464 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY
465 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT
466 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_EMPTY
467 };
468
469 private static final long[] sSameAwakePssTimes = new long[] {
470 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT
471 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT_UI
472 PSS_SHORT_INTERVAL, // ActivityManager.PROCESS_STATE_TOP
473 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
474 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
475 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_BACKUP
476 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
477 PSS_SAME_SERVICE_INTERVAL, // ActivityManager.PROCESS_STATE_SERVICE
478 PSS_SAME_SERVICE_INTERVAL, // ActivityManager.PROCESS_STATE_RECEIVER
479 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_HOME
480 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
481 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY
482 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT
483 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_EMPTY
484 };
485
486 public static boolean procStatesDifferForMem(int procState1, int procState2) {
487 return sProcStateToProcMem[procState1] != sProcStateToProcMem[procState2];
488 }
489
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700490 public static long computeNextPssTime(int procState, boolean first, boolean sleeping,
491 long now) {
492 final long[] table = sleeping
493 ? (first
494 ? sFirstAwakePssTimes
495 : sSameAwakePssTimes)
496 : (first
497 ? sFirstAwakePssTimes
498 : sSameAwakePssTimes);
499 return now + table[procState];
500 }
501
Dianne Hackborn7d608422011-08-07 16:24:18 -0700502 long getMemLevel(int adjustment) {
503 for (int i=0; i<mOomAdj.length; i++) {
504 if (adjustment <= mOomAdj[i]) {
505 return mOomMinFree[i] * 1024;
506 }
507 }
508 return mOomMinFree[mOomAdj.length-1] * 1024;
509 }
510
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700511 /**
512 * Return the maximum pss size in kb that we consider a process acceptable to
513 * restore from its cached state for running in the background when RAM is low.
514 */
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700515 long getCachedRestoreThresholdKb() {
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700516 return mCachedRestoreLevel;
517 }
518
Todd Poynor91ecb362013-07-10 19:15:07 -0700519 /**
520 * Set the out-of-memory badness adjustment for a process.
521 *
522 * @param pid The process identifier to set.
523 * @param amt Adjustment value -- lmkd allows -16 to +15.
524 *
525 * {@hide}
526 */
527 public static final void setOomAdj(int pid, int amt) {
528 if (amt == UNKNOWN_ADJ)
529 return;
530
531 ByteBuffer buf = ByteBuffer.allocate(4 * 3);
532 buf.putInt(LMK_PROCPRIO);
533 buf.putInt(pid);
534 buf.putInt(amt);
535 writeLmkd(buf);
536 }
537
538 /*
539 * {@hide}
540 */
541 public static final void remove(int pid) {
542 ByteBuffer buf = ByteBuffer.allocate(4 * 2);
543 buf.putInt(LMK_PROCREMOVE);
544 buf.putInt(pid);
545 writeLmkd(buf);
546 }
547
548 private static boolean openLmkdSocket() {
Dianne Hackborn7d608422011-08-07 16:24:18 -0700549 try {
Todd Poynor91ecb362013-07-10 19:15:07 -0700550 sLmkdSocket = new LocalSocket(LocalSocket.SOCKET_SEQPACKET);
551 sLmkdSocket.connect(
552 new LocalSocketAddress("lmkd",
553 LocalSocketAddress.Namespace.RESERVED));
554 sLmkdOutputStream = sLmkdSocket.getOutputStream();
555 } catch (IOException ex) {
556 Slog.w(ActivityManagerService.TAG,
557 "lowmemorykiller daemon socket open failed");
558 sLmkdSocket = null;
559 return false;
560 }
561
562 return true;
563 }
564
565 private static void writeLmkd(ByteBuffer buf) {
566
567 for (int i = 0; i < 3; i++) {
568 if (sLmkdSocket == null) {
569 if (openLmkdSocket() == false) {
570 try {
571 Thread.sleep(1000);
572 } catch (InterruptedException ie) {
573 }
574 continue;
575 }
576 }
577
578 try {
579 sLmkdOutputStream.write(buf.array(), 0, buf.position());
580 return;
581 } catch (IOException ex) {
582 Slog.w(ActivityManagerService.TAG,
583 "Error writing to lowmemorykiller socket");
584
Dianne Hackborn7d608422011-08-07 16:24:18 -0700585 try {
Todd Poynor91ecb362013-07-10 19:15:07 -0700586 sLmkdSocket.close();
587 } catch (IOException ex2) {
Dianne Hackborn7d608422011-08-07 16:24:18 -0700588 }
Todd Poynor91ecb362013-07-10 19:15:07 -0700589
590 sLmkdSocket = null;
Dianne Hackborn7d608422011-08-07 16:24:18 -0700591 }
592 }
593 }
594}