summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/storage/DiskStatsFileLogger.java
blob: 0094ab5559d878111e7e85a77ffd1e02d2f6dc78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

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;

import com.android.server.storage.FileCollector.MeasurementResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * DiskStatsFileLogger logs collected storage information to a file in a JSON format.
 *
 * The following information is cached in the file:
 * 1. Size of images on disk.
 * 2. Size of videos on disk.
 * 3. Size of audio on disk.
 * 4. Size of the downloads folder.
 * 5. System size.
 * 6. Aggregate and individual app and app cache sizes.
 * 7. How much storage couldn't be categorized in one of the above categories.
 */
public class DiskStatsFileLogger {
    private static final String TAG = "DiskStatsLogger";

    public static final String PHOTOS_KEY = "photosSize";
    public static final String VIDEOS_KEY = "videosSize";
    public static final String AUDIO_KEY = "audioSize";
    public static final String DOWNLOADS_KEY = "downloadsSize";
    public static final String SYSTEM_KEY = "systemSize";
    public static final String MISC_KEY = "otherSize";
    public static final String APP_SIZE_AGG_KEY = "appSize";
    public static final String APP_CACHE_AGG_KEY = "cacheSize";
    public static final String PACKAGE_NAMES_KEY = "packageNames";
    public static final String APP_SIZES_KEY = "appSizes";
    public static final String APP_CACHES_KEY = "cacheSizes";
    public static final String LAST_QUERY_TIMESTAMP_KEY = "queryTime";

    private MeasurementResult mResult;
    private long mDownloadsSize;
    private long mSystemSize;
    private List<PackageStats> mPackageStats;

    /**
     * Constructs a DiskStatsFileLogger with calculated measurement results.
     */
    public DiskStatsFileLogger(MeasurementResult result, MeasurementResult downloadsResult,
            List<PackageStats> stats, long systemSize) {
        mResult = result;
        mDownloadsSize = downloadsResult.totalAccountedSize();
        mSystemSize = systemSize;
        mPackageStats = stats;
    }

    /**
     * Dumps the storage collection output to a file.
     * @param file File to write the output into.
     * @throws FileNotFoundException
     */
    public void dumpToFile(File file) throws FileNotFoundException {
        PrintWriter pw = new PrintWriter(file);
        JSONObject representation = getJsonRepresentation();
        if (representation != null) {
            pw.println(representation);
        }
        pw.close();
    }

    private JSONObject getJsonRepresentation() {
        JSONObject json = new JSONObject();
        try {
            json.put(LAST_QUERY_TIMESTAMP_KEY, System.currentTimeMillis());
            json.put(PHOTOS_KEY, mResult.imagesSize);
            json.put(VIDEOS_KEY, mResult.videosSize);
            json.put(AUDIO_KEY, mResult.audioSize);
            json.put(DOWNLOADS_KEY, mDownloadsSize);
            json.put(SYSTEM_KEY, mSystemSize);
            json.put(MISC_KEY, mResult.miscSize);
            addAppsToJson(json);
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
            return null;
        }

        return json;
    }

    private void addAppsToJson(JSONObject json) throws JSONException {
        JSONArray names = new JSONArray();
        JSONArray appSizeList = new JSONArray();
        JSONArray cacheSizeList = new JSONArray();

        long appSizeSum = 0L;
        long cacheSizeSum = 0L;
        boolean isExternal = Environment.isExternalStorageEmulated();
        for (Map.Entry<String, PackageStats> entry : filterOnlyPrimaryUser().entrySet()) {
            PackageStats stat = entry.getValue();
            long appSize = stat.codeSize + stat.dataSize;
            long cacheSize = stat.cacheSize;
            if (isExternal) {
                appSize += stat.externalCodeSize + stat.externalDataSize;
                cacheSize += stat.externalCacheSize;
            }
            appSizeSum += appSize;
            cacheSizeSum += cacheSize;

            names.put(stat.packageName);
            appSizeList.put(appSize);
            cacheSizeList.put(cacheSize);
        }
        json.put(PACKAGE_NAMES_KEY, names);
        json.put(APP_SIZES_KEY, appSizeList);
        json.put(APP_CACHES_KEY, cacheSizeList);
        json.put(APP_SIZE_AGG_KEY, appSizeSum);
        json.put(APP_CACHE_AGG_KEY, cacheSizeSum);
    }

    /**
     * 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> 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;
                existingStats.codeSize += stat.codeSize;
                existingStats.dataSize += stat.dataSize;
                existingStats.externalCacheSize += stat.externalCacheSize;
                existingStats.externalCodeSize += stat.externalCodeSize;
                existingStats.externalDataSize += stat.externalDataSize;
            } else {
                packageMap.put(stat.packageName, new PackageStats(stat));
            }
        }
        return packageMap;
    }
}