benchtests: Clean up the alloc_bufs
[platform/upstream/glibc.git] / benchtests / bench-string.h
1 /* Measure string and memory functions.
2    Copyright (C) 2013-2018 Free Software Foundation, Inc.
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
19 #include <getopt.h>
20 #include <sys/cdefs.h>
21
22 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
23    for us.  */
24 #include "config.h"
25 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
26 # define inhibit_loop_to_libcall \
27     __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
28 #else
29 # define inhibit_loop_to_libcall
30 #endif
31
32 typedef struct
33 {
34   const char *name;
35   void (*fn) (void);
36   long test;
37 } impl_t;
38 extern impl_t __start_impls[], __stop_impls[];
39
40 #define IMPL(name, test) \
41   impl_t tst_ ## name                                                   \
42   __attribute__ ((section ("impls"), aligned (sizeof (void *))))        \
43        = { __STRING (name), (void (*) (void))name, test };
44
45 #ifdef TEST_MAIN
46
47 # ifndef _GNU_SOURCE
48 #  define _GNU_SOURCE
49 # endif
50
51 # undef __USE_STRING_INLINES
52
53 # include <stdio.h>
54 # include <stdlib.h>
55 # include <string.h>
56 # include <sys/mman.h>
57 # include <sys/param.h>
58 # include <unistd.h>
59 # include <fcntl.h>
60 # include <error.h>
61 # include <errno.h>
62 # include <time.h>
63 # include <ifunc-impl-list.h>
64 # define GL(x) _##x
65 # define GLRO(x) _##x
66 # include "bench-timing.h"
67
68
69 # define TEST_FUNCTION test_main
70 # ifndef TIMEOUT
71 #  define TIMEOUT (4 * 60)
72 # endif
73 # define OPT_ITERATIONS 10000
74 # define OPT_RANDOM 10001
75 # define OPT_SEED 10002
76
77 # define INNER_LOOP_ITERS 64
78
79 int ret, do_srandom;
80 unsigned int seed;
81
82 # ifndef ITERATIONS
83 size_t iterations = 100000;
84 #  define ITERATIONS_OPTIONS \
85      { "iterations", required_argument, NULL, OPT_ITERATIONS },
86 #  define ITERATIONS_PROCESS \
87      case OPT_ITERATIONS:                                                     \
88        iterations = strtoul (optarg, NULL, 0);                                \
89        break;
90 #  define ITERATIONS iterations
91 # else
92 #  define ITERATIONS_OPTIONS
93 #  define ITERATIONS_PROCESS
94 # endif
95
96 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
97     { "random", no_argument, NULL, OPT_RANDOM },                              \
98     { "seed", required_argument, NULL, OPT_SEED },
99
100 static void __attribute__ ((used))
101 cmdline_process_function (int c)
102 {
103   switch (c)
104     {
105       ITERATIONS_PROCESS
106       case OPT_RANDOM:
107         {
108           int fdr = open ("/dev/urandom", O_RDONLY);
109           if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed))
110             seed = time (NULL);
111           if (fdr >= 0)
112             close (fdr);
113           do_srandom = 1;
114           break;
115         }
116
117       case OPT_SEED:
118         seed = strtoul (optarg, NULL, 0);
119         do_srandom = 1;
120       break;
121     }
122 }
123 # define CMDLINE_PROCESS cmdline_process_function
124 # define CALL(impl, ...)        \
125     (* (proto_t) (impl)->fn) (__VA_ARGS__)
126
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;
133
134 #  define FOR_EACH_IMPL(impl, notall) \
135      impl_t *impl;                                                            \
136      int count;                                                               \
137      if (impl_count == -1)                                                    \
138        {                                                                      \
139          impl_count = 0;                                                      \
140          if (func_count != 0)                                                 \
141            {                                                                  \
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)                                       \
156                  {                                                            \
157                    a->name = func_list[f].name;                               \
158                    a->fn = func_list[f].fn;                                   \
159                    a->test = 1;                                               \
160                    a++;                                                       \
161                  }                                                            \
162              impl_count = a - impl_array;                                     \
163            }                                                                  \
164          else                                                                 \
165            {                                                                  \
166              impl_count = __stop_impls - __start_impls;                       \
167              impl_array = __start_impls;                                      \
168            }                                                                  \
169        }                                                                      \
170      impl = impl_array;                                                       \
171      for (count = 0; count < impl_count; ++count, ++impl)                     \
172        if (!notall || impl->test)
173 # else /* !TEST_NAME */
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 /* !TEST_NAME */
178
179 # ifndef BUF1PAGES
180 #  define BUF1PAGES 1
181 # endif
182
183 unsigned char *buf1, *buf2;
184 static size_t buf1_size, buf2_size, page_size;
185
186 static void
187 init_sizes (void)
188 {
189   page_size = 2 * getpagesize ();
190 # ifdef MIN_PAGE_SIZE
191   if (page_size < MIN_PAGE_SIZE)
192     page_size = MIN_PAGE_SIZE;
193 # endif
194
195   buf1_size = BUF1PAGES * page_size;
196   buf2_size = page_size;
197 }
198
199 static void
200 exit_error (const char *id, const char *func)
201 {
202   error (EXIT_FAILURE, errno, "%s: %s failed", id, func);
203 }
204
205 /* Allocate a buffer of size SIZE with a guard page at the end.  */
206 static void
207 alloc_buf (const char *id, size_t size, unsigned char **retbuf)
208 {
209   size_t alloc_size = size + page_size;
210
211   if (*retbuf != NULL)
212     {
213         int ret = munmap (*retbuf, alloc_size);
214         if (ret != 0)
215           exit_error (id, "munmap");
216     }
217
218   unsigned char *buf = mmap (0, alloc_size, PROT_READ | PROT_WRITE,
219                              MAP_PRIVATE | MAP_ANON, -1, 0);
220
221   if (buf == MAP_FAILED)
222     exit_error (id, "mmap");
223   if (mprotect (buf + size, page_size, PROT_NONE))
224     exit_error (id, "mprotect");
225
226   *retbuf = buf;
227 }
228
229 static void
230 alloc_bufs (void)
231 {
232   alloc_buf ("buf1", buf1_size, &buf1);
233   alloc_buf ("buf2", buf2_size, &buf2);
234 }
235
236 static void
237 test_init (void)
238 {
239 # ifdef TEST_NAME
240   func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
241                                        (sizeof func_list
242                                         / sizeof func_list[0]));
243 # endif
244
245   init_sizes ();
246   alloc_bufs ();
247
248   if (do_srandom)
249     {
250       printf ("Setting seed to 0x%x\n", seed);
251       srandom (seed);
252     }
253 }
254
255 #endif /* TEST_MAIN */