1/* Measure memset function throughput with large data sizes.
2 Copyright (C) 2017-2024 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 START_SIZE 128
26#define MIN_PAGE_SIZE (getpagesize () + 32 * 1024 * 1024)
27#define TIMEOUT (20 * 60)
28#include "bench-string.h"
29
30#include "json-lib.h"
31
32#ifdef DO_MEMSET
33void *generic_memset (void *, int, size_t);
34
35typedef void *(*proto_t) (void *, int, size_t);
36
37IMPL (memset, 1)
38IMPL (generic_memset, 0)
39
40#else
41static void
42memset_zero (void * s, size_t len)
43{
44 memset (s, '\0', len);
45}
46
47typedef void (*proto_t) (void *, size_t);
48
49IMPL (bzero, 1)
50IMPL (memset_zero, 0)
51#endif
52
53static void
54do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, CHAR *s_end,
55 size_t n)
56{
57 size_t i, iters = MIN_PAGE_SIZE / n;
58 timing_t start, stop, cur;
59
60 TIMING_NOW (start);
61 for (i = 0; i < iters && s <= s_end; s_end -= n, i++)
62#ifdef DO_MEMSET
63 CALL (impl, s, 0, n);
64#else
65 CALL (impl, s, n);
66#endif
67 TIMING_NOW (stop);
68
69 TIMING_DIFF (cur, start, stop);
70
71 /* Get time taken per function call. */
72 json_element_double (ctx: json_ctx, d: (double) cur / i);
73}
74
75static void
76do_test (json_ctx_t *json_ctx, size_t len)
77{
78 json_element_object_begin (ctx: json_ctx);
79 json_attr_uint (ctx: json_ctx, name: "length", d: len);
80 json_array_begin (ctx: json_ctx, name: "timings");
81
82 FOR_EACH_IMPL (impl, 0)
83 {
84 do_one_test (json_ctx, impl, s: (CHAR *) buf1,
85 s_end: (CHAR *) buf1 + MIN_PAGE_SIZE - len, n: len);
86 alloc_bufs ();
87 }
88
89 json_array_end (ctx: json_ctx);
90 json_element_object_end (ctx: json_ctx);
91}
92
93int
94test_main (void)
95{
96 json_ctx_t json_ctx;
97 size_t i;
98
99 test_init ();
100
101 json_init (ctx: &json_ctx, indent_level: 0, stdout);
102
103 json_document_begin (ctx: &json_ctx);
104 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
105
106 json_attr_object_begin (ctx: &json_ctx, name: "functions");
107 json_attr_object_begin (ctx: &json_ctx, TEST_NAME);
108 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "walk");
109
110 json_array_begin (ctx: &json_ctx, name: "ifuncs");
111 FOR_EACH_IMPL (impl, 0)
112 json_element_string (ctx: &json_ctx, s: impl->name);
113 json_array_end (ctx: &json_ctx);
114
115 json_array_begin (ctx: &json_ctx, name: "results");
116 for (i = 1; i <= 64; i++)
117 do_test (json_ctx: &json_ctx, len: i);
118
119 for (i = START_SIZE; i <= MIN_PAGE_SIZE; i <<= 1)
120 {
121 do_test (json_ctx: &json_ctx, len: i);
122 do_test (json_ctx: &json_ctx, len: i + 1);
123 }
124
125 json_array_end (ctx: &json_ctx);
126 json_attr_object_end (ctx: &json_ctx);
127 json_attr_object_end (ctx: &json_ctx);
128 json_document_end (ctx: &json_ctx);
129
130 return ret;
131}
132
133#include <support/test-driver.c>
134
135#ifdef DO_MEMSET
136# define libc_hidden_builtin_def(X)
137# define libc_hidden_def(X)
138# define libc_hidden_weak(X)
139# define weak_alias(X,Y)
140# undef MEMSET
141# define MEMSET generic_memset
142# include <string/memset.c>
143#endif
144

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