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 | |
| 5 | #include "perf_tool.h" |
| 6 | |
| 7 | #include "process_with_output.h" |
| 8 | |
| 9 | namespace debugd { |
| 10 | |
| 11 | PerfTool::PerfTool() { } |
| 12 | |
| 13 | PerfTool::~PerfTool() { } |
| 14 | |
| 15 | // Tool methods have the same signature as the generated DBus adaptors. Most |
| 16 | // pertinently, this means they take their DBus::Error argument as a non-const |
| 17 | // reference (hence the NOLINT). Tool methods are generally written in |
| 18 | // can't-fail style, since their output is usually going to be displayed to the |
| 19 | // user; instead of returning a DBus exception, we tend to return a string |
| 20 | // indicating what went wrong. |
| 21 | std::vector<uint8> PerfTool::GetPerfData(const uint32_t& duration, |
| 22 | DBus::Error& error) { // NOLINT |
| 23 | std::string output_string; |
| 24 | GetPerfDataHelper(duration, error, &output_string); |
| 25 | std::vector<uint8> output_vector(output_string.begin(), |
| 26 | output_string.end()); |
| 27 | return output_vector; |
| 28 | } |
| 29 | |
| 30 | void PerfTool::GetPerfDataHelper(const uint32_t& duration, |
| 31 | DBus::Error& error, |
| 32 | std::string* data_string) { // NOLINT |
| 33 | std::string path = "/usr/bin/quipper"; |
| 34 | |
| 35 | if (path.length() > PATH_MAX) |
| 36 | *data_string = "<path too long>"; |
| 37 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 38 | // completion, then gather up its output to return it. |
| 39 | ProcessWithOutput process; |
| 40 | process.SandboxAs("root", "root"); |
| 41 | if (!process.Init()) |
| 42 | *data_string = "<process init failed>"; |
| 43 | // If you're going to add switches to a command, have a look at the Process |
| 44 | // interface; there's support for adding options specifically. |
| 45 | process.AddArg(path); |
| 46 | process.AddArg(StringPrintf("%u", duration)); |
| 47 | process.AddArg("/usr/sbin/perf record -a"); |
| 48 | // Run the process to completion. If the process might take a while, you may |
| 49 | // have to make this asynchronous using .Start(). |
| 50 | int status = process.Run(); |
| 51 | if (status != 0) |
| 52 | *data_string = StringPrintf("<process exited with status: %d", status); |
| 53 | process.GetOutput(data_string); |
| 54 | } |
| 55 | |
| 56 | }; // namespace debugd |
| 57 | |