1 /* Measure string and memory functions.
2 Copyright (C) 2013-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include <sys/cdefs.h>
22 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
25 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
26 # define inhibit_loop_to_libcall \
27 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
29 # define inhibit_loop_to_libcall
38 extern impl_t __start_impls[], __stop_impls[];
40 #define IMPL(name, test) \
42 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
43 = { __STRING (name), (void (*) (void))name, test };
51 # undef __USE_STRING_INLINES
56 # include <sys/mman.h>
57 # include <sys/param.h>
63 # include <ifunc-impl-list.h>
66 # include "bench-timing.h"
69 # define TEST_FUNCTION test_main
71 # define TIMEOUT (4 * 60)
73 # define OPT_ITERATIONS 10000
74 # define OPT_RANDOM 10001
75 # define OPT_SEED 10002
77 # define INNER_LOOP_ITERS 64
83 size_t iterations = 100000;
84 # define ITERATIONS_OPTIONS \
85 { "iterations", required_argument, NULL, OPT_ITERATIONS },
86 # define ITERATIONS_PROCESS \
87 case OPT_ITERATIONS: \
88 iterations = strtoul (optarg, NULL, 0); \
90 # define ITERATIONS iterations
92 # define ITERATIONS_OPTIONS
93 # define ITERATIONS_PROCESS
96 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
97 { "random", no_argument, NULL, OPT_RANDOM }, \
98 { "seed", required_argument, NULL, OPT_SEED },
100 static void __attribute__ ((used))
101 cmdline_process_function (int c)
108 int fdr = open ("/dev/urandom", O_RDONLY);
109 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed))
118 seed = strtoul (optarg, NULL, 0);
123 # define CMDLINE_PROCESS cmdline_process_function
124 # define CALL(impl, ...) \
125 (* (proto_t) (impl)->fn) (__VA_ARGS__)
128 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
129 static struct libc_ifunc_impl func_list[32];
130 static int func_count;
131 static int impl_count = -1;
132 static impl_t *impl_array;
134 # define FOR_EACH_IMPL(impl, notall) \
137 if (impl_count == -1) \
140 if (func_count != 0) \
143 impl_t *skip = NULL, *a; \
144 for (impl = __start_impls; impl < __stop_impls; ++impl) \
145 if (strcmp (impl->name, TEST_NAME) == 0) \
149 a = impl_array = malloc ((impl_count + func_count) * \
151 for (impl = __start_impls; impl < __stop_impls; ++impl) \
154 for (f = 0; f < func_count; f++) \
155 if (func_list[f].usable) \
157 a->name = func_list[f].name; \
158 a->fn = func_list[f].fn; \
162 impl_count = a - impl_array; \
166 impl_count = __stop_impls - __start_impls; \
167 impl_array = __start_impls; \
171 for (count = 0; count < impl_count; ++count, ++impl) \
172 if (!notall || impl->test)
173 # else /* !TEST_NAME */
174 # define FOR_EACH_IMPL(impl, notall) \
175 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
176 if (!notall || impl->test)
177 # endif /* !TEST_NAME */
183 unsigned char *buf1, *buf2;
184 static size_t buf1_size, buf2_size, page_size;
189 page_size = 2 * getpagesize ();
190 # ifdef MIN_PAGE_SIZE
191 if (page_size < MIN_PAGE_SIZE)
192 page_size = MIN_PAGE_SIZE;
195 buf1_size = BUF1PAGES * page_size;
196 buf2_size = page_size;
200 exit_error (const char *id, const char *func)
202 error (EXIT_FAILURE, errno, "%s: %s failed", id, func);
205 /* Allocate a buffer of size SIZE with a guard page at the end. */
207 alloc_buf (const char *id, size_t size, unsigned char **retbuf)
209 size_t alloc_size = size + page_size;
213 int ret = munmap (*retbuf, alloc_size);
215 exit_error (id, "munmap");
218 unsigned char *buf = mmap (0, alloc_size, PROT_READ | PROT_WRITE,
219 MAP_PRIVATE | MAP_ANON, -1, 0);
221 if (buf == MAP_FAILED)
222 exit_error (id, "mmap");
223 if (mprotect (buf + size, page_size, PROT_NONE))
224 exit_error (id, "mprotect");
232 alloc_buf ("buf1", buf1_size, &buf1);
233 alloc_buf ("buf2", buf2_size, &buf2);
240 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
242 / sizeof func_list[0]));
250 printf ("Setting seed to 0x%x\n", seed);
255 #endif /* TEST_MAIN */