• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util.proto;
18 
19 import android.util.AggStats;
20 import android.util.Duration;
21 
22 import java.io.IOException;
23 
24 /**
25  * This class contains a list of helper functions to write common proto in
26  * //frameworks/base/core/proto/android/base directory
27  */
28 public class ProtoUtils {
29 
30     /**
31      * Dump AggStats to ProtoOutputStream
32      * @hide
33      */
toAggStatsProto(ProtoOutputStream proto, long fieldId, long min, long average, long max)34     public static void toAggStatsProto(ProtoOutputStream proto, long fieldId,
35             long min, long average, long max) {
36         final long aggStatsToken = proto.start(fieldId);
37         proto.write(AggStats.MIN, min);
38         proto.write(AggStats.AVERAGE, average);
39         proto.write(AggStats.MAX, max);
40         proto.end(aggStatsToken);
41     }
42 
43     /**
44      * Dump Duration to ProtoOutputStream
45      * @hide
46      */
toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs)47     public static void toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs) {
48         final long token = proto.start(fieldId);
49         proto.write(Duration.START_MS, startMs);
50         proto.write(Duration.END_MS, endMs);
51         proto.end(token);
52     }
53 
54     /**
55      * Helper function to write bit-wise flags to proto as repeated enums
56      * @hide
57      */
writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId, int flags, int[] origEnums, int[] protoEnums)58     public static void writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId,
59             int flags, int[] origEnums, int[] protoEnums) {
60         if (protoEnums.length != origEnums.length) {
61             throw new IllegalArgumentException("The length of origEnums must match protoEnums");
62         }
63         int len = origEnums.length;
64         for (int i = 0; i < len; i++) {
65             // handle zero flag case.
66             if (origEnums[i] == 0 && flags == 0) {
67                 proto.write(fieldId, protoEnums[i]);
68                 return;
69             }
70             if ((flags & origEnums[i]) != 0) {
71                 proto.write(fieldId, protoEnums[i]);
72             }
73         }
74     }
75 
76     /**
77      * Provide debug data about the current field as a string
78      */
currentFieldToString(ProtoInputStream proto)79     public static String currentFieldToString(ProtoInputStream proto) throws IOException {
80         StringBuilder sb = new StringBuilder();
81 
82         final int fieldNumber = proto.getFieldNumber();
83         final int wireType = proto.getWireType();
84         long fieldConstant;
85 
86         sb.append("Offset : 0x" + Integer.toHexString(proto.getOffset()));
87         sb.append("\nField Number : 0x" + Integer.toHexString(proto.getFieldNumber()));
88         sb.append("\nWire Type : ");
89         switch (wireType) {
90             case ProtoStream.WIRE_TYPE_VARINT:
91                 sb.append("varint");
92                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
93                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_INT64);
94                 sb.append("\nField Value : 0x" + Long.toHexString(proto.readLong(fieldConstant)));
95                 break;
96             case ProtoStream.WIRE_TYPE_FIXED64:
97                 sb.append("fixed64");
98                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
99                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED64);
100                 sb.append("\nField Value : 0x" + Long.toHexString(proto.readLong(fieldConstant)));
101                 break;
102             case ProtoStream.WIRE_TYPE_LENGTH_DELIMITED:
103                 sb.append("length delimited");
104                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
105                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_BYTES);
106                 sb.append("\nField Bytes : " + proto.readBytes(fieldConstant));
107                 break;
108             case ProtoStream.WIRE_TYPE_START_GROUP:
109                 sb.append("start group");
110                 break;
111             case ProtoStream.WIRE_TYPE_END_GROUP:
112                 sb.append("end group");
113                 break;
114             case ProtoStream.WIRE_TYPE_FIXED32:
115                 sb.append("fixed32");
116                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
117                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED32);
118                 sb.append("\nField Value : 0x" + Integer.toHexString(proto.readInt(fieldConstant)));
119                 break;
120             default:
121                 sb.append("unknown(" + proto.getWireType() + ")");
122         }
123         return sb.toString();
124     }
125 }
126