blob: f390850e01db027a1829a8dfce9db190301c6f25 [file] [log] [blame]
Yifan Hongd4a77e82017-09-06 19:40:24 -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 "lshal"
17#include <android-base/logging.h>
18
Yifan Hongfee209d2017-09-14 18:23:38 -070019#include <hidl-hash/Hash.h>
20
Yifan Hongd4a77e82017-09-06 19:40:24 -070021#include "TableEntry.h"
22
23#include "TextTable.h"
24#include "utils.h"
25
26namespace android {
27namespace lshal {
28
29static const std::string &getArchString(Architecture arch) {
30 static const std::string sStr64 = "64";
31 static const std::string sStr32 = "32";
32 static const std::string sStrBoth = "32+64";
33 static const std::string sStrUnknown = "";
34 switch (arch) {
35 case ARCH64:
36 return sStr64;
37 case ARCH32:
38 return sStr32;
39 case ARCH_BOTH:
40 return sStrBoth;
41 case ARCH_UNKNOWN: // fall through
42 default:
43 return sStrUnknown;
44 }
45}
46
47static std::string getTitle(TableColumnType type) {
48 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070049 case TableColumnType::INTERFACE_NAME: return "Interface";
50 case TableColumnType::TRANSPORT: return "Transport";
51 case TableColumnType::SERVER_PID: return "Server";
52 case TableColumnType::SERVER_CMD: return "Server CMD";
53 case TableColumnType::SERVER_ADDR: return "PTR";
54 case TableColumnType::CLIENT_PIDS: return "Clients";
55 case TableColumnType::CLIENT_CMDS: return "Clients CMD";
56 case TableColumnType::ARCH: return "Arch";
57 case TableColumnType::THREADS: return "Thread Use";
Yifan Hongfee209d2017-09-14 18:23:38 -070058 case TableColumnType::RELEASED: return "R";
59 case TableColumnType::HASH: return "Hash";
Yifan Hongd43d7052017-09-14 11:16:12 -070060 default:
Yifan Hongfee209d2017-09-14 18:23:38 -070061 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -070062 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070063 }
64}
65
66std::string TableEntry::getField(TableColumnType type) const {
67 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070068 case TableColumnType::INTERFACE_NAME:
Yifan Hongd4a77e82017-09-06 19:40:24 -070069 return interfaceName;
Yifan Hongd43d7052017-09-14 11:16:12 -070070 case TableColumnType::TRANSPORT:
Yifan Hongd4a77e82017-09-06 19:40:24 -070071 return transport;
Yifan Hongd43d7052017-09-14 11:16:12 -070072 case TableColumnType::SERVER_PID:
Yifan Hongd4a77e82017-09-06 19:40:24 -070073 return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
Yifan Hongd43d7052017-09-14 11:16:12 -070074 case TableColumnType::SERVER_CMD:
Yifan Hongd4a77e82017-09-06 19:40:24 -070075 return serverCmdline;
Yifan Hongd43d7052017-09-14 11:16:12 -070076 case TableColumnType::SERVER_ADDR:
Yifan Hongd4a77e82017-09-06 19:40:24 -070077 return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
Yifan Hongd43d7052017-09-14 11:16:12 -070078 case TableColumnType::CLIENT_PIDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070079 return join(clientPids, " ");
Yifan Hongd43d7052017-09-14 11:16:12 -070080 case TableColumnType::CLIENT_CMDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070081 return join(clientCmdlines, ";");
Yifan Hongd43d7052017-09-14 11:16:12 -070082 case TableColumnType::ARCH:
Yifan Hongd4a77e82017-09-06 19:40:24 -070083 return getArchString(arch);
Yifan Hongd43d7052017-09-14 11:16:12 -070084 case TableColumnType::THREADS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070085 return getThreadUsage();
Yifan Hongfee209d2017-09-14 18:23:38 -070086 case TableColumnType::RELEASED:
87 return isReleased();
88 case TableColumnType::HASH:
89 return hash;
Yifan Hongd43d7052017-09-14 11:16:12 -070090 default:
Yifan Hongfee209d2017-09-14 18:23:38 -070091 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -070092 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070093 }
94}
95
Yifan Hongfee209d2017-09-14 18:23:38 -070096std::string TableEntry::isReleased() const {
97 static const std::string unreleased = Hash::hexString(Hash::kEmptyHash);
98
Yifan Hongd5ee11a2018-05-25 12:57:04 -070099 if (hash.empty()) {
100 return "?";
101 }
102 if (hash == unreleased) {
103 return "N"; // unknown or unreleased
Yifan Hongfee209d2017-09-14 18:23:38 -0700104 }
105 return "Y"; // released
106}
107
Yifan Hongd4a77e82017-09-06 19:40:24 -0700108TextTable Table::createTextTable(bool neat,
109 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
110
111 TextTable textTable;
112 std::vector<std::string> row;
113 if (!neat) {
114 textTable.add(mDescription);
115
116 row.clear();
117 for (TableColumnType type : mSelectedColumns) {
118 row.push_back(getTitle(type));
119 }
120 textTable.add(std::move(row));
121 }
122
123 for (const auto& entry : mEntries) {
124 row.clear();
125 for (TableColumnType type : mSelectedColumns) {
126 row.push_back(entry.getField(type));
127 }
128 textTable.add(std::move(row));
129
130 if (emitDebugInfo) {
131 std::string debugInfo = emitDebugInfo(entry.interfaceName);
132 if (!debugInfo.empty()) textTable.add(debugInfo);
133 }
134 }
135 return textTable;
136}
137
138TextTable MergedTable::createTextTable() {
139 TextTable textTable;
140 for (const Table* table : mTables) {
141 textTable.addAll(table->createTextTable());
142 }
143 return textTable;
144}
145
Yifan Hong8bf73162017-09-07 18:06:13 -0700146bool TableEntry::operator==(const TableEntry& other) const {
147 if (this == &other) {
148 return true;
149 }
150 return interfaceName == other.interfaceName && transport == other.transport &&
151 serverPid == other.serverPid && threadUsage == other.threadUsage &&
152 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
153 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
154 clientCmdlines == other.clientCmdlines && arch == other.arch;
155}
156
157std::string TableEntry::to_string() const {
158 std::stringstream ss;
159 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
160 << ";server=" << serverPid
161 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
162 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
163 << getArchString(arch);
164 return ss.str();
165
166}
167
Yifan Hongd4a77e82017-09-06 19:40:24 -0700168} // namespace lshal
169} // namespace android