1 /* Measure bzero functions.
2    Copyright (C) 2022 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    <https://www.gnu.org/licenses/>.  */
18 
19 #define TEST_MAIN
20 #ifdef DO_MEMSET
21 # define TEST_NAME "memset"
22 #else
23 # define TEST_NAME "bzero"
24 #endif
25 #define MIN_PAGE_SIZE 131072
26 #include "bench-string.h"
27 
28 #include "json-lib.h"
29 
30 #ifdef DO_MEMSET
31 void *generic_memset (void *, int, size_t);
32 
33 typedef void *(*proto_t) (void *, int, size_t);
34 
35 IMPL (memset, 1)
36 IMPL (generic_memset, 0)
37 
38 #else
39 static void
40 memset_zero (void * s, size_t len)
41 {
42   memset (s, '\0', len);
43 }
44 
45 typedef void (*proto_t) (void *, size_t);
46 
47 IMPL (bzero, 1)
48 IMPL (memset_zero, 0)
49 #endif
50 
51 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,CHAR * s,size_t n)52 do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, size_t n)
53 {
54   size_t i, iters = INNER_LOOP_ITERS_LARGE;
55   timing_t start, stop, cur;
56 
57   TIMING_NOW (start);
58   for (i = 0; i < iters; ++i)
59     {
60 #ifdef DO_MEMSET
61       CALL (impl, s, 0, n);
62 #else
63       CALL (impl, s, n);
64 #endif
65     }
66   TIMING_NOW (stop);
67 
68   TIMING_DIFF (cur, start, stop);
69 
70   json_element_double (json_ctx, (double) cur / (double) iters);
71 }
72 
73 static void
do_test(json_ctx_t * json_ctx,size_t align,size_t len)74 do_test (json_ctx_t *json_ctx, size_t align, size_t len)
75 {
76   align &= 4095;
77   if ((align + len) * sizeof (CHAR) > page_size)
78     return;
79 
80   json_element_object_begin (json_ctx);
81   json_attr_uint (json_ctx, "length", len);
82   json_attr_uint (json_ctx, "alignment", align);
83   json_array_begin (json_ctx, "timings");
84 
85   FOR_EACH_IMPL (impl, 0)
86     {
87       do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, len);
88     }
89 
90   json_array_end (json_ctx);
91   json_element_object_end (json_ctx);
92 }
93 
94 int
test_main(void)95 test_main (void)
96 {
97   json_ctx_t json_ctx;
98   size_t i;
99 
100   test_init ();
101   alloc_bufs ();
102   json_init (&json_ctx, 0, stdout);
103 
104   json_document_begin (&json_ctx);
105   json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
106 
107   json_attr_object_begin (&json_ctx, "functions");
108   json_attr_object_begin (&json_ctx, TEST_NAME);
109   json_attr_string (&json_ctx, "bench-variant", "default");
110 
111   json_array_begin (&json_ctx, "ifuncs");
112   FOR_EACH_IMPL (impl, 0)
113     json_element_string (&json_ctx, impl->name);
114   json_array_end (&json_ctx);
115 
116   json_array_begin (&json_ctx, "results");
117 
118   for (i = 0; i < 18; ++i)
119     do_test (&json_ctx, 0, 1 << i);
120   for (i = 0; i < 64; ++i)
121     {
122       do_test (&json_ctx, i, i);
123       do_test (&json_ctx, 4096 - i, i);
124       do_test (&json_ctx, 4095, i);
125       if (i & (i - 1))
126 	do_test (&json_ctx, 0, i);
127     }
128   for (i = 32; i < 1024; i+=32)
129     {
130       do_test (&json_ctx, 0, i);
131       do_test (&json_ctx, i, i);
132     }
133   do_test (&json_ctx, 1, 14);
134   do_test (&json_ctx, 3, 1024);
135   do_test (&json_ctx, 4, 64);
136   do_test (&json_ctx, 2, 25);
137 
138   for (i = 33; i <= 256; i += 4)
139     {
140       do_test (&json_ctx, 0, 32 * i);
141       do_test (&json_ctx, i, 32 * i);
142     }
143 
144   json_array_end (&json_ctx);
145   json_attr_object_end (&json_ctx);
146   json_attr_object_end (&json_ctx);
147   json_document_end (&json_ctx);
148 
149   return ret;
150 }
151 
152 #include <support/test-driver.c>
153 
154 #ifdef DO_MEMSET
155 # define libc_hidden_builtin_def(X)
156 # define libc_hidden_def(X)
157 # define libc_hidden_weak(X)
158 # define weak_alias(X,Y)
159 # undef MEMSET
160 # define MEMSET generic_memset
161 # include <string/memset.c>
162 #endif
163