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 } 58 59 std::vector<int> GetPmuCpumask(); 60 61 std::string name; 62 uint32_t type; 63 uint64_t config; 64 std::string description; 65 std::string limited_arch; 66 }; 67 68 // Used to temporarily change event types returned by GetAllEventTypes(). 69 class ScopedEventTypes { 70 public: 71 static std::string BuildString(const std::vector<const EventType*>& event_types); 72 73 ScopedEventTypes(const std::string& event_type_str); 74 ~ScopedEventTypes(); 75 }; 76 77 struct EventTypeAndModifier { 78 std::string name; 79 EventType event_type; 80 std::string modifier; 81 bool exclude_user; 82 bool exclude_kernel; 83 bool exclude_hv; 84 bool exclude_host; 85 bool exclude_guest; 86 int precise_ip : 2; 87 EventTypeAndModifierEventTypeAndModifier88 EventTypeAndModifier() 89 : exclude_user(false), 90 exclude_kernel(false), 91 exclude_hv(false), 92 exclude_host(false), 93 exclude_guest(false), 94 precise_ip(0) {} 95 }; 96 97 enum class EventFinderType; 98 class EventTypeFinder; 99 class RawTypeFinder; 100 class TracepointSystemFinder; 101 102 class EventTypeManager { 103 public: Instance()104 static EventTypeManager& Instance() { return instance_; } 105 ~EventTypeManager(); 106 107 bool ReadTracepointsFromFile(const std::string& filepath); 108 bool WriteTracepointsToFile(const std::string& filepath); 109 110 // Iterate through all event types, and stop when callback returns false. 111 bool ForEachType(const std::function<bool(const EventType&)>& callback); 112 const EventType* FindType(const std::string& name); 113 const EventType* AddRawType(const std::string& name); 114 void RemoveProbeType(const std::string& name); GetScopedFinder()115 const EventTypeFinder* GetScopedFinder() { return scoped_finder_.get(); } 116 void SetScopedFinder(std::unique_ptr<EventTypeFinder>&& finder); 117 118 private: 119 EventTypeManager(); 120 std::unique_ptr<EventTypeFinder>& GetFinder(EventFinderType type); 121 RawTypeFinder& GetRawTypeFinder(); 122 TracepointSystemFinder& GetTracepointSystemFinder(); 123 124 static EventTypeManager instance_; 125 126 std::vector<std::unique_ptr<EventTypeFinder>> type_finders_; 127 std::unique_ptr<EventTypeFinder> scoped_finder_; 128 }; 129 130 const EventType* FindEventTypeByName(const std::string& name, bool report_error = true); 131 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str); 132 bool IsEtmEventType(uint32_t type); 133 134 } // namespace simpleperf 135 136 #endif // SIMPLE_PERF_EVENT_H_ 137