2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/resource.h>
27 #include "bandwidth.h"
35 option_t bandwidth_opts[] = {
37 { "num_warm_loops", true },
38 { "num_loops", true },
43 option_t per_core_opts[] = {
45 { "num_warm_loops", true},
46 { "num_loops", true },
51 option_t multithread_opts[] = {
53 { "num_warm_loops", true},
54 { "num_loops", true },
56 { "num_threads", true },
62 const char *char_value;
64 typedef std::map<const char*, arg_value_t> arg_t;
66 bool processBandwidthOptions(int argc, char** argv, option_t options[],
68 for (int i = 1; i < argc; i++) {
69 if (argv[i][0] == '-' && argv[i][1] == '-' && !isdigit(argv[i][2])) {
70 char *arg = &argv[i][2];
72 for (int j = 0; options[j].name != NULL; j++) {
73 if (strcmp(arg, options[j].name) == 0) {
74 const char *name = options[j].name;
76 printf("The option --%s requires an argument.\n", name);
79 if (options[j].int_type) {
80 (*values)[name].int_value = strtol(argv[++i], NULL, 0);
82 (*values)[name].char_value = argv[++i];
92 BandwidthBenchmark *createBandwidthBenchmarkObject(arg_t values) {
93 BandwidthBenchmark *bench = NULL;
95 const char *name = values["type"].char_value;
97 if (values.count("size") > 0) {
98 size = values["size"].int_value;
100 if (strcmp(name, "copy_ldrd_strd") == 0) {
101 bench = new CopyLdrdStrdBenchmark(size);
102 } else if (strcmp(name, "copy_ldmia_stmia") == 0) {
103 bench = new CopyLdmiaStmiaBenchmark(size);
104 } else if (strcmp(name, "copy_vld_vst") == 0) {
105 bench = new CopyVldVstBenchmark(size);
106 } else if (strcmp(name, "copy_vldmia_vstmia") == 0) {
107 bench = new CopyVldmiaVstmiaBenchmark(size);
108 } else if (strcmp(name, "memcpy") == 0) {
109 bench = new MemcpyBenchmark(size);
110 } else if (strcmp(name, "write_strd") == 0) {
111 bench = new WriteStrdBenchmark(size);
112 } else if (strcmp(name, "write_stmia") == 0) {
113 bench = new WriteStmiaBenchmark(size);
114 } else if (strcmp(name, "write_vst") == 0) {
115 bench = new WriteVstBenchmark(size);
116 } else if (strcmp(name, "write_vstmia") == 0) {
117 bench = new WriteVstmiaBenchmark(size);
118 } else if (strcmp(name, "memset") == 0) {
119 bench = new MemsetBenchmark(size);
123 if (values.count("num_warm_loops") > 0) {
124 bench->set_num_loops(values["num_warm_loops"].int_value);
126 if (values.count("num_loops") > 0) {
127 bench->set_num_loops(values["num_loops"].int_value);
134 bool getAvailCpus(std::vector<int> *cpu_list) {
138 if (sched_getaffinity(0, sizeof(cpuset), &cpuset) != 0) {
139 perror("sched_getaffinity failed.");
143 for (int i = 0; i < CPU_SETSIZE; i++) {
144 if (CPU_ISSET(i, &cpuset)) {
145 cpu_list->push_back(i);
154 BandwidthBenchmark *bench;
159 void *runBandwidthThread(void *data) {
160 thread_arg_t *arg = reinterpret_cast<thread_arg_t *>(data);
162 if (arg->core >= 0) {
165 CPU_SET(arg->core, &cpuset);
166 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
167 perror("sched_setaffinity failed");
172 // Spinloop waiting for the run variable to get set to true.
177 for (int run = 1; ; run++) {
180 // Throw away the last data point since it's possible not
181 // all of the threads are running at this point.
184 avg_mb = (avg_mb/run) * (run-1) + arg->bench->mb_per_sec()/run;
186 arg->avg_mb = avg_mb;
191 bool processThreadArgs(int argc, char** argv, option_t options[],
193 // Use some smaller values for the number of loops.
194 (*values)["num_warm_loops"].int_value = 1000000;
195 (*values)["num_loops"].int_value = 10000000;
197 if (!processBandwidthOptions(argc, argv, options, values)) {
200 if (values->count("size") > 0 && ((*values)["size"].int_value % 64) != 0) {
201 printf("The size values must be a multiple of 64.\n");
204 if (values->count("type") == 0) {
205 printf("Must specify the type value.\n");
209 BandwidthBenchmark *bench = createBandwidthBenchmarkObject(*values);
211 printf("Unknown type %s\n", (*values)["type"].char_value);
215 if (setpriority(PRIO_PROCESS, 0, -20)) {
216 perror("Unable to raise priority of process.");
220 printf("Calculating optimum run time...\n");
221 nsecs_t t = system_time();
223 t = system_time() - t;
224 // Since this is only going to be running single threaded, assume that
225 // if the number is set to ten times this value, we should get at least
226 // a couple of samples per thread.
227 int run_time = int((t/1000000000.0)*10 + 0.5) + 5;
229 (*values)["run_time"].int_value = run_time;
230 (*values)["size"].int_value = bench->size();
231 (*values)["num_warm_loops"].int_value = bench->num_warm_loops();
232 (*values)["num_loops"].int_value = bench->num_loops();
238 bool runThreadedTest(thread_arg_t args[], int num_threads, int run_time) {
239 pthread_t threads[num_threads];
240 volatile bool run = false;
243 for (int i = 0; i < num_threads; i++) {
245 rc = pthread_create(&threads[i], NULL, runBandwidthThread,
248 printf("Failed to launch thread %d\n", i);
253 // Kick start the threads.
256 // Let the threads run.
262 // Wait for the threads to complete.
263 for (int i = 0; i < num_threads; i++) {
264 rc = pthread_join(threads[i], NULL);
266 printf("Thread %d failed to join.\n", i);
269 printf("Thread %d: bandwidth using %s %0.2f MB/s\n", i,
270 args[i].bench->getName(), args[i].avg_mb);
276 int per_core_bandwidth(int argc, char** argv) {
278 if (!processThreadArgs(argc, argv, per_core_opts, &values)) {
282 std::vector<int> cpu_list;
283 if (!getAvailCpus(&cpu_list)) {
284 printf("Failed to get available cpu list.\n");
288 thread_arg_t args[cpu_list.size()];
291 for (std::vector<int>::iterator it = cpu_list.begin();
292 it != cpu_list.end(); ++it, ++i) {
294 args[i].bench = createBandwidthBenchmarkObject(values);
297 printf("Running on %d cores\n", cpu_list.size());
298 printf(" run_time = %ds\n", values["run_time"].int_value);
299 printf(" size = %d\n", values["size"].int_value);
300 printf(" num_warm_loops = %d\n", values["num_warm_loops"].int_value);
301 printf(" num_loops = %d\n", values["num_loops"].int_value);
304 if (!runThreadedTest(args, cpu_list.size(), values["run_time"].int_value)) {
311 int multithread_bandwidth(int argc, char** argv) {
313 if (!processThreadArgs(argc, argv, multithread_opts, &values)) {
316 if (values.count("num_threads") == 0) {
317 printf("Must specify the num_threads value.\n");
320 int num_threads = values["num_threads"].int_value;
322 thread_arg_t args[num_threads];
325 for (int i = 0; i < num_threads; i++) {
327 args[i].bench = createBandwidthBenchmarkObject(values);
330 printf("Running %d threads\n", num_threads);
331 printf(" run_time = %ds\n", values["run_time"].int_value);
332 printf(" size = %d\n", values["size"].int_value);
333 printf(" num_warm_loops = %d\n", values["num_warm_loops"].int_value);
334 printf(" num_loops = %d\n", values["num_loops"].int_value);
337 if (!runThreadedTest(args, num_threads, values["run_time"].int_value)) {
344 int copy_bandwidth(int argc, char** argv) {
346 values["size"].int_value = 0;
347 values["num_loops"].int_value = BandwidthBenchmark::DEFAULT_NUM_LOOPS;
348 values["num_warm_loops"].int_value = BandwidthBenchmark::DEFAULT_NUM_WARM_LOOPS;
349 if (!processBandwidthOptions(argc, argv, bandwidth_opts, &values)) {
352 size_t size = values["size"].int_value;
353 if ((size % 64) != 0) {
354 printf("The size value must be a multiple of 64.\n");
358 if (setpriority(PRIO_PROCESS, 0, -20)) {
359 perror("Unable to raise priority of process.");
363 std::vector<BandwidthBenchmark*> bench_objs;
364 bench_objs.push_back(new CopyLdrdStrdBenchmark(size));
365 bench_objs.push_back(new CopyLdmiaStmiaBenchmark(size));
366 bench_objs.push_back(new CopyVldVstBenchmark(size));
367 bench_objs.push_back(new CopyVldmiaVstmiaBenchmark(size));
368 bench_objs.push_back(new MemcpyBenchmark(size));
370 printf("Benchmarking copy bandwidth\n");
371 printf(" size = %d\n", bench_objs[0]->size());
372 printf(" num_warm_loops = %d\n", values["num_warm_loops"].int_value);
373 printf(" num_loops = %d\n\n", values["num_loops"].int_value);
374 for (std::vector<BandwidthBenchmark*>::iterator it = bench_objs.begin();
375 it != bench_objs.end(); ++it) {
376 (*it)->set_num_warm_loops(values["num_warm_loops"].int_value);
377 (*it)->set_num_loops(values["num_loops"].int_value);
379 printf(" Copy bandwidth with %s: %0.2f MB/s\n", (*it)->getName(),
380 (*it)->mb_per_sec());
386 int write_bandwidth(int argc, char** argv) {
388 values["size"].int_value = 0;
389 values["num_loops"].int_value = BandwidthBenchmark::DEFAULT_NUM_LOOPS;
390 values["num_warm_loops"].int_value = BandwidthBenchmark::DEFAULT_NUM_WARM_LOOPS;
391 if (!processBandwidthOptions(argc, argv, bandwidth_opts, &values)) {
395 size_t size = values["size"].int_value;
396 if ((size % 64) != 0) {
397 printf("The size value must be a multiple of 64.\n");
401 if (setpriority(PRIO_PROCESS, 0, -20)) {
402 perror("Unable to raise priority of process.");
406 std::vector<BandwidthBenchmark*> bench_objs;
407 bench_objs.push_back(new WriteStrdBenchmark(size));
408 bench_objs.push_back(new WriteStmiaBenchmark(size));
409 bench_objs.push_back(new WriteVstBenchmark(size));
410 bench_objs.push_back(new WriteVstmiaBenchmark(size));
411 bench_objs.push_back(new MemsetBenchmark(size));
413 printf("Benchmarking write bandwidth\n");
414 printf(" size = %d\n", bench_objs[0]->size());
415 printf(" num_warm_loops = %d\n", values["num_warm_loops"].int_value);
416 printf(" num_loops = %d\n\n", values["num_loops"].int_value);
417 for (std::vector<BandwidthBenchmark*>::iterator it = bench_objs.begin();
418 it != bench_objs.end(); ++it) {
419 (*it)->set_num_warm_loops(values["num_warm_loops"].int_value);
420 (*it)->set_num_loops(values["num_loops"].int_value);
422 printf(" Write bandwidth with %s: %0.2f MB/s\n", (*it)->getName(),
423 (*it)->mb_per_sec());