blob: 787d3a1557d698384f04aa4cc59213cfcf59e42f [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// ================================================================================
Yi Jin99c248f2017-08-25 18:11:58 -070064status_t NoopParser::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 if (!WriteStringToFd(content, out)) {
72 fprintf(stderr, "[%s]Failed to write data to incidentd\n", this->name.string());
73 return -1;
74 }
75 return NO_ERROR;
76}
77
78// ================================================================================
Yi Jin0a3406f2017-06-22 19:23:11 -070079status_t ReverseParser::Parse(const int in, const int out) const
80{
81 string content;
82 if (!ReadFdToString(in, &content)) {
83 fprintf(stderr, "[%s]Failed to read data from incidentd\n", this->name.string());
84 return -1;
85 }
86 // reverse the content
87 reverse(content.begin(), content.end());
88 if (!WriteStringToFd(content, out)) {
89 fprintf(stderr, "[%s]Failed to write data to incidentd\n", this->name.string());
90 return -1;
91 }
92 return NO_ERROR;
93}
94
95// ================================================================================
Yi Jin603f3b32017-08-03 18:50:48 -070096static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t";
Yi Jin0a3406f2017-06-22 19:23:11 -070097
Yi Jin0a3406f2017-06-22 19:23:11 -070098status_t KernelWakesParser::Parse(const int in, const int out) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070099 Reader reader(in);
Yi Jin0a3406f2017-06-22 19:23:11 -0700100 string line;
Yi Jinb44f7d42017-07-21 12:12:59 -0700101 header_t header; // the header of /d/wakeup_sources
102 record_t record; // retain each record
Yi Jin0a3406f2017-06-22 19:23:11 -0700103 int nline = 0;
104
105 KernelWakeSources proto;
106
107 // parse line by line
Yi Jinb44f7d42017-07-21 12:12:59 -0700108 while (reader.readLine(line)) {
109 if (line.empty()) continue;
Yi Jin0a3406f2017-06-22 19:23:11 -0700110 // parse head line
Yi Jinb44f7d42017-07-21 12:12:59 -0700111 if (nline++ == 0) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700112 header = parseHeader(line, KERNEL_WAKEUP_LINE_DELIMITER);
Yi Jinb44f7d42017-07-21 12:12:59 -0700113 continue;
Yi Jin0a3406f2017-06-22 19:23:11 -0700114 }
115
116 // parse for each record, the line delimiter is \t only!
Yi Jin4ef28b72017-08-14 14:45:28 -0700117 record = parseRecord(line, KERNEL_WAKEUP_LINE_DELIMITER);
Yi Jin0a3406f2017-06-22 19:23:11 -0700118
119 if (record.size() != header.size()) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700120 // TODO: log this to incident report!
121 fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline, line.c_str());
122 continue;
Yi Jin0a3406f2017-06-22 19:23:11 -0700123 }
124
125 WakeupSourceProto* source = proto.add_wakeup_sources();
Yi Jin603f3b32017-08-03 18:50:48 -0700126 for (int i=0; i<(int)record.size(); i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700127 if (!SetTableField(source, header[i], record[i])) {
128 fprintf(stderr, "[%s]Line %d has bad value %s of %s\n",
129 this->name.string(), nline, header[i].c_str(), record[i].c_str());
130 }
Yi Jin603f3b32017-08-03 18:50:48 -0700131 }
Yi Jin0a3406f2017-06-22 19:23:11 -0700132 }
133
Yi Jinb44f7d42017-07-21 12:12:59 -0700134 if (!reader.ok(line)) {
135 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
136 return -1;
137 }
Yi Jin0a3406f2017-06-22 19:23:11 -0700138
139 if (!proto.SerializeToFileDescriptor(out)) {
140 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
141 return -1;
142 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700143 fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize());
Yi Jin0a3406f2017-06-22 19:23:11 -0700144 return NO_ERROR;
145}
Yi Jinb44f7d42017-07-21 12:12:59 -0700146
147// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700148status_t ProcrankParser::Parse(const int in, const int out) const {
149 Reader reader(in);
150 string line, content;
151 header_t header; // the header of /d/wakeup_sources
152 record_t record; // retain each record
153 int nline = 0;
154
155 Procrank proto;
156
157 // parse line by line
158 while (reader.readLine(line)) {
159 if (line.empty()) continue;
160
161 // parse head line
162 if (nline++ == 0) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700163 header = parseHeader(line);
Yi Jinb44f7d42017-07-21 12:12:59 -0700164 continue;
165 }
166
Yi Jin4ef28b72017-08-14 14:45:28 -0700167 record = parseRecord(line);
Yi Jinb44f7d42017-07-21 12:12:59 -0700168 if (record.size() != header.size()) {
169 if (record[record.size() - 1] == "TOTAL") { // TOTAL record
170 ProcessProto* total = proto.mutable_summary()->mutable_total();
Yi Jin603f3b32017-08-03 18:50:48 -0700171 for (int i=1; i<=(int)record.size(); i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700172 SetTableField(total, header[header.size() - i], record[record.size() - i]);
Yi Jin603f3b32017-08-03 18:50:48 -0700173 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700174 } else if (record[0] == "ZRAM:") {
Yi Jin4ef28b72017-08-14 14:45:28 -0700175 record = parseRecord(line, ":");
Yi Jinb44f7d42017-07-21 12:12:59 -0700176 proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]);
177 } else if (record[0] == "RAM:") {
Yi Jin4ef28b72017-08-14 14:45:28 -0700178 record = parseRecord(line, ":");
Yi Jinb44f7d42017-07-21 12:12:59 -0700179 proto.mutable_summary()->mutable_ram()->set_raw_text(record[1]);
180 } else {
181 fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline,
182 line.c_str());
183 }
184 continue;
185 }
186
187 ProcessProto* process = proto.add_processes();
Yi Jin603f3b32017-08-03 18:50:48 -0700188 for (int i=0; i<(int)record.size(); i++) {
Yi Jin4ef28b72017-08-14 14:45:28 -0700189 if (!SetTableField(process, header[i], record[i])) {
190 fprintf(stderr, "[%s]Line %d has bad value %s of %s\n",
191 this->name.string(), nline, header[i].c_str(), record[i].c_str());
192 }
Yi Jin603f3b32017-08-03 18:50:48 -0700193 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700194 }
195
196 if (!reader.ok(line)) {
197 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
198 return -1;
199 }
200
201 if (!proto.SerializeToFileDescriptor(out)) {
202 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
203 return -1;
204 }
205 fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize());
206 return NO_ERROR;
Yi Jin99c248f2017-08-25 18:11:58 -0700207}