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 #include "event_selection_set.h"
19 #include <base/logging.h>
21 #include "environment.h"
22 #include "event_attr.h"
23 #include "event_type.h"
25 void EventSelectionSet::AddEventType(const EventType& event_type) {
26 EventSelection selection;
27 selection.event_type = &event_type;
28 selection.event_attr = CreateDefaultPerfEventAttr(event_type);
29 selections_.push_back(std::move(selection));
32 void EventSelectionSet::EnableOnExec() {
33 for (auto& selection : selections_) {
34 selection.event_attr.enable_on_exec = 1;
38 void EventSelectionSet::SampleIdAll() {
39 for (auto& selection : selections_) {
40 selection.event_attr.sample_id_all = 1;
44 void EventSelectionSet::SetSampleFreq(uint64_t sample_freq) {
45 for (auto& selection : selections_) {
46 perf_event_attr& attr = selection.event_attr;
48 attr.sample_freq = sample_freq;
52 void EventSelectionSet::SetSamplePeriod(uint64_t sample_period) {
53 for (auto& selection : selections_) {
54 perf_event_attr& attr = selection.event_attr;
56 attr.sample_period = sample_period;
60 bool EventSelectionSet::OpenEventFilesForAllCpus() {
61 std::vector<int> cpus = GetOnlineCpus();
65 for (auto& selection : selections_) {
66 for (auto& cpu : cpus) {
67 auto event_fd = EventFd::OpenEventFileForCpu(selection.event_attr, cpu);
68 if (event_fd != nullptr) {
69 selection.event_fds.push_back(std::move(event_fd));
72 // As the online cpus can be enabled or disabled at runtime, we may not open event file for
73 // all cpus successfully. But we should open at least one cpu successfully.
74 if (selection.event_fds.empty()) {
75 LOG(ERROR) << "failed to open perf event file for event_type " << selection.event_type->name
83 bool EventSelectionSet::OpenEventFilesForProcess(pid_t pid) {
84 for (auto& selection : selections_) {
85 auto event_fd = EventFd::OpenEventFileForProcess(selection.event_attr, pid);
86 if (event_fd == nullptr) {
87 PLOG(ERROR) << "failed to open perf event file for event type " << selection.event_type->name
91 selection.event_fds.push_back(std::move(event_fd));
96 bool EventSelectionSet::EnableEvents() {
97 for (auto& selection : selections_) {
98 for (auto& event_fd : selection.event_fds) {
99 if (!event_fd->EnableEvent()) {
107 bool EventSelectionSet::ReadCounters(
108 std::map<const EventType*, std::vector<PerfCounter>>* counters_map) {
109 for (auto& selection : selections_) {
110 std::vector<PerfCounter> counters;
111 for (auto& event_fd : selection.event_fds) {
113 if (!event_fd->ReadCounter(&counter)) {
116 counters.push_back(counter);
118 counters_map->insert(std::make_pair(selection.event_type, counters));
123 void EventSelectionSet::PreparePollForEventFiles(std::vector<pollfd>* pollfds) {
124 for (auto& selection : selections_) {
125 for (auto& event_fd : selection.event_fds) {
127 event_fd->PreparePollForMmapData(&poll_fd);
128 pollfds->push_back(poll_fd);
133 bool EventSelectionSet::MmapEventFiles(size_t mmap_pages) {
134 for (auto& selection : selections_) {
135 for (auto& event_fd : selection.event_fds) {
136 if (!event_fd->MmapContent(mmap_pages)) {
144 static bool ReadMmapEventDataForFd(std::unique_ptr<EventFd>& event_fd,
145 std::function<bool(const char*, size_t)> callback,
150 size_t size = event_fd->GetAvailableMmapData(&data);
154 if (!callback(data, size)) {
158 event_fd->DiscardMmapData(size);
163 bool EventSelectionSet::ReadMmapEventData(std::function<bool(const char*, size_t)> callback) {
164 for (auto& selection : selections_) {
165 for (auto& event_fd : selection.event_fds) {
168 if (!ReadMmapEventDataForFd(event_fd, callback, &have_data)) {
180 std::string EventSelectionSet::FindEventFileNameById(uint64_t id) {
181 for (auto& selection : selections_) {
182 for (auto& event_fd : selection.event_fds) {
183 if (event_fd->Id() == id) {
184 return event_fd->Name();
191 EventSelectionSet::EventSelection* EventSelectionSet::FindSelectionByType(
192 const EventType& event_type) {
193 for (auto& selection : selections_) {
194 if (selection.event_type->name == event_type.name) {
201 const perf_event_attr& EventSelectionSet::FindEventAttrByType(const EventType& event_type) {
202 return FindSelectionByType(event_type)->event_attr;
205 const std::vector<std::unique_ptr<EventFd>>& EventSelectionSet::FindEventFdsByType(
206 const EventType& event_type) {
207 return FindSelectionByType(event_type)->event_fds;