1 // SPDX-License-Identifier: GPL-2.0
5 * Simple memcpy() and memset() benchmarks
7 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
11 #include "../perf-sys.h"
12 #include <subcmd/parse-options.h>
13 #include "../util/header.h"
14 #include "../util/cloexec.h"
15 #include "../util/string2.h"
17 #include "mem-memcpy-arch.h"
18 #include "mem-memset-arch.h"
26 #include <linux/time64.h>
27 #include <linux/zalloc.h>
31 static const char *size_str
= "1MB";
32 static const char *function_str
= "all";
33 static int nr_loops
= 1;
34 static bool use_cycles
;
37 static const struct option options
[] = {
38 OPT_STRING('s', "size", &size_str
, "1MB",
39 "Specify the size of the memory buffers. "
40 "Available units: B, KB, MB, GB and TB (case insensitive)"),
42 OPT_STRING('f', "function", &function_str
, "all",
43 "Specify the function to run, \"all\" runs all available functions, \"help\" lists them"),
45 OPT_INTEGER('l', "nr_loops", &nr_loops
,
46 "Specify the number of loops to run. (default: 1)"),
48 OPT_BOOLEAN('c', "cycles", &use_cycles
,
49 "Use a cycles event instead of gettimeofday() to measure performance"),
54 typedef void *(*memcpy_t
)(void *, const void *, size_t);
55 typedef void *(*memset_t
)(void *, int, size_t);
66 static struct perf_event_attr cycle_attr
= {
67 .type
= PERF_TYPE_HARDWARE
,
68 .config
= PERF_COUNT_HW_CPU_CYCLES
71 static int init_cycles(void)
73 cycles_fd
= sys_perf_event_open(&cycle_attr
, getpid(), -1, -1, perf_event_open_cloexec_flag());
75 if (cycles_fd
< 0 && errno
== ENOSYS
) {
76 pr_debug("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
83 static u64
get_cycles(void)
88 ret
= read(cycles_fd
, &clk
, sizeof(u64
));
89 BUG_ON(ret
!= sizeof(u64
));
94 static double timeval2double(struct timeval
*ts
)
96 return (double)ts
->tv_sec
+ (double)ts
->tv_usec
/ (double)USEC_PER_SEC
;
99 #define print_bps(x) do { \
101 printf(" %14lf bytes/sec\n", x); \
102 else if (x < K * K) \
103 printf(" %14lfd KB/sec\n", x / K); \
104 else if (x < K * K * K) \
105 printf(" %14lf MB/sec\n", x / K / K); \
107 printf(" %14lf GB/sec\n", x / K / K / K); \
110 struct bench_mem_info
{
111 const struct function
*functions
;
112 u64 (*do_cycles
)(const struct function
*r
, size_t size
, void *src
, void *dst
);
113 double (*do_gettimeofday
)(const struct function
*r
, size_t size
, void *src
, void *dst
);
114 const char *const *usage
;
118 static void __bench_mem_function(struct bench_mem_info
*info
, int r_idx
, size_t size
, double size_total
)
120 const struct function
*r
= &info
->functions
[r_idx
];
121 double result_bps
= 0.0;
122 u64 result_cycles
= 0;
123 void *src
= NULL
, *dst
= zalloc(size
);
125 printf("# function '%s' (%s)\n", r
->name
, r
->desc
);
128 goto out_alloc_failed
;
130 if (info
->alloc_src
) {
133 goto out_alloc_failed
;
136 if (bench_format
== BENCH_FORMAT_DEFAULT
)
137 printf("# Copying %s bytes ...\n\n", size_str
);
140 result_cycles
= info
->do_cycles(r
, size
, src
, dst
);
142 result_bps
= info
->do_gettimeofday(r
, size
, src
, dst
);
145 switch (bench_format
) {
146 case BENCH_FORMAT_DEFAULT
:
148 printf(" %14lf cycles/byte\n", (double)result_cycles
/size_total
);
150 print_bps(result_bps
);
154 case BENCH_FORMAT_SIMPLE
:
156 printf("%lf\n", (double)result_cycles
/size_total
);
158 printf("%lf\n", result_bps
);
172 printf("# Memory allocation failed - maybe size (%s) is too large?\n", size_str
);
176 static int bench_mem_common(int argc
, const char **argv
, struct bench_mem_info
*info
)
182 argc
= parse_options(argc
, argv
, options
, info
->usage
, 0);
187 fprintf(stderr
, "Failed to open cycles counter\n");
192 size
= (size_t)perf_atoll((char *)size_str
);
193 size_total
= (double)size
* nr_loops
;
195 if ((s64
)size
<= 0) {
196 fprintf(stderr
, "Invalid size:%s\n", size_str
);
200 if (!strncmp(function_str
, "all", 3)) {
201 for (i
= 0; info
->functions
[i
].name
; i
++)
202 __bench_mem_function(info
, i
, size
, size_total
);
206 for (i
= 0; info
->functions
[i
].name
; i
++) {
207 if (!strcmp(info
->functions
[i
].name
, function_str
))
210 if (!info
->functions
[i
].name
) {
211 if (strcmp(function_str
, "help") && strcmp(function_str
, "h"))
212 printf("Unknown function: %s\n", function_str
);
213 printf("Available functions:\n");
214 for (i
= 0; info
->functions
[i
].name
; i
++) {
215 printf("\t%s ... %s\n",
216 info
->functions
[i
].name
, info
->functions
[i
].desc
);
221 __bench_mem_function(info
, i
, size
, size_total
);
226 static u64
do_memcpy_cycles(const struct function
*r
, size_t size
, void *src
, void *dst
)
228 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
229 memcpy_t fn
= r
->fn
.memcpy
;
232 /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
233 memset(src
, 0, size
);
236 * We prefault the freshly allocated memory range here,
237 * to not measure page fault overhead:
241 cycle_start
= get_cycles();
242 for (i
= 0; i
< nr_loops
; ++i
)
244 cycle_end
= get_cycles();
246 return cycle_end
- cycle_start
;
249 static double do_memcpy_gettimeofday(const struct function
*r
, size_t size
, void *src
, void *dst
)
251 struct timeval tv_start
, tv_end
, tv_diff
;
252 memcpy_t fn
= r
->fn
.memcpy
;
256 * We prefault the freshly allocated memory range here,
257 * to not measure page fault overhead:
261 BUG_ON(gettimeofday(&tv_start
, NULL
));
262 for (i
= 0; i
< nr_loops
; ++i
)
264 BUG_ON(gettimeofday(&tv_end
, NULL
));
266 timersub(&tv_end
, &tv_start
, &tv_diff
);
268 return (double)(((double)size
* nr_loops
) / timeval2double(&tv_diff
));
271 struct function memcpy_functions
[] = {
273 .desc
= "Default memcpy() provided by glibc",
274 .fn
.memcpy
= memcpy
},
276 #ifdef HAVE_ARCH_X86_64_SUPPORT
277 # define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
278 # include "mem-memcpy-x86-64-asm-def.h"
285 static const char * const bench_mem_memcpy_usage
[] = {
286 "perf bench mem memcpy <options>",
290 int bench_mem_memcpy(int argc
, const char **argv
)
292 struct bench_mem_info info
= {
293 .functions
= memcpy_functions
,
294 .do_cycles
= do_memcpy_cycles
,
295 .do_gettimeofday
= do_memcpy_gettimeofday
,
296 .usage
= bench_mem_memcpy_usage
,
300 return bench_mem_common(argc
, argv
, &info
);
303 static u64
do_memset_cycles(const struct function
*r
, size_t size
, void *src __maybe_unused
, void *dst
)
305 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
306 memset_t fn
= r
->fn
.memset
;
310 * We prefault the freshly allocated memory range here,
311 * to not measure page fault overhead:
315 cycle_start
= get_cycles();
316 for (i
= 0; i
< nr_loops
; ++i
)
318 cycle_end
= get_cycles();
320 return cycle_end
- cycle_start
;
323 static double do_memset_gettimeofday(const struct function
*r
, size_t size
, void *src __maybe_unused
, void *dst
)
325 struct timeval tv_start
, tv_end
, tv_diff
;
326 memset_t fn
= r
->fn
.memset
;
330 * We prefault the freshly allocated memory range here,
331 * to not measure page fault overhead:
335 BUG_ON(gettimeofday(&tv_start
, NULL
));
336 for (i
= 0; i
< nr_loops
; ++i
)
338 BUG_ON(gettimeofday(&tv_end
, NULL
));
340 timersub(&tv_end
, &tv_start
, &tv_diff
);
342 return (double)(((double)size
* nr_loops
) / timeval2double(&tv_diff
));
345 static const char * const bench_mem_memset_usage
[] = {
346 "perf bench mem memset <options>",
350 static const struct function memset_functions
[] = {
352 .desc
= "Default memset() provided by glibc",
353 .fn
.memset
= memset
},
355 #ifdef HAVE_ARCH_X86_64_SUPPORT
356 # define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
357 # include "mem-memset-x86-64-asm-def.h"
364 int bench_mem_memset(int argc
, const char **argv
)
366 struct bench_mem_info info
= {
367 .functions
= memset_functions
,
368 .do_cycles
= do_memset_cycles
,
369 .do_gettimeofday
= do_memset_gettimeofday
,
370 .usage
= bench_mem_memset_usage
,
373 return bench_mem_common(argc
, argv
, &info
);