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_ITERS_MEDIUM 1024
129 # define INNER_LOOP_ITERS_SMALL 32
135 size_t iterations
= 100000;
136 # define ITERATIONS_OPTIONS \
137 { "iterations", required_argument, NULL, OPT_ITERATIONS },
138 # define ITERATIONS_PROCESS \
139 case OPT_ITERATIONS: \
140 iterations = strtoul (optarg, NULL, 0); \
142 # define ITERATIONS iterations
144 # define ITERATIONS_OPTIONS
145 # define ITERATIONS_PROCESS
148 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
149 { "random", no_argument, NULL, OPT_RANDOM }, \
150 { "seed", required_argument, NULL, OPT_SEED },
152 static void __attribute__ ((used
))
153 cmdline_process_function (int c
)
160 int fdr
= open ("/dev/urandom", O_RDONLY
);
161 if (fdr
< 0 || read (fdr
, &seed
, sizeof (seed
)) != sizeof (seed
))
170 seed
= strtoul (optarg
, NULL
, 0);
175 # define CMDLINE_PROCESS cmdline_process_function
176 # define CALL(impl, ...) \
177 (* (proto_t) (impl)->fn) (__VA_ARGS__)
180 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
181 static struct libc_ifunc_impl func_list
[32];
182 static int func_count
;
183 static int impl_count
= -1;
184 static impl_t
*impl_array
;
186 # define FOR_EACH_IMPL(impl, notall) \
189 if (impl_count == -1) \
192 if (func_count != 0) \
195 impl_t *skip = NULL, *a; \
196 for (impl = __start_impls; impl < __stop_impls; ++impl) \
197 if (strcmp (impl->name, TEST_NAME) == 0) \
201 a = impl_array = malloc ((impl_count + func_count) * \
203 for (impl = __start_impls; impl < __stop_impls; ++impl) \
206 for (f = 0; f < func_count; f++) \
207 if (func_list[f].usable) \
209 a->name = func_list[f].name; \
210 a->fn = func_list[f].fn; \
214 impl_count = a - impl_array; \
218 impl_count = __stop_impls - __start_impls; \
219 impl_array = __start_impls; \
223 for (count = 0; count < impl_count; ++count, ++impl) \
224 if (!notall || impl->test)
225 # else /* !TEST_NAME */
226 # define FOR_EACH_IMPL(impl, notall) \
227 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
228 if (!notall || impl->test)
229 # endif /* !TEST_NAME */
235 unsigned char *buf1
, *buf2
;
236 static size_t buf1_size
, buf2_size
, page_size
;
241 page_size
= 2 * getpagesize ();
242 # ifdef MIN_PAGE_SIZE
243 if (page_size
< MIN_PAGE_SIZE
)
244 page_size
= MIN_PAGE_SIZE
;
247 buf1_size
= BUF1PAGES
* page_size
;
248 buf2_size
= page_size
;
252 exit_error (const char *id
, const char *func
)
254 error (EXIT_FAILURE
, errno
, "%s: %s failed", id
, func
);
257 /* Allocate a buffer of size SIZE with a guard page at the end. */
259 alloc_buf (const char *id
, size_t size
, unsigned char **retbuf
)
261 size_t alloc_size
= size
+ page_size
;
265 int ret
= munmap (*retbuf
, alloc_size
);
267 exit_error (id
, "munmap");
270 unsigned char *buf
= mmap (0, alloc_size
, PROT_READ
| PROT_WRITE
,
271 MAP_PRIVATE
| MAP_ANON
, -1, 0);
273 if (buf
== MAP_FAILED
)
274 exit_error (id
, "mmap");
275 if (mprotect (buf
+ size
, page_size
, PROT_NONE
))
276 exit_error (id
, "mprotect");
284 alloc_buf ("buf1", buf1_size
, &buf1
);
285 alloc_buf ("buf2", buf2_size
, &buf2
);
292 func_count
= __libc_ifunc_impl_list (TEST_NAME
, func_list
,
294 / sizeof func_list
[0]));
302 printf ("Setting seed to 0x%x\n", seed
);
307 #endif /* TEST_MAIN */