| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 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 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 22 | #include <set> |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 23 | #include <unordered_map> |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 27 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 28 | #include "event_attr.h" |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 29 | #include "event_fd.h" |
| Yabin Cui | d115b2f | 2015-06-15 13:57:23 -0700 | [diff] [blame] | 30 | #include "event_type.h" |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 31 | #include "InplaceSamplerClient.h" |
| 32 | #include "IOEventLoop.h" |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 33 | #include "perf_event.h" |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 34 | #include "record.h" |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 35 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 36 | constexpr double DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC = 0.5; |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 37 | constexpr double DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC = 1; |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 38 | |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 39 | struct CounterInfo { |
| 40 | pid_t tid; |
| 41 | int cpu; |
| 42 | PerfCounter counter; |
| 43 | }; |
| 44 | |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 45 | struct CountersInfo { |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 46 | uint32_t group_id; |
| 47 | std::string event_name; |
| 48 | std::string event_modifier; |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 49 | std::vector<CounterInfo> counters; |
| 50 | }; |
| 51 | |
| Yabin Cui | 20b49f8 | 2017-08-03 15:54:43 -0700 | [diff] [blame] | 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 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 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. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 80 | |
| 81 | class EventSelectionSet { |
| 82 | public: |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 83 | EventSelectionSet(bool for_stat_cmd) |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 84 | : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(new IOEventLoop) {} |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 85 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 86 | bool empty() const { return groups_.empty(); } |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 87 | |
| Yabin Cui | 20b49f8 | 2017-08-03 15:54:43 -0700 | [diff] [blame] | 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); |
| Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 90 | std::vector<const EventType*> GetEvents() const; |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 91 | std::vector<const EventType*> GetTracepointEvents() const; |
| Yabin Cui | d3cb3b0 | 2017-07-24 14:59:46 -0700 | [diff] [blame] | 92 | bool ExcludeKernel() const; |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 93 | bool HasInplaceSampler() const; |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 94 | std::vector<EventAttrWithId> GetEventAttrWithId() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 95 | |
| Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 96 | void SetEnableOnExec(bool enable); |
| 97 | bool GetEnableOnExec(); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 98 | void SampleIdAll(); |
| Yabin Cui | 20b49f8 | 2017-08-03 15:54:43 -0700 | [diff] [blame] | 99 | void SetSampleSpeed(size_t group_id, const SampleSpeed& speed); |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 100 | bool SetBranchSampling(uint64_t branch_sample_type); |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 101 | void EnableFpCallChainSampling(); |
| 102 | bool EnableDwarfCallChainSampling(uint32_t dump_stack_size); |
| Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 103 | void SetInherit(bool enable); |
| Yabin Cui | 6d4959c | 2017-09-01 15:40:41 -0700 | [diff] [blame] | 104 | void SetClockId(int clock_id); |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 105 | bool NeedKernelSymbol() const; |
| Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 106 | void SetRecordNotExecutableMaps(bool record); |
| 107 | bool RecordNotExecutableMaps() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 108 | |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 109 | void AddMonitoredProcesses(const std::set<pid_t>& processes) { |
| 110 | processes_.insert(processes.begin(), processes.end()); |
| 111 | } |
| 112 | |
| 113 | void AddMonitoredThreads(const std::set<pid_t>& threads) { |
| 114 | threads_.insert(threads.begin(), threads.end()); |
| 115 | } |
| 116 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 117 | const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; } |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 118 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 119 | const std::set<pid_t>& GetMonitoredThreads() const { return threads_; } |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 120 | |
| 121 | bool HasMonitoredTarget() const { |
| 122 | return !processes_.empty() || !threads_.empty(); |
| 123 | } |
| 124 | |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 125 | IOEventLoop* GetIOEventLoop() { |
| 126 | return loop_.get(); |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 129 | bool OpenEventFiles(const std::vector<int>& on_cpus); |
| Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 130 | bool ReadCounters(std::vector<CountersInfo>* counters); |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 131 | bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages); |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 132 | bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback); |
| Yabin Cui | cdc11a3 | 2018-03-20 15:29:03 -0700 | [diff] [blame] | 133 | bool ReadMmapEventData(); |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 134 | bool FinishReadMmapEventData(); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 135 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 136 | // If monitored_cpus is empty, monitor all cpus. |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 137 | bool HandleCpuHotplugEvents(const std::vector<int>& monitored_cpus, |
| 138 | double check_interval_in_sec = |
| 139 | DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC); |
| 140 | |
| 141 | // Stop profiling if all monitored processes/threads don't exist. |
| 142 | bool StopWhenNoMoreTargets(double check_interval_in_sec = |
| 143 | DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC); |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 144 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 145 | private: |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 146 | struct EventSelection { |
| 147 | EventTypeAndModifier event_type_modifier; |
| 148 | perf_event_attr event_attr; |
| 149 | std::vector<std::unique_ptr<EventFd>> event_fds; |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 150 | std::vector<std::unique_ptr<InplaceSamplerClient>> inplace_samplers; |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 151 | // counters for event files closed for cpu hotplug events |
| 152 | std::vector<CounterInfo> hotplugged_counters; |
| 153 | }; |
| 154 | typedef std::vector<EventSelection> EventSelectionGroup; |
| 155 | |
| Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 156 | bool BuildAndCheckEventSelection(const std::string& event_name, |
| 157 | EventSelection* selection); |
| Yabin Cui | afdb9ce | 2015-08-19 15:46:51 -0700 | [diff] [blame] | 158 | void UnionSampleType(); |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 159 | bool IsUserSpaceSamplerGroup(EventSelectionGroup& group); |
| 160 | bool OpenUserSpaceSamplersOnGroup(EventSelectionGroup& group, |
| 161 | const std::map<pid_t, std::set<pid_t>>& process_map); |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 162 | bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu, |
| 163 | std::string* failed_event_type); |
| 164 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 165 | bool MmapEventFiles(size_t mmap_pages, bool report_error); |
| Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 166 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 167 | bool DetectCpuHotplugEvents(); |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 168 | bool HandleCpuOnlineEvent(int cpu); |
| 169 | bool HandleCpuOfflineEvent(int cpu); |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 170 | bool CreateMappedBufferForCpu(int cpu); |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 171 | bool CheckMonitoredTargets(); |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 172 | bool HasSampler(); |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 173 | |
| Yabin Cui | 4cf37d1 | 2016-08-19 15:42:39 -0700 | [diff] [blame] | 174 | const bool for_stat_cmd_; |
| 175 | |
| Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 176 | std::vector<EventSelectionGroup> groups_; |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 177 | std::set<pid_t> processes_; |
| 178 | std::set<pid_t> threads_; |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 179 | size_t mmap_pages_; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 180 | |
| Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 181 | std::unique_ptr<IOEventLoop> loop_; |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 182 | std::function<bool(Record*)> record_callback_; |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 183 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 184 | std::set<int> monitored_cpus_; |
| 185 | std::vector<int> online_cpus_; |
| 186 | |
| Yabin Cui | 2ea6de1 | 2016-10-24 19:13:09 -0700 | [diff] [blame] | 187 | // Records from all mapped buffers are stored in record_buffer_, each |
| 188 | // RecordBufferHead manages records read from one mapped buffer. Create |
| 189 | // record_buffer_heads_ and record_buffer_ here to avoid allocating them |
| 190 | // from heap each time calling ReadMmapEventData(). |
| 191 | struct RecordBufferHead { |
| 192 | size_t current_pos; // current position in record_buffer_ |
| 193 | size_t end_pos; // end position in record_buffer_ |
| 194 | perf_event_attr* attr; |
| 195 | uint64_t timestamp; |
| 196 | std::unique_ptr<Record> r; |
| 197 | }; |
| 198 | std::vector<RecordBufferHead> record_buffer_heads_; |
| 199 | std::vector<char> record_buffer_; |
| 200 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(EventSelectionSet); |
| 202 | }; |
| 203 | |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 204 | bool IsBranchSamplingSupported(); |
| 205 | bool IsDwarfCallChainSamplingSupported(); |
| Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 206 | bool IsDumpingRegsForTracepointEventsSupported(); |
| Yabin Cui | 6d4959c | 2017-09-01 15:40:41 -0700 | [diff] [blame] | 207 | bool IsSettingClockIdSupported(); |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 208 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 209 | #endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_ |