1 /* Measure string and memory functions.
2 Copyright (C) 2013-2025 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 <https://www.gnu.org/licenses/>. */
20 #include <sys/cdefs.h>
21 #include <programs/xmalloc.h>
23 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
26 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
27 # define inhibit_loop_to_libcall \
28 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
30 # define inhibit_loop_to_libcall
39 extern impl_t __start_impls
[], __stop_impls
[];
41 #define IMPL(name, test) \
43 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
44 = { __STRING (name), (void (*) (void))name, test };
52 # undef __USE_STRING_INLINES
57 # include <sys/mman.h>
58 # include <sys/param.h>
64 # include <ifunc-impl-list.h>
67 # include "bench-timing.h"
71 # define UCHAR unsigned char
73 # define MAX_CHAR CHAR_MAX
74 # define MEMCHR memchr
75 # define MEMCMP memcmp
76 # define MEMCPY memcpy
77 # define MEMSET memset
78 # define STRCAT strcat
79 # define STRLEN strlen
80 # define STRCMP strcmp
81 # define STRCHR strchr
82 # define STRCPY strcpy
83 # define STRNLEN strnlen
84 # define STRCSPN strcspn
85 # define STRNCAT strncat
86 # define STRNCMP strncmp
87 # define STRNCPY strncpy
88 # define STRPBRK strpbrk
89 # define STRRCHR strrchr
90 # define STRSPN strspn
91 # define STPCPY stpcpy
92 # define STPNCPY stpncpy
96 # define UCHAR wchar_t
98 # define MAX_CHAR WCHAR_MAX
99 # define MEMCHR wmemchr
100 # define MEMCMP wmemcmp
101 # define MEMCPY wmemcpy
102 # define MEMSET wmemset
103 # define STRCAT wcscat
104 # define STRLEN wcslen
105 # define STRCMP wcscmp
106 # define STRCHR wcschr
107 # define STRCPY wcscpy
108 # define STRNLEN wcsnlen
109 # define STRCSPN wcscspn
110 # define STRNCAT wcsncat
111 # define STRNCMP wcsncmp
112 # define STRNCPY wcsncpy
113 # define STRPBRK wcspbrk
114 # define STRRCHR wcsrchr
115 # define STRSPN wcsspn
116 # define STPCPY wcpcpy
117 # define STPNCPY wcpncpy
120 # define TEST_FUNCTION test_main
122 # define TIMEOUT (4 * 60)
124 # define OPT_ITERATIONS 10000
125 # define OPT_RANDOM 10001
126 # define OPT_SEED 10002
128 # define INNER_LOOP_ITERS 8192
129 # define INNER_LOOP_ITERS8 32768
130 # define INNER_LOOP_ITERS_LARGE 131072
131 # define INNER_LOOP_ITERS_MEDIUM 2048
132 # define INNER_LOOP_ITERS_SMALL 256
138 size_t iterations
= 100000;
139 # define ITERATIONS_OPTIONS \
140 { "iterations", required_argument, NULL, OPT_ITERATIONS },
141 # define ITERATIONS_PROCESS \
142 case OPT_ITERATIONS: \
143 iterations = strtoul (optarg, NULL, 0); \
145 # define ITERATIONS iterations
147 # define ITERATIONS_OPTIONS
148 # define ITERATIONS_PROCESS
151 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
152 { "random", no_argument, NULL, OPT_RANDOM }, \
153 { "seed", required_argument, NULL, OPT_SEED },
155 static void __attribute__ ((used
))
156 cmdline_process_function (int c
)
163 int fdr
= open ("/dev/urandom", O_RDONLY
);
164 if (fdr
< 0 || read (fdr
, &seed
, sizeof (seed
)) != sizeof (seed
))
173 seed
= strtoul (optarg
, NULL
, 0);
178 # define CMDLINE_PROCESS cmdline_process_function
179 # define CALL(impl, ...) \
180 (* (proto_t) (impl)->fn) (__VA_ARGS__)
183 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
184 static struct libc_ifunc_impl func_list
[32];
185 static int func_count
;
186 static int impl_count
= -1;
187 static impl_t
*impl_array
;
189 # define FOR_EACH_IMPL(impl, notall) \
192 if (impl_count == -1) \
195 if (func_count != 0) \
198 impl_t *skip = NULL, *a; \
199 for (impl = __start_impls; impl < __stop_impls; ++impl) \
200 if (strcmp (impl->name, TEST_NAME) == 0) \
204 a = impl_array = xmalloc ((impl_count + func_count) * \
206 for (impl = __start_impls; impl < __stop_impls; ++impl) \
209 for (f = 0; f < func_count; f++) \
210 if (func_list[f].usable) \
212 a->name = func_list[f].name; \
213 a->fn = func_list[f].fn; \
217 impl_count = a - impl_array; \
221 impl_count = __stop_impls - __start_impls; \
222 impl_array = __start_impls; \
226 for (count = 0; count < impl_count; ++count, ++impl) \
227 if (!notall || impl->test)
228 # else /* !TEST_NAME */
229 # define FOR_EACH_IMPL(impl, notall) \
230 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
231 if (!notall || impl->test)
232 # endif /* !TEST_NAME */
238 unsigned char *buf1
, *buf2
;
239 static size_t buf1_size
, buf2_size
, page_size
;
244 page_size
= 2 * getpagesize ();
245 # ifdef MIN_PAGE_SIZE
246 if (page_size
< MIN_PAGE_SIZE
)
247 page_size
= MIN_PAGE_SIZE
;
250 buf1_size
= BUF1PAGES
* page_size
;
251 buf2_size
= page_size
;
255 exit_error (const char *id
, const char *func
)
257 error (EXIT_FAILURE
, errno
, "%s: %s failed", id
, func
);
260 /* Allocate a buffer of size SIZE with a guard page at the end. */
262 alloc_buf (const char *id
, size_t size
, unsigned char **retbuf
)
264 size_t alloc_size
= size
+ page_size
;
268 int ret
= munmap (*retbuf
, alloc_size
);
270 exit_error (id
, "munmap");
273 unsigned char *buf
= mmap (0, alloc_size
, PROT_READ
| PROT_WRITE
,
274 MAP_PRIVATE
| MAP_ANON
, -1, 0);
276 if (buf
== MAP_FAILED
)
277 exit_error (id
, "mmap");
278 if (mprotect (buf
+ size
, page_size
, PROT_NONE
))
279 exit_error (id
, "mprotect");
287 alloc_buf ("buf1", buf1_size
, &buf1
);
288 alloc_buf ("buf2", buf2_size
, &buf2
);
295 func_count
= __libc_ifunc_impl_list (TEST_NAME
, func_list
,
297 / sizeof func_list
[0]));
305 printf ("Setting seed to 0x%x\n", seed
);
310 #endif /* TEST_MAIN */