| 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 | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 31 | #include "perf_event.h" |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 32 | #include "record.h" |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 33 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 34 | 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] | 35 | constexpr double DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC = 1; |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 36 | |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 37 | struct CounterInfo { |
| 38 | pid_t tid; |
| 39 | int cpu; |
| 40 | PerfCounter counter; |
| 41 | }; |
| 42 | |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 43 | struct CountersInfo { |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 44 | uint32_t group_id; |
| 45 | std::string event_name; |
| 46 | std::string event_modifier; |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 47 | std::vector<CounterInfo> counters; |
| 48 | }; |
| 49 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 50 | class IOEventLoop; |
| Yabin Cui | 19e6b6d | 2016-03-10 11:49:57 -0800 | [diff] [blame] | 51 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 52 | // EventSelectionSet helps to monitor events. It is used in following steps: |
| 53 | // 1. Create an EventSelectionSet, and add event types to monitor by calling |
| 54 | // AddEventType() or AddEventGroup(). |
| 55 | // 2. Define how to monitor events by calling SetEnableOnExec(), SampleIdAll(), |
| 56 | // SetSampleFreq(), etc. |
| 57 | // 3. Start monitoring by calling OpenEventFilesForCpus() or |
| 58 | // OpenEventFilesForThreadsOnCpus(). If SetEnableOnExec() has been called |
| 59 | // in step 2, monitor will be delayed until the monitored thread calls |
| 60 | // exec(). |
| 61 | // 4. Read counters by calling ReadCounters(), or read mapped event records |
| 62 | // by calling MmapEventFiles(), PrepareToReadMmapEventData() and |
| 63 | // FinishReadMmapEventData(). |
| 64 | // 5. Stop monitoring automatically in the destructor of EventSelectionSet by |
| 65 | // closing perf event files. |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 66 | |
| 67 | class EventSelectionSet { |
| 68 | public: |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 69 | EventSelectionSet(bool for_stat_cmd) |
| 70 | : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(nullptr) {} |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 71 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 72 | bool empty() const { return groups_.empty(); } |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 73 | |
| Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 74 | bool AddEventType(const std::string& event_name); |
| 75 | bool AddEventGroup(const std::vector<std::string>& event_names); |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 76 | std::vector<const EventType*> GetTracepointEvents() const; |
| 77 | std::vector<EventAttrWithId> GetEventAttrWithId() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 78 | |
| Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 79 | void SetEnableOnExec(bool enable); |
| 80 | bool GetEnableOnExec(); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 81 | void SampleIdAll(); |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 82 | void SetSampleFreq(uint64_t sample_freq); |
| 83 | void SetSamplePeriod(uint64_t sample_period); |
| 84 | void UseDefaultSampleFreq(); |
| Yabin Cui | ddddc06 | 2015-06-02 17:54:52 -0700 | [diff] [blame] | 85 | bool SetBranchSampling(uint64_t branch_sample_type); |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 86 | void EnableFpCallChainSampling(); |
| 87 | bool EnableDwarfCallChainSampling(uint32_t dump_stack_size); |
| Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 88 | void SetInherit(bool enable); |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 89 | bool NeedKernelSymbol() const; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 90 | |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 91 | void AddMonitoredProcesses(const std::set<pid_t>& processes) { |
| 92 | processes_.insert(processes.begin(), processes.end()); |
| 93 | } |
| 94 | |
| 95 | void AddMonitoredThreads(const std::set<pid_t>& threads) { |
| 96 | threads_.insert(threads.begin(), threads.end()); |
| 97 | } |
| 98 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 99 | const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; } |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 100 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 101 | const std::set<pid_t>& GetMonitoredThreads() const { return threads_; } |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 102 | |
| 103 | bool HasMonitoredTarget() const { |
| 104 | return !processes_.empty() || !threads_.empty(); |
| 105 | } |
| 106 | |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 107 | void SetIOEventLoop(IOEventLoop& loop) { |
| 108 | loop_ = &loop; |
| 109 | } |
| 110 | |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 111 | bool OpenEventFiles(const std::vector<int>& on_cpus); |
| Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 112 | bool ReadCounters(std::vector<CountersInfo>* counters); |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 113 | bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages); |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 114 | bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback); |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 115 | bool FinishReadMmapEventData(); |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 116 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 117 | // If monitored_cpus is empty, monitor all cpus. |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 118 | bool HandleCpuHotplugEvents(const std::vector<int>& monitored_cpus, |
| 119 | double check_interval_in_sec = |
| 120 | DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC); |
| 121 | |
| 122 | // Stop profiling if all monitored processes/threads don't exist. |
| 123 | bool StopWhenNoMoreTargets(double check_interval_in_sec = |
| 124 | DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC); |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 125 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 126 | private: |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 127 | struct EventSelection { |
| 128 | EventTypeAndModifier event_type_modifier; |
| 129 | perf_event_attr event_attr; |
| 130 | std::vector<std::unique_ptr<EventFd>> event_fds; |
| 131 | // counters for event files closed for cpu hotplug events |
| 132 | std::vector<CounterInfo> hotplugged_counters; |
| 133 | }; |
| 134 | typedef std::vector<EventSelection> EventSelectionGroup; |
| 135 | |
| Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 136 | bool BuildAndCheckEventSelection(const std::string& event_name, |
| 137 | EventSelection* selection); |
| Yabin Cui | afdb9ce | 2015-08-19 15:46:51 -0700 | [diff] [blame] | 138 | void UnionSampleType(); |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 139 | bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu, |
| 140 | std::string* failed_event_type); |
| 141 | |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 142 | bool MmapEventFiles(size_t mmap_pages, bool report_error); |
| Yabin Cui | 2ea6de1 | 2016-10-24 19:13:09 -0700 | [diff] [blame] | 143 | bool ReadMmapEventData(); |
| Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 144 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 145 | bool DetectCpuHotplugEvents(); |
| Yabin Cui | 778424b | 2016-08-24 19:32:55 -0700 | [diff] [blame] | 146 | bool HandleCpuOnlineEvent(int cpu); |
| 147 | bool HandleCpuOfflineEvent(int cpu); |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 148 | bool CreateMappedBufferForCpu(int cpu); |
| Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 149 | bool CheckMonitoredTargets(); |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 150 | |
| Yabin Cui | 4cf37d1 | 2016-08-19 15:42:39 -0700 | [diff] [blame] | 151 | const bool for_stat_cmd_; |
| 152 | |
| Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 153 | std::vector<EventSelectionGroup> groups_; |
| Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 154 | std::set<pid_t> processes_; |
| 155 | std::set<pid_t> threads_; |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 156 | size_t mmap_pages_; |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 157 | |
| Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 158 | IOEventLoop* loop_; |
| Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 159 | std::function<bool(Record*)> record_callback_; |
| Yabin Cui | 2d6efe4 | 2016-04-01 20:22:35 -0700 | [diff] [blame] | 160 | |
| Yabin Cui | 994cb62 | 2016-08-19 17:24:37 -0700 | [diff] [blame] | 161 | std::set<int> monitored_cpus_; |
| 162 | std::vector<int> online_cpus_; |
| 163 | |
| Yabin Cui | 2ea6de1 | 2016-10-24 19:13:09 -0700 | [diff] [blame] | 164 | // Records from all mapped buffers are stored in record_buffer_, each |
| 165 | // RecordBufferHead manages records read from one mapped buffer. Create |
| 166 | // record_buffer_heads_ and record_buffer_ here to avoid allocating them |
| 167 | // from heap each time calling ReadMmapEventData(). |
| 168 | struct RecordBufferHead { |
| 169 | size_t current_pos; // current position in record_buffer_ |
| 170 | size_t end_pos; // end position in record_buffer_ |
| 171 | perf_event_attr* attr; |
| 172 | uint64_t timestamp; |
| 173 | std::unique_ptr<Record> r; |
| 174 | }; |
| 175 | std::vector<RecordBufferHead> record_buffer_heads_; |
| 176 | std::vector<char> record_buffer_; |
| 177 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 178 | DISALLOW_COPY_AND_ASSIGN(EventSelectionSet); |
| 179 | }; |
| 180 | |
| Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 181 | bool IsBranchSamplingSupported(); |
| 182 | bool IsDwarfCallChainSamplingSupported(); |
| 183 | |
| Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 184 | #endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_ |