Only count user 0 for diskstats package logging.

This avoids sizing issues and allows us to be accurate within
a subset of the users, rather than less accurate over the entire
user base.

Bug: 34457425
Test: services instrumentation tests
Change-Id: Ic460a564a9ab9805c7739b4f40502e96bbfe7953
This commit is contained in:
Daniel Nishi
2017-01-19 11:19:18 -08:00
parent dbb1176b49
commit e47eac74f7
2 changed files with 14 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ package com.android.server.storage;
import android.content.pm.PackageStats;
import android.os.Environment;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Log;
@@ -118,7 +119,7 @@ public class DiskStatsFileLogger {
long appSizeSum = 0L;
long cacheSizeSum = 0L;
boolean isExternal = Environment.isExternalStorageEmulated();
for (Map.Entry<String, PackageStats> entry : mergePackagesAcrossUsers().entrySet()) {
for (Map.Entry<String, PackageStats> entry : filterOnlyPrimaryUser().entrySet()) {
PackageStats stat = entry.getValue();
long appSize = stat.codeSize + stat.dataSize;
long cacheSize = stat.cacheSize;
@@ -141,14 +142,17 @@ public class DiskStatsFileLogger {
}
/**
* A given package may exist for multiple users with distinct sizes. This function merges
* the duplicated packages together and sums up their sizes to get the actual totals for the
* package.
* A given package may exist for multiple users with distinct sizes. This function filters
* the packages that do not belong to user 0 out to ensure that we get good stats for a subset.
* @return A mapping of package name to merged package stats.
*/
private ArrayMap<String, PackageStats> mergePackagesAcrossUsers() {
private ArrayMap<String, PackageStats> filterOnlyPrimaryUser() {
ArrayMap<String, PackageStats> packageMap = new ArrayMap<>();
for (PackageStats stat : mPackageStats) {
if (stat.userHandle != UserHandle.USER_SYSTEM) {
continue;
}
PackageStats existingStats = packageMap.get(stat.packageName);
if (existingStats != null) {
existingStats.cacheSize += stat.cacheSize;

View File

@@ -155,7 +155,7 @@ public class DiskStatsFileLoggerTest extends AndroidTestCase {
}
@Test
public void testDuplicatePackageNameIsMergedAcrossMultipleUsers() throws Exception {
public void testDuplicatePackageNameIsNotMergedAcrossMultipleUsers() throws Exception {
PackageStats app = new PackageStats("com.test.app");
app.dataSize = 1000;
app.externalDataSize = 1000;
@@ -175,19 +175,19 @@ public class DiskStatsFileLoggerTest extends AndroidTestCase {
logger.dumpToFile(mOutputFile);
JSONObject output = getOutputFileAsJson();
assertThat(output.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(2200);
assertThat(output.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(22);
assertThat(output.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY)).isEqualTo(2000);
assertThat(output.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY)).isEqualTo(20);
JSONArray packageNames = output.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
assertThat(packageNames.length()).isEqualTo(1);
assertThat(packageNames.getString(0)).isEqualTo("com.test.app");
JSONArray appSizes = output.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
assertThat(appSizes.length()).isEqualTo(1);
assertThat(appSizes.getLong(0)).isEqualTo(2200);
assertThat(appSizes.getLong(0)).isEqualTo(2000);
JSONArray cacheSizes = output.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
assertThat(cacheSizes.length()).isEqualTo(1);
assertThat(cacheSizes.getLong(0)).isEqualTo(22);
assertThat(cacheSizes.getLong(0)).isEqualTo(20);
}
private JSONObject getOutputFileAsJson() throws Exception {