【Android】dumpsys --proto简介

dumpsys --proto

 

2018-11-9

在Android 8.0开始,在有些dump方法中添加了—proto的处理,如batteryService中

private final class BinderService extends Binder {

        @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {

            if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;

 

            if (args.length > 0 && "--proto".equals(args[0])) {

                dumpProto(fd);

            } else {

                dumpInternal(fd, pw, args);

            }

        }

 

查看dumpProto的实现

    private void dumpProto(FileDescriptor fd) {

        final ProtoOutputStream proto = new ProtoOutputStream(fd);

 

        synchronized (mLock) {

            proto.write(BatteryServiceDumpProto.ARE_UPDATES_STOPPED, mUpdatesStopped);

            int batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_NONE;

            if (mBatteryProps.chargerAcOnline) {

                batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_AC;

            } else if (mBatteryProps.chargerUsbOnline) {

                batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_USB;

            } else if (mBatteryProps.chargerWirelessOnline) {

                batteryPluggedValue = BatteryServiceDumpProto.BATTERY_PLUGGED_WIRELESS;

            }

            proto.write(BatteryServiceDumpProto.PLUGGED, batteryPluggedValue);

            proto.write(BatteryServiceDumpProto.MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);

            proto.write(BatteryServiceDumpProto.MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);

            proto.write(BatteryServiceDumpProto.CHARGE_COUNTER, mBatteryProps.batteryChargeCounter);

            proto.write(BatteryServiceDumpProto.STATUS, mBatteryProps.batteryStatus);

            proto.write(BatteryServiceDumpProto.HEALTH, mBatteryProps.batteryHealth);

            proto.write(BatteryServiceDumpProto.IS_PRESENT, mBatteryProps.batteryPresent);

            proto.write(BatteryServiceDumpProto.LEVEL, mBatteryProps.batteryLevel);

            proto.write(BatteryServiceDumpProto.SCALE, BATTERY_SCALE);

            proto.write(BatteryServiceDumpProto.VOLTAGE, mBatteryProps.batteryVoltage);

            proto.write(BatteryServiceDumpProto.TEMPERATURE, mBatteryProps.batteryTemperature);

            proto.write(BatteryServiceDumpProto.TECHNOLOGY, mBatteryProps.batteryTechnology);

        }

        proto.flush();

    }

 

查看其修改记录,是为cts测试添加的

Protobufferize BatteryManager dumpsys

Bug: 34227623
Test: cts-tradefed run cts-dev -t CtsIncidentHostTestCases
Change-Id: I6e36d725c50105ad1f6f86f890db49ea253907c0

 

cts/hostsidetests/incident/src/com/android/server/cts/BatteryIncidentTest.java 中,有

 

25
26    public void testBatteryServiceDump() throws Exception {
27        final BatteryServiceDumpProto dump =
28                getDump(BatteryServiceDumpProto.parser(), "dumpsys battery --proto");
29
30        if (!dump.getIsPresent()) {
31            /* If the battery isn't present, no need to run this test. */
32            return;
33        }
34
39
40        assertTrue(
41                dump.getPlugged()
42                        != BatteryServiceDumpProto.BatteryPlugged.BATTERY_PLUGGED_WIRELESS);
43        assertTrue(dump.getChargeCounter() > 0);
44 

 

 

getDump的实现为

cts/hostsidetests/incident/src/com/android/server/cts/ProtoDumpTestCase.java

 

76    public <T extends MessageLite> T getDump(Parser<T> parser, String command)
77            throws DeviceNotAvailableException, InvalidProtocolBufferException {
78        final CollectingByteOutputReceiver receiver = new CollectingByteOutputReceiver();
79        getDevice().executeShellCommand(command, receiver);
80        return parser.parseFrom(receiver.getOutput());
81    }

 

可以看出,通过这种Proto存取数据的方式,获取到batteryService中的变量值,用来进行后续的处理。

 

BatteryServiceDumpProto的定义在

frameworks/base/core/proto/android/service/battery.proto, 这是proto类型的文件,编译的时候会生成对应的java文件

out$ find . -name BatteryServiceDumpProto.java

./target/common/obj/JAVA_LIBRARIES/framework_intermediates/proto/src/android/service/battery/BatteryServiceDumpProto.java

 

// Generated by protoc-gen-javastream. DO NOT MODIFY.

// source: frameworks/base/core/proto/android/service/battery.proto

 

package android.service.battery;

 

/** @hide */

// message BatteryServiceDumpProto

public final class BatteryServiceDumpProto {

 

    // enum BatteryPlugged

    public static final int BATTERY_PLUGGED_NONE = 0;

    public static final int BATTERY_PLUGGED_AC = 1;

    public static final int BATTERY_PLUGGED_USB = 2;

    public static final int BATTERY_PLUGGED_WIRELESS = 4;

 

    // enum BatteryStatus

    public static final int BATTERY_STATUS_INVALID = 0;

    public static final int BATTERY_STATUS_UNKNOWN = 1;

    public static final int BATTERY_STATUS_CHARGING = 2;

    public static final int BATTERY_STATUS_DISCHARGING = 3;

    public static final int BATTERY_STATUS_NOT_CHARGING = 4;

    public static final int BATTERY_STATUS_FULL = 5;

 

    // enum BatteryHealth

    public static final int BATTERY_HEALTH_INVALID = 0;

    public static final int BATTERY_HEALTH_UNKNOWN = 1;

    public static final int BATTERY_HEALTH_GOOD = 2;

    public static final int BATTERY_HEALTH_OVERHEAT = 3;

    public static final int BATTERY_HEALTH_DEAD = 4;

    public static final int BATTERY_HEALTH_OVER_VOLTAGE = 5;

    public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6;

    public static final int BATTERY_HEALTH_COLD = 7;

 

    // optional bool are_updates_stopped = 1;

    public static final long ARE_UPDATES_STOPPED = 0x0000010d00000001L;

 

    // optional .android.service.battery.BatteryServiceDumpProto.BatteryPlugged plugged = 2;

    public static final long PLUGGED = 0x0000011000000002L;

 

    // optional int32 max_charging_current = 3;

    public static final long MAX_CHARGING_CURRENT = 0x0000010300000003L;

 

    // optional int32 max_charging_voltage = 4;

    public static final long MAX_CHARGING_VOLTAGE = 0x0000010300000004L;

 

    // optional int32 charge_counter = 5;

    public static final long CHARGE_COUNTER = 0x0000010300000005L;

 

    // optional .android.service.battery.BatteryServiceDumpProto.BatteryStatus status = 6;

    public static final long STATUS = 0x0000011000000006L;

 

    // optional .android.service.battery.BatteryServiceDumpProto.BatteryHealth health = 7;

    public static final long HEALTH = 0x0000011000000007L;

 

    // optional bool is_present = 8;

    public static final long IS_PRESENT = 0x0000010d00000008L;

 

    // optional int32 level = 9;

    public static final long LEVEL = 0x0000010300000009L;

 

    // optional int32 scale = 10;

    public static final long SCALE = 0x000001030000000aL;

 

    // optional int32 voltage = 11;

    public static final long VOLTAGE = 0x000001030000000bL;

 

    // optional int32 temperature = 12;

    public static final long TEMPERATURE = 0x000001030000000cL;

 

    // optional string technology = 13;

    public static final long TECHNOLOGY = 0x0000010e0000000dL;

 

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值