blob: 04dfb8041fe8e8c19b247928e948be53eae6918e [file] [log] [blame]
Yifan Hongb0dde932017-02-10 17:49:58 -08001/*
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 Hong38d53e02017-02-13 17:51:59 -080024#include <iostream>
Yifan Hongb0dde932017-02-10 17:49:58 -080025
Yifan Hongf31aa052018-02-02 15:17:51 -080026#include <procpartition/procpartition.h>
Yifan Hong8304e412018-05-25 15:05:36 -070027#include <vintf/Transport.h>
Yifan Hongf31aa052018-02-02 15:17:51 -080028
Yifan Hongd4a77e82017-09-06 19:40:24 -070029#include "TextTable.h"
30
Yifan Hongb0dde932017-02-10 17:49:58 -080031namespace android {
32namespace lshal {
33
Yifan Hongf31aa052018-02-02 15:17:51 -080034using android::procpartition::Partition;
Yifan Hongb0dde932017-02-10 17:49:58 -080035using Pids = std::vector<int32_t>;
36
Yifan Hong4b865492017-02-28 19:38:24 -080037enum : unsigned int {
38 HWSERVICEMANAGER_LIST, // through defaultServiceManager()->list()
39 PTSERVICEMANAGER_REG_CLIENT, // through registerPassthroughClient
40 LIST_DLLIB, // through listing dynamic libraries
41};
42using TableEntrySource = unsigned int;
43
Yifan Hongb4479022017-03-02 16:54:11 -080044enum : unsigned int {
45 ARCH_UNKNOWN = 0,
Yifan Hong3fdbd9f2017-03-08 14:01:58 -080046 ARCH32 = 1 << 0,
47 ARCH64 = 1 << 1,
Yifan Hongb4479022017-03-02 16:54:11 -080048 ARCH_BOTH = ARCH32 | ARCH64
49};
50using Architecture = unsigned int;
51
Yifan Hongd4a77e82017-09-06 19:40:24 -070052enum class TableColumnType : unsigned int {
53 INTERFACE_NAME,
54 TRANSPORT,
55 SERVER_PID,
56 SERVER_CMD,
57 SERVER_ADDR,
58 CLIENT_PIDS,
59 CLIENT_CMDS,
60 ARCH,
61 THREADS,
Yifan Hongfee209d2017-09-14 18:23:38 -070062 RELEASED,
63 HASH,
Yifan Hongd4a77e82017-09-06 19:40:24 -070064};
65
Yifan Hong22ea7b82017-09-14 18:07:43 -070066enum {
67 NO_PID = -1,
68 NO_PTR = 0
69};
70
Yifan Hongb0dde932017-02-10 17:49:58 -080071struct TableEntry {
Yifan Hong22ea7b82017-09-14 18:07:43 -070072 std::string interfaceName{};
Yifan Hong8304e412018-05-25 15:05:36 -070073 vintf::Transport transport{vintf::Transport::EMPTY};
Yifan Hong22ea7b82017-09-14 18:07:43 -070074 int32_t serverPid{NO_PID};
75 uint32_t threadUsage{0};
76 uint32_t threadCount{0};
77 std::string serverCmdline{};
78 uint64_t serverObjectAddress{NO_PTR};
79 Pids clientPids{};
80 std::vector<std::string> clientCmdlines{};
81 Architecture arch{ARCH_UNKNOWN};
Yifan Hongfee209d2017-09-14 18:23:38 -070082 // empty: unknown, all zeros: unreleased, otherwise: released
83 std::string hash{};
Yifan Hongf31aa052018-02-02 15:17:51 -080084 Partition partition{Partition::UNKNOWN};
Yifan Hong38d53e02017-02-13 17:51:59 -080085
86 static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) {
87 return a.interfaceName < b.interfaceName;
88 };
89 static bool sortByServerPid(const TableEntry &a, const TableEntry &b) {
90 return a.serverPid < b.serverPid;
91 };
Steven Morelandd8e20192017-05-24 11:23:08 -070092
93 std::string getThreadUsage() const {
94 if (threadCount == 0) {
95 return "N/A";
96 }
97
98 return std::to_string(threadUsage) + "/" + std::to_string(threadCount);
99 }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700100
Yifan Hongfee209d2017-09-14 18:23:38 -0700101 std::string isReleased() const;
102
Yifan Hongd4a77e82017-09-06 19:40:24 -0700103 std::string getField(TableColumnType type) const;
Yifan Hong8bf73162017-09-07 18:06:13 -0700104
105 bool operator==(const TableEntry& other) const;
106 std::string to_string() const;
Yifan Hongb0dde932017-02-10 17:49:58 -0800107};
108
Yifan Hongd4a77e82017-09-06 19:40:24 -0700109using SelectedColumns = std::vector<TableColumnType>;
Yifan Honga3b87092017-03-02 19:19:29 -0800110
Yifan Hongd4a77e82017-09-06 19:40:24 -0700111class Table {
112public:
113 using Entries = std::vector<TableEntry>;
114
115 Entries::iterator begin() { return mEntries.begin(); }
116 Entries::const_iterator begin() const { return mEntries.begin(); }
117 Entries::iterator end() { return mEntries.end(); }
118 Entries::const_iterator end() const { return mEntries.end(); }
Yifan Hong8bf73162017-09-07 18:06:13 -0700119 size_t size() const { return mEntries.size(); }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700120
121 void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); }
122
123 void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; }
124 const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; }
125
126 void setDescription(std::string&& d) { mDescription = std::move(d); }
127
128 // Write table content.
129 TextTable createTextTable(bool neat = true,
130 const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const;
131
132private:
133 std::string mDescription;
134 Entries mEntries;
135 SelectedColumns mSelectedColumns;
Yifan Honga3b87092017-03-02 19:19:29 -0800136};
137
Yifan Hong38d53e02017-02-13 17:51:59 -0800138using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>;
139
Yifan Hongd4a77e82017-09-06 19:40:24 -0700140class MergedTable {
141public:
142 MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {}
143 TextTable createTextTable();
144private:
145 std::vector<const Table*> mTables;
Yifan Hong38d53e02017-02-13 17:51:59 -0800146};
147
Yifan Hongb0dde932017-02-10 17:49:58 -0800148} // namespace lshal
149} // namespace android
150
151#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_TABLE_ENTRY_H_