blob: 664c48f75f9ffca4f9f6f26777662738f0184c1e [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;
32using namespace std;
33
34// ================================================================================
35status_t ReverseParser::Parse(const int in, const int out) const
36{
37 string content;
38 if (!ReadFdToString(in, &content)) {
39 fprintf(stderr, "[%s]Failed to read data from incidentd\n", this->name.string());
40 return -1;
41 }
42 // reverse the content
43 reverse(content.begin(), content.end());
44 if (!WriteStringToFd(content, out)) {
45 fprintf(stderr, "[%s]Failed to write data to incidentd\n", this->name.string());
46 return -1;
47 }
48 return NO_ERROR;
49}
50
51// ================================================================================
Yi Jin603f3b32017-08-03 18:50:48 -070052static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t";
Yi Jin0a3406f2017-06-22 19:23:11 -070053
Yi Jin603f3b32017-08-03 18:50:48 -070054static void SetWakeupSourceField(WakeupSourceProto* source, string name, string value) {
55 if (name == "name") {
56 source->set_name(value.c_str());
57 } else if (name == "active_count") {
58 source->set_active_count(atoi(value.c_str()));
59 } else if (name == "event_count") {
60 source->set_event_count(atoi(value.c_str()));
61 } else if (name == "wakeup_count") {
62 source->set_wakeup_count(atoi(value.c_str()));
63 } else if (name == "expire_count") {
64 source->set_expire_count(atoi(value.c_str()));
65 } else if (name == "active_count") {
66 source->set_active_since(atol(value.c_str()));
67 } else if (name == "total_time") {
68 source->set_total_time(atol(value.c_str()));
69 } else if (name == "max_time") {
70 source->set_max_time(atol(value.c_str()));
71 } else if (name == "last_change") {
72 source->set_last_change(atol(value.c_str()));
73 } else if (name == "prevent_suspend_time") {
74 source->set_prevent_suspend_time(atol(value.c_str()));
75 }
76 // add new fields
77}
Yi Jin0a3406f2017-06-22 19:23:11 -070078
79status_t KernelWakesParser::Parse(const int in, const int out) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070080 Reader reader(in);
Yi Jin0a3406f2017-06-22 19:23:11 -070081 string line;
Yi Jinb44f7d42017-07-21 12:12:59 -070082 header_t header; // the header of /d/wakeup_sources
83 record_t record; // retain each record
Yi Jin0a3406f2017-06-22 19:23:11 -070084 int nline = 0;
85
86 KernelWakeSources proto;
87
88 // parse line by line
Yi Jinb44f7d42017-07-21 12:12:59 -070089 while (reader.readLine(line)) {
90 if (line.empty()) continue;
Yi Jin0a3406f2017-06-22 19:23:11 -070091 // parse head line
Yi Jinb44f7d42017-07-21 12:12:59 -070092 if (nline++ == 0) {
93 split(line, header, KERNEL_WAKEUP_LINE_DELIMITER);
Yi Jinb44f7d42017-07-21 12:12:59 -070094 continue;
Yi Jin0a3406f2017-06-22 19:23:11 -070095 }
96
97 // parse for each record, the line delimiter is \t only!
Yi Jinb44f7d42017-07-21 12:12:59 -070098 split(line, record, KERNEL_WAKEUP_LINE_DELIMITER);
Yi Jin0a3406f2017-06-22 19:23:11 -070099
100 if (record.size() != header.size()) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700101 // TODO: log this to incident report!
102 fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline, line.c_str());
103 continue;
Yi Jin0a3406f2017-06-22 19:23:11 -0700104 }
105
106 WakeupSourceProto* source = proto.add_wakeup_sources();
Yi Jin603f3b32017-08-03 18:50:48 -0700107 for (int i=0; i<(int)record.size(); i++) {
108 SetWakeupSourceField(source, header[i], record[i]);
109 }
Yi Jin0a3406f2017-06-22 19:23:11 -0700110 }
111
Yi Jinb44f7d42017-07-21 12:12:59 -0700112 if (!reader.ok(line)) {
113 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
114 return -1;
115 }
Yi Jin0a3406f2017-06-22 19:23:11 -0700116
117 if (!proto.SerializeToFileDescriptor(out)) {
118 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
119 return -1;
120 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700121 fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize());
Yi Jin0a3406f2017-06-22 19:23:11 -0700122 return NO_ERROR;
123}
Yi Jinb44f7d42017-07-21 12:12:59 -0700124
125// ================================================================================
Yi Jin603f3b32017-08-03 18:50:48 -0700126// Remove K for numeric fields
127static void SetProcessField(ProcessProto* process, string name, string value) {
128 ssize_t len = value.size();
129 if (name == "PID") {
130 process->set_pid(atoi(value.c_str()));
131 } else if (name == "Vss") {
132 process->set_vss(atol(value.substr(0, len - 1).c_str()));
133 } else if (name == "Rss") {
134 process->set_rss(atol(value.substr(0, len - 1).c_str()));
135 } else if (name == "Pss") {
136 process->set_pss(atol(value.substr(0, len - 1).c_str()));
137 } else if (name == "Uss") {
138 process->set_uss(atol(value.substr(0, len - 1).c_str()));
139 } else if (name == "Swap") {
140 process->set_swap(atol(value.substr(0, len - 1).c_str()));
141 } else if (name == "PSwap") {
142 process->set_pswap(atol(value.substr(0, len - 1).c_str()));
143 } else if (name == "USwap") {
144 process->set_uswap(atol(value.substr(0, len - 1).c_str()));
145 } else if (name == "ZSwap") {
146 process->set_zswap(atol(value.substr(0, len - 1).c_str()));
147 } else if (name == "cmdline") {
148 process->set_cmdline(value);
149 }
150}
Yi Jinb44f7d42017-07-21 12:12:59 -0700151
152status_t ProcrankParser::Parse(const int in, const int out) const {
153 Reader reader(in);
154 string line, content;
155 header_t header; // the header of /d/wakeup_sources
156 record_t record; // retain each record
157 int nline = 0;
158
159 Procrank proto;
160
161 // parse line by line
162 while (reader.readLine(line)) {
163 if (line.empty()) continue;
164
165 // parse head line
166 if (nline++ == 0) {
167 split(line, header);
Yi Jinb44f7d42017-07-21 12:12:59 -0700168 continue;
169 }
170
171 split(line, record);
172 if (record.size() != header.size()) {
173 if (record[record.size() - 1] == "TOTAL") { // TOTAL record
174 ProcessProto* total = proto.mutable_summary()->mutable_total();
Yi Jin603f3b32017-08-03 18:50:48 -0700175 for (int i=1; i<=(int)record.size(); i++) {
176 SetProcessField(total, header[header.size() - i], record[record.size() - i]);
177 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700178 } else if (record[0] == "ZRAM:") {
179 split(line, record, ":");
180 proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]);
181 } else if (record[0] == "RAM:") {
182 split(line, record, ":");
183 proto.mutable_summary()->mutable_ram()->set_raw_text(record[1]);
184 } else {
185 fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline,
186 line.c_str());
187 }
188 continue;
189 }
190
191 ProcessProto* process = proto.add_processes();
Yi Jin603f3b32017-08-03 18:50:48 -0700192 for (int i=0; i<(int)record.size(); i++) {
193 SetProcessField(process, header[i], record[i]);
194 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700195 }
196
197 if (!reader.ok(line)) {
198 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
199 return -1;
200 }
201
202 if (!proto.SerializeToFileDescriptor(out)) {
203 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
204 return -1;
205 }
206 fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize());
207 return NO_ERROR;
208}