2 * Copyright (c) 2011, Linaro Limited
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Linaro nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /** A simple harness that times how long a string function takes to
32 /* PENDING: Add EPL */
44 #define NUM_ELEMS(_x) (sizeof(_x) / sizeof((_x)[0]))
47 #define VERSION "(unknown version)"
50 /** Make sure a function is called by using the return value */
51 #define SPOIL(_x) volatile long x = (long)(_x); (void)x
53 /** Type of functions that can be tested */
54 typedef void (*stub_t)(void *dest, void *src, size_t n);
56 /** Meta data about one test */
61 /** Function to test */
65 /** Flush the cache by reading a chunk of memory */
66 static void empty(volatile char *against)
68 /* We know that there's a 16 k cache with 64 byte lines giving
69 a total of 256 lines. Read randomly from 256*5 places should
71 int offset = (1024 - 256)*1024;
73 for (int i = offset; i < offset + 16*1024*3; i += 64)
79 /** Stub that does nothing. Used for calibrating */
80 static void xbounce(void *dest, void *src, size_t n)
85 /** Stub that calls memcpy */
86 static void xmemcpy(void *dest, void *src, size_t n)
88 SPOIL(memcpy(dest, src, n));
91 /** Stub that calls memset */
92 static void xmemset(void *dest, void *src, size_t n)
94 SPOIL(memset(dest, 0, n));
97 /** Stub that calls memcmp */
98 static void xmemcmp(void *dest, void *src, size_t n)
100 SPOIL(memcmp(dest, src, n));
103 /** Stub that calls strcpy */
104 static void xstrcpy(void *dest, void *src, size_t n)
106 SPOIL(strcpy(dest, src));
109 /** Stub that calls strlen */
110 static void xstrlen(void *dest, void *src, size_t n)
115 /** Stub that calls strcmp */
116 static void xstrcmp(void *dest, void *src, size_t n)
118 SPOIL(strcmp(dest, src));
121 /** Stub that calls strchr */
122 static void xstrchr(void *dest, void *src, size_t n)
124 /* Put the character at the end of the string and before the null */
125 ((char *)src)[n-1] = 32;
126 SPOIL(strchr(src, 32));
129 /** Stub that calls memchr */
130 static void xmemchr(void *dest, void *src, size_t n)
132 /* Put the character at the end of the block */
133 ((char *)src)[n-1] = 32;
134 SPOIL(memchr(src, 32, n));
137 /** All functions that can be tested */
138 static const struct test tests[] =
140 { "bounce", xbounce },
141 { "memchr", xmemchr },
142 { "memcpy", xmemcpy },
143 { "memset", xmemset },
144 { "memcmp", xmemcmp },
145 { "strchr", xstrchr },
146 { "strcmp", xstrcmp },
147 { "strcpy", xstrcpy },
148 { "strlen", xstrlen },
152 /** Show basic usage */
153 static void usage(const char* name)
155 printf("%s %s: run a string related benchmark.\n"
156 "usage: %s [-c block-size] [-l loop-count] [-a alignment|src_alignment:dst_alignment] [-f] [-t test-name] [-r run-id]\n"
157 , name, VERSION, name);
161 for (const struct test *ptest = tests; ptest->name != NULL; ptest++)
163 printf(" %s", ptest->name);
171 /** Find the test by name */
172 static const struct test *find_test(const char *name)
180 for (const struct test *p = tests; p->name != NULL; p++)
182 if (strcmp(p->name, name) == 0)
192 #define MIN_BUFFER_SIZE 1024*1024
193 #define MAX_ALIGNMENT 256
195 /** Take a pointer and ensure that the lower bits == alignment */
196 static char *realign(char *p, int alignment)
198 uintptr_t pp = (uintptr_t)p;
199 pp = (pp + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1);
205 static int parse_int_arg(const char *arg, const char *exe_name)
210 ret = strtol(arg, NULL, 0);
220 static void parse_alignment_arg(const char *arg, const char *exe_name,
221 int *src_alignment, int *dst_alignment)
227 ret = strtol(arg, &endptr, 0);
234 *src_alignment = (int)ret;
236 if (ret > 256 || ret < 1)
238 printf("Alignment should be in the range [1, 256].\n");
245 if (endptr && *endptr == ':')
248 ret = strtol(endptr + 1, NULL, 0);
255 if (ret > 256 || ret < 1)
257 printf("Alignment should be in the range [1, 256].\n");
265 *dst_alignment = (int)ret;
268 /** Setup and run a test */
269 int main(int argc, char **argv)
271 /* Size of src and dest buffers */
272 size_t buffer_size = MIN_BUFFER_SIZE;
274 /* Number of bytes per call */
276 /* Number of times to run */
277 int loops = 10000000;
278 /* True to flush the cache each time */
280 /* Name of the test */
281 const char *name = NULL;
282 /* Alignment of buffers */
283 int src_alignment = 8;
284 int dst_alignment = 8;
285 /* Name of the run */
286 const char *run_id = "0";
290 while ((opt = getopt(argc, argv, "c:l:ft:r:hva:")) > 0)
295 count = parse_int_arg(optarg, argv[0]);
298 loops = parse_int_arg(optarg, argv[0]);
301 parse_alignment_arg(optarg, argv[0], &src_alignment, &dst_alignment);
307 name = strdup(optarg);
310 run_id = strdup(optarg);
321 /* Find the test by name */
322 const struct test *ptest = find_test(name);
329 if (count + MAX_ALIGNMENT * 2 > MIN_BUFFER_SIZE)
331 buffer_size = count + MAX_ALIGNMENT * 2;
334 /* Buffers to read and write from */
335 char *src = malloc(buffer_size);
336 char *dest = malloc(buffer_size);
338 assert(src != NULL && dest != NULL);
340 src = realign(src, src_alignment);
341 dest = realign(dest, dst_alignment);
343 /* Fill the buffer with non-zero, reproducable random data */
346 for (int i = 0; i < buffer_size; i++)
348 src[i] = (char)random() | 1;
352 /* Make sure the buffers are null terminated for any string tests */
356 struct timespec start, end;
357 int err = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
361 stub_t stub = ptest->stub;
363 /* Run two variants to reduce the cost of testing for the flush */
366 for (int i = 0; i < loops; i++)
368 (*stub)(dest, src, count);
373 for (int i = 0; i < loops; i++)
375 (*stub)(dest, src, count);
380 err = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
383 /* Drop any leading path and pull the variant name out of the executable */
384 char *variant = strrchr(argv[0], '/');
391 variant = strstr(variant, "try-");
392 assert(variant != NULL);
394 double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
395 /* Estimate the bounce time. Measured on a Panda. */
396 double bounced = 0.448730 * loops / 50000000;
398 /* Dump both machine and human readable versions */
399 printf("%s:%s:%u:%u:%d:%d:%s:%.6f: took %.6f s for %u calls to %s of %u bytes. ~%.3f MB/s corrected.\n",
400 variant + 4, ptest->name,
401 count, loops, src_alignment, dst_alignment, run_id,
403 elapsed, loops, ptest->name, count,
404 (double)loops*count/(elapsed - bounced)/(1024*1024));