blob: 8aca7840c04030495328eb0d26d34176eac15962 [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 Cui9759e1b2015-04-28 15:54:13 -070031#include "perf_event.h"
Yabin Cui2d6efe42016-04-01 20:22:35 -070032#include "record.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070033
Yabin Cui994cb622016-08-19 17:24:37 -070034constexpr double DEFAULT_PERIOD_TO_DETECT_CPU_HOTPLUG_EVENTS_IN_SEC = 0.5;
Yabin Cui5f43fc42016-12-13 13:47:49 -080035constexpr double DEFAULT_PERIOD_TO_CHECK_MONITORED_TARGETS_IN_SEC = 1;
Yabin Cui994cb622016-08-19 17:24:37 -070036
Yabin Cui778424b2016-08-24 19:32:55 -070037struct CounterInfo {
38 pid_t tid;
39 int cpu;
40 PerfCounter counter;
41};
42
Yabin Cui778424b2016-08-24 19:32:55 -070043struct CountersInfo {
Yabin Cui003b2452016-09-29 15:32:45 -070044 uint32_t group_id;
45 std::string event_name;
46 std::string event_modifier;
Yabin Cui778424b2016-08-24 19:32:55 -070047 std::vector<CounterInfo> counters;
48};
49
Yabin Cuidbbda302016-07-28 12:55:41 -070050class IOEventLoop;
Yabin Cui19e6b6d2016-03-10 11:49:57 -080051
Yabin Cuidbbda302016-07-28 12:55:41 -070052// 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 Cui9759e1b2015-04-28 15:54:13 -070066
67class EventSelectionSet {
68 public:
Yabin Cui825e56b2016-08-26 18:25:21 -070069 EventSelectionSet(bool for_stat_cmd)
70 : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(nullptr) {}
Yabin Cui9759e1b2015-04-28 15:54:13 -070071
Yabin Cuidbbda302016-07-28 12:55:41 -070072 bool empty() const { return groups_.empty(); }
Yabin Cui9759e1b2015-04-28 15:54:13 -070073
Yabin Cui877751b2016-06-13 18:03:47 -070074 bool AddEventType(const std::string& event_name);
75 bool AddEventGroup(const std::vector<std::string>& event_names);
Yabin Cui003b2452016-09-29 15:32:45 -070076 std::vector<const EventType*> GetTracepointEvents() const;
77 std::vector<EventAttrWithId> GetEventAttrWithId() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -070078
Yabin Cuib032de72015-06-17 21:15:09 -070079 void SetEnableOnExec(bool enable);
80 bool GetEnableOnExec();
Yabin Cui9759e1b2015-04-28 15:54:13 -070081 void SampleIdAll();
Yabin Cui003b2452016-09-29 15:32:45 -070082 void SetSampleFreq(uint64_t sample_freq);
83 void SetSamplePeriod(uint64_t sample_period);
84 void UseDefaultSampleFreq();
Yabin Cuiddddc062015-06-02 17:54:52 -070085 bool SetBranchSampling(uint64_t branch_sample_type);
Yabin Cui76769e52015-07-13 12:23:54 -070086 void EnableFpCallChainSampling();
87 bool EnableDwarfCallChainSampling(uint32_t dump_stack_size);
Yabin Cui4be41262015-06-22 14:23:01 -070088 void SetInherit(bool enable);
Yabin Cui003b2452016-09-29 15:32:45 -070089 bool NeedKernelSymbol() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -070090
Yabin Cuibc2a1022016-08-29 12:33:17 -070091 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 Cui003b2452016-09-29 15:32:45 -070099 const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; }
Yabin Cuibc2a1022016-08-29 12:33:17 -0700100
Yabin Cui003b2452016-09-29 15:32:45 -0700101 const std::set<pid_t>& GetMonitoredThreads() const { return threads_; }
Yabin Cuibc2a1022016-08-29 12:33:17 -0700102
103 bool HasMonitoredTarget() const {
104 return !processes_.empty() || !threads_.empty();
105 }
106
Yabin Cui5f43fc42016-12-13 13:47:49 -0800107 void SetIOEventLoop(IOEventLoop& loop) {
108 loop_ = &loop;
109 }
110
Yabin Cuibc2a1022016-08-29 12:33:17 -0700111 bool OpenEventFiles(const std::vector<int>& on_cpus);
Yabin Cui04d08a32015-08-19 15:01:12 -0700112 bool ReadCounters(std::vector<CountersInfo>* counters);
Yabin Cuidbbda302016-07-28 12:55:41 -0700113 bool MmapEventFiles(size_t min_mmap_pages, size_t max_mmap_pages);
Yabin Cui5f43fc42016-12-13 13:47:49 -0800114 bool PrepareToReadMmapEventData(const std::function<bool(Record*)>& callback);
Yabin Cui2d6efe42016-04-01 20:22:35 -0700115 bool FinishReadMmapEventData();
Yabin Cui9759e1b2015-04-28 15:54:13 -0700116
Yabin Cui994cb622016-08-19 17:24:37 -0700117 // If monitored_cpus is empty, monitor all cpus.
Yabin Cui5f43fc42016-12-13 13:47:49 -0800118 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 Cui994cb622016-08-19 17:24:37 -0700125
Yabin Cui9759e1b2015-04-28 15:54:13 -0700126 private:
Yabin Cui003b2452016-09-29 15:32:45 -0700127 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 Cui877751b2016-06-13 18:03:47 -0700136 bool BuildAndCheckEventSelection(const std::string& event_name,
137 EventSelection* selection);
Yabin Cuiafdb9ce2015-08-19 15:46:51 -0700138 void UnionSampleType();
Yabin Cui003b2452016-09-29 15:32:45 -0700139 bool OpenEventFilesOnGroup(EventSelectionGroup& group, pid_t tid, int cpu,
140 std::string* failed_event_type);
141
Yabin Cuidbbda302016-07-28 12:55:41 -0700142 bool MmapEventFiles(size_t mmap_pages, bool report_error);
Yabin Cui2ea6de12016-10-24 19:13:09 -0700143 bool ReadMmapEventData();
Yabin Cui4be41262015-06-22 14:23:01 -0700144
Yabin Cui994cb622016-08-19 17:24:37 -0700145 bool DetectCpuHotplugEvents();
Yabin Cui778424b2016-08-24 19:32:55 -0700146 bool HandleCpuOnlineEvent(int cpu);
147 bool HandleCpuOfflineEvent(int cpu);
Yabin Cui825e56b2016-08-26 18:25:21 -0700148 bool CreateMappedBufferForCpu(int cpu);
Yabin Cui5f43fc42016-12-13 13:47:49 -0800149 bool CheckMonitoredTargets();
Yabin Cui994cb622016-08-19 17:24:37 -0700150
Yabin Cui4cf37d12016-08-19 15:42:39 -0700151 const bool for_stat_cmd_;
152
Yabin Cui877751b2016-06-13 18:03:47 -0700153 std::vector<EventSelectionGroup> groups_;
Yabin Cuibc2a1022016-08-29 12:33:17 -0700154 std::set<pid_t> processes_;
155 std::set<pid_t> threads_;
Yabin Cui825e56b2016-08-26 18:25:21 -0700156 size_t mmap_pages_;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700157
Yabin Cui825e56b2016-08-26 18:25:21 -0700158 IOEventLoop* loop_;
Yabin Cuidbbda302016-07-28 12:55:41 -0700159 std::function<bool(Record*)> record_callback_;
Yabin Cui2d6efe42016-04-01 20:22:35 -0700160
Yabin Cui994cb622016-08-19 17:24:37 -0700161 std::set<int> monitored_cpus_;
162 std::vector<int> online_cpus_;
163
Yabin Cui2ea6de12016-10-24 19:13:09 -0700164 // 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 Cui9759e1b2015-04-28 15:54:13 -0700178 DISALLOW_COPY_AND_ASSIGN(EventSelectionSet);
179};
180
Yabin Cui76769e52015-07-13 12:23:54 -0700181bool IsBranchSamplingSupported();
182bool IsDwarfCallChainSamplingSupported();
183
Yabin Cui9759e1b2015-04-28 15:54:13 -0700184#endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_