2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SIMPLE_PERF_EVENT_SELECTION_SET_H_
18 #define SIMPLE_PERF_EVENT_SELECTION_SET_H_
23 #include <unordered_map>
26 #include <android-base/macros.h>
28 #include "event_attr.h"
30 #include "event_type.h"
31 #include "InplaceSamplerClient.h"
32 #include "IOEventLoop.h"
33 #include "perf_event.h"
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;
47 std::string event_name;
48 std::string event_modifier;
49 std::vector<CounterInfo> counters;
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
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.
67 class EventSelectionSet {
69 EventSelectionSet(bool for_stat_cmd)
70 : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(new IOEventLoop) {}
72 bool empty() const { return groups_.empty(); }
74 bool AddEventType(const std::string& event_name);
75 bool AddEventGroup(const std::vector<std::string>& event_names);
76 std::vector<const EventType*> GetTracepointEvents() const;
77 bool HasInplaceSampler() const;
78 std::vector<EventAttrWithId> GetEventAttrWithId() const;
80 void SetEnableOnExec(bool enable);
81 bool GetEnableOnExec();
83 void SetSampleFreq(uint64_t sample_freq);
84 void SetSamplePeriod(uint64_t sample_period);
85 void UseDefaultSampleFreq();
86 bool SetBranchSampling(uint64_t branch_sample_type);
87 void EnableFpCallChainSampling();
88 bool EnableDwarfCallChainSampling(uint32_t dump_stack_size);
89 void SetInherit(bool enable);
90 bool NeedKernelSymbol() const;
92 void AddMonitoredProcesses(const std::set<pid_t>& processes) {
93 processes_.insert(processes.begin(), processes.end());
96 void AddMonitoredThreads(const std::set<pid_t>& threads) {
97 threads_.insert(threads.begin(), threads.end());
100 const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; }
102 const std::set<pid_t>& GetMonitoredThreads() const { return threads_; }
104 bool HasMonitoredTarget() const {
105 return !processes_.empty() || !threads_.empty();
108 IOEventLoop* GetIOEventLoop() {
112 bool OpenEventFiles(const std::vector<int>& on_cpus);
113 bool ReadCounters(std::vector<CountersInfo>* counters);
114 bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages);
115 bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback);
116 bool FinishReadMmapEventData();
118 // If monitored_cpus is empty, monitor all cpus.
119 bool HandleCpuHotplugEvents(const std::vector<int>& monitored_cpus,
120 double check_interval_in_sec =
121 DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC);
123 // Stop profiling if all monitored processes/threads don't exist.
124 bool StopWhenNoMoreTargets(double check_interval_in_sec =
125 DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC);
128 struct EventSelection {
129 EventTypeAndModifier event_type_modifier;
130 perf_event_attr event_attr;
131 std::vector<std::unique_ptr<EventFd>> event_fds;
132 std::vector<std::unique_ptr<InplaceSamplerClient>> inplace_samplers;
133 // counters for event files closed for cpu hotplug events
134 std::vector<CounterInfo> hotplugged_counters;
136 typedef std::vector<EventSelection> EventSelectionGroup;
138 bool BuildAndCheckEventSelection(const std::string& event_name,
139 EventSelection* selection);
140 void UnionSampleType();
141 bool IsUserSpaceSamplerGroup(EventSelectionGroup& group);
142 bool OpenUserSpaceSamplersOnGroup(EventSelectionGroup& group,
143 const std::map<pid_t, std::set<pid_t>>& process_map);
144 bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu,
145 std::string* failed_event_type);
147 bool MmapEventFiles(size_t mmap_pages, bool report_error);
148 bool ReadMmapEventData();
150 bool DetectCpuHotplugEvents();
151 bool HandleCpuOnlineEvent(int cpu);
152 bool HandleCpuOfflineEvent(int cpu);
153 bool CreateMappedBufferForCpu(int cpu);
154 bool CheckMonitoredTargets();
157 const bool for_stat_cmd_;
159 std::vector<EventSelectionGroup> groups_;
160 std::set<pid_t> processes_;
161 std::set<pid_t> threads_;
164 std::unique_ptr<IOEventLoop> loop_;
165 std::function<bool(Record*)> record_callback_;
167 std::set<int> monitored_cpus_;
168 std::vector<int> online_cpus_;
170 // Records from all mapped buffers are stored in record_buffer_, each
171 // RecordBufferHead manages records read from one mapped buffer. Create
172 // record_buffer_heads_ and record_buffer_ here to avoid allocating them
173 // from heap each time calling ReadMmapEventData().
174 struct RecordBufferHead {
175 size_t current_pos; // current position in record_buffer_
176 size_t end_pos; // end position in record_buffer_
177 perf_event_attr* attr;
179 std::unique_ptr<Record> r;
181 std::vector<RecordBufferHead> record_buffer_heads_;
182 std::vector<char> record_buffer_;
184 DISALLOW_COPY_AND_ASSIGN(EventSelectionSet);
187 bool IsBranchSamplingSupported();
188 bool IsDwarfCallChainSamplingSupported();
190 #endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_