Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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> |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 21 | #include <sched.h> |
Yabin Cui | 987d9ab | 2016-07-15 14:08:48 -0700 | [diff] [blame] | 22 | #include <sys/prctl.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 23 | #include <sys/wait.h> |
| 24 | #include <unistd.h> |
| 25 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 27 | #include <android-base/strings.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 28 | |
Yabin Cui | 3485af9 | 2021-10-27 10:50:53 -0700 | [diff] [blame] | 29 | #include "environment.h" |
| 30 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 31 | namespace simpleperf { |
| 32 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 33 | std::unique_ptr<Workload> Workload::CreateWorkload(const std::vector<std::string>& args) { |
Yabin Cui | 3485af9 | 2021-10-27 10:50:53 -0700 | [diff] [blame] | 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 |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 40 | std::unique_ptr<Workload> workload(new Workload(args, std::function<void()>())); |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 41 | if (workload != nullptr && workload->CreateNewProcess()) { |
| 42 | return workload; |
| 43 | } |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 47 | std::unique_ptr<Workload> Workload::CreateWorkload(const std::function<void()>& function) { |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 48 | std::unique_ptr<Workload> workload(new Workload(std::vector<std::string>(), function)); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 49 | if (workload != nullptr && workload->CreateNewProcess()) { |
| 50 | return workload; |
| 51 | } |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 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) { |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 59 | LOG(ERROR) << "Failed to run cmd " << arg_str << ", exitcode " << ret; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | return ret == 0; |
| 63 | } |
| 64 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 65 | Workload::Workload(const std::vector<std::string>& args, const std::function<void()>& function) |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 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) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 72 | kill_function_ = [](pid_t pid) { kill(pid, SIGKILL); }; |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Yabin Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 75 | Workload::~Workload() { |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 76 | if (work_pid_ != -1 && work_state_ != NotYetCreateNewProcess) { |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 77 | if (!Workload::WaitChildProcess(false, false, nullptr)) { |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 78 | kill_function_(work_pid_); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 79 | Workload::WaitChildProcess(true, true, nullptr); |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 80 | } |
Yabin Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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]); |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 119 | ChildProcessFn(start_signal_pipe[0], exec_child_pipe[1]); |
Yabin Cui | c8fcde2 | 2015-09-24 12:37:27 -0700 | [diff] [blame] | 120 | _exit(0); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 132 | void Workload::ChildProcessFn(int start_signal_fd, int exec_child_fd) { |
Yabin Cui | 987d9ab | 2016-07-15 14:08:48 -0700 | [diff] [blame] | 133 | // Die if parent exits. |
| 134 | prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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); |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 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 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 159 | } else { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 160 | PLOG(ERROR) << "child process failed to receive start_signal, nread = " << nread; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 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) { |
Yabin Cui | 5aded99 | 2022-08-25 10:51:11 -0700 | [diff] [blame] | 170 | PLOG(WARNING) << "sched_setaffinity failed"; |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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) { |
Yabin Cui | 42aa127 | 2015-09-18 11:10:55 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | work_state_ = Started; |
| 195 | return true; |
| 196 | } |
| 197 | |
Yabin Cui | 248ef5e | 2021-12-16 14:09:39 -0800 | [diff] [blame] | 198 | bool Workload::WaitChildProcess(bool wait_forever, int* exit_code) { |
| 199 | return WaitChildProcess(wait_forever, false, exit_code); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool Workload::WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code) { |
| 203 | if (work_state_ == Finished) { |
| 204 | return true; |
| 205 | } |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 206 | bool finished = false; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 207 | int status; |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 208 | pid_t result = TEMP_FAILURE_RETRY(waitpid(work_pid_, &status, (wait_forever ? 0 : WNOHANG))); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 209 | if (result == work_pid_) { |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 210 | finished = true; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 211 | work_state_ = Finished; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 212 | if (WIFSIGNALED(status)) { |
Yabin Cui | 6d1ee48 | 2016-07-07 15:00:10 -0700 | [diff] [blame] | 213 | if (!(is_child_killed && WTERMSIG(status) == SIGKILL)) { |
| 214 | LOG(WARNING) << "child process was terminated by signal " << strsignal(WTERMSIG(status)); |
| 215 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 222 | } |
| 223 | } else if (result == -1) { |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 224 | PLOG(ERROR) << "waitpid() failed"; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 225 | } |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 226 | return finished; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 227 | } |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 228 | |
| 229 | } // namespace simpleperf |