2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <android-base/logging.h>
25 #include "event_attr.h"
27 #include "event_type.h"
29 static 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());
41 class ListCommand : public Command {
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") {
49 bool Run(const std::vector<std::string>& args) override;
52 bool 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"}},
60 std::vector<std::string> names;
62 for (auto& item : type_map) {
63 names.push_back(item.first);
66 for (auto& arg : args) {
67 if (type_map.find(arg) != type_map.end()) {
70 LOG(ERROR) << "unknown event type category: " << arg << ", try using \"help list\"";
76 auto& event_types = GetAllEventTypes();
78 for (auto& name : names) {
79 auto it = type_map.find(name);
80 PrintEventTypesOfType(it->second.first, it->second.second, event_types);
85 void RegisterListCommand() {
86 RegisterCommand("list", [] { return std::unique_ptr<Command>(new ListCommand); });