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; |
| 32 | using namespace std; |
| 33 | |
| 34 | // ================================================================================ |
| 35 | status_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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame^] | 52 | static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t"; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 53 | |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame^] | 54 | static 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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 78 | |
| 79 | status_t KernelWakesParser::Parse(const int in, const int out) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 80 | Reader reader(in); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 81 | string line; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 82 | header_t header; // the header of /d/wakeup_sources |
| 83 | record_t record; // retain each record |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 84 | int nline = 0; |
| 85 | |
| 86 | KernelWakeSources proto; |
| 87 | |
| 88 | // parse line by line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 89 | while (reader.readLine(line)) { |
| 90 | if (line.empty()) continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 91 | // parse head line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 92 | if (nline++ == 0) { |
| 93 | split(line, header, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 94 | continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // parse for each record, the line delimiter is \t only! |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 98 | split(line, record, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 99 | |
| 100 | if (record.size() != header.size()) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 101 | // 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 Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | WakeupSourceProto* source = proto.add_wakeup_sources(); |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame^] | 107 | for (int i=0; i<(int)record.size(); i++) { |
| 108 | SetWakeupSourceField(source, header[i], record[i]); |
| 109 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 112 | if (!reader.ok(line)) { |
| 113 | fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); |
| 114 | return -1; |
| 115 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 116 | |
| 117 | if (!proto.SerializeToFileDescriptor(out)) { |
| 118 | fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); |
| 119 | return -1; |
| 120 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 121 | 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] | 122 | return NO_ERROR; |
| 123 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 124 | |
| 125 | // ================================================================================ |
Yi Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame^] | 126 | // Remove K for numeric fields |
| 127 | static 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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 151 | |
| 152 | status_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 Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 168 | 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame^] | 175 | for (int i=1; i<=(int)record.size(); i++) { |
| 176 | SetProcessField(total, header[header.size() - i], record[record.size() - i]); |
| 177 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 178 | } 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 Jin | 603f3b3 | 2017-08-03 18:50:48 -0700 | [diff] [blame^] | 192 | for (int i=0; i<(int)record.size(); i++) { |
| 193 | SetProcessField(process, header[i], record[i]); |
| 194 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 195 | } |
| 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 | } |