1 | /* Test and measure string and memory functions.
|
---|
2 | Copyright (C) 1999, 2002, 2004 Free Software Foundation, Inc.
|
---|
3 | This file is part of the GNU C Library.
|
---|
4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with the GNU C Library; if not, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | typedef struct
|
---|
22 | {
|
---|
23 | const char *name;
|
---|
24 | void (*fn) (void);
|
---|
25 | long test;
|
---|
26 | } impl_t;
|
---|
27 |
|
---|
28 | #ifdef __EMX__ /* aout based */
|
---|
29 | #include <stdint.h>
|
---|
30 | extern uintptr_t __impls__;
|
---|
31 | asm (".stabs \"___impls__\", 21, 0, 0, 0xffffffff");
|
---|
32 |
|
---|
33 | #define IMPL(name, test) \
|
---|
34 | impl_t tst_ ## name = { #name, (void (*) (void))name, test }; \
|
---|
35 | asm (".stabs \"___impls__\", 25, 0, 0, _tst_" #name);
|
---|
36 | #else
|
---|
37 | extern impl_t __start_impls[], __stop_impls[];
|
---|
38 | #define IMPL(name, test) \
|
---|
39 | impl_t tst_ ## name \
|
---|
40 | __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
|
---|
41 | = { #name, (void (*) (void))name, test };
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #ifdef TEST_MAIN
|
---|
45 |
|
---|
46 | #ifndef _GNU_SOURCE
|
---|
47 | #define _GNU_SOURCE
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #undef __USE_STRING_INLINES
|
---|
51 |
|
---|
52 | #include <stdio.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <string.h>
|
---|
55 | #include <sys/mman.h>
|
---|
56 | #include <unistd.h>
|
---|
57 | #include <fcntl.h>
|
---|
58 | #include <error.h>
|
---|
59 | #include <errno.h>
|
---|
60 | #include <time.h>
|
---|
61 | #define GL(x) _##x
|
---|
62 | #define GLRO(x) _##x
|
---|
63 | #include <hp-timing.h>
|
---|
64 |
|
---|
65 | /* bird added for bsd */
|
---|
66 | #ifndef TEMP_FAILURE_RETRY /* special GNU idea */
|
---|
67 | # define TEMP_FAILURE_RETRY(expression) \
|
---|
68 | (__extension__ \
|
---|
69 | ({ long int __result; \
|
---|
70 | do __result = (long int) (expression); \
|
---|
71 | while (__result == -1L && errno == EINTR); \
|
---|
72 | __result; }))
|
---|
73 | #endif
|
---|
74 | #ifndef HAVE_STRNLEN
|
---|
75 | static inline size_t strnlen(const char *psz, size_t cch)
|
---|
76 | {
|
---|
77 | const char *psz2 = psz;
|
---|
78 | while (cch > 0 && *psz)
|
---|
79 | cch--, psz++;
|
---|
80 | return psz - psz2;
|
---|
81 | }
|
---|
82 | #endif
|
---|
83 |
|
---|
84 |
|
---|
85 | # define TEST_FUNCTION test_main ()
|
---|
86 | # define TIMEOUT (4 * 60)
|
---|
87 | # define OPT_ITERATIONS 10000
|
---|
88 | # define OPT_RANDOM 10001
|
---|
89 | # define OPT_SEED 10002
|
---|
90 |
|
---|
91 | unsigned char *buf1, *buf2;
|
---|
92 | int ret, do_srandom;
|
---|
93 | unsigned int seed;
|
---|
94 | size_t page_size;
|
---|
95 |
|
---|
96 | hp_timing_t _dl_hp_timing_overhead;
|
---|
97 |
|
---|
98 | # ifndef ITERATIONS
|
---|
99 | size_t iterations = 100000;
|
---|
100 | # define ITERATIONS_OPTIONS \
|
---|
101 | { "iterations", required_argument, NULL, OPT_ITERATIONS },
|
---|
102 | # define ITERATIONS_PROCESS \
|
---|
103 | case OPT_ITERATIONS: \
|
---|
104 | iterations = strtoul (optarg, NULL, 0); \
|
---|
105 | break;
|
---|
106 | # define ITERATIONS iterations
|
---|
107 | # else
|
---|
108 | # define ITERATIONS_OPTIONS
|
---|
109 | # define ITERATIONS_PROCESS
|
---|
110 | # endif
|
---|
111 |
|
---|
112 | # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
|
---|
113 | { "random", no_argument, NULL, OPT_RANDOM }, \
|
---|
114 | { "seed", required_argument, NULL, OPT_SEED },
|
---|
115 | # define CMDLINE_PROCESS ITERATIONS_PROCESS \
|
---|
116 | case OPT_RANDOM: \
|
---|
117 | { \
|
---|
118 | int fdr = open ("/dev/urandom", O_RDONLY); \
|
---|
119 | \
|
---|
120 | if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
|
---|
121 | seed = time (NULL); \
|
---|
122 | if (fdr >= 0) \
|
---|
123 | close (fdr); \
|
---|
124 | do_srandom = 1; \
|
---|
125 | break; \
|
---|
126 | } \
|
---|
127 | \
|
---|
128 | case OPT_SEED: \
|
---|
129 | seed = strtoul (optarg, NULL, 0); \
|
---|
130 | do_srandom = 1; \
|
---|
131 | break;
|
---|
132 |
|
---|
133 | #define CALL(impl, ...) \
|
---|
134 | (* (proto_t) (impl)->fn) (__VA_ARGS__)
|
---|
135 |
|
---|
136 | #ifdef __EMX__
|
---|
137 | #define FOR_EACH_IMPL(impl, notall) \
|
---|
138 | for (uintptr_t *_ptrfirst = __impls__ == -1 ? &__impls__ - 1 : &__impls__; \
|
---|
139 | _ptrfirst; \
|
---|
140 | _ptrfirst = NULL) \
|
---|
141 | for (uintptr_t *_ptr = *_ptrfirst == -2 ? _ptrfirst + 1 : _ptrfirst + 2; \
|
---|
142 | _ptr && *_ptr != NULL; \
|
---|
143 | _ptr = *_ptrfirst == -2 /* emxomf */ || (_ptr - _ptrfirst < *_ptrfirst /* a.out */ ) ? _ptr + 1 : NULL) \
|
---|
144 | for (impl_t *impl = *(impl_t **)_ptr; impl; impl = NULL) \
|
---|
145 | if (!notall || impl->test)
|
---|
146 | #else
|
---|
147 | #define FOR_EACH_IMPL(impl, notall) \
|
---|
148 | for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
|
---|
149 | if (!notall || impl->test)
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | #define HP_TIMING_BEST(best_time, start, end) \
|
---|
153 | do \
|
---|
154 | { \
|
---|
155 | hp_timing_t tmptime; \
|
---|
156 | HP_TIMING_DIFF (tmptime, start + _dl_hp_timing_overhead, end); \
|
---|
157 | if (best_time > tmptime) \
|
---|
158 | best_time = tmptime; \
|
---|
159 | } \
|
---|
160 | while (0)
|
---|
161 |
|
---|
162 | static void
|
---|
163 | test_init (void)
|
---|
164 | {
|
---|
165 | page_size = 2 * getpagesize ();
|
---|
166 | #ifdef MIN_PAGE_SIZE
|
---|
167 | if (page_size < MIN_PAGE_SIZE)
|
---|
168 | page_size = MIN_PAGE_SIZE;
|
---|
169 | #endif
|
---|
170 | #ifdef HAVE_MMAP
|
---|
171 | buf1 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
|
---|
172 | MAP_PRIVATE | MAP_ANON, -1, 0);
|
---|
173 | if (buf1 == MAP_FAILED)
|
---|
174 | error (EXIT_FAILURE, errno, "mmap failed");
|
---|
175 | #else
|
---|
176 | buf1 = memalign (getpagesize (), 2 * page_size);
|
---|
177 | if (!buf1)
|
---|
178 | error (EXIT_FAILURE, errno, "memalign failed");
|
---|
179 | #endif
|
---|
180 | if (mprotect (buf1 + page_size, page_size, PROT_NONE))
|
---|
181 | error (EXIT_FAILURE, errno, "mprotect failed");
|
---|
182 | #ifdef HAVE_MMAP
|
---|
183 | buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
|
---|
184 | MAP_PRIVATE | MAP_ANON, -1, 0);
|
---|
185 | if (buf2 == MAP_FAILED)
|
---|
186 | error (EXIT_FAILURE, errno, "mmap failed");
|
---|
187 | #else
|
---|
188 | buf2 = memalign (getpagesize (), 2 * page_size);
|
---|
189 | if (!buf2)
|
---|
190 | error (EXIT_FAILURE, errno, "memalign failed");
|
---|
191 | #endif
|
---|
192 | if (mprotect (buf2 + page_size, page_size, PROT_NONE))
|
---|
193 | error (EXIT_FAILURE, errno, "mprotect failed");
|
---|
194 | HP_TIMING_DIFF_INIT ();
|
---|
195 | if (do_srandom)
|
---|
196 | {
|
---|
197 | printf ("Setting seed to 0x%x\n", seed);
|
---|
198 | srandom (seed);
|
---|
199 | }
|
---|
200 |
|
---|
201 | memset (buf1, 0xa5, page_size);
|
---|
202 | memset (buf2, 0x5a, page_size);
|
---|
203 | }
|
---|
204 |
|
---|
205 | #endif
|
---|