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 
20 #include <functional>
21 #include <map>
22 #include <set>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include <android-base/macros.h>
27 
28 #include "event_attr.h"
29 #include "event_fd.h"
30 #include "event_type.h"
31 #include "InplaceSamplerClient.h"
32 #include "IOEventLoop.h"
33 #include "perf_event.h"
34 #include "record.h"
35 
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;
38 
39 struct CounterInfo {
40   pid_t tid;
41   int cpu;
42   PerfCounter counter;
43 };
44 
45 struct CountersInfo {
46   uint32_t group_id;
47   std::string event_name;
48   std::string event_modifier;
49   std::vector<CounterInfo> counters;
50 };
51 
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.
66 
67 class EventSelectionSet {
68  public:
EventSelectionSet(bool for_stat_cmd)69   EventSelectionSet(bool for_stat_cmd)
70       : for_stat_cmd_(for_stat_cmd), mmap_pages_(0), loop_(new IOEventLoop) {}
71 
empty()72   bool empty() const { return groups_.empty(); }
73 
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;
79 
80   void SetEnableOnExec(bool enable);
81   bool GetEnableOnExec();
82   void SampleIdAll();
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;
91 
AddMonitoredProcesses(const std::set<pid_t> & processes)92   void AddMonitoredProcesses(const std::set<pid_t>& processes) {
93     processes_.insert(processes.begin(), processes.end());
94   }
95 
AddMonitoredThreads(const std::set<pid_t> & threads)96   void AddMonitoredThreads(const std::set<pid_t>& threads) {
97     threads_.insert(threads.begin(), threads.end());
98   }
99 
GetMonitoredProcesses()100   const std::set<pid_t>& GetMonitoredProcesses() const { return processes_; }
101 
GetMonitoredThreads()102   const std::set<pid_t>& GetMonitoredThreads() const { return threads_; }
103 
HasMonitoredTarget()104   bool HasMonitoredTarget() const {
105     return !processes_.empty() || !threads_.empty();
106   }
107 
GetIOEventLoop()108   IOEventLoop* GetIOEventLoop() {
109     return loop_.get();
110   }
111 
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();
117 
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);
122 
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);
126 
127  private:
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;
135   };
136   typedef std::vector<EventSelection> EventSelectionGroup;
137 
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);
146 
147   bool MmapEventFiles(size_t mmap_pages, bool report_error);
148   bool ReadMmapEventData();
149 
150   bool DetectCpuHotplugEvents();
151   bool HandleCpuOnlineEvent(int cpu);
152   bool HandleCpuOfflineEvent(int cpu);
153   bool CreateMappedBufferForCpu(int cpu);
154   bool CheckMonitoredTargets();
155   bool HasSampler();
156 
157   const bool for_stat_cmd_;
158 
159   std::vector<EventSelectionGroup> groups_;
160   std::set<pid_t> processes_;
161   std::set<pid_t> threads_;
162   size_t mmap_pages_;
163 
164   std::unique_ptr<IOEventLoop> loop_;
165   std::function<bool(Record*)> record_callback_;
166 
167   std::set<int> monitored_cpus_;
168   std::vector<int> online_cpus_;
169 
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;
178     uint64_t timestamp;
179     std::unique_ptr<Record> r;
180   };
181   std::vector<RecordBufferHead> record_buffer_heads_;
182   std::vector<char> record_buffer_;
183 
184   DISALLOW_COPY_AND_ASSIGN(EventSelectionSet);
185 };
186 
187 bool IsBranchSamplingSupported();
188 bool IsDwarfCallChainSamplingSupported();
189 
190 #endif  // SIMPLE_PERF_EVENT_SELECTION_SET_H_
191