• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (IsEventAttrSupportedByKernel(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   };
69 
70   std::vector<std::string> names;
71   if (args.empty()) {
72     for (auto& item : type_map) {
73       names.push_back(item.first);
74     }
75   } else {
76     for (auto& arg : args) {
77       if (type_map.find(arg) != type_map.end()) {
78         names.push_back(arg);
79       } else {
80         LOG(ERROR) << "unknown event type category: " << arg << ", try using \"help list\"";
81         return false;
82       }
83     }
84   }
85 
86   auto& event_types = GetAllEventTypes();
87 
88   for (auto& name : names) {
89     auto it = type_map.find(name);
90     PrintEventTypesOfType(it->second.first, it->second.second, event_types);
91   }
92   return true;
93 }
94 
RegisterListCommand()95 void RegisterListCommand() {
96   RegisterCommand("list", [] { return std::unique_ptr<Command>(new ListCommand); });
97 }
98