Home | History | Annotate | Download | only in am
      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 static com.android.server.am.ActivityManagerDebugConfig.DEBUG_POWER_QUICK;
     20 
     21 import android.app.ActivityThread;
     22 import android.content.ContentResolver;
     23 import android.content.Context;
     24 import android.database.ContentObserver;
     25 import android.net.Uri;
     26 import android.os.Build;
     27 import android.os.Handler;
     28 import android.provider.DeviceConfig;
     29 import android.provider.DeviceConfig.OnPropertiesChangedListener;
     30 import android.provider.DeviceConfig.Properties;
     31 import android.provider.Settings;
     32 import android.text.TextUtils;
     33 import android.util.KeyValueListParser;
     34 import android.util.Slog;
     35 
     36 import java.io.PrintWriter;
     37 
     38 /**
     39  * Settings constants that can modify the activity manager's behavior.
     40  */
     41 final class ActivityManagerConstants extends ContentObserver {
     42     private static final String TAG = "ActivityManagerConstants";
     43 
     44     // Key names stored in the settings value.
     45     private static final String KEY_BACKGROUND_SETTLE_TIME = "background_settle_time";
     46     private static final String KEY_FGSERVICE_MIN_SHOWN_TIME
     47             = "fgservice_min_shown_time";
     48     private static final String KEY_FGSERVICE_MIN_REPORT_TIME
     49             = "fgservice_min_report_time";
     50     private static final String KEY_FGSERVICE_SCREEN_ON_BEFORE_TIME
     51             = "fgservice_screen_on_before_time";
     52     private static final String KEY_FGSERVICE_SCREEN_ON_AFTER_TIME
     53             = "fgservice_screen_on_after_time";
     54     private static final String KEY_CONTENT_PROVIDER_RETAIN_TIME = "content_provider_retain_time";
     55     private static final String KEY_GC_TIMEOUT = "gc_timeout";
     56     private static final String KEY_GC_MIN_INTERVAL = "gc_min_interval";
     57     private static final String KEY_FULL_PSS_MIN_INTERVAL = "full_pss_min_interval";
     58     private static final String KEY_FULL_PSS_LOWERED_INTERVAL = "full_pss_lowered_interval";
     59     private static final String KEY_POWER_CHECK_INTERVAL = "power_check_interval";
     60     private static final String KEY_POWER_CHECK_MAX_CPU_1 = "power_check_max_cpu_1";
     61     private static final String KEY_POWER_CHECK_MAX_CPU_2 = "power_check_max_cpu_2";
     62     private static final String KEY_POWER_CHECK_MAX_CPU_3 = "power_check_max_cpu_3";
     63     private static final String KEY_POWER_CHECK_MAX_CPU_4 = "power_check_max_cpu_4";
     64     private static final String KEY_SERVICE_USAGE_INTERACTION_TIME
     65             = "service_usage_interaction_time";
     66     private static final String KEY_USAGE_STATS_INTERACTION_INTERVAL
     67             = "usage_stats_interaction_interval";
     68     static final String KEY_SERVICE_RESTART_DURATION = "service_restart_duration";
     69     static final String KEY_SERVICE_RESET_RUN_DURATION = "service_reset_run_duration";
     70     static final String KEY_SERVICE_RESTART_DURATION_FACTOR = "service_restart_duration_factor";
     71     static final String KEY_SERVICE_MIN_RESTART_TIME_BETWEEN = "service_min_restart_time_between";
     72     static final String KEY_MAX_SERVICE_INACTIVITY = "service_max_inactivity";
     73     static final String KEY_BG_START_TIMEOUT = "service_bg_start_timeout";
     74     static final String KEY_SERVICE_BG_ACTIVITY_START_TIMEOUT = "service_bg_activity_start_timeout";
     75     static final String KEY_BOUND_SERVICE_CRASH_RESTART_DURATION = "service_crash_restart_duration";
     76     static final String KEY_BOUND_SERVICE_CRASH_MAX_RETRY = "service_crash_max_retry";
     77     static final String KEY_PROCESS_START_ASYNC = "process_start_async";
     78     static final String KEY_MEMORY_INFO_THROTTLE_TIME = "memory_info_throttle_time";
     79     static final String KEY_TOP_TO_FGS_GRACE_DURATION = "top_to_fgs_grace_duration";
     80 
     81     private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
     82     private static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60*1000;
     83     private static final long DEFAULT_FGSERVICE_MIN_SHOWN_TIME = 2*1000;
     84     private static final long DEFAULT_FGSERVICE_MIN_REPORT_TIME = 3*1000;
     85     private static final long DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME = 1*1000;
     86     private static final long DEFAULT_FGSERVICE_SCREEN_ON_AFTER_TIME = 5*1000;
     87     private static final long DEFAULT_CONTENT_PROVIDER_RETAIN_TIME = 20*1000;
     88     private static final long DEFAULT_GC_TIMEOUT = 5*1000;
     89     private static final long DEFAULT_GC_MIN_INTERVAL = 60*1000;
     90     private static final long DEFAULT_FULL_PSS_MIN_INTERVAL = 20*60*1000;
     91     private static final long DEFAULT_FULL_PSS_LOWERED_INTERVAL = 5*60*1000;
     92     private static final long DEFAULT_POWER_CHECK_INTERVAL = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
     93     private static final int DEFAULT_POWER_CHECK_MAX_CPU_1 = 25;
     94     private static final int DEFAULT_POWER_CHECK_MAX_CPU_2 = 25;
     95     private static final int DEFAULT_POWER_CHECK_MAX_CPU_3 = 10;
     96     private static final int DEFAULT_POWER_CHECK_MAX_CPU_4 = 2;
     97     private static final long DEFAULT_SERVICE_USAGE_INTERACTION_TIME = 30*60*1000;
     98     private static final long DEFAULT_USAGE_STATS_INTERACTION_INTERVAL = 2*60*60*1000L;
     99     private static final long DEFAULT_SERVICE_RESTART_DURATION = 1*1000;
    100     private static final long DEFAULT_SERVICE_RESET_RUN_DURATION = 60*1000;
    101     private static final int DEFAULT_SERVICE_RESTART_DURATION_FACTOR = 4;
    102     private static final long DEFAULT_SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
    103     private static final long DEFAULT_MAX_SERVICE_INACTIVITY = 30*60*1000;
    104     private static final long DEFAULT_BG_START_TIMEOUT = 15*1000;
    105     private static final long DEFAULT_SERVICE_BG_ACTIVITY_START_TIMEOUT = 10_000;
    106     private static final long DEFAULT_BOUND_SERVICE_CRASH_RESTART_DURATION = 30*60_000;
    107     private static final int DEFAULT_BOUND_SERVICE_CRASH_MAX_RETRY = 16;
    108     private static final boolean DEFAULT_PROCESS_START_ASYNC = true;
    109     private static final long DEFAULT_MEMORY_INFO_THROTTLE_TIME = 5*60*1000;
    110     private static final long DEFAULT_TOP_TO_FGS_GRACE_DURATION = 15 * 1000;
    111 
    112     // Flag stored in the DeviceConfig API.
    113     /**
    114      * Maximum number of cached processes.
    115      */
    116     private static final String KEY_MAX_CACHED_PROCESSES = "max_cached_processes";
    117 
    118     /**
    119      * Default value for mFlagBackgroundActivityStartsEnabled if not explicitly set in
    120      * Settings.Global. This allows it to be set experimentally unless it has been
    121      * enabled/disabled in developer options. Defaults to false.
    122      */
    123     private static final String KEY_DEFAULT_BACKGROUND_ACTIVITY_STARTS_ENABLED =
    124             "default_background_activity_starts_enabled";
    125 
    126 
    127     // Maximum number of cached processes we will allow.
    128     public int MAX_CACHED_PROCESSES = DEFAULT_MAX_CACHED_PROCESSES;
    129 
    130     // This is the amount of time we allow an app to settle after it goes into the background,
    131     // before we start restricting what it can do.
    132     public long BACKGROUND_SETTLE_TIME = DEFAULT_BACKGROUND_SETTLE_TIME;
    133 
    134     // The minimum time we allow a foreground service to run with a notification and the
    135     // screen on without otherwise telling the user about it.  (If it runs for less than this,
    136     // it will still be reported to the user as a running app for at least this amount of time.)
    137     public long FGSERVICE_MIN_SHOWN_TIME = DEFAULT_FGSERVICE_MIN_SHOWN_TIME;
    138 
    139     // If a foreground service is shown for less than FGSERVICE_MIN_SHOWN_TIME, we will display
    140     // the background app running notification about it for at least this amount of time (if it
    141     // is larger than the remaining shown time).
    142     public long FGSERVICE_MIN_REPORT_TIME = DEFAULT_FGSERVICE_MIN_REPORT_TIME;
    143 
    144     // The minimum amount of time the foreground service needs to have remain being shown
    145     // before the screen goes on for us to consider it not worth showing to the user.  That is
    146     // if an app has a foreground service that stops itself this amount of time or more before
    147     // the user turns on the screen, we will just let it go without the user being told about it.
    148     public long FGSERVICE_SCREEN_ON_BEFORE_TIME = DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME;
    149 
    150     // The minimum amount of time a foreground service should remain reported to the user if
    151     // it is stopped when the screen turns on.  This is the time from when the screen turns
    152     // on until we will stop reporting it.
    153     public long FGSERVICE_SCREEN_ON_AFTER_TIME = DEFAULT_FGSERVICE_SCREEN_ON_AFTER_TIME;
    154 
    155     // How long we will retain processes hosting content providers in the "last activity"
    156     // state before allowing them to drop down to the regular cached LRU list.  This is
    157     // to avoid thrashing of provider processes under low memory situations.
    158     long CONTENT_PROVIDER_RETAIN_TIME = DEFAULT_CONTENT_PROVIDER_RETAIN_TIME;
    159 
    160     // How long to wait after going idle before forcing apps to GC.
    161     long GC_TIMEOUT = DEFAULT_GC_TIMEOUT;
    162 
    163     // The minimum amount of time between successive GC requests for a process.
    164     long GC_MIN_INTERVAL = DEFAULT_GC_MIN_INTERVAL;
    165 
    166     // The minimum amount of time between successive PSS requests for a process.
    167     long FULL_PSS_MIN_INTERVAL = DEFAULT_FULL_PSS_MIN_INTERVAL;
    168 
    169     // The minimum amount of time between successive PSS requests for a process
    170     // when the request is due to the memory state being lowered.
    171     long FULL_PSS_LOWERED_INTERVAL = DEFAULT_FULL_PSS_LOWERED_INTERVAL;
    172 
    173     // The minimum sample duration we will allow before deciding we have
    174     // enough data on CPU usage to start killing things.
    175     long POWER_CHECK_INTERVAL = DEFAULT_POWER_CHECK_INTERVAL;
    176 
    177     // The maximum CPU (as a percentage) a process is allowed to use over the first
    178     // power check interval that it is cached.
    179     int POWER_CHECK_MAX_CPU_1 = DEFAULT_POWER_CHECK_MAX_CPU_1;
    180 
    181     // The maximum CPU (as a percentage) a process is allowed to use over the second
    182     // power check interval that it is cached.  The home app will never check for less
    183     // CPU than this (it will not test against the 3 or 4 levels).
    184     int POWER_CHECK_MAX_CPU_2 = DEFAULT_POWER_CHECK_MAX_CPU_2;
    185 
    186     // The maximum CPU (as a percentage) a process is allowed to use over the third
    187     // power check interval that it is cached.
    188     int POWER_CHECK_MAX_CPU_3 = DEFAULT_POWER_CHECK_MAX_CPU_3;
    189 
    190     // The maximum CPU (as a percentage) a process is allowed to use over the fourth
    191     // power check interval that it is cached.
    192     int POWER_CHECK_MAX_CPU_4 = DEFAULT_POWER_CHECK_MAX_CPU_4;
    193 
    194     // This is the amount of time an app needs to be running a foreground service before
    195     // we will consider it to be doing interaction for usage stats.
    196     long SERVICE_USAGE_INTERACTION_TIME = DEFAULT_SERVICE_USAGE_INTERACTION_TIME;
    197 
    198     // Maximum amount of time we will allow to elapse before re-reporting usage stats
    199     // interaction with foreground processes.
    200     long USAGE_STATS_INTERACTION_INTERVAL = DEFAULT_USAGE_STATS_INTERACTION_INTERVAL;
    201 
    202     // How long a service needs to be running until restarting its process
    203     // is no longer considered to be a relaunch of the service.
    204     public long SERVICE_RESTART_DURATION = DEFAULT_SERVICE_RESTART_DURATION;
    205 
    206     // How long a service needs to be running until it will start back at
    207     // SERVICE_RESTART_DURATION after being killed.
    208     public long SERVICE_RESET_RUN_DURATION = DEFAULT_SERVICE_RESET_RUN_DURATION;
    209 
    210     // Multiplying factor to increase restart duration time by, for each time
    211     // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
    212     public int SERVICE_RESTART_DURATION_FACTOR = DEFAULT_SERVICE_RESTART_DURATION_FACTOR;
    213 
    214     // The minimum amount of time between restarting services that we allow.
    215     // That is, when multiple services are restarting, we won't allow each
    216     // to restart less than this amount of time from the last one.
    217     public long SERVICE_MIN_RESTART_TIME_BETWEEN = DEFAULT_SERVICE_MIN_RESTART_TIME_BETWEEN;
    218 
    219     // Maximum amount of time for there to be no activity on a service before
    220     // we consider it non-essential and allow its process to go on the
    221     // LRU background list.
    222     public long MAX_SERVICE_INACTIVITY = DEFAULT_MAX_SERVICE_INACTIVITY;
    223 
    224     // How long we wait for a background started service to stop itself before
    225     // allowing the next pending start to run.
    226     public long BG_START_TIMEOUT = DEFAULT_BG_START_TIMEOUT;
    227 
    228     // For how long after a whitelisted service's start its process can start a background activity
    229     public long SERVICE_BG_ACTIVITY_START_TIMEOUT = DEFAULT_SERVICE_BG_ACTIVITY_START_TIMEOUT;
    230 
    231     // Initial backoff delay for retrying bound foreground services
    232     public long BOUND_SERVICE_CRASH_RESTART_DURATION = DEFAULT_BOUND_SERVICE_CRASH_RESTART_DURATION;
    233 
    234     // Maximum number of retries for bound foreground services that crash soon after start
    235     public long BOUND_SERVICE_MAX_CRASH_RETRY = DEFAULT_BOUND_SERVICE_CRASH_MAX_RETRY;
    236 
    237     // Indicates if the processes need to be started asynchronously.
    238     public boolean FLAG_PROCESS_START_ASYNC = DEFAULT_PROCESS_START_ASYNC;
    239 
    240     // The minimum time we allow between requests for the MemoryInfo of a process to
    241     // throttle requests from apps.
    242     public long MEMORY_INFO_THROTTLE_TIME = DEFAULT_MEMORY_INFO_THROTTLE_TIME;
    243 
    244     // Allow app just moving from TOP to FOREGROUND_SERVICE to stay in a higher adj value for
    245     // this long.
    246     public long TOP_TO_FGS_GRACE_DURATION = DEFAULT_TOP_TO_FGS_GRACE_DURATION;
    247 
    248     // Indicates whether the activity starts logging is enabled.
    249     // Controlled by Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED
    250     volatile boolean mFlagActivityStartsLoggingEnabled;
    251 
    252     // Indicates whether the background activity starts is enabled.
    253     // Controlled by Settings.Global.BACKGROUND_ACTIVITY_STARTS_ENABLED.
    254     // If not set explicitly the default is controlled by DeviceConfig.
    255     volatile boolean mFlagBackgroundActivityStartsEnabled;
    256 
    257     private final ActivityManagerService mService;
    258     private ContentResolver mResolver;
    259     private final KeyValueListParser mParser = new KeyValueListParser(',');
    260 
    261     private int mOverrideMaxCachedProcesses = -1;
    262 
    263     // The maximum number of cached processes we will keep around before killing them.
    264     // NOTE: this constant is *only* a control to not let us go too crazy with
    265     // keeping around processes on devices with large amounts of RAM.  For devices that
    266     // are tighter on RAM, the out of memory killer is responsible for killing background
    267     // processes as RAM is needed, and we should *never* be relying on this limit to
    268     // kill them.  Also note that this limit only applies to cached background processes;
    269     // we have no limit on the number of service, visible, foreground, or other such
    270     // processes and the number of those processes does not count against the cached
    271     // process limit.
    272     public int CUR_MAX_CACHED_PROCESSES;
    273 
    274     // The maximum number of empty app processes we will let sit around.
    275     public int CUR_MAX_EMPTY_PROCESSES;
    276 
    277     // The number of empty apps at which we don't consider it necessary to do
    278     // memory trimming.
    279     public int CUR_TRIM_EMPTY_PROCESSES;
    280 
    281     // The number of cached at which we don't consider it necessary to do
    282     // memory trimming.
    283     public int CUR_TRIM_CACHED_PROCESSES;
    284 
    285     private static final long MIN_AUTOMATIC_HEAP_DUMP_PSS_THRESHOLD_BYTES = 100 * 1024; // 100 KB
    286 
    287     private final boolean mSystemServerAutomaticHeapDumpEnabled;
    288 
    289     /** Package to report to when the memory usage exceeds the limit. */
    290     private final String mSystemServerAutomaticHeapDumpPackageName;
    291 
    292     /** Byte limit for dump heap monitoring. */
    293     private long mSystemServerAutomaticHeapDumpPssThresholdBytes;
    294 
    295     private static final Uri ACTIVITY_MANAGER_CONSTANTS_URI = Settings.Global.getUriFor(
    296                 Settings.Global.ACTIVITY_MANAGER_CONSTANTS);
    297 
    298     private static final Uri ACTIVITY_STARTS_LOGGING_ENABLED_URI = Settings.Global.getUriFor(
    299                 Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED);
    300 
    301     private static final Uri ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS_URI =
    302             Settings.Global.getUriFor(Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS);
    303 
    304     private final OnPropertiesChangedListener mOnDeviceConfigChangedListener =
    305             new OnPropertiesChangedListener() {
    306                 @Override
    307                 public void onPropertiesChanged(Properties properties) {
    308                     for (String name : properties.getKeyset()) {
    309                         if (name == null) {
    310                             return;
    311                         }
    312                         switch (name) {
    313                             case KEY_MAX_CACHED_PROCESSES:
    314                                 updateMaxCachedProcesses();
    315                                 break;
    316                             case KEY_DEFAULT_BACKGROUND_ACTIVITY_STARTS_ENABLED:
    317                                 updateBackgroundActivityStarts();
    318                                 break;
    319                             default:
    320                                 break;
    321                         }
    322                     }
    323                 }
    324             };
    325 
    326     ActivityManagerConstants(Context context, ActivityManagerService service, Handler handler) {
    327         super(handler);
    328         mService = service;
    329         mSystemServerAutomaticHeapDumpEnabled = Build.IS_DEBUGGABLE
    330                 && context.getResources().getBoolean(
    331                 com.android.internal.R.bool.config_debugEnableAutomaticSystemServerHeapDumps);
    332         mSystemServerAutomaticHeapDumpPackageName = context.getPackageName();
    333         mSystemServerAutomaticHeapDumpPssThresholdBytes = Math.max(
    334                 MIN_AUTOMATIC_HEAP_DUMP_PSS_THRESHOLD_BYTES,
    335                 context.getResources().getInteger(
    336                         com.android.internal.R.integer.config_debugSystemServerPssThresholdBytes));
    337     }
    338 
    339     public void start(ContentResolver resolver) {
    340         mResolver = resolver;
    341         mResolver.registerContentObserver(ACTIVITY_MANAGER_CONSTANTS_URI, false, this);
    342         mResolver.registerContentObserver(ACTIVITY_STARTS_LOGGING_ENABLED_URI, false, this);
    343         if (mSystemServerAutomaticHeapDumpEnabled) {
    344             mResolver.registerContentObserver(ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS_URI,
    345                     false, this);
    346         }
    347         updateConstants();
    348         if (mSystemServerAutomaticHeapDumpEnabled) {
    349             updateEnableAutomaticSystemServerHeapDumps();
    350         }
    351         DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
    352                 ActivityThread.currentApplication().getMainExecutor(),
    353                 mOnDeviceConfigChangedListener);
    354         updateMaxCachedProcesses();
    355         updateActivityStartsLoggingEnabled();
    356         updateBackgroundActivityStarts();
    357     }
    358 
    359     public void setOverrideMaxCachedProcesses(int value) {
    360         mOverrideMaxCachedProcesses = value;
    361         updateMaxCachedProcesses();
    362     }
    363 
    364     public int getOverrideMaxCachedProcesses() {
    365         return mOverrideMaxCachedProcesses;
    366     }
    367 
    368     public static int computeEmptyProcessLimit(int totalProcessLimit) {
    369         return totalProcessLimit/2;
    370     }
    371 
    372     @Override
    373     public void onChange(boolean selfChange, Uri uri) {
    374         if (uri == null) return;
    375         if (ACTIVITY_MANAGER_CONSTANTS_URI.equals(uri)) {
    376             updateConstants();
    377         } else if (ACTIVITY_STARTS_LOGGING_ENABLED_URI.equals(uri)) {
    378             updateActivityStartsLoggingEnabled();
    379         } else if (ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS_URI.equals(uri)) {
    380             updateEnableAutomaticSystemServerHeapDumps();
    381         }
    382     }
    383 
    384     private void updateConstants() {
    385         final String setting = Settings.Global.getString(mResolver,
    386                 Settings.Global.ACTIVITY_MANAGER_CONSTANTS);
    387         synchronized (mService) {
    388             try {
    389                 mParser.setString(setting);
    390             } catch (IllegalArgumentException e) {
    391                 // Failed to parse the settings string, log this and move on
    392                 // with defaults.
    393                 Slog.e("ActivityManagerConstants", "Bad activity manager config settings", e);
    394             }
    395             BACKGROUND_SETTLE_TIME = mParser.getLong(KEY_BACKGROUND_SETTLE_TIME,
    396                     DEFAULT_BACKGROUND_SETTLE_TIME);
    397             FGSERVICE_MIN_SHOWN_TIME = mParser.getLong(KEY_FGSERVICE_MIN_SHOWN_TIME,
    398                     DEFAULT_FGSERVICE_MIN_SHOWN_TIME);
    399             FGSERVICE_MIN_REPORT_TIME = mParser.getLong(KEY_FGSERVICE_MIN_REPORT_TIME,
    400                     DEFAULT_FGSERVICE_MIN_REPORT_TIME);
    401             FGSERVICE_SCREEN_ON_BEFORE_TIME = mParser.getLong(KEY_FGSERVICE_SCREEN_ON_BEFORE_TIME,
    402                     DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME);
    403             FGSERVICE_SCREEN_ON_AFTER_TIME = mParser.getLong(KEY_FGSERVICE_SCREEN_ON_AFTER_TIME,
    404                     DEFAULT_FGSERVICE_SCREEN_ON_AFTER_TIME);
    405             CONTENT_PROVIDER_RETAIN_TIME = mParser.getLong(KEY_CONTENT_PROVIDER_RETAIN_TIME,
    406                     DEFAULT_CONTENT_PROVIDER_RETAIN_TIME);
    407             GC_TIMEOUT = mParser.getLong(KEY_GC_TIMEOUT,
    408                     DEFAULT_GC_TIMEOUT);
    409             GC_MIN_INTERVAL = mParser.getLong(KEY_GC_MIN_INTERVAL,
    410                     DEFAULT_GC_MIN_INTERVAL);
    411             FULL_PSS_MIN_INTERVAL = mParser.getLong(KEY_FULL_PSS_MIN_INTERVAL,
    412                     DEFAULT_FULL_PSS_MIN_INTERVAL);
    413             FULL_PSS_LOWERED_INTERVAL = mParser.getLong(KEY_FULL_PSS_LOWERED_INTERVAL,
    414                     DEFAULT_FULL_PSS_LOWERED_INTERVAL);
    415             POWER_CHECK_INTERVAL = mParser.getLong(KEY_POWER_CHECK_INTERVAL,
    416                     DEFAULT_POWER_CHECK_INTERVAL);
    417             POWER_CHECK_MAX_CPU_1 = mParser.getInt(KEY_POWER_CHECK_MAX_CPU_1,
    418                     DEFAULT_POWER_CHECK_MAX_CPU_1);
    419             POWER_CHECK_MAX_CPU_2 = mParser.getInt(KEY_POWER_CHECK_MAX_CPU_2,
    420                     DEFAULT_POWER_CHECK_MAX_CPU_2);
    421             POWER_CHECK_MAX_CPU_3 = mParser.getInt(KEY_POWER_CHECK_MAX_CPU_3,
    422                     DEFAULT_POWER_CHECK_MAX_CPU_3);
    423             POWER_CHECK_MAX_CPU_4 = mParser.getInt(KEY_POWER_CHECK_MAX_CPU_4,
    424                     DEFAULT_POWER_CHECK_MAX_CPU_4);
    425             SERVICE_USAGE_INTERACTION_TIME = mParser.getLong(KEY_SERVICE_USAGE_INTERACTION_TIME,
    426                     DEFAULT_SERVICE_USAGE_INTERACTION_TIME);
    427             USAGE_STATS_INTERACTION_INTERVAL = mParser.getLong(KEY_USAGE_STATS_INTERACTION_INTERVAL,
    428                     DEFAULT_USAGE_STATS_INTERACTION_INTERVAL);
    429             SERVICE_RESTART_DURATION = mParser.getLong(KEY_SERVICE_RESTART_DURATION,
    430                     DEFAULT_SERVICE_RESTART_DURATION);
    431             SERVICE_RESET_RUN_DURATION = mParser.getLong(KEY_SERVICE_RESET_RUN_DURATION,
    432                     DEFAULT_SERVICE_RESET_RUN_DURATION);
    433             SERVICE_RESTART_DURATION_FACTOR = mParser.getInt(KEY_SERVICE_RESTART_DURATION_FACTOR,
    434                     DEFAULT_SERVICE_RESTART_DURATION_FACTOR);
    435             SERVICE_MIN_RESTART_TIME_BETWEEN = mParser.getLong(KEY_SERVICE_MIN_RESTART_TIME_BETWEEN,
    436                     DEFAULT_SERVICE_MIN_RESTART_TIME_BETWEEN);
    437             MAX_SERVICE_INACTIVITY = mParser.getLong(KEY_MAX_SERVICE_INACTIVITY,
    438                     DEFAULT_MAX_SERVICE_INACTIVITY);
    439             BG_START_TIMEOUT = mParser.getLong(KEY_BG_START_TIMEOUT,
    440                     DEFAULT_BG_START_TIMEOUT);
    441             SERVICE_BG_ACTIVITY_START_TIMEOUT = mParser.getLong(
    442                     KEY_SERVICE_BG_ACTIVITY_START_TIMEOUT,
    443                     DEFAULT_SERVICE_BG_ACTIVITY_START_TIMEOUT);
    444             BOUND_SERVICE_CRASH_RESTART_DURATION = mParser.getLong(
    445                 KEY_BOUND_SERVICE_CRASH_RESTART_DURATION,
    446                 DEFAULT_BOUND_SERVICE_CRASH_RESTART_DURATION);
    447             BOUND_SERVICE_MAX_CRASH_RETRY = mParser.getInt(KEY_BOUND_SERVICE_CRASH_MAX_RETRY,
    448                 DEFAULT_BOUND_SERVICE_CRASH_MAX_RETRY);
    449             FLAG_PROCESS_START_ASYNC = mParser.getBoolean(KEY_PROCESS_START_ASYNC,
    450                     DEFAULT_PROCESS_START_ASYNC);
    451             MEMORY_INFO_THROTTLE_TIME = mParser.getLong(KEY_MEMORY_INFO_THROTTLE_TIME,
    452                     DEFAULT_MEMORY_INFO_THROTTLE_TIME);
    453             TOP_TO_FGS_GRACE_DURATION = mParser.getDurationMillis(KEY_TOP_TO_FGS_GRACE_DURATION,
    454                     DEFAULT_TOP_TO_FGS_GRACE_DURATION);
    455 
    456             // For new flags that are intended for server-side experiments, please use the new
    457             // DeviceConfig package.
    458 
    459             updateMaxCachedProcesses();
    460         }
    461     }
    462 
    463     private void updateActivityStartsLoggingEnabled() {
    464         mFlagActivityStartsLoggingEnabled = Settings.Global.getInt(mResolver,
    465                 Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED, 1) == 1;
    466     }
    467 
    468     private void updateBackgroundActivityStarts() {
    469         mFlagBackgroundActivityStartsEnabled = DeviceConfig.getBoolean(
    470                 DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
    471                 KEY_DEFAULT_BACKGROUND_ACTIVITY_STARTS_ENABLED,
    472                 /*defaultValue*/ false);
    473     }
    474 
    475     private void updateEnableAutomaticSystemServerHeapDumps() {
    476         if (!mSystemServerAutomaticHeapDumpEnabled) {
    477             Slog.wtf(TAG,
    478                     "updateEnableAutomaticSystemServerHeapDumps called when leak detection "
    479                             + "disabled");
    480             return;
    481         }
    482         // Monitoring is on by default, so if the setting hasn't been set by the user,
    483         // monitoring should be on.
    484         final boolean enabled = Settings.Global.getInt(mResolver,
    485                 Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, 1) == 1;
    486 
    487         // Setting the threshold to 0 stops the checking.
    488         final long threshold = enabled ? mSystemServerAutomaticHeapDumpPssThresholdBytes : 0;
    489         mService.setDumpHeapDebugLimit(null, 0, threshold,
    490                 mSystemServerAutomaticHeapDumpPackageName);
    491     }
    492 
    493     private void updateMaxCachedProcesses() {
    494         String maxCachedProcessesFlag = DeviceConfig.getProperty(
    495                 DeviceConfig.NAMESPACE_ACTIVITY_MANAGER, KEY_MAX_CACHED_PROCESSES);
    496         try {
    497             CUR_MAX_CACHED_PROCESSES = mOverrideMaxCachedProcesses < 0
    498                     ? (TextUtils.isEmpty(maxCachedProcessesFlag)
    499                     ? DEFAULT_MAX_CACHED_PROCESSES : Integer.parseInt(maxCachedProcessesFlag))
    500                     : mOverrideMaxCachedProcesses;
    501         } catch (NumberFormatException e) {
    502             // Bad flag value from Phenotype, revert to default.
    503             Slog.e(TAG,
    504                     "Unable to parse flag for max_cached_processes: " + maxCachedProcessesFlag, e);
    505             CUR_MAX_CACHED_PROCESSES = DEFAULT_MAX_CACHED_PROCESSES;
    506         }
    507         CUR_MAX_EMPTY_PROCESSES = computeEmptyProcessLimit(CUR_MAX_CACHED_PROCESSES);
    508 
    509         // Note the trim levels do NOT depend on the override process limit, we want
    510         // to consider the same level the point where we do trimming regardless of any
    511         // additional enforced limit.
    512         final int rawMaxEmptyProcesses = computeEmptyProcessLimit(MAX_CACHED_PROCESSES);
    513         CUR_TRIM_EMPTY_PROCESSES = rawMaxEmptyProcesses/2;
    514         CUR_TRIM_CACHED_PROCESSES = (MAX_CACHED_PROCESSES-rawMaxEmptyProcesses)/3;
    515     }
    516 
    517     void dump(PrintWriter pw) {
    518         pw.println("ACTIVITY MANAGER SETTINGS (dumpsys activity settings) "
    519                 + Settings.Global.ACTIVITY_MANAGER_CONSTANTS + ":");
    520 
    521         pw.print("  "); pw.print(KEY_MAX_CACHED_PROCESSES); pw.print("=");
    522         pw.println(MAX_CACHED_PROCESSES);
    523         pw.print("  "); pw.print(KEY_BACKGROUND_SETTLE_TIME); pw.print("=");
    524         pw.println(BACKGROUND_SETTLE_TIME);
    525         pw.print("  "); pw.print(KEY_FGSERVICE_MIN_SHOWN_TIME); pw.print("=");
    526         pw.println(FGSERVICE_MIN_SHOWN_TIME);
    527         pw.print("  "); pw.print(KEY_FGSERVICE_MIN_REPORT_TIME); pw.print("=");
    528         pw.println(FGSERVICE_MIN_REPORT_TIME);
    529         pw.print("  "); pw.print(KEY_FGSERVICE_SCREEN_ON_BEFORE_TIME); pw.print("=");
    530         pw.println(FGSERVICE_SCREEN_ON_BEFORE_TIME);
    531         pw.print("  "); pw.print(KEY_FGSERVICE_SCREEN_ON_AFTER_TIME); pw.print("=");
    532         pw.println(FGSERVICE_SCREEN_ON_AFTER_TIME);
    533         pw.print("  "); pw.print(KEY_CONTENT_PROVIDER_RETAIN_TIME); pw.print("=");
    534         pw.println(CONTENT_PROVIDER_RETAIN_TIME);
    535         pw.print("  "); pw.print(KEY_GC_TIMEOUT); pw.print("=");
    536         pw.println(GC_TIMEOUT);
    537         pw.print("  "); pw.print(KEY_GC_MIN_INTERVAL); pw.print("=");
    538         pw.println(GC_MIN_INTERVAL);
    539         pw.print("  "); pw.print(KEY_FULL_PSS_MIN_INTERVAL); pw.print("=");
    540         pw.println(FULL_PSS_MIN_INTERVAL);
    541         pw.print("  "); pw.print(KEY_FULL_PSS_LOWERED_INTERVAL); pw.print("=");
    542         pw.println(FULL_PSS_LOWERED_INTERVAL);
    543         pw.print("  "); pw.print(KEY_POWER_CHECK_INTERVAL); pw.print("=");
    544         pw.println(POWER_CHECK_INTERVAL);
    545         pw.print("  "); pw.print(KEY_POWER_CHECK_MAX_CPU_1); pw.print("=");
    546         pw.println(POWER_CHECK_MAX_CPU_1);
    547         pw.print("  "); pw.print(KEY_POWER_CHECK_MAX_CPU_2); pw.print("=");
    548         pw.println(POWER_CHECK_MAX_CPU_2);
    549         pw.print("  "); pw.print(KEY_POWER_CHECK_MAX_CPU_3); pw.print("=");
    550         pw.println(POWER_CHECK_MAX_CPU_3);
    551         pw.print("  "); pw.print(KEY_POWER_CHECK_MAX_CPU_4); pw.print("=");
    552         pw.println(POWER_CHECK_MAX_CPU_4);
    553         pw.print("  "); pw.print(KEY_SERVICE_USAGE_INTERACTION_TIME); pw.print("=");
    554         pw.println(SERVICE_USAGE_INTERACTION_TIME);
    555         pw.print("  "); pw.print(KEY_USAGE_STATS_INTERACTION_INTERVAL); pw.print("=");
    556         pw.println(USAGE_STATS_INTERACTION_INTERVAL);
    557         pw.print("  "); pw.print(KEY_SERVICE_RESTART_DURATION); pw.print("=");
    558         pw.println(SERVICE_RESTART_DURATION);
    559         pw.print("  "); pw.print(KEY_SERVICE_RESET_RUN_DURATION); pw.print("=");
    560         pw.println(SERVICE_RESET_RUN_DURATION);
    561         pw.print("  "); pw.print(KEY_SERVICE_RESTART_DURATION_FACTOR); pw.print("=");
    562         pw.println(SERVICE_RESTART_DURATION_FACTOR);
    563         pw.print("  "); pw.print(KEY_SERVICE_MIN_RESTART_TIME_BETWEEN); pw.print("=");
    564         pw.println(SERVICE_MIN_RESTART_TIME_BETWEEN);
    565         pw.print("  "); pw.print(KEY_MAX_SERVICE_INACTIVITY); pw.print("=");
    566         pw.println(MAX_SERVICE_INACTIVITY);
    567         pw.print("  "); pw.print(KEY_BG_START_TIMEOUT); pw.print("=");
    568         pw.println(BG_START_TIMEOUT);
    569         pw.print("  "); pw.print(KEY_SERVICE_BG_ACTIVITY_START_TIMEOUT); pw.print("=");
    570         pw.println(SERVICE_BG_ACTIVITY_START_TIMEOUT);
    571         pw.print("  "); pw.print(KEY_BOUND_SERVICE_CRASH_RESTART_DURATION); pw.print("=");
    572         pw.println(BOUND_SERVICE_CRASH_RESTART_DURATION);
    573         pw.print("  "); pw.print(KEY_BOUND_SERVICE_CRASH_MAX_RETRY); pw.print("=");
    574         pw.println(BOUND_SERVICE_MAX_CRASH_RETRY);
    575         pw.print("  "); pw.print(KEY_PROCESS_START_ASYNC); pw.print("=");
    576         pw.println(FLAG_PROCESS_START_ASYNC);
    577         pw.print("  "); pw.print(KEY_MEMORY_INFO_THROTTLE_TIME); pw.print("=");
    578         pw.println(MEMORY_INFO_THROTTLE_TIME);
    579         pw.print("  "); pw.print(KEY_TOP_TO_FGS_GRACE_DURATION); pw.print("=");
    580         pw.println(TOP_TO_FGS_GRACE_DURATION);
    581 
    582         pw.println();
    583         if (mOverrideMaxCachedProcesses >= 0) {
    584             pw.print("  mOverrideMaxCachedProcesses="); pw.println(mOverrideMaxCachedProcesses);
    585         }
    586         pw.print("  CUR_MAX_CACHED_PROCESSES="); pw.println(CUR_MAX_CACHED_PROCESSES);
    587         pw.print("  CUR_MAX_EMPTY_PROCESSES="); pw.println(CUR_MAX_EMPTY_PROCESSES);
    588         pw.print("  CUR_TRIM_EMPTY_PROCESSES="); pw.println(CUR_TRIM_EMPTY_PROCESSES);
    589         pw.print("  CUR_TRIM_CACHED_PROCESSES="); pw.println(CUR_TRIM_CACHED_PROCESSES);
    590     }
    591 }
    592