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 "environment.h"
26 #include "event_attr.h"
27 #include "event_fd.h"
28 #include "event_type.h"
29 
PrintEventTypesOfType(uint32_t type,const std::string & type_name,const std::vector<EventType> & event_types)30 static void PrintEventTypesOfType(uint32_t type, const std::string& type_name,
31                                   const std::vector<EventType>& event_types) {
32   printf("List of %s:\n", type_name.c_str());
33   for (auto& event_type : event_types) {
34     if (event_type.type == type) {
35       perf_event_attr attr = CreateDefaultPerfEventAttr(event_type);
36       // Exclude kernel to list supported events even when
37       // /proc/sys/kernel/perf_event_paranoid is 2.
38       attr.exclude_kernel = 1;
39       if (IsEventAttrSupported(attr)) {
40         printf("  %s\n", event_type.name.c_str());
41       }
42     }
43   }
44   printf("\n");
45 }
46 
47 class ListCommand : public Command {
48  public:
ListCommand()49   ListCommand()
50       : Command("list", "list available event types",
51                 "Usage: simpleperf list [hw|sw|cache|tracepoint]\n"
52                 "    List all available perf events on this machine.\n") {
53   }
54 
55   bool Run(const std::vector<std::string>& args) override;
56 };
57 
Run(const std::vector<std::string> & args)58 bool ListCommand::Run(const std::vector<std::string>& args) {
59   if (!CheckPerfEventLimit()) {
60     return false;
61   }
62 
63   static std::map<std::string, std::pair<int, std::string>> type_map = {
64       {"hw", {PERF_TYPE_HARDWARE, "hardware events"}},
65       {"sw", {PERF_TYPE_SOFTWARE, "software events"}},
66       {"cache", {PERF_TYPE_HW_CACHE, "hw-cache events"}},
67       {"tracepoint", {PERF_TYPE_TRACEPOINT, "tracepoint events"}},
68       {"user-space-sampler", {SIMPLEPERF_TYPE_USER_SPACE_SAMPLERS, "user-space samplers"}},
69   };
70 
71   std::vector<std::string> names;
72   if (args.empty()) {
73     for (auto& item : type_map) {
74       names.push_back(item.first);
75     }
76   } else {
77     for (auto& arg : args) {
78       if (type_map.find(arg) != type_map.end()) {
79         names.push_back(arg);
80       } else {
81         LOG(ERROR) << "unknown event type category: " << arg << ", try using \"help list\"";
82         return false;
83       }
84     }
85   }
86 
87   auto& event_types = GetAllEventTypes();
88 
89   for (auto& name : names) {
90     auto it = type_map.find(name);
91     PrintEventTypesOfType(it->second.first, it->second.second, event_types);
92   }
93   return true;
94 }
95 
RegisterListCommand()96 void RegisterListCommand() {
97   RegisterCommand("list", [] { return std::unique_ptr<Command>(new ListCommand); });
98 }
99