]>
Commit | Line | Data |
---|---|---|
c1f75dc3 | 1 | /* Measure string and memory functions. |
04277e02 | 2 | Copyright (C) 2013-2019 Free Software Foundation, Inc. |
c1f75dc3 SP |
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 | ||
b598e134 | 19 | #include <getopt.h> |
c1f75dc3 SP |
20 | #include <sys/cdefs.h> |
21 | ||
f1c8185d WD |
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 | ||
c1f75dc3 SP |
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 | |
44558701 | 66 | # include "bench-timing.h" |
c1f75dc3 | 67 | |
90d3320d WD |
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 */ | |
c1f75dc3 | 118 | |
b598e134 | 119 | # define TEST_FUNCTION test_main |
a25322f4 L |
120 | # ifndef TIMEOUT |
121 | # define TIMEOUT (4 * 60) | |
122 | # endif | |
c1f75dc3 SP |
123 | # define OPT_ITERATIONS 10000 |
124 | # define OPT_RANDOM 10001 | |
125 | # define OPT_SEED 10002 | |
126 | ||
44558701 WN |
127 | # define INNER_LOOP_ITERS 64 |
128 | ||
c1f75dc3 SP |
129 | int ret, do_srandom; |
130 | unsigned int seed; | |
c1f75dc3 | 131 | |
c1f75dc3 SP |
132 | # ifndef ITERATIONS |
133 | size_t iterations = 100000; | |
134 | # define ITERATIONS_OPTIONS \ | |
135 | { "iterations", required_argument, NULL, OPT_ITERATIONS }, | |
136 | # define ITERATIONS_PROCESS \ | |
137 | case OPT_ITERATIONS: \ | |
138 | iterations = strtoul (optarg, NULL, 0); \ | |
139 | break; | |
140 | # define ITERATIONS iterations | |
141 | # else | |
142 | # define ITERATIONS_OPTIONS | |
143 | # define ITERATIONS_PROCESS | |
144 | # endif | |
145 | ||
146 | # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \ | |
147 | { "random", no_argument, NULL, OPT_RANDOM }, \ | |
148 | { "seed", required_argument, NULL, OPT_SEED }, | |
c1f75dc3 | 149 | |
b598e134 AZ |
150 | static void __attribute__ ((used)) |
151 | cmdline_process_function (int c) | |
152 | { | |
153 | switch (c) | |
154 | { | |
155 | ITERATIONS_PROCESS | |
156 | case OPT_RANDOM: | |
157 | { | |
158 | int fdr = open ("/dev/urandom", O_RDONLY); | |
c4f50205 | 159 | if (fdr < 0 || read (fdr, &seed, sizeof (seed)) != sizeof (seed)) |
b598e134 AZ |
160 | seed = time (NULL); |
161 | if (fdr >= 0) | |
162 | close (fdr); | |
163 | do_srandom = 1; | |
164 | break; | |
165 | } | |
166 | ||
167 | case OPT_SEED: | |
168 | seed = strtoul (optarg, NULL, 0); | |
169 | do_srandom = 1; | |
170 | break; | |
171 | } | |
172 | } | |
173 | # define CMDLINE_PROCESS cmdline_process_function | |
c1f75dc3 SP |
174 | # define CALL(impl, ...) \ |
175 | (* (proto_t) (impl)->fn) (__VA_ARGS__) | |
176 | ||
bd5dadac | 177 | # ifdef TEST_NAME |
c1f75dc3 SP |
178 | /* Increase size of FUNC_LIST if assert is triggered at run-time. */ |
179 | static struct libc_ifunc_impl func_list[32]; | |
180 | static int func_count; | |
181 | static int impl_count = -1; | |
182 | static impl_t *impl_array; | |
183 | ||
184 | # define FOR_EACH_IMPL(impl, notall) \ | |
185 | impl_t *impl; \ | |
186 | int count; \ | |
187 | if (impl_count == -1) \ | |
188 | { \ | |
189 | impl_count = 0; \ | |
190 | if (func_count != 0) \ | |
191 | { \ | |
192 | int f; \ | |
193 | impl_t *skip = NULL, *a; \ | |
194 | for (impl = __start_impls; impl < __stop_impls; ++impl) \ | |
195 | if (strcmp (impl->name, TEST_NAME) == 0) \ | |
196 | skip = impl; \ | |
197 | else \ | |
198 | impl_count++; \ | |
199 | a = impl_array = malloc ((impl_count + func_count) * \ | |
200 | sizeof (impl_t)); \ | |
201 | for (impl = __start_impls; impl < __stop_impls; ++impl) \ | |
202 | if (impl != skip) \ | |
203 | *a++ = *impl; \ | |
204 | for (f = 0; f < func_count; f++) \ | |
205 | if (func_list[f].usable) \ | |
206 | { \ | |
207 | a->name = func_list[f].name; \ | |
208 | a->fn = func_list[f].fn; \ | |
209 | a->test = 1; \ | |
210 | a++; \ | |
211 | } \ | |
212 | impl_count = a - impl_array; \ | |
213 | } \ | |
214 | else \ | |
215 | { \ | |
216 | impl_count = __stop_impls - __start_impls; \ | |
217 | impl_array = __start_impls; \ | |
218 | } \ | |
219 | } \ | |
220 | impl = impl_array; \ | |
221 | for (count = 0; count < impl_count; ++count, ++impl) \ | |
222 | if (!notall || impl->test) | |
bd5dadac | 223 | # else /* !TEST_NAME */ |
c1f75dc3 SP |
224 | # define FOR_EACH_IMPL(impl, notall) \ |
225 | for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \ | |
226 | if (!notall || impl->test) | |
bd5dadac | 227 | # endif /* !TEST_NAME */ |
c1f75dc3 | 228 | |
c1f75dc3 SP |
229 | # ifndef BUF1PAGES |
230 | # define BUF1PAGES 1 | |
231 | # endif | |
232 | ||
014efdd7 SP |
233 | unsigned char *buf1, *buf2; |
234 | static size_t buf1_size, buf2_size, page_size; | |
235 | ||
c1f75dc3 | 236 | static void |
014efdd7 | 237 | init_sizes (void) |
c1f75dc3 | 238 | { |
c1f75dc3 SP |
239 | page_size = 2 * getpagesize (); |
240 | # ifdef MIN_PAGE_SIZE | |
241 | if (page_size < MIN_PAGE_SIZE) | |
242 | page_size = MIN_PAGE_SIZE; | |
243 | # endif | |
014efdd7 SP |
244 | |
245 | buf1_size = BUF1PAGES * page_size; | |
246 | buf2_size = page_size; | |
247 | } | |
248 | ||
249 | static void | |
250 | exit_error (const char *id, const char *func) | |
251 | { | |
252 | error (EXIT_FAILURE, errno, "%s: %s failed", id, func); | |
503c92c3 SP |
253 | } |
254 | ||
014efdd7 | 255 | /* Allocate a buffer of size SIZE with a guard page at the end. */ |
503c92c3 | 256 | static void |
014efdd7 | 257 | alloc_buf (const char *id, size_t size, unsigned char **retbuf) |
503c92c3 | 258 | { |
014efdd7 | 259 | size_t alloc_size = size + page_size; |
503c92c3 | 260 | |
014efdd7 SP |
261 | if (*retbuf != NULL) |
262 | { | |
263 | int ret = munmap (*retbuf, alloc_size); | |
264 | if (ret != 0) | |
265 | exit_error (id, "munmap"); | |
266 | } | |
503c92c3 | 267 | |
014efdd7 SP |
268 | unsigned char *buf = mmap (0, alloc_size, PROT_READ | PROT_WRITE, |
269 | MAP_PRIVATE | MAP_ANON, -1, 0); | |
503c92c3 | 270 | |
014efdd7 SP |
271 | if (buf == MAP_FAILED) |
272 | exit_error (id, "mmap"); | |
273 | if (mprotect (buf + size, page_size, PROT_NONE)) | |
274 | exit_error (id, "mprotect"); | |
503c92c3 | 275 | |
014efdd7 SP |
276 | *retbuf = buf; |
277 | } | |
503c92c3 | 278 | |
014efdd7 SP |
279 | static void |
280 | alloc_bufs (void) | |
281 | { | |
282 | alloc_buf ("buf1", buf1_size, &buf1); | |
283 | alloc_buf ("buf2", buf2_size, &buf2); | |
503c92c3 SP |
284 | } |
285 | ||
286 | static void | |
287 | test_init (void) | |
288 | { | |
289 | # ifdef TEST_NAME | |
290 | func_count = __libc_ifunc_impl_list (TEST_NAME, func_list, | |
291 | (sizeof func_list | |
292 | / sizeof func_list[0])); | |
293 | # endif | |
294 | ||
014efdd7 | 295 | init_sizes (); |
503c92c3 SP |
296 | alloc_bufs (); |
297 | ||
c1f75dc3 SP |
298 | if (do_srandom) |
299 | { | |
300 | printf ("Setting seed to 0x%x\n", seed); | |
301 | srandom (seed); | |
302 | } | |
c1f75dc3 SP |
303 | } |
304 | ||
305 | #endif /* TEST_MAIN */ |