4 * Simple memcpy() and memset() benchmarks
6 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
11 #include "../util/util.h"
12 #include <subcmd/parse-options.h>
13 #include "../util/header.h"
14 #include "../util/cloexec.h"
16 #include "mem-memcpy-arch.h"
17 #include "mem-memset-arch.h"
24 #include <linux/time64.h>
28 static const char *size_str
= "1MB";
29 static const char *function_str
= "all";
30 static int nr_loops
= 1;
31 static bool use_cycles
;
34 static const struct option options
[] = {
35 OPT_STRING('s', "size", &size_str
, "1MB",
36 "Specify the size of the memory buffers. "
37 "Available units: B, KB, MB, GB and TB (case insensitive)"),
39 OPT_STRING('f', "function", &function_str
, "all",
40 "Specify the function to run, \"all\" runs all available functions, \"help\" lists them"),
42 OPT_INTEGER('l', "nr_loops", &nr_loops
,
43 "Specify the number of loops to run. (default: 1)"),
45 OPT_BOOLEAN('c', "cycles", &use_cycles
,
46 "Use a cycles event instead of gettimeofday() to measure performance"),
51 typedef void *(*memcpy_t
)(void *, const void *, size_t);
52 typedef void *(*memset_t
)(void *, int, size_t);
63 static struct perf_event_attr cycle_attr
= {
64 .type
= PERF_TYPE_HARDWARE
,
65 .config
= PERF_COUNT_HW_CPU_CYCLES
68 static int init_cycles(void)
70 cycles_fd
= sys_perf_event_open(&cycle_attr
, getpid(), -1, -1, perf_event_open_cloexec_flag());
72 if (cycles_fd
< 0 && errno
== ENOSYS
) {
73 pr_debug("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
80 static u64
get_cycles(void)
85 ret
= read(cycles_fd
, &clk
, sizeof(u64
));
86 BUG_ON(ret
!= sizeof(u64
));
91 static double timeval2double(struct timeval
*ts
)
93 return (double)ts
->tv_sec
+ (double)ts
->tv_usec
/ (double)USEC_PER_SEC
;
96 #define print_bps(x) do { \
98 printf(" %14lf bytes/sec\n", x); \
100 printf(" %14lfd KB/sec\n", x / K); \
101 else if (x < K * K * K) \
102 printf(" %14lf MB/sec\n", x / K / K); \
104 printf(" %14lf GB/sec\n", x / K / K / K); \
107 struct bench_mem_info
{
108 const struct function
*functions
;
109 u64 (*do_cycles
)(const struct function
*r
, size_t size
);
110 double (*do_gettimeofday
)(const struct function
*r
, size_t size
);
111 const char *const *usage
;
114 static void __bench_mem_function(struct bench_mem_info
*info
, int r_idx
, size_t size
, double size_total
)
116 const struct function
*r
= &info
->functions
[r_idx
];
117 double result_bps
= 0.0;
118 u64 result_cycles
= 0;
120 printf("# function '%s' (%s)\n", r
->name
, r
->desc
);
122 if (bench_format
== BENCH_FORMAT_DEFAULT
)
123 printf("# Copying %s bytes ...\n\n", size_str
);
126 result_cycles
= info
->do_cycles(r
, size
);
128 result_bps
= info
->do_gettimeofday(r
, size
);
131 switch (bench_format
) {
132 case BENCH_FORMAT_DEFAULT
:
134 printf(" %14lf cycles/byte\n", (double)result_cycles
/size_total
);
136 print_bps(result_bps
);
140 case BENCH_FORMAT_SIMPLE
:
142 printf("%lf\n", (double)result_cycles
/size_total
);
144 printf("%lf\n", result_bps
);
154 static int bench_mem_common(int argc
, const char **argv
, struct bench_mem_info
*info
)
160 argc
= parse_options(argc
, argv
, options
, info
->usage
, 0);
165 fprintf(stderr
, "Failed to open cycles counter\n");
170 size
= (size_t)perf_atoll((char *)size_str
);
171 size_total
= (double)size
* nr_loops
;
173 if ((s64
)size
<= 0) {
174 fprintf(stderr
, "Invalid size:%s\n", size_str
);
178 if (!strncmp(function_str
, "all", 3)) {
179 for (i
= 0; info
->functions
[i
].name
; i
++)
180 __bench_mem_function(info
, i
, size
, size_total
);
184 for (i
= 0; info
->functions
[i
].name
; i
++) {
185 if (!strcmp(info
->functions
[i
].name
, function_str
))
188 if (!info
->functions
[i
].name
) {
189 if (strcmp(function_str
, "help") && strcmp(function_str
, "h"))
190 printf("Unknown function: %s\n", function_str
);
191 printf("Available functions:\n");
192 for (i
= 0; info
->functions
[i
].name
; i
++) {
193 printf("\t%s ... %s\n",
194 info
->functions
[i
].name
, info
->functions
[i
].desc
);
199 __bench_mem_function(info
, i
, size
, size_total
);
204 static void memcpy_alloc_mem(void **dst
, void **src
, size_t size
)
208 die("memory allocation failed - maybe size is too large?\n");
212 die("memory allocation failed - maybe size is too large?\n");
214 /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
215 memset(*src
, 0, size
);
218 static u64
do_memcpy_cycles(const struct function
*r
, size_t size
)
220 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
221 void *src
= NULL
, *dst
= NULL
;
222 memcpy_t fn
= r
->fn
.memcpy
;
225 memcpy_alloc_mem(&dst
, &src
, size
);
228 * We prefault the freshly allocated memory range here,
229 * to not measure page fault overhead:
233 cycle_start
= get_cycles();
234 for (i
= 0; i
< nr_loops
; ++i
)
236 cycle_end
= get_cycles();
240 return cycle_end
- cycle_start
;
243 static double do_memcpy_gettimeofday(const struct function
*r
, size_t size
)
245 struct timeval tv_start
, tv_end
, tv_diff
;
246 memcpy_t fn
= r
->fn
.memcpy
;
247 void *src
= NULL
, *dst
= NULL
;
250 memcpy_alloc_mem(&dst
, &src
, size
);
253 * We prefault the freshly allocated memory range here,
254 * to not measure page fault overhead:
258 BUG_ON(gettimeofday(&tv_start
, NULL
));
259 for (i
= 0; i
< nr_loops
; ++i
)
261 BUG_ON(gettimeofday(&tv_end
, NULL
));
263 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
, const char *prefix __maybe_unused
)
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
,
299 return bench_mem_common(argc
, argv
, &info
);
302 static void memset_alloc_mem(void **dst
, size_t size
)
306 die("memory allocation failed - maybe size is too large?\n");
309 static u64
do_memset_cycles(const struct function
*r
, size_t size
)
311 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
312 memset_t fn
= r
->fn
.memset
;
316 memset_alloc_mem(&dst
, size
);
319 * We prefault the freshly allocated memory range here,
320 * to not measure page fault overhead:
324 cycle_start
= get_cycles();
325 for (i
= 0; i
< nr_loops
; ++i
)
327 cycle_end
= get_cycles();
330 return cycle_end
- cycle_start
;
333 static double do_memset_gettimeofday(const struct function
*r
, size_t size
)
335 struct timeval tv_start
, tv_end
, tv_diff
;
336 memset_t fn
= r
->fn
.memset
;
340 memset_alloc_mem(&dst
, size
);
343 * We prefault the freshly allocated memory range here,
344 * to not measure page fault overhead:
348 BUG_ON(gettimeofday(&tv_start
, NULL
));
349 for (i
= 0; i
< nr_loops
; ++i
)
351 BUG_ON(gettimeofday(&tv_end
, NULL
));
353 timersub(&tv_end
, &tv_start
, &tv_diff
);
356 return (double)(((double)size
* nr_loops
) / timeval2double(&tv_diff
));
359 static const char * const bench_mem_memset_usage
[] = {
360 "perf bench mem memset <options>",
364 static const struct function memset_functions
[] = {
366 .desc
= "Default memset() provided by glibc",
367 .fn
.memset
= memset
},
369 #ifdef HAVE_ARCH_X86_64_SUPPORT
370 # define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
371 # include "mem-memset-x86-64-asm-def.h"
378 int bench_mem_memset(int argc
, const char **argv
, const char *prefix __maybe_unused
)
380 struct bench_mem_info info
= {
381 .functions
= memset_functions
,
382 .do_cycles
= do_memset_cycles
,
383 .do_gettimeofday
= do_memset_gettimeofday
,
384 .usage
= bench_mem_memset_usage
,
387 return bench_mem_common(argc
, argv
, &info
);