Merge "Cache compat change values per ProcessRecord for OomAdjuster" into sc-qpr1-dev

This commit is contained in:
Jing Ji
2021-09-08 00:24:59 +00:00
committed by Android (Google) Code Review
2 changed files with 64 additions and 18 deletions

View File

@@ -72,6 +72,7 @@ import static com.android.server.am.AppProfiler.TAG_PSS;
import static com.android.server.am.ProcessList.TAG_PROCESS_OBSERVERS;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
import android.annotation.IntDef;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityThread;
@@ -114,6 +115,8 @@ import com.android.server.wm.ActivityServiceConnectionsHolder;
import com.android.server.wm.WindowProcessController;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayDeque;
import java.util.ArrayList;
@@ -171,6 +174,27 @@ public class OomAdjuster {
@EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.S)
static final long USE_SHORT_FGS_USAGE_INTERACTION_TIME = 183972877L;
static final int CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY = 0;
static final int CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY = 1;
static final int CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME = 2;
@IntDef(prefix = { "CACHED_COMPAT_CHANGE_" }, value = {
CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY,
CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY,
CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME,
})
@Retention(RetentionPolicy.SOURCE)
static @interface CachedCompatChangeId{}
/**
* Mapping from CACHED_COMPAT_CHANGE_* to the actual compat change id.
*/
static final long[] CACHED_COMPAT_CHANGE_IDS_MAPPING = new long[] {
PROCESS_CAPABILITY_CHANGE_ID,
CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID,
USE_SHORT_FGS_USAGE_INTERACTION_TIME,
};
/**
* For some direct access we need to power manager.
*/
@@ -368,6 +392,12 @@ public class OomAdjuster {
return mPlatformCompatCache;
}
boolean isChangeEnabled(@CachedCompatChangeId int cachedCompatChangeId, ApplicationInfo app,
boolean defaultValue) {
return getPlatformCompatCache().isChangeEnabled(
CACHED_COMPAT_CHANGE_IDS_MAPPING[cachedCompatChangeId], app, defaultValue);
}
OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
this(service, processList, activeUids, createAdjusterThread());
}
@@ -1929,12 +1959,8 @@ public class OomAdjuster {
(fgsType & FOREGROUND_SERVICE_TYPE_LOCATION)
!= 0 ? PROCESS_CAPABILITY_FOREGROUND_LOCATION : 0;
boolean enabled = false;
try {
enabled = getPlatformCompatCache().isChangeEnabled(
CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID, s.appInfo);
} catch (RemoteException e) {
}
final boolean enabled = state.getCachedCompatChange(
CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY);
if (enabled) {
capabilityFromFGS |=
(fgsType & FOREGROUND_SERVICE_TYPE_CAMERA)
@@ -2151,12 +2177,8 @@ public class OomAdjuster {
// to client's state.
clientProcState = PROCESS_STATE_BOUND_TOP;
state.bumpAllowStartFgsState(PROCESS_STATE_BOUND_TOP);
boolean enabled = false;
try {
enabled = getPlatformCompatCache().isChangeEnabled(
PROCESS_CAPABILITY_CHANGE_ID, client.info);
} catch (RemoteException e) {
}
final boolean enabled = cstate.getCachedCompatChange(
CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY);
if (enabled) {
if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
// TOP process passes all capabilities to the service.
@@ -2800,8 +2822,8 @@ public class OomAdjuster {
state.setProcStateChanged(true);
}
} else if (state.hasReportedInteraction()) {
final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
final boolean fgsInteractionChangeEnabled = state.getCachedCompatChange(
CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME);
final long interactionThreshold = fgsInteractionChangeEnabled
? mConstants.USAGE_STATS_INTERACTION_INTERVAL_POST_S
: mConstants.USAGE_STATS_INTERACTION_INTERVAL_PRE_S;
@@ -2811,8 +2833,8 @@ public class OomAdjuster {
maybeUpdateUsageStatsLSP(app, nowElapsed);
}
} else {
final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
final boolean fgsInteractionChangeEnabled = state.getCachedCompatChange(
CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME);
final long interactionThreshold = fgsInteractionChangeEnabled
? mConstants.SERVICE_USAGE_INTERACTION_TIME_POST_S
: mConstants.SERVICE_USAGE_INTERACTION_TIME_PRE_S;
@@ -2901,8 +2923,8 @@ public class OomAdjuster {
if (mService.mUsageStatsService == null) {
return;
}
final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
final boolean fgsInteractionChangeEnabled = state.getCachedCompatChange(
CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME);
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

View File

@@ -21,6 +21,7 @@ import static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_OOM_ADJ;
import static com.android.server.am.OomAdjuster.CachedCompatChangeId;
import static com.android.server.am.ProcessRecord.TAG;
import android.annotation.ElapsedRealtimeLong;
@@ -377,6 +378,16 @@ final class ProcessStateRecord {
@GuardedBy("mService")
private int mCachedIsReceivingBroadcast = VALUE_INVALID;
/**
* Cache the return value of PlatformCompat.isChangeEnabled().
*/
@GuardedBy("mService")
private int[] mCachedCompatChanges = new int[] {
VALUE_INVALID, // CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY
VALUE_INVALID, // CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY
VALUE_INVALID, // CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME
};
@GuardedBy("mService")
private int mCachedAdj = ProcessList.INVALID_ADJ;
@GuardedBy("mService")
@@ -1008,6 +1019,16 @@ final class ProcessStateRecord {
return mCachedIsReceivingBroadcast == VALUE_TRUE;
}
@GuardedBy("mService")
boolean getCachedCompatChange(@CachedCompatChangeId int cachedCompatChangeId) {
if (mCachedCompatChanges[cachedCompatChangeId] == VALUE_INVALID) {
mCachedCompatChanges[cachedCompatChangeId] = mService.mOomAdjuster
.isChangeEnabled(cachedCompatChangeId, mApp.info, false /* default */)
? VALUE_TRUE : VALUE_FALSE;
}
return mCachedCompatChanges[cachedCompatChangeId] == VALUE_TRUE;
}
@GuardedBy("mService")
void computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback,
int adj, boolean foregroundActivities, boolean hasVisibleActivities, int procState,
@@ -1088,6 +1109,9 @@ final class ProcessStateRecord {
mCurSchedGroup = mSetSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
mCurProcState = mCurRawProcState = mSetProcState = mAllowStartFgsState =
PROCESS_STATE_NONEXISTENT;
for (int i = 0; i < mCachedCompatChanges.length; i++) {
mCachedCompatChanges[i] = VALUE_INVALID;
}
}
@GuardedBy("mService")