• 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  * @hide
28  */
29 public class ProtoUtils {
30 
31     /**
32      * Dump AggStats to ProtoOutputStream
33      */
toAggStatsProto(ProtoOutputStream proto, long fieldId, long min, long average, long max, int meanKb, int maxKb)34     public static void toAggStatsProto(ProtoOutputStream proto, long fieldId,
35             long min, long average, long max, int meanKb, int maxKb) {
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.write(AggStats.MEAN_KB, meanKb);
41         proto.write(AggStats.MAX_KB, maxKb);
42         proto.end(aggStatsToken);
43     }
44 
45     /**
46      * Dump AggStats to ProtoOutputStream
47      */
toAggStatsProto(ProtoOutputStream proto, long fieldId, long min, long average, long max)48     public static void toAggStatsProto(ProtoOutputStream proto, long fieldId,
49             long min, long average, long max) {
50         toAggStatsProto(proto, fieldId, min, average, max, 0, 0);
51     }
52 
53     /**
54      * Dump Duration to ProtoOutputStream
55      */
toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs)56     public static void toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs) {
57         final long token = proto.start(fieldId);
58         proto.write(Duration.START_MS, startMs);
59         proto.write(Duration.END_MS, endMs);
60         proto.end(token);
61     }
62 
63     /**
64      * Helper function to write bit-wise flags to proto as repeated enums
65      */
writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId, int flags, int[] origEnums, int[] protoEnums)66     public static void writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId,
67             int flags, int[] origEnums, int[] protoEnums) {
68         if (protoEnums.length != origEnums.length) {
69             throw new IllegalArgumentException("The length of origEnums must match protoEnums");
70         }
71         int len = origEnums.length;
72         for (int i = 0; i < len; i++) {
73             // handle zero flag case.
74             if (origEnums[i] == 0 && flags == 0) {
75                 proto.write(fieldId, protoEnums[i]);
76                 return;
77             }
78             if ((flags & origEnums[i]) != 0) {
79                 proto.write(fieldId, protoEnums[i]);
80             }
81         }
82     }
83 
84     /**
85      * Provide debug data about the current field as a string
86      */
currentFieldToString(ProtoInputStream proto)87     public static String currentFieldToString(ProtoInputStream proto) throws IOException {
88         StringBuilder sb = new StringBuilder();
89 
90         final int fieldNumber = proto.getFieldNumber();
91         final int wireType = proto.getWireType();
92         long fieldConstant;
93 
94         sb.append("Offset : 0x" + Integer.toHexString(proto.getOffset()));
95         sb.append("\nField Number : 0x" + Integer.toHexString(proto.getFieldNumber()));
96         sb.append("\nWire Type : ");
97         switch (wireType) {
98             case ProtoStream.WIRE_TYPE_VARINT:
99                 sb.append("varint");
100                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
101                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_INT64);
102                 sb.append("\nField Value : 0x" + Long.toHexString(proto.readLong(fieldConstant)));
103                 break;
104             case ProtoStream.WIRE_TYPE_FIXED64:
105                 sb.append("fixed64");
106                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
107                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED64);
108                 sb.append("\nField Value : 0x" + Long.toHexString(proto.readLong(fieldConstant)));
109                 break;
110             case ProtoStream.WIRE_TYPE_LENGTH_DELIMITED:
111                 sb.append("length delimited");
112                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
113                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_BYTES);
114                 sb.append("\nField Bytes : " + proto.readBytes(fieldConstant));
115                 break;
116             case ProtoStream.WIRE_TYPE_START_GROUP:
117                 sb.append("start group");
118                 break;
119             case ProtoStream.WIRE_TYPE_END_GROUP:
120                 sb.append("end group");
121                 break;
122             case ProtoStream.WIRE_TYPE_FIXED32:
123                 sb.append("fixed32");
124                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
125                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED32);
126                 sb.append("\nField Value : 0x" + Integer.toHexString(proto.readInt(fieldConstant)));
127                 break;
128             default:
129                 sb.append("unknown(" + proto.getWireType() + ")");
130         }
131         return sb.toString();
132     }
133 }
134