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 #ifndef SIMPLE_PERF_EVENT_H_
18 #define SIMPLE_PERF_EVENT_H_
19 
20 #include <stdint.h>
21 #include <strings.h>
22 #include <memory>
23 #include <set>
24 #include <string>
25 #include <vector>
26 
27 namespace simpleperf {
28 
29 inline const std::string kETMEventName = "cs-etm";
30 
31 // EventType represents one type of event, like cpu_cycle_event, cache_misses_event.
32 // The user knows one event type by its name, and the kernel knows one event type by its
33 // (type, config) pair. EventType connects the two representations, and tells the user if
34 // the event type is supported by the kernel.
35 
36 struct EventType {
EventTypeEventType37   EventType(const std::string& name, uint32_t type, uint64_t config, const std::string& description,
38             const std::string& limited_arch)
39       : name(name),
40         type(type),
41         config(config),
42         description(description),
43         limited_arch(limited_arch) {}
44 
EventTypeEventType45   EventType() : type(0), config(0) {}
46 
47   bool operator<(const EventType& other) const {
48     return strcasecmp(name.c_str(), other.name.c_str()) < 0;
49   }
50 
IsPmuEventEventType51   bool IsPmuEvent() const { return name.find('/') != std::string::npos; }
IsEtmEventEventType52   bool IsEtmEvent() const { return name == kETMEventName; }
53 
54   std::vector<int> GetPmuCpumask();
55 
56   std::string name;
57   uint32_t type;
58   uint64_t config;
59   std::string description;
60   std::string limited_arch;
61 };
62 
63 // Used to temporarily change event types returned by GetAllEventTypes().
64 class ScopedEventTypes {
65  public:
66   static std::string BuildString(const std::vector<const EventType*>& event_types);
67 
68   ScopedEventTypes(const std::string& event_type_str);
69   ~ScopedEventTypes();
70 };
71 
72 struct EventTypeAndModifier {
73   std::string name;
74   EventType event_type;
75   std::string modifier;
76   bool exclude_user;
77   bool exclude_kernel;
78   bool exclude_hv;
79   bool exclude_host;
80   bool exclude_guest;
81   int precise_ip : 2;
82 
EventTypeAndModifierEventTypeAndModifier83   EventTypeAndModifier()
84       : exclude_user(false),
85         exclude_kernel(false),
86         exclude_hv(false),
87         exclude_host(false),
88         exclude_guest(false),
89         precise_ip(0) {}
90 };
91 
92 enum class EventFinderType;
93 class EventTypeFinder;
94 class RawTypeFinder;
95 class TracepointSystemFinder;
96 
97 class EventTypeManager {
98  public:
Instance()99   static EventTypeManager& Instance() { return instance_; }
100   ~EventTypeManager();
101 
102   bool ReadTracepointsFromFile(const std::string& filepath);
103   bool WriteTracepointsToFile(const std::string& filepath);
104 
105   // Iterate through all event types, and stop when callback returns false.
106   bool ForEachType(const std::function<bool(const EventType&)>& callback);
107   const EventType* FindType(const std::string& name);
108   const EventType* AddRawType(const std::string& name);
109   void RemoveProbeType(const std::string& name);
GetScopedFinder()110   const EventTypeFinder* GetScopedFinder() { return scoped_finder_.get(); }
111   void SetScopedFinder(std::unique_ptr<EventTypeFinder>&& finder);
112 
113  private:
114   EventTypeManager();
115   std::unique_ptr<EventTypeFinder>& GetFinder(EventFinderType type);
116   RawTypeFinder& GetRawTypeFinder();
117   TracepointSystemFinder& GetTracepointSystemFinder();
118 
119   static EventTypeManager instance_;
120 
121   std::vector<std::unique_ptr<EventTypeFinder>> type_finders_;
122   std::unique_ptr<EventTypeFinder> scoped_finder_;
123 };
124 
125 const EventType* FindEventTypeByName(const std::string& name, bool report_error = true);
126 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str);
127 bool IsEtmEventType(uint32_t type);
128 
129 }  // namespace simpleperf
130 
131 #endif  // SIMPLE_PERF_EVENT_H_
132