blob: 0ff0c96d38c80ff390e3c60b3cc1067fb5040586 [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
Steven Moreland0b8e3872019-06-25 08:54:34 -070017#pragma once
Yifan Hongb0dde932017-02-10 17:49:58 -080018
19#include <stdint.h>
20
21#include <string>
22#include <vector>
Yifan Hong38d53e02017-02-13 17:51:59 -080023#include <iostream>
Yifan Hongb0dde932017-02-10 17:49:58 -080024
Yifan Hongf31aa052018-02-02 15:17:51 -080025#include <procpartition/procpartition.h>
Yifan Hong0ad64f52018-05-25 15:29:17 -070026#include <vintf/Arch.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 Hongd4a77e82017-09-06 19:40:24 -070037enum class TableColumnType : unsigned int {
38 INTERFACE_NAME,
39 TRANSPORT,
40 SERVER_PID,
41 SERVER_CMD,
42 SERVER_ADDR,
43 CLIENT_PIDS,
44 CLIENT_CMDS,
45 ARCH,
46 THREADS,
Yifan Hongfee209d2017-09-14 18:23:38 -070047 RELEASED,
48 HASH,
Yifan Hongbdf44f82018-05-25 14:20:00 -070049 VINTF,
Yifan Hong13ba0a92018-06-25 16:15:56 -070050 SERVICE_STATUS,
Yifan Hongd4a77e82017-09-06 19:40:24 -070051};
52
Yifan Hongbdf44f82018-05-25 14:20:00 -070053enum : unsigned int {
54 VINTF_INFO_EMPTY = 0,
55 DEVICE_MANIFEST = 1 << 0,
56 DEVICE_MATRIX = 1 << 1,
57 FRAMEWORK_MANIFEST = 1 << 2,
58 FRAMEWORK_MATRIX = 1 << 3,
59};
60using VintfInfo = unsigned int;
61
Yifan Hong22ea7b82017-09-14 18:07:43 -070062enum {
63 NO_PID = -1,
64 NO_PTR = 0
65};
66
Yifan Hong13ba0a92018-06-25 16:15:56 -070067enum class ServiceStatus {
68 UNKNOWN, // For passthrough
69 ALIVE,
70 NON_RESPONSIVE, // registered but not respond to calls
71 DECLARED, // in VINTF manifest
72};
73std::string to_string(ServiceStatus s);
74
Yifan Hongb0dde932017-02-10 17:49:58 -080075struct TableEntry {
Yifan Hong22ea7b82017-09-14 18:07:43 -070076 std::string interfaceName{};
Yifan Hong8304e412018-05-25 15:05:36 -070077 vintf::Transport transport{vintf::Transport::EMPTY};
Yifan Hong22ea7b82017-09-14 18:07:43 -070078 int32_t serverPid{NO_PID};
79 uint32_t threadUsage{0};
80 uint32_t threadCount{0};
81 std::string serverCmdline{};
82 uint64_t serverObjectAddress{NO_PTR};
83 Pids clientPids{};
84 std::vector<std::string> clientCmdlines{};
Yifan Hong0ad64f52018-05-25 15:29:17 -070085 vintf::Arch arch{vintf::Arch::ARCH_EMPTY};
Yifan Hongfee209d2017-09-14 18:23:38 -070086 // empty: unknown, all zeros: unreleased, otherwise: released
87 std::string hash{};
Yifan Hongf31aa052018-02-02 15:17:51 -080088 Partition partition{Partition::UNKNOWN};
Yifan Hongbdf44f82018-05-25 14:20:00 -070089 VintfInfo vintfInfo{VINTF_INFO_EMPTY};
Yifan Hong13ba0a92018-06-25 16:15:56 -070090 // true iff hwbinder and service started
91 ServiceStatus serviceStatus{ServiceStatus::UNKNOWN};
Yifan Hong38d53e02017-02-13 17:51:59 -080092
93 static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) {
94 return a.interfaceName < b.interfaceName;
95 };
96 static bool sortByServerPid(const TableEntry &a, const TableEntry &b) {
97 return a.serverPid < b.serverPid;
98 };
Steven Morelandd8e20192017-05-24 11:23:08 -070099
100 std::string getThreadUsage() const {
101 if (threadCount == 0) {
102 return "N/A";
103 }
104
105 return std::to_string(threadUsage) + "/" + std::to_string(threadCount);
106 }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700107
Yifan Hongfee209d2017-09-14 18:23:38 -0700108 std::string isReleased() const;
109
Yifan Hongbdf44f82018-05-25 14:20:00 -0700110 std::string getVintfInfo() const;
111
Yifan Hongd4a77e82017-09-06 19:40:24 -0700112 std::string getField(TableColumnType type) const;
Yifan Hong8bf73162017-09-07 18:06:13 -0700113
114 bool operator==(const TableEntry& other) const;
115 std::string to_string() const;
Yifan Hongb0dde932017-02-10 17:49:58 -0800116};
117
Yifan Hongd4a77e82017-09-06 19:40:24 -0700118using SelectedColumns = std::vector<TableColumnType>;
Yifan Honga3b87092017-03-02 19:19:29 -0800119
Yifan Hongd4a77e82017-09-06 19:40:24 -0700120class Table {
121public:
122 using Entries = std::vector<TableEntry>;
123
124 Entries::iterator begin() { return mEntries.begin(); }
125 Entries::const_iterator begin() const { return mEntries.begin(); }
126 Entries::iterator end() { return mEntries.end(); }
127 Entries::const_iterator end() const { return mEntries.end(); }
Yifan Hong8bf73162017-09-07 18:06:13 -0700128 size_t size() const { return mEntries.size(); }
Yifan Hongd4a77e82017-09-06 19:40:24 -0700129
130 void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); }
131
132 void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; }
133 const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; }
134
135 void setDescription(std::string&& d) { mDescription = std::move(d); }
136
137 // Write table content.
138 TextTable createTextTable(bool neat = true,
139 const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const;
140
141private:
142 std::string mDescription;
143 Entries mEntries;
144 SelectedColumns mSelectedColumns;
Yifan Honga3b87092017-03-02 19:19:29 -0800145};
146
Yifan Hong38d53e02017-02-13 17:51:59 -0800147using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>;
148
Yifan Hongd4a77e82017-09-06 19:40:24 -0700149class MergedTable {
150public:
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -0800151 explicit MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {}
Yifan Hongd4a77e82017-09-06 19:40:24 -0700152 TextTable createTextTable();
153private:
154 std::vector<const Table*> mTables;
Yifan Hong38d53e02017-02-13 17:51:59 -0800155};
156
Yifan Hongb0dde932017-02-10 17:49:58 -0800157} // namespace lshal
158} // namespace android