Enhance APP_BACKGROUND_RESTRICTIONS_INFO log.

Only log APP_BACKGROUND_RESTRICTIONS_INFO when the restricion level
change causes update to AppStandbyController.

Besides the new restriction level, also log previous restriction level,
so we know restriction level changed from where to where.

Fill in BatteryTrackerInfo if tracker type is BatteryTracker. Add fields
foreground and cached battery usage in BatteryTrackerInfo.

Bug: NA
Test: statsd_testdrive 441
Change-Id: Ie9cf8b8d17c37fad7ee2a26ba3c0e323a6f1e374
This commit is contained in:
Hui Yu
2022-04-24 23:03:02 -07:00
parent c6de69d8a5
commit 8fdf543c03
3 changed files with 47 additions and 12 deletions

View File

@@ -73,12 +73,17 @@ message AppBackgroundRestrictionsInfo {
optional FgsTrackerInfo fgs_tracker_info = 5;
message BatteryTrackerInfo {
// total battery usage within last 24h (percentage)
// total battery usage within last 24h (1/10000th)
optional int32 battery_24h = 1;
// background battery usage (percentage)
// background battery usage (1/10000th)
optional int32 battery_usage_background = 2;
// FGS battery usage (percentage)
// FGS battery usage (1/10000th)
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;
@@ -197,5 +202,8 @@ message AppBackgroundRestrictionsInfo {
// indicates if the current device is a low ram device.
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.UNKNOWN, // OptimizationLevel
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];
final double usageFgs =
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) {
Slog.d(TAG, "getBatteryTrackerInfoProtoLocked uid:" + uid
+ " allUsage:" + String.format("%4.2f%%", allUsage)
+ " 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();
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_24H,
@@ -317,6 +325,10 @@ final class AppBatteryTracker extends BaseAppStateTracker<AppBatteryPolicy>
usageBackground * 10000);
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_FGS,
usageFgs * 10000);
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_FOREGROUND,
usageForeground * 10000);
proto.write(AppBackgroundRestrictionsInfo.BatteryTrackerInfo.BATTERY_USAGE_CACHED,
usageCached * 10000);
proto.flush();
return proto.getBytes();
}

View File

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