1/* Measure memcpy function combined throughput for different alignments.
2 Copyright (C) 2017-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/* This microbenchmark measures the throughput of memcpy for various sizes from
20 1 byte to 32MiB, doubling every iteration and then misaligning by 0-15
21 bytes. The copies are done from source to destination and then back and the
22 source walks forward across the array and the destination walks backward by
23 one byte each, thus measuring misaligned accesses as well. The idea is to
24 avoid caching effects by copying a different string and far enough from each
25 other, walking in different directions so that we can measure prefetcher
26 efficiency (software or hardware) more closely than with a loop copying the
27 same data over and over, which eventually only gives us L1 cache
28 performance. */
29
30#ifndef MEMCPY_RESULT
31# define MEMCPY_RESULT(dst, len) dst
32# define START_SIZE 128
33# define MIN_PAGE_SIZE (getpagesize () + 32 * 1024 * 1024)
34# define TEST_MAIN
35# define TEST_NAME "memcpy"
36# define TIMEOUT (20 * 60)
37# include "bench-string.h"
38
39IMPL (memcpy, 1)
40#endif
41
42#include "json-lib.h"
43
44typedef char *(*proto_t) (char *, const char *, size_t);
45
46static void
47do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, char *src,
48 size_t len)
49{
50 size_t i = 0;
51 timing_t start, stop, cur;
52
53 char *dst_end = dst + MIN_PAGE_SIZE - len;
54 char *src_end = src + MIN_PAGE_SIZE - len;
55
56 TIMING_NOW (start);
57 /* Copy the entire buffer backwards, LEN at a time. */
58 for (; src_end >= src && dst_end >= dst; src_end -= len, dst_end -= len, i++)
59 CALL (impl, src_end, dst_end, len);
60 TIMING_NOW (stop);
61
62 TIMING_DIFF (cur, start, stop);
63
64 /* Get time taken per function call. */
65 json_element_double (ctx: json_ctx, d: (double) cur / i);
66}
67
68static void
69do_test (json_ctx_t *json_ctx, size_t len, int both_ways)
70{
71
72 char *s1, *s2;
73 size_t repeats;
74 s1 = (char *) (buf1);
75 s2 = (char *) (buf2);
76
77 for (repeats = both_ways ? 2 : 1; repeats; --repeats)
78 {
79 json_element_object_begin (ctx: json_ctx);
80 json_attr_uint (ctx: json_ctx, name: "length", d: (double) len);
81 json_attr_uint (ctx: json_ctx, name: "dst > src", d: (double) (s2 > s1));
82 json_array_begin (ctx: json_ctx, name: "timings");
83
84 FOR_EACH_IMPL (impl, 0)
85 do_one_test (json_ctx, impl, dst: s2, src: s1, len);
86
87 json_array_end (ctx: json_ctx);
88 json_element_object_end (ctx: json_ctx);
89
90 s1 = (char *) (buf2);
91 s2 = (char *) (buf1);
92 }
93}
94
95int
96test_main (void)
97{
98 json_ctx_t json_ctx;
99
100 test_init ();
101
102 json_init (ctx: &json_ctx, indent_level: 0, stdout);
103
104 json_document_begin (ctx: &json_ctx);
105 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
106
107 json_attr_object_begin (ctx: &json_ctx, name: "functions");
108 json_attr_object_begin (ctx: &json_ctx, name: "memcpy");
109 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "walk");
110
111 json_array_begin (ctx: &json_ctx, name: "ifuncs");
112 FOR_EACH_IMPL (impl, 0)
113 json_element_string (ctx: &json_ctx, s: impl->name);
114 json_array_end (ctx: &json_ctx);
115
116 json_array_begin (ctx: &json_ctx, name: "results");
117 for (size_t i = START_SIZE; i <= MIN_PAGE_SIZE; i <<= 1)
118 {
119 do_test (json_ctx: &json_ctx, len: i, both_ways: 1);
120 do_test (json_ctx: &json_ctx, len: i + 1, both_ways: 1);
121 }
122
123 json_array_end (ctx: &json_ctx);
124 json_attr_object_end (ctx: &json_ctx);
125 json_attr_object_end (ctx: &json_ctx);
126 json_document_end (ctx: &json_ctx);
127
128 return ret;
129}
130
131#include <support/test-driver.c>
132

source code of glibc/benchtests/bench-memcpy-walk.c