diff options
author | Yabin Cui <yabinc@google.com> | 2016-09-29 15:32:45 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2016-09-29 15:42:37 -0700 |
commit | 003b245939bae5e86ed53b3c6b333637dbc571b4 (patch) | |
tree | 24d04f34e38f206c64ad1bc1bbd8e3d43e858454 /simpleperf/event_selection_set.cpp | |
parent | 1a35848b34d6572a6b65fe50133414b2dcbea92b (diff) | |
download | extras-003b245939bae5e86ed53b3c6b333637dbc571b4.tar.gz |
Simpleperf: don't expose EventSelection.
And some tiny improvements.
Bug: http://b/30974760
Test: run simpleperf_unit_test.
Change-Id: Ie2d46c8ab9ee763d107527c9a54590f845569da4
Diffstat (limited to 'simpleperf/event_selection_set.cpp')
-rw-r--r-- | simpleperf/event_selection_set.cpp | 114 |
1 files changed, 89 insertions, 25 deletions
diff --git a/simpleperf/event_selection_set.cpp b/simpleperf/event_selection_set.cpp index d6b1fe68..9202bfad 100644 --- a/simpleperf/event_selection_set.cpp +++ b/simpleperf/event_selection_set.cpp @@ -25,6 +25,9 @@ #include "perf_regs.h" #include "utils.h" +constexpr uint64_t DEFAULT_SAMPLE_FREQ_FOR_NONTRACEPOINT_EVENT = 4000; +constexpr uint64_t DEFAULT_SAMPLE_PERIOD_FOR_TRACEPOINT_EVENT = 1; + bool IsBranchSamplingSupported() { const EventType* type = FindEventTypeByName("cpu-cycles"); if (type == nullptr) { @@ -106,8 +109,6 @@ bool EventSelectionSet::AddEventGroup( if (!BuildAndCheckEventSelection(event_name, &selection)) { return false; } - selection.selection_id = group.size(); - selection.group_id = groups_.size(); group.push_back(std::move(selection)); } groups_.push_back(std::move(group)); @@ -115,6 +116,34 @@ bool EventSelectionSet::AddEventGroup( return true; } +std::vector<const EventType*> EventSelectionSet::GetTracepointEvents() const { + std::vector<const EventType*> result; + for (const auto& group : groups_) { + for (const auto& selection : group) { + if (selection.event_type_modifier.event_type.type == + PERF_TYPE_TRACEPOINT) { + result.push_back(&selection.event_type_modifier.event_type); + } + } + } + return result; +} + +std::vector<EventAttrWithId> EventSelectionSet::GetEventAttrWithId() const { + std::vector<EventAttrWithId> result; + for (const auto& group : groups_) { + for (const auto& selection : group) { + EventAttrWithId attr_id; + attr_id.attr = &selection.event_attr; + for (const auto& fd : selection.event_fds) { + attr_id.ids.push_back(fd->Id()); + } + result.push_back(attr_id); + } + } + return result; +} + // Union the sample type of different event attrs can make reading sample // records in perf.data easier. void EventSelectionSet::UnionSampleType() { @@ -169,18 +198,39 @@ void EventSelectionSet::SampleIdAll() { } } -void EventSelectionSet::SetSampleFreq(const EventSelection& selection, - uint64_t sample_freq) { - EventSelection& sel = groups_[selection.group_id][selection.selection_id]; - sel.event_attr.freq = 1; - sel.event_attr.sample_freq = sample_freq; +void EventSelectionSet::SetSampleFreq(uint64_t sample_freq) { + for (auto& group : groups_) { + for (auto& selection : group) { + selection.event_attr.freq = 1; + selection.event_attr.sample_freq = sample_freq; + } + } } -void EventSelectionSet::SetSamplePeriod(const EventSelection& selection, - uint64_t sample_period) { - EventSelection& sel = groups_[selection.group_id][selection.selection_id]; - sel.event_attr.freq = 0; - sel.event_attr.sample_period = sample_period; +void EventSelectionSet::SetSamplePeriod(uint64_t sample_period) { + for (auto& group : groups_) { + for (auto& selection : group) { + selection.event_attr.freq = 0; + selection.event_attr.sample_period = sample_period; + } + } +} + +void EventSelectionSet::UseDefaultSampleFreq() { + for (auto& group : groups_) { + for (auto& selection : group) { + if (selection.event_type_modifier.event_type.type == + PERF_TYPE_TRACEPOINT) { + selection.event_attr.freq = 0; + selection.event_attr.sample_period = + DEFAULT_SAMPLE_PERIOD_FOR_TRACEPOINT_EVENT; + } else { + selection.event_attr.freq = 1; + selection.event_attr.sample_freq = + DEFAULT_SAMPLE_FREQ_FOR_NONTRACEPOINT_EVENT; + } + } + } } bool EventSelectionSet::SetBranchSampling(uint64_t branch_sample_type) { @@ -253,6 +303,17 @@ void EventSelectionSet::SetLowWatermark() { } } +bool EventSelectionSet::NeedKernelSymbol() const { + for (const auto& group : groups_) { + for (const auto& selection : group) { + if (!selection.event_type_modifier.exclude_kernel) { + return true; + } + } + } + return false; +} + static bool CheckIfCpusOnline(const std::vector<int>& cpus) { std::vector<int> online_cpus = GetOnlineCpus(); for (const auto& cpu : cpus) { @@ -265,16 +326,14 @@ static bool CheckIfCpusOnline(const std::vector<int>& cpus) { return true; } -static bool OpenEventFile(EventSelectionGroup& group, pid_t tid, int cpu, - std::string* failed_event_type) { +bool EventSelectionSet::OpenEventFilesOnGroup(EventSelectionGroup& group, + pid_t tid, int cpu, + std::string* failed_event_type) { std::vector<std::unique_ptr<EventFd>> event_fds; // Given a tid and cpu, events on the same group should be all opened // successfully or all failed to open. + EventFd* group_fd = nullptr; for (auto& selection : group) { - EventFd* group_fd = nullptr; - if (selection.selection_id != 0) { - group_fd = event_fds[0].get(); - } std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(selection.event_attr, tid, cpu, group_fd); if (event_fd != nullptr) { @@ -286,6 +345,9 @@ static bool OpenEventFile(EventSelectionGroup& group, pid_t tid, int cpu, return false; } } + if (group_fd == nullptr) { + group_fd = event_fd.get(); + } } for (size_t i = 0; i < group.size(); ++i) { group[i].event_fds.push_back(std::move(event_fds[i])); @@ -319,13 +381,13 @@ bool EventSelectionSet::OpenEventFiles(const std::vector<int>& on_cpus) { size_t success_cpu_count = 0; std::string failed_event_type; for (const auto& cpu : cpus) { - if (OpenEventFile(group, tid, cpu, &failed_event_type)) { + if (OpenEventFilesOnGroup(group, tid, cpu, &failed_event_type)) { success_cpu_count++; } } // As the online cpus can be enabled or disabled at runtime, we may not - // open event file for all cpus successfully. But we should open at least - // one cpu successfully. + // open event file for all cpus successfully. But we should open at + // least one cpu successfully. if (success_cpu_count == 0) { PLOG(ERROR) << "failed to open perf event file for event_type " << failed_event_type << " for " @@ -350,10 +412,12 @@ static bool ReadCounter(const EventFd* event_fd, CounterInfo* counter) { bool EventSelectionSet::ReadCounters(std::vector<CountersInfo>* counters) { counters->clear(); - for (auto& group : groups_) { - for (auto& selection : group) { + for (size_t i = 0; i < groups_.size(); ++i) { + for (auto& selection : groups_[i]) { CountersInfo counters_info; - counters_info.selection = &selection; + counters_info.group_id = i; + counters_info.event_name = selection.event_type_modifier.event_type.name; + counters_info.event_modifier = selection.event_type_modifier.modifier; counters_info.counters = selection.hotplugged_counters; for (auto& event_fd : selection.event_fds) { CounterInfo counter; @@ -550,7 +614,7 @@ bool EventSelectionSet::HandleCpuOnlineEvent(int cpu) { for (auto& group : groups_) { for (const auto& tid : threads) { std::string failed_event_type; - if (!OpenEventFile(group, tid, cpu, &failed_event_type)) { + if (!OpenEventFilesOnGroup(group, tid, cpu, &failed_event_type)) { // If failed to open event files, maybe the cpu has been offlined. PLOG(WARNING) << "failed to open perf event file for event_type " << failed_event_type << " for " |