Merge "Enhance APP_BACKGROUND_RESTRICTIONS_INFO log." into tm-dev

This commit is contained in:
Hui Yu
2022-04-27 23:00:38 +00:00
committed by Android (Google) Code Review
3 changed files with 47 additions and 12 deletions

View File

@@ -73,12 +73,17 @@ message AppBackgroundRestrictionsInfo {
optional FgsTrackerInfo fgs_tracker_info = 5; optional FgsTrackerInfo fgs_tracker_info = 5;
message BatteryTrackerInfo { message BatteryTrackerInfo {
// total battery usage within last 24h (percentage) // total battery usage within last 24h (1/10000th)
optional int32 battery_24h = 1; optional int32 battery_24h = 1;
// background battery usage (percentage) // background battery usage (1/10000th)
optional int32 battery_usage_background = 2; optional int32 battery_usage_background = 2;
// FGS battery usage (percentage) // FGS battery usage (1/10000th)
optional int32 battery_usage_fgs = 3; optional int32 battery_usage_fgs = 3;
// Foreground battery usage (1/10000th)
optional int32 battery_usage_foreground = 4;
// Cached battery usage (1/10000th)
optional int32 battery_usage_cached = 5;
} }
optional BatteryTrackerInfo battery_tracker_info = 6; optional BatteryTrackerInfo battery_tracker_info = 6;
@@ -197,5 +202,8 @@ message AppBackgroundRestrictionsInfo {
// indicates if the current device is a low ram device. // indicates if the current device is a low ram device.
optional bool low_mem_device = 12; optional bool low_mem_device = 12;
// indicates previous background restriction level.
optional RestrictionLevel previous_restriction_level = 13;
} }

View File

@@ -276,7 +276,9 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
AppBackgroundRestrictionsInfo.REASON_UNKNOWN, // ExemptionReason AppBackgroundRestrictionsInfo.REASON_UNKNOWN, // ExemptionReason
AppBackgroundRestrictionsInfo.UNKNOWN, // OptimizationLevel AppBackgroundRestrictionsInfo.UNKNOWN, // OptimizationLevel
AppBackgroundRestrictionsInfo.SDK_UNKNOWN, // TargetSdk AppBackgroundRestrictionsInfo.SDK_UNKNOWN, // TargetSdk
isLowRamDeviceStatic()); isLowRamDeviceStatic(),
AppBackgroundRestrictionsInfo.LEVEL_UNKNOWN // previous RestrictionLevel
);
} }
} }
} }
@@ -304,11 +306,17 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
bgUsage.mPercentage[BatteryUsage.BATTERY_USAGE_INDEX_BACKGROUND]; bgUsage.mPercentage[BatteryUsage.BATTERY_USAGE_INDEX_BACKGROUND];
final double usageFgs = final double usageFgs =
bgUsage.mPercentage[BatteryUsage.BATTERY_USAGE_INDEX_FOREGROUND_SERVICE]; bgUsage.mPercentage[BatteryUsage.BATTERY_USAGE_INDEX_FOREGROUND_SERVICE];
final double usageForeground =
bgUsage.mPercentage[BatteryUsage.BATTERY_USAGE_INDEX_FOREGROUND];
final double usageCached =
bgUsage.mPercentage[BatteryUsage.BATTERY_USAGE_INDEX_CACHED];
if (DEBUG_BACKGROUND_BATTERY_TRACKER_VERBOSE) { if (DEBUG_BACKGROUND_BATTERY_TRACKER_VERBOSE) {
Slog.d(TAG, "getBatteryTrackerInfoProtoLocked uid:" + uid Slog.d(TAG, "getBatteryTrackerInfoProtoLocked uid:" + uid
+ " allUsage:" + String.format("%4.2f%%", allUsage) + " allUsage:" + String.format("%4.2f%%", allUsage)
+ " usageBackground:" + String.format("%4.2f%%", usageBackground) + " usageBackground:" + String.format("%4.2f%%", usageBackground)
+ " usageFgs:" + String.format("%4.2f%%", usageFgs)); + " usageFgs:" + String.format("%4.2f%%", usageFgs)
+ " usageForeground:" + String.format("%4.2f%%", usageForeground)
+ " usageCached:" + String.format("%4.2f%%", usageCached));
} }
final ProtoOutputStream proto = new ProtoOutputStream(); final ProtoOutputStream proto = new ProtoOutputStream();
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_24H, proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_24H,
@@ -317,6 +325,10 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
usageBackground * 10000); usageBackground * 10000);
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_FGS, proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_FGS,
usageFgs * 10000); usageFgs * 10000);
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_FOREGROUND,
usageForeground * 10000);
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_CACHED,
usageCached * 10000);
proto.flush(); proto.flush();
return proto.getBytes(); return proto.getBytes();
} }

View File

@@ -2086,6 +2086,9 @@ public final class AppRestrictionController {
int curLevel; int curLevel;
int prevReason; int prevReason;
final AppStandbyInternal appStandbyInternal = mInjector.getAppStandbyInternal(); final AppStandbyInternal appStandbyInternal = mInjector.getAppStandbyInternal();
if (trackerInfo == null) {
trackerInfo = mEmptyTrackerInfo;
}
synchronized (mSettingsLock) { synchronized (mSettingsLock) {
curLevel = getRestrictionLevel(uid, pkgName); curLevel = getRestrictionLevel(uid, pkgName);
if (curLevel == level) { if (curLevel == level) {
@@ -2138,14 +2141,21 @@ public final class AppRestrictionController {
// It's currently active, enqueue it. // It's currently active, enqueue it.
final int localReason = reason; final int localReason = reason;
final int localSubReason = subReason; final int localSubReason = subReason;
mActiveUids.add(uid, pkgName, () -> appStandbyInternal.restrictApp( final TrackerInfo localTrackerInfo = trackerInfo;
pkgName, UserHandle.getUserId(uid), localReason, localSubReason)); mActiveUids.add(uid, pkgName, () -> {
appStandbyInternal.restrictApp(pkgName, UserHandle.getUserId(uid),
localReason, localSubReason);
logAppBackgroundRestrictionInfo(pkgName, uid, curLevel, level,
localTrackerInfo, localReason);
});
doIt = false; doIt = false;
} }
} }
if (doIt) { if (doIt) {
appStandbyInternal.restrictApp(pkgName, UserHandle.getUserId(uid), appStandbyInternal.restrictApp(pkgName, UserHandle.getUserId(uid),
reason, subReason); reason, subReason);
logAppBackgroundRestrictionInfo(pkgName, uid, curLevel, level, trackerInfo,
reason);
} }
} }
} else if (curLevel >= RESTRICTION_LEVEL_RESTRICTED_BUCKET } else if (curLevel >= RESTRICTION_LEVEL_RESTRICTED_BUCKET
@@ -2160,11 +2170,14 @@ public final class AppRestrictionController {
appStandbyInternal.maybeUnrestrictApp(pkgName, UserHandle.getUserId(uid), appStandbyInternal.maybeUnrestrictApp(pkgName, UserHandle.getUserId(uid),
prevReason & REASON_MAIN_MASK, prevReason & REASON_SUB_MASK, prevReason & REASON_MAIN_MASK, prevReason & REASON_SUB_MASK,
reason, subReason); reason, subReason);
logAppBackgroundRestrictionInfo(pkgName, uid, curLevel, level, trackerInfo,
reason);
} }
}
if (trackerInfo == null) { private void logAppBackgroundRestrictionInfo(String pkgName, int uid,
trackerInfo = mEmptyTrackerInfo; @RestrictionLevel int prevLevel, @RestrictionLevel int level,
} @NonNull TrackerInfo trackerInfo, int reason) {
FrameworkStatsLog.write(FrameworkStatsLog.APP_BACKGROUND_RESTRICTIONS_INFO, uid, FrameworkStatsLog.write(FrameworkStatsLog.APP_BACKGROUND_RESTRICTIONS_INFO, uid,
getRestrictionLevelStatsd(level), getRestrictionLevelStatsd(level),
getThresholdStatsd(reason), getThresholdStatsd(reason),
@@ -2176,7 +2189,8 @@ public final class AppRestrictionController {
getExemptionReasonStatsd(uid, level), getExemptionReasonStatsd(uid, level),
getOptimizationLevelStatsd(level), getOptimizationLevelStatsd(level),
getTargetSdkStatsd(pkgName), getTargetSdkStatsd(pkgName),
ActivityManager.isLowRamDeviceStatic()); ActivityManager.isLowRamDeviceStatic(),
getRestrictionLevelStatsd(prevLevel));
} }
private void handleBackgroundRestrictionChanged(int uid, String pkgName, boolean restricted) { private void handleBackgroundRestrictionChanged(int uid, String pkgName, boolean restricted) {
@@ -2449,7 +2463,8 @@ public final class AppRestrictionController {
mBgController.getBackgroundRestrictionExemptionReason(uid)), mBgController.getBackgroundRestrictionExemptionReason(uid)),
AppBackgroundRestrictionsInfo.UNKNOWN, // OptimizationLevel AppBackgroundRestrictionsInfo.UNKNOWN, // OptimizationLevel
AppBackgroundRestrictionsInfo.SDK_UNKNOWN, // TargetSdk AppBackgroundRestrictionsInfo.SDK_UNKNOWN, // TargetSdk
ActivityManager.isLowRamDeviceStatic()); ActivityManager.isLowRamDeviceStatic(),
mBgController.getRestrictionLevel(uid));
PendingIntent pendingIntent; PendingIntent pendingIntent;
if (!mBgController.mConstantsObserver.mBgPromptFgsWithNotiOnLongRunning if (!mBgController.mConstantsObserver.mBgPromptFgsWithNotiOnLongRunning
&& mBgController.hasForegroundServiceNotifications(packageName, uid)) { && mBgController.hasForegroundServiceNotifications(packageName, uid)) {