blob: 76edd6341b553155368683c246ed1e97325086f1 [file] [log] [blame]
Mat Bevilacqua81601212020-07-30 17:26:45 -07001/*
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
17package com.android.server.powerstats;
18
19import java.io.FileInputStream;
20import 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 */
27public class PowerStatsServiceProtoParser {
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070028 private static void printEnergyMeterInfo(PowerStatsServiceMeterProto proto) {
Mat Bevilacqua81601212020-07-30 17:26:45 -070029 String csvHeader = new String();
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070030 for (int i = 0; i < proto.getChannelInfoCount(); i++) {
31 ChannelInfoProto energyMeterInfo = proto.getChannelInfo(i);
32 csvHeader += "Index,Timestamp," + energyMeterInfo.getChannelId()
33 + "/" + energyMeterInfo.getChannelName() + ",";
Mat Bevilacqua81601212020-07-30 17:26:45 -070034 }
35 System.out.println(csvHeader);
36 }
37
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070038 private static void printEnergyMeasurements(PowerStatsServiceMeterProto proto) {
39 int energyMeterInfoCount = proto.getChannelInfoCount();
Mat Bevilacqua81601212020-07-30 17:26:45 -070040
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070041 if (energyMeterInfoCount > 0) {
42 int energyMeasurementCount = proto.getEnergyMeasurementCount();
43 int energyMeasurementSetCount = energyMeasurementCount / energyMeterInfoCount;
Mat Bevilacqua81601212020-07-30 17:26:45 -070044
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070045 for (int i = 0; i < energyMeasurementSetCount; i++) {
Mat Bevilacqua81601212020-07-30 17:26:45 -070046 String csvRow = new String();
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070047 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 Bevilacqua81601212020-07-30 17:26:45 -070053 }
54 System.out.println(csvRow);
55 }
56 } else {
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070057 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 Bevilacqua81601212020-07-30 17:26:45 -070090 }
91 }
92
93 private static void generateCsvFile(String pathToIncidentReport) {
94 try {
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070095 // Print power meter data.
96 IncidentReportMeterProto irMeterProto =
97 IncidentReportMeterProto.parseFrom(new FileInputStream(pathToIncidentReport));
Mat Bevilacqua81601212020-07-30 17:26:45 -070098
Mat Bevilacquaa375ea02020-09-21 18:59:06 -070099 if (irMeterProto.hasIncidentReport()) {
100 PowerStatsServiceMeterProto pssMeterProto = irMeterProto.getIncidentReport();
101 printEnergyMeterInfo(pssMeterProto);
102 printEnergyMeasurements(pssMeterProto);
Mat Bevilacqua81601212020-07-30 17:26:45 -0700103 } else {
Mat Bevilacquaa375ea02020-09-21 18:59:06 -0700104 System.out.println("Meter incident report not found. Exiting.");
105 }
106
107 // Print power model data.
108 IncidentReportModelProto irModelProto =
109 IncidentReportModelProto.parseFrom(new FileInputStream(pathToIncidentReport));
110
111 if (irModelProto.hasIncidentReport()) {
112 PowerStatsServiceModelProto pssModelProto = irModelProto.getIncidentReport();
113 printEnergyConsumerId(pssModelProto);
114 printEnergyConsumerResults(pssModelProto);
115 } else {
116 System.out.println("Model incident report not found. Exiting.");
Mat Bevilacqua81601212020-07-30 17:26:45 -0700117 }
118 } catch (IOException e) {
119 System.out.println("Unable to open incident report file: " + pathToIncidentReport);
120 System.out.println(e);
121 }
122 }
123
124 /**
125 * This is the entry point to parse the ODPM data out of incident reports.
126 * It requires one argument which is the path to the incident_report.proto
127 * file captured in a bugreport.
128 *
129 * @param args Path to incident_report.proto passed in from command line.
130 */
131 public static void main(String[] args) {
132 if (args.length > 0) {
133 generateCsvFile(args[0]);
134 } else {
135 System.err.println("Usage: PowerStatsServiceProtoParser <incident_report.proto>");
136 System.err.println("Missing path to incident_report.proto. Exiting.");
137 System.exit(1);
138 }
139 }
140}