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 { |
| 28 | private static void printRailInfo(PowerStatsServiceProto proto) { |
| 29 | String csvHeader = new String(); |
| 30 | for (int i = 0; i < proto.getRailInfoCount(); i++) { |
| 31 | RailInfoProto railInfo = proto.getRailInfo(i); |
| 32 | csvHeader += "Index" + "," |
| 33 | + "Timestamp" + "," |
| 34 | + railInfo.getRailName() + "/" + railInfo.getSubsysName() + ","; |
| 35 | } |
| 36 | System.out.println(csvHeader); |
| 37 | } |
| 38 | |
| 39 | private static void printEnergyData(PowerStatsServiceProto proto) { |
| 40 | int railInfoCount = proto.getRailInfoCount(); |
| 41 | |
| 42 | if (railInfoCount > 0) { |
| 43 | int energyDataCount = proto.getEnergyDataCount(); |
| 44 | int energyDataSetCount = energyDataCount / railInfoCount; |
| 45 | |
| 46 | for (int i = 0; i < energyDataSetCount; i++) { |
| 47 | String csvRow = new String(); |
| 48 | for (int j = 0; j < railInfoCount; j++) { |
| 49 | EnergyDataProto energyData = proto.getEnergyData(i * railInfoCount + j); |
| 50 | csvRow += energyData.getIndex() + "," |
| 51 | + energyData.getTimestampMs() + "," |
| 52 | + energyData.getEnergyUws() + ","; |
| 53 | } |
| 54 | System.out.println(csvRow); |
| 55 | } |
| 56 | } else { |
| 57 | System.out.println("Error: railInfoCount is zero"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static void generateCsvFile(String pathToIncidentReport) { |
| 62 | try { |
| 63 | IncidentReportProto irProto = |
| 64 | IncidentReportProto.parseFrom(new FileInputStream(pathToIncidentReport)); |
| 65 | |
| 66 | if (irProto.hasIncidentReport()) { |
| 67 | PowerStatsServiceProto pssProto = irProto.getIncidentReport(); |
| 68 | printRailInfo(pssProto); |
| 69 | printEnergyData(pssProto); |
| 70 | } else { |
| 71 | System.out.println("Incident report not found. Exiting."); |
| 72 | } |
| 73 | } catch (IOException e) { |
| 74 | System.out.println("Unable to open incident report file: " + pathToIncidentReport); |
| 75 | System.out.println(e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * This is the entry point to parse the ODPM data out of incident reports. |
| 81 | * It requires one argument which is the path to the incident_report.proto |
| 82 | * file captured in a bugreport. |
| 83 | * |
| 84 | * @param args Path to incident_report.proto passed in from command line. |
| 85 | */ |
| 86 | public static void main(String[] args) { |
| 87 | if (args.length > 0) { |
| 88 | generateCsvFile(args[0]); |
| 89 | } else { |
| 90 | System.err.println("Usage: PowerStatsServiceProtoParser <incident_report.proto>"); |
| 91 | System.err.println("Missing path to incident_report.proto. Exiting."); |
| 92 | System.exit(1); |
| 93 | } |
| 94 | } |
| 95 | } |