Yi Jin | 04625ad | 2017-10-17 18:29:33 -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 | #define LOG_TAG "incident_helper" |
| 17 | |
| 18 | #include <android/util/ProtoOutputStream.h> |
| 19 | |
| 20 | #include "frameworks/base/core/proto/android/os/pagetypeinfo.proto.h" |
| 21 | #include "ih_util.h" |
| 22 | #include "PageTypeInfoParser.h" |
| 23 | |
| 24 | using namespace android::os; |
| 25 | |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 26 | status_t |
| 27 | PageTypeInfoParser::Parse(const int in, const int out) const |
| 28 | { |
| 29 | Reader reader(in); |
| 30 | string line; |
| 31 | bool migrateTypeSession = false; |
| 32 | int pageBlockOrder; |
| 33 | header_t blockHeader; |
| 34 | |
| 35 | ProtoOutputStream proto; |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 36 | Table table(PageTypeInfoProto::Block::_FIELD_NAMES, |
| 37 | PageTypeInfoProto::Block::_FIELD_IDS, |
| 38 | PageTypeInfoProto::Block::_FIELD_COUNT); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 39 | |
| 40 | while (reader.readLine(&line)) { |
| 41 | if (line.empty()) { |
| 42 | migrateTypeSession = false; |
| 43 | blockHeader.clear(); |
| 44 | continue; |
| 45 | } |
| 46 | |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 47 | if (stripPrefix(&line, "Page block order:")) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 48 | pageBlockOrder = toInt(line); |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 49 | proto.write(PageTypeInfoProto::PAGE_BLOCK_ORDER, pageBlockOrder); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 50 | continue; |
| 51 | } |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 52 | if (stripPrefix(&line, "Pages per block:")) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 53 | proto.write(PageTypeInfoProto::PAGES_PER_BLOCK, toInt(line)); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 54 | continue; |
| 55 | } |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 56 | if (stripPrefix(&line, "Free pages count per migrate type at order")) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 57 | migrateTypeSession = true; |
| 58 | continue; |
| 59 | } |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 60 | if (stripPrefix(&line, "Number of blocks type")) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 61 | blockHeader = parseHeader(line); |
| 62 | continue; |
| 63 | } |
| 64 | |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 65 | record_t record = parseRecord(line, COMMA_DELIMITER); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 66 | if (migrateTypeSession && record.size() == 3) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 67 | long long token = proto.start(PageTypeInfoProto::MIGRATE_TYPES); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 68 | // expect part 0 starts with "Node" |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 69 | if (stripPrefix(&record[0], "Node")) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 70 | proto.write(PageTypeInfoProto::MigrateType::NODE, toInt(record[0])); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 71 | } else return BAD_VALUE; |
| 72 | // expect part 1 starts with "zone" |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 73 | if (stripPrefix(&record[1], "zone")) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 74 | proto.write(PageTypeInfoProto::MigrateType::ZONE, record[1]); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 75 | } else return BAD_VALUE; |
| 76 | // expect part 2 starts with "type" |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 77 | if (stripPrefix(&record[2], "type")) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 78 | // expect the rest of part 2 has number of (pageBlockOrder + 2) parts |
| 79 | // An example looks like: |
| 80 | // header line: type 0 1 2 3 4 5 6 7 8 9 10 |
| 81 | // record line: Unmovable 426 279 226 1 1 1 0 0 2 2 0 |
| 82 | // The pageBlockOrder = 10 and it's zero-indexed. so total parts |
| 83 | // are 10 + 1(zero-indexed) + 1(the type part) = 12. |
| 84 | record_t pageCounts = parseRecord(record[2]); |
| 85 | int pageCountsSize = pageBlockOrder + 2; |
| 86 | if ((int)pageCounts.size() != pageCountsSize) return BAD_VALUE; |
| 87 | |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 88 | proto.write(PageTypeInfoProto::MigrateType::TYPE, pageCounts[0]); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 89 | for (auto i=1; i<pageCountsSize; i++) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 90 | proto.write(PageTypeInfoProto::MigrateType::FREE_PAGES_COUNT, toInt(pageCounts[i])); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 91 | } |
| 92 | } else return BAD_VALUE; |
| 93 | |
| 94 | proto.end(token); |
| 95 | } else if (!blockHeader.empty() && record.size() == 2) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 96 | long long token = proto.start(PageTypeInfoProto::BLOCKS); |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 97 | if (stripPrefix(&record[0], "Node")) { |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 98 | proto.write(PageTypeInfoProto::Block::NODE, toInt(record[0])); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 99 | } else return BAD_VALUE; |
| 100 | |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 101 | if (stripPrefix(&record[1], "zone")) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 102 | record_t blockCounts = parseRecord(record[1]); |
Yi Jin | 51d4c54 | 2018-01-24 21:48:36 -0800 | [diff] [blame] | 103 | proto.write(PageTypeInfoProto::Block::ZONE, blockCounts[0]); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 104 | |
| 105 | for (size_t i=0; i<blockHeader.size(); i++) { |
Yi Jin | e2f7f79 | 2017-11-01 17:08:27 -0700 | [diff] [blame] | 106 | if (!table.insertField(&proto, blockHeader[i], blockCounts[i+1])) { |
Yi Jin | 6bd2f25 | 2018-02-20 14:32:54 -0800 | [diff] [blame] | 107 | fprintf(stderr, "Header %s has bad data %s\n", blockHeader[i].c_str(), |
| 108 | blockCounts[i+1].c_str()); |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } else return BAD_VALUE; |
| 112 | proto.end(token); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (!reader.ok(&line)) { |
| 117 | fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | if (!proto.flush(out)) { |
| 122 | fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.string(), proto.size()); |
| 127 | return NO_ERROR; |
| 128 | } |