blob: 45a0e7b459d255d9dbbe11bfa9d754a8ec90c764 [file] [log] [blame]
Yi Jin04625ad2017-10-17 18:29:33 -07001/*
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
24using namespace android::os;
25
Yi Jin04625ad2017-10-17 18:29:33 -070026status_t
27PageTypeInfoParser::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 Jin51d4c542018-01-24 21:48:36 -080036 Table table(PageTypeInfoProto::Block::_FIELD_NAMES,
37 PageTypeInfoProto::Block::_FIELD_IDS,
38 PageTypeInfoProto::Block::_FIELD_COUNT);
Yi Jin04625ad2017-10-17 18:29:33 -070039
40 while (reader.readLine(&line)) {
41 if (line.empty()) {
42 migrateTypeSession = false;
43 blockHeader.clear();
44 continue;
45 }
46
Yi Jine2f7f792017-11-01 17:08:27 -070047 if (stripPrefix(&line, "Page block order:")) {
Yi Jin04625ad2017-10-17 18:29:33 -070048 pageBlockOrder = toInt(line);
Yi Jin51d4c542018-01-24 21:48:36 -080049 proto.write(PageTypeInfoProto::PAGE_BLOCK_ORDER, pageBlockOrder);
Yi Jin04625ad2017-10-17 18:29:33 -070050 continue;
51 }
Yi Jine2f7f792017-11-01 17:08:27 -070052 if (stripPrefix(&line, "Pages per block:")) {
Yi Jin51d4c542018-01-24 21:48:36 -080053 proto.write(PageTypeInfoProto::PAGES_PER_BLOCK, toInt(line));
Yi Jin04625ad2017-10-17 18:29:33 -070054 continue;
55 }
Yi Jine2f7f792017-11-01 17:08:27 -070056 if (stripPrefix(&line, "Free pages count per migrate type at order")) {
Yi Jin04625ad2017-10-17 18:29:33 -070057 migrateTypeSession = true;
58 continue;
59 }
Yi Jine2f7f792017-11-01 17:08:27 -070060 if (stripPrefix(&line, "Number of blocks type")) {
Yi Jin04625ad2017-10-17 18:29:33 -070061 blockHeader = parseHeader(line);
62 continue;
63 }
64
Yi Jine2f7f792017-11-01 17:08:27 -070065 record_t record = parseRecord(line, COMMA_DELIMITER);
Yi Jin04625ad2017-10-17 18:29:33 -070066 if (migrateTypeSession && record.size() == 3) {
Yi Jin51d4c542018-01-24 21:48:36 -080067 long long token = proto.start(PageTypeInfoProto::MIGRATE_TYPES);
Yi Jin04625ad2017-10-17 18:29:33 -070068 // expect part 0 starts with "Node"
Yi Jine2f7f792017-11-01 17:08:27 -070069 if (stripPrefix(&record[0], "Node")) {
Yi Jin51d4c542018-01-24 21:48:36 -080070 proto.write(PageTypeInfoProto::MigrateType::NODE, toInt(record[0]));
Yi Jin04625ad2017-10-17 18:29:33 -070071 } else return BAD_VALUE;
72 // expect part 1 starts with "zone"
Yi Jine2f7f792017-11-01 17:08:27 -070073 if (stripPrefix(&record[1], "zone")) {
Yi Jin51d4c542018-01-24 21:48:36 -080074 proto.write(PageTypeInfoProto::MigrateType::ZONE, record[1]);
Yi Jin04625ad2017-10-17 18:29:33 -070075 } else return BAD_VALUE;
76 // expect part 2 starts with "type"
Yi Jine2f7f792017-11-01 17:08:27 -070077 if (stripPrefix(&record[2], "type")) {
Yi Jin04625ad2017-10-17 18:29:33 -070078 // 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 Jin51d4c542018-01-24 21:48:36 -080088 proto.write(PageTypeInfoProto::MigrateType::TYPE, pageCounts[0]);
Yi Jin04625ad2017-10-17 18:29:33 -070089 for (auto i=1; i<pageCountsSize; i++) {
Yi Jin51d4c542018-01-24 21:48:36 -080090 proto.write(PageTypeInfoProto::MigrateType::FREE_PAGES_COUNT, toInt(pageCounts[i]));
Yi Jin04625ad2017-10-17 18:29:33 -070091 }
92 } else return BAD_VALUE;
93
94 proto.end(token);
95 } else if (!blockHeader.empty() && record.size() == 2) {
Yi Jin51d4c542018-01-24 21:48:36 -080096 long long token = proto.start(PageTypeInfoProto::BLOCKS);
Yi Jine2f7f792017-11-01 17:08:27 -070097 if (stripPrefix(&record[0], "Node")) {
Yi Jin51d4c542018-01-24 21:48:36 -080098 proto.write(PageTypeInfoProto::Block::NODE, toInt(record[0]));
Yi Jin04625ad2017-10-17 18:29:33 -070099 } else return BAD_VALUE;
100
Yi Jine2f7f792017-11-01 17:08:27 -0700101 if (stripPrefix(&record[1], "zone")) {
Yi Jin04625ad2017-10-17 18:29:33 -0700102 record_t blockCounts = parseRecord(record[1]);
Yi Jin51d4c542018-01-24 21:48:36 -0800103 proto.write(PageTypeInfoProto::Block::ZONE, blockCounts[0]);
Yi Jin04625ad2017-10-17 18:29:33 -0700104
105 for (size_t i=0; i<blockHeader.size(); i++) {
Yi Jine2f7f792017-11-01 17:08:27 -0700106 if (!table.insertField(&proto, blockHeader[i], blockCounts[i+1])) {
Yi Jin04625ad2017-10-17 18:29:33 -0700107 return BAD_VALUE;
108 }
109 }
110 } else return BAD_VALUE;
111 proto.end(token);
112 }
113 }
114
115 if (!reader.ok(&line)) {
116 fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
117 return -1;
118 }
119
120 if (!proto.flush(out)) {
121 fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
122 return -1;
123 }
124
125 fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.string(), proto.size());
126 return NO_ERROR;
127}