OSDN Git Service

Merge \\\\\\\\\"DO NOT MERGE revert public api loadSafeLabel\\\\\\\\\" into lmp-dev...
[android-x86/frameworks-base.git] / services / core / java / com / android / server / DiskStatsService.java
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.StatFs;
23 import android.os.SystemClock;
24 import android.os.storage.StorageManager;
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 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         if (StorageManager.isFileEncryptedNativeOnly()) {
84             pw.println("File-based Encryption: true");
85         }
86
87         // TODO: Read /proc/yaffs and report interesting values;
88         // add configurable (through args) performance test parameters.
89     }
90
91     private void reportFreeSpace(File path, String name, PrintWriter pw) {
92         try {
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);
100             }
101
102             pw.print(name);
103             pw.print("-Free: ");
104             pw.print(avail * bsize / 1024);
105             pw.print("K / ");
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) {
111             pw.print(name);
112             pw.print("-Error: ");
113             pw.println(e.toString());
114             return;
115         }
116     }
117 }