blob: bc12fc5c44e08ee4a221378890b1665cd3fe3ff2 [file] [log] [blame]
Dan Egnor621bc542010-03-25 16:20:14 -07001/*
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;
Dan Egnor621bc542010-03-25 16:20:14 -070022import android.os.StatFs;
23import android.os.SystemClock;
24
25import java.io.File;
26import java.io.FileDescriptor;
27import java.io.FileOutputStream;
28import java.io.IOException;
29import 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 */
35public class DiskStatsService extends Binder {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070036 private static final String TAG = "DiskStatsService";
37
Dan Egnor621bc542010-03-25 16:20:14 -070038 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) {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070046 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Dan Egnor621bc542010-03-25 16:20:14 -070047
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}