Add an option, --reset-all, to dumpsys batterystats.

* Removes historical battery usage stats data in addition to clearing batterystats.

Test: atest FrameworksCoreTests:BatteryUsageStatsStoreTest#testRemoveAllBusSnapshots
Bug: 226443003
Change-Id: I9b29f97b45f7aa3c49cd8e354107ab7f13a2ce7d
This commit is contained in:
Michael Rosenfeld
2022-04-22 17:38:18 -07:00
parent 6d8f27328f
commit f8d37749b4
3 changed files with 46 additions and 1 deletions

View File

@@ -320,4 +320,19 @@ public class BatteryUsageStatsStore {
mFileSizes.remove(file);
}
}
public void removeAllSnapshots() {
lockSnapshotDirectory();
try {
for (File file : mStoreDir.listFiles()) {
if (file.getName().endsWith(SNAPSHOT_FILE_EXTENSION)) {
if (!file.delete()) {
Slog.e(TAG, "Cannot delete battery usage stats " + file);
}
}
}
} finally {
unlockSnapshotDirectory();
}
}
}

View File

@@ -131,6 +131,26 @@ public class BatteryUsageStatsStoreTest {
.isAtMost(MAX_BATTERY_STATS_SNAPSHOT_STORAGE_BYTES);
}
@Test
public void testRemoveAllSnapshots() throws Exception {
prepareBatteryStats();
for (int i = 0; i < 3; i++) {
mMockClock.realtime += 10_000_000;
mMockClock.uptime += 10_000_000;
mMockClock.currentTime += 10_000_000;
prepareBatteryStats();
mBatteryStats.resetAllStatsCmdLocked();
}
assertThat(getDirectorySize(mStoreDirectory)).isNotEqualTo(0);
mBatteryUsageStatsStore.removeAllSnapshots();
assertThat(getDirectorySize(mStoreDirectory)).isEqualTo(0);
}
@Test
public void testSavingStatsdAtomPullTimestamp() {
mBatteryUsageStatsStore.setLastBatteryUsageStatsBeforeResetAtomPullTimestamp(1234);

View File

@@ -2260,7 +2260,8 @@ public final class BatteryStatsService extends IBatteryStats.Stub
private void dumpHelp(PrintWriter pw) {
pw.println("Battery stats (batterystats) dump options:");
pw.println(" [--checkin] [--proto] [--history] [--history-start] [--charged] [-c]");
pw.println(" [--daily] [--reset] [--write] [--new-daily] [--read-daily] [-h] [<package.name>]");
pw.println(" [--daily] [--reset] [--reset-all] [--write] [--new-daily] [--read-daily]");
pw.println(" [-h] [<package.name>]");
pw.println(" --checkin: generate output for a checkin report; will write (and clear) the");
pw.println(" last old completed stats when they had been reset.");
pw.println(" -c: write the current stats in checkin format.");
@@ -2271,6 +2272,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
pw.println(" --charged: only output data since last charged.");
pw.println(" --daily: only output full daily data.");
pw.println(" --reset: reset the stats, clearing all current data.");
pw.println(" --reset-all: reset the stats, clearing all current and past data.");
pw.println(" --write: force write current collected stats to disk.");
pw.println(" --new-daily: immediately create and write new daily stats record.");
pw.println(" --read-daily: read-load last written daily stats.");
@@ -2407,6 +2409,14 @@ public final class BatteryStatsService extends IBatteryStats.Stub
flags |= BatteryStats.DUMP_CHARGED_ONLY;
} else if ("--daily".equals(arg)) {
flags |= BatteryStats.DUMP_DAILY_ONLY;
} else if ("--reset-all".equals(arg)) {
awaitCompletion();
synchronized (mStats) {
mStats.resetAllStatsCmdLocked();
mBatteryUsageStatsStore.removeAllSnapshots();
pw.println("Battery stats and history reset.");
noOutput = true;
}
} else if ("--reset".equals(arg)) {
awaitCompletion();
synchronized (mStats) {