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 | |
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> |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 9 | #include <sys/utsname.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 10 | |
| 11 | #include <algorithm> |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 12 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 13 | #include "debugd/src/cpu_info_parser.h" |
| 14 | #include "debugd/src/process_with_output.h" |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 15 | #include "debugd/src/random_selector.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 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | // Location of quipper on ChromeOS. |
| 22 | const char kQuipperLocation[] = "/usr/bin/quipper"; |
| 23 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 24 | // This is the key in /proc/cpuinfo whose value is the model name of the CPU. |
| 25 | const char kCPUModelNameKey[] = "model name"; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 26 | |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 27 | // This is registered trademark symbol that appears in model name strings. |
| 28 | const char kRegisteredTrademarkSymbol[] = "(R)"; |
| 29 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 30 | // Processor model name substrings for which we have perf commands. |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 31 | |
| 32 | // For 64-bit x86 processors. |
| 33 | const char* kx86_64CPUOddsFiles[] = { |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 34 | "celeron-2955u", |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 35 | NULL, |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 36 | }; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 37 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 38 | // For 32-bit x86 processors. |
| 39 | const char* kx86_32CPUOddsFiles[] = { |
| 40 | // 32-bit x86 doesn't have any special cases, so all processors use the |
| 41 | // default commands. Add future special cases here. |
| 42 | NULL, |
| 43 | }; |
| 44 | |
| 45 | // For ARMv7 processors. |
| 46 | const char* kARMv7CPUOddsFiles[] = { |
| 47 | // ARMv7 doesn't have any special cases, so all processors use the default |
| 48 | // commands. Add future special cases here. |
| 49 | NULL, |
| 50 | }; |
| 51 | |
| 52 | // For miscellaneous processors models of a known architecture. |
| 53 | const char kMiscCPUModelOddsFile[] = "default"; |
| 54 | |
| 55 | // Odds file name for miscellaneous processor architectures. |
| 56 | const char kMiscCPUArchOddsFile[] = "unknown"; |
| 57 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 58 | // Prefix path to attach to the CPU odds file. |
Ahmad Sharif | bdb3391 | 2013-07-16 11:44:49 -0400 | [diff] [blame] | 59 | const char kCPUOddsFilePrefix[] = "/etc/perf_commands/"; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 60 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 61 | // Suffix to attach to the CPU odds file. |
| 62 | const char kCPUOddsFileSuffix[] = ".txt"; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 63 | |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 64 | // Converts an CPU model name string into a format that can be used as a file |
| 65 | // name. The rules are: |
| 66 | // - Replace spaces with hyphens. |
| 67 | // - Strip all "(R)" symbols. |
| 68 | // - Convert to lower case. |
| 69 | std::string ModelNameToFileName(const std::string& model_name) { |
| 70 | std::string result = model_name; |
| 71 | std::replace(result.begin(), result.end(), ' ', '-'); |
| 72 | ReplaceSubstringsAfterOffset(&result, 0, kRegisteredTrademarkSymbol, ""); |
Ben Chan | e1ca84e | 2014-09-05 08:20:59 -0700 | [diff] [blame] | 73 | return base::StringToLowerASCII(result); |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 76 | // For the given |cpu_arch| and |cpu_model_name|, look for the CPU odds file |
| 77 | // that corresponds to these strings. If no matches are found for |CPU_arch|, |
| 78 | // return the odds file for unknown CPU types. If |cpu_arch| is valid, but no |
| 79 | // matches are found |cpu_model_name|, return the odds file for unknown models |
| 80 | // of class |cpu_arch|. |
| 81 | std::string GetOddsFilenameForCPU(const std::string& cpu_arch, |
| 82 | const std::string& cpu_model_name) { |
| 83 | const char** cpu_odds_file_list = NULL; |
| 84 | if (cpu_arch == "i386" || cpu_arch == "i486" || cpu_arch == "i586" || |
| 85 | cpu_arch == "i686") { |
| 86 | cpu_odds_file_list = kx86_32CPUOddsFiles; |
| 87 | } else if (cpu_arch == "amd64" || cpu_arch == "x86_64") { |
| 88 | cpu_odds_file_list = kx86_64CPUOddsFiles; |
| 89 | } else if (cpu_arch == "armv7l") { |
| 90 | cpu_odds_file_list = kARMv7CPUOddsFiles; |
| 91 | } else { |
| 92 | // If the CPU arch doesn't match any of the recognized arch families, just |
| 93 | // use the CPU odds file for unknown CPU types. |
| 94 | return kMiscCPUArchOddsFile; |
| 95 | } |
| 96 | |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 97 | std::string adjusted_model_name = ModelNameToFileName(cpu_model_name); |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 98 | for (size_t i = 0; cpu_odds_file_list[i]; ++i) { |
| 99 | if (adjusted_model_name.find(cpu_odds_file_list[i]) != std::string::npos) { |
| 100 | return cpu_arch + "/" + cpu_odds_file_list[i]; |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 103 | // If there isn't an odds file for the particular model, use the generic odds |
| 104 | // for the CPU arch. |
| 105 | return cpu_arch + "/" + kMiscCPUModelOddsFile; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 110 | namespace debugd { |
| 111 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 112 | PerfTool::PerfTool() : PerfTool(CPUInfoReader(), new RandomSelector) {} |
| 113 | |
| 114 | PerfTool::PerfTool(const CPUInfoReader& cpu_info, |
| 115 | RandomSelector* random_selector) |
| 116 | : random_selector_(random_selector) { |
| 117 | std::string odds_filename = |
| 118 | GetOddsFilenameForCPU(cpu_info.arch(), cpu_info.model()); |
| 119 | random_selector_->SetOddsFromFile( |
| 120 | kCPUOddsFilePrefix + odds_filename + kCPUOddsFileSuffix); |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 121 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 122 | |
Ben Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 123 | std::vector<uint8_t> PerfTool::GetRichPerfData(const uint32_t& duration_secs, |
| 124 | DBus::Error* error) { |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 125 | const std::vector<std::string>& perf_args = random_selector_->GetNext(); |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame^] | 126 | if (perf_args[1] != "record") |
| 127 | return std::vector<uint8_t>(); |
| 128 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 129 | std::string output_string; |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame^] | 130 | int result = |
| 131 | GetPerfOutputHelper(duration_secs, perf_args, error, &output_string); |
| 132 | |
| 133 | if (result > 0) |
| 134 | return std::vector<uint8_t>(); |
| 135 | |
Ben Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 136 | return std::vector<uint8_t>(output_string.begin(), output_string.end()); |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame^] | 139 | int PerfTool::GetRandomPerfOutput(const uint32_t& duration_secs, |
| 140 | std::vector<uint8_t>* perf_data, |
| 141 | std::vector<uint8_t>* perf_stat, |
| 142 | DBus::Error* error) { |
| 143 | const std::vector<std::string>& perf_args = random_selector_->GetNext(); |
| 144 | std::string output_string; |
| 145 | int result = |
| 146 | GetPerfOutputHelper(duration_secs, perf_args, error, &output_string); |
| 147 | |
| 148 | if (perf_args[1] == "record") |
| 149 | perf_data->assign(output_string.begin(), output_string.end()); |
| 150 | else if (perf_args[1] == "stat") |
| 151 | perf_stat->assign(output_string.begin(), output_string.end()); |
| 152 | |
| 153 | return result; |
| 154 | } |
| 155 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 156 | PerfTool::CPUInfoReader::CPUInfoReader() { |
| 157 | // Get CPU model name, e.g. "Intel(R) Celeron(R) 2955U @ 1.40GHz". |
| 158 | debugd::CPUInfoParser cpu_info_parser; |
| 159 | cpu_info_parser.GetKey(kCPUModelNameKey, &model_); |
| 160 | |
| 161 | // Get CPU machine hardware class, e.g. "i686", "x86_64", "armv7l". |
| 162 | struct utsname uname_info; |
| 163 | if (!uname(&uname_info)) |
| 164 | arch_ = uname_info.machine; |
| 165 | } |
| 166 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame^] | 167 | int PerfTool::GetPerfOutputHelper(const uint32_t& duration_secs, |
| 168 | const std::vector<std::string>& perf_args, |
| 169 | DBus::Error* error, |
| 170 | std::string* data_string) { |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 171 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 172 | // completion, then gather up its output to return it. |
| 173 | ProcessWithOutput process; |
| 174 | process.SandboxAs("root", "root"); |
| 175 | if (!process.Init()) |
| 176 | *data_string = "<process init failed>"; |
| 177 | // If you're going to add switches to a command, have a look at the Process |
| 178 | // interface; there's support for adding options specifically. |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 179 | process.AddArg(kQuipperLocation); |
| 180 | process.AddArg(StringPrintf("%u", duration_secs)); |
David Sharp | dada3d0 | 2015-02-09 18:24:48 -0800 | [diff] [blame] | 181 | for (const auto& arg : perf_args) { |
| 182 | process.AddArg(arg); |
| 183 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 184 | // Run the process to completion. If the process might take a while, you may |
| 185 | // have to make this asynchronous using .Start(). |
| 186 | int status = process.Run(); |
| 187 | if (status != 0) |
| 188 | *data_string = StringPrintf("<process exited with status: %d", status); |
| 189 | process.GetOutput(data_string); |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame^] | 190 | |
| 191 | return status; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 194 | } // namespace debugd |