/services/java/com/android/server/DiskStatsService.java

https://github.com/aizuzi/platform_frameworks_base · Java · 113 lines · 75 code · 16 blank · 22 comment · 9 complexity · e7c6b76c574bddd779154682897eb4b1 MD5 · raw file

  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. package com.android.server;
  17. import android.content.Context;
  18. import android.os.Binder;
  19. import android.os.Environment;
  20. import android.os.FileUtils;
  21. import android.os.StatFs;
  22. import android.os.SystemClock;
  23. import java.io.File;
  24. import java.io.FileDescriptor;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.PrintWriter;
  28. /**
  29. * This service exists only as a "dumpsys" target which reports
  30. * statistics about the status of the disk.
  31. */
  32. public class DiskStatsService extends Binder {
  33. private static final String TAG = "DiskStatsService";
  34. private final Context mContext;
  35. public DiskStatsService(Context context) {
  36. mContext = context;
  37. }
  38. @Override
  39. protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
  40. mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
  41. // Run a quick-and-dirty performance test: write 512 bytes
  42. byte[] junk = new byte[512];
  43. for (int i = 0; i < junk.length; i++) junk[i] = (byte) i; // Write nonzero bytes
  44. File tmp = new File(Environment.getDataDirectory(), "system/perftest.tmp");
  45. FileOutputStream fos = null;
  46. IOException error = null;
  47. long before = SystemClock.uptimeMillis();
  48. try {
  49. fos = new FileOutputStream(tmp);
  50. fos.write(junk);
  51. } catch (IOException e) {
  52. error = e;
  53. } finally {
  54. try { if (fos != null) fos.close(); } catch (IOException e) {}
  55. }
  56. long after = SystemClock.uptimeMillis();
  57. if (tmp.exists()) tmp.delete();
  58. if (error != null) {
  59. pw.print("Test-Error: ");
  60. pw.println(error.toString());
  61. } else {
  62. pw.print("Latency: ");
  63. pw.print(after - before);
  64. pw.println("ms [512B Data Write]");
  65. }
  66. reportFreeSpace(Environment.getDataDirectory(), "Data", pw);
  67. reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw);
  68. reportFreeSpace(new File("/system"), "System", pw);
  69. // TODO: Read /proc/yaffs and report interesting values;
  70. // add configurable (through args) performance test parameters.
  71. }
  72. private void reportFreeSpace(File path, String name, PrintWriter pw) {
  73. try {
  74. StatFs statfs = new StatFs(path.getPath());
  75. long bsize = statfs.getBlockSize();
  76. long avail = statfs.getAvailableBlocks();
  77. long total = statfs.getBlockCount();
  78. if (bsize <= 0 || total <= 0) {
  79. throw new IllegalArgumentException(
  80. "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
  81. }
  82. pw.print(name);
  83. pw.print("-Free: ");
  84. pw.print(avail * bsize / 1024);
  85. pw.print("K / ");
  86. pw.print(total * bsize / 1024);
  87. pw.print("K total = ");
  88. pw.print(avail * 100 / total);
  89. pw.println("% free");
  90. } catch (IllegalArgumentException e) {
  91. pw.print(name);
  92. pw.print("-Error: ");
  93. pw.println(e.toString());
  94. return;
  95. }
  96. }
  97. }