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

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

Merge "Add proto output of battery usage stats" into tm-qpr-dev

parents a84c7a7b eb91d0c6
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -34,6 +34,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;
@@ -449,6 +450,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 BatteryConsumer deviceBatteryConsumer = getAggregateBatteryConsumer(
+59 −0
Original line number Diff line number Diff line
@@ -36,6 +36,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;
@@ -2282,6 +2283,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:");
@@ -2325,6 +2330,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) {
@@ -2478,6 +2508,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) == '-'){