• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include "workload.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/prctl.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24 
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
27 
28 namespace simpleperf {
29 
CreateWorkload(const std::vector<std::string> & args)30 std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) {
31   std::unique_ptr<Workload> workload(new Workload(args, std::function<void()>()));
32   if (workload != nullptr && workload->CreateNewProcess()) {
33     return workload;
34   }
35   return nullptr;
36 }
37 
CreateWorkload(const std::function<void ()> & function)38 std::unique_ptr<Workload> Workload::CreateWorkload(const std::function<void()>& function) {
39   std::unique_ptr<Workload> workload(new Workload(std::vector<std::string>(), function));
40   if (workload != nullptr && workload->CreateNewProcess()) {
41     return workload;
42   }
43   return nullptr;
44 }
45 
RunCmd(const std::vector<std::string> & args,bool report_error)46 bool Workload::RunCmd(const std::vector<std::string>& args, bool report_error) {
47   std::string arg_str = android::base::Join(args, ' ');
48   int ret = system(arg_str.c_str());
49   if (ret != 0 && report_error) {
50     LOG(ERROR) << "Failed to run cmd " << arg_str << ", exitcode " << ret;
51     return false;
52   }
53   return ret == 0;
54 }
55 
Workload(const std::vector<std::string> & args,const std::function<void ()> & function)56 Workload::Workload(const std::vector<std::string>& args, const std::function<void()>& function)
57     : work_state_(NotYetCreateNewProcess),
58       child_proc_args_(args),
59       child_proc_function_(function),
60       work_pid_(-1),
61       start_signal_fd_(-1),
62       exec_child_fd_(-1) {
63   kill_function_ = [](pid_t pid) { kill(pid, SIGKILL); };
64 }
65 
~Workload()66 Workload::~Workload() {
67   if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) {
68     if (!Workload::WaitChildProcess(false, false, nullptr)) {
69       kill_function_(work_pid_);
70       Workload::WaitChildProcess(true, true, nullptr);
71     }
72   }
73   if (start_signal_fd_ != -1) {
74     close(start_signal_fd_);
75   }
76   if (exec_child_fd_ != -1) {
77     close(exec_child_fd_);
78   }
79 }
80 
CreateNewProcess()81 bool Workload::CreateNewProcess() {
82   CHECK_EQ(work_state_, NotYetCreateNewProcess);
83 
84   int start_signal_pipe[2];
85   if (pipe2(start_signal_pipe, O_CLOEXEC) != 0) {
86     PLOG(ERROR) << "pipe2() failed";
87     return false;
88   }
89 
90   int exec_child_pipe[2];
91   if (pipe2(exec_child_pipe, O_CLOEXEC) != 0) {
92     PLOG(ERROR) << "pipe2() failed";
93     close(start_signal_pipe[0]);
94     close(start_signal_pipe[1]);
95     return false;
96   }
97 
98   pid_t pid = fork();
99   if (pid == -1) {
100     PLOG(ERROR) << "fork() failed";
101     close(start_signal_pipe[0]);
102     close(start_signal_pipe[1]);
103     close(exec_child_pipe[0]);
104     close(exec_child_pipe[1]);
105     return false;
106   } else if (pid == 0) {
107     // In child process.
108     close(start_signal_pipe[1]);
109     close(exec_child_pipe[0]);
110     ChildProcessFn(start_signal_pipe[0], exec_child_pipe[1]);
111     _exit(0);
112   }
113   // In parent process.
114   close(start_signal_pipe[0]);
115   close(exec_child_pipe[1]);
116   start_signal_fd_ = start_signal_pipe[1];
117   exec_child_fd_ = exec_child_pipe[0];
118   work_pid_ = pid;
119   work_state_ = NotYetStartNewProcess;
120   return true;
121 }
122 
ChildProcessFn(int start_signal_fd,int exec_child_fd)123 void Workload::ChildProcessFn(int start_signal_fd, int exec_child_fd) {
124   // Die if parent exits.
125   prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
126 
127   char start_signal = 0;
128   ssize_t nread = TEMP_FAILURE_RETRY(read(start_signal_fd, &start_signal, 1));
129   if (nread == 1 && start_signal == 1) {
130     close(start_signal_fd);
131     if (child_proc_function_) {
132       close(exec_child_fd);
133       child_proc_function_();
134     } else {
135       char* argv[child_proc_args_.size() + 1];
136       for (size_t i = 0; i < child_proc_args_.size(); ++i) {
137         argv[i] = &child_proc_args_[i][0];
138       }
139       argv[child_proc_args_.size()] = nullptr;
140       execvp(argv[0], argv);
141       // If execvp() succeed, we will not arrive here. But if it failed, we need to
142       // report the failure to the parent process by writing 1 to exec_child_fd.
143       int saved_errno = errno;
144       char exec_child_failed = 1;
145       TEMP_FAILURE_RETRY(write(exec_child_fd, &exec_child_failed, 1));
146       close(exec_child_fd);
147       errno = saved_errno;
148       PLOG(ERROR) << "child process failed to execvp(" << argv[0] << ")";
149     }
150   } else {
151     PLOG(ERROR) << "child process failed to receive start_signal, nread = " << nread;
152   }
153 }
154 
Start()155 bool Workload::Start() {
156   CHECK_EQ(work_state_, NotYetStartNewProcess);
157   char start_signal = 1;
158   ssize_t nwrite = TEMP_FAILURE_RETRY(write(start_signal_fd_, &start_signal, 1));
159   if (nwrite != 1) {
160     PLOG(ERROR) << "write start signal failed";
161     return false;
162   }
163   char exec_child_failed;
164   ssize_t nread = TEMP_FAILURE_RETRY(read(exec_child_fd_, &exec_child_failed, 1));
165   if (nread != 0) {
166     if (nread == -1) {
167       PLOG(ERROR) << "failed to receive error message from child process";
168     } else {
169       LOG(ERROR) << "received error message from child process";
170     }
171     return false;
172   }
173   work_state_ = Started;
174   return true;
175 }
176 
WaitChildProcess(int * exit_code)177 bool Workload::WaitChildProcess(int* exit_code) {
178   return WaitChildProcess(true, false, exit_code);
179 }
180 
WaitChildProcess(bool wait_forever,bool is_child_killed,int * exit_code)181 bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) {
182   if (work_state_ == Finished) {
183     return true;
184   }
185   bool finished = false;
186   int status;
187   pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG)));
188   if (result == work_pid_) {
189     finished = true;
190     work_state_ = Finished;
191     if (WIFSIGNALED(status)) {
192       if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) {
193         LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status));
194       }
195     } else if (WIFEXITED(status)) {
196       if (exit_code != nullptr) {
197         *exit_code = WEXITSTATUS(status);
198       } else if (WEXITSTATUS(status) != 0) {
199         LOG(WARNING) << "child process exited with exit code " << WEXITSTATUS(status);
200       }
201     }
202   } else if (result == -1) {
203     PLOG(ERROR) << "waitpid() failed";
204   }
205   return finished;
206 }
207 
208 }  // namespace simpleperf
209