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