Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 19 | import java.io.FileOutputStream; |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | import com.android.internal.util.MemInfoReader; |
| 23 | import com.android.server.wm.WindowManagerService; |
| 24 | |
| 25 | import android.graphics.Point; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 27 | import android.view.Display; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Activity manager code dealing with processes. |
| 31 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 32 | final class ProcessList { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 33 | // 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 Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 39 | // 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 Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 44 | // This is a process only hosting activities that are not visible, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 45 | // so it can be killed without any disruption. |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 46 | static final int CACHED_APP_MAX_ADJ = 15; |
Dianne Hackborn | 20cdcee | 2013-07-10 18:47:04 -0700 | [diff] [blame] | 47 | static final int CACHED_APP_MIN_ADJ = 9; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 48 | |
| 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 Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 51 | 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 Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 60 | |
| 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 Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 66 | // 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 Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 69 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 70 | // 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 Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 73 | 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 Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 78 | |
| 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 Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 81 | // immediately visible. An example is background music playback. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 82 | static final int PERCEPTIBLE_APP_ADJ = 2; |
| 83 | |
| 84 | // This is a process only hosting activities that are visible to the |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 85 | // user, so we'd prefer they don't disappear. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 86 | static final int VISIBLE_APP_ADJ = 1; |
| 87 | |
| 88 | // This is the process running the current foreground app. We'd really |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 89 | // rather not kill it! |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 90 | static final int FOREGROUND_APP_ADJ = 0; |
| 91 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 92 | // This is a system persistent process, such as telephony. Definitely |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 93 | // don't want to kill it, but doing so is not completely fatal. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 94 | static final int PERSISTENT_PROC_ADJ = -12; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 95 | |
| 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 Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 102 | // The minimum number of cached apps we want to be able to keep around, |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 103 | // without empty apps being able to push them out of memory. |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 104 | static final int MIN_CACHED_APPS = 2; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 105 | |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 106 | // 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 Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 115 | static final int MAX_CACHED_APPS = 24; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 116 | |
| 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 Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 120 | // 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 Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 122 | |
| 123 | // The number of empty apps at which we don't consider it necessary to do |
| 124 | // memory trimming. |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 125 | 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 Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 130 | |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 131 | // Threshold of number of cached+empty where we consider memory critical. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 132 | static final int TRIM_CRITICAL_THRESHOLD = 3; |
| 133 | |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 134 | // Threshold of number of cached+empty where we consider memory critical. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 135 | static final int TRIM_LOW_THRESHOLD = 5; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 136 | |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 137 | // We put empty content processes after any cached processes that have |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 138 | // been idle for less than 15 seconds. |
| 139 | static final long CONTENT_APP_IDLE_OFFSET = 15*1000; |
| 140 | |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 141 | // We put empty content processes after any cached processes that have |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 142 | // 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 Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 150 | BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 151 | }; |
| 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 Cheng | b5cda42 | 2013-03-27 17:36:13 -0700 | [diff] [blame] | 161 | 49152, 61440, 73728, |
| 162 | 86016, 98304, 122880 |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 163 | }; |
| 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 Cross | 637dbfc | 2013-07-18 17:15:15 -0700 | [diff] [blame] | 181 | wm.getBaseDisplaySize(Display.DEFAULT_DISPLAY, p); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 182 | 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 Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 198 | //Slog.i("XXXXXX", "scaleDisp=" + scaleDisp + " dw=" + displayWidth + " dh=" + displayHeight); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 199 | |
| 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 Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 228 | public static int computeEmptyProcessLimit(int totalProcessLimit) { |
| 229 | return (totalProcessLimit*2)/3; |
| 230 | } |
| 231 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 232 | 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 Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 241 | 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 | } |