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 | |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 7 | #include <base/strings/string_split.h> |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 8 | |
| 9 | #include "cpu_info_parser.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 10 | #include "process_with_output.h" |
| 11 | |
Ben Chan | 55903dd | 2014-04-24 00:29:04 -0700 | [diff] [blame] | 12 | using base::StringPrintf; |
| 13 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // Location of quipper on ChromeOS. |
| 17 | const char kQuipperLocation[] = "/usr/bin/quipper"; |
| 18 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 19 | // This is the key in /proc/cpuinfo whose value is the model name of the CPU. |
| 20 | const char kCPUModelNameKey[] = "model name"; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 21 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 22 | // Processor model name substrings for which we have perf commands. |
| 23 | const char* kCPUOddsFiles[] = { |
| 24 | "unknown", |
| 25 | "core", |
| 26 | "arm", |
| 27 | }; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 28 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 29 | // Prefix path to attach to the CPU odds file. |
Ahmad Sharif | bdb3391 | 2013-07-16 11:44:49 -0400 | [diff] [blame] | 30 | const char kCPUOddsFilePrefix[] = "/etc/perf_commands/"; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 31 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 32 | // Suffix to attach to the CPU odds file. |
| 33 | const char kCPUOddsFileSuffix[] = ".txt"; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 34 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 35 | // Goes through the list of kCPUOddsFiles, and if the any of those strings is a |
| 36 | // substring of the |cpu_model_name|, returns that string. If no matches are |
| 37 | // found, returns the first string of |kCPUOddsFiles| ("unknown"). |
| 38 | void GetOddsFilenameForCPU(const std::string& cpu_model_name, |
| 39 | std::string* odds_filename) { |
| 40 | std::string lowered_cpu_model_name = StringToLowerASCII(cpu_model_name); |
| 41 | for (size_t i = 0; i < arraysize(kCPUOddsFiles); ++i) { |
| 42 | if (lowered_cpu_model_name.find(kCPUOddsFiles[i]) != std::string::npos) { |
| 43 | *odds_filename = kCPUOddsFiles[i]; |
| 44 | return; |
| 45 | } |
| 46 | } |
| 47 | *odds_filename = kCPUOddsFiles[0]; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 52 | namespace debugd { |
| 53 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 54 | PerfTool::PerfTool() { |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 55 | std::string cpu_model_name; |
| 56 | debugd::CPUInfoParser cpu_info_parser; |
| 57 | cpu_info_parser.GetKey(kCPUModelNameKey, &cpu_model_name); |
| 58 | std::string odds_filename; |
| 59 | GetOddsFilenameForCPU(cpu_model_name, &odds_filename); |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 60 | std::string odds_file_path = |
| 61 | std::string(kCPUOddsFilePrefix) + odds_filename + kCPUOddsFileSuffix; |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 62 | random_selector_.SetOddsFromFile(odds_file_path); |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 63 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 64 | |
| 65 | PerfTool::~PerfTool() { } |
| 66 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 67 | std::vector<uint8> PerfTool::GetRichPerfData(const uint32_t& duration_secs, |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 68 | DBus::Error* error) { |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 69 | std::string perf_command_line; |
| 70 | random_selector_.GetNext(&perf_command_line); |
| 71 | std::string output_string; |
| 72 | GetPerfDataHelper(duration_secs, perf_command_line, error, &output_string); |
| 73 | return std::vector<uint8>(output_string.begin(), output_string.end()); |
| 74 | } |
| 75 | |
| 76 | void PerfTool::GetPerfDataHelper(const uint32_t& duration_secs, |
| 77 | const std::string& perf_command_line, |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 78 | DBus::Error* error, |
| 79 | std::string* data_string) { |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 80 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 81 | // completion, then gather up its output to return it. |
| 82 | ProcessWithOutput process; |
| 83 | process.SandboxAs("root", "root"); |
| 84 | if (!process.Init()) |
| 85 | *data_string = "<process init failed>"; |
| 86 | // If you're going to add switches to a command, have a look at the Process |
| 87 | // interface; there's support for adding options specifically. |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 88 | process.AddArg(kQuipperLocation); |
| 89 | process.AddArg(StringPrintf("%u", duration_secs)); |
| 90 | process.AddArg(perf_command_line); |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 91 | // Run the process to completion. If the process might take a while, you may |
| 92 | // have to make this asynchronous using .Start(). |
| 93 | int status = process.Run(); |
| 94 | if (status != 0) |
| 95 | *data_string = StringPrintf("<process exited with status: %d", status); |
| 96 | process.GetOutput(data_string); |
| 97 | } |
| 98 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 99 | } // namespace debugd |