blob: fa56a1f7ac0a2ea0d094b32a1fd8a7b1bb88e6ed [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;
53
54 int args[MAX_ARGS];
55 int num_args;
56} command_data_t;
57
Christopher Ferris014cf9d2013-06-26 13:42:18 -070058typedef void *(*void_func_t)();
59typedef void *(*memcpy_func_t)(void *, const void *, size_t);
60typedef void *(*memset_func_t)(void *, int, size_t);
61typedef int (*strcmp_func_t)(const char *, const char *);
62typedef char *(*strcpy_func_t)(char *, const char *);
63
Christopher Ferris82ac1af2013-02-15 12:27:58 -080064// Struct that contains a mapping of benchmark name to benchmark function.
65typedef struct {
66 const char *name;
Christopher Ferris014cf9d2013-06-26 13:42:18 -070067 int (*ptr)(const char *, const command_data_t &, void_func_t func);
68 void_func_t func;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080069} function_t;
70
71// Get the current time in nanoseconds.
72uint64_t nanoTime() {
73 struct timespec t;
74
75 t.tv_sec = t.tv_nsec = 0;
76 clock_gettime(CLOCK_MONOTONIC, &t);
77 return static_cast<uint64_t>(t.tv_sec) * NS_PER_SEC + t.tv_nsec;
78}
79
80// Allocate memory with a specific alignment and return that pointer.
81// This function assumes an alignment value that is a power of 2.
82// If the alignment is 0, then use the pointer returned by malloc.
Christopher Ferris25ada902013-04-02 13:28:16 -070083uint8_t *getAlignedMemory(uint8_t *orig_ptr, int alignment, int or_mask) {
84 uint64_t ptr = reinterpret_cast<uint64_t>(orig_ptr);
Christopher Ferris82ac1af2013-02-15 12:27:58 -080085 if (alignment > 0) {
86 // When setting the alignment, set it to exactly the alignment chosen.
87 // The pointer returned will be guaranteed not to be aligned to anything
88 // more than that.
89 ptr += alignment - (ptr & (alignment - 1));
Christopher Ferris25ada902013-04-02 13:28:16 -070090 ptr |= alignment | or_mask;
Christopher Ferris82ac1af2013-02-15 12:27:58 -080091 }
92
93 return reinterpret_cast<uint8_t*>(ptr);
94}
95
Christopher Ferris25ada902013-04-02 13:28:16 -070096// Allocate memory with a specific alignment and return that pointer.
97// This function assumes an alignment value that is a power of 2.
98// If the alignment is 0, then use the pointer returned by malloc.
99uint8_t *allocateAlignedMemory(size_t size, int alignment, int or_mask) {
100 uint64_t ptr = reinterpret_cast<uint64_t>(malloc(size + 3 * alignment));
101 if (!ptr)
102 return NULL;
103 return getAlignedMemory((uint8_t*)ptr, alignment, or_mask);
104}
105
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700106static inline double computeAverage(uint64_t time_ns, int size, int copies) {
107 return ((size/1024.0) * copies) / ((double)time_ns/NS_PER_SEC);
108}
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800109
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700110static inline double computeRunningAvg(double avg, double running_avg, size_t cur_idx) {
111 return (running_avg / (cur_idx + 1)) * cur_idx + (avg / (cur_idx + 1));
112}
113
114static inline double computeRunningSquareAvg(double avg, double square_avg, size_t cur_idx) {
115 return (square_avg / (cur_idx + 1)) * cur_idx + (avg / (cur_idx + 1)) * avg;
116}
117
118static inline double computeStdDev(double square_avg, double running_avg) {
119 return sqrt(square_avg - running_avg * running_avg);
120}
121
122static inline void printIter(uint64_t time_ns, const char *name, int size, int copies, double avg) {
123 printf("%s %dx%d bytes took %.06f seconds (%f MB/s)\n",
124 name, copies, size, (double)time_ns/NS_PER_SEC, avg/1024.0);
125}
126
127static inline void printSummary(uint64_t time_ns, const char *name, int size, int copies, double running_avg, double std_dev, double min, double max) {
128 printf(" %s %dx%d bytes average %.2f MB/s std dev %.4f min %.2f MB/s max %.2f MB/s\n",
129 name, copies, size, running_avg/1024.0, std_dev/1024.0, min/1024.0,
130 max/1024.0);
131}
132
133#define MAINLOOP(cmd_data, BENCH, COMPUTE_AVG, PRINT_ITER, PRINT_AVG) \
134 uint64_t time_ns; \
135 int iters = cmd_data.args[1]; \
136 bool print_average = cmd_data.print_average; \
137 bool print_each_iter = cmd_data.print_each_iter; \
138 double min = 0.0, max = 0.0, running_avg = 0.0, square_avg = 0.0; \
139 double avg; \
140 for (int i = 0; iters == -1 || i < iters; i++) { \
141 time_ns = nanoTime(); \
142 BENCH; \
143 time_ns = nanoTime() - time_ns; \
144 avg = COMPUTE_AVG; \
145 if (print_average) { \
146 running_avg = computeRunningAvg(avg, running_avg, i); \
147 square_avg = computeRunningSquareAvg(avg, square_avg, i); \
148 if (min == 0.0 || avg < min) { \
149 min = avg; \
150 } \
151 if (avg > max) { \
152 max = avg; \
153 } \
154 } \
155 if (print_each_iter) { \
156 PRINT_ITER; \
157 } \
158 } \
159 if (print_average) { \
160 PRINT_AVG; \
161 }
162
163#define MAINLOOP_DATA(name, cmd_data, size, BENCH) \
164 int copies = cmd_data.data_size/size; \
165 int j; \
166 MAINLOOP(cmd_data, \
167 for (j = 0; j < copies; j++) { \
168 BENCH; \
169 }, \
170 computeAverage(time_ns, size, copies), \
171 printIter(time_ns, name, size, copies, avg), \
172 double std_dev = computeStdDev(square_avg, running_avg); \
173 printSummary(time_ns, name, size, copies, running_avg, \
174 std_dev, min, max));
175
176int benchmarkSleep(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800177 int delay = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700178 MAINLOOP(cmd_data, sleep(delay),
179 (double)time_ns/NS_PER_SEC,
180 printf("sleep(%d) took %.06f seconds\n", delay, avg);,
181 printf(" sleep(%d) average %.06f seconds std dev %f min %.06f seconds max %0.6f seconds\n", \
182 delay, running_avg, computeStdDev(square_avg, running_avg), \
183 min, max));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800184
185 return 0;
186}
187
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700188int benchmarkCpu(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800189 // Use volatile so that the loop is not optimized away by the compiler.
190 volatile int cpu_foo;
191
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700192 MAINLOOP(cmd_data,
193 for (cpu_foo = 0; cpu_foo < 100000000; cpu_foo++),
194 (double)time_ns/NS_PER_SEC,
195 printf("cpu took %.06f seconds\n", avg),
196 printf(" cpu average %.06f seconds std dev %f min %0.6f seconds max %0.6f seconds\n", \
197 running_avg, computeStdDev(square_avg, running_avg), min, max));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800198
199 return 0;
200}
201
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700202int benchmarkMemset(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800203 int size = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700204 memset_func_t memset_func = reinterpret_cast<memset_func_t>(func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800205
Christopher Ferris25ada902013-04-02 13:28:16 -0700206 uint8_t *dst = allocateAlignedMemory(size, cmd_data.dst_align, cmd_data.dst_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800207 if (!dst)
208 return -1;
209
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700210 MAINLOOP_DATA(name, cmd_data, size, memset_func(dst, 0, size));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800211
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800212 return 0;
213}
214
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700215int benchmarkMemcpy(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800216 int size = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700217 memcpy_func_t memcpy_func = reinterpret_cast<memcpy_func_t>(func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800218
Christopher Ferris25ada902013-04-02 13:28:16 -0700219 uint8_t *src = allocateAlignedMemory(size, cmd_data.src_align, cmd_data.src_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800220 if (!src)
221 return -1;
Christopher Ferris25ada902013-04-02 13:28:16 -0700222 uint8_t *dst = allocateAlignedMemory(size, cmd_data.dst_align, cmd_data.dst_or_mask);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800223 if (!dst)
224 return -1;
225
Christopher Ferris25ada902013-04-02 13:28:16 -0700226 // Initialize the source and destination to known values.
227 // If not initialized, the benchmark results are skewed.
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700228 memset(src, 0xff, size);
Christopher Ferris25ada902013-04-02 13:28:16 -0700229 memset(dst, 0, size);
230
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700231 MAINLOOP_DATA(name, cmd_data, size, memcpy_func(dst, src, size));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800232
Christopher Ferris25ada902013-04-02 13:28:16 -0700233 return 0;
234}
235
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700236int benchmarkMemread(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris25ada902013-04-02 13:28:16 -0700237 int size = cmd_data.args[0];
Christopher Ferris25ada902013-04-02 13:28:16 -0700238
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700239 uint32_t *src = reinterpret_cast<uint32_t*>(malloc(size));
240 if (!src)
Christopher Ferris25ada902013-04-02 13:28:16 -0700241 return -1;
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700242 memset(src, 0xff, size);
Christopher Ferris25ada902013-04-02 13:28:16 -0700243
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700244 // Use volatile so the compiler does not optimize away the reads.
245 volatile int foo;
246 size_t k;
247 MAINLOOP_DATA(name, cmd_data, size,
248 for (k = 0; k < size/sizeof(uint32_t); k++) foo = src[k]);
249
250 return 0;
251}
252
253int benchmarkStrcmp(const char *name, const command_data_t &cmd_data, void_func_t func) {
254 int size = cmd_data.args[0];
255 strcmp_func_t strcmp_func = reinterpret_cast<strcmp_func_t>(func);
256
257 char *string1 = reinterpret_cast<char*>(allocateAlignedMemory(size, cmd_data.src_align, cmd_data.src_or_mask));
258 if (!string1)
259 return -1;
260 char *string2 = reinterpret_cast<char*>(allocateAlignedMemory(size, cmd_data.dst_align, cmd_data.dst_or_mask));
261 if (!string2)
262 return -1;
Christopher Ferris25ada902013-04-02 13:28:16 -0700263
264 for (int i = 0; i < size - 1; i++) {
265 string1[i] = (char)(32 + (i % 96));
266 string2[i] = string1[i];
267 }
268 string1[size-1] = '\0';
269 string2[size-1] = '\0';
270
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700271 int retval;
272 MAINLOOP_DATA(name, cmd_data, size,
273 retval = strcmp_func(string1, string2); \
274 if (retval != 0) printf("%s failed, return value %d\n", name, retval));
Christopher Ferris25ada902013-04-02 13:28:16 -0700275
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800276 return 0;
277}
278
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700279int benchmarkStrcpy(const char *name, const command_data_t &cmd_data, void_func_t func) {
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800280 int size = cmd_data.args[0];
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700281 strcpy_func_t strcpy_func = reinterpret_cast<strcpy_func_t>(func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800282
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700283 char *src = reinterpret_cast<char*>(allocateAlignedMemory(size, cmd_data.src_align, cmd_data.src_or_mask));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800284 if (!src)
285 return -1;
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700286 char *dst = reinterpret_cast<char*>(allocateAlignedMemory(size, cmd_data.dst_align, cmd_data.dst_or_mask));
287 if (!dst)
288 return -1;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800289
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700290 for (int i = 0; i < size - 1; i++) {
291 src[i] = (char)(32 + (i % 96));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800292 }
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700293 src[size-1] = '\0';
294 memset(dst, 0, size);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800295
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700296 MAINLOOP_DATA(name, cmd_data, size, strcpy_func(dst, src));
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800297
298 return 0;
299}
300
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700301
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800302// Create the mapping structure.
303function_t function_table[] = {
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700304 { "sleep", benchmarkSleep, NULL },
305 { "cpu", benchmarkCpu, NULL },
306 { "memread", benchmarkMemread, NULL },
307 { "memset", benchmarkMemset, reinterpret_cast<void_func_t>(memset) },
308 { "memcpy", benchmarkMemcpy, reinterpret_cast<void_func_t>(memcpy) },
309 { "strcmp", benchmarkStrcmp, reinterpret_cast<void_func_t>(strcmp) },
310 { "strcpy", benchmarkStrcpy, reinterpret_cast<void_func_t>(strcpy) },
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800311};
312
313void usage() {
314 printf("Usage:\n");
315 printf(" micro_bench [--data_size DATA_BYTES] [--print_average]\n");
316 printf(" [--no_print_each_iter] [--lock_to_cpu CORE]\n");
317 printf(" --data_size DATA_BYTES\n");
318 printf(" For the data benchmarks (memcpy/memset/memread) the approximate\n");
319 printf(" size of data, in bytes, that will be manipulated in each iteration.\n");
320 printf(" --print_average\n");
321 printf(" Print the average and standard deviation of all iterations.\n");
322 printf(" --no_print_each_iter\n");
323 printf(" Do not print any values in each iteration.\n");
324 printf(" --lock_to_cpu CORE\n");
325 printf(" Lock to the specified CORE. The default is to use the last core found.\n");
326 printf(" ITERS\n");
327 printf(" The number of iterations to execute each benchmark. If not\n");
328 printf(" passed in then run forever.\n");
329 printf(" micro_bench sleep TIME_TO_SLEEP [ITERS]\n");
330 printf(" TIME_TO_SLEEP\n");
331 printf(" The time in seconds to sleep.\n");
332 printf(" micro_bench cpu UNUSED [ITERS]\n");
333 printf(" micro_bench [--dst_align ALIGN] memset NUM_BYTES [ITERS]\n");
334 printf(" --dst_align ALIGN\n");
335 printf(" Align the memset destination pointer to ALIGN. The default is to use the\n");
336 printf(" value returned by malloc.\n");
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700337 printf(" micro_bench [--src_align ALIGN] [--dst_align ALIGN] strcpy NUM_BYTES [ITERS]\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800338 printf(" --src_align ALIGN\n");
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700339 printf(" Align the strcpy source string to ALIGN. The default is to use the\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800340 printf(" value returned by malloc.\n");
341 printf(" --dst_align ALIGN\n");
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700342 printf(" Align the strcpy destination string to ALIGN. The default is to use the\n");
343 printf(" value returned by malloc.\n");
344 printf(" micro_bench [--src_align ALIGN] [--dst_align ALIGN] strcmp NUM_BYTES [ITERS]\n");
345 printf(" --src_align ALIGN\n");
346 printf(" Align the first strcmp string to ALIGN. The default is to use the\n");
347 printf(" value returned by malloc.\n");
348 printf(" --dst_align ALIGN\n");
349 printf(" Align the second strcmp string to ALIGN. The default is to use the\n");
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800350 printf(" value returned by malloc.\n");
351 printf(" micro_bench memread NUM_BYTES [ITERS]\n");
352}
353
354function_t *processOptions(int argc, char **argv, command_data_t *cmd_data) {
355 function_t *command = NULL;
356
357 // Initialize the command_flags.
358 cmd_data->print_average = false;
359 cmd_data->print_each_iter = true;
360 cmd_data->dst_align = 0;
361 cmd_data->src_align = 0;
Christopher Ferris25ada902013-04-02 13:28:16 -0700362 cmd_data->src_or_mask = 0;
363 cmd_data->dst_or_mask = 0;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800364 cmd_data->num_args = 0;
365 cmd_data->cpu_to_lock = -1;
366 cmd_data->data_size = DEFAULT_DATA_SIZE;
367 for (int i = 0; i < MAX_ARGS; i++) {
368 cmd_data->args[i] = -1;
369 }
370
371 for (int i = 1; i < argc; i++) {
372 if (argv[i][0] == '-') {
373 int *save_value = NULL;
374 if (strcmp(argv[i], "--print_average") == 0) {
375 cmd_data->print_average = true;
376 } else if (strcmp(argv[i], "--no_print_each_iter") == 0) {
377 cmd_data->print_each_iter = false;
378 } else if (strcmp(argv[i], "--dst_align") == 0) {
379 save_value = &cmd_data->dst_align;
380 } else if (strcmp(argv[i], "--src_align") == 0) {
381 save_value = &cmd_data->src_align;
Christopher Ferris25ada902013-04-02 13:28:16 -0700382 } else if (strcmp(argv[i], "--dst_or_mask") == 0) {
383 save_value = &cmd_data->dst_or_mask;
384 } else if (strcmp(argv[i], "--src_or_mask") == 0) {
385 save_value = &cmd_data->src_or_mask;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800386 } else if (strcmp(argv[i], "--lock_to_cpu") == 0) {
387 save_value = &cmd_data->cpu_to_lock;
388 } else if (strcmp(argv[i], "--data_size") == 0) {
389 save_value = &cmd_data->data_size;
390 } else {
391 printf("Unknown option %s\n", argv[i]);
392 return NULL;
393 }
394 if (save_value) {
395 // Checking both characters without a strlen() call should be
396 // safe since as long as the argument exists, one character will
397 // be present (\0). And if the first character is '-', then
398 // there will always be a second character (\0 again).
399 if (i == argc - 1 || (argv[i + 1][0] == '-' && !isdigit(argv[i + 1][1]))) {
400 printf("The option %s requires one argument.\n",
401 argv[i]);
402 return NULL;
403 }
Christopher Ferris25ada902013-04-02 13:28:16 -0700404 *save_value = (int)strtol(argv[++i], NULL, 0);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800405 }
406 } else if (!command) {
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700407 for (size_t j = 0; j < sizeof(function_table)/sizeof(function_t); j++) {
408 if (strcmp(argv[i], function_table[j].name) == 0) {
409 command = &function_table[j];
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800410 break;
411 }
412 }
413 if (!command) {
414 printf("Uknown command %s\n", argv[i]);
415 return NULL;
416 }
417 } else if (cmd_data->num_args > MAX_ARGS) {
418 printf("More than %d number arguments passed in.\n", MAX_ARGS);
419 return NULL;
420 } else {
421 cmd_data->args[cmd_data->num_args++] = atoi(argv[i]);
422 }
423 }
424
425 // Check the arguments passed in make sense.
426 if (cmd_data->num_args != 1 && cmd_data->num_args != 2) {
427 printf("Not enough arguments passed in.\n");
428 return NULL;
429 } else if (cmd_data->dst_align < 0) {
430 printf("The --dst_align option must be greater than or equal to 0.\n");
431 return NULL;
432 } else if (cmd_data->src_align < 0) {
433 printf("The --src_align option must be greater than or equal to 0.\n");
434 return NULL;
435 } else if (cmd_data->data_size <= 0) {
436 printf("The --data_size option must be a positive number.\n");
437 return NULL;
438 } else if ((cmd_data->dst_align & (cmd_data->dst_align - 1))) {
439 printf("The --dst_align option must be a power of 2.\n");
440 return NULL;
441 } else if ((cmd_data->src_align & (cmd_data->src_align - 1))) {
442 printf("The --src_align option must be a power of 2.\n");
443 return NULL;
Christopher Ferris25ada902013-04-02 13:28:16 -0700444 } else if (!cmd_data->src_align && cmd_data->src_or_mask) {
445 printf("The --src_or_mask option requires that --src_align be set.\n");
446 return NULL;
447 } else if (!cmd_data->dst_align && cmd_data->dst_or_mask) {
448 printf("The --dst_or_mask option requires that --dst_align be set.\n");
449 return NULL;
450 } else if (cmd_data->src_or_mask > cmd_data->src_align) {
451 printf("The value of --src_or_mask cannot be larger that --src_align.\n");
452 return NULL;
453 } else if (cmd_data->dst_or_mask > cmd_data->dst_align) {
454 printf("The value of --src_or_mask cannot be larger that --src_align.\n");
455 return NULL;
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800456 }
457
458 return command;
459}
460
461bool raisePriorityAndLock(int cpu_to_lock) {
462 cpu_set_t cpuset;
463
464 if (setpriority(PRIO_PROCESS, 0, -20)) {
465 perror("Unable to raise priority of process.\n");
466 return false;
467 }
468
469 CPU_ZERO(&cpuset);
470 if (sched_getaffinity(0, sizeof(cpuset), &cpuset) != 0) {
471 perror("sched_getaffinity failed");
472 return false;
473 }
474
475 if (cpu_to_lock < 0) {
476 // Lock to the last active core we find.
477 for (int i = 0; i < CPU_SETSIZE; i++) {
478 if (CPU_ISSET(i, &cpuset)) {
479 cpu_to_lock = i;
480 }
481 }
482 } else if (!CPU_ISSET(cpu_to_lock, &cpuset)) {
483 printf("Cpu %d does not exist.\n", cpu_to_lock);
484 return false;
485 }
486
487 if (cpu_to_lock < 0) {
488 printf("Cannot find any valid cpu to lock.\n");
489 return false;
490 }
491
492 CPU_ZERO(&cpuset);
493 CPU_SET(cpu_to_lock, &cpuset);
494 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
495 perror("sched_setaffinity failed");
496 return false;
497 }
498
499 return true;
500}
501
502int main(int argc, char **argv) {
503 command_data_t cmd_data;
504
505 function_t *command = processOptions(argc, argv, &cmd_data);
506 if (!command) {
507 usage();
508 return -1;
509 }
510
511 if (!raisePriorityAndLock(cmd_data.cpu_to_lock)) {
512 return -1;
513 }
514
515 printf("%s\n", command->name);
Christopher Ferris014cf9d2013-06-26 13:42:18 -0700516 return (*command->ptr)(command->name, cmd_data, command->func);
Christopher Ferris82ac1af2013-02-15 12:27:58 -0800517}