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 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 7 | #include <base/strings/string_number_conversions.h> |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 8 | #include <base/strings/string_split.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 9 | #include <base/strings/string_util.h> |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 10 | #include <sys/utsname.h> |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 13 | #include <map> |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 15 | #include "debugd/src/cpu_info_parser.h" |
| 16 | #include "debugd/src/process_with_output.h" |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 17 | #include "debugd/src/random_selector.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 18 | |
Ben Chan | 55903dd | 2014-04-24 00:29:04 -0700 | [diff] [blame] | 19 | using base::StringPrintf; |
| 20 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 21 | namespace debugd { |
| 22 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Location of quipper on ChromeOS. |
| 26 | const char kQuipperLocation[] = "/usr/bin/quipper"; |
| 27 | |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 28 | // This is registered trademark symbol that appears in model name strings. |
| 29 | const char kRegisteredTrademarkSymbol[] = "(R)"; |
| 30 | |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 31 | // Processor model name substrings for which we have perf commands. |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 32 | |
| 33 | // For 64-bit x86 processors. |
| 34 | const char* kx86_64CPUOddsFiles[] = { |
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 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 64 | const std::map<std::string, std::string> kIntelUarchFileTable { |
| 65 | // These were found on various sources on the Internet. Main ones are: |
| 66 | // http://instlatx64.atw.hu/ for CPUID to model name and |
| 67 | // http://www.cpu-world.com for model name to microarchitecture |
| 68 | // {"06_1C", "Bonnell"}, // Atom |
| 69 | // {"06_26", "Bonnell"}, // Atom |
| 70 | // {"06_36", "Saltwell"}, // Atom |
| 71 | // {"06_4C", "Airmont"}, // Braswell |
| 72 | // {"06_4E", "Skylake"}, |
David Sharp | 75bfc0e | 2015-07-09 14:50:10 -0700 | [diff] [blame] | 73 | // {"06_37", "Silvermont"}, |
| 74 | {"06_56", "Broadwell"}, // Broadwell-DE |
| 75 | {"06_47", "Broadwell"}, // Broadwell-H |
| 76 | {"06_3D", "Broadwell"}, |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 77 | {"06_3C", "Haswell"}, |
| 78 | {"06_3F", "Haswell"}, |
| 79 | {"06_45", "Haswell"}, |
| 80 | {"06_46", "Haswell"}, |
| 81 | {"06_3A", "IvyBridge"}, |
| 82 | {"06_3E", "IvyBridge"}, |
| 83 | {"06_2A", "SandyBridge"}, |
| 84 | {"06_2D", "SandyBridge"}, |
| 85 | // {"06_0F", "Merom"}, |
| 86 | // {"06_16", "Merom"}, |
| 87 | // {"06_17", "Nehalem"}, |
| 88 | // {"06_1A", "Nehalem"}, |
| 89 | // {"06_1D", "Nehalem"}, |
| 90 | // {"06_1E", "Nehalem"}, |
| 91 | // {"06_1F", "Nehalem"}, |
| 92 | // {"06_2E", "Nehalem"}, |
| 93 | // {"06_0D", "Dothan"}, |
| 94 | // {"06_09", "Banias"}, |
| 95 | // {"0F_03", "Prescott"}, |
| 96 | // {"0F_04", "Prescott"}, |
| 97 | // {"0F_06", "Presler"}, |
| 98 | // {"06_25", "Westmere"}, |
| 99 | // {"06_2C", "Westmere"}, |
| 100 | // {"06_2F", "Westmere"}, |
| 101 | }; |
| 102 | |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 103 | // Converts an CPU model name string into a format that can be used as a file |
| 104 | // name. The rules are: |
| 105 | // - Replace spaces with hyphens. |
| 106 | // - Strip all "(R)" symbols. |
| 107 | // - Convert to lower case. |
| 108 | std::string ModelNameToFileName(const std::string& model_name) { |
| 109 | std::string result = model_name; |
| 110 | std::replace(result.begin(), result.end(), ' ', '-'); |
| 111 | ReplaceSubstringsAfterOffset(&result, 0, kRegisteredTrademarkSymbol, ""); |
Ben Chan | e1ca84e | 2014-09-05 08:20:59 -0700 | [diff] [blame] | 112 | return base::StringToLowerASCII(result); |
Simon Que | 21bb790 | 2014-07-28 16:17:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 115 | // For the given |cpu_info|, look for the CPU odds file that corresponds to |
| 116 | // this CPU. If no matches are found for |cpu_info.arch()|, return the odds |
| 117 | // file for unknown CPU types. If the arch is valid, but no matches are |
| 118 | // found for |cpu_info.model_name()|, return the odds file for unknown models |
| 119 | // of the CPU architecture. |
| 120 | std::string GetOddsFilenameForCPU(const PerfTool::CPUInfoReader& cpu_info) { |
| 121 | const std::string& arch = cpu_info.arch(); |
| 122 | const std::string& model_name = cpu_info.model_name(); |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 123 | const char** cpu_odds_file_list = NULL; |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 124 | if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") { |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 125 | cpu_odds_file_list = kx86_32CPUOddsFiles; |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 126 | } else if (arch == "amd64" || arch == "x86_64") { |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 127 | cpu_odds_file_list = kx86_64CPUOddsFiles; |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 128 | } else if (arch == "armv7l") { |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 129 | cpu_odds_file_list = kARMv7CPUOddsFiles; |
| 130 | } else { |
| 131 | // If the CPU arch doesn't match any of the recognized arch families, just |
| 132 | // use the CPU odds file for unknown CPU types. |
| 133 | return kMiscCPUArchOddsFile; |
| 134 | } |
| 135 | |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 136 | std::string adjusted_model_name = ModelNameToFileName(model_name); |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 137 | for (size_t i = 0; cpu_odds_file_list[i]; ++i) { |
| 138 | if (adjusted_model_name.find(cpu_odds_file_list[i]) != std::string::npos) { |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 139 | return arch + "/" + cpu_odds_file_list[i]; |
Ahmad Sharif | 3abea3c | 2013-05-13 20:43:04 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 142 | |
| 143 | if (!cpu_info.intel_family_model().empty()) { |
| 144 | // See if we have a microarchitecture-specific file. |
| 145 | const auto& it = kIntelUarchFileTable.find(cpu_info.intel_family_model()); |
| 146 | if (it != kIntelUarchFileTable.end()) { |
| 147 | const std::string& uarch = it->second; |
| 148 | return arch + "/" + uarch; |
| 149 | } |
| 150 | } |
| 151 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 152 | // If there isn't an odds file for the particular model, use the generic odds |
| 153 | // for the CPU arch. |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 154 | return arch + "/" + kMiscCPUModelOddsFile; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace |
| 158 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 159 | PerfTool::PerfTool() : PerfTool(CPUInfoReader(), new RandomSelector) {} |
| 160 | |
| 161 | PerfTool::PerfTool(const CPUInfoReader& cpu_info, |
| 162 | RandomSelector* random_selector) |
| 163 | : random_selector_(random_selector) { |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 164 | std::string odds_filename = GetOddsFilenameForCPU(cpu_info); |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 165 | random_selector_->SetOddsFromFile( |
| 166 | kCPUOddsFilePrefix + odds_filename + kCPUOddsFileSuffix); |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 167 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 168 | |
Ben Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 169 | std::vector<uint8_t> PerfTool::GetRichPerfData(const uint32_t& duration_secs, |
| 170 | DBus::Error* error) { |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 171 | const std::vector<std::string>& perf_args = random_selector_->GetNext(); |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 172 | if (perf_args[1] != "record") |
| 173 | return std::vector<uint8_t>(); |
| 174 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 175 | std::string output_string; |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 176 | int result = |
| 177 | GetPerfOutputHelper(duration_secs, perf_args, error, &output_string); |
| 178 | |
| 179 | if (result > 0) |
| 180 | return std::vector<uint8_t>(); |
| 181 | |
Ben Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 182 | return std::vector<uint8_t>(output_string.begin(), output_string.end()); |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 185 | int PerfTool::GetRandomPerfOutput(const uint32_t& duration_secs, |
| 186 | std::vector<uint8_t>* perf_data, |
| 187 | std::vector<uint8_t>* perf_stat, |
| 188 | DBus::Error* error) { |
| 189 | const std::vector<std::string>& perf_args = random_selector_->GetNext(); |
| 190 | std::string output_string; |
| 191 | int result = |
| 192 | GetPerfOutputHelper(duration_secs, perf_args, error, &output_string); |
| 193 | |
| 194 | if (perf_args[1] == "record") |
| 195 | perf_data->assign(output_string.begin(), output_string.end()); |
| 196 | else if (perf_args[1] == "stat") |
| 197 | perf_stat->assign(output_string.begin(), output_string.end()); |
| 198 | |
| 199 | return result; |
| 200 | } |
| 201 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 202 | PerfTool::CPUInfoReader::CPUInfoReader() { |
| 203 | // Get CPU model name, e.g. "Intel(R) Celeron(R) 2955U @ 1.40GHz". |
| 204 | debugd::CPUInfoParser cpu_info_parser; |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 205 | cpu_info_parser.GetKey("model name", &model_name_); |
| 206 | std::string vendor; |
| 207 | cpu_info_parser.GetKey("vendor_id", &vendor); |
| 208 | if (vendor == "GenuineIntel") { |
| 209 | std::string cpu_family; |
| 210 | cpu_info_parser.GetKey("cpu family", &cpu_family); |
| 211 | unsigned int cpu_family_int; |
| 212 | base::StringToUint(cpu_family, &cpu_family_int); |
| 213 | |
| 214 | std::string model; |
| 215 | cpu_info_parser.GetKey("model", &model); |
| 216 | unsigned int model_int; |
| 217 | base::StringToUint(model, &model_int); |
| 218 | |
| 219 | intel_family_model_ = StringPrintf("%02x_%02x", cpu_family_int, model_int); |
| 220 | } |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 221 | |
| 222 | // Get CPU machine hardware class, e.g. "i686", "x86_64", "armv7l". |
| 223 | struct utsname uname_info; |
| 224 | if (!uname(&uname_info)) |
| 225 | arch_ = uname_info.machine; |
| 226 | } |
| 227 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 228 | int PerfTool::GetPerfOutputHelper(const uint32_t& duration_secs, |
| 229 | const std::vector<std::string>& perf_args, |
| 230 | DBus::Error* error, |
| 231 | std::string* data_string) { |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 232 | // This whole method is synchronous, so we create a subprocess, let it run to |
| 233 | // completion, then gather up its output to return it. |
| 234 | ProcessWithOutput process; |
| 235 | process.SandboxAs("root", "root"); |
| 236 | if (!process.Init()) |
| 237 | *data_string = "<process init failed>"; |
| 238 | // If you're going to add switches to a command, have a look at the Process |
| 239 | // interface; there's support for adding options specifically. |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 240 | process.AddArg(kQuipperLocation); |
| 241 | process.AddArg(StringPrintf("%u", duration_secs)); |
David Sharp | dada3d0 | 2015-02-09 18:24:48 -0800 | [diff] [blame] | 242 | for (const auto& arg : perf_args) { |
| 243 | process.AddArg(arg); |
| 244 | } |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 245 | // Run the process to completion. If the process might take a while, you may |
| 246 | // have to make this asynchronous using .Start(). |
| 247 | int status = process.Run(); |
| 248 | if (status != 0) |
| 249 | *data_string = StringPrintf("<process exited with status: %d", status); |
| 250 | process.GetOutput(data_string); |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 251 | |
| 252 | return status; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 255 | } // namespace debugd |