benchtests: Fix validate_benchout.py exceptions
[platform/upstream/glibc.git] / benchtests / bench-memcpy.c
1 /* Measure memcpy functions.
2    Copyright (C) 2013-2021 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 #ifndef MEMCPY_RESULT
20 # define MEMCPY_RESULT(dst, len) dst
21 # define MIN_PAGE_SIZE 131072
22 # define TEST_MAIN
23 # define TEST_NAME "memcpy"
24 # include "bench-string.h"
25
26 void *generic_memcpy (void *, const void *, size_t);
27
28 IMPL (memcpy, 1)
29 IMPL (generic_memcpy, 0)
30
31 #endif
32
33 # include "json-lib.h"
34
35 typedef void *(*proto_t) (void *, const void *, size_t);
36
37 static void
38 do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, const char *src,
39              size_t len)
40 {
41   size_t i, iters = INNER_LOOP_ITERS;
42   timing_t start, stop, cur;
43
44   TIMING_NOW (start);
45   for (i = 0; i < iters; ++i)
46     {
47       CALL (impl, dst, src, len);
48     }
49   TIMING_NOW (stop);
50
51   TIMING_DIFF (cur, start, stop);
52
53   json_element_double (json_ctx, (double) cur / (double) iters);
54 }
55
56 static void
57 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
58          int both_ways)
59 {
60   size_t i, j;
61   char *s1, *s2;
62   size_t repeats;
63   align1 &= 63;
64   if (align1 + len >= page_size)
65     return;
66
67   align2 &= 63;
68   if (align2 + len >= page_size)
69     return;
70
71   s1 = (char *) (buf1 + align1);
72   s2 = (char *) (buf2 + align2);
73
74   for (repeats = both_ways ? 2 : 1; repeats; --repeats)
75     {
76       for (i = 0, j = 1; i < len; i++, j += 23)
77         s1[i] = j;
78
79       json_element_object_begin (json_ctx);
80       json_attr_uint (json_ctx, "length", (double) len);
81       json_attr_uint (json_ctx, "align1", (double) align1);
82       json_attr_uint (json_ctx, "align2", (double) align2);
83       json_attr_uint (json_ctx, "dst > src", (double) (s2 > s1));
84       json_array_begin (json_ctx, "timings");
85
86       FOR_EACH_IMPL (impl, 0)
87         do_one_test (json_ctx, impl, s2, s1, len);
88
89       json_array_end (json_ctx);
90       json_element_object_end (json_ctx);
91
92       s1 = (char *) (buf2 + align1);
93       s2 = (char *) (buf1 + align2);
94     }
95 }
96
97 int
98 test_main (void)
99 {
100   json_ctx_t json_ctx;
101   size_t i;
102
103   test_init ();
104
105   json_init (&json_ctx, 0, stdout);
106
107   json_document_begin (&json_ctx);
108   json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
109
110   json_attr_object_begin (&json_ctx, "functions");
111   json_attr_object_begin (&json_ctx, TEST_NAME);
112   json_attr_string (&json_ctx, "bench-variant", "default");
113
114   json_array_begin (&json_ctx, "ifuncs");
115   FOR_EACH_IMPL (impl, 0)
116     json_element_string (&json_ctx, impl->name);
117   json_array_end (&json_ctx);
118
119   json_array_begin (&json_ctx, "results");
120   for (i = 0; i < 18; ++i)
121     {
122       do_test (&json_ctx, 0, 0, 1 << i, 1);
123       do_test (&json_ctx, i, 0, 1 << i, 1);
124       do_test (&json_ctx, 0, i, 1 << i, 1);
125       do_test (&json_ctx, i, i, 1 << i, 1);
126     }
127
128   for (i = 0; i < 32; ++i)
129     {
130       do_test (&json_ctx, 0, 0, i, 0);
131       do_test (&json_ctx, i, 0, i, 0);
132       do_test (&json_ctx, 0, i, i, 0);
133       do_test (&json_ctx, i, i, i, 0);
134     }
135
136   for (i = 3; i < 32; ++i)
137     {
138       if ((i & (i - 1)) == 0)
139         continue;
140       do_test (&json_ctx, 0, 0, 16 * i, 1);
141       do_test (&json_ctx, i, 0, 16 * i, 1);
142       do_test (&json_ctx, 0, i, 16 * i, 1);
143       do_test (&json_ctx, i, i, 16 * i, 1);
144     }
145
146   for (i = 32; i < 64; ++i)
147     {
148       do_test (&json_ctx, 0, 0, 32 * i, 1);
149       do_test (&json_ctx, i, 0, 32 * i, 1);
150       do_test (&json_ctx, 0, i, 32 * i, 1);
151       do_test (&json_ctx, i, i, 32 * i, 1);
152     }
153
154   do_test (&json_ctx, 0, 0, getpagesize (), 1);
155
156   for (i = 0; i <= 32; ++i)
157     {
158       do_test (&json_ctx, 0, 0, 2048 + 64 * i, 1);
159       do_test (&json_ctx, i, 0, 2048 + 64 * i, 1);
160       do_test (&json_ctx, 0, i, 2048 + 64 * i, 1);
161       do_test (&json_ctx, i, i, 2048 + 64 * i, 1);
162     }
163
164   json_array_end (&json_ctx);
165   json_attr_object_end (&json_ctx);
166   json_attr_object_end (&json_ctx);
167   json_document_end (&json_ctx);
168
169   return ret;
170 }
171
172 #include <support/test-driver.c>
173
174 #define libc_hidden_builtin_def(X)
175 #undef MEMCPY
176 #define MEMCPY generic_memcpy
177 #include <string/memcpy.c>
178 #include <string/wordcopy.c>