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