summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2017-03-07 19:06:56 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-03-07 19:06:56 +0000
commit8e9eabe7f4c4c114babdb7e2af259bc1fc0eb3c4 (patch)
tree68c37c1350406027f56d448df91a3642be92cfbd
parentdbcd26053b5fe35b72126e1aab131341cd7d973c (diff)
parent0720d488b902fa5ddea20813dfaf34e942218c48 (diff)
downloadextras-8e9eabe7f4c4c114babdb7e2af259bc1fc0eb3c4.tar.gz
Merge "simpleperf: adjust sample_freq based on max_sample_rate file."
-rw-r--r--simpleperf/environment.cpp25
-rw-r--r--simpleperf/environment.h1
-rw-r--r--simpleperf/event_fd.cpp13
3 files changed, 28 insertions, 11 deletions
diff --git a/simpleperf/environment.cpp b/simpleperf/environment.cpp
index 69f7c144..ed161c23 100644
--- a/simpleperf/environment.cpp
+++ b/simpleperf/environment.cpp
@@ -406,23 +406,30 @@ bool CheckPerfEventLimit() {
return true;
}
-bool CheckSampleFrequency(uint64_t sample_freq) {
- if (sample_freq == 0) {
- LOG(ERROR) << "Sample frequency can't be zero.";
- return false;
- }
+bool GetMaxSampleFrequency(uint64_t* max_sample_freq) {
std::string s;
if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_max_sample_rate", &s)) {
PLOG(DEBUG) << "failed to read /proc/sys/kernel/perf_event_max_sample_rate";
- // Omit the check if perf_event_max_sample_rate doesn't exist.
- return true;
+ return false;
}
s = android::base::Trim(s);
- uint64_t max_sample_freq;
- if (!android::base::ParseUint(s.c_str(), &max_sample_freq)) {
+ if (!android::base::ParseUint(s.c_str(), max_sample_freq)) {
LOG(ERROR) << "failed to parse /proc/sys/kernel/perf_event_max_sample_rate: " << s;
return false;
}
+ return true;
+}
+
+bool CheckSampleFrequency(uint64_t sample_freq) {
+ if (sample_freq == 0) {
+ LOG(ERROR) << "Sample frequency can't be zero.";
+ return false;
+ }
+ uint64_t max_sample_freq;
+ if (!GetMaxSampleFrequency(&max_sample_freq)) {
+ // Omit the check if can't read perf_event_max_sample_rate.
+ return true;
+ }
if (sample_freq > max_sample_freq) {
LOG(ERROR) << "Sample frequency " << sample_freq << " is out of range [1, "
<< max_sample_freq << "]";
diff --git a/simpleperf/environment.h b/simpleperf/environment.h
index 16df6906..11eee2ff 100644
--- a/simpleperf/environment.h
+++ b/simpleperf/environment.h
@@ -69,6 +69,7 @@ bool GetThreadName(pid_t tid, std::string* name);
bool GetValidThreadsFromThreadString(const std::string& tid_str, std::set<pid_t>* tid_set);
bool CheckPerfEventLimit();
+bool GetMaxSampleFrequency(uint64_t* max_sample_freq);
bool CheckSampleFrequency(uint64_t sample_freq);
bool CheckKernelSymbolAddresses();
diff --git a/simpleperf/event_fd.cpp b/simpleperf/event_fd.cpp
index b78da4c0..18eff2d2 100644
--- a/simpleperf/event_fd.cpp
+++ b/simpleperf/event_fd.cpp
@@ -32,6 +32,7 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
+#include "environment.h"
#include "event_attr.h"
#include "event_type.h"
#include "perf_event.h"
@@ -51,7 +52,15 @@ std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr,
if (group_event_fd != nullptr) {
group_fd = group_event_fd->perf_event_fd_;
}
- int perf_event_fd = perf_event_open(attr, tid, cpu, group_fd, 0);
+ perf_event_attr real_attr = attr;
+ if (attr.freq) {
+ uint64_t max_sample_freq;
+ if (GetMaxSampleFrequency(&max_sample_freq) && max_sample_freq < attr.sample_freq) {
+ PLOG(INFO) << "Adjust sample freq to max allowed sample freq " << max_sample_freq;
+ real_attr.sample_freq = max_sample_freq;
+ }
+ }
+ int perf_event_fd = perf_event_open(real_attr, tid, cpu, group_fd, 0);
if (perf_event_fd == -1) {
if (report_error) {
PLOG(ERROR) << "open perf_event_file (event " << event_name << ", tid "
@@ -77,7 +86,7 @@ std::unique_ptr<EventFd> EventFd::OpenEventFile(const perf_event_attr& attr,
return nullptr;
}
return std::unique_ptr<EventFd>(
- new EventFd(attr, perf_event_fd, event_name, tid, cpu));
+ new EventFd(real_attr, perf_event_fd, event_name, tid, cpu));
}
EventFd::~EventFd() {