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 | |
| 19 | #include "TableEntry.h" |
| 20 | |
| 21 | #include "TextTable.h" |
| 22 | #include "utils.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace lshal { |
| 26 | |
| 27 | static 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 | |
| 45 | static std::string getTitle(TableColumnType type) { |
| 46 | switch (type) { |
| 47 | case TableColumnType::INTERFACE_NAME: { |
| 48 | return "Interface"; |
| 49 | } break; |
| 50 | case TableColumnType::TRANSPORT: { |
| 51 | return "Transport"; |
| 52 | } break; |
| 53 | case TableColumnType::SERVER_PID: { |
| 54 | return "Server"; |
| 55 | } break; |
| 56 | case TableColumnType::SERVER_CMD: { |
| 57 | return "Server CMD"; |
| 58 | } |
| 59 | case TableColumnType::SERVER_ADDR: { |
| 60 | return "PTR"; |
| 61 | } break; |
| 62 | case TableColumnType::CLIENT_PIDS: { |
| 63 | return "Clients"; |
| 64 | } break; |
| 65 | case TableColumnType::CLIENT_CMDS: { |
| 66 | return "Clients CMD"; |
| 67 | } break; |
| 68 | case TableColumnType::ARCH: { |
| 69 | return "Arch"; |
| 70 | } break; |
| 71 | case TableColumnType::THREADS: { |
| 72 | return "Thread Use"; |
| 73 | } break; |
| 74 | default: { |
| 75 | LOG(FATAL) << "Should not reach here."; |
| 76 | return ""; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | std::string TableEntry::getField(TableColumnType type) const { |
| 82 | switch (type) { |
| 83 | case TableColumnType::INTERFACE_NAME: { |
| 84 | return interfaceName; |
| 85 | } break; |
| 86 | case TableColumnType::TRANSPORT: { |
| 87 | return transport; |
| 88 | } break; |
| 89 | case TableColumnType::SERVER_PID: { |
| 90 | return serverPid == NO_PID ? "N/A" : std::to_string(serverPid); |
| 91 | } break; |
| 92 | case TableColumnType::SERVER_CMD: { |
| 93 | return serverCmdline; |
| 94 | } break; |
| 95 | case TableColumnType::SERVER_ADDR: { |
| 96 | return serverObjectAddress == NO_PTR ? "N/A" : toHexString(serverObjectAddress); |
| 97 | } break; |
| 98 | case TableColumnType::CLIENT_PIDS: { |
| 99 | return join(clientPids, " "); |
| 100 | } break; |
| 101 | case TableColumnType::CLIENT_CMDS: { |
| 102 | return join(clientCmdlines, ";"); |
| 103 | } break; |
| 104 | case TableColumnType::ARCH: { |
| 105 | return getArchString(arch); |
| 106 | } break; |
| 107 | case TableColumnType::THREADS: { |
| 108 | return getThreadUsage(); |
| 109 | } break; |
| 110 | default: { |
| 111 | LOG(FATAL) << "Should not reach here."; |
| 112 | return ""; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | TextTable Table::createTextTable(bool neat, |
| 118 | const std::function<std::string(const std::string&)>& emitDebugInfo) const { |
| 119 | |
| 120 | TextTable textTable; |
| 121 | std::vector<std::string> row; |
| 122 | if (!neat) { |
| 123 | textTable.add(mDescription); |
| 124 | |
| 125 | row.clear(); |
| 126 | for (TableColumnType type : mSelectedColumns) { |
| 127 | row.push_back(getTitle(type)); |
| 128 | } |
| 129 | textTable.add(std::move(row)); |
| 130 | } |
| 131 | |
| 132 | for (const auto& entry : mEntries) { |
| 133 | row.clear(); |
| 134 | for (TableColumnType type : mSelectedColumns) { |
| 135 | row.push_back(entry.getField(type)); |
| 136 | } |
| 137 | textTable.add(std::move(row)); |
| 138 | |
| 139 | if (emitDebugInfo) { |
| 140 | std::string debugInfo = emitDebugInfo(entry.interfaceName); |
| 141 | if (!debugInfo.empty()) textTable.add(debugInfo); |
| 142 | } |
| 143 | } |
| 144 | return textTable; |
| 145 | } |
| 146 | |
| 147 | TextTable MergedTable::createTextTable() { |
| 148 | TextTable textTable; |
| 149 | for (const Table* table : mTables) { |
| 150 | textTable.addAll(table->createTextTable()); |
| 151 | } |
| 152 | return textTable; |
| 153 | } |
| 154 | |
| 155 | } // namespace lshal |
| 156 | } // namespace android |