blob: 7f8d5cffbdf27230e000e746815ef9e3c7c25aae [file] [log] [blame]
Ahmad Sharifae1714d2013-01-17 11:29:37 -08001// 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 Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/perf_tool.h"
Ahmad Sharifae1714d2013-01-17 11:29:37 -08006
David Sharp16d35652016-04-05 17:20:08 -07007#include <unistd.h>
Simon Que21bb7902014-07-28 16:17:20 -07008
Ben Chan51b0c142017-01-06 18:10:27 -08009#include <base/bind.h>
10#include <base/strings/stringprintf.h>
David Sharp16d35652016-04-05 17:20:08 -070011
Eric Carusocc7106c2017-04-27 14:22:42 -070012#include "debugd/src/error_utils.h"
Alex Vakulenko262be3f2014-07-30 15:25:50 -070013#include "debugd/src/process_with_output.h"
Ahmad Sharifae1714d2013-01-17 11:29:37 -080014
David Sharpe01f7252015-06-30 16:24:05 -070015namespace debugd {
16
Ahmad Shariff5597f62013-04-25 12:25:41 -070017namespace {
18
David Sharp61901312015-08-05 13:32:07 -070019const char kUnsupportedPerfToolErrorName[] =
20 "org.chromium.debugd.error.UnsupportedPerfTool";
David Sharp16d35652016-04-05 17:20:08 -070021const char kProcessErrorName[] = "org.chromium.debugd.error.RunProcess";
David Sharp61901312015-08-05 13:32:07 -070022
Eric Carusocc7106c2017-04-27 14:22:42 -070023const char kArgsError[] = "perf_args must begin with {\"perf\", \"record\"}, "
24 " {\"perf\", \"stat\"}, or {\"perf\", \"mem\"}";
25
Ahmad Shariff5597f62013-04-25 12:25:41 -070026// Location of quipper on ChromeOS.
27const char kQuipperLocation[] = "/usr/bin/quipper";
28
Simon Queecb08352015-10-02 17:38:12 -070029enum 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]|.
38PerfSubcommand 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
David Sharp16d35652016-04-05 17:20:08 -070051void AddQuipperArguments(brillo::Process* process,
52 const uint32_t duration_secs,
53 const std::vector<std::string>& perf_args) {
54 process->AddArg(kQuipperLocation);
Eric Caruso96d03d32017-04-25 18:01:17 -070055 process->AddArg(base::StringPrintf("%u", duration_secs));
David Sharp16d35652016-04-05 17:20:08 -070056 for (const auto& arg : perf_args) {
57 process->AddArg(arg);
58 }
59}
60
61// For use with brillo::Process::SetPreExecCallback(), this runs after
62// the fork() in the child process, but before exec(). Call fork() again
63// and exit the parent (child of main process). The grandchild will get
64// orphaned, meaning init will wait() for it up so we don't have to.
65bool Orphan() {
66 if (fork() == 0) { // (grand-)child
67 return true;
68 }
69 // parent
70 ::_Exit(EXIT_SUCCESS);
71}
72
73
Ahmad Shariff5597f62013-04-25 12:25:41 -070074} // namespace
75
David Sharp131e86b2015-11-17 12:55:31 -080076PerfTool::PerfTool() {}
Ahmad Sharifae1714d2013-01-17 11:29:37 -080077
David Sharp61901312015-08-05 13:32:07 -070078int PerfTool::GetPerfOutput(const uint32_t& duration_secs,
79 const std::vector<std::string>& perf_args,
80 std::vector<uint8_t>* perf_data,
81 std::vector<uint8_t>* perf_stat,
Eric Carusocc7106c2017-04-27 14:22:42 -070082 brillo::ErrorPtr* error) {
Simon Queecb08352015-10-02 17:38:12 -070083 PerfSubcommand subcommand = GetPerfSubcommandType(perf_args);
84 if (subcommand == PERF_COMMAND_UNSUPPORTED) {
Eric Carusocc7106c2017-04-27 14:22:42 -070085 DEBUGD_ADD_ERROR(error, kUnsupportedPerfToolErrorName, kArgsError);
David Sharp61901312015-08-05 13:32:07 -070086 return -1;
87 }
88
Simon Queac5f9cf2015-06-20 13:50:22 -070089 std::string output_string;
90 int result =
Eric Carusocc7106c2017-04-27 14:22:42 -070091 GetPerfOutputHelper(duration_secs, perf_args, &output_string);
Simon Queac5f9cf2015-06-20 13:50:22 -070092
Simon Queecb08352015-10-02 17:38:12 -070093 switch (subcommand) {
94 case PERF_COMMAND_RECORD:
95 case PERF_COMMAND_MEM:
Simon Queac5f9cf2015-06-20 13:50:22 -070096 perf_data->assign(output_string.begin(), output_string.end());
Simon Queecb08352015-10-02 17:38:12 -070097 break;
98 case PERF_COMMAND_STAT:
Simon Queac5f9cf2015-06-20 13:50:22 -070099 perf_stat->assign(output_string.begin(), output_string.end());
Simon Queecb08352015-10-02 17:38:12 -0700100 break;
101 default:
102 // Discard the output.
103 break;
104 }
Simon Queac5f9cf2015-06-20 13:50:22 -0700105
106 return result;
107}
108
Eric Caruso8fe49c72017-04-25 10:43:59 -0700109bool PerfTool::GetPerfOutputFd(const uint32_t& duration_secs,
David Sharp16d35652016-04-05 17:20:08 -0700110 const std::vector<std::string>& perf_args,
Eric Carusocc7106c2017-04-27 14:22:42 -0700111 const dbus::FileDescriptor& stdout_fd,
112 brillo::ErrorPtr* error) {
David Sharp16d35652016-04-05 17:20:08 -0700113 PerfSubcommand subcommand = GetPerfSubcommandType(perf_args);
114 if (subcommand == PERF_COMMAND_UNSUPPORTED) {
Eric Carusocc7106c2017-04-27 14:22:42 -0700115 DEBUGD_ADD_ERROR(error, kUnsupportedPerfToolErrorName, kArgsError);
Eric Caruso8fe49c72017-04-25 10:43:59 -0700116 return false;
David Sharp16d35652016-04-05 17:20:08 -0700117 }
118
119 SandboxedProcess process;
120 process.SandboxAs("root", "root");
121 if (!process.Init()) {
Eric Carusocc7106c2017-04-27 14:22:42 -0700122 DEBUGD_ADD_ERROR(
123 error, kProcessErrorName, "Process initialization failure.");
Eric Caruso8fe49c72017-04-25 10:43:59 -0700124 return false;
David Sharp16d35652016-04-05 17:20:08 -0700125 }
126
127 AddQuipperArguments(&process, duration_secs, perf_args);
128
129 process.SetPreExecCallback(base::Bind(Orphan));
Eric Carusocc7106c2017-04-27 14:22:42 -0700130 process.BindFd(stdout_fd.value(), 1);
David Sharp16d35652016-04-05 17:20:08 -0700131
132 if (process.Run() != 0) {
Eric Carusocc7106c2017-04-27 14:22:42 -0700133 DEBUGD_ADD_ERROR(error, kProcessErrorName, "Process start failure.");
Eric Caruso8fe49c72017-04-25 10:43:59 -0700134 return false;
David Sharp16d35652016-04-05 17:20:08 -0700135 }
Eric Caruso8fe49c72017-04-25 10:43:59 -0700136 return true;
David Sharp16d35652016-04-05 17:20:08 -0700137}
138
Simon Queac5f9cf2015-06-20 13:50:22 -0700139int PerfTool::GetPerfOutputHelper(const uint32_t& duration_secs,
140 const std::vector<std::string>& perf_args,
Simon Queac5f9cf2015-06-20 13:50:22 -0700141 std::string* data_string) {
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800142 // This whole method is synchronous, so we create a subprocess, let it run to
143 // completion, then gather up its output to return it.
144 ProcessWithOutput process;
145 process.SandboxAs("root", "root");
Eric Caruso6e4d21a2017-04-26 15:26:04 -0700146 if (!process.Init()) {
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800147 *data_string = "<process init failed>";
Eric Caruso6e4d21a2017-04-26 15:26:04 -0700148 return -1;
149 }
David Sharp16d35652016-04-05 17:20:08 -0700150
151 AddQuipperArguments(&process, duration_secs, perf_args);
152
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800153 // Run the process to completion. If the process might take a while, you may
154 // have to make this asynchronous using .Start().
155 int status = process.Run();
Eric Caruso96d03d32017-04-25 18:01:17 -0700156 if (status != 0) {
157 *data_string =
158 base::StringPrintf("<process exited with status: %d>", status);
159 } else {
Eric Caruso6e4d21a2017-04-26 15:26:04 -0700160 process.GetOutput(data_string);
Eric Caruso96d03d32017-04-25 18:01:17 -0700161 }
Simon Queac5f9cf2015-06-20 13:50:22 -0700162
163 return status;
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800164}
165
Ben Chana0011d82014-05-13 00:19:29 -0700166} // namespace debugd