1 /*
2  * Copyright (C) 2017 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 android.content.ContentResolver;
20 import android.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.Handler;
23 import android.provider.Settings;
24 import android.util.KeyValueListParser;
25 import android.util.Slog;
26 
27 import java.io.PrintWriter;
28 
29 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_POWER_QUICK;
30 
31 /**
32  * Settings constants that can modify the activity manager's behavior.
33  */
34 final class ActivityManagerConstants extends ContentObserver {
35     // Key names stored in the settings value.
36     private static final String KEY_MAX_CACHED_PROCESSES = "max_cached_processes";
37     private static final String KEY_BACKGROUND_SETTLE_TIME = "background_settle_time";
38     private static final String KEY_FGSERVICE_MIN_SHOWN_TIME
39             = "fgservice_min_shown_time";
40     private static final String KEY_FGSERVICE_MIN_REPORT_TIME
41             = "fgservice_min_report_time";
42     private static final String KEY_FGSERVICE_SCREEN_ON_BEFORE_TIME
43             = "fgservice_screen_on_before_time";
44     private static final String KEY_FGSERVICE_SCREEN_ON_AFTER_TIME
45             = "fgservice_screen_on_after_time";
46     private static final String KEY_CONTENT_PROVIDER_RETAIN_TIME = "content_provider_retain_time";
47     private static final String KEY_GC_TIMEOUT = "gc_timeout";
48     private static final String KEY_GC_MIN_INTERVAL = "gc_min_interval";
49     private static final String KEY_FULL_PSS_MIN_INTERVAL = "full_pss_min_interval";
50     private static final String KEY_FULL_PSS_LOWERED_INTERVAL = "full_pss_lowered_interval";
51     private static final String KEY_POWER_CHECK_DELAY = "power_check_delay";
52     private static final String KEY_WAKE_LOCK_MIN_CHECK_DURATION = "wake_lock_min_check_duration";
53     private static final String KEY_CPU_MIN_CHECK_DURATION = "cpu_min_check_duration";
54     private static final String KEY_SERVICE_USAGE_INTERACTION_TIME
55             = "service_usage_interaction_time";
56     private static final String KEY_USAGE_STATS_INTERACTION_INTERVAL
57             = "usage_stats_interaction_interval";
58     static final String KEY_SERVICE_RESTART_DURATION = "service_restart_duration";
59     static final String KEY_SERVICE_RESET_RUN_DURATION = "service_reset_run_duration";
60     static final String KEY_SERVICE_RESTART_DURATION_FACTOR = "service_restart_duration_factor";
61     static final String KEY_SERVICE_MIN_RESTART_TIME_BETWEEN = "service_min_restart_time_between";
62     static final String KEY_MAX_SERVICE_INACTIVITY = "service_max_inactivity";
63     static final String KEY_BG_START_TIMEOUT = "service_bg_start_timeout";
64 
65     private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
66     private static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60*1000;
67     private static final long DEFAULT_FGSERVICE_MIN_SHOWN_TIME = 2*1000;
68     private static final long DEFAULT_FGSERVICE_MIN_REPORT_TIME = 3*1000;
69     private static final long DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME = 1*1000;
70     private static final long DEFAULT_FGSERVICE_SCREEN_ON_AFTER_TIME = 5*1000;
71     private static final long DEFAULT_CONTENT_PROVIDER_RETAIN_TIME = 20*1000;
72     private static final long DEFAULT_GC_TIMEOUT = 5*1000;
73     private static final long DEFAULT_GC_MIN_INTERVAL = 60*1000;
74     private static final long DEFAULT_FULL_PSS_MIN_INTERVAL = 10*60*1000;
75     private static final long DEFAULT_FULL_PSS_LOWERED_INTERVAL = 2*60*1000;
76     private static final long DEFAULT_POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
77     private static final long DEFAULT_WAKE_LOCK_MIN_CHECK_DURATION
78             = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
79     private static final long DEFAULT_CPU_MIN_CHECK_DURATION
80             = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
81     private static final long DEFAULT_SERVICE_USAGE_INTERACTION_TIME = 30*60*1000;
82     private static final long DEFAULT_USAGE_STATS_INTERACTION_INTERVAL = 24*60*60*1000L;
83     private static final long DEFAULT_SERVICE_RESTART_DURATION = 1*1000;
84     private static final long DEFAULT_SERVICE_RESET_RUN_DURATION = 60*1000;
85     private static final int DEFAULT_SERVICE_RESTART_DURATION_FACTOR = 4;
86     private static final long DEFAULT_SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
87     private static final long DEFAULT_MAX_SERVICE_INACTIVITY = 30*60*1000;
88     private static final long DEFAULT_BG_START_TIMEOUT = 15*1000;
89 
90     // Maximum number of cached processes we will allow.
91     public int MAX_CACHED_PROCESSES = DEFAULT_MAX_CACHED_PROCESSES;
92 
93     // This is the amount of time we allow an app to settle after it goes into the background,
94     // before we start restricting what it can do.
95     public long BACKGROUND_SETTLE_TIME = DEFAULT_BACKGROUND_SETTLE_TIME;
96 
97     // The minimum time we allow a foreground service to run with a notification and the
98     // screen on without otherwise telling the user about it.  (If it runs for less than this,
99     // it will still be reported to the user as a running app for at least this amount of time.)
100     public long FGSERVICE_MIN_SHOWN_TIME = DEFAULT_FGSERVICE_MIN_SHOWN_TIME;
101 
102     // If a foreground service is shown for less than FGSERVICE_MIN_SHOWN_TIME, we will display
103     // the background app running notification about it for at least this amount of time (if it
104     // is larger than the remaining shown time).
105     public long FGSERVICE_MIN_REPORT_TIME = DEFAULT_FGSERVICE_MIN_REPORT_TIME;
106 
107     // The minimum amount of time the foreground service needs to have remain being shown
108     // before the screen goes on for us to consider it not worth showing to the user.  That is
109     // if an app has a foreground service that stops itself this amount of time or more before
110     // the user turns on the screen, we will just let it go without the user being told about it.
111     public long FGSERVICE_SCREEN_ON_BEFORE_TIME = DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME;
112 
113     // The minimum amount of time a foreground service should remain reported to the user if
114     // it is stopped when the screen turns on.  This is the time from when the screen turns
115     // on until we will stop reporting it.
116     public long FGSERVICE_SCREEN_ON_AFTER_TIME = DEFAULT_FGSERVICE_SCREEN_ON_AFTER_TIME;
117 
118     // How long we will retain processes hosting content providers in the "last activity"
119     // state before allowing them to drop down to the regular cached LRU list.  This is
120     // to avoid thrashing of provider processes under low memory situations.
121     long CONTENT_PROVIDER_RETAIN_TIME = DEFAULT_CONTENT_PROVIDER_RETAIN_TIME;
122 
123     // How long to wait after going idle before forcing apps to GC.
124     long GC_TIMEOUT = DEFAULT_GC_TIMEOUT;
125 
126     // The minimum amount of time between successive GC requests for a process.
127     long GC_MIN_INTERVAL = DEFAULT_GC_MIN_INTERVAL;
128 
129     // The minimum amount of time between successive PSS requests for a process.
130     long FULL_PSS_MIN_INTERVAL = DEFAULT_FULL_PSS_MIN_INTERVAL;
131 
132     // The minimum amount of time between successive PSS requests for a process
133     // when the request is due to the memory state being lowered.
134     long FULL_PSS_LOWERED_INTERVAL = DEFAULT_FULL_PSS_LOWERED_INTERVAL;
135 
136     // The rate at which we check for apps using excessive power -- 15 mins.
137     long POWER_CHECK_DELAY = DEFAULT_POWER_CHECK_DELAY;
138 
139     // The minimum sample duration we will allow before deciding we have
140     // enough data on wake locks to start killing things.
141     long WAKE_LOCK_MIN_CHECK_DURATION = DEFAULT_WAKE_LOCK_MIN_CHECK_DURATION;
142 
143     // The minimum sample duration we will allow before deciding we have
144     // enough data on CPU usage to start killing things.
145     long CPU_MIN_CHECK_DURATION = DEFAULT_CPU_MIN_CHECK_DURATION;
146 
147     // This is the amount of time an app needs to be running a foreground service before
148     // we will consider it to be doing interaction for usage stats.
149     long SERVICE_USAGE_INTERACTION_TIME = DEFAULT_SERVICE_USAGE_INTERACTION_TIME;
150 
151     // Maximum amount of time we will allow to elapse before re-reporting usage stats
152     // interaction with foreground processes.
153     long USAGE_STATS_INTERACTION_INTERVAL = DEFAULT_USAGE_STATS_INTERACTION_INTERVAL;
154 
155     // How long a service needs to be running until restarting its process
156     // is no longer considered to be a relaunch of the service.
157     public long SERVICE_RESTART_DURATION = DEFAULT_SERVICE_RESTART_DURATION;
158 
159     // How long a service needs to be running until it will start back at
160     // SERVICE_RESTART_DURATION after being killed.
161     public long SERVICE_RESET_RUN_DURATION = DEFAULT_SERVICE_RESET_RUN_DURATION;
162 
163     // Multiplying factor to increase restart duration time by, for each time
164     // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
165     public int SERVICE_RESTART_DURATION_FACTOR = DEFAULT_SERVICE_RESTART_DURATION_FACTOR;
166 
167     // The minimum amount of time between restarting services that we allow.
168     // That is, when multiple services are restarting, we won't allow each
169     // to restart less than this amount of time from the last one.
170     public long SERVICE_MIN_RESTART_TIME_BETWEEN = DEFAULT_SERVICE_MIN_RESTART_TIME_BETWEEN;
171 
172     // Maximum amount of time for there to be no activity on a service before
173     // we consider it non-essential and allow its process to go on the
174     // LRU background list.
175     public long MAX_SERVICE_INACTIVITY = DEFAULT_MAX_SERVICE_INACTIVITY;
176 
177     // How long we wait for a background started service to stop itself before
178     // allowing the next pending start to run.
179     public long BG_START_TIMEOUT = DEFAULT_BG_START_TIMEOUT;
180 
181     private final ActivityManagerService mService;
182     private ContentResolver mResolver;
183     private final KeyValueListParser mParser = new KeyValueListParser(',');
184 
185     private int mOverrideMaxCachedProcesses = -1;
186 
187     // The maximum number of cached processes we will keep around before killing them.
188     // NOTE: this constant is *only* a control to not let us go too crazy with
189     // keeping around processes on devices with large amounts of RAM.  For devices that
190     // are tighter on RAM, the out of memory killer is responsible for killing background
191     // processes as RAM is needed, and we should *never* be relying on this limit to
192     // kill them.  Also note that this limit only applies to cached background processes;
193     // we have no limit on the number of service, visible, foreground, or other such
194     // processes and the number of those processes does not count against the cached
195     // process limit.
196     public int CUR_MAX_CACHED_PROCESSES;
197 
198     // The maximum number of empty app processes we will let sit around.
199     public int CUR_MAX_EMPTY_PROCESSES;
200 
201     // The number of empty apps at which we don't consider it necessary to do
202     // memory trimming.
203     public int CUR_TRIM_EMPTY_PROCESSES;
204 
205     // The number of cached at which we don't consider it necessary to do
206     // memory trimming.
207     public int CUR_TRIM_CACHED_PROCESSES;
208 
ActivityManagerConstants(ActivityManagerService service, Handler handler)209     public ActivityManagerConstants(ActivityManagerService service, Handler handler) {
210         super(handler);
211         mService = service;
212         updateMaxCachedProcesses();
213     }
214 
start(ContentResolver resolver)215     public void start(ContentResolver resolver) {
216         mResolver = resolver;
217         mResolver.registerContentObserver(Settings.Global.getUriFor(
218                 Settings.Global.ACTIVITY_MANAGER_CONSTANTS), false, this);
219         updateConstants();
220     }
221 
setOverrideMaxCachedProcesses(int value)222     public void setOverrideMaxCachedProcesses(int value) {
223         mOverrideMaxCachedProcesses = value;
224         updateMaxCachedProcesses();
225     }
226 
getOverrideMaxCachedProcesses()227     public int getOverrideMaxCachedProcesses() {
228         return mOverrideMaxCachedProcesses;
229     }
230 
computeEmptyProcessLimit(int totalProcessLimit)231     public static int computeEmptyProcessLimit(int totalProcessLimit) {
232         return totalProcessLimit/2;
233     }
234 
235     @Override
onChange(boolean selfChange, Uri uri)236     public void onChange(boolean selfChange, Uri uri) {
237         updateConstants();
238     }
239 
updateConstants()240     private void updateConstants() {
241         final String setting = Settings.Global.getString(mResolver,
242                 Settings.Global.ACTIVITY_MANAGER_CONSTANTS);
243         synchronized (mService) {
244             try {
245                 mParser.setString(setting);
246             } catch (IllegalArgumentException e) {
247                 // Failed to parse the settings string, log this and move on
248                 // with defaults.
249                 Slog.e("ActivityManagerConstants", "Bad activity manager config settings", e);
250             }
251             MAX_CACHED_PROCESSES = mParser.getInt(KEY_MAX_CACHED_PROCESSES,
252                     DEFAULT_MAX_CACHED_PROCESSES);
253             BACKGROUND_SETTLE_TIME = mParser.getLong(KEY_BACKGROUND_SETTLE_TIME,
254                     DEFAULT_BACKGROUND_SETTLE_TIME);
255             FGSERVICE_MIN_SHOWN_TIME = mParser.getLong(KEY_FGSERVICE_MIN_SHOWN_TIME,
256                     DEFAULT_FGSERVICE_MIN_SHOWN_TIME);
257             FGSERVICE_MIN_REPORT_TIME = mParser.getLong(KEY_FGSERVICE_MIN_REPORT_TIME,
258                     DEFAULT_FGSERVICE_MIN_REPORT_TIME);
259             FGSERVICE_SCREEN_ON_BEFORE_TIME = mParser.getLong(KEY_FGSERVICE_SCREEN_ON_BEFORE_TIME,
260                     DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME);
261             FGSERVICE_SCREEN_ON_AFTER_TIME = mParser.getLong(KEY_FGSERVICE_SCREEN_ON_AFTER_TIME,
262                     DEFAULT_FGSERVICE_SCREEN_ON_AFTER_TIME);
263             CONTENT_PROVIDER_RETAIN_TIME = mParser.getLong(KEY_CONTENT_PROVIDER_RETAIN_TIME,
264                     DEFAULT_CONTENT_PROVIDER_RETAIN_TIME);
265             GC_TIMEOUT = mParser.getLong(KEY_GC_TIMEOUT,
266                     DEFAULT_GC_TIMEOUT);
267             GC_MIN_INTERVAL = mParser.getLong(KEY_GC_MIN_INTERVAL,
268                     DEFAULT_GC_MIN_INTERVAL);
269             FULL_PSS_MIN_INTERVAL = mParser.getLong(KEY_FULL_PSS_MIN_INTERVAL,
270                     DEFAULT_FULL_PSS_MIN_INTERVAL);
271             FULL_PSS_LOWERED_INTERVAL = mParser.getLong(KEY_FULL_PSS_LOWERED_INTERVAL,
272                     DEFAULT_FULL_PSS_LOWERED_INTERVAL);
273             POWER_CHECK_DELAY = mParser.getLong(KEY_POWER_CHECK_DELAY,
274                     DEFAULT_POWER_CHECK_DELAY);
275             WAKE_LOCK_MIN_CHECK_DURATION = mParser.getLong(KEY_WAKE_LOCK_MIN_CHECK_DURATION,
276                     DEFAULT_WAKE_LOCK_MIN_CHECK_DURATION);
277             CPU_MIN_CHECK_DURATION = mParser.getLong(KEY_CPU_MIN_CHECK_DURATION,
278                     DEFAULT_CPU_MIN_CHECK_DURATION);
279             SERVICE_USAGE_INTERACTION_TIME = mParser.getLong(KEY_SERVICE_USAGE_INTERACTION_TIME,
280                     DEFAULT_SERVICE_USAGE_INTERACTION_TIME);
281             USAGE_STATS_INTERACTION_INTERVAL = mParser.getLong(KEY_USAGE_STATS_INTERACTION_INTERVAL,
282                     DEFAULT_USAGE_STATS_INTERACTION_INTERVAL);
283             SERVICE_RESTART_DURATION = mParser.getLong(KEY_SERVICE_RESTART_DURATION,
284                     DEFAULT_SERVICE_RESTART_DURATION);
285             SERVICE_RESET_RUN_DURATION = mParser.getLong(KEY_SERVICE_RESET_RUN_DURATION,
286                     DEFAULT_SERVICE_RESET_RUN_DURATION);
287             SERVICE_RESTART_DURATION_FACTOR = mParser.getInt(KEY_SERVICE_RESTART_DURATION_FACTOR,
288                     DEFAULT_SERVICE_RESTART_DURATION_FACTOR);
289             SERVICE_MIN_RESTART_TIME_BETWEEN = mParser.getLong(KEY_SERVICE_MIN_RESTART_TIME_BETWEEN,
290                     DEFAULT_SERVICE_MIN_RESTART_TIME_BETWEEN);
291             MAX_SERVICE_INACTIVITY = mParser.getLong(KEY_MAX_SERVICE_INACTIVITY,
292                     DEFAULT_MAX_SERVICE_INACTIVITY);
293             BG_START_TIMEOUT = mParser.getLong(KEY_BG_START_TIMEOUT,
294                     DEFAULT_BG_START_TIMEOUT);
295             updateMaxCachedProcesses();
296         }
297     }
298 
updateMaxCachedProcesses()299     private void updateMaxCachedProcesses() {
300         CUR_MAX_CACHED_PROCESSES = mOverrideMaxCachedProcesses < 0
301                 ? MAX_CACHED_PROCESSES : mOverrideMaxCachedProcesses;
302         CUR_MAX_EMPTY_PROCESSES = computeEmptyProcessLimit(CUR_MAX_CACHED_PROCESSES);
303 
304         // Note the trim levels do NOT depend on the override process limit, we want
305         // to consider the same level the point where we do trimming regardless of any
306         // additional enforced limit.
307         final int rawMaxEmptyProcesses = computeEmptyProcessLimit(MAX_CACHED_PROCESSES);
308         CUR_TRIM_EMPTY_PROCESSES = rawMaxEmptyProcesses/2;
309         CUR_TRIM_CACHED_PROCESSES = (MAX_CACHED_PROCESSES-rawMaxEmptyProcesses)/3;
310     }
311 
312     void dump(PrintWriter pw) {
313         pw.println("ACTIVITY MANAGER SETTINGS (dumpsys activity settings) "
314                 + Settings.Global.ACTIVITY_MANAGER_CONSTANTS + ":");
315 
316         pw.print("  "); pw.print(KEY_MAX_CACHED_PROCESSES); pw.print("=");
317         pw.println(MAX_CACHED_PROCESSES);
318         pw.print("  "); pw.print(KEY_BACKGROUND_SETTLE_TIME); pw.print("=");
319         pw.println(BACKGROUND_SETTLE_TIME);
320         pw.print("  "); pw.print(KEY_FGSERVICE_MIN_SHOWN_TIME); pw.print("=");
321         pw.println(FGSERVICE_MIN_SHOWN_TIME);
322         pw.print("  "); pw.print(KEY_FGSERVICE_MIN_REPORT_TIME); pw.print("=");
323         pw.println(FGSERVICE_MIN_REPORT_TIME);
324         pw.print("  "); pw.print(KEY_FGSERVICE_SCREEN_ON_BEFORE_TIME); pw.print("=");
325         pw.println(FGSERVICE_SCREEN_ON_BEFORE_TIME);
326         pw.print("  "); pw.print(KEY_FGSERVICE_SCREEN_ON_AFTER_TIME); pw.print("=");
327         pw.println(FGSERVICE_SCREEN_ON_AFTER_TIME);
328         pw.print("  "); pw.print(KEY_CONTENT_PROVIDER_RETAIN_TIME); pw.print("=");
329         pw.println(CONTENT_PROVIDER_RETAIN_TIME);
330         pw.print("  "); pw.print(KEY_GC_TIMEOUT); pw.print("=");
331         pw.println(GC_TIMEOUT);
332         pw.print("  "); pw.print(KEY_GC_MIN_INTERVAL); pw.print("=");
333         pw.println(GC_MIN_INTERVAL);
334         pw.print("  "); pw.print(KEY_FULL_PSS_MIN_INTERVAL); pw.print("=");
335         pw.println(FULL_PSS_MIN_INTERVAL);
336         pw.print("  "); pw.print(KEY_FULL_PSS_LOWERED_INTERVAL); pw.print("=");
337         pw.println(FULL_PSS_LOWERED_INTERVAL);
338         pw.print("  "); pw.print(KEY_POWER_CHECK_DELAY); pw.print("=");
339         pw.println(POWER_CHECK_DELAY);
340         pw.print("  "); pw.print(KEY_WAKE_LOCK_MIN_CHECK_DURATION); pw.print("=");
341         pw.println(WAKE_LOCK_MIN_CHECK_DURATION);
342         pw.print("  "); pw.print(KEY_CPU_MIN_CHECK_DURATION); pw.print("=");
343         pw.println(CPU_MIN_CHECK_DURATION);
344         pw.print("  "); pw.print(KEY_SERVICE_USAGE_INTERACTION_TIME); pw.print("=");
345         pw.println(SERVICE_USAGE_INTERACTION_TIME);
346         pw.print("  "); pw.print(KEY_USAGE_STATS_INTERACTION_INTERVAL); pw.print("=");
347         pw.println(USAGE_STATS_INTERACTION_INTERVAL);
348         pw.print("  "); pw.print(KEY_SERVICE_RESTART_DURATION); pw.print("=");
349         pw.println(SERVICE_RESTART_DURATION);
350         pw.print("  "); pw.print(KEY_SERVICE_RESET_RUN_DURATION); pw.print("=");
351         pw.println(SERVICE_RESET_RUN_DURATION);
352         pw.print("  "); pw.print(KEY_SERVICE_RESTART_DURATION_FACTOR); pw.print("=");
353         pw.println(SERVICE_RESTART_DURATION_FACTOR);
354         pw.print("  "); pw.print(KEY_SERVICE_MIN_RESTART_TIME_BETWEEN); pw.print("=");
355         pw.println(SERVICE_MIN_RESTART_TIME_BETWEEN);
356         pw.print("  "); pw.print(KEY_MAX_SERVICE_INACTIVITY); pw.print("=");
357         pw.println(MAX_SERVICE_INACTIVITY);
358         pw.print("  "); pw.print(KEY_BG_START_TIMEOUT); pw.print("=");
359         pw.println(BG_START_TIMEOUT);
360 
361         pw.println();
362         if (mOverrideMaxCachedProcesses >= 0) {
363             pw.print("  mOverrideMaxCachedProcesses="); pw.println(mOverrideMaxCachedProcesses);
364         }
365         pw.print("  CUR_MAX_CACHED_PROCESSES="); pw.println(CUR_MAX_CACHED_PROCESSES);
366         pw.print("  CUR_MAX_EMPTY_PROCESSES="); pw.println(CUR_MAX_EMPTY_PROCESSES);
367         pw.print("  CUR_TRIM_EMPTY_PROCESSES="); pw.println(CUR_TRIM_EMPTY_PROCESSES);
368         pw.print("  CUR_TRIM_CACHED_PROCESSES="); pw.println(CUR_TRIM_CACHED_PROCESSES);
369     }
370 }
371