blob: cbcf979f90a0850c63b2ddf9ad4215594d0661a2 [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
19#include "TableEntry.h"
20
21#include "TextTable.h"
22#include "utils.h"
23
24namespace android {
25namespace lshal {
26
27static const std::string &getArchString(Architecture arch) {
28 static const std::string sStr64 = "64";
29 static const std::string sStr32 = "32";
30 static const std::string sStrBoth = "32+64";
31 static const std::string sStrUnknown = "";
32 switch (arch) {
33 case ARCH64:
34 return sStr64;
35 case ARCH32:
36 return sStr32;
37 case ARCH_BOTH:
38 return sStrBoth;
39 case ARCH_UNKNOWN: // fall through
40 default:
41 return sStrUnknown;
42 }
43}
44
45static std::string getTitle(TableColumnType type) {
46 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070047 case TableColumnType::INTERFACE_NAME: return "Interface";
48 case TableColumnType::TRANSPORT: return "Transport";
49 case TableColumnType::SERVER_PID: return "Server";
50 case TableColumnType::SERVER_CMD: return "Server CMD";
51 case TableColumnType::SERVER_ADDR: return "PTR";
52 case TableColumnType::CLIENT_PIDS: return "Clients";
53 case TableColumnType::CLIENT_CMDS: return "Clients CMD";
54 case TableColumnType::ARCH: return "Arch";
55 case TableColumnType::THREADS: return "Thread Use";
56 default:
Yifan Hongd4a77e82017-09-06 19:40:24 -070057 LOG(FATAL) << "Should not reach here.";
58 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070059 }
60}
61
62std::string TableEntry::getField(TableColumnType type) const {
63 switch (type) {
Yifan Hongd43d7052017-09-14 11:16:12 -070064 case TableColumnType::INTERFACE_NAME:
Yifan Hongd4a77e82017-09-06 19:40:24 -070065 return interfaceName;
Yifan Hongd43d7052017-09-14 11:16:12 -070066 case TableColumnType::TRANSPORT:
Yifan Hongd4a77e82017-09-06 19:40:24 -070067 return transport;
Yifan Hongd43d7052017-09-14 11:16:12 -070068 case TableColumnType::SERVER_PID:
Yifan Hongd4a77e82017-09-06 19:40:24 -070069 return serverPid == NO_PID ? "N/A" : std::to_string(serverPid);
Yifan Hongd43d7052017-09-14 11:16:12 -070070 case TableColumnType::SERVER_CMD:
Yifan Hongd4a77e82017-09-06 19:40:24 -070071 return serverCmdline;
Yifan Hongd43d7052017-09-14 11:16:12 -070072 case TableColumnType::SERVER_ADDR:
Yifan Hongd4a77e82017-09-06 19:40:24 -070073 return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress);
Yifan Hongd43d7052017-09-14 11:16:12 -070074 case TableColumnType::CLIENT_PIDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070075 return join(clientPids, " ");
Yifan Hongd43d7052017-09-14 11:16:12 -070076 case TableColumnType::CLIENT_CMDS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070077 return join(clientCmdlines, ";");
Yifan Hongd43d7052017-09-14 11:16:12 -070078 case TableColumnType::ARCH:
Yifan Hongd4a77e82017-09-06 19:40:24 -070079 return getArchString(arch);
Yifan Hongd43d7052017-09-14 11:16:12 -070080 case TableColumnType::THREADS:
Yifan Hongd4a77e82017-09-06 19:40:24 -070081 return getThreadUsage();
Yifan Hongd43d7052017-09-14 11:16:12 -070082 default:
Yifan Hongd4a77e82017-09-06 19:40:24 -070083 LOG(FATAL) << "Should not reach here.";
84 return "";
Yifan Hongd4a77e82017-09-06 19:40:24 -070085 }
86}
87
88TextTable Table::createTextTable(bool neat,
89 const std::function<std::string(const std::string&)>& emitDebugInfo) const {
90
91 TextTable textTable;
92 std::vector<std::string> row;
93 if (!neat) {
94 textTable.add(mDescription);
95
96 row.clear();
97 for (TableColumnType type : mSelectedColumns) {
98 row.push_back(getTitle(type));
99 }
100 textTable.add(std::move(row));
101 }
102
103 for (const auto& entry : mEntries) {
104 row.clear();
105 for (TableColumnType type : mSelectedColumns) {
106 row.push_back(entry.getField(type));
107 }
108 textTable.add(std::move(row));
109
110 if (emitDebugInfo) {
111 std::string debugInfo = emitDebugInfo(entry.interfaceName);
112 if (!debugInfo.empty()) textTable.add(debugInfo);
113 }
114 }
115 return textTable;
116}
117
118TextTable MergedTable::createTextTable() {
119 TextTable textTable;
120 for (const Table* table : mTables) {
121 textTable.addAll(table->createTextTable());
122 }
123 return textTable;
124}
125
Yifan Hong8bf73162017-09-07 18:06:13 -0700126bool TableEntry::operator==(const TableEntry& other) const {
127 if (this == &other) {
128 return true;
129 }
130 return interfaceName == other.interfaceName && transport == other.transport &&
131 serverPid == other.serverPid && threadUsage == other.threadUsage &&
132 threadCount == other.threadCount && serverCmdline == other.serverCmdline &&
133 serverObjectAddress == other.serverObjectAddress && clientPids == other.clientPids &&
134 clientCmdlines == other.clientCmdlines && arch == other.arch;
135}
136
137std::string TableEntry::to_string() const {
138 std::stringstream ss;
139 ss << "name=" << interfaceName << ";transport=" << transport << ";thread=" << getThreadUsage()
140 << ";server=" << serverPid
141 << "(" << serverObjectAddress << ";" << serverCmdline << ");clients=["
142 << join(clientPids, ";") << "](" << join(clientCmdlines, ";") << ");arch="
143 << getArchString(arch);
144 return ss.str();
145
146}
147
Yifan Hongd4a77e82017-09-06 19:40:24 -0700148} // namespace lshal
149} // namespace android