From f0bb51b1856c0632db16620a0b66862e1f44b880 Mon Sep 17 00:00:00 2001 From: Neharika Jali Date: Wed, 20 Oct 2021 06:28:44 +0000 Subject: [PATCH] Change reserved cache space as sum of cache quotas of all uids Bug: 203650406 Test: atest frameworks/base/core/tests/coretests/src/android/os/storage/StorageManagerBaseTest atest cts/tests/tests/os/src/android/os/storage/cts/StorageManagerTest#testComputeStorageCacheBytes Change-Id: I72710c89961ecc16b27ebb0647798f28e2e810ca --- core/api/module-lib-current.txt | 1 + core/api/test-current.txt | 1 + .../android/os/storage/StorageManager.java | 83 +++++++++++++++---- .../os/storage/StorageManagerBaseTest.java | 49 ++++++++++- 4 files changed, 117 insertions(+), 17 deletions(-) diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt index 70bb13a36b6ec..ba34ce433ac6d 100644 --- a/core/api/module-lib-current.txt +++ b/core/api/module-lib-current.txt @@ -341,6 +341,7 @@ package android.os { package android.os.storage { public class StorageManager { + method public long computeStorageCacheBytes(@NonNull java.io.File); method public void notifyAppIoBlocked(@NonNull java.util.UUID, int, int, int); method public void notifyAppIoResumed(@NonNull java.util.UUID, int, int, int); field public static final int APP_IO_BLOCKED_REASON_TRANSCODING = 1; // 0x1 diff --git a/core/api/test-current.txt b/core/api/test-current.txt index c4b20117b5b8a..96374fbd05fa0 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -1976,6 +1976,7 @@ package android.os.storage { } public class StorageManager { + method public long computeStorageCacheBytes(@NonNull java.io.File); method @NonNull public static java.util.UUID convert(@NonNull String); method @NonNull public static String convert(@NonNull java.util.UUID); method public boolean isAppIoBlocked(@NonNull java.util.UUID, int, int, int); diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index 77c794cd17a89..e52cd5aa6cffd 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -1434,11 +1434,36 @@ public class StorageManager { throw new IllegalStateException("Missing primary storage"); } - private static final int DEFAULT_THRESHOLD_PERCENTAGE = 5; - private static final long DEFAULT_THRESHOLD_MAX_BYTES = DataUnit.MEBIBYTES.toBytes(500); + /** + * Devices having above STORAGE_THRESHOLD_PERCENT_HIGH of total space free are considered to be + * in high free space category. + * + * @hide + */ + public static final int STORAGE_THRESHOLD_PERCENT_HIGH = 20; + /** + * Devices having below STORAGE_THRESHOLD_PERCENT_LOW of total space free are considered to be + * in low free space category. + * + * @hide + */ + public static final int STORAGE_THRESHOLD_PERCENT_LOW = 5; + /** + * For devices in high free space category, CACHE_RESERVE_PERCENT_HIGH percent of total space is + * allocated for cache. + * + * @hide + */ + public static final int CACHE_RESERVE_PERCENT_HIGH = 10; + /** + * For devices in low free space category, CACHE_RESERVE_PERCENT_LOW percent of total space is + * allocated for cache. + * + * @hide + */ + public static final int CACHE_RESERVE_PERCENT_LOW = 2; - private static final int DEFAULT_CACHE_PERCENTAGE = 10; - private static final long DEFAULT_CACHE_MAX_BYTES = DataUnit.GIBIBYTES.toBytes(5); + private static final long DEFAULT_THRESHOLD_MAX_BYTES = DataUnit.MEBIBYTES.toBytes(500); private static final long DEFAULT_FULL_THRESHOLD_BYTES = DataUnit.MEBIBYTES.toBytes(1); @@ -1462,7 +1487,7 @@ public class StorageManager { @UnsupportedAppUsage public long getStorageLowBytes(File path) { final long lowPercent = Settings.Global.getInt(mResolver, - Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE, DEFAULT_THRESHOLD_PERCENTAGE); + Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE, STORAGE_THRESHOLD_PERCENT_LOW); final long lowBytes = (path.getTotalSpace() * lowPercent) / 100; final long maxLowBytes = Settings.Global.getLong(mResolver, @@ -1471,29 +1496,55 @@ public class StorageManager { return Math.min(lowBytes, maxLowBytes); } + /** + * Compute the minimum number of bytes of storage on the device that could + * be reserved for cached data depending on the device state which is then passed on + * to getStorageCacheBytes. + * + * @hide + */ + @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) + @TestApi + @SuppressLint("StreamFiles") + public long computeStorageCacheBytes(@NonNull File path) { + final long totalBytes = path.getTotalSpace(); + final long usableBytes = path.getUsableSpace(); + final long storageThresholdHighBytes = totalBytes * STORAGE_THRESHOLD_PERCENT_HIGH / 100; + final long storageThresholdLowBytes = getStorageLowBytes(path); + long result; + if (usableBytes > storageThresholdHighBytes) { + // If free space is >STORAGE_THRESHOLD_PERCENT_HIGH of total space, + // reserve CACHE_RESERVE_PERCENT_HIGH of total space + result = totalBytes * CACHE_RESERVE_PERCENT_HIGH / 100; + } else if (usableBytes < storageThresholdLowBytes) { + // If free space is