blob: 27829a6b07587d8d2d197cb94d898d5fcba7a06b [file] [log] [blame]
Anders Lewisa7b0f882017-07-24 20:01:13 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Anders Lewisa98a5fb2017-08-09 16:52:19 -070017#include <err.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070018#include <getopt.h>
19#include <math.h>
20#include <sys/resource.h>
21
22#include <map>
23#include <mutex>
24#include <sstream>
25#include <string>
Christopher Ferris858e3362017-11-30 08:53:15 -080026#include <utility>
Anders Lewisa7b0f882017-07-24 20:01:13 -070027#include <vector>
28
Christopher Ferrisd9d39be2017-08-23 18:03:51 -070029#include <android-base/file.h>
30#include <android-base/strings.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070031#include <benchmark/benchmark.h>
32#include <tinyxml2.h>
33#include "util.h"
34
Christopher Ferris858e3362017-11-30 08:53:15 -080035std::map<std::string, std::pair<benchmark_func_t, std::string>> g_str_to_func;
Anders Lewisa7b0f882017-07-24 20:01:13 -070036
37std::mutex g_map_lock;
38
39static struct option g_long_options[] =
40{
41 {"bionic_cpu", required_argument, 0, 'c'},
42 {"bionic_xml", required_argument, 0, 'x'},
43 {"bionic_iterations", required_argument, 0, 'i'},
44 {"bionic_extra", required_argument, 0, 'a'},
45 {"help", no_argument, 0, 'h'},
46 {0, 0, 0, 0},
47};
48
49typedef std::vector<std::vector<int>> args_vector_t;
50
51void Usage() {
52 printf("Usage:\n");
Christopher Ferrisd9d39be2017-08-23 18:03:51 -070053 printf("bionic_benchmarks [--bionic_cpu=<cpu_to_isolate>]\n");
54 printf(" [--bionic_xml=<path_to_xml>]\n");
Anders Lewisa7b0f882017-07-24 20:01:13 -070055 printf(" [--bionic_iterations=<num_iter>]\n");
Christopher Ferrisd9d39be2017-08-23 18:03:51 -070056 printf(" [--bionic_extra=\"<fn_name> <arg1> <arg 2> ...\"]\n");
Anders Lewisa7b0f882017-07-24 20:01:13 -070057 printf(" [<Google benchmark flags>]\n");
58 printf("Google benchmark flags:\n");
59
60 int fake_argc = 2;
61 char argv0[] = "bionic_benchmarks";
62 char argv1[] = "--help";
63 char* fake_argv[3] {argv0, argv1, NULL};
64 benchmark::Initialize(&fake_argc, fake_argv);
Anders Lewisa98a5fb2017-08-09 16:52:19 -070065 exit(1);
Anders Lewisa7b0f882017-07-24 20:01:13 -070066}
67
68// This function removes any bionic benchmarks command line arguments by checking them
69// against g_long_options. It fills new_argv with the filtered args.
70void SanitizeOpts(int argc, char** argv, std::vector<char*>* new_argv) {
71 // TO THOSE ADDING OPTIONS: This currently doesn't support optional arguments.
72 (*new_argv)[0] = argv[0];
73 for (int i = 1; i < argc; ++i) {
74 char* optarg = argv[i];
75 size_t opt_idx = 0;
76
77 // Iterate through g_long_options until either we hit the end or we have a match.
78 for (opt_idx = 0; g_long_options[opt_idx].name &&
79 strncmp(g_long_options[opt_idx].name, optarg + 2,
80 strlen(g_long_options[opt_idx].name)); ++opt_idx) {
81 }
82
83 if (!g_long_options[opt_idx].name) {
84 new_argv->push_back(optarg);
85 } else {
86 if (g_long_options[opt_idx].has_arg == required_argument) {
87 // If the arg was passed in with an =, it spans one char *.
88 // Otherwise, we skip a spot for the argument.
89 if (!strchr(optarg, '=')) {
90 i++;
91 }
92 }
93 }
94 }
95 new_argv->push_back(0);
96}
97
98bench_opts_t ParseOpts(int argc, char** argv) {
99 bench_opts_t opts;
100 int opt;
101 int option_index = 0;
102
103 opts.cpu_to_lock = LONG_MAX;
104 opts.num_iterations = 0;
105
106 // To make this parser handle the benchmark options silently:
107 extern int opterr;
108 opterr = 0;
109
110 while ((opt = getopt_long(argc, argv, "c:x:i:a:h", g_long_options, &option_index)) != -1) {
111 if (opt == -1) {
112 break;
113 }
114 switch (opt) {
115 case 'c':
116 if (*optarg) {
117 char* check_null;
118 opts.cpu_to_lock = strtol(optarg, &check_null, 10);
119 if (*check_null) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700120 errx(1, "ERROR: Args %s is not a valid integer.", optarg);
Anders Lewisa7b0f882017-07-24 20:01:13 -0700121 }
122 } else {
123 printf("ERROR: no argument specified for bionic_cpu\n");
124 Usage();
125 }
126 break;
127 case 'x':
128 if (*optarg) {
129 opts.xmlpath = optarg;
130 } else {
131 printf("ERROR: no argument specified for bionic_xml\n");
132 Usage();
133 }
134 break;
135 case 'a':
136 if (*optarg) {
137 opts.extra_benchmarks.push_back(optarg);
138 } else {
139 printf("ERROR: no argument specified for bionic_extra\n");
140 Usage();
141 }
142 break;
143 case 'i':
144 if (*optarg){
145 char* check_null;
146 opts.num_iterations = strtol(optarg, &check_null, 10);
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700147 if (*check_null != '\0' or opts.num_iterations < 0) {
148 errx(1, "ERROR: Args %s is not a valid number of iterations.", optarg);
Anders Lewisa7b0f882017-07-24 20:01:13 -0700149 }
150 } else {
151 printf("ERROR: no argument specified for bionic_iterations\n");
152 Usage();
153 }
154 break;
155 case 'h':
156 Usage();
157 break;
158 case '?':
159 break;
160 default:
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700161 exit(1);
Anders Lewisa7b0f882017-07-24 20:01:13 -0700162 }
163 }
164 return opts;
165}
166
167// This is a wrapper for every function call for per-benchmark cpu pinning.
168void LockAndRun(benchmark::State& state, benchmark_func_t func_to_bench, long cpu_to_lock) {
169 if (cpu_to_lock != LONG_MAX) LockToCPU(cpu_to_lock);
170 // To avoid having to link against Google benchmarks in libutil,
171 // benchmarks are kept without parameter information, necessitating this cast.
172 reinterpret_cast<void(*) (benchmark::State&)>(func_to_bench)(state);
173}
174
175args_vector_t* ResolveArgs(args_vector_t* to_populate, std::string args,
176 std::map<std::string, args_vector_t>& args_shorthand) {
177 // args is either a space-separated list of ints or a macro name.
178 // To ease formatting in XML files, args is left and right trimmed.
179 if (args_shorthand.count(args)) {
180 return &args_shorthand[args];
181 }
182 to_populate->push_back(std::vector<int>());
183 std::stringstream sstream(args);
184 std::string argstr;
185 while (sstream >> argstr) {
186 char* check_null;
187 int converted = static_cast<int>(strtol(argstr.c_str(), &check_null, 10));
188 if (*check_null) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700189 errx(1, "ERROR: Args str %s contains an invalid macro or int.", args.c_str());
Anders Lewisa7b0f882017-07-24 20:01:13 -0700190 }
191 (*to_populate)[0].push_back(converted);
192 }
193 return to_populate;
194}
195
196void RegisterGoogleBenchmarks(bench_opts_t primary_opts, bench_opts_t secondary_opts,
197 std::string fn_name, args_vector_t* run_args) {
198 if (g_str_to_func.find(fn_name) == g_str_to_func.end()) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700199 errx(1, "ERROR: No benchmark for function %s", fn_name.c_str());
Anders Lewisa7b0f882017-07-24 20:01:13 -0700200 }
201 long iterations_to_use = primary_opts.num_iterations ? primary_opts.num_iterations :
202 secondary_opts.num_iterations;
203 int cpu_to_use = INT_MAX;
204 if (primary_opts.cpu_to_lock != INT_MAX) {
205 cpu_to_use = primary_opts.cpu_to_lock;
206
207 } else if (secondary_opts.cpu_to_lock != INT_MAX) {
208 cpu_to_use = secondary_opts.cpu_to_lock;
209 }
210
Christopher Ferris858e3362017-11-30 08:53:15 -0800211 benchmark_func_t benchmark_function = g_str_to_func.at(fn_name).first;
Anders Lewisa7b0f882017-07-24 20:01:13 -0700212 for (std::vector<int> args : (*run_args)) {
213 auto registration = benchmark::RegisterBenchmark(fn_name.c_str(), LockAndRun,
Christopher Ferris858e3362017-11-30 08:53:15 -0800214 benchmark_function,
Anders Lewisa7b0f882017-07-24 20:01:13 -0700215 cpu_to_use)->Args(args);
216 if (iterations_to_use > 0) {
217 registration->Iterations(iterations_to_use);
218 }
219 }
220}
221
222void RegisterCliBenchmarks(bench_opts_t cmdline_opts,
223 std::map<std::string, args_vector_t>& args_shorthand) {
224 // Register any of the extra benchmarks that were specified in the options.
225 args_vector_t arg_vector;
226 args_vector_t* run_args = &arg_vector;
227 for (std::string extra_fn : cmdline_opts.extra_benchmarks) {
228 android::base::Trim(extra_fn);
229 size_t first_space_pos = extra_fn.find(" ");
230 std::string fn_name = extra_fn.substr(0, first_space_pos);
231 std::string cmd_args;
232 if (first_space_pos != std::string::npos) {
233 cmd_args = extra_fn.substr(extra_fn.find(" ") + 1);
234 } else {
235 cmd_args = "";
236 }
237 run_args = ResolveArgs(run_args, cmd_args, args_shorthand);
238 RegisterGoogleBenchmarks(bench_opts_t(), cmdline_opts, fn_name, run_args);
239
240 run_args = &arg_vector;
241 arg_vector.clear();
242 }
243}
244
245int RegisterXmlBenchmarks(bench_opts_t cmdline_opts,
246 std::map<std::string, args_vector_t>& args_shorthand) {
247 // Structure of the XML file:
248 // - Element "fn" Function to benchmark.
249 // - - Element "iterations" Number of iterations to run. Leaving this blank uses
250 // Google benchmarks' convergence heuristics.
251 // - - Element "cpu" CPU to isolate to, if any.
252 // - - Element "args" Whitespace-separated list of per-function integer arguments, or
253 // one of the macros defined in util.h.
254 tinyxml2::XMLDocument doc;
Elliott Hughesc2223f72017-08-08 11:23:27 -0700255 if (doc.LoadFile(cmdline_opts.xmlpath.c_str()) != tinyxml2::XML_SUCCESS) {
Anders Lewisa7b0f882017-07-24 20:01:13 -0700256 doc.PrintError();
257 return doc.ErrorID();
258 }
259
260 // Read and register the functions.
261 tinyxml2::XMLNode* fn = doc.FirstChildElement("fn");
Anders Lewisa7b0f882017-07-24 20:01:13 -0700262 while (fn) {
Christopher Ferrisd9d39be2017-08-23 18:03:51 -0700263 if (fn == fn->ToComment()) {
264 // Skip comments.
265 fn = fn->NextSibling();
266 continue;
267 }
268
Anders Lewisa7b0f882017-07-24 20:01:13 -0700269 auto fn_elem = fn->FirstChildElement("name");
270 if (!fn_elem) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700271 errx(1, "ERROR: Malformed XML entry: missing name element.");
Anders Lewisa7b0f882017-07-24 20:01:13 -0700272 }
273 std::string fn_name = fn_elem->GetText();
274 if (fn_name.empty()) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700275 errx(1, "ERROR: Malformed XML entry: error parsing name text.");
Anders Lewisa7b0f882017-07-24 20:01:13 -0700276 }
277 auto* xml_args = fn->FirstChildElement("args");
Christopher Ferrisd9d39be2017-08-23 18:03:51 -0700278 args_vector_t arg_vector;
279 args_vector_t* run_args = ResolveArgs(&arg_vector,
280 xml_args ? android::base::Trim(xml_args->GetText()) : "",
281 args_shorthand);
Anders Lewisa7b0f882017-07-24 20:01:13 -0700282
283 // XML values for CPU and iterations take precedence over those passed in via CLI.
284 bench_opts_t xml_opts{};
285 auto* num_iterations_elem = fn->FirstChildElement("iterations");
286 if (num_iterations_elem) {
287 int temp;
288 num_iterations_elem->QueryIntText(&temp);
289 xml_opts.num_iterations = temp;
290 } else {
291 xml_opts.num_iterations = 0;
292 }
293 auto* cpu_to_lock_elem = fn->FirstChildElement("cpu");
294 if (cpu_to_lock_elem) {
295 int temp;
296 cpu_to_lock_elem->QueryIntText(&temp);
297 xml_opts.cpu_to_lock = temp;
298 } else {
299 xml_opts.cpu_to_lock = INT_MAX;
300 }
301
302 RegisterGoogleBenchmarks(xml_opts, cmdline_opts, fn_name, run_args);
303
304 fn = fn->NextSibling();
Anders Lewisa7b0f882017-07-24 20:01:13 -0700305 }
306 return 0;
307}
308
309std::map<std::string, args_vector_t> GetShorthand() {
310 std::map<std::string, args_vector_t> args_shorthand {
311 {"AT_ALIGNED_TWOBUF", args_vector_t{ {8, 0, 0},
Anders Lewisac4f4b42017-08-08 18:29:51 -0700312 {64, 0, 0},
313 {512, 0, 0},
314 {1 * KB, 0, 0},
315 {8 * KB, 0, 0},
316 {16 * KB, 0, 0},
317 {32 * KB, 0, 0},
318 {64 * KB, 0, 0} }},
Anders Lewisa7b0f882017-07-24 20:01:13 -0700319 {"AT_ALIGNED_ONEBUF", args_vector_t{ {(8), 0},
Anders Lewisac4f4b42017-08-08 18:29:51 -0700320 {(64), 0},
321 {(512), 0},
322 {(1*KB), 0},
323 {(8*KB), 0},
324 {(16*KB), 0},
325 {(32*KB), 0},
326 {(64*KB), 0}}},
Anders Lewisa7b0f882017-07-24 20:01:13 -0700327
328 {"AT_COMMON_SIZES", args_vector_t{ {8}, {64}, {512}, {1*KB}, {8*KB}, {16*KB},
329 {32*KB}, {64*KB}}},
330
331 // Do not exceed 512. that is about the largest number of properties
332 // that can be created with the current property area size.
333 {"NUM_PROPS", args_vector_t{ {1}, {4}, {16}, {64}, {128}, {256}, {512} }},
334
335 {"MATH_COMMON", args_vector_t{ {0}, {1}, {2}, {3} }}
336 };
337 for (int i = 1; i < 15; i++) {
338 int align = pow(2, i);
339 std::stringstream sstream;
340 sstream << "AT_" << align << "_ALIGN_TWOBUF";
341 args_shorthand.emplace(sstream.str(),
342 args_vector_t{ {8, align, align},
343 {64, align, align},
344 {512, align, align},
345 {1 * KB, align, align},
346 {8 * KB, align, align},
347 {16 * KB, align, align},
348 {32 * KB, align, align},
349 {64 * KB, align, align} });
350 sstream.str("");
351 sstream << "AT_" << align << "_ALIGN_ONEBUF";
352 args_shorthand.emplace(sstream.str(),
353 args_vector_t{ {(8), align},
354 {(64), align},
355 {(512), align},
356 {(1*KB), align},
357 {(8*KB), align},
358 {(16*KB), align},
359 {(32*KB), align},
360 {(64*KB), align} });
361 sstream.str("");
362 }
363 return args_shorthand;
364}
365
Christopher Ferrisd9d39be2017-08-23 18:03:51 -0700366static bool FileExists(const std::string& file) {
367 struct stat st;
368 return stat(file.c_str(), &st) != -1 && S_ISREG(st.st_mode);
369}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700370
Christopher Ferris858e3362017-11-30 08:53:15 -0800371void RegisterAllBenchmarks(const bench_opts_t& opts,
372 std::map<std::string, args_vector_t>& args_shorthand) {
373 // Add the property tests at the end since they might cause segfaults in
374 // tests running afterwards (b/62197783).
375 std::vector<std::string> prop_tests;
376
377 for (auto& entry : g_str_to_func) {
378 if (android::base::StartsWith(entry.first, "BM_property_")) {
379 prop_tests.push_back(entry.first);
380 } else {
381 auto& function_info = entry.second;
382 args_vector_t arg_vector;
383 args_vector_t* run_args = ResolveArgs(&arg_vector, function_info.second,
384 args_shorthand);
385 RegisterGoogleBenchmarks(bench_opts_t(), opts, entry.first, run_args);
386 }
387 }
388
389 for (auto& prop_name : prop_tests) {
390 auto& function_info = g_str_to_func.at(prop_name);
391 args_vector_t arg_vector;
392 args_vector_t* run_args = ResolveArgs(&arg_vector, function_info.second,
393 args_shorthand);
394 RegisterGoogleBenchmarks(bench_opts_t(), opts, prop_name, run_args);
395 }
396}
397
Anders Lewisa7b0f882017-07-24 20:01:13 -0700398int main(int argc, char** argv) {
399 std::map<std::string, args_vector_t> args_shorthand = GetShorthand();
400 bench_opts_t opts = ParseOpts(argc, argv);
401 std::vector<char*> new_argv(argc);
402 SanitizeOpts(argc, argv, &new_argv);
403
Christopher Ferrisd9d39be2017-08-23 18:03:51 -0700404 if (opts.xmlpath.empty()) {
405 // Don't add the default xml file if a user is specifying the tests to run.
406 if (opts.extra_benchmarks.empty()) {
Christopher Ferris858e3362017-11-30 08:53:15 -0800407 RegisterAllBenchmarks(opts, args_shorthand);
Christopher Ferrisd9d39be2017-08-23 18:03:51 -0700408 }
409 } else if (!FileExists(opts.xmlpath)) {
410 // See if this is a file in the suites directory.
411 std::string file(android::base::GetExecutableDirectory() + "/suites/" + opts.xmlpath);
412 if (opts.xmlpath[0] == '/' || !FileExists(file)) {
413 printf("Cannot find xml file %s: does not exist or is not a file.\n", opts.xmlpath.c_str());
414 return 1;
415 }
416 opts.xmlpath = file;
417 }
418
Anders Lewisa7b0f882017-07-24 20:01:13 -0700419 if (!opts.xmlpath.empty()) {
420 if (int err = RegisterXmlBenchmarks(opts, args_shorthand)) {
421 return err;
422 }
423 }
424 RegisterCliBenchmarks(opts, args_shorthand);
425
426 // Set the thread priority to the maximum.
427 if (setpriority(PRIO_PROCESS, 0, -20)) {
428 perror("Failed to raise priority of process. Are you root?\n");
429 }
430
431 int new_argc = new_argv.size();
432 benchmark::Initialize(&new_argc, new_argv.data());
433 benchmark::RunSpecifiedBenchmarks();
434}