Update copyright notices with scripts/update-copyrights
[glibc.git] / benchtests / bench-string.h
blob26096806f331458033c705f189fef4174d3ad3d3
1 /* Measure string and memory functions.
2 Copyright (C) 2013-2014 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>
20 #define TEST_IFUNC 1
22 typedef struct
24 const char *name;
25 void (*fn) (void);
26 long test;
27 } impl_t;
28 extern impl_t __start_impls[], __stop_impls[];
30 #define IMPL(name, test) \
31 impl_t tst_ ## name \
32 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
33 = { __STRING (name), (void (*) (void))name, test };
35 #ifdef TEST_MAIN
37 # ifndef _GNU_SOURCE
38 # define _GNU_SOURCE
39 # endif
41 # undef __USE_STRING_INLINES
43 # include <stdio.h>
44 # include <stdlib.h>
45 # include <string.h>
46 # include <sys/mman.h>
47 # include <sys/param.h>
48 # include <unistd.h>
49 # include <fcntl.h>
50 # include <error.h>
51 # include <errno.h>
52 # include <time.h>
53 # include <ifunc-impl-list.h>
54 # define GL(x) _##x
55 # define GLRO(x) _##x
56 # include "bench-timing.h"
59 # define TEST_FUNCTION test_main ()
60 # define TIMEOUT (4 * 60)
61 # define OPT_ITERATIONS 10000
62 # define OPT_RANDOM 10001
63 # define OPT_SEED 10002
65 # define INNER_LOOP_ITERS 64
67 unsigned char *buf1, *buf2;
68 int ret, do_srandom;
69 unsigned int seed;
70 size_t page_size;
72 hp_timing_t _dl_hp_timing_overhead;
74 # ifndef ITERATIONS
75 size_t iterations = 100000;
76 # define ITERATIONS_OPTIONS \
77 { "iterations", required_argument, NULL, OPT_ITERATIONS },
78 # define ITERATIONS_PROCESS \
79 case OPT_ITERATIONS: \
80 iterations = strtoul (optarg, NULL, 0); \
81 break;
82 # define ITERATIONS iterations
83 # else
84 # define ITERATIONS_OPTIONS
85 # define ITERATIONS_PROCESS
86 # endif
88 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
89 { "random", no_argument, NULL, OPT_RANDOM }, \
90 { "seed", required_argument, NULL, OPT_SEED },
91 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
92 case OPT_RANDOM: \
93 { \
94 int fdr = open ("/dev/urandom", O_RDONLY); \
96 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
97 seed = time (NULL); \
98 if (fdr >= 0) \
99 close (fdr); \
100 do_srandom = 1; \
101 break; \
104 case OPT_SEED: \
105 seed = strtoul (optarg, NULL, 0); \
106 do_srandom = 1; \
107 break;
109 # define CALL(impl, ...) \
110 (* (proto_t) (impl)->fn) (__VA_ARGS__)
112 # if defined TEST_IFUNC && defined TEST_NAME
113 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
114 static struct libc_ifunc_impl func_list[32];
115 static int func_count;
116 static int impl_count = -1;
117 static impl_t *impl_array;
119 # define FOR_EACH_IMPL(impl, notall) \
120 impl_t *impl; \
121 int count; \
122 if (impl_count == -1) \
124 impl_count = 0; \
125 if (func_count != 0) \
127 int f; \
128 impl_t *skip = NULL, *a; \
129 for (impl = __start_impls; impl < __stop_impls; ++impl) \
130 if (strcmp (impl->name, TEST_NAME) == 0) \
131 skip = impl; \
132 else \
133 impl_count++; \
134 a = impl_array = malloc ((impl_count + func_count) * \
135 sizeof (impl_t)); \
136 for (impl = __start_impls; impl < __stop_impls; ++impl) \
137 if (impl != skip) \
138 *a++ = *impl; \
139 for (f = 0; f < func_count; f++) \
140 if (func_list[f].usable) \
142 a->name = func_list[f].name; \
143 a->fn = func_list[f].fn; \
144 a->test = 1; \
145 a++; \
147 impl_count = a - impl_array; \
149 else \
151 impl_count = __stop_impls - __start_impls; \
152 impl_array = __start_impls; \
155 impl = impl_array; \
156 for (count = 0; count < impl_count; ++count, ++impl) \
157 if (!notall || impl->test)
158 # else /* ! (defined TEST_IFUNC && defined TEST_NAME) */
159 # define FOR_EACH_IMPL(impl, notall) \
160 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
161 if (!notall || impl->test)
162 # endif /* ! (defined TEST_IFUNC && defined TEST_NAME) */
164 # ifndef BUF1PAGES
165 # define BUF1PAGES 1
166 # endif
168 static void
169 test_init (void)
171 # if defined TEST_IFUNC && defined TEST_NAME
172 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
173 (sizeof func_list
174 / sizeof func_list[0]));
175 # endif
177 page_size = 2 * getpagesize ();
178 # ifdef MIN_PAGE_SIZE
179 if (page_size < MIN_PAGE_SIZE)
180 page_size = MIN_PAGE_SIZE;
181 # endif
182 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
183 MAP_PRIVATE | MAP_ANON, -1, 0);
184 if (buf1 == MAP_FAILED)
185 error (EXIT_FAILURE, errno, "mmap failed");
186 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
187 error (EXIT_FAILURE, errno, "mprotect failed");
188 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
189 MAP_PRIVATE | MAP_ANON, -1, 0);
190 if (buf2 == MAP_FAILED)
191 error (EXIT_FAILURE, errno, "mmap failed");
192 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
193 error (EXIT_FAILURE, errno, "mprotect failed");
194 if (do_srandom)
196 printf ("Setting seed to 0x%x\n", seed);
197 srandom (seed);
200 memset (buf1, 0xa5, BUF1PAGES * page_size);
201 memset (buf2, 0x5a, page_size);
204 #endif /* TEST_MAIN */