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

Commit d01ec8fe authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add recent disk write speed metrics to Disk Stats"

parents c2393e12 3e20a105
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ message DiskStatsServiceDumpProto {
    optional EncryptionType encryption = 5;
    // Cached values of folder sizes, etc.
    optional DiskStatsCachedValuesProto cached_folder_sizes = 6;
    // Average write speed of storaged benchmark for last 24 hours
    optional int32 benchmarked_write_speed_kbps = 7;
}

message DiskStatsCachedValuesProto {
+47 −0
Original line number Diff line number Diff line
@@ -19,6 +19,10 @@ package com.android.server;
import android.content.Context;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.os.IStoraged;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.StatFs;
import android.os.SystemClock;
import android.os.storage.StorageManager;
@@ -109,6 +113,12 @@ public class DiskStatsService extends Binder {
            }
        }

        if (protoFormat) {
            reportDiskWriteSpeedProto(proto);
        } else {
            reportDiskWriteSpeed(pw);
        }

        reportFreeSpace(Environment.getDataDirectory(), "Data", pw, proto,
                DiskStatsFreeSpaceProto.FOLDER_DATA);
        reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw, proto,
@@ -285,4 +295,41 @@ public class DiskStatsService extends Binder {
            Log.w(TAG, "exception reading diskstats cache file", e);
        }
    }

    private int getRecentPerf() throws RemoteException, IllegalStateException {
        IBinder binder = ServiceManager.getService("storaged");
        if (binder == null) throw new IllegalStateException("storaged not found");
        IStoraged storaged = IStoraged.Stub.asInterface(binder);
        return storaged.getRecentPerf();
    }

    // Keep reportDiskWriteSpeed and reportDiskWriteSpeedProto in sync
    private void reportDiskWriteSpeed(PrintWriter pw) {
        try {
            long perf = getRecentPerf();
            if (perf != 0) {
                pw.print("Recent Disk Write Speed (kB/s) = ");
                pw.println(perf);
            } else {
                pw.println("Recent Disk Write Speed data unavailable");
                Log.w(TAG, "Recent Disk Write Speed data unavailable!");
            }
        } catch (RemoteException | IllegalStateException e) {
            pw.println(e.toString());
            Log.e(TAG, e.toString());
        }
    }

    private void reportDiskWriteSpeedProto(ProtoOutputStream proto) {
        try {
            long perf = getRecentPerf();
            if (perf != 0) {
                proto.write(DiskStatsServiceDumpProto.BENCHMARKED_WRITE_SPEED_KBPS, perf);
            } else {
                Log.w(TAG, "Recent Disk Write Speed data unavailable!");
            }
        } catch (RemoteException | IllegalStateException e) {
            Log.e(TAG, e.toString());
        }
    }
}