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 | // ================================================================================ |
| 64 | status_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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 81 | static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t"; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 82 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 83 | status_t KernelWakesParser::Parse(const int in, const int out) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 84 | Reader reader(in); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 85 | string line; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 86 | header_t header; // the header of /d/wakeup_sources |
| 87 | record_t record; // retain each record |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 88 | int nline = 0; |
| 89 | |
| 90 | KernelWakeSources proto; |
| 91 | |
| 92 | // parse line by line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 93 | while (reader.readLine(line)) { |
| 94 | if (line.empty()) continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 95 | // parse head line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 96 | if (nline++ == 0) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 97 | header = parseHeader(line, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 98 | continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // parse for each record, the line delimiter is \t only! |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 102 | record = parseRecord(line, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 103 | |
| 104 | if (record.size() != header.size()) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 105 | // 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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | WakeupSourceProto* source = proto.add_wakeup_sources(); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 111 | for (int i=0; i<(int)record.size(); i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 112 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 116 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 119 | if (!reader.ok(line)) { |
| 120 | fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); |
| 121 | return -1; |
| 122 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 123 | |
| 124 | if (!proto.SerializeToFileDescriptor(out)) { |
| 125 | fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); |
| 126 | return -1; |
| 127 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 128 | 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] | 129 | return NO_ERROR; |
| 130 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 131 | |
| 132 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 133 | status_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 Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 148 | header = parseHeader(line); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 152 | record = parseRecord(line); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 153 | if (record.size() != header.size()) { |
| 154 | if (record[record.size() - 1] == "TOTAL") { // TOTAL record |
| 155 | ProcessProto* total = proto.mutable_summary()->mutable_total(); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 156 | for (int i=1; i<=(int)record.size(); i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 157 | SetTableField(total, header[header.size() - i], record[record.size() - i]); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 158 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 159 | } else if (record[0] == "ZRAM:") { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 160 | record = parseRecord(line, ":"); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 161 | proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]); |
| 162 | } else if (record[0] == "RAM:") { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 163 | record = parseRecord(line, ":"); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 164 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 173 | for (int i=0; i<(int)record.size(); i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 174 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame] | 178 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 179 | } |
| 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 | } |