blob: 66c6ec235436797959971312d3073d535187e3ec [file] [log] [blame]
Christopher Ferris82ac1af2013-02-15 12:27:58 -08001/*
2** Copyright 2010 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
17/*
Christopher Ferris25ada902013-04-02 13:28:16 -070018 * Micro-benchmarking of sleep/cpu speed/memcpy/memset/memory reads/strcmp.
Christopher Ferris82ac1af2013-02-15 12:27:58 -080019 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include <math.h>
25#include <sched.h>
26#include <sys/resource.h>
27#include <time.h>
28#include <unistd.h>
29
30// The default size of data that will be manipulated in each iteration of
31// a memory benchmark. Can be modified with the --data_size option.
32#define DEFAULT_DATA_SIZE 1000000000
33
34// Number of nanoseconds in a second.
35#define NS_PER_SEC 1000000000
36
37// The maximum number of arguments that a benchmark will accept.
38#define MAX_ARGS 2
39
Christopher Ferris82ac1af2013-02-15 12:27:58 -080040// Contains information about benchmark options.
41typedef struct {
42 bool print_average;
43 bool print_each_iter;
44
45 int dst_align;
Christopher Ferris25ada902013-04-02 13:28:16 -070046 int dst_or_mask;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080047 int src_align;
Christopher Ferris25ada902013-04-02 13:28:16 -070048 int src_or_mask;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080049
50 int cpu_to_lock;
51
52 int data_size;
Christopher Ferris7401bc12013-07-10 18:55:57 -070053 int dst_str_size;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080054
55 int args[MAX_ARGS];
56 int num_args;
57} command_data_t;
58
Christopher Ferris014cf9d2013-06-26 13:42:18 -070059typedef void *(*void_func_t)();
60typedef void *(*memcpy_func_t)(void *, const void *, size_t);
61typedef void *(*memset_func_t)(void *, int, size_t);
62typedef int (*strcmp_func_t)(const char *, const char *);
Christopher Ferris7401bc12013-07-10 18:55:57 -070063typedef char *(*str_func_t)(char *, const char *);
64typedef size_t (*strlen_func_t)(const char *);
Christopher Ferris014cf9d2013-06-26 13:42:18 -070065
Christopher Ferris82ac1af2013-02-15 12:27:58 -080066// Struct that contains a mapping of benchmark name to benchmark function.
67typedef struct {
68 const char *name;
Christopher Ferris014cf9d2013-06-26 13:42:18 -070069 int (*ptr)(const char *, const command_data_t &, void_func_t func);
70 void_func_t func;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080071} function_t;
72
73// Get the current time in nanoseconds.
74uint64_t nanoTime() {
75 struct timespec t;
76
77 t.tv_sec = t.tv_nsec = 0;
78 clock_gettime(CLOCK_MONOTONIC, &t);
79 return static_cast<uint64_t>(t.tv_sec) * NS_PER_SEC + t.tv_nsec;
80}
81
82// Allocate memory with a specific alignment and return that pointer.
83// This function assumes an alignment value that is a power of 2.
84// If the alignment is 0, then use the pointer returned by malloc.
Christopher Ferris25ada902013-04-02 13:28:16 -070085uint8_t *getAlignedMemory(uint8_t *orig_ptr, int alignment, int or_mask) {
86 uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr);
Christopher Ferris82ac1af2013-02-15 12:27:58 -080087 if (alignment > 0) {
88 // When setting the alignment, set it to exactly the alignment chosen.
89 // The pointer returned will be guaranteed not to be aligned to anything
90 // more than that.
91 ptr += alignment - (ptr & (alignment - 1));
Christopher Ferris25ada902013-04-02 13:28:16 -070092 ptr |= alignment | or_mask;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080093 }
94
95 return reinterpret_cast<uint8_t*>(ptr);
96}
97
Christopher Ferris25ada902013-04-02 13:28:16 -070098// Allocate memory with a specific alignment and return that pointer.
99// This function assumes an alignment value that is a power of 2.
100// If the alignment is 0, then use the pointer returned by malloc.
101uint8_t *allocateAlignedMemory(size_t size, int alignment, int or_mask) {
102 uint64_t ptr = reinterpret_cast<uint64_t>(malloc(size + 3 * alignment));
103 if (!ptr)
104 return NULL;
105 return getAlignedMemory((uint8_t*)ptr, alignment, or_mask);
106}
107
Christopher Ferris7401bc12013-07-10 18:55:57 -0700108char *allocateAlignedString(int size, int align, int or_mask, bool init = true) {
109 char *buf = reinterpret_cast<char*>(allocateAlignedMemory(size, align, or_mask));
110 if (!buf) {
111 return NULL;
112 }
113
114 if (init) {
115 for (int i = 0; i < size - 1; i++) {
116 buf[i] = (char)(32 + (i % 96));
117 }
118 buf[size-1] = '\0';
119 } else {
120 memset(buf, 0, size);
121 }
122
123 return buf;
124}
125
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700126static inline double computeAverage(uint64_t time_ns, int size, int copies) {
127 return ((size/1024.0) * copies) / ((double)time_ns/NS_PER_SEC);
128}
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800129
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700130static inline double computeRunningAvg(double avg, double running_avg, size_t cur_idx) {
131 return (running_avg / (cur_idx + 1)) * cur_idx + (avg / (cur_idx + 1));
132}
133
134static inline double computeRunningSquareAvg(double avg, double square_avg, size_t cur_idx) {
135 return (square_avg / (cur_idx + 1)) * cur_idx + (avg / (cur_idx + 1)) * avg;
136}
137
138static inline double computeStdDev(double square_avg, double running_avg) {
139 return sqrt(square_avg - running_avg * running_avg);
140}
141
142static inline void printIter(uint64_t time_ns, const char *name, int size, int copies, double avg) {
143 printf("%s %dx%d bytes took %.06f seconds (%f MB/s)\n",
144 name, copies, size, (double)time_ns/NS_PER_SEC, avg/1024.0);
145}
146
147static inline void printSummary(uint64_t time_ns, const char *name, int size, int copies, double running_avg, double std_dev, double min, double max) {
148 printf(" %s %dx%d bytes average %.2f MB/s std dev %.4f min %.2f MB/s max %.2f MB/s\n",
149 name, copies, size, running_avg/1024.0, std_dev/1024.0, min/1024.0,
150 max/1024.0);
151}
152
Christopher Ferris7401bc12013-07-10 18:55:57 -0700153#define MAINLOOP(cmd_data, BENCH, AFTER_BENCH, COMPUTE_AVG, PRINT_ITER, PRINT_AVG) \
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700154 uint64_t time_ns; \
155 int iters = cmd_data.args[1]; \
156 bool print_average = cmd_data.print_average; \
157 bool print_each_iter = cmd_data.print_each_iter; \
158 double min = 0.0, max = 0.0, running_avg = 0.0, square_avg = 0.0; \
159 double avg; \
160 for (int i = 0; iters == -1 || i < iters; i++) { \
161 time_ns = nanoTime(); \
162 BENCH; \
163 time_ns = nanoTime() - time_ns; \
Christopher Ferris7401bc12013-07-10 18:55:57 -0700164 AFTER_BENCH; \
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700165 avg = COMPUTE_AVG; \
166 if (print_average) { \
167 running_avg = computeRunningAvg(avg, running_avg, i); \
168 square_avg = computeRunningSquareAvg(avg, square_avg, i); \
169 if (min == 0.0 || avg < min) { \
170 min = avg; \
171 } \
172 if (avg > max) { \
173 max = avg; \
174 } \
175 } \
176 if (print_each_iter) { \
177 PRINT_ITER; \
178 } \
179 } \
180 if (print_average) { \
181 PRINT_AVG; \
182 }
183
Christopher Ferris7401bc12013-07-10 18:55:57 -0700184#define MAINLOOP_DATA(name, cmd_data, size, BENCH, AFTER_BENCH) \
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700185 int copies = cmd_data.data_size/size; \
186 int j; \
187 MAINLOOP(cmd_data, \
188 for (j = 0; j < copies; j++) { \
189 BENCH; \
190 }, \
Christopher Ferris7401bc12013-07-10 18:55:57 -0700191 AFTER_BENCH, \
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700192 computeAverage(time_ns, size, copies), \
193 printIter(time_ns, name, size, copies, avg), \
194 double std_dev = computeStdDev(square_avg, running_avg); \
195 printSummary(time_ns, name, size, copies, running_avg, \
196 std_dev, min, max));
197
198int benchmarkSleep(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800199 int delay = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700200 MAINLOOP(cmd_data, sleep(delay),
Christopher Ferris7401bc12013-07-10 18:55:57 -0700201 ;,
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700202 (double)time_ns/NS_PER_SEC,
203 printf("sleep(%d) took %.06f seconds\n", delay, avg);,
204 printf(" sleep(%d) average %.06f seconds std dev %f min %.06f seconds max %0.6f seconds\n", \
205 delay, running_avg, computeStdDev(square_avg, running_avg), \
206 min, max));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800207
208 return 0;
209}
210
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700211int benchmarkCpu(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800212 // Use volatile so that the loop is not optimized away by the compiler.
213 volatile int cpu_foo;
214
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700215 MAINLOOP(cmd_data,
216 for (cpu_foo = 0; cpu_foo < 100000000; cpu_foo++),
Christopher Ferris7401bc12013-07-10 18:55:57 -0700217 ;,
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700218 (double)time_ns/NS_PER_SEC,
219 printf("cpu took %.06f seconds\n", avg),
220 printf(" cpu average %.06f seconds std dev %f min %0.6f seconds max %0.6f seconds\n", \
221 running_avg, computeStdDev(square_avg, running_avg), min, max));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800222
223 return 0;
224}
225
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700226int benchmarkMemset(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800227 int size = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700228 memset_func_t memset_func = reinterpret_cast<memset_func_t>(func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800229
Christopher Ferris25ada902013-04-02 13:28:16 -0700230 uint8_t *dst = allocateAlignedMemory(size, cmd_data.dst_align, cmd_data.dst_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800231 if (!dst)
232 return -1;
233
Christopher Ferris7401bc12013-07-10 18:55:57 -0700234 MAINLOOP_DATA(name, cmd_data, size, memset_func(dst, 0, size), ;);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800235
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800236 return 0;
237}
238
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700239int benchmarkMemcpy(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800240 int size = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700241 memcpy_func_t memcpy_func = reinterpret_cast<memcpy_func_t>(func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800242
Christopher Ferris25ada902013-04-02 13:28:16 -0700243 uint8_t *src = allocateAlignedMemory(size, cmd_data.src_align, cmd_data.src_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800244 if (!src)
245 return -1;
Christopher Ferris25ada902013-04-02 13:28:16 -0700246 uint8_t *dst = allocateAlignedMemory(size, cmd_data.dst_align, cmd_data.dst_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800247 if (!dst)
248 return -1;
249
Christopher Ferris25ada902013-04-02 13:28:16 -0700250 // Initialize the source and destination to known values.
251 // If not initialized, the benchmark results are skewed.
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700252 memset(src, 0xff, size);
Christopher Ferris25ada902013-04-02 13:28:16 -0700253 memset(dst, 0, size);
254
Christopher Ferris7401bc12013-07-10 18:55:57 -0700255 MAINLOOP_DATA(name, cmd_data, size, memcpy_func(dst, src, size), ;);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800256
Christopher Ferris25ada902013-04-02 13:28:16 -0700257 return 0;
258}
259
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700260int benchmarkMemread(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris25ada902013-04-02 13:28:16 -0700261 int size = cmd_data.args[0];
Christopher Ferris25ada902013-04-02 13:28:16 -0700262
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700263 uint32_t *src = reinterpret_cast<uint32_t*>(malloc(size));
264 if (!src)
Christopher Ferris25ada902013-04-02 13:28:16 -0700265 return -1;
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700266 memset(src, 0xff, size);
Christopher Ferris25ada902013-04-02 13:28:16 -0700267
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700268 // Use volatile so the compiler does not optimize away the reads.
269 volatile int foo;
270 size_t k;
271 MAINLOOP_DATA(name, cmd_data, size,
Christopher Ferris7401bc12013-07-10 18:55:57 -0700272 for (k = 0; k < size/sizeof(uint32_t); k++) foo = src[k], ;);
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700273
274 return 0;
275}
276
277int benchmarkStrcmp(const char *name, const command_data_t &cmd_data, void_func_t func) {
278 int size = cmd_data.args[0];
279 strcmp_func_t strcmp_func = reinterpret_cast<strcmp_func_t>(func);
280
Christopher Ferris7401bc12013-07-10 18:55:57 -0700281 char *string1 = allocateAlignedString(size, cmd_data.src_align, cmd_data.src_or_mask);
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700282 if (!string1)
283 return -1;
Christopher Ferris7401bc12013-07-10 18:55:57 -0700284 char *string2 = allocateAlignedString(size, cmd_data.dst_align, cmd_data.dst_or_mask);
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700285 if (!string2)
286 return -1;
Christopher Ferris25ada902013-04-02 13:28:16 -0700287
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700288 int retval;
289 MAINLOOP_DATA(name, cmd_data, size,
290 retval = strcmp_func(string1, string2); \
Christopher Ferris7401bc12013-07-10 18:55:57 -0700291 if (retval != 0) printf("%s failed, return value %d\n", name, retval), ;);
292
293 return 0;
294}
295
296int benchmarkStrlen(const char *name, const command_data_t &cmd_data, void_func_t func) {
297 size_t size = cmd_data.args[0];
298 strlen_func_t strlen_func = reinterpret_cast<strlen_func_t>(func);
299
300 char *buf = allocateAlignedString(size, cmd_data.dst_align, cmd_data.dst_or_mask);
301
302 size_t real_size;
303 MAINLOOP_DATA(name, cmd_data, size,
304 real_size = strlen_func(buf); \
305 if (real_size + 1 != size) { \
306 printf("%s failed, expected %u, got %u\n", name, size, real_size); \
307 return -1; \
308 }, ;);
309
310 return 0;
311}
312
313int benchmarkStrcat(const char *name, const command_data_t &cmd_data, void_func_t func) {
314 int size = cmd_data.args[0];
315 str_func_t str_func = reinterpret_cast<str_func_t>(func);
316
317 int dst_str_size = cmd_data.dst_str_size;
318 if (dst_str_size <= 0) {
319 printf("%s requires --dst_str_size to be set to a non-zero value.\n",
320 name);
321 return -1;
322 }
323
324 char *src = allocateAlignedString(size, cmd_data.src_align, cmd_data.src_or_mask);
325 if (!src)
326 return -1;
327 char *dst = allocateAlignedString(size + dst_str_size, cmd_data.dst_align, cmd_data.dst_or_mask);
328 if (!dst)
329 return -1;
330 dst[dst_str_size-1] = '\0';
331
332 MAINLOOP_DATA(name, cmd_data, size,
333 str_func(dst, src); dst[dst_str_size-1] = '\0';,
334 ;);
Christopher Ferris25ada902013-04-02 13:28:16 -0700335
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800336 return 0;
337}
338
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700339int benchmarkStrcpy(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800340 int size = cmd_data.args[0];
Christopher Ferris7401bc12013-07-10 18:55:57 -0700341 str_func_t str_func = reinterpret_cast<str_func_t>(func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800342
Christopher Ferris7401bc12013-07-10 18:55:57 -0700343 char *src = allocateAlignedString(size, cmd_data.src_align, cmd_data.src_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800344 if (!src)
345 return -1;
Christopher Ferris7401bc12013-07-10 18:55:57 -0700346 char *dst = allocateAlignedString(size, cmd_data.dst_align, cmd_data.dst_or_mask);
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700347 if (!dst)
348 return -1;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800349
Christopher Ferris7401bc12013-07-10 18:55:57 -0700350 MAINLOOP_DATA(name, cmd_data, size, str_func(dst, src), ;);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800351
352 return 0;
353}
354
355// Create the mapping structure.
356function_t function_table[] = {
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700357 { "sleep", benchmarkSleep, NULL },
358 { "cpu", benchmarkCpu, NULL },
359 { "memread", benchmarkMemread, NULL },
360 { "memset", benchmarkMemset, reinterpret_cast<void_func_t>(memset) },
361 { "memcpy", benchmarkMemcpy, reinterpret_cast<void_func_t>(memcpy) },
362 { "strcmp", benchmarkStrcmp, reinterpret_cast<void_func_t>(strcmp) },
Christopher Ferris7401bc12013-07-10 18:55:57 -0700363 { "strlen", benchmarkStrlen, reinterpret_cast<void_func_t>(strlen) },
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700364 { "strcpy", benchmarkStrcpy, reinterpret_cast<void_func_t>(strcpy) },
Christopher Ferris7401bc12013-07-10 18:55:57 -0700365 { "strcat", benchmarkStrcat, reinterpret_cast<void_func_t>(strcat) },
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800366};
367
368void usage() {
369 printf("Usage:\n");
370 printf(" micro_bench [--data_size DATA_BYTES] [--print_average]\n");
371 printf(" [--no_print_each_iter] [--lock_to_cpu CORE]\n");
Christopher Ferris7401bc12013-07-10 18:55:57 -0700372 printf(" [--src_align ALIGN] [--src_or_mask OR_MASK]\n");
373 printf(" [--dst_align ALIGN] [--dst_or_mask OR_MASK]\n");
374 printf(" [--dst_str_size SIZE]\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800375 printf(" --data_size DATA_BYTES\n");
376 printf(" For the data benchmarks (memcpy/memset/memread) the approximate\n");
377 printf(" size of data, in bytes, that will be manipulated in each iteration.\n");
378 printf(" --print_average\n");
379 printf(" Print the average and standard deviation of all iterations.\n");
380 printf(" --no_print_each_iter\n");
381 printf(" Do not print any values in each iteration.\n");
382 printf(" --lock_to_cpu CORE\n");
383 printf(" Lock to the specified CORE. The default is to use the last core found.\n");
Christopher Ferris7401bc12013-07-10 18:55:57 -0700384 printf(" --dst_align ALIGN\n");
385 printf(" If the command supports it, align the destination pointer to ALIGN.\n");
386 printf(" The default is to use the value returned by malloc.\n");
387 printf(" --dst_or_mask OR_MASK\n");
388 printf(" If the command supports it, or in the OR_MASK on to the destination pointer.\n");
389 printf(" The OR_MASK must be smaller than the dst_align value.\n");
390 printf(" The default value is 0.\n");
391
392 printf(" --src_align ALIGN\n");
393 printf(" If the command supports it, align the source pointer to ALIGN. The default is to use the\n");
394 printf(" value returned by malloc.\n");
395 printf(" --src_or_mask OR_MASK\n");
396 printf(" If the command supports it, or in the OR_MASK on to the source pointer.\n");
397 printf(" The OR_MASK must be smaller than the src_align value.\n");
398 printf(" The default value is 0.\n");
399 printf(" --dst_str_size SIZE\n");
400 printf(" If the command supports it, create a destination string of this length.\n");
401 printf(" The default is to not update the destination string.\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800402 printf(" ITERS\n");
403 printf(" The number of iterations to execute each benchmark. If not\n");
404 printf(" passed in then run forever.\n");
405 printf(" micro_bench sleep TIME_TO_SLEEP [ITERS]\n");
406 printf(" TIME_TO_SLEEP\n");
407 printf(" The time in seconds to sleep.\n");
408 printf(" micro_bench cpu UNUSED [ITERS]\n");
Christopher Ferris7401bc12013-07-10 18:55:57 -0700409 printf(" micro_bench [--dst_align ALIGN] [--dst_or_mask OR_MASK] memcpy NUM_BYTES [ITERS]\n");
410 printf(" micro_bench [--dst_align ALIGN] [--dst_or_mask OR_MASK] memset NUM_BYTES [ITERS]\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800411 printf(" micro_bench memread NUM_BYTES [ITERS]\n");
Christopher Ferris7401bc12013-07-10 18:55:57 -0700412 printf(" micro_bench [--src_align ALIGN] [--src_or_mask OR_MASK] [--dst_align ALIGN] [--dst_or_mask] [--dst_str_size SIZE] strcat NUM_BYTES [ITERS]\n");
413 printf(" micro_bench [--src_align ALIGN] [--src_or_mask OR_MASK] [--dst_align ALIGN] [--dst_or_mask OR_MASK] strcmp NUM_BYTES [ITERS]\n");
414 printf(" micro_bench [--src_align ALIGN] [--src_or_mask OR_MASK] [--dst_align ALIGN] [--dst_or_mask] strcpy NUM_BYTES [ITERS]\n");
415 printf(" micro_bench [--dst_align ALIGN] [--dst_or_mask OR_MASK] strlen NUM_BYTES [ITERS]\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800416}
417
418function_t *processOptions(int argc, char **argv, command_data_t *cmd_data) {
419 function_t *command = NULL;
420
421 // Initialize the command_flags.
422 cmd_data->print_average = false;
423 cmd_data->print_each_iter = true;
424 cmd_data->dst_align = 0;
425 cmd_data->src_align = 0;
Christopher Ferris25ada902013-04-02 13:28:16 -0700426 cmd_data->src_or_mask = 0;
427 cmd_data->dst_or_mask = 0;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800428 cmd_data->num_args = 0;
429 cmd_data->cpu_to_lock = -1;
430 cmd_data->data_size = DEFAULT_DATA_SIZE;
Christopher Ferris7401bc12013-07-10 18:55:57 -0700431 cmd_data->dst_str_size = -1;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800432 for (int i = 0; i < MAX_ARGS; i++) {
433 cmd_data->args[i] = -1;
434 }
435
436 for (int i = 1; i < argc; i++) {
437 if (argv[i][0] == '-') {
438 int *save_value = NULL;
439 if (strcmp(argv[i], "--print_average") == 0) {
440 cmd_data->print_average = true;
441 } else if (strcmp(argv[i], "--no_print_each_iter") == 0) {
442 cmd_data->print_each_iter = false;
443 } else if (strcmp(argv[i], "--dst_align") == 0) {
444 save_value = &cmd_data->dst_align;
445 } else if (strcmp(argv[i], "--src_align") == 0) {
446 save_value = &cmd_data->src_align;
Christopher Ferris25ada902013-04-02 13:28:16 -0700447 } else if (strcmp(argv[i], "--dst_or_mask") == 0) {
448 save_value = &cmd_data->dst_or_mask;
449 } else if (strcmp(argv[i], "--src_or_mask") == 0) {
450 save_value = &cmd_data->src_or_mask;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800451 } else if (strcmp(argv[i], "--lock_to_cpu") == 0) {
452 save_value = &cmd_data->cpu_to_lock;
453 } else if (strcmp(argv[i], "--data_size") == 0) {
454 save_value = &cmd_data->data_size;
Christopher Ferris7401bc12013-07-10 18:55:57 -0700455 } else if (strcmp(argv[i], "--dst_str_size") == 0) {
456 save_value = &cmd_data->dst_str_size;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800457 } else {
458 printf("Unknown option %s\n", argv[i]);
459 return NULL;
460 }
461 if (save_value) {
462 // Checking both characters without a strlen() call should be
463 // safe since as long as the argument exists, one character will
464 // be present (\0). And if the first character is '-', then
465 // there will always be a second character (\0 again).
466 if (i == argc - 1 || (argv[i + 1][0] == '-' && !isdigit(argv[i + 1][1]))) {
467 printf("The option %s requires one argument.\n",
468 argv[i]);
469 return NULL;
470 }
Christopher Ferris25ada902013-04-02 13:28:16 -0700471 *save_value = (int)strtol(argv[++i], NULL, 0);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800472 }
473 } else if (!command) {
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700474 for (size_t j = 0; j < sizeof(function_table)/sizeof(function_t); j++) {
475 if (strcmp(argv[i], function_table[j].name) == 0) {
476 command = &function_table[j];
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800477 break;
478 }
479 }
480 if (!command) {
481 printf("Uknown command %s\n", argv[i]);
482 return NULL;
483 }
484 } else if (cmd_data->num_args > MAX_ARGS) {
485 printf("More than %d number arguments passed in.\n", MAX_ARGS);
486 return NULL;
487 } else {
488 cmd_data->args[cmd_data->num_args++] = atoi(argv[i]);
489 }
490 }
491
492 // Check the arguments passed in make sense.
493 if (cmd_data->num_args != 1 && cmd_data->num_args != 2) {
494 printf("Not enough arguments passed in.\n");
495 return NULL;
496 } else if (cmd_data->dst_align < 0) {
497 printf("The --dst_align option must be greater than or equal to 0.\n");
498 return NULL;
499 } else if (cmd_data->src_align < 0) {
500 printf("The --src_align option must be greater than or equal to 0.\n");
501 return NULL;
502 } else if (cmd_data->data_size <= 0) {
503 printf("The --data_size option must be a positive number.\n");
504 return NULL;
505 } else if ((cmd_data->dst_align & (cmd_data->dst_align - 1))) {
506 printf("The --dst_align option must be a power of 2.\n");
507 return NULL;
508 } else if ((cmd_data->src_align & (cmd_data->src_align - 1))) {
509 printf("The --src_align option must be a power of 2.\n");
510 return NULL;
Christopher Ferris25ada902013-04-02 13:28:16 -0700511 } else if (!cmd_data->src_align && cmd_data->src_or_mask) {
512 printf("The --src_or_mask option requires that --src_align be set.\n");
513 return NULL;
514 } else if (!cmd_data->dst_align && cmd_data->dst_or_mask) {
515 printf("The --dst_or_mask option requires that --dst_align be set.\n");
516 return NULL;
517 } else if (cmd_data->src_or_mask > cmd_data->src_align) {
518 printf("The value of --src_or_mask cannot be larger that --src_align.\n");
519 return NULL;
520 } else if (cmd_data->dst_or_mask > cmd_data->dst_align) {
521 printf("The value of --src_or_mask cannot be larger that --src_align.\n");
522 return NULL;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800523 }
524
525 return command;
526}
527
528bool raisePriorityAndLock(int cpu_to_lock) {
529 cpu_set_t cpuset;
530
531 if (setpriority(PRIO_PROCESS, 0, -20)) {
532 perror("Unable to raise priority of process.\n");
533 return false;
534 }
535
536 CPU_ZERO(&cpuset);
537 if (sched_getaffinity(0, sizeof(cpuset), &cpuset) != 0) {
538 perror("sched_getaffinity failed");
539 return false;
540 }
541
542 if (cpu_to_lock < 0) {
543 // Lock to the last active core we find.
544 for (int i = 0; i < CPU_SETSIZE; i++) {
545 if (CPU_ISSET(i, &cpuset)) {
546 cpu_to_lock = i;
547 }
548 }
549 } else if (!CPU_ISSET(cpu_to_lock, &cpuset)) {
550 printf("Cpu %d does not exist.\n", cpu_to_lock);
551 return false;
552 }
553
554 if (cpu_to_lock < 0) {
555 printf("Cannot find any valid cpu to lock.\n");
556 return false;
557 }
558
559 CPU_ZERO(&cpuset);
560 CPU_SET(cpu_to_lock, &cpuset);
561 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
562 perror("sched_setaffinity failed");
563 return false;
564 }
565
566 return true;
567}
568
569int main(int argc, char **argv) {
570 command_data_t cmd_data;
571
572 function_t *command = processOptions(argc, argv, &cmd_data);
573 if (!command) {
574 usage();
575 return -1;
576 }
577
578 if (!raisePriorityAndLock(cmd_data.cpu_to_lock)) {
579 return -1;
580 }
581
582 printf("%s\n", command->name);
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700583 return (*command->ptr)(command->name, cmd_data, command->func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800584}