]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-string.h
NEWS: Add advisories.
[thirdparty/glibc.git] / benchtests / bench-string.h
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.
4
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.
9
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.
14
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/>. */
18
19 #include <getopt.h>
20 #include <sys/cdefs.h>
21
22 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
23 for us. */
24 #include "config.h"
25 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
26 # define inhibit_loop_to_libcall \
27 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
28 #else
29 # define inhibit_loop_to_libcall
30 #endif
31
32 typedef struct
33 {
34 const char *name;
35 void (*fn) (void);
36 long test;
37 } impl_t;
38 extern impl_t __start_impls[], __stop_impls[];
39
40 #define IMPL(name, test) \
41 impl_t tst_ ## name \
42 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
43 = { __STRING (name), (void (*) (void))name, test };
44
45 #ifdef TEST_MAIN
46
47 # ifndef _GNU_SOURCE
48 # define _GNU_SOURCE
49 # endif
50
51 # undef __USE_STRING_INLINES
52
53 # include <stdio.h>
54 # include <stdlib.h>
55 # include <string.h>
56 # include <sys/mman.h>
57 # include <sys/param.h>
58 # include <unistd.h>
59 # include <fcntl.h>
60 # include <error.h>
61 # include <errno.h>
62 # include <time.h>
63 # include <ifunc-impl-list.h>
64 # define GL(x) _##x
65 # define GLRO(x) _##x
66 # include "bench-timing.h"
67
68 # ifndef WIDE
69 # define CHAR char
70 # define UCHAR unsigned char
71 # define CHARBYTES 1
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
92 # else
93 # include <wchar.h>
94 # define CHAR wchar_t
95 # define UCHAR wchar_t
96 # define CHARBYTES 4
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
117 # endif /* WIDE */
118
119 # define TEST_FUNCTION test_main
120 # ifndef TIMEOUT
121 # define TIMEOUT (4 * 60)
122 # endif
123 # define OPT_ITERATIONS 10000
124 # define OPT_RANDOM 10001
125 # define OPT_SEED 10002
126
127 # define INNER_LOOP_ITERS 8192
128 # define INNER_LOOP_ITERS_MEDIUM 1024
129 # define INNER_LOOP_ITERS_SMALL 32
130
131 int ret, do_srandom;
132 unsigned int seed;
133
134 # ifndef ITERATIONS
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); \
141 break;
142 # define ITERATIONS iterations
143 # else
144 # define ITERATIONS_OPTIONS
145 # define ITERATIONS_PROCESS
146 # endif
147
148 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
149 { "random", no_argument, NULL, OPT_RANDOM }, \
150 { "seed", required_argument, NULL, OPT_SEED },
151
152 static void __attribute__ ((used))
153 cmdline_process_function (int c)
154 {
155 switch (c)
156 {
157 ITERATIONS_PROCESS
158 case OPT_RANDOM:
159 {
160 int fdr = open ("/dev/urandom", O_RDONLY);
161 if (fdr < 0 || read (fdr, &seed, sizeof (seed)) != sizeof (seed))
162 seed = time (NULL);
163 if (fdr >= 0)
164 close (fdr);
165 do_srandom = 1;
166 break;
167 }
168
169 case OPT_SEED:
170 seed = strtoul (optarg, NULL, 0);
171 do_srandom = 1;
172 break;
173 }
174 }
175 # define CMDLINE_PROCESS cmdline_process_function
176 # define CALL(impl, ...) \
177 (* (proto_t) (impl)->fn) (__VA_ARGS__)
178
179 # ifdef TEST_NAME
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;
185
186 # define FOR_EACH_IMPL(impl, notall) \
187 impl_t *impl; \
188 int count; \
189 if (impl_count == -1) \
190 { \
191 impl_count = 0; \
192 if (func_count != 0) \
193 { \
194 int f; \
195 impl_t *skip = NULL, *a; \
196 for (impl = __start_impls; impl < __stop_impls; ++impl) \
197 if (strcmp (impl->name, TEST_NAME) == 0) \
198 skip = impl; \
199 else \
200 impl_count++; \
201 a = impl_array = malloc ((impl_count + func_count) * \
202 sizeof (impl_t)); \
203 for (impl = __start_impls; impl < __stop_impls; ++impl) \
204 if (impl != skip) \
205 *a++ = *impl; \
206 for (f = 0; f < func_count; f++) \
207 if (func_list[f].usable) \
208 { \
209 a->name = func_list[f].name; \
210 a->fn = func_list[f].fn; \
211 a->test = 1; \
212 a++; \
213 } \
214 impl_count = a - impl_array; \
215 } \
216 else \
217 { \
218 impl_count = __stop_impls - __start_impls; \
219 impl_array = __start_impls; \
220 } \
221 } \
222 impl = impl_array; \
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 */
230
231 # ifndef BUF1PAGES
232 # define BUF1PAGES 1
233 # endif
234
235 unsigned char *buf1, *buf2;
236 static size_t buf1_size, buf2_size, page_size;
237
238 static void
239 init_sizes (void)
240 {
241 page_size = 2 * getpagesize ();
242 # ifdef MIN_PAGE_SIZE
243 if (page_size < MIN_PAGE_SIZE)
244 page_size = MIN_PAGE_SIZE;
245 # endif
246
247 buf1_size = BUF1PAGES * page_size;
248 buf2_size = page_size;
249 }
250
251 static void
252 exit_error (const char *id, const char *func)
253 {
254 error (EXIT_FAILURE, errno, "%s: %s failed", id, func);
255 }
256
257 /* Allocate a buffer of size SIZE with a guard page at the end. */
258 static void
259 alloc_buf (const char *id, size_t size, unsigned char **retbuf)
260 {
261 size_t alloc_size = size + page_size;
262
263 if (*retbuf != NULL)
264 {
265 int ret = munmap (*retbuf, alloc_size);
266 if (ret != 0)
267 exit_error (id, "munmap");
268 }
269
270 unsigned char *buf = mmap (0, alloc_size, PROT_READ | PROT_WRITE,
271 MAP_PRIVATE | MAP_ANON, -1, 0);
272
273 if (buf == MAP_FAILED)
274 exit_error (id, "mmap");
275 if (mprotect (buf + size, page_size, PROT_NONE))
276 exit_error (id, "mprotect");
277
278 *retbuf = buf;
279 }
280
281 static void
282 alloc_bufs (void)
283 {
284 alloc_buf ("buf1", buf1_size, &buf1);
285 alloc_buf ("buf2", buf2_size, &buf2);
286 }
287
288 static void
289 test_init (void)
290 {
291 # ifdef TEST_NAME
292 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
293 (sizeof func_list
294 / sizeof func_list[0]));
295 # endif
296
297 init_sizes ();
298 alloc_bufs ();
299
300 if (do_srandom)
301 {
302 printf ("Setting seed to 0x%x\n", seed);
303 srandom (seed);
304 }
305 }
306
307 #endif /* TEST_MAIN */