Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 92ba57f6 authored by Dan Egnor's avatar Dan Egnor Committed by Android (Google) Code Review
Browse files

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

parents dd654316 621bc545
Loading
Loading
Loading
Loading
+111 −0
Original line number Diff line number Diff line
/*
 * 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;
        }
    }
}
+37 −1
Original line number Diff line number Diff line
@@ -19,11 +19,16 @@ package com.android.server;
import android.content.Context;
import android.net.TrafficStats;
import android.os.INetStatService;
import android.os.SystemClock;

import java.io.FileDescriptor;
import java.io.PrintWriter;

public class NetStatService extends INetStatService.Stub {
    private final Context mContext;

    public NetStatService(Context context) {

        mContext = context;
    }

    public long getMobileTxPackets() {
@@ -57,4 +62,35 @@ public class NetStatService extends INetStatService.Stub {
    public long getTotalRxBytes() {
        return TrafficStats.getTotalRxBytes();
    }

    @Override
    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        // This data is accessible to any app -- no permission check needed.

        pw.print("Elapsed: total=");
        pw.print(SystemClock.elapsedRealtime());
        pw.print("ms awake=");
        pw.print(SystemClock.uptimeMillis());
        pw.println("ms");

        pw.print("Mobile: Tx=");
        pw.print(getMobileTxBytes());
        pw.print("B/");
        pw.print(getMobileTxPackets());
        pw.print("Pkts Rx=");
        pw.print(getMobileRxBytes());
        pw.print("B/");
        pw.print(getMobileRxPackets());
        pw.println("Pkts");

        pw.print("Total: Tx=");
        pw.print(getTotalTxBytes());
        pw.print("B/");
        pw.print(getTotalTxPackets());
        pw.print("Pkts Rx=");
        pw.print(getTotalRxBytes());
        pw.print("B/");
        pw.print(getTotalRxPackets());
        pw.println("Pkts");
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -399,6 +399,13 @@ class ServerThread extends Thread {
            } catch (Throwable e) {
                Slog.e(TAG, "Failure installing status bar icons", e);
            }

            try {
                Slog.i(TAG, "DiskStats Service");
                ServiceManager.addService("diskstats", new DiskStatsService(context));
            } catch (Throwable e) {
                Slog.e(TAG, "Failure starting DiskStats Service", e);
            }
        }

        // make sure the ADB_ENABLED setting value matches the secure property value