2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
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 import android.os.storage.StorageManager;
27 import java.io.FileDescriptor;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.PrintWriter;
33 * This service exists only as a "dumpsys" target which reports
34 * statistics about the status of the disk.
36 public class DiskStatsService extends Binder {
37 private static final String TAG = "DiskStatsService";
39 private final Context mContext;
41 public DiskStatsService(Context context) {
46 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
47 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
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
53 File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp");
54 FileOutputStream fos = null;
55 IOException error = null;
57 long before = SystemClock.uptimeMillis();
59 fos = new FileOutputStream(tmp);
61 } catch (IOException e) {
64 try { if (fos != null) fos.close(); } catch (IOException e) {}
67 long after = SystemClock.uptimeMillis();
68 if (tmp.exists()) tmp.delete();
71 pw.print("Test-Error: ");
72 pw.println(error.toString());
74 pw.print("Latency: ");
75 pw.print(after - before);
76 pw.println("ms [512B Data Write]");
79 reportFreeSpace(Environment.getDataDirectory(), "Data", pw);
80 reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw);
81 reportFreeSpace(new File("/system"), "System", pw);
83 if (StorageManager.isFileEncryptedNativeOnly()) {
84 pw.println("File-based Encryption: true");
87 // TODO: Read /proc/yaffs and report interesting values;
88 // add configurable (through args) performance test parameters.
91 private void reportFreeSpace(File path, String name, PrintWriter pw) {
93 StatFs statfs = new StatFs(path.getPath());
94 long bsize = statfs.getBlockSize();
95 long avail = statfs.getAvailableBlocks();
96 long total = statfs.getBlockCount();
97 if (bsize <= 0 || total <= 0) {
98 throw new IllegalArgumentException(
99 "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
104 pw.print(avail * bsize / 1024);
106 pw.print(total * bsize / 1024);
107 pw.print("K total = ");
108 pw.print(avail * 100 / total);
109 pw.println("% free");
110 } catch (IllegalArgumentException e) {
112 pw.print("-Error: ");
113 pw.println(e.toString());