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 "IOEventLoop.h" 29 #include "RecordReadThread.h" 30 #include "event_attr.h" 31 #include "event_fd.h" 32 #include "event_type.h" 33 #include "perf_event.h" 34 #include "record.h" 35 36 namespace simpleperf { 37 38 constexpr double DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC = 1; 39 constexpr uint64_t DEFAULT_SAMPLE_FREQ_FOR_NONTRACEPOINT_EVENT = 4000; 40 constexpr uint64_t DEFAULT_SAMPLE_PERIOD_FOR_TRACEPOINT_EVENT = 1; 41 42 struct CounterInfo { 43 pid_t tid; 44 int cpu; 45 PerfCounter counter; 46 }; 47 48 struct CountersInfo { 49 uint32_t group_id; 50 std::string event_name; 51 std::string event_modifier; 52 std::vector<CounterInfo> counters; 53 }; 54 55 struct SampleSpeed { 56 // There are two ways to set sample speed: 57 // 1. sample_freq: take [sample_freq] samples every second. 58 // 2. sample_period: take one sample every [sample_period] events happen. 59 uint64_t sample_freq; 60 uint64_t sample_period; sample_freqSampleSpeed61 SampleSpeed(uint64_t freq = 0, uint64_t period = 0) : sample_freq(freq), sample_period(period) {} UseFreqSampleSpeed62 bool UseFreq() const { 63 // Only use one way to set sample speed. 64 CHECK_NE(sample_freq != 0u, sample_period != 0u); 65 return sample_freq != 0u; 66 } 67 }; 68 69 struct AddrFilter { 70 enum Type { 71 FILE_RANGE, 72 FILE_START, 73 FILE_STOP, 74 KERNEL_RANGE, 75 KERNEL_START, 76 KERNEL_STOP, 77 } type; 78 uint64_t addr; 79 uint64_t size; 80 std::string file_path; 81 AddrFilterAddrFilter82 AddrFilter(AddrFilter::Type type, uint64_t addr, uint64_t size, const std::string& file_path) 83 : type(type), addr(addr), size(size), file_path(file_path) {} 84 85 std::string ToString() const; 86 }; 87 88 // EventSelectionSet helps to monitor events. It is used in following steps: 89 // 1. Create an EventSelectionSet, and add event types to monitor by calling 90 // AddEventType() or AddEventGroup(). 91 // 2. Define how to monitor events by calling SetEnableOnExec(), SampleIdAll(), 92 // SetSampleFreq(), etc. 93 // 3. Start monitoring by calling OpenEventFilesForCpus() or 94 // OpenEventFilesForThreadsOnCpus(). If SetEnableOnExec() has been called 95 // in step 2, monitor will be delayed until the monitored thread calls 96 // exec(). 97 // 4. Read counters by calling ReadCounters(), or read mapped event records 98 // by calling MmapEventFiles(), PrepareToReadMmapEventData() and 99 // FinishReadMmapEventData(). 100 // 5. Stop monitoring automatically in the destructor of EventSelectionSet by 101 // closing perf event files. 102 103 class EventSelectionSet { 104 public: 105 EventSelectionSet(bool for_stat_cmd); 106 ~EventSelectionSet(); 107 empty()108 bool empty() const { return groups_.empty(); } 109 110 bool AddEventType(const std::string& event_name, size_t* group_id = nullptr); 111 bool AddEventGroup(const std::vector<std::string>& event_names, size_t* group_id = nullptr); 112 // For each sample generated for the existing event group, add counters for selected events. 113 bool AddCounters(const std::vector<std::string>& event_names); 114 std::vector<const EventType*> GetEvents() const; 115 std::vector<const EventType*> GetTracepointEvents() const; 116 bool ExcludeKernel() const; HasAuxTrace()117 bool HasAuxTrace() const { return has_aux_trace_; } 118 EventAttrIds GetEventAttrWithId() const; 119 std::unordered_map<uint64_t, std::string> GetEventNamesById() const; 120 121 void SetEnableOnExec(bool enable); 122 bool GetEnableOnExec(); 123 void SampleIdAll(); 124 void SetSampleSpeed(size_t group_id, const SampleSpeed& speed); 125 bool SetBranchSampling(uint64_t branch_sample_type); 126 void EnableFpCallChainSampling(); 127 bool EnableDwarfCallChainSampling(uint32_t dump_stack_size); 128 void SetInherit(bool enable); 129 void SetClockId(int clock_id); 130 bool NeedKernelSymbol() const; 131 void SetRecordNotExecutableMaps(bool record); 132 bool RecordNotExecutableMaps() const; 133 void EnableSwitchRecord(); 134 void WakeupPerSample(); SetAddrFilters(std::vector<AddrFilter> && filters)135 void SetAddrFilters(std::vector<AddrFilter>&& filters) { addr_filters_ = std::move(filters); } 136 bool SetTracepointFilter(const std::string& filter); 137 138 template <typename Collection = std::vector<pid_t>> AddMonitoredProcesses(const Collection & processes)139 void AddMonitoredProcesses(const Collection& processes) { 140 processes_.insert(processes.begin(), processes.end()); 141 } 142 143 template <typename Collection = std::vector<pid_t>> AddMonitoredThreads(const Collection & threads)144 void AddMonitoredThreads(const Collection& threads) { 145 threads_.insert(threads.begin(), threads.end()); 146 } 147 GetMonitoredProcesses()148 const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; } 149 GetMonitoredThreads()150 const std::set<pid_t>& GetMonitoredThreads() const { return threads_; } 151 ClearMonitoredTargets()152 void ClearMonitoredTargets() { 153 processes_.clear(); 154 threads_.clear(); 155 } 156 HasMonitoredTarget()157 bool HasMonitoredTarget() const { return !processes_.empty() || !threads_.empty(); } 158 GetIOEventLoop()159 IOEventLoop* GetIOEventLoop() { return loop_.get(); } 160 161 // If cpus = {}, monitor on all cpus, with a perf event file for each cpu. 162 // If cpus = {-1}, monitor on all cpus, with a perf event file shared by all cpus. 163 // Otherwise, monitor on selected cpus, with a perf event file for each cpu. 164 bool OpenEventFiles(const std::vector<int>& cpus); 165 bool ReadCounters(std::vector<CountersInfo>* counters); 166 bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages, size_t aux_buffer_size, 167 size_t record_buffer_size, bool allow_cutting_samples, bool exclude_perf); 168 bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback); 169 bool SyncKernelBuffer(); 170 bool FinishReadMmapEventData(); 171 void CloseEventFiles(); 172 GetRecordStat()173 const simpleperf::RecordStat& GetRecordStat() { return record_read_thread_->GetStat(); } 174 175 // Stop profiling if all monitored processes/threads don't exist. 176 bool StopWhenNoMoreTargets( 177 double check_interval_in_sec = DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC); 178 179 bool SetEnableEvents(bool enable); 180 181 private: 182 struct EventSelection { 183 EventTypeAndModifier event_type_modifier; 184 perf_event_attr event_attr; 185 std::vector<std::unique_ptr<EventFd>> event_fds; 186 // counters for event files closed for cpu hotplug events 187 std::vector<CounterInfo> hotplugged_counters; 188 std::vector<int> allowed_cpus; 189 std::string tracepoint_filter; 190 }; 191 typedef std::vector<EventSelection> EventSelectionGroup; 192 193 bool BuildAndCheckEventSelection(const std::string& event_name, bool first_event, 194 EventSelection* selection); 195 void UnionSampleType(); 196 bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu, 197 std::string* failed_event_type); 198 bool ApplyFilters(); 199 bool ApplyAddrFilters(); 200 bool ApplyTracepointFilters(); 201 bool ReadMmapEventData(bool with_time_limit); 202 203 bool CheckMonitoredTargets(); 204 bool HasSampler(); 205 206 const bool for_stat_cmd_; 207 208 std::vector<EventSelectionGroup> groups_; 209 std::set<pid_t> processes_; 210 std::set<pid_t> threads_; 211 212 std::unique_ptr<IOEventLoop> loop_; 213 std::function<bool(Record*)> record_callback_; 214 215 std::unique_ptr<simpleperf::RecordReadThread> record_read_thread_; 216 217 bool has_aux_trace_ = false; 218 std::vector<AddrFilter> addr_filters_; 219 220 DISALLOW_COPY_AND_ASSIGN(EventSelectionSet); 221 }; 222 223 bool IsBranchSamplingSupported(); 224 bool IsDwarfCallChainSamplingSupported(); 225 bool IsDumpingRegsForTracepointEventsSupported(); 226 bool IsSettingClockIdSupported(); 227 bool IsMmap2Supported(); 228 bool IsHardwareEventSupported(); 229 bool IsSwitchRecordSupported(); 230 231 } // namespace simpleperf 232 233 #endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_ 234