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