1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h> 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #include <string> 22*38e8c45fSAndroid Build Coastguard Worker #include <vector> 23*38e8c45fSAndroid Build Coastguard Worker #include <iostream> 24*38e8c45fSAndroid Build Coastguard Worker 25*38e8c45fSAndroid Build Coastguard Worker #include <procpartition/procpartition.h> 26*38e8c45fSAndroid Build Coastguard Worker #include <vintf/Arch.h> 27*38e8c45fSAndroid Build Coastguard Worker #include <vintf/Transport.h> 28*38e8c45fSAndroid Build Coastguard Worker 29*38e8c45fSAndroid Build Coastguard Worker #include "TextTable.h" 30*38e8c45fSAndroid Build Coastguard Worker 31*38e8c45fSAndroid Build Coastguard Worker namespace android { 32*38e8c45fSAndroid Build Coastguard Worker namespace lshal { 33*38e8c45fSAndroid Build Coastguard Worker 34*38e8c45fSAndroid Build Coastguard Worker using android::procpartition::Partition; 35*38e8c45fSAndroid Build Coastguard Worker using Pids = std::vector<pid_t>; 36*38e8c45fSAndroid Build Coastguard Worker 37*38e8c45fSAndroid Build Coastguard Worker enum class TableColumnType : unsigned int { 38*38e8c45fSAndroid Build Coastguard Worker INTERFACE_NAME = 0, 39*38e8c45fSAndroid Build Coastguard Worker TRANSPORT, 40*38e8c45fSAndroid Build Coastguard Worker SERVER_PID, 41*38e8c45fSAndroid Build Coastguard Worker SERVER_ADDR, 42*38e8c45fSAndroid Build Coastguard Worker ARCH, 43*38e8c45fSAndroid Build Coastguard Worker THREADS, 44*38e8c45fSAndroid Build Coastguard Worker RELEASED, 45*38e8c45fSAndroid Build Coastguard Worker HASH, 46*38e8c45fSAndroid Build Coastguard Worker VINTF, 47*38e8c45fSAndroid Build Coastguard Worker SERVICE_STATUS, 48*38e8c45fSAndroid Build Coastguard Worker CLIENT_PIDS, 49*38e8c45fSAndroid Build Coastguard Worker 50*38e8c45fSAndroid Build Coastguard Worker // Not a real TableColumnType. Used to determine all TableColumnTypes. 51*38e8c45fSAndroid Build Coastguard Worker LAST, 52*38e8c45fSAndroid Build Coastguard Worker 53*38e8c45fSAndroid Build Coastguard Worker // Not included in all TableColumnTypes because they replace *PID(S) when the 54*38e8c45fSAndroid Build Coastguard Worker // --cmdline option is set. 55*38e8c45fSAndroid Build Coastguard Worker SERVER_CMD, 56*38e8c45fSAndroid Build Coastguard Worker CLIENT_CMDS, 57*38e8c45fSAndroid Build Coastguard Worker }; 58*38e8c45fSAndroid Build Coastguard Worker 59*38e8c45fSAndroid Build Coastguard Worker enum : unsigned int { 60*38e8c45fSAndroid Build Coastguard Worker VINTF_INFO_EMPTY = 0, 61*38e8c45fSAndroid Build Coastguard Worker DEVICE_MANIFEST = 1 << 0, 62*38e8c45fSAndroid Build Coastguard Worker DEVICE_MATRIX = 1 << 1, 63*38e8c45fSAndroid Build Coastguard Worker FRAMEWORK_MANIFEST = 1 << 2, 64*38e8c45fSAndroid Build Coastguard Worker FRAMEWORK_MATRIX = 1 << 3, 65*38e8c45fSAndroid Build Coastguard Worker }; 66*38e8c45fSAndroid Build Coastguard Worker using VintfInfo = unsigned int; 67*38e8c45fSAndroid Build Coastguard Worker 68*38e8c45fSAndroid Build Coastguard Worker enum { 69*38e8c45fSAndroid Build Coastguard Worker NO_PID = -1, 70*38e8c45fSAndroid Build Coastguard Worker NO_PTR = 0 71*38e8c45fSAndroid Build Coastguard Worker }; 72*38e8c45fSAndroid Build Coastguard Worker 73*38e8c45fSAndroid Build Coastguard Worker enum class ServiceStatus { 74*38e8c45fSAndroid Build Coastguard Worker UNKNOWN, // For passthrough 75*38e8c45fSAndroid Build Coastguard Worker ALIVE, 76*38e8c45fSAndroid Build Coastguard Worker NON_RESPONSIVE, // registered but not respond to calls 77*38e8c45fSAndroid Build Coastguard Worker DECLARED, // in VINTF manifest 78*38e8c45fSAndroid Build Coastguard Worker }; 79*38e8c45fSAndroid Build Coastguard Worker std::string to_string(ServiceStatus s); 80*38e8c45fSAndroid Build Coastguard Worker 81*38e8c45fSAndroid Build Coastguard Worker struct TableEntry { 82*38e8c45fSAndroid Build Coastguard Worker std::string interfaceName{}; 83*38e8c45fSAndroid Build Coastguard Worker vintf::Transport transport{vintf::Transport::EMPTY}; 84*38e8c45fSAndroid Build Coastguard Worker int32_t serverPid{NO_PID}; 85*38e8c45fSAndroid Build Coastguard Worker uint32_t threadUsage{0}; 86*38e8c45fSAndroid Build Coastguard Worker uint32_t threadCount{0}; 87*38e8c45fSAndroid Build Coastguard Worker std::string serverCmdline{}; 88*38e8c45fSAndroid Build Coastguard Worker uint64_t serverObjectAddress{NO_PTR}; 89*38e8c45fSAndroid Build Coastguard Worker Pids clientPids{}; 90*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> clientCmdlines{}; 91*38e8c45fSAndroid Build Coastguard Worker vintf::Arch arch{vintf::Arch::ARCH_EMPTY}; 92*38e8c45fSAndroid Build Coastguard Worker // empty: unknown, all zeros: unreleased, otherwise: released 93*38e8c45fSAndroid Build Coastguard Worker std::string hash{}; 94*38e8c45fSAndroid Build Coastguard Worker Partition partition{Partition::UNKNOWN}; 95*38e8c45fSAndroid Build Coastguard Worker VintfInfo vintfInfo{VINTF_INFO_EMPTY}; 96*38e8c45fSAndroid Build Coastguard Worker // true iff hwbinder and service started 97*38e8c45fSAndroid Build Coastguard Worker ServiceStatus serviceStatus{ServiceStatus::UNKNOWN}; 98*38e8c45fSAndroid Build Coastguard Worker sortByInterfaceNameTableEntry99*38e8c45fSAndroid Build Coastguard Worker static bool sortByInterfaceName(const TableEntry &a, const TableEntry &b) { 100*38e8c45fSAndroid Build Coastguard Worker return a.interfaceName < b.interfaceName; 101*38e8c45fSAndroid Build Coastguard Worker }; sortByServerPidTableEntry102*38e8c45fSAndroid Build Coastguard Worker static bool sortByServerPid(const TableEntry &a, const TableEntry &b) { 103*38e8c45fSAndroid Build Coastguard Worker return a.serverPid < b.serverPid; 104*38e8c45fSAndroid Build Coastguard Worker }; 105*38e8c45fSAndroid Build Coastguard Worker getThreadUsageTableEntry106*38e8c45fSAndroid Build Coastguard Worker std::string getThreadUsage() const { 107*38e8c45fSAndroid Build Coastguard Worker if (threadCount == 0) { 108*38e8c45fSAndroid Build Coastguard Worker return "N/A"; 109*38e8c45fSAndroid Build Coastguard Worker } 110*38e8c45fSAndroid Build Coastguard Worker 111*38e8c45fSAndroid Build Coastguard Worker return std::to_string(threadUsage) + "/" + std::to_string(threadCount); 112*38e8c45fSAndroid Build Coastguard Worker } 113*38e8c45fSAndroid Build Coastguard Worker 114*38e8c45fSAndroid Build Coastguard Worker std::string isReleased() const; 115*38e8c45fSAndroid Build Coastguard Worker 116*38e8c45fSAndroid Build Coastguard Worker std::string getVintfInfo() const; 117*38e8c45fSAndroid Build Coastguard Worker 118*38e8c45fSAndroid Build Coastguard Worker std::string getField(TableColumnType type) const; 119*38e8c45fSAndroid Build Coastguard Worker 120*38e8c45fSAndroid Build Coastguard Worker bool operator==(const TableEntry& other) const; 121*38e8c45fSAndroid Build Coastguard Worker std::string to_string() const; 122*38e8c45fSAndroid Build Coastguard Worker }; 123*38e8c45fSAndroid Build Coastguard Worker 124*38e8c45fSAndroid Build Coastguard Worker using SelectedColumns = std::vector<TableColumnType>; 125*38e8c45fSAndroid Build Coastguard Worker 126*38e8c45fSAndroid Build Coastguard Worker class Table { 127*38e8c45fSAndroid Build Coastguard Worker public: 128*38e8c45fSAndroid Build Coastguard Worker using Entries = std::vector<TableEntry>; 129*38e8c45fSAndroid Build Coastguard Worker begin()130*38e8c45fSAndroid Build Coastguard Worker Entries::iterator begin() { return mEntries.begin(); } begin()131*38e8c45fSAndroid Build Coastguard Worker Entries::const_iterator begin() const { return mEntries.begin(); } end()132*38e8c45fSAndroid Build Coastguard Worker Entries::iterator end() { return mEntries.end(); } end()133*38e8c45fSAndroid Build Coastguard Worker Entries::const_iterator end() const { return mEntries.end(); } size()134*38e8c45fSAndroid Build Coastguard Worker size_t size() const { return mEntries.size(); } 135*38e8c45fSAndroid Build Coastguard Worker add(TableEntry && entry)136*38e8c45fSAndroid Build Coastguard Worker void add(TableEntry&& entry) { mEntries.push_back(std::move(entry)); } 137*38e8c45fSAndroid Build Coastguard Worker setSelectedColumns(const SelectedColumns & s)138*38e8c45fSAndroid Build Coastguard Worker void setSelectedColumns(const SelectedColumns& s) { mSelectedColumns = s; } getSelectedColumns()139*38e8c45fSAndroid Build Coastguard Worker const SelectedColumns& getSelectedColumns() const { return mSelectedColumns; } 140*38e8c45fSAndroid Build Coastguard Worker setDescription(std::string && d)141*38e8c45fSAndroid Build Coastguard Worker void setDescription(std::string&& d) { mDescription = std::move(d); } 142*38e8c45fSAndroid Build Coastguard Worker 143*38e8c45fSAndroid Build Coastguard Worker // Write table content. 144*38e8c45fSAndroid Build Coastguard Worker TextTable createTextTable(bool neat = true, 145*38e8c45fSAndroid Build Coastguard Worker const std::function<std::string(const std::string&)>& emitDebugInfo = nullptr) const; 146*38e8c45fSAndroid Build Coastguard Worker 147*38e8c45fSAndroid Build Coastguard Worker private: 148*38e8c45fSAndroid Build Coastguard Worker std::string mDescription; 149*38e8c45fSAndroid Build Coastguard Worker Entries mEntries; 150*38e8c45fSAndroid Build Coastguard Worker SelectedColumns mSelectedColumns; 151*38e8c45fSAndroid Build Coastguard Worker }; 152*38e8c45fSAndroid Build Coastguard Worker 153*38e8c45fSAndroid Build Coastguard Worker using TableEntryCompare = std::function<bool(const TableEntry &, const TableEntry &)>; 154*38e8c45fSAndroid Build Coastguard Worker 155*38e8c45fSAndroid Build Coastguard Worker class MergedTable { 156*38e8c45fSAndroid Build Coastguard Worker public: MergedTable(std::vector<const Table * > && tables)157*38e8c45fSAndroid Build Coastguard Worker explicit MergedTable(std::vector<const Table*>&& tables) : mTables(std::move(tables)) {} 158*38e8c45fSAndroid Build Coastguard Worker TextTable createTextTable(); 159*38e8c45fSAndroid Build Coastguard Worker private: 160*38e8c45fSAndroid Build Coastguard Worker std::vector<const Table*> mTables; 161*38e8c45fSAndroid Build Coastguard Worker }; 162*38e8c45fSAndroid Build Coastguard Worker 163*38e8c45fSAndroid Build Coastguard Worker } // namespace lshal 164*38e8c45fSAndroid Build Coastguard Worker } // namespace android 165