diff options
Diffstat (limited to 'simpleperf/workload.cpp')
-rw-r--r-- | simpleperf/workload.cpp | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/simpleperf/workload.cpp b/simpleperf/workload.cpp index dcb0e78a..b3bd459f 100644 --- a/simpleperf/workload.cpp +++ b/simpleperf/workload.cpp @@ -23,6 +23,7 @@ #include <unistd.h> #include <android-base/logging.h> +#include <android-base/strings.h> std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) { std::unique_ptr<Workload> workload(new Workload(args, std::function<void ()>())); @@ -40,11 +41,21 @@ std::unique_ptr<Workload> Workload::CreateWorkload(const std::function<void ()>& return nullptr; } +bool Workload::RunCmd(const std::vector<std::string>& args, bool report_error) { + std::string arg_str = android::base::Join(args, ' '); + int ret = system(arg_str.c_str()); + if (ret != 0 && report_error) { + PLOG(ERROR) << "Failed to run cmd " << arg_str; + return false; + } + return ret == 0; +} + Workload::~Workload() { if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) { - if (!Workload::WaitChildProcess(false, false)) { + if (!Workload::WaitChildProcess(false, false, nullptr)) { kill(work_pid_, SIGKILL); - Workload::WaitChildProcess(true, true); + Workload::WaitChildProcess(true, true, nullptr); } } if (start_signal_fd_ != -1) { @@ -151,18 +162,30 @@ bool Workload::Start() { return true; } -bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed) { +bool Workload::WaitChildProcess(int* exit_code) { + return WaitChildProcess(true, false, exit_code); +} + +bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) { + if (work_state_ == Finished) { + return true; + } bool finished = false; int status; pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG))); if (result == work_pid_) { finished = true; + work_state_ = Finished; if (WIFSIGNALED(status)) { if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) { LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status)); } - } else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { - LOG(WARNING) << "child process exited with exit code " << WEXITSTATUS(status); + } else if (WIFEXITED(status)) { + if (exit_code != nullptr) { + *exit_code = WEXITSTATUS(status); + } else if (WEXITSTATUS(status) != 0) { + LOG(WARNING) << "child process exited with exit code " << WEXITSTATUS(status); + } } } else if (result == -1) { PLOG(ERROR) << "waitpid() failed"; |