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