blob: 289208a5becddcbdd5cf1ebce260e64c9eee453d [file] [log] [blame]
Yabin Cui9759e1b2015-04-28 15:54:13 -07001/*
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 Cui9759e1b2015-04-28 15:54:13 -070020#include <functional>
21#include <map>
Yabin Cui994cb622016-08-19 17:24:37 -070022#include <set>
Yabin Cui2d6efe42016-04-01 20:22:35 -070023#include <unordered_map>
Yabin Cui9759e1b2015-04-28 15:54:13 -070024#include <vector>
25
Elliott Hughes66dd09e2015-12-04 14:00:57 -080026#include <android-base/macros.h>
Yabin Cui9759e1b2015-04-28 15:54:13 -070027
Yabin Cui003b2452016-09-29 15:32:45 -070028#include "event_attr.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070029#include "event_fd.h"
Yabin Cuid115b2f2015-06-15 13:57:23 -070030#include "event_type.h"
Yabin Cui26968e62017-01-30 11:34:24 -080031#include "InplaceSamplerClient.h"
32#include "IOEventLoop.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070033#include "perf_event.h"
Yabin Cui2d6efe42016-04-01 20:22:35 -070034#include "record.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070035
Yabin Cui994cb622016-08-19 17:24:37 -070036constexpr double DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC = 0.5;
Yabin Cui5f43fc42016-12-13 13:47:49 -080037constexpr double DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC = 1;
Yabin Cui994cb622016-08-19 17:24:37 -070038
Yabin Cui778424b2016-08-24 19:32:55 -070039struct CounterInfo {
40 pid_t tid;
41 int cpu;
42 PerfCounter counter;
43};
44
Yabin Cui778424b2016-08-24 19:32:55 -070045struct CountersInfo {
Yabin Cui003b2452016-09-29 15:32:45 -070046 uint32_t group_id;
47 std::string event_name;
48 std::string event_modifier;
Yabin Cui778424b2016-08-24 19:32:55 -070049 std::vector<CounterInfo> counters;
50};
51
Yabin Cui20b49f82017-08-03 15:54:43 -070052struct 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 Cuidbbda302016-07-28 12:55:41 -070066// 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 Cui9759e1b2015-04-28 15:54:13 -070080
81class EventSelectionSet {
82 public:
Yabin Cui825e56b2016-08-26 18:25:21 -070083 EventSelectionSet(bool for_stat_cmd)
Yabin Cui26968e62017-01-30 11:34:24 -080084 : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(new IOEventLoop) {}
Yabin Cui9759e1b2015-04-28 15:54:13 -070085
Yabin Cuidbbda302016-07-28 12:55:41 -070086 bool empty() const { return groups_.empty(); }
Yabin Cui9759e1b2015-04-28 15:54:13 -070087
Yabin Cui20b49f82017-08-03 15:54:43 -070088 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 Cui68b83832017-07-19 17:54:57 -070090 std::vector<const EventType*> GetEvents() const;
Yabin Cui003b2452016-09-29 15:32:45 -070091 std::vector<const EventType*> GetTracepointEvents() const;
Yabin Cuid3cb3b02017-07-24 14:59:46 -070092 bool ExcludeKernel() const;
Yabin Cui26968e62017-01-30 11:34:24 -080093 bool HasInplaceSampler() const;
Yabin Cui003b2452016-09-29 15:32:45 -070094 std::vector<EventAttrWithId> GetEventAttrWithId() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -070095
Yabin Cuib032de72015-06-17 21:15:09 -070096 void SetEnableOnExec(bool enable);
97 bool GetEnableOnExec();
Yabin Cui9759e1b2015-04-28 15:54:13 -070098 void SampleIdAll();
Yabin Cui20b49f82017-08-03 15:54:43 -070099 void SetSampleSpeed(size_t group_id, const SampleSpeed& speed);
Yabin Cuiddddc062015-06-02 17:54:52 -0700100 bool SetBranchSampling(uint64_t branch_sample_type);
Yabin Cui76769e52015-07-13 12:23:54 -0700101 void EnableFpCallChainSampling();
102 bool EnableDwarfCallChainSampling(uint32_t dump_stack_size);
Yabin Cui4be41262015-06-22 14:23:01 -0700103 void SetInherit(bool enable);
Yabin Cui6d4959c2017-09-01 15:40:41 -0700104 void SetClockId(int clock_id);
Yabin Cui003b2452016-09-29 15:32:45 -0700105 bool NeedKernelSymbol() const;
Yabin Cui516a87c2018-03-26 17:34:00 -0700106 void SetRecordNotExecutableMaps(bool record);
107 bool RecordNotExecutableMaps() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700108
Yabin Cuibc2a1022016-08-29 12:33:17 -0700109 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 Cui003b2452016-09-29 15:32:45 -0700117 const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; }
Yabin Cuibc2a1022016-08-29 12:33:17 -0700118
Yabin Cui003b2452016-09-29 15:32:45 -0700119 const std::set<pid_t>& GetMonitoredThreads() const { return threads_; }
Yabin Cuibc2a1022016-08-29 12:33:17 -0700120
121 bool HasMonitoredTarget() const {
122 return !processes_.empty() || !threads_.empty();
123 }
124
Yabin Cui26968e62017-01-30 11:34:24 -0800125 IOEventLoop* GetIOEventLoop() {
126 return loop_.get();
Yabin Cui5f43fc42016-12-13 13:47:49 -0800127 }
128
Yabin Cuibc2a1022016-08-29 12:33:17 -0700129 bool OpenEventFiles(const std::vector<int>& on_cpus);
Yabin Cui04d08a32015-08-19 15:01:12 -0700130 bool ReadCounters(std::vector<CountersInfo>* counters);
Yabin Cuidbbda302016-07-28 12:55:41 -0700131 bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages);
Yabin Cui5f43fc42016-12-13 13:47:49 -0800132 bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback);
Yabin Cuicdc11a32018-03-20 15:29:03 -0700133 bool ReadMmapEventData();
Yabin Cui2d6efe42016-04-01 20:22:35 -0700134 bool FinishReadMmapEventData();
Yabin Cui9759e1b2015-04-28 15:54:13 -0700135
Yabin Cui994cb622016-08-19 17:24:37 -0700136 // If monitored_cpus is empty, monitor all cpus.
Yabin Cui5f43fc42016-12-13 13:47:49 -0800137 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 Cui994cb622016-08-19 17:24:37 -0700144
Yabin Cui9759e1b2015-04-28 15:54:13 -0700145 private:
Yabin Cui003b2452016-09-29 15:32:45 -0700146 struct EventSelection {
147 EventTypeAndModifier event_type_modifier;
148 perf_event_attr event_attr;
149 std::vector<std::unique_ptr<EventFd>> event_fds;
Yabin Cui26968e62017-01-30 11:34:24 -0800150 std::vector<std::unique_ptr<InplaceSamplerClient>> inplace_samplers;
Yabin Cui003b2452016-09-29 15:32:45 -0700151 // counters for event files closed for cpu hotplug events
152 std::vector<CounterInfo> hotplugged_counters;
153 };
154 typedef std::vector<EventSelection> EventSelectionGroup;
155
Yabin Cui877751b2016-06-13 18:03:47 -0700156 bool BuildAndCheckEventSelection(const std::string& event_name,
157 EventSelection* selection);
Yabin Cuiafdb9ce2015-08-19 15:46:51 -0700158 void UnionSampleType();
Yabin Cui26968e62017-01-30 11:34:24 -0800159 bool IsUserSpaceSamplerGroup(EventSelectionGroup& group);
160 bool OpenUserSpaceSamplersOnGroup(EventSelectionGroup& group,
161 const std::map<pid_t, std::set<pid_t>>& process_map);
Yabin Cui003b2452016-09-29 15:32:45 -0700162 bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu,
163 std::string* failed_event_type);
164
Yabin Cuidbbda302016-07-28 12:55:41 -0700165 bool MmapEventFiles(size_t mmap_pages, bool report_error);
Yabin Cui4be41262015-06-22 14:23:01 -0700166
Yabin Cui994cb622016-08-19 17:24:37 -0700167 bool DetectCpuHotplugEvents();
Yabin Cui778424b2016-08-24 19:32:55 -0700168 bool HandleCpuOnlineEvent(int cpu);
169 bool HandleCpuOfflineEvent(int cpu);
Yabin Cui825e56b2016-08-26 18:25:21 -0700170 bool CreateMappedBufferForCpu(int cpu);
Yabin Cui5f43fc42016-12-13 13:47:49 -0800171 bool CheckMonitoredTargets();
Yabin Cui26968e62017-01-30 11:34:24 -0800172 bool HasSampler();
Yabin Cui994cb622016-08-19 17:24:37 -0700173
Yabin Cui4cf37d12016-08-19 15:42:39 -0700174 const bool for_stat_cmd_;
175
Yabin Cui877751b2016-06-13 18:03:47 -0700176 std::vector<EventSelectionGroup> groups_;
Yabin Cuibc2a1022016-08-29 12:33:17 -0700177 std::set<pid_t> processes_;
178 std::set<pid_t> threads_;
Yabin Cui825e56b2016-08-26 18:25:21 -0700179 size_t mmap_pages_;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700180
Yabin Cui26968e62017-01-30 11:34:24 -0800181 std::unique_ptr<IOEventLoop> loop_;
Yabin Cuidbbda302016-07-28 12:55:41 -0700182 std::function<bool(Record*)> record_callback_;
Yabin Cui2d6efe42016-04-01 20:22:35 -0700183
Yabin Cui994cb622016-08-19 17:24:37 -0700184 std::set<int> monitored_cpus_;
185 std::vector<int> online_cpus_;
186
Yabin Cui2ea6de12016-10-24 19:13:09 -0700187 // 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 Cui9759e1b2015-04-28 15:54:13 -0700201 DISALLOW_COPY_AND_ASSIGN(EventSelectionSet);
202};
203
Yabin Cui76769e52015-07-13 12:23:54 -0700204bool IsBranchSamplingSupported();
205bool IsDwarfCallChainSamplingSupported();
Yabin Cui68b83832017-07-19 17:54:57 -0700206bool IsDumpingRegsForTracepointEventsSupported();
Yabin Cui6d4959c2017-09-01 15:40:41 -0700207bool IsSettingClockIdSupported();
Yabin Cui76769e52015-07-13 12:23:54 -0700208
Yabin Cui9759e1b2015-04-28 15:54:13 -0700209#endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_