Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 com.android.server.powerstats; |
| 18 | |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | /** |
| 23 | * This class implements a utility to parse ODPM data out |
| 24 | * of incident reports contained in bugreports. The data |
| 25 | * is output to STDOUT in csv format. |
| 26 | */ |
| 27 | public class PowerStatsServiceProtoParser { |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 28 | private static void printEnergyMeterInfo(PowerStatsServiceMeterProto proto) { |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 29 | String csvHeader = new String(); |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 30 | for (int i = 0; i < proto.getChannelInfoCount(); i++) { |
| 31 | ChannelInfoProto energyMeterInfo = proto.getChannelInfo(i); |
| 32 | csvHeader += "Index,Timestamp," + energyMeterInfo.getChannelId() |
| 33 | + "/" + energyMeterInfo.getChannelName() + ","; |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 34 | } |
| 35 | System.out.println(csvHeader); |
| 36 | } |
| 37 | |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 38 | private static void printEnergyMeasurements(PowerStatsServiceMeterProto proto) { |
| 39 | int energyMeterInfoCount = proto.getChannelInfoCount(); |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 40 | |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 41 | if (energyMeterInfoCount > 0) { |
| 42 | int energyMeasurementCount = proto.getEnergyMeasurementCount(); |
| 43 | int energyMeasurementSetCount = energyMeasurementCount / energyMeterInfoCount; |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 44 | |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 45 | for (int i = 0; i < energyMeasurementSetCount; i++) { |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 46 | String csvRow = new String(); |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 47 | for (int j = 0; j < energyMeterInfoCount; j++) { |
| 48 | EnergyMeasurementProto energyMeasurement = |
| 49 | proto.getEnergyMeasurement(i * energyMeterInfoCount + j); |
| 50 | csvRow += energyMeasurement.getChannelId() + "," |
| 51 | + energyMeasurement.getTimestampMs() + "," |
| 52 | + energyMeasurement.getEnergyUws() + ","; |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 53 | } |
| 54 | System.out.println(csvRow); |
| 55 | } |
| 56 | } else { |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 57 | System.out.println("Error: energyMeterInfoCount is zero"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static void printEnergyConsumerId(PowerStatsServiceModelProto proto) { |
| 62 | String csvHeader = new String(); |
| 63 | for (int i = 0; i < proto.getEnergyConsumerIdCount(); i++) { |
| 64 | EnergyConsumerIdProto energyConsumerId = proto.getEnergyConsumerId(i); |
| 65 | csvHeader += "Index,Timestamp," + energyConsumerId.getEnergyConsumerId() + ","; |
| 66 | } |
| 67 | System.out.println(csvHeader); |
| 68 | } |
| 69 | |
| 70 | private static void printEnergyConsumerResults(PowerStatsServiceModelProto proto) { |
| 71 | int energyConsumerIdCount = proto.getEnergyConsumerIdCount(); |
| 72 | |
| 73 | if (energyConsumerIdCount > 0) { |
| 74 | int energyConsumerResultCount = proto.getEnergyConsumerResultCount(); |
| 75 | int energyConsumerResultSetCount = energyConsumerResultCount / energyConsumerIdCount; |
| 76 | |
| 77 | for (int i = 0; i < energyConsumerResultSetCount; i++) { |
| 78 | String csvRow = new String(); |
| 79 | for (int j = 0; j < energyConsumerIdCount; j++) { |
| 80 | EnergyConsumerResultProto energyConsumerResult = |
| 81 | proto.getEnergyConsumerResult(i * energyConsumerIdCount + j); |
| 82 | csvRow += energyConsumerResult.getEnergyConsumerId() + "," |
| 83 | + energyConsumerResult.getTimestampMs() + "," |
| 84 | + energyConsumerResult.getEnergyUws() + ","; |
| 85 | } |
| 86 | System.out.println(csvRow); |
| 87 | } |
| 88 | } else { |
| 89 | System.out.println("Error: energyConsumerIdCount is zero"); |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Mat Bevilacqua | 723784a | 2020-12-22 09:57:01 -0800 | [diff] [blame] | 93 | private static void printPowerEntityInfo(PowerStatsServiceResidencyProto proto) { |
| 94 | String csvHeader = new String(); |
| 95 | for (int i = 0; i < proto.getPowerEntityInfoCount(); i++) { |
| 96 | PowerEntityInfoProto powerEntityInfo = proto.getPowerEntityInfo(i); |
| 97 | csvHeader += powerEntityInfo.getPowerEntityId() + "," |
| 98 | + powerEntityInfo.getPowerEntityName() + ","; |
| 99 | for (int j = 0; j < powerEntityInfo.getStatesCount(); j++) { |
| 100 | StateInfoProto stateInfo = powerEntityInfo.getStates(j); |
| 101 | csvHeader += stateInfo.getStateId() + "," + stateInfo.getStateName() + ","; |
| 102 | } |
| 103 | } |
| 104 | System.out.println(csvHeader); |
| 105 | } |
| 106 | |
| 107 | private static void printStateResidencyResult(PowerStatsServiceResidencyProto proto) { |
| 108 | for (int i = 0; i < proto.getStateResidencyResultCount(); i++) { |
| 109 | String csvRow = new String(); |
| 110 | |
| 111 | StateResidencyResultProto stateResidencyResult = proto.getStateResidencyResult(i); |
| 112 | csvRow += stateResidencyResult.getPowerEntityId() + ","; |
| 113 | |
| 114 | for (int j = 0; j < stateResidencyResult.getStateResidencyDataCount(); j++) { |
| 115 | StateResidencyProto stateResidency = stateResidencyResult.getStateResidencyData(j); |
| 116 | csvRow += stateResidency.getStateId() + "," |
| 117 | + stateResidency.getTotalTimeInStateMs() + "," |
| 118 | + stateResidency.getTotalStateEntryCount() + "," |
| 119 | + stateResidency.getLastEntryTimestampMs() + ","; |
| 120 | } |
| 121 | System.out.println(csvRow); |
| 122 | } |
| 123 | } |
| 124 | |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 125 | private static void generateCsvFile(String pathToIncidentReport) { |
| 126 | try { |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 127 | // Print power meter data. |
| 128 | IncidentReportMeterProto irMeterProto = |
| 129 | IncidentReportMeterProto.parseFrom(new FileInputStream(pathToIncidentReport)); |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 130 | |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 131 | if (irMeterProto.hasIncidentReport()) { |
| 132 | PowerStatsServiceMeterProto pssMeterProto = irMeterProto.getIncidentReport(); |
| 133 | printEnergyMeterInfo(pssMeterProto); |
| 134 | printEnergyMeasurements(pssMeterProto); |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 135 | } else { |
Mat Bevilacqua | a375ea0 | 2020-09-21 18:59:06 -0700 | [diff] [blame] | 136 | System.out.println("Meter incident report not found. Exiting."); |
| 137 | } |
| 138 | |
| 139 | // Print power model data. |
| 140 | IncidentReportModelProto irModelProto = |
| 141 | IncidentReportModelProto.parseFrom(new FileInputStream(pathToIncidentReport)); |
| 142 | |
| 143 | if (irModelProto.hasIncidentReport()) { |
| 144 | PowerStatsServiceModelProto pssModelProto = irModelProto.getIncidentReport(); |
| 145 | printEnergyConsumerId(pssModelProto); |
| 146 | printEnergyConsumerResults(pssModelProto); |
| 147 | } else { |
| 148 | System.out.println("Model incident report not found. Exiting."); |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 149 | } |
Mat Bevilacqua | 723784a | 2020-12-22 09:57:01 -0800 | [diff] [blame] | 150 | |
| 151 | // Print state residency data. |
| 152 | IncidentReportResidencyProto irResidencyProto = |
| 153 | IncidentReportResidencyProto.parseFrom( |
| 154 | new FileInputStream(pathToIncidentReport)); |
| 155 | |
| 156 | if (irResidencyProto.hasIncidentReport()) { |
| 157 | PowerStatsServiceResidencyProto pssResidencyProto = |
| 158 | irResidencyProto.getIncidentReport(); |
| 159 | printPowerEntityInfo(pssResidencyProto); |
| 160 | printStateResidencyResult(pssResidencyProto); |
| 161 | } else { |
| 162 | System.out.println("Residency incident report not found. Exiting."); |
| 163 | } |
| 164 | |
Mat Bevilacqua | 8160121 | 2020-07-30 17:26:45 -0700 | [diff] [blame] | 165 | } catch (IOException e) { |
| 166 | System.out.println("Unable to open incident report file: " + pathToIncidentReport); |
| 167 | System.out.println(e); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This is the entry point to parse the ODPM data out of incident reports. |
| 173 | * It requires one argument which is the path to the incident_report.proto |
| 174 | * file captured in a bugreport. |
| 175 | * |
| 176 | * @param args Path to incident_report.proto passed in from command line. |
| 177 | */ |
| 178 | public static void main(String[] args) { |
| 179 | if (args.length > 0) { |
| 180 | generateCsvFile(args[0]); |
| 181 | } else { |
| 182 | System.err.println("Usage: PowerStatsServiceProtoParser <incident_report.proto>"); |
| 183 | System.err.println("Missing path to incident_report.proto. Exiting."); |
| 184 | System.exit(1); |
| 185 | } |
| 186 | } |
| 187 | } |