summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2017-04-19 17:41:40 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-04-19 17:41:41 +0000
commitfaa89a17fd652dcfc02a614667ee8915da2379f7 (patch)
treee8af19f1391087e710ee5c4eb32468a33531c93b
parent05af0c8c65fa9dd0b1cdc119fedb96caf9f261a7 (diff)
parente1c33a9a73075d72e9056fc9a1d275ce2e60b1f2 (diff)
downloadextras-faa89a17fd652dcfc02a614667ee8915da2379f7.tar.gz
Merge "simpleperf: add --start_profiling_fd option for record cmd."
-rw-r--r--simpleperf/cmd_record.cpp20
-rw-r--r--simpleperf/cmd_record_test.cpp21
2 files changed, 40 insertions, 1 deletions
diff --git a/simpleperf/cmd_record.cpp b/simpleperf/cmd_record.cpp
index f1bf7aad..3376af06 100644
--- a/simpleperf/cmd_record.cpp
+++ b/simpleperf/cmd_record.cpp
@@ -137,6 +137,8 @@ class RecordCommand : public Command {
" will be unwound while recording by default. But it may lose\n"
" records as stacking unwinding can be time consuming. Use this\n"
" option to unwind the user's stack after recording.\n"
+"--start_profiling_fd fd_no After starting profiling, write \"STARTED\" to\n"
+" <fd_no>, then close <fd_no>.\n"
"--symfs <dir> Look for files with symbols relative to this directory.\n"
" This option is used to provide files with symbol table and\n"
" debug information, which are used by --dump-symbols and -g.\n"
@@ -163,7 +165,8 @@ class RecordCommand : public Command {
record_filename_("perf.data"),
start_sampling_time_in_ns_(0),
sample_record_count_(0),
- lost_record_count_(0) {
+ lost_record_count_(0),
+ start_profiling_fd_(-1) {
// Stop profiling if parent exits.
prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
}
@@ -219,6 +222,7 @@ class RecordCommand : public Command {
uint64_t sample_record_count_;
uint64_t lost_record_count_;
+ int start_profiling_fd_;
};
bool RecordCommand::Run(const std::vector<std::string>& args) {
@@ -321,6 +325,12 @@ bool RecordCommand::Run(const std::vector<std::string>& args) {
if (workload != nullptr && !workload->IsStarted() && !workload->Start()) {
return false;
}
+ if (start_profiling_fd_ != -1) {
+ if (!android::base::WriteStringToFd("STARTED", start_profiling_fd_)) {
+ PLOG(ERROR) << "failed to write to start_profiling_fd_";
+ }
+ close(start_profiling_fd_);
+ }
if (!loop->RunLoop()) {
return false;
}
@@ -512,6 +522,14 @@ bool RecordCommand::ParseOptions(const std::vector<std::string>& args,
event_selection_set_.AddMonitoredProcesses(pids);
} else if (args[i] == "--post-unwind") {
post_unwind_ = true;
+ } else if (args[i] == "--start_profiling_fd") {
+ if (!NextArgumentOrError(args, &i)) {
+ return false;
+ }
+ if (!android::base::ParseInt(args[i].c_str(), &start_profiling_fd_, 0)) {
+ LOG(ERROR) << "Invalid start_profiling_fd: " << args[i];
+ return false;
+ }
} else if (args[i] == "--symfs") {
if (!NextArgumentOrError(args, &i)) {
return false;
diff --git a/simpleperf/cmd_record_test.cpp b/simpleperf/cmd_record_test.cpp
index 2cfa57c5..85bbe34b 100644
--- a/simpleperf/cmd_record_test.cpp
+++ b/simpleperf/cmd_record_test.cpp
@@ -16,6 +16,9 @@
#include <gtest/gtest.h>
+#include <unistd.h>
+
+#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <android-base/test_utils.h>
@@ -407,3 +410,21 @@ TEST(record_cmd, donot_stop_when_having_targets) {
uint64_t end_time_in_ns = GetSystemClock();
ASSERT_GT(end_time_in_ns - start_time_in_ns, static_cast<uint64_t>(2e9));
}
+
+TEST(record_cmd, start_profiling_fd_option) {
+ int pipefd[2];
+ ASSERT_EQ(0, pipe(pipefd));
+ int read_fd = pipefd[0];
+ int write_fd = pipefd[1];
+ ASSERT_EXIT(
+ {
+ close(read_fd);
+ exit(RunRecordCmd({"--start_profiling_fd", std::to_string(write_fd)}) ? 0 : 1);
+ },
+ testing::ExitedWithCode(0), "");
+ close(write_fd);
+ std::string s;
+ ASSERT_TRUE(android::base::ReadFdToString(read_fd, &s));
+ close(read_fd);
+ ASSERT_EQ("STARTED", s);
+}