1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server;
18 
19 import android.content.Context;
20 import android.os.Binder;
21 import android.os.Environment;
22 import android.os.IBinder;
23 import android.os.IStoraged;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.os.StatFs;
27 import android.os.SystemClock;
28 import android.os.storage.StorageManager;
29 import android.service.diskstats.DiskStatsAppSizesProto;
30 import android.service.diskstats.DiskStatsCachedValuesProto;
31 import android.service.diskstats.DiskStatsFreeSpaceProto;
32 import android.service.diskstats.DiskStatsServiceDumpProto;
33 import android.util.Log;
34 import android.util.Slog;
35 import android.util.proto.ProtoOutputStream;
36 
37 import com.android.internal.util.DumpUtils;
38 import com.android.server.storage.DiskStatsFileLogger;
39 import com.android.server.storage.DiskStatsLoggingService;
40 
41 import libcore.io.IoUtils;
42 
43 import org.json.JSONArray;
44 import org.json.JSONException;
45 import org.json.JSONObject;
46 
47 import java.io.File;
48 import java.io.FileDescriptor;
49 import java.io.FileOutputStream;
50 import java.io.IOException;
51 import java.io.PrintWriter;
52 
53 /**
54  * This service exists only as a "dumpsys" target which reports
55  * statistics about the status of the disk.
56  */
57 public class DiskStatsService extends Binder {
58     private static final String TAG = "DiskStatsService";
59     private static final String DISKSTATS_DUMP_FILE = "/data/system/diskstats_cache.json";
60 
61     private final Context mContext;
62 
DiskStatsService(Context context)63     public DiskStatsService(Context context) {
64         mContext = context;
65         DiskStatsLoggingService.schedule(context);
66     }
67 
68     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)69     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
70         if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return;
71 
72         // Run a quick-and-dirty performance test: write 512 bytes
73         byte[] junk = new byte[512];
74         for (int i = 0; i < junk.length; i++) junk[i] = (byte) i;  // Write nonzero bytes
75 
76         File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp");
77         FileOutputStream fos = null;
78         IOException error = null;
79 
80         long before = SystemClock.uptimeMillis();
81         try {
82             fos = new FileOutputStream(tmp);
83             fos.write(junk);
84         } catch (IOException e) {
85             error = e;
86         } finally {
87             try { if (fos != null) fos.close(); } catch (IOException e) {}
88         }
89 
90         long after = SystemClock.uptimeMillis();
91         if (tmp.exists()) tmp.delete();
92 
93         boolean protoFormat = hasOption(args, "--proto");
94         ProtoOutputStream proto = null;
95 
96         if (protoFormat) {
97             proto = new ProtoOutputStream(fd);
98             pw = null;
99             proto.write(DiskStatsServiceDumpProto.HAS_TEST_ERROR, error != null);
100             if (error != null) {
101                 proto.write(DiskStatsServiceDumpProto.ERROR_MESSAGE, error.toString());
102             } else {
103                 proto.write(DiskStatsServiceDumpProto.WRITE_512B_LATENCY_MILLIS, after - before);
104             }
105         } else {
106             if (error != null) {
107                 pw.print("Test-Error: ");
108                 pw.println(error.toString());
109             } else {
110                 pw.print("Latency: ");
111                 pw.print(after - before);
112                 pw.println("ms [512B Data Write]");
113             }
114         }
115 
116         if (protoFormat) {
117             reportDiskWriteSpeedProto(proto);
118         } else {
119             reportDiskWriteSpeed(pw);
120         }
121 
122         reportFreeSpace(Environment.getDataDirectory(), "Data", pw, proto,
123                 DiskStatsFreeSpaceProto.FOLDER_DATA);
124         reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw, proto,
125                 DiskStatsFreeSpaceProto.FOLDER_CACHE);
126         reportFreeSpace(new File("/system"), "System", pw, proto,
127                 DiskStatsFreeSpaceProto.FOLDER_SYSTEM);
128         reportFreeSpace(Environment.getMetadataDirectory(), "Metadata", pw, proto,
129                 DiskStatsFreeSpaceProto.FOLDER_METADATA);
130 
131         boolean fileBased = StorageManager.isFileEncrypted();
132         if (protoFormat) {
133             if (fileBased) {
134                 proto.write(DiskStatsServiceDumpProto.ENCRYPTION,
135                         DiskStatsServiceDumpProto.ENCRYPTION_FILE_BASED);
136             } else {
137                 proto.write(DiskStatsServiceDumpProto.ENCRYPTION,
138                         DiskStatsServiceDumpProto.ENCRYPTION_NONE);
139             }
140         } else if (fileBased) {
141             pw.println("File-based Encryption: true");
142         }
143 
144         if (protoFormat) {
145             reportCachedValuesProto(proto);
146         } else {
147             reportCachedValues(pw);
148         }
149 
150         if (protoFormat) {
151             proto.flush();
152         }
153         // TODO: Read /proc/yaffs and report interesting values;
154         // add configurable (through args) performance test parameters.
155     }
156 
reportFreeSpace(File path, String name, PrintWriter pw, ProtoOutputStream proto, int folderType)157     private void reportFreeSpace(File path, String name, PrintWriter pw,
158             ProtoOutputStream proto, int folderType) {
159         try {
160             StatFs statfs = new StatFs(path.getPath());
161             long bsize = statfs.getBlockSize();
162             long avail = statfs.getAvailableBlocks();
163             long total = statfs.getBlockCount();
164             if (bsize <= 0 || total <= 0) {
165                 throw new IllegalArgumentException(
166                         "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
167             }
168 
169             if (proto != null) {
170                 long freeSpaceToken = proto.start(DiskStatsServiceDumpProto.PARTITIONS_FREE_SPACE);
171                 proto.write(DiskStatsFreeSpaceProto.FOLDER, folderType);
172                 proto.write(DiskStatsFreeSpaceProto.AVAILABLE_SPACE_KB, avail * bsize / 1024);
173                 proto.write(DiskStatsFreeSpaceProto.TOTAL_SPACE_KB, total * bsize / 1024);
174                 proto.end(freeSpaceToken);
175             } else {
176                 pw.print(name);
177                 pw.print("-Free: ");
178                 pw.print(avail * bsize / 1024);
179                 pw.print("K / ");
180                 pw.print(total * bsize / 1024);
181                 pw.print("K total = ");
182                 pw.print(avail * 100 / total);
183                 pw.println("% free");
184             }
185         } catch (IllegalArgumentException e) {
186             if (proto != null) {
187                 // Empty proto
188             } else {
189                 pw.print(name);
190                 pw.print("-Error: ");
191                 pw.println(e.toString());
192             }
193             return;
194         }
195     }
196 
hasOption(String[] args, String arg)197     private boolean hasOption(String[] args, String arg) {
198         for (String opt : args) {
199             if (arg.equals(opt)) {
200                 return true;
201             }
202         }
203         return false;
204     }
205 
206     // If you change this method, make sure to modify the Proto version of this method as well.
reportCachedValues(PrintWriter pw)207     private void reportCachedValues(PrintWriter pw) {
208         try {
209             String jsonString = IoUtils.readFileAsString(DISKSTATS_DUMP_FILE);
210             JSONObject json = new JSONObject(jsonString);
211             pw.print("App Size: ");
212             pw.println(json.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY));
213             pw.print("App Data Size: ");
214             pw.println(json.getLong(DiskStatsFileLogger.APP_DATA_SIZE_AGG_KEY));
215             pw.print("App Cache Size: ");
216             pw.println(json.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY));
217             pw.print("Photos Size: ");
218             pw.println(json.getLong(DiskStatsFileLogger.PHOTOS_KEY));
219             pw.print("Videos Size: ");
220             pw.println(json.getLong(DiskStatsFileLogger.VIDEOS_KEY));
221             pw.print("Audio Size: ");
222             pw.println(json.getLong(DiskStatsFileLogger.AUDIO_KEY));
223             pw.print("Downloads Size: ");
224             pw.println(json.getLong(DiskStatsFileLogger.DOWNLOADS_KEY));
225             pw.print("System Size: ");
226             pw.println(json.getLong(DiskStatsFileLogger.SYSTEM_KEY));
227             pw.print("Other Size: ");
228             pw.println(json.getLong(DiskStatsFileLogger.MISC_KEY));
229             pw.print("Package Names: ");
230             pw.println(json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY));
231             pw.print("App Sizes: ");
232             pw.println(json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY));
233             pw.print("App Data Sizes: ");
234             pw.println(json.getJSONArray(DiskStatsFileLogger.APP_DATA_KEY));
235             pw.print("Cache Sizes: ");
236             pw.println(json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY));
237         } catch (IOException | JSONException e) {
238             Log.w(TAG, "exception reading diskstats cache file", e);
239         }
240     }
241 
reportCachedValuesProto(ProtoOutputStream proto)242     private void reportCachedValuesProto(ProtoOutputStream proto) {
243         try {
244             String jsonString = IoUtils.readFileAsString(DISKSTATS_DUMP_FILE);
245             JSONObject json = new JSONObject(jsonString);
246             long cachedValuesToken = proto.start(DiskStatsServiceDumpProto.CACHED_FOLDER_SIZES);
247 
248             proto.write(DiskStatsCachedValuesProto.AGG_APPS_SIZE_KB,
249                     json.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY));
250             proto.write(DiskStatsCachedValuesProto.AGG_APPS_DATA_SIZE_KB,
251                     json.getLong(DiskStatsFileLogger.APP_DATA_SIZE_AGG_KEY));
252             proto.write(DiskStatsCachedValuesProto.AGG_APPS_CACHE_SIZE_KB,
253                     json.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY));
254             proto.write(DiskStatsCachedValuesProto.PHOTOS_SIZE_KB,
255                     json.getLong(DiskStatsFileLogger.PHOTOS_KEY));
256             proto.write(DiskStatsCachedValuesProto.VIDEOS_SIZE_KB,
257                     json.getLong(DiskStatsFileLogger.VIDEOS_KEY));
258             proto.write(DiskStatsCachedValuesProto.AUDIO_SIZE_KB,
259                     json.getLong(DiskStatsFileLogger.AUDIO_KEY));
260             proto.write(DiskStatsCachedValuesProto.DOWNLOADS_SIZE_KB,
261                     json.getLong(DiskStatsFileLogger.DOWNLOADS_KEY));
262             proto.write(DiskStatsCachedValuesProto.SYSTEM_SIZE_KB,
263                     json.getLong(DiskStatsFileLogger.SYSTEM_KEY));
264             proto.write(DiskStatsCachedValuesProto.OTHER_SIZE_KB,
265                     json.getLong(DiskStatsFileLogger.MISC_KEY));
266 
267             JSONArray packageNamesArray = json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
268             JSONArray appSizesArray = json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
269             JSONArray appDataSizesArray = json.getJSONArray(DiskStatsFileLogger.APP_DATA_KEY);
270             JSONArray cacheSizesArray = json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
271             final int len = packageNamesArray.length();
272             if (len == appSizesArray.length()
273                     && len == appDataSizesArray.length()
274                     && len == cacheSizesArray.length()) {
275                 for (int i = 0; i < len; i++) {
276                     long packageToken = proto.start(DiskStatsCachedValuesProto.APP_SIZES);
277 
278                     proto.write(DiskStatsAppSizesProto.PACKAGE_NAME,
279                             packageNamesArray.getString(i));
280                     proto.write(DiskStatsAppSizesProto.APP_SIZE_KB, appSizesArray.getLong(i));
281                     proto.write(DiskStatsAppSizesProto.APP_DATA_SIZE_KB, appDataSizesArray.getLong(i));
282                     proto.write(DiskStatsAppSizesProto.CACHE_SIZE_KB, cacheSizesArray.getLong(i));
283 
284                     proto.end(packageToken);
285                 }
286             } else {
287                 Slog.wtf(TAG, "Sizes of packageNamesArray, appSizesArray, appDataSizesArray "
288                         + " and cacheSizesArray are not the same");
289             }
290 
291             proto.end(cachedValuesToken);
292         } catch (IOException | JSONException e) {
293             Log.w(TAG, "exception reading diskstats cache file", e);
294         }
295     }
296 
getRecentPerf()297     private int getRecentPerf() throws RemoteException, IllegalStateException {
298         IBinder binder = ServiceManager.getService("storaged");
299         if (binder == null) throw new IllegalStateException("storaged not found");
300         IStoraged storaged = IStoraged.Stub.asInterface(binder);
301         return storaged.getRecentPerf();
302     }
303 
304     // Keep reportDiskWriteSpeed and reportDiskWriteSpeedProto in sync
reportDiskWriteSpeed(PrintWriter pw)305     private void reportDiskWriteSpeed(PrintWriter pw) {
306         try {
307             long perf = getRecentPerf();
308             if (perf != 0) {
309                 pw.print("Recent Disk Write Speed (kB/s) = ");
310                 pw.println(perf);
311             } else {
312                 pw.println("Recent Disk Write Speed data unavailable");
313                 Log.w(TAG, "Recent Disk Write Speed data unavailable!");
314             }
315         } catch (RemoteException | IllegalStateException e) {
316             pw.println(e.toString());
317             Log.e(TAG, e.toString());
318         }
319     }
320 
reportDiskWriteSpeedProto(ProtoOutputStream proto)321     private void reportDiskWriteSpeedProto(ProtoOutputStream proto) {
322         try {
323             long perf = getRecentPerf();
324             if (perf != 0) {
325                 proto.write(DiskStatsServiceDumpProto.BENCHMARKED_WRITE_SPEED_KBPS, perf);
326             } else {
327                 Log.w(TAG, "Recent Disk Write Speed data unavailable!");
328             }
329         } catch (RemoteException | IllegalStateException e) {
330             Log.e(TAG, e.toString());
331         }
332     }
333 }
334