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 | |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame^] | 7 | #include <unistd.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 10 | #include <map> |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 11 | |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame^] | 12 | #include <base/callback.h> |
| 13 | #include <base/strings/string_number_conversions.h> |
| 14 | #include <base/strings/string_split.h> |
| 15 | #include <base/strings/string_util.h> |
| 16 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 17 | #include "debugd/src/process_with_output.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 18 | |
Ben Chan | 55903dd | 2014-04-24 00:29:04 -0700 | [diff] [blame] | 19 | using base::StringPrintf; |
| 20 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 21 | namespace debugd { |
| 22 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 25 | const char kUnsupportedPerfToolErrorName[] = |
| 26 | "org.chromium.debugd.error.UnsupportedPerfTool"; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame^] | 27 | const char kProcessErrorName[] = "org.chromium.debugd.error.RunProcess"; |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 28 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 29 | // Location of quipper on ChromeOS. |
| 30 | const char kQuipperLocation[] = "/usr/bin/quipper"; |
| 31 | |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 32 | enum PerfSubcommand { |
| 33 | PERF_COMMAND_RECORD, |
| 34 | PERF_COMMAND_STAT, |
| 35 | PERF_COMMAND_MEM, |
| 36 | PERF_COMMAND_UNSUPPORTED, |
| 37 | }; |
| 38 | |
| 39 | // Returns one of the above enums given an vector of perf arguments, starting |
| 40 | // with "perf" itself in |args[0]|. |
| 41 | PerfSubcommand GetPerfSubcommandType(const std::vector<std::string>& args) { |
| 42 | if (args[0] == "perf" && args.size() > 1) { |
| 43 | if (args[1] == "record") |
| 44 | return PERF_COMMAND_RECORD; |
| 45 | if (args[1] == "stat") |
| 46 | return PERF_COMMAND_STAT; |
| 47 | if (args[1] == "mem") |
| 48 | return PERF_COMMAND_MEM; |
| 49 | } |
| 50 | |
| 51 | return PERF_COMMAND_UNSUPPORTED; |
| 52 | } |
| 53 | |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame^] | 54 | void AddQuipperArguments(brillo::Process* process, |
| 55 | const uint32_t duration_secs, |
| 56 | const std::vector<std::string>& perf_args) { |
| 57 | process->AddArg(kQuipperLocation); |
| 58 | process->AddArg(StringPrintf("%u", duration_secs)); |
| 59 | for (const auto& arg : perf_args) { |
| 60 | process->AddArg(arg); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // For use with brillo::Process::SetPreExecCallback(), this runs after |
| 65 | // the fork() in the child process, but before exec(). Call fork() again |
| 66 | // and exit the parent (child of main process). The grandchild will get |
| 67 | // orphaned, meaning init will wait() for it up so we don't have to. |
| 68 | bool Orphan() { |
| 69 | if (fork() == 0) { // (grand-)child |
| 70 | return true; |
| 71 | } |
| 72 | // parent |
| 73 | ::_Exit(EXIT_SUCCESS); |
| 74 | } |
| 75 | |
| 76 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 77 | } // namespace |
| 78 | |
David Sharp | 131e86b | 2015-11-17 12:55:31 -0800 | [diff] [blame] | 79 | PerfTool::PerfTool() {} |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 80 | |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 81 | int PerfTool::GetPerfOutput(const uint32_t& duration_secs, |
| 82 | const std::vector<std::string>& perf_args, |
| 83 | std::vector<uint8_t>* perf_data, |
| 84 | std::vector<uint8_t>* perf_stat, |
| 85 | DBus::Error* error) { |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 86 | PerfSubcommand subcommand = GetPerfSubcommandType(perf_args); |
| 87 | if (subcommand == PERF_COMMAND_UNSUPPORTED) { |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 88 | error->set(kUnsupportedPerfToolErrorName, |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 89 | "perf_args must begin with {\"perf\", \"record\"}, " |
| 90 | " {\"perf\", \"stat\"}, or {\"perf\", \"mem\"}"); |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 91 | return -1; |
| 92 | } |
| 93 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 94 | std::string output_string; |
| 95 | int result = |
| 96 | GetPerfOutputHelper(duration_secs, perf_args, error, &output_string); |
| 97 | |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 98 | switch (subcommand) { |
| 99 | case PERF_COMMAND_RECORD: |
| 100 | case PERF_COMMAND_MEM: |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 101 | perf_data->assign(output_string.begin(), output_string.end()); |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 102 | break; |
| 103 | case PERF_COMMAND_STAT: |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 104 | perf_stat->assign(output_string.begin(), output_string.end()); |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 105 | break; |
| 106 | default: |
| 107 | // Discard the output. |
| 108 | break; |
| 109 | } |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 110 | |
| 111 | return result; |
| 112 | } |
| 113 | |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame^] | 114 | void PerfTool::GetPerfOutputFd(const uint32_t& duration_secs, |
| 115 | const std::vector<std::string>& perf_args, |
| 116 | const DBus::FileDescriptor& stdout_fd, |
| 117 | DBus::Error* error) { |
| 118 | PerfSubcommand subcommand = GetPerfSubcommandType(perf_args); |
| 119 | if (subcommand == PERF_COMMAND_UNSUPPORTED) { |
| 120 | error->set(kUnsupportedPerfToolErrorName, |
| 121 | "perf_args must begin with {\"perf\", \"record\"}, " |
| 122 | " {\"perf\", \"stat\"}, or {\"perf\", \"mem\"}"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | SandboxedProcess process; |
| 127 | process.SandboxAs("root", "root"); |
| 128 | if (!process.Init()) { |
| 129 | error->set(kProcessErrorName, "Process initialization failure."); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | AddQuipperArguments(&process, duration_secs, perf_args); |
| 134 | |
| 135 | process.SetPreExecCallback(base::Bind(Orphan)); |
| 136 | process.BindFd(stdout_fd.get(), 1); |
| 137 | |
| 138 | if (process.Run() != 0) { |
| 139 | error->set(kProcessErrorName, "Process start failure."); |
| 140 | } |
| 141 | } |
| 142 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 143 | int PerfTool::GetPerfOutputHelper(const uint32_t& duration_secs, |
| 144 | const std::vector<std::string>& perf_args, |
| 145 | DBus::Error* error, |
| 146 | std::string* data_string) { |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 147 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 148 | // completion, then gather up its output to return it. |
| 149 | ProcessWithOutput process; |
| 150 | process.SandboxAs("root", "root"); |
| 151 | if (!process.Init()) |
| 152 | *data_string = "<process init failed>"; |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame^] | 153 | |
| 154 | AddQuipperArguments(&process, duration_secs, perf_args); |
| 155 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 156 | // Run the process to completion. If the process might take a while, you may |
| 157 | // have to make this asynchronous using .Start(). |
| 158 | int status = process.Run(); |
| 159 | if (status != 0) |
| 160 | *data_string = StringPrintf("<process exited with status: %d", status); |
| 161 | process.GetOutput(data_string); |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 162 | |
| 163 | return status; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 166 | } // namespace debugd |