Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame^] | 19 | #include <hidl-hash/Hash.h> |
| 20 | |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 21 | #include "TableEntry.h" |
| 22 | |
| 23 | #include "TextTable.h" |
| 24 | #include "utils.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace lshal { |
| 28 | |
| 29 | static 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 | |
| 47 | static std::string getTitle(TableColumnType type) { |
| 48 | switch (type) { |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 49 | 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 Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame^] | 58 | case TableColumnType::RELEASED: return "R"; |
| 59 | case TableColumnType::HASH: return "Hash"; |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 60 | default: |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame^] | 61 | LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 62 | return ""; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | std::string TableEntry::getField(TableColumnType type) const { |
| 67 | switch (type) { |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 68 | case TableColumnType::INTERFACE_NAME: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 69 | return interfaceName; |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 70 | case TableColumnType::TRANSPORT: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 71 | return transport; |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 72 | case TableColumnType::SERVER_PID: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 73 | return serverPid == NO_PID ? "N/A" : std::to_string(serverPid); |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 74 | case TableColumnType::SERVER_CMD: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 75 | return serverCmdline; |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 76 | case TableColumnType::SERVER_ADDR: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 77 | return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress); |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 78 | case TableColumnType::CLIENT_PIDS: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 79 | return join(clientPids, " "); |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 80 | case TableColumnType::CLIENT_CMDS: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 81 | return join(clientCmdlines, ";"); |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 82 | case TableColumnType::ARCH: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 83 | return getArchString(arch); |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 84 | case TableColumnType::THREADS: |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 85 | return getThreadUsage(); |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame^] | 86 | case TableColumnType::RELEASED: |
| 87 | return isReleased(); |
| 88 | case TableColumnType::HASH: |
| 89 | return hash; |
Yifan Hong | d43d705 | 2017-09-14 11:16:12 -0700 | [diff] [blame] | 90 | default: |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame^] | 91 | LOG(FATAL) << __func__ << "Should not reach here. " << static_cast<int>(type); |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 92 | return ""; |
Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame^] | 96 | std::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 Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 105 | TextTable 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 | |
| 135 | TextTable MergedTable::createTextTable() { |
| 136 | TextTable textTable; |
| 137 | for (const Table* table : mTables) { |
| 138 | textTable.addAll(table->createTextTable()); |
| 139 | } |
| 140 | return textTable; |
| 141 | } |
| 142 | |
Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 143 | bool 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 | |
| 154 | std::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 Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 165 | } // namespace lshal |
| 166 | } // namespace android |