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