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

Commit 4e0d271b authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge "Add proto output of battery usage stats"

parents 2faf0508 db0d388e
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
@@ -448,6 +449,16 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
        return proto.getBytes();
    }

    /**
     * Writes contents in a binary protobuffer format, using
     * the android.os.BatteryUsageStatsAtomsProto proto.
     */
    public void dumpToProto(FileDescriptor fd) {
        final ProtoOutputStream proto = new ProtoOutputStream(fd);
        writeStatsProto(proto, /* max size */ Integer.MAX_VALUE);
        proto.flush();
    }

    @NonNull
    private void writeStatsProto(ProtoOutputStream proto, int maxRawSize) {
        final AggregateBatteryConsumer deviceBatteryConsumer = getAggregateBatteryConsumer(
+59 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.net.ConnectivityManager;
import android.net.INetworkManagementEventObserver;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.BatteryConsumer;
import android.os.BatteryManagerInternal;
import android.os.BatteryStats;
import android.os.BatteryStatsInternal;
@@ -2292,6 +2293,10 @@ public final class BatteryStatsService extends IBatteryStats.Stub
        pw.println("  --settings: dump the settings key/values related to batterystats");
        pw.println("  --cpu: dump cpu stats for debugging purpose");
        pw.println("  --power-profile: dump the power profile constants");
        pw.println("  --usage: write battery usage stats. Optional arguments:");
        pw.println("     --proto: output as a binary protobuffer");
        pw.println("     --model power-profile: use the power profile model"
                + " even if measured energy is available");
        pw.println("  <package.name>: optional name of package to filter output by.");
        pw.println("  -h: print this help text.");
        pw.println("Battery stats (batterystats) commands:");
@@ -2335,6 +2340,31 @@ public final class BatteryStatsService extends IBatteryStats.Stub
        }
    }

    private void dumpUsageStatsToProto(FileDescriptor fd, PrintWriter pw, int model,
            boolean proto) {
        awaitCompletion();
        syncStats("dump", BatteryExternalStatsWorker.UPDATE_ALL);

        BatteryUsageStatsQuery.Builder builder = new BatteryUsageStatsQuery.Builder()
                .setMaxStatsAgeMs(0)
                .includeProcessStateData()
                .includePowerModels();
        if (model == BatteryConsumer.POWER_MODEL_POWER_PROFILE) {
            builder.powerProfileModeledOnly();
        }
        BatteryUsageStatsQuery query = builder.build();
        synchronized (mStats) {
            mStats.prepareForDumpLocked();
            BatteryUsageStats batteryUsageStats =
                    mBatteryUsageStatsProvider.getBatteryUsageStats(query);
            if (proto) {
                batteryUsageStats.dumpToProto(fd);
            } else {
                batteryUsageStats.dump(pw, "");
            }
        }
    }

    private int doEnableOrDisable(PrintWriter pw, int i, String[] args, boolean enable) {
        i++;
        if (i >= args.length) {
@@ -2488,6 +2518,35 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                } else if ("--power-profile".equals(arg)) {
                    dumpPowerProfile(pw);
                    return;
                } else if ("--usage".equals(arg)) {
                    int model = BatteryConsumer.POWER_MODEL_UNDEFINED;
                    boolean proto = false;
                    for (int j = i + 1; j < args.length; j++) {
                        switch (args[j]) {
                            case "--proto":
                                proto = true;
                                break;
                            case "--model": {
                                if (j + 1 < args.length) {
                                    j++;
                                    if ("power-profile".equals(args[j])) {
                                        model = BatteryConsumer.POWER_MODEL_POWER_PROFILE;
                                    } else {
                                        pw.println("Unknown power model: " + args[j]);
                                        dumpHelp(pw);
                                        return;
                                    }
                                } else {
                                    pw.println("--model without a value");
                                    dumpHelp(pw);
                                    return;
                                }
                                break;
                            }
                        }
                    }
                    dumpUsageStatsToProto(fd, pw, model, proto);
                    return;
                } else if ("-a".equals(arg)) {
                    flags |= BatteryStats.DUMP_VERBOSE;
                } else if (arg.length() > 0 && arg.charAt(0) == '-'){