Home | History | Annotate | Download | only in server
      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 
     63     public DiskStatsService(Context context) {
     64         mContext = context;
     65         DiskStatsLoggingService.schedule(context);
     66     }
     67 
     68     @Override
     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 
    129         boolean fileBased = StorageManager.isFileEncryptedNativeOnly();
    130         boolean blockBased = fileBased ? false : StorageManager.isBlockEncrypted();
    131         if (protoFormat) {
    132             if (fileBased) {
    133                 proto.write(DiskStatsServiceDumpProto.ENCRYPTION,
    134                         DiskStatsServiceDumpProto.ENCRYPTION_FILE_BASED);
    135             } else if (blockBased) {
    136                 proto.write(DiskStatsServiceDumpProto.ENCRYPTION,
    137                         DiskStatsServiceDumpProto.ENCRYPTION_FULL_DISK);
    138             } else {
    139                 proto.write(DiskStatsServiceDumpProto.ENCRYPTION,
    140                         DiskStatsServiceDumpProto.ENCRYPTION_NONE);
    141             }
    142         } else if (fileBased) {
    143             pw.println("File-based Encryption: true");
    144         }
    145 
    146         if (protoFormat) {
    147             reportCachedValuesProto(proto);
    148         } else {
    149             reportCachedValues(pw);
    150         }
    151 
    152         if (protoFormat) {
    153             proto.flush();
    154         }
    155         // TODO: Read /proc/yaffs and report interesting values;
    156         // add configurable (through args) performance test parameters.
    157     }
    158 
    159     private void reportFreeSpace(File path, String name, PrintWriter pw,
    160             ProtoOutputStream proto, int folderType) {
    161         try {
    162             StatFs statfs = new StatFs(path.getPath());
    163             long bsize = statfs.getBlockSize();
    164             long avail = statfs.getAvailableBlocks();
    165             long total = statfs.getBlockCount();
    166             if (bsize <= 0 || total <= 0) {
    167                 throw new IllegalArgumentException(
    168                         "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
    169             }
    170 
    171             if (proto != null) {
    172                 long freeSpaceToken = proto.start(DiskStatsServiceDumpProto.PARTITIONS_FREE_SPACE);
    173                 proto.write(DiskStatsFreeSpaceProto.FOLDER, folderType);
    174                 proto.write(DiskStatsFreeSpaceProto.AVAILABLE_SPACE_KB, avail * bsize / 1024);
    175                 proto.write(DiskStatsFreeSpaceProto.TOTAL_SPACE_KB, total * bsize / 1024);
    176                 proto.end(freeSpaceToken);
    177             } else {
    178                 pw.print(name);
    179                 pw.print("-Free: ");
    180                 pw.print(avail * bsize / 1024);
    181                 pw.print("K / ");
    182                 pw.print(total * bsize / 1024);
    183                 pw.print("K total = ");
    184                 pw.print(avail * 100 / total);
    185                 pw.println("% free");
    186             }
    187         } catch (IllegalArgumentException e) {
    188             if (proto != null) {
    189                 // Empty proto
    190             } else {
    191                 pw.print(name);
    192                 pw.print("-Error: ");
    193                 pw.println(e.toString());
    194             }
    195             return;
    196         }
    197     }
    198 
    199     private boolean hasOption(String[] args, String arg) {
    200         for (String opt : args) {
    201             if (arg.equals(opt)) {
    202                 return true;
    203             }
    204         }
    205         return false;
    206     }
    207 
    208     // If you change this method, make sure to modify the Proto version of this method as well.
    209     private void reportCachedValues(PrintWriter pw) {
    210         try {
    211             String jsonString = IoUtils.readFileAsString(DISKSTATS_DUMP_FILE);
    212             JSONObject json = new JSONObject(jsonString);
    213             pw.print("App Size: ");
    214             pw.println(json.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY));
    215             pw.print("App Data Size: ");
    216             pw.println(json.getLong(DiskStatsFileLogger.APP_DATA_SIZE_AGG_KEY));
    217             pw.print("App Cache Size: ");
    218             pw.println(json.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY));
    219             pw.print("Photos Size: ");
    220             pw.println(json.getLong(DiskStatsFileLogger.PHOTOS_KEY));
    221             pw.print("Videos Size: ");
    222             pw.println(json.getLong(DiskStatsFileLogger.VIDEOS_KEY));
    223             pw.print("Audio Size: ");
    224             pw.println(json.getLong(DiskStatsFileLogger.AUDIO_KEY));
    225             pw.print("Downloads Size: ");
    226             pw.println(json.getLong(DiskStatsFileLogger.DOWNLOADS_KEY));
    227             pw.print("System Size: ");
    228             pw.println(json.getLong(DiskStatsFileLogger.SYSTEM_KEY));
    229             pw.print("Other Size: ");
    230             pw.println(json.getLong(DiskStatsFileLogger.MISC_KEY));
    231             pw.print("Package Names: ");
    232             pw.println(json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY));
    233             pw.print("App Sizes: ");
    234             pw.println(json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY));
    235             pw.print("App Data Sizes: ");
    236             pw.println(json.getJSONArray(DiskStatsFileLogger.APP_DATA_KEY));
    237             pw.print("Cache Sizes: ");
    238             pw.println(json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY));
    239         } catch (IOException | JSONException e) {
    240             Log.w(TAG, "exception reading diskstats cache file", e);
    241         }
    242     }
    243 
    244     private void reportCachedValuesProto(ProtoOutputStream proto) {
    245         try {
    246             String jsonString = IoUtils.readFileAsString(DISKSTATS_DUMP_FILE);
    247             JSONObject json = new JSONObject(jsonString);
    248             long cachedValuesToken = proto.start(DiskStatsServiceDumpProto.CACHED_FOLDER_SIZES);
    249 
    250             proto.write(DiskStatsCachedValuesProto.AGG_APPS_SIZE_KB,
    251                     json.getLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY));
    252             proto.write(DiskStatsCachedValuesProto.AGG_APPS_DATA_SIZE_KB,
    253                     json.getLong(DiskStatsFileLogger.APP_DATA_SIZE_AGG_KEY));
    254             proto.write(DiskStatsCachedValuesProto.AGG_APPS_CACHE_SIZE_KB,
    255                     json.getLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY));
    256             proto.write(DiskStatsCachedValuesProto.PHOTOS_SIZE_KB,
    257                     json.getLong(DiskStatsFileLogger.PHOTOS_KEY));
    258             proto.write(DiskStatsCachedValuesProto.VIDEOS_SIZE_KB,
    259                     json.getLong(DiskStatsFileLogger.VIDEOS_KEY));
    260             proto.write(DiskStatsCachedValuesProto.AUDIO_SIZE_KB,
    261                     json.getLong(DiskStatsFileLogger.AUDIO_KEY));
    262             proto.write(DiskStatsCachedValuesProto.DOWNLOADS_SIZE_KB,
    263                     json.getLong(DiskStatsFileLogger.DOWNLOADS_KEY));
    264             proto.write(DiskStatsCachedValuesProto.SYSTEM_SIZE_KB,
    265                     json.getLong(DiskStatsFileLogger.SYSTEM_KEY));
    266             proto.write(DiskStatsCachedValuesProto.OTHER_SIZE_KB,
    267                     json.getLong(DiskStatsFileLogger.MISC_KEY));
    268 
    269             JSONArray packageNamesArray = json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY);
    270             JSONArray appSizesArray = json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY);
    271             JSONArray appDataSizesArray = json.getJSONArray(DiskStatsFileLogger.APP_DATA_KEY);
    272             JSONArray cacheSizesArray = json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY);
    273             final int len = packageNamesArray.length();
    274             if (len == appSizesArray.length()
    275                     && len == appDataSizesArray.length()
    276                     && len == cacheSizesArray.length()) {
    277                 for (int i = 0; i < len; i++) {
    278                     long packageToken = proto.start(DiskStatsCachedValuesProto.APP_SIZES);
    279 
    280                     proto.write(DiskStatsAppSizesProto.PACKAGE_NAME,
    281                             packageNamesArray.getString(i));
    282                     proto.write(DiskStatsAppSizesProto.APP_SIZE_KB, appSizesArray.getLong(i));
    283                     proto.write(DiskStatsAppSizesProto.APP_DATA_SIZE_KB, appDataSizesArray.getLong(i));
    284                     proto.write(DiskStatsAppSizesProto.CACHE_SIZE_KB, cacheSizesArray.getLong(i));
    285 
    286                     proto.end(packageToken);
    287                 }
    288             } else {
    289                 Slog.wtf(TAG, "Sizes of packageNamesArray, appSizesArray, appDataSizesArray "
    290                         + " and cacheSizesArray are not the same");
    291             }
    292 
    293             proto.end(cachedValuesToken);
    294         } catch (IOException | JSONException e) {
    295             Log.w(TAG, "exception reading diskstats cache file", e);
    296         }
    297     }
    298 
    299     private int getRecentPerf() throws RemoteException, IllegalStateException {
    300         IBinder binder = ServiceManager.getService("storaged");
    301         if (binder == null) throw new IllegalStateException("storaged not found");
    302         IStoraged storaged = IStoraged.Stub.asInterface(binder);
    303         return storaged.getRecentPerf();
    304     }
    305 
    306     // Keep reportDiskWriteSpeed and reportDiskWriteSpeedProto in sync
    307     private void reportDiskWriteSpeed(PrintWriter pw) {
    308         try {
    309             long perf = getRecentPerf();
    310             if (perf != 0) {
    311                 pw.print("Recent Disk Write Speed (kB/s) = ");
    312                 pw.println(perf);
    313             } else {
    314                 pw.println("Recent Disk Write Speed data unavailable");
    315                 Log.w(TAG, "Recent Disk Write Speed data unavailable!");
    316             }
    317         } catch (RemoteException | IllegalStateException e) {
    318             pw.println(e.toString());
    319             Log.e(TAG, e.toString());
    320         }
    321     }
    322 
    323     private void reportDiskWriteSpeedProto(ProtoOutputStream proto) {
    324         try {
    325             long perf = getRecentPerf();
    326             if (perf != 0) {
    327                 proto.write(DiskStatsServiceDumpProto.BENCHMARKED_WRITE_SPEED_KBPS, perf);
    328             } else {
    329                 Log.w(TAG, "Recent Disk Write Speed data unavailable!");
    330             }
    331         } catch (RemoteException | IllegalStateException e) {
    332             Log.e(TAG, e.toString());
    333         }
    334     }
    335 }
    336