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 | // ================================================================================ |
| 52 | // This list must be in order and sync with kernelwake.proto |
| 53 | const char* kernel_wake_headers[] = { |
| 54 | "name", // id: 1 |
| 55 | "active_count", // id: 2 |
| 56 | "event_count", // id: 3 |
| 57 | "wakeup_count", // id: 4 |
| 58 | "expire_count", // id: 5 |
| 59 | "active_since", // id: 6 |
| 60 | "total_time", // id: 7 |
| 61 | "max_time", // id: 8 |
| 62 | "last_change", // id: 9 |
| 63 | "prevent_suspend_time", // id: 10 |
| 64 | }; |
| 65 | |
| 66 | const string KERNEL_WAKEUP_LINE_DELIMITER = "\t"; |
| 67 | |
| 68 | status_t KernelWakesParser::Parse(const int in, const int out) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 69 | Reader reader(in); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 70 | string line; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 71 | header_t header; // the header of /d/wakeup_sources |
| 72 | record_t record; // retain each record |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 73 | int nline = 0; |
| 74 | |
| 75 | KernelWakeSources proto; |
| 76 | |
| 77 | // parse line by line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 78 | while (reader.readLine(line)) { |
| 79 | if (line.empty()) continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 80 | // parse head line |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 81 | if (nline++ == 0) { |
| 82 | split(line, header, KERNEL_WAKEUP_LINE_DELIMITER); |
| 83 | if (!assertHeaders(kernel_wake_headers, header)) { |
| 84 | fprintf(stderr, "[%s]Bad header:\n%s\n", this->name.string(), line.c_str()); |
| 85 | return BAD_VALUE; |
| 86 | } |
| 87 | continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // parse for each record, the line delimiter is \t only! |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 91 | split(line, record, KERNEL_WAKEUP_LINE_DELIMITER); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 92 | |
| 93 | if (record.size() != header.size()) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 94 | // TODO: log this to incident report! |
| 95 | fprintf(stderr, "[%s]Line %d has missing fields\n%s\n", this->name.string(), nline, line.c_str()); |
| 96 | continue; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | WakeupSourceProto* source = proto.add_wakeup_sources(); |
| 100 | source->set_name(record.at(0).c_str()); |
| 101 | // below are int32 |
| 102 | source->set_active_count(atoi(record.at(1).c_str())); |
| 103 | source->set_event_count(atoi(record.at(2).c_str())); |
| 104 | source->set_wakeup_count(atoi(record.at(3).c_str())); |
| 105 | source->set_expire_count(atoi(record.at(4).c_str())); |
| 106 | // below are int64 |
| 107 | source->set_active_since(atol(record.at(5).c_str())); |
| 108 | source->set_total_time(atol(record.at(6).c_str())); |
| 109 | source->set_max_time(atol(record.at(7).c_str())); |
| 110 | source->set_last_change(atol(record.at(8).c_str())); |
| 111 | source->set_prevent_suspend_time(atol(record.at(9).c_str())); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 114 | if (!reader.ok(line)) { |
| 115 | fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); |
| 116 | return -1; |
| 117 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 118 | |
| 119 | if (!proto.SerializeToFileDescriptor(out)) { |
| 120 | fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); |
| 121 | return -1; |
| 122 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 123 | 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] | 124 | return NO_ERROR; |
| 125 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 126 | |
| 127 | // ================================================================================ |
| 128 | const char* procrank_headers[] = { |
| 129 | "PID", // id: 1 |
| 130 | "Vss", // id: 2 |
| 131 | "Rss", // id: 3 |
| 132 | "Pss", // id: 4 |
| 133 | "Uss", // id: 5 |
| 134 | "Swap", // id: 6 |
| 135 | "PSwap", // id: 7 |
| 136 | "USwap", // id: 8 |
| 137 | "ZSwap", // id: 9 |
| 138 | "cmdline", // id: 10 |
| 139 | }; |
| 140 | |
| 141 | status_t ProcrankParser::Parse(const int in, const int out) const { |
| 142 | Reader reader(in); |
| 143 | string line, content; |
| 144 | header_t header; // the header of /d/wakeup_sources |
| 145 | record_t record; // retain each record |
| 146 | int nline = 0; |
| 147 | |
| 148 | Procrank proto; |
| 149 | |
| 150 | // parse line by line |
| 151 | while (reader.readLine(line)) { |
| 152 | if (line.empty()) continue; |
| 153 | |
| 154 | // parse head line |
| 155 | if (nline++ == 0) { |
| 156 | split(line, header); |
| 157 | if (!assertHeaders(procrank_headers, header)) { |
| 158 | fprintf(stderr, "[%s]Bad header:\n%s\n", this->name.string(), line.c_str()); |
| 159 | return BAD_VALUE; |
| 160 | } |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | split(line, record); |
| 165 | if (record.size() != header.size()) { |
| 166 | if (record[record.size() - 1] == "TOTAL") { // TOTAL record |
| 167 | ProcessProto* total = proto.mutable_summary()->mutable_total(); |
| 168 | total->set_pss(atol(record.at(0).substr(0, record.at(0).size() - 1).c_str())); |
| 169 | total->set_uss(atol(record.at(1).substr(0, record.at(1).size() - 1).c_str())); |
| 170 | total->set_swap(atol(record.at(2).substr(0, record.at(2).size() - 1).c_str())); |
| 171 | total->set_pswap(atol(record.at(3).substr(0, record.at(3).size() - 1).c_str())); |
| 172 | total->set_uswap(atol(record.at(4).substr(0, record.at(4).size() - 1).c_str())); |
| 173 | total->set_zswap(atol(record.at(5).substr(0, record.at(5).size() - 1).c_str())); |
| 174 | } else if (record[0] == "ZRAM:") { |
| 175 | split(line, record, ":"); |
| 176 | proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]); |
| 177 | } else if (record[0] == "RAM:") { |
| 178 | split(line, record, ":"); |
| 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(); |
| 188 | // int32 |
| 189 | process->set_pid(atoi(record.at(0).c_str())); |
| 190 | // int64, remove 'K' at the end |
| 191 | process->set_vss(atol(record.at(1).substr(0, record.at(1).size() - 1).c_str())); |
| 192 | process->set_rss(atol(record.at(2).substr(0, record.at(2).size() - 1).c_str())); |
| 193 | process->set_pss(atol(record.at(3).substr(0, record.at(3).size() - 1).c_str())); |
| 194 | process->set_uss(atol(record.at(4).substr(0, record.at(4).size() - 1).c_str())); |
| 195 | process->set_swap(atol(record.at(5).substr(0, record.at(5).size() - 1).c_str())); |
| 196 | process->set_pswap(atol(record.at(6).substr(0, record.at(6).size() - 1).c_str())); |
| 197 | process->set_uswap(atol(record.at(7).substr(0, record.at(7).size() - 1).c_str())); |
| 198 | process->set_zswap(atol(record.at(8).substr(0, record.at(8).size() - 1).c_str())); |
| 199 | // string |
| 200 | process->set_cmdline(record.at(9)); |
| 201 | } |
| 202 | |
| 203 | if (!reader.ok(line)) { |
| 204 | fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | if (!proto.SerializeToFileDescriptor(out)) { |
| 209 | fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); |
| 210 | return -1; |
| 211 | } |
| 212 | fprintf(stderr, "[%s]Proto size: %d bytes\n", this->name.string(), proto.ByteSize()); |
| 213 | return NO_ERROR; |
| 214 | } |