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.StatFs;
     23 import android.os.SystemClock;
     24 
     25 import java.io.File;
     26 import java.io.FileDescriptor;
     27 import java.io.FileOutputStream;
     28 import java.io.IOException;
     29 import java.io.PrintWriter;
     30 
     31 /**
     32  * This service exists only as a "dumpsys" target which reports
     33  * statistics about the status of the disk.
     34  */
     35 public class DiskStatsService extends Binder {
     36     private static final String TAG = "DiskStatsService";
     37 
     38     private final Context mContext;
     39 
     40     public DiskStatsService(Context context) {
     41         mContext = context;
     42     }
     43 
     44     @Override
     45     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
     46         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
     47 
     48         // Run a quick-and-dirty performance test: write 512 bytes
     49         byte[] junk = new byte[512];
     50         for (int i = 0; i < junk.length; i++) junk[i] = (byte) i;  // Write nonzero bytes
     51 
     52         File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp");
     53         FileOutputStream fos = null;
     54         IOException error = null;
     55 
     56         long before = SystemClock.uptimeMillis();
     57         try {
     58             fos = new FileOutputStream(tmp);
     59             fos.write(junk);
     60         } catch (IOException e) {
     61             error = e;
     62         } finally {
     63             try { if (fos != null) fos.close(); } catch (IOException e) {}
     64         }
     65 
     66         long after = SystemClock.uptimeMillis();
     67         if (tmp.exists()) tmp.delete();
     68 
     69         if (error != null) {
     70             pw.print("Test-Error: ");
     71             pw.println(error.toString());
     72         } else {
     73             pw.print("Latency: ");
     74             pw.print(after - before);
     75             pw.println("ms [512B Data Write]");
     76         }
     77 
     78         reportFreeSpace(Environment.getDataDirectory(), "Data", pw);
     79         reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw);
     80         reportFreeSpace(new File("/system"), "System", pw);
     81 
     82         // TODO: Read /proc/yaffs and report interesting values;
     83         // add configurable (through args) performance test parameters.
     84     }
     85 
     86     private void reportFreeSpace(File path, String name, PrintWriter pw) {
     87         try {
     88             StatFs statfs = new StatFs(path.getPath());
     89             long bsize = statfs.getBlockSize();
     90             long avail = statfs.getAvailableBlocks();
     91             long total = statfs.getBlockCount();
     92             if (bsize <= 0 || total <= 0) {
     93                 throw new IllegalArgumentException(
     94                         "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
     95             }
     96 
     97             pw.print(name);
     98             pw.print("-Free: ");
     99             pw.print(avail * bsize / 1024);
    100             pw.print("K / ");
    101             pw.print(total * bsize / 1024);
    102             pw.print("K total = ");
    103             pw.print(avail * 100 / total);
    104             pw.println("% free");
    105         } catch (IllegalArgumentException e) {
    106             pw.print(name);
    107             pw.print("-Error: ");
    108             pw.println(e.toString());
    109             return;
    110         }
    111     }
    112 }
    113