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