4 * Simple memcpy() and memset() benchmarks
6 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
10 #include "../util/util.h"
11 #include "../util/parse-options.h"
12 #include "../util/header.h"
13 #include "../util/cloexec.h"
15 #include "mem-memcpy-arch.h"
16 #include "mem-memset-arch.h"
26 static const char *length_str = "1MB";
27 static const char *routine = "all";
28 static int iterations = 1;
29 static bool use_cycle;
31 static bool only_prefault;
32 static bool no_prefault;
34 static const struct option options[] = {
35 OPT_STRING('l', "length", &length_str, "1MB",
36 "Specify length of memory to copy. "
37 "Available units: B, KB, MB, GB and TB (upper and lower)"),
38 OPT_STRING('r', "routine", &routine, "all",
39 "Specify routine to copy, \"all\" runs all available routines"),
40 OPT_INTEGER('i', "iterations", &iterations,
41 "repeat memcpy() invocation this number of times"),
42 OPT_BOOLEAN('c', "cycle", &use_cycle,
43 "Use cycles event instead of gettimeofday() for measuring"),
44 OPT_BOOLEAN('o', "only-prefault", &only_prefault,
45 "Show only the result with page faults before memcpy()"),
46 OPT_BOOLEAN('n', "no-prefault", &no_prefault,
47 "Show only the result without page faults before memcpy()"),
51 typedef void *(*memcpy_t)(void *, const void *, size_t);
52 typedef void *(*memset_t)(void *, int, size_t);
63 struct routine memcpy_routines[] = {
65 .desc = "Default memcpy() provided by glibc",
66 .fn.memcpy = memcpy },
68 #ifdef HAVE_ARCH_X86_64_SUPPORT
69 # define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
70 # include "mem-memcpy-x86-64-asm-def.h"
77 static const char * const bench_mem_memcpy_usage[] = {
78 "perf bench mem memcpy <options>",
82 static struct perf_event_attr cycle_attr = {
83 .type = PERF_TYPE_HARDWARE,
84 .config = PERF_COUNT_HW_CPU_CYCLES
87 static void init_cycle(void)
89 cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, perf_event_open_cloexec_flag());
91 if (cycle_fd < 0 && errno == ENOSYS)
92 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
97 static u64 get_cycle(void)
102 ret = read(cycle_fd, &clk, sizeof(u64));
103 BUG_ON(ret != sizeof(u64));
108 static double timeval2double(struct timeval *ts)
110 return (double)ts->tv_sec + (double)ts->tv_usec / (double)1000000;
113 #define print_bps(x) do { \
115 printf(" %14lf B/Sec", x); \
116 else if (x < K * K) \
117 printf(" %14lfd KB/Sec", x / K); \
118 else if (x < K * K * K) \
119 printf(" %14lf MB/Sec", x / K / K); \
121 printf(" %14lf GB/Sec", x / K / K / K); \
124 struct bench_mem_info {
125 const struct routine *routines;
126 u64 (*do_cycle)(const struct routine *r, size_t len, bool prefault);
127 double (*do_gettimeofday)(const struct routine *r, size_t len, bool prefault);
128 const char *const *usage;
131 static void __bench_mem_routine(struct bench_mem_info *info, int r_idx, size_t len, double totallen)
133 const struct routine *r = &info->routines[r_idx];
134 double result_bps[2];
136 int prefault = no_prefault ? 0 : 1;
138 result_cycle[0] = result_cycle[1] = 0ULL;
139 result_bps[0] = result_bps[1] = 0.0;
141 printf("Routine %s (%s)\n", r->name, r->desc);
143 if (bench_format == BENCH_FORMAT_DEFAULT)
144 printf("# Copying %s Bytes ...\n\n", length_str);
146 if (!only_prefault && prefault) {
147 /* Show both results: */
149 result_cycle[0] = info->do_cycle(r, len, false);
150 result_cycle[1] = info->do_cycle(r, len, true);
152 result_bps[0] = info->do_gettimeofday(r, len, false);
153 result_bps[1] = info->do_gettimeofday(r, len, true);
157 result_cycle[prefault] = info->do_cycle(r, len, only_prefault);
159 result_bps[prefault] = info->do_gettimeofday(r, len, only_prefault);
162 switch (bench_format) {
163 case BENCH_FORMAT_DEFAULT:
164 if (!only_prefault && prefault) {
166 printf(" %14lf Cycle/Byte\n",
167 (double)result_cycle[0]
169 printf(" %14lf Cycle/Byte (with prefault)\n",
170 (double)result_cycle[1]
173 print_bps(result_bps[0]);
175 print_bps(result_bps[1]);
176 printf(" (with prefault)\n");
180 printf(" %14lf Cycle/Byte",
181 (double)result_cycle[prefault]
184 print_bps(result_bps[prefault]);
186 printf("%s\n", only_prefault ? " (with prefault)" : "");
189 case BENCH_FORMAT_SIMPLE:
190 if (!only_prefault && prefault) {
193 (double)result_cycle[0] / totallen,
194 (double)result_cycle[1] / totallen);
197 result_bps[0], result_bps[1]);
201 printf("%lf\n", (double)result_cycle[prefault]
204 printf("%lf\n", result_bps[prefault]);
208 /* Reaching this means there's some disaster: */
209 die("unknown format: %d\n", bench_format);
214 static int bench_mem_common(int argc, const char **argv,
215 const char *prefix __maybe_unused,
216 struct bench_mem_info *info)
222 argc = parse_options(argc, argv, options, info->usage, 0);
224 if (no_prefault && only_prefault) {
225 fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
232 len = (size_t)perf_atoll((char *)length_str);
233 totallen = (double)len * iterations;
236 fprintf(stderr, "Invalid length:%s\n", length_str);
240 /* Same as without specifying either of prefault and no-prefault: */
241 if (only_prefault && no_prefault)
242 only_prefault = no_prefault = false;
244 if (!strncmp(routine, "all", 3)) {
245 for (i = 0; info->routines[i].name; i++)
246 __bench_mem_routine(info, i, len, totallen);
250 for (i = 0; info->routines[i].name; i++) {
251 if (!strcmp(info->routines[i].name, routine))
254 if (!info->routines[i].name) {
255 printf("Unknown routine:%s\n", routine);
256 printf("Available routines...\n");
257 for (i = 0; info->routines[i].name; i++) {
258 printf("\t%s ... %s\n",
259 info->routines[i].name, info->routines[i].desc);
264 __bench_mem_routine(info, i, len, totallen);
269 static void memcpy_alloc_mem(void **dst, void **src, size_t length)
271 *dst = zalloc(length);
273 die("memory allocation failed - maybe length is too large?\n");
275 *src = zalloc(length);
277 die("memory allocation failed - maybe length is too large?\n");
279 /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
280 memset(*src, 0, length);
283 static u64 do_memcpy_cycle(const struct routine *r, size_t len, bool prefault)
285 u64 cycle_start = 0ULL, cycle_end = 0ULL;
286 void *src = NULL, *dst = NULL;
287 memcpy_t fn = r->fn.memcpy;
290 memcpy_alloc_mem(&dst, &src, len);
295 cycle_start = get_cycle();
296 for (i = 0; i < iterations; ++i)
298 cycle_end = get_cycle();
302 return cycle_end - cycle_start;
305 static double do_memcpy_gettimeofday(const struct routine *r, size_t len, bool prefault)
307 struct timeval tv_start, tv_end, tv_diff;
308 memcpy_t fn = r->fn.memcpy;
309 void *src = NULL, *dst = NULL;
312 memcpy_alloc_mem(&dst, &src, len);
317 BUG_ON(gettimeofday(&tv_start, NULL));
318 for (i = 0; i < iterations; ++i)
320 BUG_ON(gettimeofday(&tv_end, NULL));
322 timersub(&tv_end, &tv_start, &tv_diff);
326 return (double)(((double)len * iterations) / timeval2double(&tv_diff));
329 int bench_mem_memcpy(int argc, const char **argv,
330 const char *prefix __maybe_unused)
332 struct bench_mem_info info = {
333 .routines = memcpy_routines,
334 .do_cycle = do_memcpy_cycle,
335 .do_gettimeofday = do_memcpy_gettimeofday,
336 .usage = bench_mem_memcpy_usage,
339 return bench_mem_common(argc, argv, prefix, &info);
342 static void memset_alloc_mem(void **dst, size_t length)
344 *dst = zalloc(length);
346 die("memory allocation failed - maybe length is too large?\n");
349 static u64 do_memset_cycle(const struct routine *r, size_t len, bool prefault)
351 u64 cycle_start = 0ULL, cycle_end = 0ULL;
352 memset_t fn = r->fn.memset;
356 memset_alloc_mem(&dst, len);
361 cycle_start = get_cycle();
362 for (i = 0; i < iterations; ++i)
364 cycle_end = get_cycle();
367 return cycle_end - cycle_start;
370 static double do_memset_gettimeofday(const struct routine *r, size_t len,
373 struct timeval tv_start, tv_end, tv_diff;
374 memset_t fn = r->fn.memset;
378 memset_alloc_mem(&dst, len);
383 BUG_ON(gettimeofday(&tv_start, NULL));
384 for (i = 0; i < iterations; ++i)
386 BUG_ON(gettimeofday(&tv_end, NULL));
388 timersub(&tv_end, &tv_start, &tv_diff);
391 return (double)(((double)len * iterations) / timeval2double(&tv_diff));
394 static const char * const bench_mem_memset_usage[] = {
395 "perf bench mem memset <options>",
399 static const struct routine memset_routines[] = {
401 .desc = "Default memset() provided by glibc",
402 .fn.memset = memset },
404 #ifdef HAVE_ARCH_X86_64_SUPPORT
405 # define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
406 # include "mem-memset-x86-64-asm-def.h"
413 int bench_mem_memset(int argc, const char **argv, const char *prefix __maybe_unused)
415 struct bench_mem_info info = {
416 .routines = memset_routines,
417 .do_cycle = do_memset_cycle,
418 .do_gettimeofday = do_memset_gettimeofday,
419 .usage = bench_mem_memset_usage,
422 return bench_mem_common(argc, argv, prefix, &info);