]>
Commit | Line | Data |
---|---|---|
58ef9ef7 | 1 | /* Test and measure string and memory functions. |
16292edd | 2 | Copyright (C) 1999, 2002, 2004, 2008, 2011 Free Software Foundation, Inc. |
58ef9ef7 RM |
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 | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
58ef9ef7 | 19 | |
16292edd UD |
20 | #include <sys/cdefs.h> |
21 | ||
58ef9ef7 RM |
22 | typedef struct |
23 | { | |
24 | const char *name; | |
25 | void (*fn) (void); | |
e8c1660f | 26 | long test; |
58ef9ef7 RM |
27 | } impl_t; |
28 | extern impl_t __start_impls[], __stop_impls[]; | |
29 | ||
30 | #define IMPL(name, test) \ | |
e8c1660f RM |
31 | impl_t tst_ ## name \ |
32 | __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \ | |
16292edd | 33 | = { __STRING (name), (void (*) (void))name, test }; |
58ef9ef7 RM |
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 <unistd.h> | |
48 | #include <fcntl.h> | |
49 | #include <error.h> | |
50 | #include <errno.h> | |
51 | #include <time.h> | |
52 | #define GL(x) _##x | |
9a850087 | 53 | #define GLRO(x) _##x |
58ef9ef7 RM |
54 | #include <hp-timing.h> |
55 | ||
56 | ||
57 | # define TEST_FUNCTION test_main () | |
58 | # define TIMEOUT (4 * 60) | |
59 | # define OPT_ITERATIONS 10000 | |
60 | # define OPT_RANDOM 10001 | |
61 | # define OPT_SEED 10002 | |
62 | ||
63 | unsigned char *buf1, *buf2; | |
64 | int ret, do_srandom; | |
65 | unsigned int seed; | |
66 | size_t page_size; | |
67 | ||
68 | hp_timing_t _dl_hp_timing_overhead; | |
69 | ||
70 | # ifndef ITERATIONS | |
71 | size_t iterations = 100000; | |
72 | # define ITERATIONS_OPTIONS \ | |
73 | { "iterations", required_argument, NULL, OPT_ITERATIONS }, | |
74 | # define ITERATIONS_PROCESS \ | |
75 | case OPT_ITERATIONS: \ | |
76 | iterations = strtoul (optarg, NULL, 0); \ | |
77 | break; | |
78 | # define ITERATIONS iterations | |
79 | # else | |
80 | # define ITERATIONS_OPTIONS | |
81 | # define ITERATIONS_PROCESS | |
82 | # endif | |
83 | ||
84 | # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \ | |
85 | { "random", no_argument, NULL, OPT_RANDOM }, \ | |
86 | { "seed", required_argument, NULL, OPT_SEED }, | |
87 | # define CMDLINE_PROCESS ITERATIONS_PROCESS \ | |
88 | case OPT_RANDOM: \ | |
89 | { \ | |
90 | int fdr = open ("/dev/urandom", O_RDONLY); \ | |
91 | \ | |
92 | if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \ | |
93 | seed = time (NULL); \ | |
94 | if (fdr >= 0) \ | |
95 | close (fdr); \ | |
96 | do_srandom = 1; \ | |
97 | break; \ | |
98 | } \ | |
99 | \ | |
100 | case OPT_SEED: \ | |
101 | seed = strtoul (optarg, NULL, 0); \ | |
102 | do_srandom = 1; \ | |
103 | break; | |
104 | ||
105 | #define CALL(impl, ...) \ | |
106 | (* (proto_t) (impl)->fn) (__VA_ARGS__) | |
107 | ||
108 | #define FOR_EACH_IMPL(impl, notall) \ | |
109 | for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \ | |
110 | if (!notall || impl->test) | |
111 | ||
112 | #define HP_TIMING_BEST(best_time, start, end) \ | |
113 | do \ | |
114 | { \ | |
115 | hp_timing_t tmptime; \ | |
116 | HP_TIMING_DIFF (tmptime, start + _dl_hp_timing_overhead, end); \ | |
117 | if (best_time > tmptime) \ | |
118 | best_time = tmptime; \ | |
119 | } \ | |
120 | while (0) | |
121 | ||
f63abf58 UD |
122 | #ifndef BUF1PAGES |
123 | # define BUF1PAGES 1 | |
124 | #endif | |
125 | ||
58ef9ef7 RM |
126 | static void |
127 | test_init (void) | |
128 | { | |
129 | page_size = 2 * getpagesize (); | |
130 | #ifdef MIN_PAGE_SIZE | |
131 | if (page_size < MIN_PAGE_SIZE) | |
132 | page_size = MIN_PAGE_SIZE; | |
133 | #endif | |
f63abf58 | 134 | buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE, |
58ef9ef7 RM |
135 | MAP_PRIVATE | MAP_ANON, -1, 0); |
136 | if (buf1 == MAP_FAILED) | |
137 | error (EXIT_FAILURE, errno, "mmap failed"); | |
f63abf58 | 138 | if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE)) |
58ef9ef7 RM |
139 | error (EXIT_FAILURE, errno, "mprotect failed"); |
140 | buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE, | |
141 | MAP_PRIVATE | MAP_ANON, -1, 0); | |
142 | if (buf2 == MAP_FAILED) | |
143 | error (EXIT_FAILURE, errno, "mmap failed"); | |
144 | if (mprotect (buf2 + page_size, page_size, PROT_NONE)) | |
145 | error (EXIT_FAILURE, errno, "mprotect failed"); | |
146 | HP_TIMING_DIFF_INIT (); | |
147 | if (do_srandom) | |
148 | { | |
149 | printf ("Setting seed to 0x%x\n", seed); | |
150 | srandom (seed); | |
151 | } | |
9372c958 | 152 | |
f63abf58 | 153 | memset (buf1, 0xa5, BUF1PAGES * page_size); |
9372c958 | 154 | memset (buf2, 0x5a, page_size); |
58ef9ef7 RM |
155 | } |
156 | ||
157 | #endif |