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