Remove TIMING_INIT
[glibc.git] / string / test-string.h
blobbefd3e993155d4c787f6e091f68e7f66d54e2d51
1 /* Test and measure string and memory functions.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <sys/cdefs.h>
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 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
44 for us. */
45 #include "config.h"
46 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
47 # define inhibit_loop_to_libcall \
48 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
49 #else
50 # define inhibit_loop_to_libcall
51 #endif
53 #include <getopt.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sys/mman.h>
59 #include <sys/param.h>
60 #include <unistd.h>
61 #include <fcntl.h>
62 #include <error.h>
63 #include <errno.h>
64 #include <time.h>
65 #include <ifunc-impl-list.h>
66 #define GL(x) _##x
67 #define GLRO(x) _##x
70 # define TEST_FUNCTION test_main
71 # define TIMEOUT (4 * 60)
72 # define OPT_ITERATIONS 10000
73 # define OPT_RANDOM 10001
74 # define OPT_SEED 10002
76 unsigned char *buf1, *buf2;
77 int ret, do_srandom;
78 unsigned int seed;
79 size_t page_size;
81 # ifndef ITERATIONS
82 size_t iterations = 100000;
83 # define ITERATIONS_OPTIONS \
84 { "iterations", required_argument, NULL, OPT_ITERATIONS },
85 # define ITERATIONS_PROCESS \
86 case OPT_ITERATIONS: \
87 iterations = strtoul (optarg, NULL, 0); \
88 break;
89 # define ITERATIONS iterations
90 # else
91 # define ITERATIONS_OPTIONS
92 # define ITERATIONS_PROCESS
93 # endif
95 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
96 { "random", no_argument, NULL, OPT_RANDOM }, \
97 { "seed", required_argument, NULL, OPT_SEED },
99 static void __attribute__ ((used))
100 cmdline_process_function (int c)
102 switch (c)
104 ITERATIONS_PROCESS
105 case OPT_RANDOM:
107 int fdr = open ("/dev/urandom", O_RDONLY);
108 if (fdr < 0 || read (fdr, &seed, sizeof (seed)) != sizeof (seed))
109 seed = time (NULL);
110 if (fdr >= 0)
111 close (fdr);
112 do_srandom = 1;
113 break;
116 case OPT_SEED:
117 seed = strtoul (optarg, NULL, 0);
118 do_srandom = 1;
119 break;
122 # define CMDLINE_PROCESS cmdline_process_function
124 #define CALL(impl, ...) \
125 (* (proto_t) (impl)->fn) (__VA_ARGS__)
127 #ifdef TEST_NAME
128 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
129 static struct libc_ifunc_impl func_list[32];
130 static int func_count;
131 static int impl_count = -1;
132 static impl_t *impl_array;
134 # define FOR_EACH_IMPL(impl, notall) \
135 impl_t *impl; \
136 int count; \
137 if (impl_count == -1) \
139 impl_count = 0; \
140 if (func_count != 0) \
142 int f; \
143 impl_t *skip = NULL, *a; \
144 for (impl = __start_impls; impl < __stop_impls; ++impl) \
145 if (strcmp (impl->name, TEST_NAME) == 0) \
146 skip = impl; \
147 else \
148 impl_count++; \
149 a = impl_array = malloc ((impl_count + func_count) * \
150 sizeof (impl_t)); \
151 for (impl = __start_impls; impl < __stop_impls; ++impl) \
152 if (impl != skip) \
153 *a++ = *impl; \
154 for (f = 0; f < func_count; f++) \
155 if (func_list[f].usable) \
157 a->name = func_list[f].name; \
158 a->fn = func_list[f].fn; \
159 a->test = 1; \
160 a++; \
162 impl_count = a - impl_array; \
164 else \
166 impl_count = __stop_impls - __start_impls; \
167 impl_array = __start_impls; \
170 impl = impl_array; \
171 for (count = 0; count < impl_count; ++count, ++impl) \
172 if (!notall || impl->test)
173 #else
174 # define FOR_EACH_IMPL(impl, notall) \
175 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
176 if (!notall || impl->test)
177 #endif
179 #ifndef BUF1PAGES
180 # define BUF1PAGES 1
181 #endif
183 static void
184 test_init (void)
186 #ifdef TEST_NAME
187 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
188 (sizeof func_list
189 / sizeof func_list[0]));
190 #endif
192 page_size = 2 * getpagesize ();
193 #ifdef MIN_PAGE_SIZE
194 if (page_size < MIN_PAGE_SIZE)
195 page_size = MIN_PAGE_SIZE;
196 #endif
197 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
198 MAP_PRIVATE | MAP_ANON, -1, 0);
199 if (buf1 == MAP_FAILED)
200 error (EXIT_FAILURE, errno, "mmap failed");
201 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
202 error (EXIT_FAILURE, errno, "mprotect failed");
203 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
204 MAP_PRIVATE | MAP_ANON, -1, 0);
205 if (buf2 == MAP_FAILED)
206 error (EXIT_FAILURE, errno, "mmap failed");
207 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
208 error (EXIT_FAILURE, errno, "mprotect failed");
209 if (do_srandom)
211 printf ("Setting seed to 0x%x\n", seed);
212 srandom (seed);
215 memset (buf1, 0xa5, BUF1PAGES * page_size);
216 memset (buf2, 0x5a, page_size);
219 #endif