blob: 0154a2e431467dab381b9a1d7142af9e802c749d [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 Hongbdf44f82018-05-25 14:20:00 -070019#include <map>
20
21#include <android-base/strings.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070022#include <hidl-hash/Hash.h>
Yifan Hong8304e412018-05-25 15:05:36 -070023#include <vintf/parse_string.h>
Yifan Hongfee209d2017-09-14 18:23:38 -070024
Yifan Hongd4a77e82017-09-06 19:40:24 -070025#include "TableEntry.h"
26
27#include "TextTable.h"
28#include "utils.h"
29
30namespace android {
31namespace lshal {
32
Yifan Hong0ad64f52018-05-25 15:29:17 -070033static const std::string &getArchString(vintf::Arch arch) {
Yifan Hongd4a77e82017-09-06 19:40:24 -070034 static const std::string sStr64 = "64";
35 static const std::string sStr32 = "32";
36 static const std::string sStrBoth = "32+64";
37 static const std::string sStrUnknown = "";
38 switch (arch) {
Yifan Hong0ad64f52018-05-25 15:29:17 -070039 case vintf::Arch::ARCH_64:
Yifan Hongd4a77e82017-09-06 19:40:24 -070040 return sStr64;
Yifan Hong0ad64f52018-05-25 15:29:17 -070041 case vintf::Arch::ARCH_32:
Yifan Hongd4a77e82017-09-06 19:40:24 -070042 return sStr32;
Yifan Hong0ad64f52018-05-25 15:29:17 -070043 case vintf::Arch::ARCH_32_64:
Yifan Hongd4a77e82017-09-06 19:40:24 -070044 return sStrBoth;
Yifan Hong0ad64f52018-05-25 15:29:17 -070045 case vintf::Arch::ARCH_EMPTY: // fall through
Yifan Hongd4a77e82017-09-06 19:40:24 -070046 default:
47 return sStrUnknown;
48 }
49}
50
51static std::string getTitle(TableColumnType type) {
52 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070053 case TableColumnType::INTERFACE_NAME: return "Interface";
54 case TableColumnType::TRANSPORT: return "Transport";
55 case TableColumnType::SERVER_PID: return "Server";
56 case TableColumnType::SERVER_CMD: return "Server CMD";
57 case TableColumnType::SERVER_ADDR: return "PTR";
58 case TableColumnType::CLIENT_PIDS: return "Clients";
59 case TableColumnType::CLIENT_CMDS: return "Clients CMD";
60 case TableColumnType::ARCH: return "Arch";
61 case TableColumnType::THREADS: return "Thread Use";
Yifan Hongfee209d2017-09-14 18:23:38 -070062 case TableColumnType::RELEASED: return "R";
63 case TableColumnType::HASH: return "Hash";
Yifan Hongbdf44f82018-05-25 14:20:00 -070064 case TableColumnType::VINTF: return "VINTF";
Yifan Hongd43d7052017-09-14 11:16:12 -070065 default:
Yifan Hongfee209d2017-09-14 18:23:38 -070066 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -070067 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070068 }
69}
70
71std::string TableEntry::getField(TableColumnType type) const {
72 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070073 case TableColumnType::INTERFACE_NAME:
Yifan Hongd4a77e82017-09-06 19:40:24 -070074 return interfaceName;
Yifan Hongd43d7052017-09-14 11:16:12 -070075 case TableColumnType::TRANSPORT:
Yifan Hong8304e412018-05-25 15:05:36 -070076 return vintf::to_string(transport);
Yifan Hongd43d7052017-09-14 11:16:12 -070077 case TableColumnType::SERVER_PID:
Yifan Hongd4a77e82017-09-06 19:40:24 -070078 return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
Yifan Hongd43d7052017-09-14 11:16:12 -070079 case TableColumnType::SERVER_CMD:
Yifan Hongd4a77e82017-09-06 19:40:24 -070080 return serverCmdline;
Yifan Hongd43d7052017-09-14 11:16:12 -070081 case TableColumnType::SERVER_ADDR:
Yifan Hongd4a77e82017-09-06 19:40:24 -070082 return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
Yifan Hongd43d7052017-09-14 11:16:12 -070083 case TableColumnType::CLIENT_PIDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070084 return join(clientPids, " ");
Yifan Hongd43d7052017-09-14 11:16:12 -070085 case TableColumnType::CLIENT_CMDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070086 return join(clientCmdlines, ";");
Yifan Hongd43d7052017-09-14 11:16:12 -070087 case TableColumnType::ARCH:
Yifan Hongd4a77e82017-09-06 19:40:24 -070088 return getArchString(arch);
Yifan Hongd43d7052017-09-14 11:16:12 -070089 case TableColumnType::THREADS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070090 return getThreadUsage();
Yifan Hongfee209d2017-09-14 18:23:38 -070091 case TableColumnType::RELEASED:
92 return isReleased();
93 case TableColumnType::HASH:
94 return hash;
Yifan Hongbdf44f82018-05-25 14:20:00 -070095 case TableColumnType::VINTF:
96 return getVintfInfo();
Yifan Hongd43d7052017-09-14 11:16:12 -070097 default:
Yifan Hongfee209d2017-09-14 18:23:38 -070098 LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type);
Yifan Hongd4a77e82017-09-06 19:40:24 -070099 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -0700100 }
101}
102
Yifan Hongfee209d2017-09-14 18:23:38 -0700103std::string TableEntry::isReleased() const {
104 static const std::string unreleased = Hash::hexString(Hash::kEmptyHash);
105
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700106 if (hash.empty()) {
107 return "?";
108 }
109 if (hash == unreleased) {
110 return "N"; // unknown or unreleased
Yifan Hongfee209d2017-09-14 18:23:38 -0700111 }
112 return "Y"; // released
113}
114
Yifan Hongbdf44f82018-05-25 14:20:00 -0700115std::string TableEntry::getVintfInfo() const {
116 static const std::map<VintfInfo, std::string> values{
117 {DEVICE_MANIFEST, "DM"},
118 {DEVICE_MATRIX, "DC"},
119 {FRAMEWORK_MANIFEST, "FM"},
120 {FRAMEWORK_MATRIX, "FC"},
121 };
122 std::vector<std::string> ret;
123 for (const auto& pair : values) {
124 if (vintfInfo & pair.first) {
125 ret.push_back(pair.second);
126 }
127 }
128 auto joined = base::Join(ret, ',');
129 return joined.empty() ? "X" : joined;
130}
131
Yifan Hongd4a77e82017-09-06 19:40:24 -0700132TextTable Table::createTextTable(bool neat,
133 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
134
135 TextTable textTable;
136 std::vector<std::string> row;
137 if (!neat) {
138 textTable.add(mDescription);
139
140 row.clear();
141 for (TableColumnType type : mSelectedColumns) {
142 row.push_back(getTitle(type));
143 }
144 textTable.add(std::move(row));
145 }
146
147 for (const auto& entry : mEntries) {
148 row.clear();
149 for (TableColumnType type : mSelectedColumns) {
150 row.push_back(entry.getField(type));
151 }
152 textTable.add(std::move(row));
153
154 if (emitDebugInfo) {
155 std::string debugInfo = emitDebugInfo(entry.interfaceName);
156 if (!debugInfo.empty()) textTable.add(debugInfo);
157 }
158 }
159 return textTable;
160}
161
162TextTable MergedTable::createTextTable() {
163 TextTable textTable;
164 for (const Table* table : mTables) {
165 textTable.addAll(table->createTextTable());
166 }
167 return textTable;
168}
169
Yifan Hong8bf73162017-09-07 18:06:13 -0700170bool TableEntry::operator==(const TableEntry& other) const {
171 if (this == &other) {
172 return true;
173 }
174 return interfaceName == other.interfaceName && transport == other.transport &&
175 serverPid == other.serverPid && threadUsage == other.threadUsage &&
176 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
177 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
178 clientCmdlines == other.clientCmdlines && arch == other.arch;
179}
180
181std::string TableEntry::to_string() const {
Yifan Hong8304e412018-05-25 15:05:36 -0700182 using vintf::operator<<;
Yifan Hong8bf73162017-09-07 18:06:13 -0700183 std::stringstream ss;
184 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
185 << ";server=" << serverPid
186 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
187 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
188 << getArchString(arch);
189 return ss.str();
190
191}
192
Yifan Hongd4a77e82017-09-06 19:40:24 -0700193} // namespace lshal
194} // namespace android