1 /* Measure string and memory functions.
2 Copyright (C) 2013-2019 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"
70 # define UCHAR unsigned char
72 # define MAX_CHAR CHAR_MAX
73 # define MEMCHR memchr
74 # define MEMCMP memcmp
75 # define MEMCPY memcpy
76 # define MEMSET memset
77 # define STRCAT strcat
78 # define STRLEN strlen
79 # define STRCMP strcmp
80 # define STRCHR strchr
81 # define STRCPY strcpy
82 # define STRNLEN strnlen
83 # define STRCSPN strcspn
84 # define STRNCAT strncat
85 # define STRNCMP strncmp
86 # define STRNCPY strncpy
87 # define STRPBRK strpbrk
88 # define STRRCHR strrchr
89 # define STRSPN strspn
90 # define STPCPY stpcpy
91 # define STPNCPY stpncpy
95 # define UCHAR wchar_t
97 # define MAX_CHAR WCHAR_MAX
98 # define MEMCHR wmemchr
99 # define MEMCMP wmemcmp
100 # define MEMCPY wmemcpy
101 # define MEMSET wmemset
102 # define STRCAT wcscat
103 # define STRLEN wcslen
104 # define STRCMP wcscmp
105 # define STRCHR wcschr
106 # define STRCPY wcscpy
107 # define STRNLEN wcsnlen
108 # define STRCSPN wcscspn
109 # define STRNCAT wcsncat
110 # define STRNCMP wcsncmp
111 # define STRNCPY wcsncpy
112 # define STRPBRK wcspbrk
113 # define STRRCHR wcsrchr
114 # define STRSPN wcsspn
115 # define STPCPY wcpcpy
116 # define STPNCPY wcpncpy
119 # define TEST_FUNCTION test_main
121 # define TIMEOUT (4 * 60)
123 # define OPT_ITERATIONS 10000
124 # define OPT_RANDOM 10001
125 # define OPT_SEED 10002
127 # define INNER_LOOP_ITERS 8192
128 # define INNER_LOOP_ITERS8 32768
129 # define INNER_LOOP_ITERS_LARGE 131072
130 # define INNER_LOOP_ITERS_MEDIUM 2048
131 # define INNER_LOOP_ITERS_SMALL 256
137 size_t iterations
= 100000;
138 # define ITERATIONS_OPTIONS \
139 { "iterations", required_argument, NULL, OPT_ITERATIONS },
140 # define ITERATIONS_PROCESS \
141 case OPT_ITERATIONS: \
142 iterations = strtoul (optarg, NULL, 0); \
144 # define ITERATIONS iterations
146 # define ITERATIONS_OPTIONS
147 # define ITERATIONS_PROCESS
150 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
151 { "random", no_argument, NULL, OPT_RANDOM }, \
152 { "seed", required_argument, NULL, OPT_SEED },
154 static void __attribute__ ((used
))
155 cmdline_process_function (int c
)
162 int fdr
= open ("/dev/urandom", O_RDONLY
);
163 if (fdr
< 0 || read (fdr
, &seed
, sizeof (seed
)) != sizeof (seed
))
172 seed
= strtoul (optarg
, NULL
, 0);
177 # define CMDLINE_PROCESS cmdline_process_function
178 # define CALL(impl, ...) \
179 (* (proto_t) (impl)->fn) (__VA_ARGS__)
182 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
183 static struct libc_ifunc_impl func_list
[32];
184 static int func_count
;
185 static int impl_count
= -1;
186 static impl_t
*impl_array
;
188 # define FOR_EACH_IMPL(impl, notall) \
191 if (impl_count == -1) \
194 if (func_count != 0) \
197 impl_t *skip = NULL, *a; \
198 for (impl = __start_impls; impl < __stop_impls; ++impl) \
199 if (strcmp (impl->name, TEST_NAME) == 0) \
203 a = impl_array = malloc ((impl_count + func_count) * \
205 for (impl = __start_impls; impl < __stop_impls; ++impl) \
208 for (f = 0; f < func_count; f++) \
209 if (func_list[f].usable) \
211 a->name = func_list[f].name; \
212 a->fn = func_list[f].fn; \
216 impl_count = a - impl_array; \
220 impl_count = __stop_impls - __start_impls; \
221 impl_array = __start_impls; \
225 for (count = 0; count < impl_count; ++count, ++impl) \
226 if (!notall || impl->test)
227 # else /* !TEST_NAME */
228 # define FOR_EACH_IMPL(impl, notall) \
229 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
230 if (!notall || impl->test)
231 # endif /* !TEST_NAME */
237 unsigned char *buf1
, *buf2
;
238 static size_t buf1_size
, buf2_size
, page_size
;
243 page_size
= 2 * getpagesize ();
244 # ifdef MIN_PAGE_SIZE
245 if (page_size
< MIN_PAGE_SIZE
)
246 page_size
= MIN_PAGE_SIZE
;
249 buf1_size
= BUF1PAGES
* page_size
;
250 buf2_size
= page_size
;
254 exit_error (const char *id
, const char *func
)
256 error (EXIT_FAILURE
, errno
, "%s: %s failed", id
, func
);
259 /* Allocate a buffer of size SIZE with a guard page at the end. */
261 alloc_buf (const char *id
, size_t size
, unsigned char **retbuf
)
263 size_t alloc_size
= size
+ page_size
;
267 int ret
= munmap (*retbuf
, alloc_size
);
269 exit_error (id
, "munmap");
272 unsigned char *buf
= mmap (0, alloc_size
, PROT_READ
| PROT_WRITE
,
273 MAP_PRIVATE
| MAP_ANON
, -1, 0);
275 if (buf
== MAP_FAILED
)
276 exit_error (id
, "mmap");
277 if (mprotect (buf
+ size
, page_size
, PROT_NONE
))
278 exit_error (id
, "mprotect");
286 alloc_buf ("buf1", buf1_size
, &buf1
);
287 alloc_buf ("buf2", buf2_size
, &buf2
);
294 func_count
= __libc_ifunc_impl_list (TEST_NAME
, func_list
,
296 / sizeof func_list
[0]));
304 printf ("Setting seed to 0x%x\n", seed
);
309 #endif /* TEST_MAIN */