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 | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 7 | #include <base/strings/string_number_conversions.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 8 | #include <base/strings/string_split.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 9 | #include <base/strings/string_util.h> |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 10 | #include <sys/utsname.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 13 | #include <map> |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 15 | #include "debugd/src/process_with_output.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 16 | |
Ben Chan | 55903dd | 2014-04-24 00:29:04 -0700 | [diff] [blame] | 17 | using base::StringPrintf; |
| 18 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 19 | namespace debugd { |
| 20 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 21 | namespace { |
| 22 | |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 23 | const char kUnsupportedPerfToolErrorName[] = |
| 24 | "org.chromium.debugd.error.UnsupportedPerfTool"; |
| 25 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 26 | // Location of quipper on ChromeOS. |
| 27 | const char kQuipperLocation[] = "/usr/bin/quipper"; |
| 28 | |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 29 | enum PerfSubcommand { |
| 30 | PERF_COMMAND_RECORD, |
| 31 | PERF_COMMAND_STAT, |
| 32 | PERF_COMMAND_MEM, |
| 33 | PERF_COMMAND_UNSUPPORTED, |
| 34 | }; |
| 35 | |
| 36 | // Returns one of the above enums given an vector of perf arguments, starting |
| 37 | // with "perf" itself in |args[0]|. |
| 38 | PerfSubcommand GetPerfSubcommandType(const std::vector<std::string>& args) { |
| 39 | if (args[0] == "perf" && args.size() > 1) { |
| 40 | if (args[1] == "record") |
| 41 | return PERF_COMMAND_RECORD; |
| 42 | if (args[1] == "stat") |
| 43 | return PERF_COMMAND_STAT; |
| 44 | if (args[1] == "mem") |
| 45 | return PERF_COMMAND_MEM; |
| 46 | } |
| 47 | |
| 48 | return PERF_COMMAND_UNSUPPORTED; |
| 49 | } |
| 50 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 51 | } // namespace |
| 52 | |
David Sharp | 131e86b | 2015-11-17 12:55:31 -0800 | [diff] [blame] | 53 | PerfTool::PerfTool() {} |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 54 | |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 55 | int PerfTool::GetPerfOutput(const uint32_t& duration_secs, |
| 56 | const std::vector<std::string>& perf_args, |
| 57 | std::vector<uint8_t>* perf_data, |
| 58 | std::vector<uint8_t>* perf_stat, |
| 59 | DBus::Error* error) { |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 60 | PerfSubcommand subcommand = GetPerfSubcommandType(perf_args); |
| 61 | if (subcommand == PERF_COMMAND_UNSUPPORTED) { |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 62 | error->set(kUnsupportedPerfToolErrorName, |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 63 | "perf_args must begin with {\"perf\", \"record\"}, " |
| 64 | " {\"perf\", \"stat\"}, or {\"perf\", \"mem\"}"); |
David Sharp | 6190131 | 2015-08-05 13:32:07 -0700 | [diff] [blame] | 65 | return -1; |
| 66 | } |
| 67 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 68 | std::string output_string; |
| 69 | int result = |
| 70 | GetPerfOutputHelper(duration_secs, perf_args, error, &output_string); |
| 71 | |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 72 | switch (subcommand) { |
| 73 | case PERF_COMMAND_RECORD: |
| 74 | case PERF_COMMAND_MEM: |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 75 | perf_data->assign(output_string.begin(), output_string.end()); |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 76 | break; |
| 77 | case PERF_COMMAND_STAT: |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 78 | perf_stat->assign(output_string.begin(), output_string.end()); |
Simon Que | ecb0835 | 2015-10-02 17:38:12 -0700 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | // Discard the output. |
| 82 | break; |
| 83 | } |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 88 | int PerfTool::GetPerfOutputHelper(const uint32_t& duration_secs, |
| 89 | const std::vector<std::string>& perf_args, |
| 90 | DBus::Error* error, |
| 91 | std::string* data_string) { |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 92 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 93 | // completion, then gather up its output to return it. |
| 94 | ProcessWithOutput process; |
| 95 | process.SandboxAs("root", "root"); |
| 96 | if (!process.Init()) |
| 97 | *data_string = "<process init failed>"; |
| 98 | // If you're going to add switches to a command, have a look at the Process |
| 99 | // interface; there's support for adding options specifically. |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 100 | process.AddArg(kQuipperLocation); |
| 101 | process.AddArg(StringPrintf("%u", duration_secs)); |
David Sharp | dada3d0 | 2015-02-09 18:24:48 -0800 | [diff] [blame] | 102 | for (const auto& arg : perf_args) { |
| 103 | process.AddArg(arg); |
| 104 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 105 | // Run the process to completion. If the process might take a while, you may |
| 106 | // have to make this asynchronous using .Start(). |
| 107 | int status = process.Run(); |
| 108 | if (status != 0) |
| 109 | *data_string = StringPrintf("<process exited with status: %d", status); |
| 110 | process.GetOutput(data_string); |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 111 | |
| 112 | return status; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 115 | } // namespace debugd |