1 | /* Measure memcpy functions. |
2 | Copyright (C) 2013-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 | #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 | for (i = 0; i < iters / 64; ++i) |
44 | { |
45 | CALL (impl, dst, src, len); |
46 | } |
47 | TIMING_NOW (start); |
48 | for (i = 0; i < iters; ++i) |
49 | { |
50 | CALL (impl, dst, src, len); |
51 | } |
52 | TIMING_NOW (stop); |
53 | |
54 | TIMING_DIFF (cur, start, stop); |
55 | |
56 | json_element_double (ctx: json_ctx, d: (double) cur / (double) iters); |
57 | } |
58 | |
59 | static void |
60 | do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, |
61 | int both_ways) |
62 | { |
63 | size_t i, j; |
64 | char *s1, *s2; |
65 | size_t repeats; |
66 | align1 &= (getpagesize () - 1); |
67 | if (align1 + len >= page_size) |
68 | return; |
69 | |
70 | align2 &= (getpagesize () - 1); |
71 | if (align2 + len >= page_size) |
72 | return; |
73 | |
74 | s1 = (char *) (buf1 + align1); |
75 | s2 = (char *) (buf2 + align2); |
76 | |
77 | for (repeats = both_ways ? 2 : 1; repeats; --repeats) |
78 | { |
79 | for (i = 0, j = 1; i < len; i++, j += 23) |
80 | s1[i] = j; |
81 | |
82 | json_element_object_begin (ctx: json_ctx); |
83 | json_attr_uint (ctx: json_ctx, name: "length" , d: (double) len); |
84 | json_attr_uint (ctx: json_ctx, name: "align1" , d: (double) align1); |
85 | json_attr_uint (ctx: json_ctx, name: "align2" , d: (double) align2); |
86 | json_attr_uint (ctx: json_ctx, name: "dst > src" , d: (double) (s2 > s1)); |
87 | json_array_begin (ctx: json_ctx, name: "timings" ); |
88 | |
89 | FOR_EACH_IMPL (impl, 0) |
90 | do_one_test (json_ctx, impl, dst: s2, src: s1, len); |
91 | |
92 | json_array_end (ctx: json_ctx); |
93 | json_element_object_end (ctx: json_ctx); |
94 | |
95 | s1 = (char *) (buf2 + align1); |
96 | s2 = (char *) (buf1 + align2); |
97 | } |
98 | } |
99 | |
100 | int |
101 | test_main (void) |
102 | { |
103 | json_ctx_t json_ctx; |
104 | size_t i; |
105 | size_t half_page = getpagesize () / 2; |
106 | test_init (); |
107 | |
108 | json_init (ctx: &json_ctx, indent_level: 0, stdout); |
109 | |
110 | json_document_begin (ctx: &json_ctx); |
111 | json_attr_string (ctx: &json_ctx, name: "timing_type" , TIMING_TYPE); |
112 | |
113 | json_attr_object_begin (ctx: &json_ctx, name: "functions" ); |
114 | json_attr_object_begin (ctx: &json_ctx, TEST_NAME); |
115 | json_attr_string (ctx: &json_ctx, name: "bench-variant" , s: "default" ); |
116 | |
117 | json_array_begin (ctx: &json_ctx, name: "ifuncs" ); |
118 | FOR_EACH_IMPL (impl, 0) |
119 | json_element_string (ctx: &json_ctx, s: impl->name); |
120 | json_array_end (ctx: &json_ctx); |
121 | |
122 | json_array_begin (ctx: &json_ctx, name: "results" ); |
123 | for (i = 0; i < 18; ++i) |
124 | { |
125 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 1 << i, both_ways: 1); |
126 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, len: 1 << i, both_ways: 1); |
127 | do_test (json_ctx: &json_ctx, align1: i + 32, align2: 0, len: 1 << i, both_ways: 1); |
128 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: 1 << i, both_ways: 1); |
129 | do_test (json_ctx: &json_ctx, align1: 0, align2: i + 32, len: 1 << i, both_ways: 1); |
130 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: 1 << i, both_ways: 1); |
131 | do_test (json_ctx: &json_ctx, align1: i + 32, align2: i + 32, len: 1 << i, both_ways: 1); |
132 | do_test (json_ctx: &json_ctx, align1: half_page, align2: 0, len: 1 << i, both_ways: 1); |
133 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: 0, len: 1 << i, both_ways: 1); |
134 | do_test (json_ctx: &json_ctx, align1: half_page, align2: i, len: 1 << i, both_ways: 1); |
135 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: i, len: 1 << i, both_ways: 1); |
136 | } |
137 | |
138 | for (i = 0; i < 32; ++i) |
139 | { |
140 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: i, both_ways: 0); |
141 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, len: i, both_ways: 0); |
142 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: i, both_ways: 0); |
143 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: i, both_ways: 0); |
144 | do_test (json_ctx: &json_ctx, align1: half_page, align2: 0, len: i, both_ways: 0); |
145 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: 0, len: i, both_ways: 0); |
146 | do_test (json_ctx: &json_ctx, align1: half_page, align2: i, len: i, both_ways: 0); |
147 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: i, len: i, both_ways: 0); |
148 | do_test (json_ctx: &json_ctx, align1: getpagesize () - 1, align2: 0, len: i, both_ways: 0); |
149 | do_test (json_ctx: &json_ctx, align1: 0, align2: getpagesize () - 1, len: i, both_ways: 0); |
150 | } |
151 | |
152 | for (i = 3; i < 32; ++i) |
153 | { |
154 | if ((i & (i - 1)) == 0) |
155 | continue; |
156 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 16 * i, both_ways: 1); |
157 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, len: 16 * i, both_ways: 1); |
158 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: 16 * i, both_ways: 1); |
159 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: 16 * i, both_ways: 1); |
160 | do_test (json_ctx: &json_ctx, align1: half_page, align2: 0, len: 16 * i, both_ways: 1); |
161 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: 0, len: 16 * i, both_ways: 1); |
162 | do_test (json_ctx: &json_ctx, align1: half_page, align2: i, len: 16 * i, both_ways: 1); |
163 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: i, len: 16 * i, both_ways: 1); |
164 | } |
165 | |
166 | for (i = 32; i < 64; ++i) |
167 | { |
168 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 32 * i, both_ways: 1); |
169 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, len: 32 * i, both_ways: 1); |
170 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: 32 * i, both_ways: 1); |
171 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: 32 * i, both_ways: 1); |
172 | do_test (json_ctx: &json_ctx, align1: half_page, align2: 0, len: 32 * i, both_ways: 1); |
173 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: 0, len: 32 * i, both_ways: 1); |
174 | do_test (json_ctx: &json_ctx, align1: half_page, align2: i, len: 32 * i, both_ways: 1); |
175 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: i, len: 32 * i, both_ways: 1); |
176 | } |
177 | |
178 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: getpagesize (), both_ways: 1); |
179 | |
180 | for (i = 0; i <= 48; ++i) |
181 | { |
182 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2048 + 64 * i, both_ways: 1); |
183 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, len: 2048 + 64 * i, both_ways: 1); |
184 | do_test (json_ctx: &json_ctx, align1: i + 32, align2: 0, len: 2048 + 64 * i, both_ways: 1); |
185 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: 2048 + 64 * i, both_ways: 1); |
186 | do_test (json_ctx: &json_ctx, align1: 0, align2: i + 32, len: 2048 + 64 * i, both_ways: 1); |
187 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: 2048 + 64 * i, both_ways: 1); |
188 | do_test (json_ctx: &json_ctx, align1: i + 32, align2: i + 32, len: 2048 + 64 * i, both_ways: 1); |
189 | do_test (json_ctx: &json_ctx, align1: half_page, align2: 0, len: 2048 + 64 * i, both_ways: 1); |
190 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: 0, len: 2048 + 64 * i, both_ways: 1); |
191 | do_test (json_ctx: &json_ctx, align1: half_page, align2: i, len: 2048 + 64 * i, both_ways: 1); |
192 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: i, len: 2048 + 64 * i, both_ways: 1); |
193 | do_test (json_ctx: &json_ctx, align1: i, align2: 1, len: 2048 + 64 * i, both_ways: 1); |
194 | do_test (json_ctx: &json_ctx, align1: 1, align2: i, len: 2048 + 64 * i, both_ways: 1); |
195 | do_test (json_ctx: &json_ctx, align1: i + 32, align2: 1, len: 2048 + 64 * i, both_ways: 1); |
196 | do_test (json_ctx: &json_ctx, align1: 1, align2: i + 32, len: 2048 + 64 * i, both_ways: 1); |
197 | do_test (json_ctx: &json_ctx, align1: half_page + i, align2: 1, len: 2048 + 64 * i, both_ways: 1); |
198 | do_test (json_ctx: &json_ctx, align1: half_page + 1, align2: i, len: 2048 + 64 * i, both_ways: 1); |
199 | } |
200 | |
201 | json_array_end (ctx: &json_ctx); |
202 | json_attr_object_end (ctx: &json_ctx); |
203 | json_attr_object_end (ctx: &json_ctx); |
204 | json_document_end (ctx: &json_ctx); |
205 | |
206 | return ret; |
207 | } |
208 | |
209 | #include <support/test-driver.c> |
210 | |
211 | #define libc_hidden_builtin_def(X) |
212 | #undef MEMCPY |
213 | #define MEMCPY generic_memcpy |
214 | #include <string/memcpy.c> |
215 | #include <string/wordcopy.c> |
216 | |