blob: e8792a4307f069701462343ea8b2a934323db974 [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
99 if (hash.empty() || hash == unreleased) {
100 return " "; // unknown or unreleased
101 }
102 return "Y"; // released
103}
104
Yifan Hongd4a77e82017-09-06 19:40:24 -0700105TextTable Table::createTextTable(bool neat,
106 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
107
108 TextTable textTable;
109 std::vector<std::string> row;
110 if (!neat) {
111 textTable.add(mDescription);
112
113 row.clear();
114 for (TableColumnType type : mSelectedColumns) {
115 row.push_back(getTitle(type));
116 }
117 textTable.add(std::move(row));
118 }
119
120 for (const auto& entry : mEntries) {
121 row.clear();
122 for (TableColumnType type : mSelectedColumns) {
123 row.push_back(entry.getField(type));
124 }
125 textTable.add(std::move(row));
126
127 if (emitDebugInfo) {
128 std::string debugInfo = emitDebugInfo(entry.interfaceName);
129 if (!debugInfo.empty()) textTable.add(debugInfo);
130 }
131 }
132 return textTable;
133}
134
135TextTable MergedTable::createTextTable() {
136 TextTable textTable;
137 for (const Table* table : mTables) {
138 textTable.addAll(table->createTextTable());
139 }
140 return textTable;
141}
142
Yifan Hong8bf73162017-09-07 18:06:13 -0700143bool TableEntry::operator==(const TableEntry& other) const {
144 if (this == &other) {
145 return true;
146 }
147 return interfaceName == other.interfaceName && transport == other.transport &&
148 serverPid == other.serverPid && threadUsage == other.threadUsage &&
149 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
150 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
151 clientCmdlines == other.clientCmdlines && arch == other.arch;
152}
153
154std::string TableEntry::to_string() const {
155 std::stringstream ss;
156 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
157 << ";server=" << serverPid
158 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
159 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
160 << getArchString(arch);
161 return ss.str();
162
163}
164
Yifan Hongd4a77e82017-09-06 19:40:24 -0700165} // namespace lshal
166} // namespace android