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 <vector> 23 24 #include <android-base/macros.h> 25 26 #include "event_fd.h" 27 #include "event_type.h" 28 #include "perf_event.h" 29 30 struct CountersInfo { 31 const EventTypeAndModifier* event_type; 32 struct CounterInfo { 33 pid_t tid; 34 int cpu; 35 PerfCounter counter; 36 }; 37 std::vector<CounterInfo> counters; 38 }; 39 40 struct pollfd; 41 42 // EventSelectionSet helps to monitor events. 43 // Firstly, the user creates an EventSelectionSet, and adds the specific event types to monitor. 44 // Secondly, the user defines how to monitor the events (by setting enable_on_exec flag, 45 // sample frequency, etc). 46 // Then, the user can start monitoring by ordering the EventSelectionSet to open perf event files 47 // and enable events (if enable_on_exec flag isn't used). 48 // After that, the user can read counters or read mapped event records. 49 // At last, the EventSelectionSet will clean up resources at destruction automatically. 50 51 class EventSelectionSet { 52 public: EventSelectionSet()53 EventSelectionSet() { 54 } 55 Empty()56 bool Empty() const { 57 return selections_.empty(); 58 } 59 60 bool AddEventType(const EventTypeAndModifier& event_type_modifier); 61 62 void SetEnableOnExec(bool enable); 63 bool GetEnableOnExec(); 64 void SampleIdAll(); 65 void SetSampleFreq(uint64_t sample_freq); 66 void SetSamplePeriod(uint64_t sample_period); 67 bool SetBranchSampling(uint64_t branch_sample_type); 68 void EnableFpCallChainSampling(); 69 bool EnableDwarfCallChainSampling(uint32_t dump_stack_size); 70 void SetInherit(bool enable); 71 72 bool OpenEventFilesForCpus(const std::vector<int>& cpus); 73 bool OpenEventFilesForThreadsOnCpus(const std::vector<pid_t>& threads, std::vector<int> cpus); 74 bool ReadCounters(std::vector<CountersInfo>* counters); 75 void PreparePollForEventFiles(std::vector<pollfd>* pollfds); 76 bool MmapEventFiles(size_t mmap_pages); 77 bool ReadMmapEventData(std::function<bool(const char*, size_t)> callback); 78 79 const perf_event_attr* FindEventAttrByType(const EventTypeAndModifier& event_type_modifier); 80 const std::vector<std::unique_ptr<EventFd>>* FindEventFdsByType( 81 const EventTypeAndModifier& event_type_modifier); 82 83 private: 84 void UnionSampleType(); 85 bool OpenEventFiles(const std::vector<pid_t>& threads, const std::vector<int>& cpus); 86 87 struct EventSelection { 88 EventTypeAndModifier event_type_modifier; 89 perf_event_attr event_attr; 90 std::vector<std::unique_ptr<EventFd>> event_fds; 91 }; 92 EventSelection* FindSelectionByType(const EventTypeAndModifier& event_type_modifier); 93 94 std::vector<EventSelection> selections_; 95 96 DISALLOW_COPY_AND_ASSIGN(EventSelectionSet); 97 }; 98 99 bool IsBranchSamplingSupported(); 100 bool IsDwarfCallChainSamplingSupported(); 101 102 #endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_ 103