blob: fba5e662b7c13d570b23114261d26da28969f638 [file] [log] [blame]
Yi Jin0a3406f2017-06-22 19:23:11 -07001/*
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#define LOG_TAG "incident_helper"
18
19#include "IncidentHelper.h"
Yi Jinb44f7d42017-07-21 12:12:59 -070020#include "ih_util.h"
Yi Jin0a3406f2017-06-22 19:23:11 -070021
22#include "frameworks/base/core/proto/android/os/kernelwake.pb.h"
Yi Jinb44f7d42017-07-21 12:12:59 -070023#include "frameworks/base/core/proto/android/os/procrank.pb.h"
Yi Jin0a3406f2017-06-22 19:23:11 -070024
Yi Jin0a3406f2017-06-22 19:23:11 -070025#include <android-base/file.h>
26#include <unistd.h>
Yi Jin0a3406f2017-06-22 19:23:11 -070027#include <string>
28#include <vector>
29
30using namespace android::base;
31using namespace android::os;
Yi Jin4ef28b72017-08-14 14:45:28 -070032using namespace google::protobuf;
Yi Jin0a3406f2017-06-22 19:23:11 -070033using namespace std;
34
Yi Jin4ef28b72017-08-14 14:45:28 -070035static bool
36SetTableField(::google::protobuf::Message* message, string field_name, string field_value) {
37 const Descriptor* descriptor = message->GetDescriptor();
38 const Reflection* reflection = message->GetReflection();
39
40 const FieldDescriptor* field = descriptor->FindFieldByName(field_name);
41 switch (field->type()) {
42 case FieldDescriptor::TYPE_STRING:
43 reflection->SetString(message, field, field_value);
44 return true;
45 case FieldDescriptor::TYPE_INT64:
46 reflection->SetInt64(message, field, atol(field_value.c_str()));
47 return true;
48 case FieldDescriptor::TYPE_UINT64:
49 reflection->SetUInt64(message, field, atol(field_value.c_str()));
50 return true;
51 case FieldDescriptor::TYPE_INT32:
52 reflection->SetInt32(message, field, atoi(field_value.c_str()));
53 return true;
54 case FieldDescriptor::TYPE_UINT32:
55 reflection->SetUInt32(message, field, atoi(field_value.c_str()));
56 return true;
57 default:
58 // Add new scalar types
59 return false;
60 }
61}
62
Yi Jin0a3406f2017-06-22 19:23:11 -070063// ================================================================================
64status_t ReverseParser::Parse(const int in, const int out) const
65{
66 string content;
67 if (!ReadFdToString(in, &content)) {
68 fprintf(stderr, "[%s]Failed to read data from incidentd\n", this->name.string());
69 return -1;
70 }
71 // reverse the content
72 reverse(content.begin(), content.end());
73 if (!WriteStringToFd(content, out)) {
74 fprintf(stderr, "[%s]Failed to write data to incidentd\n", this->name.string());
75 return -1;
76 }
77 return NO_ERROR;
78}
79
80// ================================================================================
Yi Jin603f3b32017-08-03 18:50:48 -070081static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t";
Yi Jin0a3406f2017-06-22 19:23:11 -070082
Yi Jin0a3406f2017-06-22 19:23:11 -070083status_t KernelWakesParser::Parse(const int in, const int out) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070084 Reader reader(in);
Yi Jin0a3406f2017-06-22 19:23:11 -070085 string line;
Yi Jinb44f7d42017-07-21 12:12:59 -070086 header_t header; // the header of /d/wakeup_sources
87 record_t record; // retain each record
Yi Jin0a3406f2017-06-22 19:23:11 -070088 int nline = 0;
89
90 KernelWakeSources proto;
91
92 // parse line by line
Yi Jinb44f7d42017-07-21 12:12:59 -070093 while (reader.readLine(line)) {
94 if (line.empty()) continue;
Yi Jin0a3406f2017-06-22 19:23:11 -070095 // parse head line
Yi Jinb44f7d42017-07-21 12:12:59 -070096 if (nline++ == 0) {
Yi Jin4ef28b72017-08-14 14:45:28 -070097 header = parseHeader(line, KERNEL_WAKEUP_LINE_DELIMITER);
Yi Jinb44f7d42017-07-21 12:12:59 -070098 continue;
Yi Jin0a3406f2017-06-22 19:23:11 -070099 }
100
101 // parse for each record, the line delimiter is \t only!
Yi Jin4ef28b72017-08-14 14:45:28 -0700102 record = parseRecord(line, KERNEL_WAKEUP_LINE_DELIMITER);
Yi Jin0a3406f2017-06-22 19:23:11 -0700103
104 if (record.size() != header.size()) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700105 // TODO: log this to incident report!
106 fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline, line.c_str());
107 continue;
Yi Jin0a3406f2017-06-22 19:23:11 -0700108 }
109
110 WakeupSourceProto* source = proto.add_wakeup_sources();
Yi Jin603f3b32017-08-03 18:50:48 -0700111 for (int i=0; i<(int)record.size(); i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700112 if (!SetTableField(source, header[i], record[i])) {
113 fprintf(stderr, "[%s]Line %d has bad value %s of %s\n",
114 this->name.string(), nline, header[i].c_str(), record[i].c_str());
115 }
Yi Jin603f3b32017-08-03 18:50:48 -0700116 }
Yi Jin0a3406f2017-06-22 19:23:11 -0700117 }
118
Yi Jinb44f7d42017-07-21 12:12:59 -0700119 if (!reader.ok(line)) {
120 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
121 return -1;
122 }
Yi Jin0a3406f2017-06-22 19:23:11 -0700123
124 if (!proto.SerializeToFileDescriptor(out)) {
125 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
126 return -1;
127 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700128 fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize());
Yi Jin0a3406f2017-06-22 19:23:11 -0700129 return NO_ERROR;
130}
Yi Jinb44f7d42017-07-21 12:12:59 -0700131
132// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700133status_t ProcrankParser::Parse(const int in, const int out) const {
134 Reader reader(in);
135 string line, content;
136 header_t header; // the header of /d/wakeup_sources
137 record_t record; // retain each record
138 int nline = 0;
139
140 Procrank proto;
141
142 // parse line by line
143 while (reader.readLine(line)) {
144 if (line.empty()) continue;
145
146 // parse head line
147 if (nline++ == 0) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700148 header = parseHeader(line);
Yi Jinb44f7d42017-07-21 12:12:59 -0700149 continue;
150 }
151
Yi Jin4ef28b72017-08-14 14:45:28 -0700152 record = parseRecord(line);
Yi Jinb44f7d42017-07-21 12:12:59 -0700153 if (record.size() != header.size()) {
154 if (record[record.size() - 1] == "TOTAL") { // TOTAL record
155 ProcessProto* total = proto.mutable_summary()->mutable_total();
Yi Jin603f3b32017-08-03 18:50:48 -0700156 for (int i=1; i<=(int)record.size(); i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700157 SetTableField(total, header[header.size() - i], record[record.size() - i]);
Yi Jin603f3b32017-08-03 18:50:48 -0700158 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700159 } else if (record[0] == "ZRAM:") {
Yi Jin4ef28b72017-08-14 14:45:28 -0700160 record = parseRecord(line, ":");
Yi Jinb44f7d42017-07-21 12:12:59 -0700161 proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]);
162 } else if (record[0] == "RAM:") {
Yi Jin4ef28b72017-08-14 14:45:28 -0700163 record = parseRecord(line, ":");
Yi Jinb44f7d42017-07-21 12:12:59 -0700164 proto.mutable_summary()->mutable_ram()->set_raw_text(record[1]);
165 } else {
166 fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline,
167 line.c_str());
168 }
169 continue;
170 }
171
172 ProcessProto* process = proto.add_processes();
Yi Jin603f3b32017-08-03 18:50:48 -0700173 for (int i=0; i<(int)record.size(); i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700174 if (!SetTableField(process, header[i], record[i])) {
175 fprintf(stderr, "[%s]Line %d has bad value %s of %s\n",
176 this->name.string(), nline, header[i].c_str(), record[i].c_str());
177 }
Yi Jin603f3b32017-08-03 18:50:48 -0700178 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700179 }
180
181 if (!reader.ok(line)) {
182 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
183 return -1;
184 }
185
186 if (!proto.SerializeToFileDescriptor(out)) {
187 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
188 return -1;
189 }
190 fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize());
191 return NO_ERROR;
192}