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