blob: 92c6537bdcdf497ef6ae5a3d9a6667ccc22764e8 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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>
Yabin6b771a22022-08-18 17:01:54 -070021#include <sched.h>
Yabin Cui987d9ab2016-07-15 14:08:48 -070022#include <sys/prctl.h>
Yabin Cui323e9452015-04-20 18:07:17 -070023#include <sys/wait.h>
24#include <unistd.h>
25
Elliott Hughes66dd09e2015-12-04 14:00:57 -080026#include <android-base/logging.h>
Yabin Cuia80f8f72017-07-12 15:50:20 -070027#include <android-base/strings.h>
Yabin Cui323e9452015-04-20 18:07:17 -070028
Yabin Cui3485af92021-10-27 10:50:53 -070029#include "environment.h"
30
Yabin Cuifaa7b922021-01-11 17:35:57 -080031namespace simpleperf {
32
Yabin Cui323e9452015-04-20 18:07:17 -070033std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) {
Yabin Cui3485af92021-10-27 10:50:53 -070034#if defined(__ANDROID__)
35 if (IsInAppUid()) {
36 LOG(ERROR) << "Running child command in app uid isn't allowed.";
37 return nullptr;
38 }
39#endif
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020040 std::unique_ptr<Workload> workload(new Workload(args, std::function<void()>()));
Yabin Cui5be914d2016-11-29 15:21:13 -080041 if (workload != nullptr && workload->CreateNewProcess()) {
42 return workload;
43 }
44 return nullptr;
45}
46
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020047std::unique_ptr<Workload> Workload::CreateWorkload(const std::function<void()>& function) {
Yabin Cui5be914d2016-11-29 15:21:13 -080048 std::unique_ptr<Workload> workload(new Workload(std::vector<std::string>(), function));
Yabin Cui323e9452015-04-20 18:07:17 -070049 if (workload != nullptr && workload->CreateNewProcess()) {
50 return workload;
51 }
52 return nullptr;
53}
54
Yabin Cuia80f8f72017-07-12 15:50:20 -070055bool 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) {
Yabin Cuif8974522017-07-17 14:36:37 -070059 LOG(ERROR) << "Failed to run cmd " << arg_str << ", exitcode " << ret;
Yabin Cuia80f8f72017-07-12 15:50:20 -070060 return false;
61 }
62 return ret == 0;
63}
64
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020065Workload::Workload(const std::vector<std::string>& args, const std::function<void()>& function)
Yabin Cui616b3a02017-07-14 15:59:56 -070066 : 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) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020072 kill_function_ = [](pid_t pid) { kill(pid, SIGKILL); };
Yabin Cui616b3a02017-07-14 15:59:56 -070073}
74
Yabin Cui7d4904d2015-06-09 13:38:42 -070075Workload::~Workload() {
Yabin Cui621a5332015-06-15 16:17:20 -070076 if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) {
Yabin Cuia80f8f72017-07-12 15:50:20 -070077 if (!Workload::WaitChildProcess(false, false, nullptr)) {
Yabin Cui616b3a02017-07-14 15:59:56 -070078 kill_function_(work_pid_);
Yabin Cuia80f8f72017-07-12 15:50:20 -070079 Workload::WaitChildProcess(true, true, nullptr);
Yabin Cui621a5332015-06-15 16:17:20 -070080 }
Yabin Cui7d4904d2015-06-09 13:38:42 -070081 }
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
Yabin Cui323e9452015-04-20 18:07:17 -070090bool 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]);
Yabin Cui5be914d2016-11-29 15:21:13 -0800119 ChildProcessFn(start_signal_pipe[0], exec_child_pipe[1]);
Yabin Cuic8fcde22015-09-24 12:37:27 -0700120 _exit(0);
Yabin Cui323e9452015-04-20 18:07:17 -0700121 }
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
Yabin Cui5be914d2016-11-29 15:21:13 -0800132void Workload::ChildProcessFn(int start_signal_fd, int exec_child_fd) {
Yabin Cui987d9ab2016-07-15 14:08:48 -0700133 // Die if parent exits.
134 prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
Yabin Cui323e9452015-04-20 18:07:17 -0700135
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);
Yabin Cui5be914d2016-11-29 15:21:13 -0800140 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 }
Yabin Cui323e9452015-04-20 18:07:17 -0700159 } else {
Yabin Cui42aa1272015-09-18 11:10:55 -0700160 PLOG(ERROR) << "child process failed to receive start_signal, nread = " << nread;
Yabin Cui323e9452015-04-20 18:07:17 -0700161 }
162}
163
Yabin6b771a22022-08-18 17:01:54 -0700164bool 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) {
Yabin Cui5aded992022-08-25 10:51:11 -0700170 PLOG(WARNING) << "sched_setaffinity failed";
Yabin6b771a22022-08-18 17:01:54 -0700171 return false;
172 }
173 return true;
174}
175
Yabin Cui323e9452015-04-20 18:07:17 -0700176bool 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) {
Yabin Cui42aa1272015-09-18 11:10:55 -0700187 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 }
Yabin Cui323e9452015-04-20 18:07:17 -0700192 return false;
193 }
194 work_state_ = Started;
195 return true;
196}
197
Yabin Cui248ef5e2021-12-16 14:09:39 -0800198bool Workload::WaitChildProcess(bool wait_forever, int* exit_code) {
199 return WaitChildProcess(wait_forever, false, exit_code);
Yabin Cuia80f8f72017-07-12 15:50:20 -0700200}
201
202bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) {
203 if (work_state_ == Finished) {
204 return true;
205 }
Yabin Cui621a5332015-06-15 16:17:20 -0700206 bool finished = false;
Yabin Cui323e9452015-04-20 18:07:17 -0700207 int status;
Yabin Cui621a5332015-06-15 16:17:20 -0700208 pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG)));
Yabin Cui323e9452015-04-20 18:07:17 -0700209 if (result == work_pid_) {
Yabin Cui621a5332015-06-15 16:17:20 -0700210 finished = true;
Yabin Cuia80f8f72017-07-12 15:50:20 -0700211 work_state_ = Finished;
Yabin Cui323e9452015-04-20 18:07:17 -0700212 if (WIFSIGNALED(status)) {
Yabin Cui6d1ee482016-07-07 15:00:10 -0700213 if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) {
214 LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status));
215 }
Yabin Cuia80f8f72017-07-12 15:50:20 -0700216 } 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 }
Yabin Cui323e9452015-04-20 18:07:17 -0700222 }
223 } else if (result == -1) {
Yabin Cui621a5332015-06-15 16:17:20 -0700224 PLOG(ERROR) << "waitpid() failed";
Yabin Cui323e9452015-04-20 18:07:17 -0700225 }
Yabin Cui621a5332015-06-15 16:17:20 -0700226 return finished;
Yabin Cui323e9452015-04-20 18:07:17 -0700227}
Yabin Cuifaa7b922021-01-11 17:35:57 -0800228
229} // namespace simpleperf