blob: 42b04c69af112473d146489074a49f92d66b70b3 [file] [log] [blame]
Ahmad Sharifae1714d2013-01-17 11:29:37 -08001// 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 Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/perf_tool.h"
Ahmad Sharifae1714d2013-01-17 11:29:37 -08006
David Sharpe01f7252015-06-30 16:24:05 -07007#include <base/strings/string_number_conversions.h>
Ben Chan9953a592014-02-05 23:32:00 -08008#include <base/strings/string_split.h>
Simon Que21bb7902014-07-28 16:17:20 -07009#include <base/strings/string_util.h>
Simon Que795327b2015-03-16 17:42:35 -070010#include <sys/utsname.h>
Simon Que21bb7902014-07-28 16:17:20 -070011
12#include <algorithm>
David Sharpe01f7252015-06-30 16:24:05 -070013#include <map>
Ahmad Sharif3abea3c2013-05-13 20:43:04 -070014
Alex Vakulenko262be3f2014-07-30 15:25:50 -070015#include "debugd/src/cpu_info_parser.h"
16#include "debugd/src/process_with_output.h"
Simon Que795327b2015-03-16 17:42:35 -070017#include "debugd/src/random_selector.h"
Ahmad Sharifae1714d2013-01-17 11:29:37 -080018
Ben Chan55903dd2014-04-24 00:29:04 -070019using base::StringPrintf;
20
David Sharpe01f7252015-06-30 16:24:05 -070021namespace debugd {
22
Ahmad Shariff5597f62013-04-25 12:25:41 -070023namespace {
24
David Sharp61901312015-08-05 13:32:07 -070025const char kUnsupportedPerfToolErrorName[] =
26 "org.chromium.debugd.error.UnsupportedPerfTool";
27
Ahmad Shariff5597f62013-04-25 12:25:41 -070028// Location of quipper on ChromeOS.
29const char kQuipperLocation[] = "/usr/bin/quipper";
30
Simon Que21bb7902014-07-28 16:17:20 -070031// This is registered trademark symbol that appears in model name strings.
32const char kRegisteredTrademarkSymbol[] = "(R)";
33
Ahmad Sharif3abea3c2013-05-13 20:43:04 -070034// Processor model name substrings for which we have perf commands.
Simon Que795327b2015-03-16 17:42:35 -070035
36// For 64-bit x86 processors.
37const char* kx86_64CPUOddsFiles[] = {
Simon Que795327b2015-03-16 17:42:35 -070038 NULL,
Ahmad Sharif3abea3c2013-05-13 20:43:04 -070039};
Ahmad Shariff5597f62013-04-25 12:25:41 -070040
Simon Que795327b2015-03-16 17:42:35 -070041// For 32-bit x86 processors.
42const char* kx86_32CPUOddsFiles[] = {
43 // 32-bit x86 doesn't have any special cases, so all processors use the
44 // default commands. Add future special cases here.
45 NULL,
46};
47
48// For ARMv7 processors.
49const char* kARMv7CPUOddsFiles[] = {
50 // ARMv7 doesn't have any special cases, so all processors use the default
51 // commands. Add future special cases here.
52 NULL,
53};
54
55// For miscellaneous processors models of a known architecture.
56const char kMiscCPUModelOddsFile[] = "default";
57
58// Odds file name for miscellaneous processor architectures.
59const char kMiscCPUArchOddsFile[] = "unknown";
60
Ahmad Sharif3abea3c2013-05-13 20:43:04 -070061// Prefix path to attach to the CPU odds file.
Ahmad Sharifbdb33912013-07-16 11:44:49 -040062const char kCPUOddsFilePrefix[] = "/etc/perf_commands/";
Ahmad Shariff5597f62013-04-25 12:25:41 -070063
Ahmad Sharif3abea3c2013-05-13 20:43:04 -070064// Suffix to attach to the CPU odds file.
65const char kCPUOddsFileSuffix[] = ".txt";
Ahmad Shariff5597f62013-04-25 12:25:41 -070066
David Sharpe01f7252015-06-30 16:24:05 -070067const std::map<std::string, std::string> kIntelUarchFileTable {
68 // These were found on various sources on the Internet. Main ones are:
69 // http://instlatx64.atw.hu/ for CPUID to model name and
70 // http://www.cpu-world.com for model name to microarchitecture
71 // {"06_1C", "Bonnell"}, // Atom
72 // {"06_26", "Bonnell"}, // Atom
73 // {"06_36", "Saltwell"}, // Atom
74 // {"06_4C", "Airmont"}, // Braswell
75 // {"06_4E", "Skylake"},
David Sharp75bfc0e2015-07-09 14:50:10 -070076 // {"06_37", "Silvermont"},
77 {"06_56", "Broadwell"}, // Broadwell-DE
78 {"06_47", "Broadwell"}, // Broadwell-H
79 {"06_3D", "Broadwell"},
David Sharpe01f7252015-06-30 16:24:05 -070080 {"06_3C", "Haswell"},
81 {"06_3F", "Haswell"},
82 {"06_45", "Haswell"},
83 {"06_46", "Haswell"},
84 {"06_3A", "IvyBridge"},
85 {"06_3E", "IvyBridge"},
86 {"06_2A", "SandyBridge"},
87 {"06_2D", "SandyBridge"},
88 // {"06_0F", "Merom"},
89 // {"06_16", "Merom"},
90 // {"06_17", "Nehalem"},
91 // {"06_1A", "Nehalem"},
92 // {"06_1D", "Nehalem"},
93 // {"06_1E", "Nehalem"},
94 // {"06_1F", "Nehalem"},
95 // {"06_2E", "Nehalem"},
96 // {"06_0D", "Dothan"},
97 // {"06_09", "Banias"},
98 // {"0F_03", "Prescott"},
99 // {"0F_04", "Prescott"},
100 // {"0F_06", "Presler"},
101 // {"06_25", "Westmere"},
102 // {"06_2C", "Westmere"},
103 // {"06_2F", "Westmere"},
104};
105
Simon Que21bb7902014-07-28 16:17:20 -0700106// Converts an CPU model name string into a format that can be used as a file
107// name. The rules are:
108// - Replace spaces with hyphens.
109// - Strip all "(R)" symbols.
110// - Convert to lower case.
111std::string ModelNameToFileName(const std::string& model_name) {
112 std::string result = model_name;
113 std::replace(result.begin(), result.end(), ' ', '-');
114 ReplaceSubstringsAfterOffset(&result, 0, kRegisteredTrademarkSymbol, "");
Ben Chane1ca84e2014-09-05 08:20:59 -0700115 return base::StringToLowerASCII(result);
Simon Que21bb7902014-07-28 16:17:20 -0700116}
117
David Sharpe01f7252015-06-30 16:24:05 -0700118// For the given |cpu_info|, look for the CPU odds file that corresponds to
119// this CPU. If no matches are found for |cpu_info.arch()|, return the odds
120// file for unknown CPU types. If the arch is valid, but no matches are
121// found for |cpu_info.model_name()|, return the odds file for unknown models
122// of the CPU architecture.
123std::string GetOddsFilenameForCPU(const PerfTool::CPUInfoReader& cpu_info) {
124 const std::string& arch = cpu_info.arch();
125 const std::string& model_name = cpu_info.model_name();
Simon Que795327b2015-03-16 17:42:35 -0700126 const char** cpu_odds_file_list = NULL;
David Sharpe01f7252015-06-30 16:24:05 -0700127 if (arch == "i386" || arch == "i486" || arch == "i586" || arch == "i686") {
Simon Que795327b2015-03-16 17:42:35 -0700128 cpu_odds_file_list = kx86_32CPUOddsFiles;
David Sharpe01f7252015-06-30 16:24:05 -0700129 } else if (arch == "amd64" || arch == "x86_64") {
Simon Que795327b2015-03-16 17:42:35 -0700130 cpu_odds_file_list = kx86_64CPUOddsFiles;
David Sharpe01f7252015-06-30 16:24:05 -0700131 } else if (arch == "armv7l") {
Simon Que795327b2015-03-16 17:42:35 -0700132 cpu_odds_file_list = kARMv7CPUOddsFiles;
133 } else {
134 // If the CPU arch doesn't match any of the recognized arch families, just
135 // use the CPU odds file for unknown CPU types.
136 return kMiscCPUArchOddsFile;
137 }
138
David Sharpe01f7252015-06-30 16:24:05 -0700139 std::string adjusted_model_name = ModelNameToFileName(model_name);
Simon Que795327b2015-03-16 17:42:35 -0700140 for (size_t i = 0; cpu_odds_file_list[i]; ++i) {
141 if (adjusted_model_name.find(cpu_odds_file_list[i]) != std::string::npos) {
David Sharpe01f7252015-06-30 16:24:05 -0700142 return arch + "/" + cpu_odds_file_list[i];
Ahmad Sharif3abea3c2013-05-13 20:43:04 -0700143 }
144 }
David Sharpe01f7252015-06-30 16:24:05 -0700145
146 if (!cpu_info.intel_family_model().empty()) {
147 // See if we have a microarchitecture-specific file.
148 const auto& it = kIntelUarchFileTable.find(cpu_info.intel_family_model());
149 if (it != kIntelUarchFileTable.end()) {
150 const std::string& uarch = it->second;
151 return arch + "/" + uarch;
152 }
153 }
154
Simon Que795327b2015-03-16 17:42:35 -0700155 // If there isn't an odds file for the particular model, use the generic odds
156 // for the CPU arch.
David Sharpe01f7252015-06-30 16:24:05 -0700157 return arch + "/" + kMiscCPUModelOddsFile;
Ahmad Shariff5597f62013-04-25 12:25:41 -0700158}
159
160} // namespace
161
Simon Que795327b2015-03-16 17:42:35 -0700162PerfTool::PerfTool() : PerfTool(CPUInfoReader(), new RandomSelector) {}
163
164PerfTool::PerfTool(const CPUInfoReader& cpu_info,
165 RandomSelector* random_selector)
166 : random_selector_(random_selector) {
David Sharpe01f7252015-06-30 16:24:05 -0700167 std::string odds_filename = GetOddsFilenameForCPU(cpu_info);
Simon Que795327b2015-03-16 17:42:35 -0700168 random_selector_->SetOddsFromFile(
169 kCPUOddsFilePrefix + odds_filename + kCPUOddsFileSuffix);
Ahmad Shariff5597f62013-04-25 12:25:41 -0700170}
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800171
Ben Chan49d264c2014-08-06 17:38:16 -0700172std::vector<uint8_t> PerfTool::GetRichPerfData(const uint32_t& duration_secs,
173 DBus::Error* error) {
Simon Que795327b2015-03-16 17:42:35 -0700174 const std::vector<std::string>& perf_args = random_selector_->GetNext();
Simon Queac5f9cf2015-06-20 13:50:22 -0700175 if (perf_args[1] != "record")
176 return std::vector<uint8_t>();
177
Ahmad Shariff5597f62013-04-25 12:25:41 -0700178 std::string output_string;
Simon Queac5f9cf2015-06-20 13:50:22 -0700179 int result =
180 GetPerfOutputHelper(duration_secs, perf_args, error, &output_string);
181
182 if (result > 0)
183 return std::vector<uint8_t>();
184
Ben Chan49d264c2014-08-06 17:38:16 -0700185 return std::vector<uint8_t>(output_string.begin(), output_string.end());
Ahmad Shariff5597f62013-04-25 12:25:41 -0700186}
187
Simon Queac5f9cf2015-06-20 13:50:22 -0700188int PerfTool::GetRandomPerfOutput(const uint32_t& duration_secs,
189 std::vector<uint8_t>* perf_data,
190 std::vector<uint8_t>* perf_stat,
191 DBus::Error* error) {
192 const std::vector<std::string>& perf_args = random_selector_->GetNext();
David Sharp61901312015-08-05 13:32:07 -0700193 return GetPerfOutput(
194 duration_secs, perf_args, perf_data, perf_stat, error);
195}
196
197int PerfTool::GetPerfOutput(const uint32_t& duration_secs,
198 const std::vector<std::string>& perf_args,
199 std::vector<uint8_t>* perf_data,
200 std::vector<uint8_t>* perf_stat,
201 DBus::Error* error) {
202 const bool is_supported_perf_subcommand =
203 perf_args[0] == "perf" &&
204 (perf_args[1] == "record" || perf_args[1] == "stat");
205 if (!is_supported_perf_subcommand) {
206 error->set(kUnsupportedPerfToolErrorName,
207 "perf_args must begin with {\"perf\", \"record\"} "
208 "or {\"perf\", \"stat\"}");
209 return -1;
210 }
211
Simon Queac5f9cf2015-06-20 13:50:22 -0700212 std::string output_string;
213 int result =
214 GetPerfOutputHelper(duration_secs, perf_args, error, &output_string);
215
216 if (perf_args[1] == "record")
217 perf_data->assign(output_string.begin(), output_string.end());
218 else if (perf_args[1] == "stat")
219 perf_stat->assign(output_string.begin(), output_string.end());
220
221 return result;
222}
223
Simon Que795327b2015-03-16 17:42:35 -0700224PerfTool::CPUInfoReader::CPUInfoReader() {
225 // Get CPU model name, e.g. "Intel(R) Celeron(R) 2955U @ 1.40GHz".
226 debugd::CPUInfoParser cpu_info_parser;
David Sharpe01f7252015-06-30 16:24:05 -0700227 cpu_info_parser.GetKey("model name", &model_name_);
228 std::string vendor;
229 cpu_info_parser.GetKey("vendor_id", &vendor);
230 if (vendor == "GenuineIntel") {
231 std::string cpu_family;
232 cpu_info_parser.GetKey("cpu family", &cpu_family);
233 unsigned int cpu_family_int;
234 base::StringToUint(cpu_family, &cpu_family_int);
235
236 std::string model;
237 cpu_info_parser.GetKey("model", &model);
238 unsigned int model_int;
239 base::StringToUint(model, &model_int);
240
241 intel_family_model_ = StringPrintf("%02x_%02x", cpu_family_int, model_int);
242 }
Simon Que795327b2015-03-16 17:42:35 -0700243
244 // Get CPU machine hardware class, e.g. "i686", "x86_64", "armv7l".
245 struct utsname uname_info;
246 if (!uname(&uname_info))
247 arch_ = uname_info.machine;
248}
249
Simon Queac5f9cf2015-06-20 13:50:22 -0700250int PerfTool::GetPerfOutputHelper(const uint32_t& duration_secs,
251 const std::vector<std::string>& perf_args,
252 DBus::Error* error,
253 std::string* data_string) {
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800254 // This whole method is synchronous, so we create a subprocess, let it run to
255 // completion, then gather up its output to return it.
256 ProcessWithOutput process;
257 process.SandboxAs("root", "root");
258 if (!process.Init())
259 *data_string = "<process init failed>";
260 // If you're going to add switches to a command, have a look at the Process
261 // interface; there's support for adding options specifically.
Ahmad Shariff5597f62013-04-25 12:25:41 -0700262 process.AddArg(kQuipperLocation);
263 process.AddArg(StringPrintf("%u", duration_secs));
David Sharpdada3d02015-02-09 18:24:48 -0800264 for (const auto& arg : perf_args) {
265 process.AddArg(arg);
266 }
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800267 // Run the process to completion. If the process might take a while, you may
268 // have to make this asynchronous using .Start().
269 int status = process.Run();
270 if (status != 0)
271 *data_string = StringPrintf("<process exited with status: %d", status);
272 process.GetOutput(data_string);
Simon Queac5f9cf2015-06-20 13:50:22 -0700273
274 return status;
Ahmad Sharifae1714d2013-01-17 11:29:37 -0800275}
276
Ben Chana0011d82014-05-13 00:19:29 -0700277} // namespace debugd