blob: 497bedf12d68ba5837ec9adaac8bc043936ab919 [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 Hongd4a77e82017-09-06 19:40:24 -070026#include "TextTable.h"
27
Yifan Hongb0dde932017-02-10 17:49:58 -080028namespace android {
29namespace lshal {
30
31using Pids = std::vector<int32_t>;
32
Yifan Hong4b865492017-02-28 19:38:24 -080033enum : unsigned int {
34 HWSERVICEMANAGER_LIST, // through defaultServiceManager()->list()
35 PTSERVICEMANAGER_REG_CLIENT, // through registerPassthroughClient
36 LIST_DLLIB, // through listing dynamic libraries
37};
38using TableEntrySource = unsigned int;
39
Yifan Hongb4479022017-03-02 16:54:11 -080040enum : unsigned int {
41 ARCH_UNKNOWN = 0,
Yifan Hong3fdbd9f2017-03-08 14:01:58 -080042 ARCH32 = 1 << 0,
43 ARCH64 = 1 << 1,
Yifan Hongb4479022017-03-02 16:54:11 -080044 ARCH_BOTH = ARCH32 | ARCH64
45};
46using Architecture = unsigned int;
47
Yifan Hongd4a77e82017-09-06 19:40:24 -070048enum 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,
58};
59
Yifan Hong22ea7b82017-09-14 18:07:43 -070060enum {
61 NO_PID = -1,
62 NO_PTR = 0
63};
64
Yifan Hongb0dde932017-02-10 17:49:58 -080065struct TableEntry {
Yifan Hong22ea7b82017-09-14 18:07:43 -070066 std::string interfaceName{};
67 std::string transport{};
68 int32_t serverPid{NO_PID};
69 uint32_t threadUsage{0};
70 uint32_t threadCount{0};
71 std::string serverCmdline{};
72 uint64_t serverObjectAddress{NO_PTR};
73 Pids clientPids{};
74 std::vector<std::string> clientCmdlines{};
75 Architecture arch{ARCH_UNKNOWN};
Yifan Hong38d53e02017-02-13 17:51:59 -080076
77 static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) {
78 return a.interfaceName < b.interfaceName;
79 };
80 static bool sortByServerPid(const TableEntry &a, const TableEntry &b) {
81 return a.serverPid < b.serverPid;
82 };
Steven Morelandd8e20192017-05-24 11:23:08 -070083
84 std::string getThreadUsage() const {
85 if (threadCount == 0) {
86 return "N/A";
87 }
88
89 return std::to_string(threadUsage) + "/" + std::to_string(threadCount);
90 }
Yifan Hongd4a77e82017-09-06 19:40:24 -070091
92 std::string getField(TableColumnType type) const;
Yifan Hong8bf73162017-09-07 18:06:13 -070093
94 bool operator==(const TableEntry& other) const;
95 std::string to_string() const;
Yifan Hongb0dde932017-02-10 17:49:58 -080096};
97
Yifan Hongd4a77e82017-09-06 19:40:24 -070098using SelectedColumns = std::vector<TableColumnType>;
Yifan Honga3b87092017-03-02 19:19:29 -080099
Yifan Hongd4a77e82017-09-06 19:40:24 -0700100class Table {
101public:
102 using Entries = std::vector<TableEntry>;
103
104 Entries::iterator begin() { return mEntries.begin(); }
105 Entries::const_iterator begin() const { return mEntries.begin(); }
106 Entries::iterator end() { return mEntries.end(); }
107 Entries::const_iterator end() const { return mEntries.end(); }
Yifan Hong8bf73162017-09-07 18:06:13 -0700108 size_t size() const { return mEntries.size(); }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700109
110 void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); }
111
112 void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; }
113 const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; }
114
115 void setDescription(std::string&& d) { mDescription = std::move(d); }
116
117 // Write table content.
118 TextTable createTextTable(bool neat = true,
119 const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const;
120
121private:
122 std::string mDescription;
123 Entries mEntries;
124 SelectedColumns mSelectedColumns;
Yifan Honga3b87092017-03-02 19:19:29 -0800125};
126
Yifan Hong38d53e02017-02-13 17:51:59 -0800127using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>;
128
Yifan Hongd4a77e82017-09-06 19:40:24 -0700129class MergedTable {
130public:
131 MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {}
132 TextTable createTextTable();
133private:
134 std::vector<const Table*> mTables;
Yifan Hong38d53e02017-02-13 17:51:59 -0800135};
136
Yifan Hongb0dde932017-02-10 17:49:58 -0800137} // namespace lshal
138} // namespace android
139
140#endif // FRAMEWORK_NATIVE_CMDS_LSHAL_TABLE_ENTRY_H_