blob: 8ca675a904c41e459364f898c01944741a0820f6 [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;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070024import android.os.storage.StorageManager;
Dan Egnor621bc542010-03-25 16:20:14 -070025
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 {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070037 private static final String TAG = "DiskStatsService";
38
Dan Egnor621bc542010-03-25 16:20:14 -070039 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) {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070047 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
Dan Egnor621bc542010-03-25 16:20:14 -070048
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
Paul Lawrence20be5d62016-02-26 13:51:17 -080083 if (StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070084 pw.println("File-based Encryption: true");
85 }
86
Dan Egnor621bc542010-03-25 16:20:14 -070087 // 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}