cmd_list.cpp revision ff7465c896ab90c162cbcf2b141c4cdd79bf437f
1/* 2 * Copyright (C) 2015 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#include <stdio.h> 18#include <map> 19#include <string> 20#include <vector> 21 22#include <android-base/logging.h> 23 24#include "command.h" 25#include "event_attr.h" 26#include "event_fd.h" 27#include "event_type.h" 28 29static void PrintEventTypesOfType(uint32_t type, const std::string& type_name, 30 const std::vector<EventType>& event_types) { 31 printf("List of %s:\n", type_name.c_str()); 32 for (auto& event_type : event_types) { 33 if (event_type.type == type && 34 IsEventAttrSupportedByKernel(CreateDefaultPerfEventAttr(event_type))) { 35 printf(" %s\n", event_type.name.c_str()); 36 } 37 } 38 printf("\n"); 39} 40 41class ListCommand : public Command { 42 public: 43 ListCommand() 44 : Command("list", "list available event types", 45 "Usage: simpleperf list [hw|sw|cache|tracepoint]\n" 46 " List all available perf events on this machine.\n") { 47 } 48 49 bool Run(const std::vector<std::string>& args) override; 50}; 51 52bool ListCommand::Run(const std::vector<std::string>& args) { 53 static std::map<std::string, std::pair<int, std::string>> type_map = { 54 {"hw", {PERF_TYPE_HARDWARE, "hardware events"}}, 55 {"sw", {PERF_TYPE_SOFTWARE, "software events"}}, 56 {"cache", {PERF_TYPE_HW_CACHE, "hw-cache events"}}, 57 {"tracepoint", {PERF_TYPE_TRACEPOINT, "tracepoint events"}}, 58 }; 59 60 std::vector<std::string> names; 61 if (args.empty()) { 62 for (auto& item : type_map) { 63 names.push_back(item.first); 64 } 65 } else { 66 for (auto& arg : args) { 67 if (type_map.find(arg) != type_map.end()) { 68 names.push_back(arg); 69 } else { 70 LOG(ERROR) << "unknown event type category: " << arg << ", try using \"help list\""; 71 return false; 72 } 73 } 74 } 75 76 auto& event_types = GetAllEventTypes(); 77 78 for (auto& name : names) { 79 auto it = type_map.find(name); 80 PrintEventTypesOfType(it->second.first, it->second.second, event_types); 81 } 82 return true; 83} 84 85void RegisterListCommand() { 86 RegisterCommand("list", [] { return std::unique_ptr<Command>(new ListCommand); }); 87} 88