Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 20 | #include "ih_util.h" |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 21 | |
| 22 | #include "frameworks/base/core/proto/android/os/kernelwake.pb.h" |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 23 | #include "frameworks/base/core/proto/android/os/procrank.pb.h" |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 24 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 25 | #include <android-base/file.h> |
| 26 | #include <unistd.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
| 30 | using namespace android::base; |
| 31 | using namespace android::os; |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 32 | using namespace google::protobuf; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 35 | static bool |
| 36 | SetTableField(::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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 63 | // ================================================================================ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame^] | 64 | status_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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 79 | status_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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 96 | static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t"; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 97 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 98 | status_t KernelWakesParser::Parse(const int in, const int out) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 99 | Reader reader(in); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 100 | string line; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 101 | header_t header; // the header of /d/wakeup_sources |
| 102 | record_t record; // retain each record |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 103 | int nline = 0; |
| 104 | |
| 105 | KernelWakeSources proto; |
| 106 | |
| 107 | // parse line by line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 108 | while (reader.readLine(line)) { |
| 109 | if (line.empty()) continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 110 | // parse head line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 111 | if (nline++ == 0) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 112 | header = parseHeader(line, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 113 | continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // parse for each record, the line delimiter is \t only! |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 117 | record = parseRecord(line, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 118 | |
| 119 | if (record.size() != header.size()) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 120 | // 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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | WakeupSourceProto* source = proto.add_wakeup_sources(); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 126 | for (int i=0; i<(int)record.size(); i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 127 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 131 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 134 | if (!reader.ok(line)) { |
| 135 | fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); |
| 136 | return -1; |
| 137 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 138 | |
| 139 | if (!proto.SerializeToFileDescriptor(out)) { |
| 140 | fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); |
| 141 | return -1; |
| 142 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 143 | fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 144 | return NO_ERROR; |
| 145 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 146 | |
| 147 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 148 | status_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 Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 163 | header = parseHeader(line); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 164 | continue; |
| 165 | } |
| 166 | |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 167 | record = parseRecord(line); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 168 | if (record.size() != header.size()) { |
| 169 | if (record[record.size() - 1] == "TOTAL") { // TOTAL record |
| 170 | ProcessProto* total = proto.mutable_summary()->mutable_total(); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 171 | for (int i=1; i<=(int)record.size(); i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 172 | SetTableField(total, header[header.size() - i], record[record.size() - i]); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 173 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 174 | } else if (record[0] == "ZRAM:") { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 175 | record = parseRecord(line, ":"); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 176 | proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]); |
| 177 | } else if (record[0] == "RAM:") { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 178 | record = parseRecord(line, ":"); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 179 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 188 | for (int i=0; i<(int)record.size(); i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 189 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 193 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 194 | } |
| 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 Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame^] | 207 | } |