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