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>
28 extern impl_t __start_impls
[], __stop_impls
[];
30 #define IMPL(name, test) \
32 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
33 = { __STRING (name), (void (*) (void))name, test };
41 # undef __USE_STRING_INLINES
46 # include <sys/mman.h>
47 # include <sys/param.h>
53 # include <ifunc-impl-list.h>
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
;
73 size_t iterations
= 100000;
74 # define ITERATIONS_OPTIONS \
75 { "iterations", required_argument, NULL, OPT_ITERATIONS },
76 # define ITERATIONS_PROCESS \
77 case OPT_ITERATIONS: \
78 iterations = strtoul (optarg, NULL, 0); \
80 # define ITERATIONS iterations
82 # define ITERATIONS_OPTIONS
83 # define ITERATIONS_PROCESS
86 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
87 { "random", no_argument, NULL, OPT_RANDOM }, \
88 { "seed", required_argument, NULL, OPT_SEED },
89 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
92 int fdr = open ("/dev/urandom", O_RDONLY); \
94 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
103 seed = strtoul (optarg, NULL, 0); \
107 # define CALL(impl, ...) \
108 (* (proto_t) (impl)->fn) (__VA_ARGS__)
110 # if defined TEST_IFUNC && defined TEST_NAME
111 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
112 static struct libc_ifunc_impl func_list
[32];
113 static int func_count
;
114 static int impl_count
= -1;
115 static impl_t
*impl_array
;
117 # define FOR_EACH_IMPL(impl, notall) \
120 if (impl_count == -1) \
123 if (func_count != 0) \
126 impl_t *skip = NULL, *a; \
127 for (impl = __start_impls; impl < __stop_impls; ++impl) \
128 if (strcmp (impl->name, TEST_NAME) == 0) \
132 a = impl_array = malloc ((impl_count + func_count) * \
134 for (impl = __start_impls; impl < __stop_impls; ++impl) \
137 for (f = 0; f < func_count; f++) \
138 if (func_list[f].usable) \
140 a->name = func_list[f].name; \
141 a->fn = func_list[f].fn; \
145 impl_count = a - impl_array; \
149 impl_count = __stop_impls - __start_impls; \
150 impl_array = __start_impls; \
154 for (count = 0; count < impl_count; ++count, ++impl) \
155 if (!notall || impl->test)
156 # else /* ! (defined TEST_IFUNC && defined TEST_NAME) */
157 # define FOR_EACH_IMPL(impl, notall) \
158 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
159 if (!notall || impl->test)
160 # endif /* ! (defined TEST_IFUNC && defined TEST_NAME) */
169 # if defined TEST_IFUNC && defined TEST_NAME
170 func_count
= __libc_ifunc_impl_list (TEST_NAME
, func_list
,
172 / sizeof func_list
[0]));
175 page_size
= 2 * getpagesize ();
176 # ifdef MIN_PAGE_SIZE
177 if (page_size
< MIN_PAGE_SIZE
)
178 page_size
= MIN_PAGE_SIZE
;
180 buf1
= mmap (0, (BUF1PAGES
+ 1) * page_size
, PROT_READ
| PROT_WRITE
,
181 MAP_PRIVATE
| MAP_ANON
, -1, 0);
182 if (buf1
== MAP_FAILED
)
183 error (EXIT_FAILURE
, errno
, "mmap failed");
184 if (mprotect (buf1
+ BUF1PAGES
* page_size
, page_size
, PROT_NONE
))
185 error (EXIT_FAILURE
, errno
, "mprotect failed");
186 buf2
= mmap (0, 2 * page_size
, PROT_READ
| PROT_WRITE
,
187 MAP_PRIVATE
| MAP_ANON
, -1, 0);
188 if (buf2
== MAP_FAILED
)
189 error (EXIT_FAILURE
, errno
, "mmap failed");
190 if (mprotect (buf2
+ page_size
, page_size
, PROT_NONE
))
191 error (EXIT_FAILURE
, errno
, "mprotect failed");
194 printf ("Setting seed to 0x%x\n", seed
);
198 memset (buf1
, 0xa5, BUF1PAGES
* page_size
);
199 memset (buf2
, 0x5a, page_size
);
202 #endif /* TEST_MAIN */