Update translations.
[glibc.git] / benchtests / bench-string.h
blob4831e210544cd4e9402ab860844cad58c575abce
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/>. */
19 #include <getopt.h>
20 #include <sys/cdefs.h>
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
32 typedef struct
34 const char *name;
35 void (*fn) (void);
36 long test;
37 } impl_t;
38 extern impl_t __start_impls[], __stop_impls[];
40 #define IMPL(name, test) \
41 impl_t tst_ ## name \
42 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
43 = { __STRING (name), (void (*) (void))name, test };
45 #ifdef TEST_MAIN
47 # ifndef _GNU_SOURCE
48 # define _GNU_SOURCE
49 # endif
51 # undef __USE_STRING_INLINES
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"
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 */
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
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
133 int ret, do_srandom;
134 unsigned int seed;
136 # ifndef ITERATIONS
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); \
143 break;
144 # define ITERATIONS iterations
145 # else
146 # define ITERATIONS_OPTIONS
147 # define ITERATIONS_PROCESS
148 # endif
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)
157 switch (c)
159 ITERATIONS_PROCESS
160 case OPT_RANDOM:
162 int fdr = open ("/dev/urandom", O_RDONLY);
163 if (fdr < 0 || read (fdr, &seed, sizeof (seed)) != sizeof (seed))
164 seed = time (NULL);
165 if (fdr >= 0)
166 close (fdr);
167 do_srandom = 1;
168 break;
171 case OPT_SEED:
172 seed = strtoul (optarg, NULL, 0);
173 do_srandom = 1;
174 break;
177 # define CMDLINE_PROCESS cmdline_process_function
178 # define CALL(impl, ...) \
179 (* (proto_t) (impl)->fn) (__VA_ARGS__)
181 # ifdef TEST_NAME
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) \
189 impl_t *impl; \
190 int count; \
191 if (impl_count == -1) \
193 impl_count = 0; \
194 if (func_count != 0) \
196 int f; \
197 impl_t *skip = NULL, *a; \
198 for (impl = __start_impls; impl < __stop_impls; ++impl) \
199 if (strcmp (impl->name, TEST_NAME) == 0) \
200 skip = impl; \
201 else \
202 impl_count++; \
203 a = impl_array = malloc ((impl_count + func_count) * \
204 sizeof (impl_t)); \
205 for (impl = __start_impls; impl < __stop_impls; ++impl) \
206 if (impl != skip) \
207 *a++ = *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; \
213 a->test = 1; \
214 a++; \
216 impl_count = a - impl_array; \
218 else \
220 impl_count = __stop_impls - __start_impls; \
221 impl_array = __start_impls; \
224 impl = impl_array; \
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 */
233 # ifndef BUF1PAGES
234 # define BUF1PAGES 1
235 # endif
237 unsigned char *buf1, *buf2;
238 static size_t buf1_size, buf2_size, page_size;
240 static void
241 init_sizes (void)
243 page_size = 2 * getpagesize ();
244 # ifdef MIN_PAGE_SIZE
245 if (page_size < MIN_PAGE_SIZE)
246 page_size = MIN_PAGE_SIZE;
247 # endif
249 buf1_size = BUF1PAGES * page_size;
250 buf2_size = page_size;
253 static void
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. */
260 static void
261 alloc_buf (const char *id, size_t size, unsigned char **retbuf)
263 size_t alloc_size = size + page_size;
265 if (*retbuf != NULL)
267 int ret = munmap (*retbuf, alloc_size);
268 if (ret != 0)
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");
280 *retbuf = buf;
283 static void
284 alloc_bufs (void)
286 alloc_buf ("buf1", buf1_size, &buf1);
287 alloc_buf ("buf2", buf2_size, &buf2);
290 static void
291 test_init (void)
293 # ifdef TEST_NAME
294 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
295 (sizeof func_list
296 / sizeof func_list[0]));
297 # endif
299 init_sizes ();
300 alloc_bufs ();
302 if (do_srandom)
304 printf ("Setting seed to 0x%x\n", seed);
305 srandom (seed);
309 #endif /* TEST_MAIN */