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 | |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.FileOutputStream; |
| 21 | import java.io.IOException; |
| 22 | |
| 23 | import com.android.internal.util.MemInfoReader; |
| 24 | import com.android.server.wm.WindowManagerService; |
| 25 | |
| 26 | import android.graphics.Point; |
| 27 | import android.os.StrictMode; |
| 28 | import android.util.Slog; |
| 29 | |
| 30 | /** |
| 31 | * Activity manager code dealing with processes. |
| 32 | */ |
| 33 | class ProcessList { |
| 34 | // The minimum time we allow between crashes, for us to consider this |
| 35 | // application to be bad and stop and its services and reject broadcasts. |
| 36 | static final int MIN_CRASH_INTERVAL = 60*1000; |
| 37 | |
| 38 | // OOM adjustments for processes in various states: |
| 39 | |
| 40 | // This is a process without anything currently running in it. Definitely |
| 41 | // the first to go! Value set in system/rootdir/init.rc on startup. |
| 42 | // This value is initalized in the constructor, careful when refering to |
| 43 | // this static variable externally. |
| 44 | static final int EMPTY_APP_ADJ = 15; |
| 45 | |
| 46 | // This is a process only hosting activities that are not visible, |
| 47 | // so it can be killed without any disruption. Value set in |
| 48 | // system/rootdir/init.rc on startup. |
| 49 | static final int HIDDEN_APP_MAX_ADJ = 15; |
| 50 | static int HIDDEN_APP_MIN_ADJ = 7; |
| 51 | |
| 52 | // This is a process holding the home application -- we want to try |
| 53 | // avoiding killing it, even if it would normally be in the background, |
| 54 | // because the user interacts with it so much. |
| 55 | static final int HOME_APP_ADJ = 6; |
| 56 | |
| 57 | // This is a process holding a secondary server -- killing it will not |
| 58 | // have much of an impact as far as the user is concerned. Value set in |
| 59 | // system/rootdir/init.rc on startup. |
| 60 | static final int SECONDARY_SERVER_ADJ = 5; |
| 61 | |
| 62 | // This is a process currently hosting a backup operation. Killing it |
| 63 | // is not entirely fatal but is generally a bad idea. |
| 64 | static final int BACKUP_APP_ADJ = 4; |
| 65 | |
| 66 | // This is a process with a heavy-weight application. It is in the |
| 67 | // background, but we want to try to avoid killing it. Value set in |
| 68 | // system/rootdir/init.rc on startup. |
| 69 | static final int HEAVY_WEIGHT_APP_ADJ = 3; |
| 70 | |
| 71 | // This is a process only hosting components that are perceptible to the |
| 72 | // user, and we really want to avoid killing them, but they are not |
| 73 | // immediately visible. An example is background music playback. Value set in |
| 74 | // system/rootdir/init.rc on startup. |
| 75 | static final int PERCEPTIBLE_APP_ADJ = 2; |
| 76 | |
| 77 | // This is a process only hosting activities that are visible to the |
| 78 | // user, so we'd prefer they don't disappear. Value set in |
| 79 | // system/rootdir/init.rc on startup. |
| 80 | static final int VISIBLE_APP_ADJ = 1; |
| 81 | |
| 82 | // This is the process running the current foreground app. We'd really |
| 83 | // rather not kill it! Value set in system/rootdir/init.rc on startup. |
| 84 | static final int FOREGROUND_APP_ADJ = 0; |
| 85 | |
| 86 | // This is a process running a core server, such as telephony. Definitely |
| 87 | // don't want to kill it, but doing so is not completely fatal. |
| 88 | static final int CORE_SERVER_ADJ = -12; |
| 89 | |
| 90 | // The system process runs at the default adjustment. |
| 91 | static final int SYSTEM_ADJ = -16; |
| 92 | |
| 93 | // Memory pages are 4K. |
| 94 | static final int PAGE_SIZE = 4*1024; |
| 95 | |
| 96 | // The minimum number of hidden apps we want to be able to keep around, |
| 97 | // without empty apps being able to push them out of memory. |
| 98 | static final int MIN_HIDDEN_APPS = 2; |
| 99 | |
| 100 | // The maximum number of hidden processes we will keep around before |
| 101 | // killing them; this is just a control to not let us go too crazy with |
| 102 | // keeping around processes on devices with large amounts of RAM. |
| 103 | static final int MAX_HIDDEN_APPS = 15; |
| 104 | |
| 105 | // We put empty content processes after any hidden processes that have |
| 106 | // been idle for less than 15 seconds. |
| 107 | static final long CONTENT_APP_IDLE_OFFSET = 15*1000; |
| 108 | |
| 109 | // We put empty content processes after any hidden processes that have |
| 110 | // been idle for less than 120 seconds. |
| 111 | static final long EMPTY_APP_IDLE_OFFSET = 120*1000; |
| 112 | |
| 113 | // These are the various interesting memory levels that we will give to |
| 114 | // the OOM killer. Note that the OOM killer only supports 6 slots, so we |
| 115 | // can't give it a different value for every possible kind of process. |
| 116 | private final int[] mOomAdj = new int[] { |
| 117 | FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ, |
| 118 | BACKUP_APP_ADJ, HIDDEN_APP_MIN_ADJ, EMPTY_APP_ADJ |
| 119 | }; |
| 120 | // These are the low-end OOM level limits. This is appropriate for an |
| 121 | // HVGA or smaller phone with less than 512MB. Values are in KB. |
| 122 | private final long[] mOomMinFreeLow = new long[] { |
| 123 | 8192, 12288, 16384, |
| 124 | 24576, 28672, 32768 |
| 125 | }; |
| 126 | // These are the high-end OOM level limits. This is appropriate for a |
| 127 | // 1280x800 or larger screen with around 1GB RAM. Values are in KB. |
| 128 | private final long[] mOomMinFreeHigh = new long[] { |
| 129 | 32768, 40960, 49152, |
| 130 | 57344, 65536, 81920 |
| 131 | }; |
| 132 | // The actual OOM killer memory levels we are using. |
| 133 | private final long[] mOomMinFree = new long[mOomAdj.length]; |
| 134 | |
| 135 | private final long mTotalMemMb; |
| 136 | |
| 137 | private boolean mHaveDisplaySize; |
| 138 | |
| 139 | ProcessList() { |
| 140 | MemInfoReader minfo = new MemInfoReader(); |
| 141 | minfo.readMemInfo(); |
| 142 | mTotalMemMb = minfo.getTotalSize()/(1024*1024); |
| 143 | updateOomLevels(0, 0, false); |
| 144 | } |
| 145 | |
| 146 | void applyDisplaySize(WindowManagerService wm) { |
| 147 | if (!mHaveDisplaySize) { |
| 148 | Point p = new Point(); |
| 149 | wm.getInitialDisplaySize(p); |
| 150 | if (p.x != 0 && p.y != 0) { |
| 151 | updateOomLevels(p.x, p.y, true); |
| 152 | mHaveDisplaySize = true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private void updateOomLevels(int displayWidth, int displayHeight, boolean write) { |
| 158 | // Scale buckets from avail memory: at 300MB we use the lowest values to |
| 159 | // 700MB or more for the top values. |
| 160 | float scaleMem = ((float)(mTotalMemMb-300))/(700-300); |
| 161 | |
| 162 | // Scale buckets from screen size. |
| 163 | int minSize = 320*480; // 153600 |
| 164 | int maxSize = 1280*800; // 1024000 230400 870400 .264 |
| 165 | float scaleDisp = ((float)(displayWidth*displayHeight)-minSize)/(maxSize-minSize); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 166 | //Slog.i("XXXXXX", "scaleDisp=" + scaleDisp + " dw=" + displayWidth + " dh=" + displayHeight); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 167 | |
| 168 | StringBuilder adjString = new StringBuilder(); |
| 169 | StringBuilder memString = new StringBuilder(); |
| 170 | |
| 171 | float scale = scaleMem > scaleDisp ? scaleMem : scaleDisp; |
| 172 | if (scale < 0) scale = 0; |
| 173 | else if (scale > 1) scale = 1; |
| 174 | for (int i=0; i<mOomAdj.length; i++) { |
| 175 | long low = mOomMinFreeLow[i]; |
| 176 | long high = mOomMinFreeHigh[i]; |
| 177 | mOomMinFree[i] = (long)(low + ((high-low)*scale)); |
| 178 | |
| 179 | if (i > 0) { |
| 180 | adjString.append(','); |
| 181 | memString.append(','); |
| 182 | } |
| 183 | adjString.append(mOomAdj[i]); |
| 184 | memString.append((mOomMinFree[i]*1024)/PAGE_SIZE); |
| 185 | } |
| 186 | |
| 187 | //Slog.i("XXXXXXX", "******************************* MINFREE: " + memString); |
| 188 | if (write) { |
| 189 | writeFile("/sys/module/lowmemorykiller/parameters/adj", adjString.toString()); |
| 190 | writeFile("/sys/module/lowmemorykiller/parameters/minfree", memString.toString()); |
| 191 | } |
| 192 | // GB: 2048,3072,4096,6144,7168,8192 |
| 193 | // HC: 8192,10240,12288,14336,16384,20480 |
| 194 | } |
| 195 | |
| 196 | long getMemLevel(int adjustment) { |
| 197 | for (int i=0; i<mOomAdj.length; i++) { |
| 198 | if (adjustment <= mOomAdj[i]) { |
| 199 | return mOomMinFree[i] * 1024; |
| 200 | } |
| 201 | } |
| 202 | return mOomMinFree[mOomAdj.length-1] * 1024; |
| 203 | } |
| 204 | |
| 205 | private void writeFile(String path, String data) { |
| 206 | FileOutputStream fos = null; |
| 207 | try { |
| 208 | fos = new FileOutputStream(path); |
| 209 | fos.write(data.getBytes()); |
| 210 | } catch (IOException e) { |
| 211 | Slog.w(ActivityManagerService.TAG, "Unable to write " + path); |
| 212 | } finally { |
| 213 | if (fos != null) { |
| 214 | try { |
| 215 | fos.close(); |
| 216 | } catch (IOException e) { |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |