Merge "Bump Standby Bucket for FGS sooner." into sc-dev

This commit is contained in:
TreeHugger Robot
2021-05-03 17:08:31 +00:00
committed by Android (Google) Code Review
3 changed files with 150 additions and 37 deletions

View File

@@ -73,10 +73,16 @@ final class ActivityManagerConstants extends ContentObserver {
private static final String KEY_POWER_CHECK_MAX_CPU_2 = "power_check_max_cpu_2";
private static final String KEY_POWER_CHECK_MAX_CPU_3 = "power_check_max_cpu_3";
private static final String KEY_POWER_CHECK_MAX_CPU_4 = "power_check_max_cpu_4";
private static final String KEY_SERVICE_USAGE_INTERACTION_TIME
= "service_usage_interaction_time";
private static final String KEY_USAGE_STATS_INTERACTION_INTERVAL
= "usage_stats_interaction_interval";
/** Used for all apps on R and earlier versions. */
private static final String KEY_SERVICE_USAGE_INTERACTION_TIME_PRE_S =
"service_usage_interaction_time";
private static final String KEY_SERVICE_USAGE_INTERACTION_TIME_POST_S =
"service_usage_interaction_time_post_s";
/** Used for all apps on R and earlier versions. */
private static final String KEY_USAGE_STATS_INTERACTION_INTERVAL_PRE_S =
"usage_stats_interaction_interval";
private static final String KEY_USAGE_STATS_INTERACTION_INTERVAL_POST_S =
"usage_stats_interaction_interval_post_s";
private static final String KEY_IMPERCEPTIBLE_KILL_EXEMPT_PACKAGES =
"imperceptible_kill_exempt_packages";
private static final String KEY_IMPERCEPTIBLE_KILL_EXEMPT_PROC_STATES =
@@ -120,8 +126,10 @@ final class ActivityManagerConstants extends ContentObserver {
private static final int DEFAULT_POWER_CHECK_MAX_CPU_2 = 25;
private static final int DEFAULT_POWER_CHECK_MAX_CPU_3 = 10;
private static final int DEFAULT_POWER_CHECK_MAX_CPU_4 = 2;
private static final long DEFAULT_SERVICE_USAGE_INTERACTION_TIME = 30*60*1000;
private static final long DEFAULT_USAGE_STATS_INTERACTION_INTERVAL = 2*60*60*1000L;
private static final long DEFAULT_SERVICE_USAGE_INTERACTION_TIME_PRE_S = 30 * 60 * 1000;
private static final long DEFAULT_SERVICE_USAGE_INTERACTION_TIME_POST_S = 60 * 1000;
private static final long DEFAULT_USAGE_STATS_INTERACTION_INTERVAL_PRE_S = 2 * 60 * 60 * 1000;
private static final long DEFAULT_USAGE_STATS_INTERACTION_INTERVAL_POST_S = 10 * 60 * 1000;
private static final long DEFAULT_SERVICE_RESTART_DURATION = 1*1000;
private static final long DEFAULT_SERVICE_RESET_RUN_DURATION = 60*1000;
private static final int DEFAULT_SERVICE_RESTART_DURATION_FACTOR = 4;
@@ -303,11 +311,23 @@ final class ActivityManagerConstants extends ContentObserver {
// This is the amount of time an app needs to be running a foreground service before
// we will consider it to be doing interaction for usage stats.
long SERVICE_USAGE_INTERACTION_TIME = DEFAULT_SERVICE_USAGE_INTERACTION_TIME;
// Only used for apps targeting pre-S versions.
long SERVICE_USAGE_INTERACTION_TIME_PRE_S = DEFAULT_SERVICE_USAGE_INTERACTION_TIME_PRE_S;
// This is the amount of time an app needs to be running a foreground service before
// we will consider it to be doing interaction for usage stats.
// Only used for apps targeting versions S and above.
long SERVICE_USAGE_INTERACTION_TIME_POST_S = DEFAULT_SERVICE_USAGE_INTERACTION_TIME_POST_S;
// Maximum amount of time we will allow to elapse before re-reporting usage stats
// interaction with foreground processes.
long USAGE_STATS_INTERACTION_INTERVAL = DEFAULT_USAGE_STATS_INTERACTION_INTERVAL;
// Only used for apps targeting pre-S versions.
long USAGE_STATS_INTERACTION_INTERVAL_PRE_S = DEFAULT_USAGE_STATS_INTERACTION_INTERVAL_PRE_S;
// Maximum amount of time we will allow to elapse before re-reporting usage stats
// interaction with foreground processes.
// Only used for apps targeting versions S and above.
long USAGE_STATS_INTERACTION_INTERVAL_POST_S = DEFAULT_USAGE_STATS_INTERACTION_INTERVAL_POST_S;
// How long a service needs to be running until restarting its process
// is no longer considered to be a relaunch of the service.
@@ -817,10 +837,18 @@ final class ActivityManagerConstants extends ContentObserver {
DEFAULT_POWER_CHECK_MAX_CPU_3);
POWER_CHECK_MAX_CPU_4 = mParser.getInt(KEY_POWER_CHECK_MAX_CPU_4,
DEFAULT_POWER_CHECK_MAX_CPU_4);
SERVICE_USAGE_INTERACTION_TIME = mParser.getLong(KEY_SERVICE_USAGE_INTERACTION_TIME,
DEFAULT_SERVICE_USAGE_INTERACTION_TIME);
USAGE_STATS_INTERACTION_INTERVAL = mParser.getLong(KEY_USAGE_STATS_INTERACTION_INTERVAL,
DEFAULT_USAGE_STATS_INTERACTION_INTERVAL);
SERVICE_USAGE_INTERACTION_TIME_PRE_S = mParser.getLong(
KEY_SERVICE_USAGE_INTERACTION_TIME_PRE_S,
DEFAULT_SERVICE_USAGE_INTERACTION_TIME_PRE_S);
SERVICE_USAGE_INTERACTION_TIME_POST_S = mParser.getLong(
KEY_SERVICE_USAGE_INTERACTION_TIME_POST_S,
DEFAULT_SERVICE_USAGE_INTERACTION_TIME_POST_S);
USAGE_STATS_INTERACTION_INTERVAL_PRE_S = mParser.getLong(
KEY_USAGE_STATS_INTERACTION_INTERVAL_PRE_S,
DEFAULT_USAGE_STATS_INTERACTION_INTERVAL_PRE_S);
USAGE_STATS_INTERACTION_INTERVAL_POST_S = mParser.getLong(
KEY_USAGE_STATS_INTERACTION_INTERVAL_POST_S,
DEFAULT_USAGE_STATS_INTERACTION_INTERVAL_POST_S);
SERVICE_RESTART_DURATION = mParser.getLong(KEY_SERVICE_RESTART_DURATION,
DEFAULT_SERVICE_RESTART_DURATION);
SERVICE_RESET_RUN_DURATION = mParser.getLong(KEY_SERVICE_RESET_RUN_DURATION,
@@ -1135,10 +1163,14 @@ final class ActivityManagerConstants extends ContentObserver {
pw.println(POWER_CHECK_MAX_CPU_3);
pw.print(" "); pw.print(KEY_POWER_CHECK_MAX_CPU_4); pw.print("=");
pw.println(POWER_CHECK_MAX_CPU_4);
pw.print(" "); pw.print(KEY_SERVICE_USAGE_INTERACTION_TIME); pw.print("=");
pw.println(SERVICE_USAGE_INTERACTION_TIME);
pw.print(" "); pw.print(KEY_USAGE_STATS_INTERACTION_INTERVAL); pw.print("=");
pw.println(USAGE_STATS_INTERACTION_INTERVAL);
pw.print(" "); pw.print(KEY_SERVICE_USAGE_INTERACTION_TIME_PRE_S); pw.print("=");
pw.println(SERVICE_USAGE_INTERACTION_TIME_PRE_S);
pw.print(" "); pw.print(KEY_SERVICE_USAGE_INTERACTION_TIME_POST_S); pw.print("=");
pw.println(SERVICE_USAGE_INTERACTION_TIME_POST_S);
pw.print(" "); pw.print(KEY_USAGE_STATS_INTERACTION_INTERVAL_PRE_S); pw.print("=");
pw.println(USAGE_STATS_INTERACTION_INTERVAL_PRE_S);
pw.print(" "); pw.print(KEY_USAGE_STATS_INTERACTION_INTERVAL_POST_S); pw.print("=");
pw.println(USAGE_STATS_INTERACTION_INTERVAL_POST_S);
pw.print(" "); pw.print(KEY_SERVICE_RESTART_DURATION); pw.print("=");
pw.println(SERVICE_RESTART_DURATION);
pw.print(" "); pw.print(KEY_SERVICE_RESET_RUN_DURATION); pw.print("=");

View File

@@ -80,6 +80,7 @@ import android.app.ApplicationExitInfo;
import android.app.usage.UsageEvents;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.compat.annotation.EnabledSince;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@@ -122,7 +123,7 @@ import java.util.Arrays;
/**
* All of the code required to compute proc states and oom_adj values.
*/
public final class OomAdjuster {
public class OomAdjuster {
static final String TAG = "OomAdjuster";
static final String OOM_ADJ_REASON_METHOD = "updateOomAdj";
static final String OOM_ADJ_REASON_NONE = OOM_ADJ_REASON_METHOD + "_meh";
@@ -163,6 +164,14 @@ public final class OomAdjuster {
@EnabledAfter(targetSdkVersion=android.os.Build.VERSION_CODES.Q)
static final long CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID = 136219221L;
/**
* For apps targeting S+, this determines whether to use a shorter timeout before elevating the
* standby bucket to ACTIVE when apps start a foreground service.
*/
@ChangeId
@EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.S)
static final long USE_SHORT_FGS_USAGE_INTERACTION_TIME = 183972877L;
/**
* For some direct access we need to power manager.
*/
@@ -249,7 +258,9 @@ public final class OomAdjuster {
private final PlatformCompatCache mPlatformCompatCache;
private static class PlatformCompatCache {
/** Overrideable by a test */
@VisibleForTesting
static class PlatformCompatCache {
private final PlatformCompat mPlatformCompat;
private final IPlatformCompat mIPlatformCompatProxy;
private final LongSparseArray<CacheItem> mCaches = new LongSparseArray<>();
@@ -278,6 +289,20 @@ public final class OomAdjuster {
: mIPlatformCompatProxy.isChangeEnabled(changeId, app);
}
/**
* Same as {@link #isChangeEnabled(long, ApplicationInfo)} but instead of throwing a
* RemoteException from platform compat, it returns the default value provided.
*/
boolean isChangeEnabled(long changeId, ApplicationInfo app, boolean defaultValue) {
try {
return mCacheEnabled ? mCaches.get(changeId).isChangeEnabled(app)
: mIPlatformCompatProxy.isChangeEnabled(changeId, app);
} catch (RemoteException e) {
Slog.w(TAG, "Error reading platform compat change " + changeId, e);
return defaultValue;
}
}
void invalidate(ApplicationInfo app) {
for (int i = mCaches.size() - 1; i >= 0; i--) {
mCaches.valueAt(i).invalidate(app);
@@ -335,6 +360,12 @@ public final class OomAdjuster {
}
}
/** Overrideable by a test */
@VisibleForTesting
protected PlatformCompatCache getPlatformCompatCache() {
return mPlatformCompatCache;
}
OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
this(service, processList, activeUids, createAdjusterThread());
}
@@ -383,7 +414,8 @@ public final class OomAdjuster {
mNumSlots = ((ProcessList.CACHED_APP_MAX_ADJ - ProcessList.CACHED_APP_MIN_ADJ + 1) >> 1)
/ ProcessList.CACHED_APP_IMPORTANCE_LEVELS;
mPlatformCompatCache = new PlatformCompatCache(new long[] {
PROCESS_CAPABILITY_CHANGE_ID, CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID
PROCESS_CAPABILITY_CHANGE_ID, CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID,
USE_SHORT_FGS_USAGE_INTERACTION_TIME
});
}
@@ -755,7 +787,7 @@ public final class OomAdjuster {
if (app != null) {
mPendingProcessSet.remove(app);
if (procDied) {
mPlatformCompatCache.invalidate(app.info);
getPlatformCompatCache().invalidate(app.info);
}
}
}
@@ -1924,7 +1956,7 @@ public final class OomAdjuster {
boolean enabled = false;
try {
enabled = mPlatformCompatCache.isChangeEnabled(
enabled = getPlatformCompatCache().isChangeEnabled(
CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID, s.appInfo);
} catch (RemoteException e) {
}
@@ -2147,7 +2179,7 @@ public final class OomAdjuster {
state.bumpAllowStartFgsState(PROCESS_STATE_BOUND_TOP);
boolean enabled = false;
try {
enabled = mPlatformCompatCache.isChangeEnabled(
enabled = getPlatformCompatCache().isChangeEnabled(
PROCESS_CAPABILITY_CHANGE_ID, client.info);
} catch (RemoteException e) {
}
@@ -2787,15 +2819,27 @@ public final class OomAdjuster {
} else {
state.setProcStateChanged(true);
}
} else if (state.hasReportedInteraction() && (nowElapsed - state.getInteractionEventTime())
> mConstants.USAGE_STATS_INTERACTION_INTERVAL) {
} else if (state.hasReportedInteraction()) {
final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
final long interactionThreshold = fgsInteractionChangeEnabled
? mConstants.USAGE_STATS_INTERACTION_INTERVAL_POST_S
: mConstants.USAGE_STATS_INTERACTION_INTERVAL_PRE_S;
// For apps that sit around for a long time in the interactive state, we need
// to report this at least once a day so they don't go idle.
maybeUpdateUsageStatsLSP(app, nowElapsed);
} else if (!state.hasReportedInteraction() && (nowElapsed - state.getFgInteractionTime())
> mConstants.SERVICE_USAGE_INTERACTION_TIME) {
if ((nowElapsed - state.getInteractionEventTime()) > interactionThreshold) {
maybeUpdateUsageStatsLSP(app, nowElapsed);
}
} else {
final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
final long interactionThreshold = fgsInteractionChangeEnabled
? mConstants.SERVICE_USAGE_INTERACTION_TIME_POST_S
: mConstants.SERVICE_USAGE_INTERACTION_TIME_PRE_S;
// For foreground services that sit around for a long time but are not interacted with.
maybeUpdateUsageStatsLSP(app, nowElapsed);
if ((nowElapsed - state.getFgInteractionTime()) > interactionThreshold) {
maybeUpdateUsageStatsLSP(app, nowElapsed);
}
}
if (state.getCurCapability() != state.getSetCapability()) {
@@ -2877,6 +2921,8 @@ public final class OomAdjuster {
if (mService.mUsageStatsService == null) {
return;
}
final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
boolean isInteraction;
// To avoid some abuse patterns, we are going to be careful about what we consider
// to be an app interaction. Being the top activity doesn't count while the display
@@ -2890,18 +2936,22 @@ public final class OomAdjuster {
state.setFgInteractionTime(nowElapsed);
isInteraction = false;
} else {
isInteraction = nowElapsed > state.getFgInteractionTime()
+ mConstants.SERVICE_USAGE_INTERACTION_TIME;
final long interactionTime = fgsInteractionChangeEnabled
? mConstants.SERVICE_USAGE_INTERACTION_TIME_POST_S
: mConstants.SERVICE_USAGE_INTERACTION_TIME_PRE_S;
isInteraction = nowElapsed > state.getFgInteractionTime() + interactionTime;
}
} else {
isInteraction =
state.getCurProcState() <= PROCESS_STATE_IMPORTANT_FOREGROUND;
state.setFgInteractionTime(0);
}
final long interactionThreshold = fgsInteractionChangeEnabled
? mConstants.USAGE_STATS_INTERACTION_INTERVAL_POST_S
: mConstants.USAGE_STATS_INTERACTION_INTERVAL_PRE_S;
if (isInteraction
&& (!state.hasReportedInteraction()
|| (nowElapsed - state.getInteractionEventTime())
> mConstants.USAGE_STATS_INTERACTION_INTERVAL)) {
|| (nowElapsed - state.getInteractionEventTime()) > interactionThreshold)) {
state.setInteractionEventTime(nowElapsed);
String[] packages = app.getPackageList();
if (packages != null) {

View File

@@ -30,6 +30,7 @@ import android.app.ActivityManager;
import android.app.usage.UsageStatsManagerInternal;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManagerInternal;
import com.android.server.LocalServices;
@@ -57,8 +58,36 @@ public class OomAdjusterTests {
private ProcessRecord mProcessRecord;
private static final long ZERO = 0L;
private static final long USAGE_STATS_INTERACTION = 2 * 60 * 60 * 1000L;
private static final long SERVICE_USAGE_INTERACTION = 30 * 60 * 1000;
private static final long USAGE_STATS_INTERACTION = 10 * 60 * 1000L;
private static final long SERVICE_USAGE_INTERACTION = 60 * 1000;
static class MyOomAdjuster extends OomAdjuster {
private final PlatformCompatCache mPlatformCompatCache;
MyOomAdjuster(ActivityManagerService service, ProcessList processList,
ActiveUids activeUids) {
super(service, processList, activeUids);
mPlatformCompatCache = new MyPlatformCompatCache(new long[]{});
}
static class MyPlatformCompatCache extends PlatformCompatCache {
MyPlatformCompatCache(long[] compatChanges) {
super(compatChanges);
}
@Override
boolean isChangeEnabled(long changeId, ApplicationInfo app, boolean defaultValue) {
return true;
}
}
@Override
protected OomAdjuster.PlatformCompatCache getPlatformCompatCache() {
return mPlatformCompatCache;
}
}
@BeforeClass
public static void setUpOnce() {
@@ -84,7 +113,7 @@ public class OomAdjusterTests {
final AppProfiler profiler = mock(AppProfiler.class);
setFieldValue(AppProfiler.class, profiler, "mProfilerLock", new Object());
setFieldValue(ActivityManagerService.class, sService, "mAppProfiler", profiler);
sService.mOomAdjuster = new OomAdjuster(sService, sService.mProcessList, null);
sService.mOomAdjuster = new MyOomAdjuster(sService, sService.mProcessList, null);
LocalServices.addService(UsageStatsManagerInternal.class,
mock(UsageStatsManagerInternal.class));
sService.mUsageStatsService = LocalServices.getService(UsageStatsManagerInternal.class);
@@ -119,8 +148,10 @@ public class OomAdjusterTests {
// Ensure certain services and constants are defined properly
assertNotNull(sService.mUsageStatsService);
assertEquals(USAGE_STATS_INTERACTION, sService.mConstants.USAGE_STATS_INTERACTION_INTERVAL);
assertEquals(SERVICE_USAGE_INTERACTION, sService.mConstants.SERVICE_USAGE_INTERACTION_TIME);
assertEquals(USAGE_STATS_INTERACTION,
sService.mConstants.USAGE_STATS_INTERACTION_INTERVAL_POST_S);
assertEquals(SERVICE_USAGE_INTERACTION,
sService.mConstants.SERVICE_USAGE_INTERACTION_TIME_POST_S);
}
@Test