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_SELECTION_SET_H_ 18 #define SIMPLE_PERF_EVENT_SELECTION_SET_H_ 19 20 #include <functional> 21 #include <map> 22 #include <set> 23 #include <unordered_map> 24 #include <vector> 25 26 #include <android-base/macros.h> 27 28 #include "event_attr.h" 29 #include "event_fd.h" 30 #include "event_type.h" 31 #include "InplaceSamplerClient.h" 32 #include "IOEventLoop.h" 33 #include "perf_event.h" 34 #include "record.h" 35 36 constexpr double DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC = 0.5; 37 constexpr double DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC = 1; 38 39 struct CounterInfo { 40 pid_t tid; 41 int cpu; 42 PerfCounter counter; 43 }; 44 45 struct CountersInfo { 46 uint32_t group_id; 47 std::string event_name; 48 std::string event_modifier; 49 std::vector<CounterInfo> counters; 50 }; 51 52 struct SampleSpeed { 53 // There are two ways to set sample speed: 54 // 1. sample_freq: take [sample_freq] samples every second. 55 // 2. sample_period: take one sample every [sample_period] events happen. 56 uint64_t sample_freq; 57 uint64_t sample_period; 58 SampleSpeed(uint64_t freq = 0, uint64_t period = 0) : sample_freq(freq), sample_period(period) {} 59 bool UseFreq() const { 60 // Only use one way to set sample speed. 61 CHECK_NE(sample_freq != 0u, sample_period != 0u); 62 return sample_freq != 0u; 63 } 64 }; 65 66 // EventSelectionSet helps to monitor events. It is used in following steps: 67 // 1. Create an EventSelectionSet, and add event types to monitor by calling 68 // AddEventType() or AddEventGroup(). 69 // 2. Define how to monitor events by calling SetEnableOnExec(), SampleIdAll(), 70 // SetSampleFreq(), etc. 71 // 3. Start monitoring by calling OpenEventFilesForCpus() or 72 // OpenEventFilesForThreadsOnCpus(). If SetEnableOnExec() has been called 73 // in step 2, monitor will be delayed until the monitored thread calls 74 // exec(). 75 // 4. Read counters by calling ReadCounters(), or read mapped event records 76 // by calling MmapEventFiles(), PrepareToReadMmapEventData() and 77 // FinishReadMmapEventData(). 78 // 5. Stop monitoring automatically in the destructor of EventSelectionSet by 79 // closing perf event files. 80 81 class EventSelectionSet { 82 public: 83 EventSelectionSet(bool for_stat_cmd) 84 : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(new IOEventLoop) {} 85 86 bool empty() const { return groups_.empty(); } 87 88 bool AddEventType(const std::string& event_name, size_t* group_id = nullptr); 89 bool AddEventGroup(const std::vector<std::string>& event_names, size_t* group_id = nullptr); 90 std::vector<const EventType*> GetEvents() const; 91 std::vector<const EventType*> GetTracepointEvents() const; 92 bool ExcludeKernel() const; 93 bool HasInplaceSampler() const; 94 std::vector<EventAttrWithId> GetEventAttrWithId() const; 95 96 void SetEnableOnExec(bool enable); 97 bool GetEnableOnExec(); 98 void SampleIdAll(); 99 void SetSampleSpeed(size_t group_id, const SampleSpeed& speed); 100 bool SetBranchSampling(uint64_t branch_sample_type); 101 void EnableFpCallChainSampling(); 102 bool EnableDwarfCallChainSampling(uint32_t dump_stack_size); 103 void SetInherit(bool enable); 104 void SetClockId(int clock_id); 105 bool NeedKernelSymbol() const; 106 107 void AddMonitoredProcesses(const std::set<pid_t>& processes) { 108 processes_.insert(processes.begin(), processes.end()); 109 } 110 111 void AddMonitoredThreads(const std::set<pid_t>& threads) { 112 threads_.insert(threads.begin(), threads.end()); 113 } 114 115 const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; } 116 117 const std::set<pid_t>& GetMonitoredThreads() const { return threads_; } 118 119 bool HasMonitoredTarget() const { 120 return !processes_.empty() || !threads_.empty(); 121 } 122 123 IOEventLoop* GetIOEventLoop() { 124 return loop_.get(); 125 } 126 127 bool OpenEventFiles(const std::vector<int>& on_cpus); 128 bool ReadCounters(std::vector<CountersInfo>* counters); 129 bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages); 130 bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback); 131 bool FinishReadMmapEventData(); 132 133 // If monitored_cpus is empty, monitor all cpus. 134 bool HandleCpuHotplugEvents(const std::vector<int>& monitored_cpus, 135 double check_interval_in_sec = 136 DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC); 137 138 // Stop profiling if all monitored processes/threads don't exist. 139 bool StopWhenNoMoreTargets(double check_interval_in_sec = 140 DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC); 141 142 private: 143 struct EventSelection { 144 EventTypeAndModifier event_type_modifier; 145 perf_event_attr event_attr; 146 std::vector<std::unique_ptr<EventFd>> event_fds; 147 std::vector<std::unique_ptr<InplaceSamplerClient>> inplace_samplers; 148 // counters for event files closed for cpu hotplug events 149 std::vector<CounterInfo> hotplugged_counters; 150 }; 151 typedef std::vector<EventSelection> EventSelectionGroup; 152 153 bool BuildAndCheckEventSelection(const std::string& event_name, 154 EventSelection* selection); 155 void UnionSampleType(); 156 bool IsUserSpaceSamplerGroup(EventSelectionGroup& group); 157 bool OpenUserSpaceSamplersOnGroup(EventSelectionGroup& group, 158 const std::map<pid_t, std::set<pid_t>>& process_map); 159 bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu, 160 std::string* failed_event_type); 161 162 bool MmapEventFiles(size_t mmap_pages, bool report_error); 163 bool ReadMmapEventData(); 164 165 bool DetectCpuHotplugEvents(); 166 bool HandleCpuOnlineEvent(int cpu); 167 bool HandleCpuOfflineEvent(int cpu); 168 bool CreateMappedBufferForCpu(int cpu); 169 bool CheckMonitoredTargets(); 170 bool HasSampler(); 171 172 const bool for_stat_cmd_; 173 174 std::vector<EventSelectionGroup> groups_; 175 std::set<pid_t> processes_; 176 std::set<pid_t> threads_; 177 size_t mmap_pages_; 178 179 std::unique_ptr<IOEventLoop> loop_; 180 std::function<bool(Record*)> record_callback_; 181 182 std::set<int> monitored_cpus_; 183 std::vector<int> online_cpus_; 184 185 // Records from all mapped buffers are stored in record_buffer_, each 186 // RecordBufferHead manages records read from one mapped buffer. Create 187 // record_buffer_heads_ and record_buffer_ here to avoid allocating them 188 // from heap each time calling ReadMmapEventData(). 189 struct RecordBufferHead { 190 size_t current_pos; // current position in record_buffer_ 191 size_t end_pos; // end position in record_buffer_ 192 perf_event_attr* attr; 193 uint64_t timestamp; 194 std::unique_ptr<Record> r; 195 }; 196 std::vector<RecordBufferHead> record_buffer_heads_; 197 std::vector<char> record_buffer_; 198 199 DISALLOW_COPY_AND_ASSIGN(EventSelectionSet); 200 }; 201 202 bool IsBranchSamplingSupported(); 203 bool IsDwarfCallChainSamplingSupported(); 204 bool IsDumpingRegsForTracepointEventsSupported(); 205 bool IsSettingClockIdSupported(); 206 207 #endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_ 208