PageRenderTime 37ms CodeModel.GetById 10ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/services/java/com/android/server/DiskStatsService.java

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