Merge "Set forgiven bytes to default PerStateBytes object when resetting stats." into sc-v2-dev
diff --git a/service/src/com/android/car/watchdog/WatchdogPerfHandler.java b/service/src/com/android/car/watchdog/WatchdogPerfHandler.java
index 8663c5d..e83288b 100644
--- a/service/src/com/android/car/watchdog/WatchdogPerfHandler.java
+++ b/service/src/com/android/car/watchdog/WatchdogPerfHandler.java
@@ -1707,12 +1707,14 @@
     }
     /** Defines I/O usage fields for a package. */
     public static final class PackageIoUsage {
+        private static final android.automotive.watchdog.PerStateBytes DEFAULT_PER_STATE_BYTES =
+                new android.automotive.watchdog.PerStateBytes();
         private android.automotive.watchdog.IoOveruseStats mIoOveruseStats;
         private android.automotive.watchdog.PerStateBytes mForgivenWriteBytes;
         private int mTotalTimesKilled;
 
         private PackageIoUsage() {
-            mForgivenWriteBytes = new android.automotive.watchdog.PerStateBytes();
+            mForgivenWriteBytes = DEFAULT_PER_STATE_BYTES;
             mTotalTimesKilled = 0;
         }
 
@@ -1779,7 +1781,7 @@
 
         void resetStats() {
             mIoOveruseStats = null;
-            mForgivenWriteBytes = null;
+            mForgivenWriteBytes = DEFAULT_PER_STATE_BYTES;
             mTotalTimesKilled = 0;
         }
     }
diff --git a/tests/carservice_unit_test/src/com/android/car/watchdog/CarWatchdogServiceUnitTest.java b/tests/carservice_unit_test/src/com/android/car/watchdog/CarWatchdogServiceUnitTest.java
index e0cbc20..a3914d7 100644
--- a/tests/carservice_unit_test/src/com/android/car/watchdog/CarWatchdogServiceUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/watchdog/CarWatchdogServiceUnitTest.java
@@ -2047,6 +2047,37 @@
     }
 
     @Test
+    public void testSaveToStorageAfterResetResourceOveruseStats() throws Exception {
+        setDate(1);
+        mGenericPackageNameByUid.put(1011200, "system_package");
+        SparseArray<PackageIoOveruseStats> stats = injectIoOveruseStatsForPackages(
+                mGenericPackageNameByUid, /* killablePackages= */ new ArraySet<>(),
+                /* shouldNotifyPackages= */ new ArraySet<>());
+
+        mWatchdogServiceForSystemImpl.resetResourceOveruseStats(
+                Collections.singletonList("system_package"));
+
+        /* |resetResourceOveruseStats| sets the package's IoOveruseStats to null, packages with
+         * null I/O stats are not written to disk. Push new IoOveruseStats to the |system_package|
+         * so that the package can be written to the database when date changes.
+         */
+        pushLatestIoOveruseStatsAndWait(Collections.singletonList(stats.get(1011200)));
+
+        /* Force write to disk by changing the date and pushing new I/O overuse stats. */
+        setDate(0);
+        pushLatestIoOveruseStatsAndWait(Collections.singletonList(new PackageIoOveruseStats()));
+
+        WatchdogStorage.IoUsageStatsEntry expectedSavedEntries =
+                new WatchdogStorage.IoUsageStatsEntry(/* userId= */ 10, "system_package",
+                        new WatchdogPerfHandler.PackageIoUsage(stats.get(1011200).ioOveruseStats,
+                                /* forgivenWriteBytes= */ constructPerStateBytes(0, 0, 0),
+                                /* totalTimesKilled= */ 0));
+
+        IoUsageStatsEntrySubject.assertThat(mIoUsageStatsEntries)
+                .containsExactlyElementsIn(Collections.singletonList(expectedSavedEntries));
+    }
+
+    @Test
     public void testGetPackageInfosForUids() throws Exception {
         injectPackageInfos(Arrays.asList(
                 constructPackageManagerPackageInfo(