]>
Commit | Line | Data |
---|---|---|
c1f75dc3 | 1 | /* Measure string and memory functions. |
bfff8b1b | 2 | Copyright (C) 2013-2017 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 | ||
22 | typedef struct | |
23 | { | |
24 | const char *name; | |
25 | void (*fn) (void); | |
26 | long test; | |
27 | } impl_t; | |
28 | extern impl_t __start_impls[], __stop_impls[]; | |
29 | ||
30 | #define IMPL(name, test) \ | |
31 | impl_t tst_ ## name \ | |
32 | __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \ | |
33 | = { __STRING (name), (void (*) (void))name, test }; | |
34 | ||
35 | #ifdef TEST_MAIN | |
36 | ||
37 | # ifndef _GNU_SOURCE | |
38 | # define _GNU_SOURCE | |
39 | # endif | |
40 | ||
41 | # undef __USE_STRING_INLINES | |
42 | ||
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 | |
44558701 | 56 | # include "bench-timing.h" |
c1f75dc3 SP |
57 | |
58 | ||
b598e134 | 59 | # define TEST_FUNCTION test_main |
a25322f4 L |
60 | # ifndef TIMEOUT |
61 | # define TIMEOUT (4 * 60) | |
62 | # endif | |
c1f75dc3 SP |
63 | # define OPT_ITERATIONS 10000 |
64 | # define OPT_RANDOM 10001 | |
65 | # define OPT_SEED 10002 | |
66 | ||
44558701 WN |
67 | # define INNER_LOOP_ITERS 64 |
68 | ||
c1f75dc3 SP |
69 | unsigned char *buf1, *buf2; |
70 | int ret, do_srandom; | |
71 | unsigned int seed; | |
72 | size_t page_size; | |
73 | ||
c1f75dc3 SP |
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 | |
87 | ||
88 | # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \ | |
89 | { "random", no_argument, NULL, OPT_RANDOM }, \ | |
90 | { "seed", required_argument, NULL, OPT_SEED }, | |
c1f75dc3 | 91 | |
b598e134 AZ |
92 | static void __attribute__ ((used)) |
93 | cmdline_process_function (int c) | |
94 | { | |
95 | switch (c) | |
96 | { | |
97 | ITERATIONS_PROCESS | |
98 | case OPT_RANDOM: | |
99 | { | |
100 | int fdr = open ("/dev/urandom", O_RDONLY); | |
101 | if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) | |
102 | seed = time (NULL); | |
103 | if (fdr >= 0) | |
104 | close (fdr); | |
105 | do_srandom = 1; | |
106 | break; | |
107 | } | |
108 | ||
109 | case OPT_SEED: | |
110 | seed = strtoul (optarg, NULL, 0); | |
111 | do_srandom = 1; | |
112 | break; | |
113 | } | |
114 | } | |
115 | # define CMDLINE_PROCESS cmdline_process_function | |
c1f75dc3 SP |
116 | # define CALL(impl, ...) \ |
117 | (* (proto_t) (impl)->fn) (__VA_ARGS__) | |
118 | ||
bd5dadac | 119 | # ifdef TEST_NAME |
c1f75dc3 SP |
120 | /* Increase size of FUNC_LIST if assert is triggered at run-time. */ |
121 | static struct libc_ifunc_impl func_list[32]; | |
122 | static int func_count; | |
123 | static int impl_count = -1; | |
124 | static impl_t *impl_array; | |
125 | ||
126 | # define FOR_EACH_IMPL(impl, notall) \ | |
127 | impl_t *impl; \ | |
128 | int count; \ | |
129 | if (impl_count == -1) \ | |
130 | { \ | |
131 | impl_count = 0; \ | |
132 | if (func_count != 0) \ | |
133 | { \ | |
134 | int f; \ | |
135 | impl_t *skip = NULL, *a; \ | |
136 | for (impl = __start_impls; impl < __stop_impls; ++impl) \ | |
137 | if (strcmp (impl->name, TEST_NAME) == 0) \ | |
138 | skip = impl; \ | |
139 | else \ | |
140 | impl_count++; \ | |
141 | a = impl_array = malloc ((impl_count + func_count) * \ | |
142 | sizeof (impl_t)); \ | |
143 | for (impl = __start_impls; impl < __stop_impls; ++impl) \ | |
144 | if (impl != skip) \ | |
145 | *a++ = *impl; \ | |
146 | for (f = 0; f < func_count; f++) \ | |
147 | if (func_list[f].usable) \ | |
148 | { \ | |
149 | a->name = func_list[f].name; \ | |
150 | a->fn = func_list[f].fn; \ | |
151 | a->test = 1; \ | |
152 | a++; \ | |
153 | } \ | |
154 | impl_count = a - impl_array; \ | |
155 | } \ | |
156 | else \ | |
157 | { \ | |
158 | impl_count = __stop_impls - __start_impls; \ | |
159 | impl_array = __start_impls; \ | |
160 | } \ | |
161 | } \ | |
162 | impl = impl_array; \ | |
163 | for (count = 0; count < impl_count; ++count, ++impl) \ | |
164 | if (!notall || impl->test) | |
bd5dadac | 165 | # else /* !TEST_NAME */ |
c1f75dc3 SP |
166 | # define FOR_EACH_IMPL(impl, notall) \ |
167 | for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \ | |
168 | if (!notall || impl->test) | |
bd5dadac | 169 | # endif /* !TEST_NAME */ |
c1f75dc3 | 170 | |
c1f75dc3 SP |
171 | # ifndef BUF1PAGES |
172 | # define BUF1PAGES 1 | |
173 | # endif | |
174 | ||
175 | static void | |
176 | test_init (void) | |
177 | { | |
bd5dadac | 178 | # ifdef TEST_NAME |
c1f75dc3 SP |
179 | func_count = __libc_ifunc_impl_list (TEST_NAME, func_list, |
180 | (sizeof func_list | |
181 | / sizeof func_list[0])); | |
182 | # endif | |
183 | ||
184 | page_size = 2 * getpagesize (); | |
185 | # ifdef MIN_PAGE_SIZE | |
186 | if (page_size < MIN_PAGE_SIZE) | |
187 | page_size = MIN_PAGE_SIZE; | |
188 | # endif | |
189 | buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE, | |
190 | MAP_PRIVATE | MAP_ANON, -1, 0); | |
191 | if (buf1 == MAP_FAILED) | |
192 | error (EXIT_FAILURE, errno, "mmap failed"); | |
193 | if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE)) | |
194 | error (EXIT_FAILURE, errno, "mprotect failed"); | |
195 | buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE, | |
196 | MAP_PRIVATE | MAP_ANON, -1, 0); | |
197 | if (buf2 == MAP_FAILED) | |
198 | error (EXIT_FAILURE, errno, "mmap failed"); | |
199 | if (mprotect (buf2 + page_size, page_size, PROT_NONE)) | |
200 | error (EXIT_FAILURE, errno, "mprotect failed"); | |
c1f75dc3 SP |
201 | if (do_srandom) |
202 | { | |
203 | printf ("Setting seed to 0x%x\n", seed); | |
204 | srandom (seed); | |
205 | } | |
206 | ||
207 | memset (buf1, 0xa5, BUF1PAGES * page_size); | |
208 | memset (buf2, 0x5a, page_size); | |
209 | } | |
210 | ||
211 | #endif /* TEST_MAIN */ |