Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/perf_tool.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 6 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 7 | #include <signal.h> |
| 8 | #include <sys/types.h> |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 10 | |
Ben Chan | 51b0c14 | 2017-01-06 18:10:27 -0800 | [diff] [blame] | 11 | #include <base/bind.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame^] | 12 | #include <base/check.h> |
| 13 | #include <base/check_op.h> |
Ben Chan | 51b0c14 | 2017-01-06 18:10:27 -0800 | [diff] [blame] | 14 | #include <base/strings/stringprintf.h> |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 15 | #include <base/time/time.h> |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 16 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 17 | #include "debugd/src/error_utils.h" |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 18 | #include "debugd/src/process_with_output.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 19 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 20 | namespace debugd { |
| 21 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 24 | const char kUnsupportedPerfToolErrorName[] = |
| 25 | "org.chromium.debugd.error.UnsupportedPerfTool"; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 26 | const char kProcessErrorName[] = "org.chromium.debugd.error.RunProcess"; |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 27 | const char kStopProcessErrorName[] = "org.chromium.debugd.error.StopProcess"; |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 28 | |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 29 | const char kArgsError[] = |
| 30 | "perf_args must begin with {\"perf\", \"record\"}, " |
| 31 | " {\"perf\", \"stat\"}, or {\"perf\", \"mem\"}"; |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 32 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 33 | // Location of quipper on ChromeOS. |
| 34 | const char kQuipperLocation[] = "/usr/bin/quipper"; |
| 35 | |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 36 | enum PerfSubcommand { |
| 37 | PERF_COMMAND_RECORD, |
| 38 | PERF_COMMAND_STAT, |
| 39 | PERF_COMMAND_MEM, |
| 40 | PERF_COMMAND_UNSUPPORTED, |
| 41 | }; |
| 42 | |
| 43 | // Returns one of the above enums given an vector of perf arguments, starting |
| 44 | // with "perf" itself in |args[0]|. |
| 45 | PerfSubcommand GetPerfSubcommandType(const std::vector<std::string>& args) { |
| 46 | if (args[0] == "perf" && args.size() > 1) { |
| 47 | if (args[1] == "record") |
| 48 | return PERF_COMMAND_RECORD; |
| 49 | if (args[1] == "stat") |
| 50 | return PERF_COMMAND_STAT; |
| 51 | if (args[1] == "mem") |
| 52 | return PERF_COMMAND_MEM; |
| 53 | } |
| 54 | |
| 55 | return PERF_COMMAND_UNSUPPORTED; |
| 56 | } |
| 57 | |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 58 | void AddQuipperArguments(brillo::Process* process, |
| 59 | const uint32_t duration_secs, |
| 60 | const std::vector<std::string>& perf_args) { |
| 61 | process->AddArg(kQuipperLocation); |
Eric Caruso | 96d03d3 | 2017-04-25 18:01:17 -0700 | [diff] [blame] | 62 | process->AddArg(base::StringPrintf("%u", duration_secs)); |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 63 | for (const auto& arg : perf_args) { |
| 64 | process->AddArg(arg); |
| 65 | } |
| 66 | } |
| 67 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 68 | } // namespace |
| 69 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 70 | PerfTool::PerfTool() { |
| 71 | signal_handler_.Init(); |
| 72 | process_reaper_.Register(&signal_handler_); |
| 73 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 74 | |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 75 | bool PerfTool::GetPerfOutput(uint32_t duration_secs, |
| 76 | const std::vector<std::string>& perf_args, |
| 77 | std::vector<uint8_t>* perf_data, |
| 78 | std::vector<uint8_t>* perf_stat, |
| 79 | int32_t* status, |
| 80 | brillo::ErrorPtr* error) { |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 81 | PerfSubcommand subcommand = GetPerfSubcommandType(perf_args); |
| 82 | if (subcommand == PERF_COMMAND_UNSUPPORTED) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 83 | DEBUGD_ADD_ERROR(error, kUnsupportedPerfToolErrorName, kArgsError); |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 84 | return false; |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 87 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 88 | // completion, then gather up its output to return it. |
| 89 | ProcessWithOutput process; |
| 90 | process.SandboxAs("root", "root"); |
| 91 | if (!process.Init()) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 92 | DEBUGD_ADD_ERROR(error, kProcessErrorName, |
| 93 | "Process initialization failure."); |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
| 97 | AddQuipperArguments(&process, duration_secs, perf_args); |
| 98 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 99 | std::string output_string; |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 100 | *status = process.Run(); |
| 101 | if (*status != 0) { |
| 102 | output_string = |
| 103 | base::StringPrintf("<process exited with status: %d>", *status); |
| 104 | } else { |
| 105 | process.GetOutput(&output_string); |
| 106 | } |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 107 | |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 108 | switch (subcommand) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 109 | case PERF_COMMAND_RECORD: |
| 110 | case PERF_COMMAND_MEM: |
| 111 | perf_data->assign(output_string.begin(), output_string.end()); |
| 112 | break; |
| 113 | case PERF_COMMAND_STAT: |
| 114 | perf_stat->assign(output_string.begin(), output_string.end()); |
| 115 | break; |
| 116 | default: |
| 117 | // Discard the output. |
| 118 | break; |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 119 | } |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 120 | |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 121 | return true; |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 124 | void PerfTool::OnQuipperProcessExited(const siginfo_t& siginfo) { |
| 125 | // Called after SIGCHLD has been received from the signalfd file descriptor. |
| 126 | // Wait() for the child process wont' block. It'll just reap the zombie child |
| 127 | // process. |
| 128 | quipper_process_->Wait(); |
| 129 | quipper_process_ = nullptr; |
Anand K Mistry | a8b97f9 | 2020-04-24 10:32:18 +1000 | [diff] [blame] | 130 | quipper_process_output_fd_.reset(); |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 131 | |
| 132 | profiler_session_id_.reset(); |
| 133 | } |
| 134 | |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 135 | bool PerfTool::GetPerfOutputFd(uint32_t duration_secs, |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 136 | const std::vector<std::string>& perf_args, |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 137 | const base::ScopedFD& stdout_fd, |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 138 | uint64_t* session_id, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 139 | brillo::ErrorPtr* error) { |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 140 | PerfSubcommand subcommand = GetPerfSubcommandType(perf_args); |
| 141 | if (subcommand == PERF_COMMAND_UNSUPPORTED) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 142 | DEBUGD_ADD_ERROR(error, kUnsupportedPerfToolErrorName, kArgsError); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 143 | return false; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 146 | if (quipper_process_) { |
| 147 | // Do not run multiple sessions at the same time. Attempt to start another |
| 148 | // profiler session using this method yields a DBus error. Note that |
| 149 | // starting another session using GetPerfOutput() will still succeed. |
| 150 | DEBUGD_ADD_ERROR(error, kProcessErrorName, "Existing perf tool running."); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | DCHECK(!profiler_session_id_); |
| 155 | |
| 156 | quipper_process_ = std::make_unique<SandboxedProcess>(); |
| 157 | quipper_process_->SandboxAs("root", "root"); |
| 158 | if (!quipper_process_->Init()) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 159 | DEBUGD_ADD_ERROR(error, kProcessErrorName, |
| 160 | "Process initialization failure."); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 161 | return false; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 164 | AddQuipperArguments(quipper_process_.get(), duration_secs, perf_args); |
| 165 | quipper_process_->BindFd(stdout_fd.get(), 1); |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 166 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 167 | if (!quipper_process_->Start()) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 168 | DEBUGD_ADD_ERROR(error, kProcessErrorName, "Process start failure."); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 169 | return false; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 170 | } |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 171 | DCHECK_GT(quipper_process_->pid(), 0); |
| 172 | |
| 173 | process_reaper_.WatchForChild( |
| 174 | FROM_HERE, quipper_process_->pid(), |
| 175 | base::Bind(&PerfTool::OnQuipperProcessExited, base::Unretained(this))); |
| 176 | |
Anand K Mistry | a8b97f9 | 2020-04-24 10:32:18 +1000 | [diff] [blame] | 177 | // When GetPerfOutputFd() is used to run the perf tool, the user will read |
| 178 | // from the read end of |stdout_fd| until the write end is closed. At that |
| 179 | // point, it may make another call to GetPerfOutputFd() and expect that will |
| 180 | // start another perf run. |stdout_fd| will be closed when the last process |
| 181 | // holding it exits, which is minijail0 in this case. However, the kernel |
| 182 | // closes fds before signaling process exit. Therefore, it's possible for |
| 183 | // |stdout_fd| to be closed and the user tries to run another |
| 184 | // GetPerfOutputFd() before we're signaled of the process exit. To mitigate |
| 185 | // this, hold on to a dup() of |stdout_fd| until we're signaled that the |
| 186 | // process has exited. This guarantees that the caller can make a new |
| 187 | // GetPerfOutputFd() call when it finishes reading the output. |
| 188 | quipper_process_output_fd_.reset(dup(stdout_fd.get())); |
| 189 | DCHECK(quipper_process_output_fd_.is_valid()); |
| 190 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 191 | // Generate an opaque, pseudo-unique, session ID using time and process ID. |
| 192 | profiler_session_id_ = *session_id = |
| 193 | static_cast<uint64_t>(base::Time::Now().ToTimeT()) << 32 | |
| 194 | (quipper_process_->pid() & 0xffffffff); |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | bool PerfTool::StopPerf(uint64_t session_id, brillo::ErrorPtr* error) { |
| 200 | if (!profiler_session_id_) { |
| 201 | DEBUGD_ADD_ERROR(error, kStopProcessErrorName, "Perf tool not started"); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | if (profiler_session_id_ != session_id) { |
| 206 | // Session ID mismatch: return a failure without affecting the existing |
| 207 | // profiler session. |
| 208 | DEBUGD_ADD_ERROR(error, kStopProcessErrorName, |
| 209 | "Invalid profile session id."); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Stop by sending SIGINT to the profiler session. The sandboxed quipper |
| 214 | // process will be reaped in OnQuipperProcessExited(). |
| 215 | if (quipper_process_) { |
| 216 | DCHECK_GT(quipper_process_->pid(), 0); |
| 217 | if (kill(quipper_process_->pid(), SIGINT) != 0) { |
| 218 | PLOG(WARNING) << "Failed to stop the profiler session."; |
| 219 | } |
| 220 | } |
| 221 | |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 222 | return true; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 225 | } // namespace debugd |