Add "dumpsys diskstats" to get a dump of disk-free values.

Also includes a trivial test of I/O latency.  (Mostly useful to be invoked
from checkin to poll disk-free and latency issues.  Also moderately useful
to see in a bugreport.)

Also make "dumpsys netstat" a non-no-op, in case we want this data.

Change-Id: Ia93550a23ca6b35586c5d9217890ee0a5801aae1
diff --git a/services/java/com/android/server/DiskStatsService.java b/services/java/com/android/server/DiskStatsService.java
new file mode 100644
index 0000000..8ef974a
--- /dev/null
+++ b/services/java/com/android/server/DiskStatsService.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server;
+
+import android.content.Context;
+import android.os.Binder;
+import android.os.Environment;
+import android.os.FileUtils;
+import android.os.StatFs;
+import android.os.SystemClock;
+
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * This service exists only as a "dumpsys" target which reports
+ * statistics about the status of the disk.
+ */
+public class DiskStatsService extends Binder {
+    private final Context mContext;
+
+    public DiskStatsService(Context context) {
+        mContext = context;
+    }
+
+    @Override
+    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        // This data is accessible to any app -- no permission check needed.
+
+        // Run a quick-and-dirty performance test: write 512 bytes
+        byte[] junk = new byte[512];
+        for (int i = 0; i < junk.length; i++) junk[i] = (byte) i;  // Write nonzero bytes
+
+        File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp");
+        FileOutputStream fos = null;
+        IOException error = null;
+
+        long before = SystemClock.uptimeMillis();
+        try {
+            fos = new FileOutputStream(tmp);
+            fos.write(junk);
+        } catch (IOException e) {
+            error = e;
+        } finally {
+            try { if (fos != null) fos.close(); } catch (IOException e) {}
+        }
+
+        long after = SystemClock.uptimeMillis();
+        if (tmp.exists()) tmp.delete();
+
+        if (error != null) {
+            pw.print("Test-Error: ");
+            pw.println(error.toString());
+        } else {
+            pw.print("Latency: ");
+            pw.print(after - before);
+            pw.println("ms [512B Data Write]");
+        }
+
+        reportFreeSpace(Environment.getDataDirectory(), "Data", pw);
+        reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw);
+        reportFreeSpace(new File("/system"), "System", pw);
+
+        // TODO: Read /proc/yaffs and report interesting values;
+        // add configurable (through args) performance test parameters.
+    }
+
+    private void reportFreeSpace(File path, String name, PrintWriter pw) {
+        try {
+            StatFs statfs = new StatFs(path.getPath());
+            long bsize = statfs.getBlockSize();
+            long avail = statfs.getAvailableBlocks();
+            long total = statfs.getBlockCount();
+            if (bsize <= 0 || total <= 0) {
+                throw new IllegalArgumentException(
+                        "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
+            }
+
+            pw.print(name);
+            pw.print("-Free: ");
+            pw.print(avail * bsize / 1024);
+            pw.print("K / ");
+            pw.print(total * bsize / 1024);
+            pw.print("K total = ");
+            pw.print(avail * 100 / total);
+            pw.println("% free");
+        } catch (IllegalArgumentException e) {
+            pw.print(name);
+            pw.print("-Error: ");
+            pw.println(e.toString());
+            return;
+        }
+    }
+}