summaryrefslogtreecommitdiff
path: root/services/usage/java
diff options
context:
space:
mode:
author Daniel Nishi <dhnishi@google.com> 2017-02-13 17:19:43 -0800
committer Daniel Nishi <dhnishi@google.com> 2017-03-02 14:22:24 -0800
commite40da3c1b7d4eb810b6f067075dbbb011d02a379 (patch)
treee9dedf06c95ab37db42ad3a180cb138c978972fb /services/usage/java
parente9f0fe9274cce675e09a117e3593601b9424ea51 (diff)
Save/load calculated cache quotas to a file.
This will prevent us from unnecessarily redoing calculation work by loading the last caches on boot and shoving them down to installd. Bug: 33965858 Test: Framework services tests Change-Id: Ie94e269aa72bceb1ebe87911eaa42e2d826c1123
Diffstat (limited to 'services/usage/java')
-rw-r--r--services/usage/java/com/android/server/usage/StorageStatsService.java39
1 files changed, 31 insertions, 8 deletions
diff --git a/services/usage/java/com/android/server/usage/StorageStatsService.java b/services/usage/java/com/android/server/usage/StorageStatsService.java
index ed1530a4eddb..89e68a63ac3b 100644
--- a/services/usage/java/com/android/server/usage/StorageStatsService.java
+++ b/services/usage/java/com/android/server/usage/StorageStatsService.java
@@ -55,6 +55,8 @@ import com.android.server.pm.Installer;
import com.android.server.pm.Installer.InstallerException;
import com.android.server.storage.CacheQuotaStrategy;
+import java.io.IOException;
+
public class StorageStatsService extends IStorageStatsManager.Stub {
private static final String TAG = "StorageStatsService";
@@ -97,7 +99,7 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
invalidateMounts();
mHandler = new H(IoThread.get().getLooper());
- mHandler.sendEmptyMessageDelayed(H.MSG_CHECK_STORAGE_DELTA, DELAY_IN_MILLIS);
+ mHandler.sendEmptyMessageDelayed(H.MSG_LOAD_CACHED_QUOTAS_FROM_FILE, DELAY_IN_MILLIS);
mStorage.registerListener(new StorageEventListener() {
@Override
@@ -343,12 +345,14 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
private class H extends Handler {
private static final int MSG_CHECK_STORAGE_DELTA = 100;
+ private static final int MSG_LOAD_CACHED_QUOTAS_FROM_FILE = 101;
/**
* By only triggering a re-calculation after the storage has changed sizes, we can avoid
* recalculating quotas too often. Minimum change delta defines the percentage of change
* we need to see before we recalculate.
*/
private static final double MINIMUM_CHANGE_DELTA = 0.05;
+ private static final int UNSET = -1;
private static final boolean DEBUG = false;
private final StatFs mStats;
@@ -361,7 +365,6 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
mStats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
mPreviousBytes = mStats.getFreeBytes();
mMinimumThresholdBytes = mStats.getTotalBytes() * MINIMUM_CHANGE_DELTA;
- // TODO: Load cache quotas from a file to avoid re-doing work.
}
public void handleMessage(Message msg) {
@@ -378,7 +381,26 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
long bytesDelta = Math.abs(mPreviousBytes - mStats.getFreeBytes());
if (bytesDelta > mMinimumThresholdBytes) {
mPreviousBytes = mStats.getFreeBytes();
- recalculateQuotas();
+ recalculateQuotas(getInitializedStrategy());
+ }
+ sendEmptyMessageDelayed(MSG_CHECK_STORAGE_DELTA, DELAY_IN_MILLIS);
+ break;
+ }
+ case MSG_LOAD_CACHED_QUOTAS_FROM_FILE: {
+ CacheQuotaStrategy strategy = getInitializedStrategy();
+ mPreviousBytes = UNSET;
+ try {
+ mPreviousBytes = strategy.setupQuotasFromFile();
+ } catch (IOException e) {
+ Slog.e(TAG, "An error occurred while reading the cache quota file.", e);
+ } catch (IllegalStateException e) {
+ Slog.e(TAG, "Cache quota XML file is malformed?", e);
+ }
+
+ // If errors occurred getting the quotas from disk, let's re-calc them.
+ if (mPreviousBytes < 0) {
+ mPreviousBytes = mStats.getFreeBytes();
+ recalculateQuotas(strategy);
}
sendEmptyMessageDelayed(MSG_CHECK_STORAGE_DELTA, DELAY_IN_MILLIS);
break;
@@ -391,17 +413,18 @@ public class StorageStatsService extends IStorageStatsManager.Stub {
}
}
- private void recalculateQuotas() {
+ private void recalculateQuotas(CacheQuotaStrategy strategy) {
if (DEBUG) {
Slog.v(TAG, ">>> recalculating quotas ");
}
+ strategy.recalculateQuotas();
+ }
+
+ private CacheQuotaStrategy getInitializedStrategy() {
UsageStatsManagerInternal usageStatsManager =
LocalServices.getService(UsageStatsManagerInternal.class);
- CacheQuotaStrategy strategy = new CacheQuotaStrategy(
- mContext, usageStatsManager, mInstaller);
- // TODO: Save cache quotas to an XML file.
- strategy.recalculateQuotas();
+ return new CacheQuotaStrategy(mContext, usageStatsManager, mInstaller);
}
}