nptl: Consolidate sem_init implementations
[glibc.git] / benchtests / bench-string.h
blob9c5371ec77c4225b6d3ee1b2db9b8252dcf70efe
1 /* Measure string and memory functions.
2 Copyright (C) 2013-2016 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 <sys/cdefs.h>
21 typedef struct
23 const char *name;
24 void (*fn) (void);
25 long test;
26 } impl_t;
27 extern impl_t __start_impls[], __stop_impls[];
29 #define IMPL(name, test) \
30 impl_t tst_ ## name \
31 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
32 = { __STRING (name), (void (*) (void))name, test };
34 #ifdef TEST_MAIN
36 # ifndef _GNU_SOURCE
37 # define _GNU_SOURCE
38 # endif
40 # undef __USE_STRING_INLINES
42 # include <stdio.h>
43 # include <stdlib.h>
44 # include <string.h>
45 # include <sys/mman.h>
46 # include <sys/param.h>
47 # include <unistd.h>
48 # include <fcntl.h>
49 # include <error.h>
50 # include <errno.h>
51 # include <time.h>
52 # include <ifunc-impl-list.h>
53 # define GL(x) _##x
54 # define GLRO(x) _##x
55 # include "bench-timing.h"
58 # define TEST_FUNCTION test_main ()
59 # ifndef TIMEOUT
60 # define TIMEOUT (4 * 60)
61 # endif
62 # define OPT_ITERATIONS 10000
63 # define OPT_RANDOM 10001
64 # define OPT_SEED 10002
66 # define INNER_LOOP_ITERS 64
68 unsigned char *buf1, *buf2;
69 int ret, do_srandom;
70 unsigned int seed;
71 size_t page_size;
73 # ifndef ITERATIONS
74 size_t iterations = 100000;
75 # define ITERATIONS_OPTIONS \
76 { "iterations", required_argument, NULL, OPT_ITERATIONS },
77 # define ITERATIONS_PROCESS \
78 case OPT_ITERATIONS: \
79 iterations = strtoul (optarg, NULL, 0); \
80 break;
81 # define ITERATIONS iterations
82 # else
83 # define ITERATIONS_OPTIONS
84 # define ITERATIONS_PROCESS
85 # endif
87 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
88 { "random", no_argument, NULL, OPT_RANDOM }, \
89 { "seed", required_argument, NULL, OPT_SEED },
90 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
91 case OPT_RANDOM: \
92 { \
93 int fdr = open ("/dev/urandom", O_RDONLY); \
95 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
96 seed = time (NULL); \
97 if (fdr >= 0) \
98 close (fdr); \
99 do_srandom = 1; \
100 break; \
103 case OPT_SEED: \
104 seed = strtoul (optarg, NULL, 0); \
105 do_srandom = 1; \
106 break;
108 # define CALL(impl, ...) \
109 (* (proto_t) (impl)->fn) (__VA_ARGS__)
111 # ifdef TEST_NAME
112 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
113 static struct libc_ifunc_impl func_list[32];
114 static int func_count;
115 static int impl_count = -1;
116 static impl_t *impl_array;
118 # define FOR_EACH_IMPL(impl, notall) \
119 impl_t *impl; \
120 int count; \
121 if (impl_count == -1) \
123 impl_count = 0; \
124 if (func_count != 0) \
126 int f; \
127 impl_t *skip = NULL, *a; \
128 for (impl = __start_impls; impl < __stop_impls; ++impl) \
129 if (strcmp (impl->name, TEST_NAME) == 0) \
130 skip = impl; \
131 else \
132 impl_count++; \
133 a = impl_array = malloc ((impl_count + func_count) * \
134 sizeof (impl_t)); \
135 for (impl = __start_impls; impl < __stop_impls; ++impl) \
136 if (impl != skip) \
137 *a++ = *impl; \
138 for (f = 0; f < func_count; f++) \
139 if (func_list[f].usable) \
141 a->name = func_list[f].name; \
142 a->fn = func_list[f].fn; \
143 a->test = 1; \
144 a++; \
146 impl_count = a - impl_array; \
148 else \
150 impl_count = __stop_impls - __start_impls; \
151 impl_array = __start_impls; \
154 impl = impl_array; \
155 for (count = 0; count < impl_count; ++count, ++impl) \
156 if (!notall || impl->test)
157 # else /* !TEST_NAME */
158 # define FOR_EACH_IMPL(impl, notall) \
159 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
160 if (!notall || impl->test)
161 # endif /* !TEST_NAME */
163 # ifndef BUF1PAGES
164 # define BUF1PAGES 1
165 # endif
167 static void
168 test_init (void)
170 # ifdef TEST_NAME
171 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
172 (sizeof func_list
173 / sizeof func_list[0]));
174 # endif
176 page_size = 2 * getpagesize ();
177 # ifdef MIN_PAGE_SIZE
178 if (page_size < MIN_PAGE_SIZE)
179 page_size = MIN_PAGE_SIZE;
180 # endif
181 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
182 MAP_PRIVATE | MAP_ANON, -1, 0);
183 if (buf1 == MAP_FAILED)
184 error (EXIT_FAILURE, errno, "mmap failed");
185 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
186 error (EXIT_FAILURE, errno, "mprotect failed");
187 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
188 MAP_PRIVATE | MAP_ANON, -1, 0);
189 if (buf2 == MAP_FAILED)
190 error (EXIT_FAILURE, errno, "mmap failed");
191 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
192 error (EXIT_FAILURE, errno, "mprotect failed");
193 if (do_srandom)
195 printf ("Setting seed to 0x%x\n", seed);
196 srandom (seed);
199 memset (buf1, 0xa5, BUF1PAGES * page_size);
200 memset (buf2, 0x5a, page_size);
203 #endif /* TEST_MAIN */