• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <string>
22 #include <vector>
23 #include <iostream>
24 
25 #include <procpartition/procpartition.h>
26 #include <vintf/Arch.h>
27 #include <vintf/Transport.h>
28 
29 #include "TextTable.h"
30 
31 namespace android {
32 namespace lshal {
33 
34 using android::procpartition::Partition;
35 using Pids = std::vector<pid_t>;
36 
37 enum class TableColumnType : unsigned int {
38     INTERFACE_NAME = 0,
39     TRANSPORT,
40     SERVER_PID,
41     SERVER_ADDR,
42     ARCH,
43     THREADS,
44     RELEASED,
45     HASH,
46     VINTF,
47     SERVICE_STATUS,
48     CLIENT_PIDS,
49 
50     // Not a real TableColumnType. Used to determine all TableColumnTypes.
51     LAST,
52 
53     // Not included in all TableColumnTypes because they replace *PID(S) when the
54     // --cmdline option is set.
55     SERVER_CMD,
56     CLIENT_CMDS,
57 };
58 
59 enum : unsigned int {
60     VINTF_INFO_EMPTY = 0,
61     DEVICE_MANIFEST = 1 << 0,
62     DEVICE_MATRIX = 1 << 1,
63     FRAMEWORK_MANIFEST = 1 << 2,
64     FRAMEWORK_MATRIX = 1 << 3,
65 };
66 using VintfInfo = unsigned int;
67 
68 enum {
69     NO_PID = -1,
70     NO_PTR = 0
71 };
72 
73 enum class ServiceStatus {
74     UNKNOWN, // For passthrough
75     ALIVE,
76     NON_RESPONSIVE, // registered but not respond to calls
77     DECLARED, // in VINTF manifest
78 };
79 std::string to_string(ServiceStatus s);
80 
81 struct TableEntry {
82     std::string interfaceName{};
83     vintf::Transport transport{vintf::Transport::EMPTY};
84     int32_t serverPid{NO_PID};
85     uint32_t threadUsage{0};
86     uint32_t threadCount{0};
87     std::string serverCmdline{};
88     uint64_t serverObjectAddress{NO_PTR};
89     Pids clientPids{};
90     std::vector<std::string> clientCmdlines{};
91     vintf::Arch arch{vintf::Arch::ARCH_EMPTY};
92     // empty: unknown, all zeros: unreleased, otherwise: released
93     std::string hash{};
94     Partition partition{Partition::UNKNOWN};
95     VintfInfo vintfInfo{VINTF_INFO_EMPTY};
96     // true iff hwbinder and service started
97     ServiceStatus serviceStatus{ServiceStatus::UNKNOWN};
98 
sortByInterfaceNameTableEntry99     static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) {
100         return a.interfaceName < b.interfaceName;
101     };
sortByServerPidTableEntry102     static bool sortByServerPid(const TableEntry &a, const TableEntry &b) {
103         return a.serverPid < b.serverPid;
104     };
105 
getThreadUsageTableEntry106     std::string getThreadUsage() const {
107         if (threadCount == 0) {
108             return "N/A";
109         }
110 
111         return std::to_string(threadUsage) + "/" + std::to_string(threadCount);
112     }
113 
114     std::string isReleased() const;
115 
116     std::string getVintfInfo() const;
117 
118     std::string getField(TableColumnType type) const;
119 
120     bool operator==(const TableEntry& other) const;
121     std::string to_string() const;
122 };
123 
124 using SelectedColumns = std::vector<TableColumnType>;
125 
126 class Table {
127 public:
128     using Entries = std::vector<TableEntry>;
129 
begin()130     Entries::iterator begin() { return mEntries.begin(); }
begin()131     Entries::const_iterator begin() const { return mEntries.begin(); }
end()132     Entries::iterator end() { return mEntries.end(); }
end()133     Entries::const_iterator end() const { return mEntries.end(); }
size()134     size_t size() const { return mEntries.size(); }
135 
add(TableEntry && entry)136     void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); }
137 
setSelectedColumns(const SelectedColumns & s)138     void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; }
getSelectedColumns()139     const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; }
140 
setDescription(std::string && d)141     void setDescription(std::string&& d) { mDescription = std::move(d); }
142 
143     // Write table content.
144     TextTable createTextTable(bool neat = true,
145         const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const;
146 
147 private:
148     std::string mDescription;
149     Entries mEntries;
150     SelectedColumns mSelectedColumns;
151 };
152 
153 using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>;
154 
155 class MergedTable {
156 public:
MergedTable(std::vector<const Table * > && tables)157     explicit MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {}
158     TextTable createTextTable();
159 private:
160     std::vector<const Table*> mTables;
161 };
162 
163 }  // namespace lshal
164 }  // namespace android
165