| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2016 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 |  | 
|  | 17 | #ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_TABLE_ENTRY_H_ | 
|  | 18 | #define FRAMEWORK_NATIVE_CMDS_LSHAL_TABLE_ENTRY_H_ | 
|  | 19 |  | 
|  | 20 | #include <stdint.h> | 
|  | 21 |  | 
|  | 22 | #include <string> | 
|  | 23 | #include <vector> | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 24 | #include <iostream> | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 25 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 26 | #include "TextTable.h" | 
|  | 27 |  | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 28 | namespace android { | 
|  | 29 | namespace lshal { | 
|  | 30 |  | 
|  | 31 | using Pids = std::vector<int32_t>; | 
|  | 32 |  | 
| Yifan Hong | 4b86549 | 2017-02-28 19:38:24 -0800 | [diff] [blame] | 33 | enum : unsigned int { | 
|  | 34 | HWSERVICEMANAGER_LIST, // through defaultServiceManager()->list() | 
|  | 35 | PTSERVICEMANAGER_REG_CLIENT, // through registerPassthroughClient | 
|  | 36 | LIST_DLLIB, // through listing dynamic libraries | 
|  | 37 | }; | 
|  | 38 | using TableEntrySource = unsigned int; | 
|  | 39 |  | 
| Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 40 | enum : unsigned int { | 
|  | 41 | ARCH_UNKNOWN = 0, | 
| Yifan Hong | 3fdbd9f | 2017-03-08 14:01:58 -0800 | [diff] [blame] | 42 | ARCH32       = 1 << 0, | 
|  | 43 | ARCH64       = 1 << 1, | 
| Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 44 | ARCH_BOTH    = ARCH32 | ARCH64 | 
|  | 45 | }; | 
|  | 46 | using Architecture = unsigned int; | 
|  | 47 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 48 | enum class TableColumnType : unsigned int { | 
|  | 49 | INTERFACE_NAME, | 
|  | 50 | TRANSPORT, | 
|  | 51 | SERVER_PID, | 
|  | 52 | SERVER_CMD, | 
|  | 53 | SERVER_ADDR, | 
|  | 54 | CLIENT_PIDS, | 
|  | 55 | CLIENT_CMDS, | 
|  | 56 | ARCH, | 
|  | 57 | THREADS, | 
| Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 58 | RELEASED, | 
|  | 59 | HASH, | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 60 | }; | 
|  | 61 |  | 
| Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 62 | enum { | 
|  | 63 | NO_PID = -1, | 
|  | 64 | NO_PTR = 0 | 
|  | 65 | }; | 
|  | 66 |  | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 67 | struct TableEntry { | 
| Yifan Hong | 22ea7b8 | 2017-09-14 18:07:43 -0700 | [diff] [blame] | 68 | std::string interfaceName{}; | 
|  | 69 | std::string transport{}; | 
|  | 70 | int32_t serverPid{NO_PID}; | 
|  | 71 | uint32_t threadUsage{0}; | 
|  | 72 | uint32_t threadCount{0}; | 
|  | 73 | std::string serverCmdline{}; | 
|  | 74 | uint64_t serverObjectAddress{NO_PTR}; | 
|  | 75 | Pids clientPids{}; | 
|  | 76 | std::vector<std::string> clientCmdlines{}; | 
|  | 77 | Architecture arch{ARCH_UNKNOWN}; | 
| Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 78 | // empty: unknown, all zeros: unreleased, otherwise: released | 
|  | 79 | std::string hash{}; | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 80 |  | 
|  | 81 | static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) { | 
|  | 82 | return a.interfaceName < b.interfaceName; | 
|  | 83 | }; | 
|  | 84 | static bool sortByServerPid(const TableEntry &a, const TableEntry &b) { | 
|  | 85 | return a.serverPid < b.serverPid; | 
|  | 86 | }; | 
| Steven Moreland | d8e2019 | 2017-05-24 11:23:08 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | std::string getThreadUsage() const { | 
|  | 89 | if (threadCount == 0) { | 
|  | 90 | return "N/A"; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | return std::to_string(threadUsage) + "/" + std::to_string(threadCount); | 
|  | 94 | } | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 95 |  | 
| Yifan Hong | fee209d | 2017-09-14 18:23:38 -0700 | [diff] [blame] | 96 | std::string isReleased() const; | 
|  | 97 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 98 | std::string getField(TableColumnType type) const; | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 99 |  | 
|  | 100 | bool operator==(const TableEntry& other) const; | 
|  | 101 | std::string to_string() const; | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 102 | }; | 
|  | 103 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 104 | using SelectedColumns = std::vector<TableColumnType>; | 
| Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 105 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 106 | class Table { | 
|  | 107 | public: | 
|  | 108 | using Entries = std::vector<TableEntry>; | 
|  | 109 |  | 
|  | 110 | Entries::iterator begin() { return mEntries.begin(); } | 
|  | 111 | Entries::const_iterator begin() const { return mEntries.begin(); } | 
|  | 112 | Entries::iterator end() { return mEntries.end(); } | 
|  | 113 | Entries::const_iterator end() const { return mEntries.end(); } | 
| Yifan Hong | 8bf7316 | 2017-09-07 18:06:13 -0700 | [diff] [blame] | 114 | size_t size() const { return mEntries.size(); } | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); } | 
|  | 117 |  | 
|  | 118 | void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; } | 
|  | 119 | const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; } | 
|  | 120 |  | 
|  | 121 | void setDescription(std::string&& d) { mDescription = std::move(d); } | 
|  | 122 |  | 
|  | 123 | // Write table content. | 
|  | 124 | TextTable createTextTable(bool neat = true, | 
|  | 125 | const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const; | 
|  | 126 |  | 
|  | 127 | private: | 
|  | 128 | std::string mDescription; | 
|  | 129 | Entries mEntries; | 
|  | 130 | SelectedColumns mSelectedColumns; | 
| Yifan Hong | a3b8709 | 2017-03-02 19:19:29 -0800 | [diff] [blame] | 131 | }; | 
|  | 132 |  | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 133 | using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>; | 
|  | 134 |  | 
| Yifan Hong | d4a77e8 | 2017-09-06 19:40:24 -0700 | [diff] [blame] | 135 | class MergedTable { | 
|  | 136 | public: | 
|  | 137 | MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {} | 
|  | 138 | TextTable createTextTable(); | 
|  | 139 | private: | 
|  | 140 | std::vector<const Table*> mTables; | 
| Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 141 | }; | 
|  | 142 |  | 
| Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 143 | }  // namespace lshal | 
|  | 144 | }  // namespace android | 
|  | 145 |  | 
|  | 146 | #endif  // FRAMEWORK_NATIVE_CMDS_LSHAL_TABLE_ENTRY_H_ |