ProcessList.java revision fdcc4d4235c829a41b4d23043431bb8fa915a440
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 17package com.android.server.am; 18 19import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; 20import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; 21 22import java.io.IOException; 23import java.io.OutputStream; 24import java.nio.ByteBuffer; 25 26import android.app.ActivityManager; 27import android.os.Build; 28import android.os.SystemClock; 29import com.android.internal.util.MemInfoReader; 30import com.android.server.wm.WindowManagerService; 31 32import android.content.res.Resources; 33import android.graphics.Point; 34import android.os.SystemProperties; 35import android.net.LocalSocketAddress; 36import android.net.LocalSocket; 37import android.util.Slog; 38import android.view.Display; 39 40/** 41 * Activity manager code dealing with processes. 42 */ 43final class ProcessList { 44 private static final String TAG = TAG_WITH_CLASS_NAME ? "ProcessList" : TAG_AM; 45 46 // The minimum time we allow between crashes, for us to consider this 47 // application to be bad and stop and its services and reject broadcasts. 48 static final int MIN_CRASH_INTERVAL = 60*1000; 49 50 // OOM adjustments for processes in various states: 51 52 // Uninitialized value for any major or minor adj fields 53 static final int INVALID_ADJ = -10000; 54 55 // Adjustment used in certain places where we don't know it yet. 56 // (Generally this is something that is going to be cached, but we 57 // don't know the exact value in the cached range to assign yet.) 58 static final int UNKNOWN_ADJ = 1001; 59 60 // This is a process only hosting activities that are not visible, 61 // so it can be killed without any disruption. 62 static final int CACHED_APP_MAX_ADJ = 906; 63 static final int CACHED_APP_MIN_ADJ = 900; 64 65 // The B list of SERVICE_ADJ -- these are the old and decrepit 66 // services that aren't as shiny and interesting as the ones in the A list. 67 static final int SERVICE_B_ADJ = 800; 68 69 // This is the process of the previous application that the user was in. 70 // This process is kept above other things, because it is very common to 71 // switch back to the previous app. This is important both for recent 72 // task switch (toggling between the two top recent apps) as well as normal 73 // UI flow such as clicking on a URI in the e-mail app to view in the browser, 74 // and then pressing back to return to e-mail. 75 static final int PREVIOUS_APP_ADJ = 700; 76 77 // This is a process holding the home application -- we want to try 78 // avoiding killing it, even if it would normally be in the background, 79 // because the user interacts with it so much. 80 static final int HOME_APP_ADJ = 600; 81 82 // This is a process holding an application service -- killing it will not 83 // have much of an impact as far as the user is concerned. 84 static final int SERVICE_ADJ = 500; 85 86 // This is a process with a heavy-weight application. It is in the 87 // background, but we want to try to avoid killing it. Value set in 88 // system/rootdir/init.rc on startup. 89 static final int HEAVY_WEIGHT_APP_ADJ = 400; 90 91 // This is a process currently hosting a backup operation. Killing it 92 // is not entirely fatal but is generally a bad idea. 93 static final int BACKUP_APP_ADJ = 300; 94 95 // This is a process only hosting components that are perceptible to the 96 // user, and we really want to avoid killing them, but they are not 97 // immediately visible. An example is background music playback. 98 static final int PERCEPTIBLE_APP_ADJ = 200; 99 100 // This is a process only hosting activities that are visible to the 101 // user, so we'd prefer they don't disappear. 102 static final int VISIBLE_APP_ADJ = 100; 103 static final int VISIBLE_APP_LAYER_MAX = PERCEPTIBLE_APP_ADJ - VISIBLE_APP_ADJ - 1; 104 105 // This is the process running the current foreground app. We'd really 106 // rather not kill it! 107 static final int FOREGROUND_APP_ADJ = 0; 108 109 // This is a process that the system or a persistent process has bound to, 110 // and indicated it is important. 111 static final int PERSISTENT_SERVICE_ADJ = -700; 112 113 // This is a system persistent process, such as telephony. Definitely 114 // don't want to kill it, but doing so is not completely fatal. 115 static final int PERSISTENT_PROC_ADJ = -800; 116 117 // The system process runs at the default adjustment. 118 static final int SYSTEM_ADJ = -900; 119 120 // Special code for native processes that are not being managed by the system (so 121 // don't have an oom adj assigned by the system). 122 static final int NATIVE_ADJ = -1000; 123 124 // Memory pages are 4K. 125 static final int PAGE_SIZE = 4*1024; 126 127 // The minimum number of cached apps we want to be able to keep around, 128 // without empty apps being able to push them out of memory. 129 static final int MIN_CACHED_APPS = 2; 130 131 // The maximum number of cached processes we will keep around before killing them. 132 // NOTE: this constant is *only* a control to not let us go too crazy with 133 // keeping around processes on devices with large amounts of RAM. For devices that 134 // are tighter on RAM, the out of memory killer is responsible for killing background 135 // processes as RAM is needed, and we should *never* be relying on this limit to 136 // kill them. Also note that this limit only applies to cached background processes; 137 // we have no limit on the number of service, visible, foreground, or other such 138 // processes and the number of those processes does not count against the cached 139 // process limit. 140 static final int MAX_CACHED_APPS = 32; 141 142 // We allow empty processes to stick around for at most 30 minutes. 143 static final long MAX_EMPTY_TIME = 30*60*1000; 144 145 // The maximum number of empty app processes we will let sit around. 146 private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS); 147 148 // The number of empty apps at which we don't consider it necessary to do 149 // memory trimming. 150 static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2; 151 152 // The number of cached at which we don't consider it necessary to do 153 // memory trimming. 154 static final int TRIM_CACHED_APPS = (MAX_CACHED_APPS-MAX_EMPTY_APPS)/3; 155 156 // Threshold of number of cached+empty where we consider memory critical. 157 static final int TRIM_CRITICAL_THRESHOLD = 3; 158 159 // Threshold of number of cached+empty where we consider memory critical. 160 static final int TRIM_LOW_THRESHOLD = 5; 161 162 // Low Memory Killer Daemon command codes. 163 // These must be kept in sync with the definitions in lmkd.c 164 // 165 // LMK_TARGET <minfree> <minkillprio> ... (up to 6 pairs) 166 // LMK_PROCPRIO <pid> <uid> <prio> 167 // LMK_PROCREMOVE <pid> 168 static final byte LMK_TARGET = 0; 169 static final byte LMK_PROCPRIO = 1; 170 static final byte LMK_PROCREMOVE = 2; 171 172 // These are the various interesting memory levels that we will give to 173 // the OOM killer. Note that the OOM killer only supports 6 slots, so we 174 // can't give it a different value for every possible kind of process. 175 private final int[] mOomAdj = new int[] { 176 FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ, 177 BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ 178 }; 179 // These are the low-end OOM level limits. This is appropriate for an 180 // HVGA or smaller phone with less than 512MB. Values are in KB. 181 private final int[] mOomMinFreeLow = new int[] { 182 12288, 18432, 24576, 183 36864, 43008, 49152 184 }; 185 // These are the high-end OOM level limits. This is appropriate for a 186 // 1280x800 or larger screen with around 1GB RAM. Values are in KB. 187 private final int[] mOomMinFreeHigh = new int[] { 188 73728, 92160, 110592, 189 129024, 147456, 184320 190 }; 191 // The actual OOM killer memory levels we are using. 192 private final int[] mOomMinFree = new int[mOomAdj.length]; 193 194 private final long mTotalMemMb; 195 196 private long mCachedRestoreLevel; 197 198 private boolean mHaveDisplaySize; 199 200 private static LocalSocket sLmkdSocket; 201 private static OutputStream sLmkdOutputStream; 202 203 ProcessList() { 204 MemInfoReader minfo = new MemInfoReader(); 205 minfo.readMemInfo(); 206 mTotalMemMb = minfo.getTotalSize()/(1024*1024); 207 updateOomLevels(0, 0, false); 208 } 209 210 void applyDisplaySize(WindowManagerService wm) { 211 if (!mHaveDisplaySize) { 212 Point p = new Point(); 213 wm.getBaseDisplaySize(Display.DEFAULT_DISPLAY, p); 214 if (p.x != 0 && p.y != 0) { 215 updateOomLevels(p.x, p.y, true); 216 mHaveDisplaySize = true; 217 } 218 } 219 } 220 221 private void updateOomLevels(int displayWidth, int displayHeight, boolean write) { 222 // Scale buckets from avail memory: at 300MB we use the lowest values to 223 // 700MB or more for the top values. 224 float scaleMem = ((float)(mTotalMemMb-350))/(700-350); 225 226 // Scale buckets from screen size. 227 int minSize = 480*800; // 384000 228 int maxSize = 1280*800; // 1024000 230400 870400 .264 229 float scaleDisp = ((float)(displayWidth*displayHeight)-minSize)/(maxSize-minSize); 230 if (false) { 231 Slog.i("XXXXXX", "scaleMem=" + scaleMem); 232 Slog.i("XXXXXX", "scaleDisp=" + scaleDisp + " dw=" + displayWidth 233 + " dh=" + displayHeight); 234 } 235 236 float scale = scaleMem > scaleDisp ? scaleMem : scaleDisp; 237 if (scale < 0) scale = 0; 238 else if (scale > 1) scale = 1; 239 int minfree_adj = Resources.getSystem().getInteger( 240 com.android.internal.R.integer.config_lowMemoryKillerMinFreeKbytesAdjust); 241 int minfree_abs = Resources.getSystem().getInteger( 242 com.android.internal.R.integer.config_lowMemoryKillerMinFreeKbytesAbsolute); 243 if (false) { 244 Slog.i("XXXXXX", "minfree_adj=" + minfree_adj + " minfree_abs=" + minfree_abs); 245 } 246 247 final boolean is64bit = Build.SUPPORTED_64_BIT_ABIS.length > 0; 248 249 for (int i=0; i<mOomAdj.length; i++) { 250 int low = mOomMinFreeLow[i]; 251 int high = mOomMinFreeHigh[i]; 252 if (is64bit) { 253 // Increase the high min-free levels for cached processes for 64-bit 254 if (i == 4) high = (high*3)/2; 255 else if (i == 5) high = (high*7)/4; 256 } 257 mOomMinFree[i] = (int)(low + ((high-low)*scale)); 258 } 259 260 if (minfree_abs >= 0) { 261 for (int i=0; i<mOomAdj.length; i++) { 262 mOomMinFree[i] = (int)((float)minfree_abs * mOomMinFree[i] 263 / mOomMinFree[mOomAdj.length - 1]); 264 } 265 } 266 267 if (minfree_adj != 0) { 268 for (int i=0; i<mOomAdj.length; i++) { 269 mOomMinFree[i] += (int)((float)minfree_adj * mOomMinFree[i] 270 / mOomMinFree[mOomAdj.length - 1]); 271 if (mOomMinFree[i] < 0) { 272 mOomMinFree[i] = 0; 273 } 274 } 275 } 276 277 // The maximum size we will restore a process from cached to background, when under 278 // memory duress, is 1/3 the size we have reserved for kernel caches and other overhead 279 // before killing background processes. 280 mCachedRestoreLevel = (getMemLevel(ProcessList.CACHED_APP_MAX_ADJ)/1024) / 3; 281 282 // Ask the kernel to try to keep enough memory free to allocate 3 full 283 // screen 32bpp buffers without entering direct reclaim. 284 int reserve = displayWidth * displayHeight * 4 * 3 / 1024; 285 int reserve_adj = Resources.getSystem().getInteger(com.android.internal.R.integer.config_extraFreeKbytesAdjust); 286 int reserve_abs = Resources.getSystem().getInteger(com.android.internal.R.integer.config_extraFreeKbytesAbsolute); 287 288 if (reserve_abs >= 0) { 289 reserve = reserve_abs; 290 } 291 292 if (reserve_adj != 0) { 293 reserve += reserve_adj; 294 if (reserve < 0) { 295 reserve = 0; 296 } 297 } 298 299 if (write) { 300 ByteBuffer buf = ByteBuffer.allocate(4 * (2*mOomAdj.length + 1)); 301 buf.putInt(LMK_TARGET); 302 for (int i=0; i<mOomAdj.length; i++) { 303 buf.putInt((mOomMinFree[i]*1024)/PAGE_SIZE); 304 buf.putInt(mOomAdj[i]); 305 } 306 307 writeLmkd(buf); 308 SystemProperties.set("sys.sysctl.extra_free_kbytes", Integer.toString(reserve)); 309 } 310 // GB: 2048,3072,4096,6144,7168,8192 311 // HC: 8192,10240,12288,14336,16384,20480 312 } 313 314 public static int computeEmptyProcessLimit(int totalProcessLimit) { 315 return totalProcessLimit/2; 316 } 317 318 private static String buildOomTag(String prefix, String space, int val, int base) { 319 if (val == base) { 320 if (space == null) return prefix; 321 return prefix + " "; 322 } 323 return prefix + "+" + Integer.toString(val-base); 324 } 325 326 public static String makeOomAdjString(int setAdj) { 327 if (setAdj >= ProcessList.CACHED_APP_MIN_ADJ) { 328 return buildOomTag("cch", " ", setAdj, ProcessList.CACHED_APP_MIN_ADJ); 329 } else if (setAdj >= ProcessList.SERVICE_B_ADJ) { 330 return buildOomTag("svcb ", null, setAdj, ProcessList.SERVICE_B_ADJ); 331 } else if (setAdj >= ProcessList.PREVIOUS_APP_ADJ) { 332 return buildOomTag("prev ", null, setAdj, ProcessList.PREVIOUS_APP_ADJ); 333 } else if (setAdj >= ProcessList.HOME_APP_ADJ) { 334 return buildOomTag("home ", null, setAdj, ProcessList.HOME_APP_ADJ); 335 } else if (setAdj >= ProcessList.SERVICE_ADJ) { 336 return buildOomTag("svc ", null, setAdj, ProcessList.SERVICE_ADJ); 337 } else if (setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { 338 return buildOomTag("hvy ", null, setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); 339 } else if (setAdj >= ProcessList.BACKUP_APP_ADJ) { 340 return buildOomTag("bkup ", null, setAdj, ProcessList.BACKUP_APP_ADJ); 341 } else if (setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { 342 return buildOomTag("prcp ", null, setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); 343 } else if (setAdj >= ProcessList.VISIBLE_APP_ADJ) { 344 return buildOomTag("vis ", null, setAdj, ProcessList.VISIBLE_APP_ADJ); 345 } else if (setAdj >= ProcessList.FOREGROUND_APP_ADJ) { 346 return buildOomTag("fore ", null, setAdj, ProcessList.FOREGROUND_APP_ADJ); 347 } else if (setAdj >= ProcessList.PERSISTENT_SERVICE_ADJ) { 348 return buildOomTag("psvc ", null, setAdj, ProcessList.PERSISTENT_SERVICE_ADJ); 349 } else if (setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { 350 return buildOomTag("pers ", null, setAdj, ProcessList.PERSISTENT_PROC_ADJ); 351 } else if (setAdj >= ProcessList.SYSTEM_ADJ) { 352 return buildOomTag("sys ", null, setAdj, ProcessList.SYSTEM_ADJ); 353 } else if (setAdj >= ProcessList.NATIVE_ADJ) { 354 return buildOomTag("ntv ", null, setAdj, ProcessList.NATIVE_ADJ); 355 } else { 356 return Integer.toString(setAdj); 357 } 358 } 359 360 public static String makeProcStateString(int curProcState) { 361 String procState; 362 switch (curProcState) { 363 case -1: 364 procState = "N "; 365 break; 366 case ActivityManager.PROCESS_STATE_PERSISTENT: 367 procState = "P "; 368 break; 369 case ActivityManager.PROCESS_STATE_PERSISTENT_UI: 370 procState = "PU"; 371 break; 372 case ActivityManager.PROCESS_STATE_TOP: 373 procState = "T "; 374 break; 375 case ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE: 376 procState = "SB"; 377 break; 378 case ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE: 379 procState = "SF"; 380 break; 381 case ActivityManager.PROCESS_STATE_TOP_SLEEPING: 382 procState = "TS"; 383 break; 384 case ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND: 385 procState = "IF"; 386 break; 387 case ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND: 388 procState = "IB"; 389 break; 390 case ActivityManager.PROCESS_STATE_BACKUP: 391 procState = "BU"; 392 break; 393 case ActivityManager.PROCESS_STATE_HEAVY_WEIGHT: 394 procState = "HW"; 395 break; 396 case ActivityManager.PROCESS_STATE_SERVICE: 397 procState = "S "; 398 break; 399 case ActivityManager.PROCESS_STATE_RECEIVER: 400 procState = "R "; 401 break; 402 case ActivityManager.PROCESS_STATE_HOME: 403 procState = "HO"; 404 break; 405 case ActivityManager.PROCESS_STATE_LAST_ACTIVITY: 406 procState = "LA"; 407 break; 408 case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY: 409 procState = "CA"; 410 break; 411 case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT: 412 procState = "Ca"; 413 break; 414 case ActivityManager.PROCESS_STATE_CACHED_EMPTY: 415 procState = "CE"; 416 break; 417 default: 418 procState = "??"; 419 break; 420 } 421 return procState; 422 } 423 424 public static void appendRamKb(StringBuilder sb, long ramKb) { 425 for (int j=0, fact=10; j<6; j++, fact*=10) { 426 if (ramKb < fact) { 427 sb.append(' '); 428 } 429 } 430 sb.append(ramKb); 431 } 432 433 // How long after a state change that it is safe to collect PSS without it being dirty. 434 public static final int PSS_SAFE_TIME_FROM_STATE_CHANGE = 1000; 435 436 // The minimum time interval after a state change it is safe to collect PSS. 437 public static final int PSS_MIN_TIME_FROM_STATE_CHANGE = 15*1000; 438 439 // The maximum amount of time we want to go between PSS collections. 440 public static final int PSS_MAX_INTERVAL = 30*60*1000; 441 442 // The minimum amount of time between successive PSS requests for *all* processes. 443 public static final int PSS_ALL_INTERVAL = 10*60*1000; 444 445 // The minimum amount of time between successive PSS requests for a process. 446 private static final int PSS_SHORT_INTERVAL = 2*60*1000; 447 448 // The amount of time until PSS when a process first becomes top. 449 private static final int PSS_FIRST_TOP_INTERVAL = 10*1000; 450 451 // The amount of time until PSS when a process first goes into the background. 452 private static final int PSS_FIRST_BACKGROUND_INTERVAL = 20*1000; 453 454 // The amount of time until PSS when a process first becomes cached. 455 private static final int PSS_FIRST_CACHED_INTERVAL = 30*1000; 456 457 // The amount of time until PSS when an important process stays in the same state. 458 private static final int PSS_SAME_IMPORTANT_INTERVAL = 15*60*1000; 459 460 // The amount of time until PSS when a service process stays in the same state. 461 private static final int PSS_SAME_SERVICE_INTERVAL = 20*60*1000; 462 463 // The amount of time until PSS when a cached process stays in the same state. 464 private static final int PSS_SAME_CACHED_INTERVAL = 30*60*1000; 465 466 // The minimum time interval after a state change it is safe to collect PSS. 467 public static final int PSS_TEST_MIN_TIME_FROM_STATE_CHANGE = 10*1000; 468 469 // The amount of time during testing until PSS when a process first becomes top. 470 private static final int PSS_TEST_FIRST_TOP_INTERVAL = 3*1000; 471 472 // The amount of time during testing until PSS when a process first goes into the background. 473 private static final int PSS_TEST_FIRST_BACKGROUND_INTERVAL = 5*1000; 474 475 // The amount of time during testing until PSS when an important process stays in same state. 476 private static final int PSS_TEST_SAME_IMPORTANT_INTERVAL = 10*1000; 477 478 // The amount of time during testing until PSS when a background process stays in same state. 479 private static final int PSS_TEST_SAME_BACKGROUND_INTERVAL = 15*1000; 480 481 public static final int PROC_MEM_PERSISTENT = 0; 482 public static final int PROC_MEM_TOP = 1; 483 public static final int PROC_MEM_IMPORTANT = 2; 484 public static final int PROC_MEM_SERVICE = 3; 485 public static final int PROC_MEM_CACHED = 4; 486 487 private static final int[] sProcStateToProcMem = new int[] { 488 PROC_MEM_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT 489 PROC_MEM_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT_UI 490 PROC_MEM_TOP, // ActivityManager.PROCESS_STATE_TOP 491 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE 492 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE 493 PROC_MEM_TOP, // ActivityManager.PROCESS_STATE_TOP_SLEEPING 494 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND 495 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND 496 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_BACKUP 497 PROC_MEM_IMPORTANT, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT 498 PROC_MEM_SERVICE, // ActivityManager.PROCESS_STATE_SERVICE 499 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_RECEIVER 500 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_HOME 501 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY 502 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY 503 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT 504 PROC_MEM_CACHED, // ActivityManager.PROCESS_STATE_CACHED_EMPTY 505 }; 506 507 private static final long[] sFirstAwakePssTimes = new long[] { 508 PSS_SHORT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT 509 PSS_SHORT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT_UI 510 PSS_FIRST_TOP_INTERVAL, // ActivityManager.PROCESS_STATE_TOP 511 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE 512 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE 513 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_TOP_SLEEPING 514 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND 515 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND 516 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_BACKUP 517 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT 518 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_SERVICE 519 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_RECEIVER 520 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_HOME 521 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY 522 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY 523 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT 524 PSS_FIRST_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_EMPTY 525 }; 526 527 private static final long[] sSameAwakePssTimes = new long[] { 528 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT 529 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT_UI 530 PSS_SHORT_INTERVAL, // ActivityManager.PROCESS_STATE_TOP 531 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE 532 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE 533 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_TOP_SLEEPING 534 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND 535 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND 536 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_BACKUP 537 PSS_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT 538 PSS_SAME_SERVICE_INTERVAL, // ActivityManager.PROCESS_STATE_SERVICE 539 PSS_SAME_SERVICE_INTERVAL, // ActivityManager.PROCESS_STATE_RECEIVER 540 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_HOME 541 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY 542 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY 543 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT 544 PSS_SAME_CACHED_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_EMPTY 545 }; 546 547 private static final long[] sTestFirstAwakePssTimes = new long[] { 548 PSS_TEST_FIRST_TOP_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT 549 PSS_TEST_FIRST_TOP_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT_UI 550 PSS_TEST_FIRST_TOP_INTERVAL, // ActivityManager.PROCESS_STATE_TOP 551 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE 552 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE 553 PSS_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_TOP_SLEEPING 554 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND 555 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND 556 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_BACKUP 557 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT 558 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_SERVICE 559 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_RECEIVER 560 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_HOME 561 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY 562 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY 563 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT 564 PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_EMPTY 565 }; 566 567 private static final long[] sTestSameAwakePssTimes = new long[] { 568 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT 569 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_PERSISTENT_UI 570 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_TOP 571 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE 572 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE 573 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_TOP_SLEEPING 574 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND 575 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND 576 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_BACKUP 577 PSS_TEST_SAME_IMPORTANT_INTERVAL, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT 578 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_SERVICE 579 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_RECEIVER 580 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_HOME 581 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY 582 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY 583 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT 584 PSS_TEST_SAME_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_CACHED_EMPTY 585 }; 586 587 public static boolean procStatesDifferForMem(int procState1, int procState2) { 588 return sProcStateToProcMem[procState1] != sProcStateToProcMem[procState2]; 589 } 590 591 public static long minTimeFromStateChange(boolean test) { 592 return test ? PSS_TEST_MIN_TIME_FROM_STATE_CHANGE : PSS_MIN_TIME_FROM_STATE_CHANGE; 593 } 594 595 public static long computeNextPssTime(int procState, boolean first, boolean test, 596 boolean sleeping, long now) { 597 final long[] table = test 598 ? (first 599 ? sTestFirstAwakePssTimes 600 : sTestSameAwakePssTimes) 601 : (first 602 ? sFirstAwakePssTimes 603 : sSameAwakePssTimes); 604 return now + table[procState]; 605 } 606 607 long getMemLevel(int adjustment) { 608 for (int i=0; i<mOomAdj.length; i++) { 609 if (adjustment <= mOomAdj[i]) { 610 return mOomMinFree[i] * 1024; 611 } 612 } 613 return mOomMinFree[mOomAdj.length-1] * 1024; 614 } 615 616 /** 617 * Return the maximum pss size in kb that we consider a process acceptable to 618 * restore from its cached state for running in the background when RAM is low. 619 */ 620 long getCachedRestoreThresholdKb() { 621 return mCachedRestoreLevel; 622 } 623 624 /** 625 * Set the out-of-memory badness adjustment for a process. 626 * 627 * @param pid The process identifier to set. 628 * @param uid The uid of the app 629 * @param amt Adjustment value -- lmkd allows -16 to +15. 630 * 631 * {@hide} 632 */ 633 public static final void setOomAdj(int pid, int uid, int amt) { 634 if (amt == UNKNOWN_ADJ) 635 return; 636 637 long start = SystemClock.elapsedRealtime(); 638 ByteBuffer buf = ByteBuffer.allocate(4 * 4); 639 buf.putInt(LMK_PROCPRIO); 640 buf.putInt(pid); 641 buf.putInt(uid); 642 buf.putInt(amt); 643 writeLmkd(buf); 644 long now = SystemClock.elapsedRealtime(); 645 if ((now-start) > 250) { 646 Slog.w("ActivityManager", "SLOW OOM ADJ: " + (now-start) + "ms for pid " + pid 647 + " = " + amt); 648 } 649 } 650 651 /* 652 * {@hide} 653 */ 654 public static final void remove(int pid) { 655 ByteBuffer buf = ByteBuffer.allocate(4 * 2); 656 buf.putInt(LMK_PROCREMOVE); 657 buf.putInt(pid); 658 writeLmkd(buf); 659 } 660 661 private static boolean openLmkdSocket() { 662 try { 663 sLmkdSocket = new LocalSocket(LocalSocket.SOCKET_SEQPACKET); 664 sLmkdSocket.connect( 665 new LocalSocketAddress("lmkd", 666 LocalSocketAddress.Namespace.RESERVED)); 667 sLmkdOutputStream = sLmkdSocket.getOutputStream(); 668 } catch (IOException ex) { 669 Slog.w(TAG, "lowmemorykiller daemon socket open failed"); 670 sLmkdSocket = null; 671 return false; 672 } 673 674 return true; 675 } 676 677 private static void writeLmkd(ByteBuffer buf) { 678 679 for (int i = 0; i < 3; i++) { 680 if (sLmkdSocket == null) { 681 if (openLmkdSocket() == false) { 682 try { 683 Thread.sleep(1000); 684 } catch (InterruptedException ie) { 685 } 686 continue; 687 } 688 } 689 690 try { 691 sLmkdOutputStream.write(buf.array(), 0, buf.position()); 692 return; 693 } catch (IOException ex) { 694 Slog.w(TAG, "Error writing to lowmemorykiller socket"); 695 696 try { 697 sLmkdSocket.close(); 698 } catch (IOException ex2) { 699 } 700 701 sLmkdSocket = null; 702 } 703 } 704 } 705} 706