4 * memcpy: Simple memory copy in various ways
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
= "default";
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
, "default",
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
},
67 #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"
80 static const char * const bench_mem_memcpy_usage
[] = {
81 "perf bench mem memcpy <options>",
85 static struct perf_event_attr cycle_attr
= {
86 .type
= PERF_TYPE_HARDWARE
,
87 .config
= PERF_COUNT_HW_CPU_CYCLES
90 static void init_cycle(void)
92 cycle_fd
= sys_perf_event_open(&cycle_attr
, getpid(), -1, -1,
93 perf_event_open_cloexec_flag());
95 if (cycle_fd
< 0 && errno
== ENOSYS
)
96 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
101 static u64
get_cycle(void)
106 ret
= read(cycle_fd
, &clk
, sizeof(u64
));
107 BUG_ON(ret
!= sizeof(u64
));
112 static double timeval2double(struct timeval
*ts
)
114 return (double)ts
->tv_sec
+
115 (double)ts
->tv_usec
/ (double)1000000;
118 #define pf (no_prefault ? 0 : 1)
120 #define print_bps(x) do { \
122 printf(" %14lf B/Sec", x); \
123 else if (x < K * K) \
124 printf(" %14lfd KB/Sec", x / K); \
125 else if (x < K * K * K) \
126 printf(" %14lf MB/Sec", x / K / K); \
128 printf(" %14lf GB/Sec", x / K / K / K); \
131 struct bench_mem_info
{
132 const struct routine
*routines
;
133 u64 (*do_cycle
)(const struct routine
*r
, size_t len
, bool prefault
);
134 double (*do_gettimeofday
)(const struct routine
*r
, size_t len
, bool prefault
);
135 const char *const *usage
;
138 static void __bench_mem_routine(struct bench_mem_info
*info
, int r_idx
, size_t len
, double totallen
)
140 const struct routine
*r
= &info
->routines
[r_idx
];
141 double result_bps
[2];
144 result_cycle
[0] = result_cycle
[1] = 0ULL;
145 result_bps
[0] = result_bps
[1] = 0.0;
147 printf("Routine %s (%s)\n", r
->name
, r
->desc
);
149 if (bench_format
== BENCH_FORMAT_DEFAULT
)
150 printf("# Copying %s Bytes ...\n\n", length_str
);
152 if (!only_prefault
&& !no_prefault
) {
153 /* show both of results */
155 result_cycle
[0] = info
->do_cycle(r
, len
, false);
156 result_cycle
[1] = info
->do_cycle(r
, len
, true);
158 result_bps
[0] = info
->do_gettimeofday(r
, len
, false);
159 result_bps
[1] = info
->do_gettimeofday(r
, len
, true);
163 result_cycle
[pf
] = info
->do_cycle(r
, len
, only_prefault
);
165 result_bps
[pf
] = info
->do_gettimeofday(r
, len
, only_prefault
);
168 switch (bench_format
) {
169 case BENCH_FORMAT_DEFAULT
:
170 if (!only_prefault
&& !no_prefault
) {
172 printf(" %14lf Cycle/Byte\n",
173 (double)result_cycle
[0]
175 printf(" %14lf Cycle/Byte (with prefault)\n",
176 (double)result_cycle
[1]
179 print_bps(result_bps
[0]);
181 print_bps(result_bps
[1]);
182 printf(" (with prefault)\n");
186 printf(" %14lf Cycle/Byte",
187 (double)result_cycle
[pf
]
190 print_bps(result_bps
[pf
]);
192 printf("%s\n", only_prefault
? " (with prefault)" : "");
195 case BENCH_FORMAT_SIMPLE
:
196 if (!only_prefault
&& !no_prefault
) {
199 (double)result_cycle
[0] / totallen
,
200 (double)result_cycle
[1] / totallen
);
203 result_bps
[0], result_bps
[1]);
207 printf("%lf\n", (double)result_cycle
[pf
]
210 printf("%lf\n", result_bps
[pf
]);
214 /* reaching this means there's some disaster: */
215 die("unknown format: %d\n", bench_format
);
220 static int bench_mem_common(int argc
, const char **argv
,
221 const char *prefix __maybe_unused
,
222 struct bench_mem_info
*info
)
228 argc
= parse_options(argc
, argv
, options
,
231 if (no_prefault
&& only_prefault
) {
232 fprintf(stderr
, "Invalid options: -o and -n are mutually exclusive\n");
239 len
= (size_t)perf_atoll((char *)length_str
);
240 totallen
= (double)len
* iterations
;
243 fprintf(stderr
, "Invalid length:%s\n", length_str
);
247 /* same to without specifying either of prefault and no-prefault */
248 if (only_prefault
&& no_prefault
)
249 only_prefault
= no_prefault
= false;
251 if (!strncmp(routine
, "all", 3)) {
252 for (i
= 0; info
->routines
[i
].name
; i
++)
253 __bench_mem_routine(info
, i
, len
, totallen
);
257 for (i
= 0; info
->routines
[i
].name
; i
++) {
258 if (!strcmp(info
->routines
[i
].name
, routine
))
261 if (!info
->routines
[i
].name
) {
262 printf("Unknown routine:%s\n", routine
);
263 printf("Available routines...\n");
264 for (i
= 0; info
->routines
[i
].name
; i
++) {
265 printf("\t%s ... %s\n",
266 info
->routines
[i
].name
, info
->routines
[i
].desc
);
271 __bench_mem_routine(info
, i
, len
, totallen
);
276 static void memcpy_alloc_mem(void **dst
, void **src
, size_t length
)
278 *dst
= zalloc(length
);
280 die("memory allocation failed - maybe length is too large?\n");
282 *src
= zalloc(length
);
284 die("memory allocation failed - maybe length is too large?\n");
285 /* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */
286 memset(*src
, 0, length
);
289 static u64
do_memcpy_cycle(const struct routine
*r
, size_t len
, bool prefault
)
291 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
292 void *src
= NULL
, *dst
= NULL
;
293 memcpy_t fn
= r
->fn
.memcpy
;
296 memcpy_alloc_mem(&dst
, &src
, len
);
301 cycle_start
= get_cycle();
302 for (i
= 0; i
< iterations
; ++i
)
304 cycle_end
= get_cycle();
308 return cycle_end
- cycle_start
;
311 static double do_memcpy_gettimeofday(const struct routine
*r
, size_t len
,
314 struct timeval tv_start
, tv_end
, tv_diff
;
315 memcpy_t fn
= r
->fn
.memcpy
;
316 void *src
= NULL
, *dst
= NULL
;
319 memcpy_alloc_mem(&dst
, &src
, len
);
324 BUG_ON(gettimeofday(&tv_start
, NULL
));
325 for (i
= 0; i
< iterations
; ++i
)
327 BUG_ON(gettimeofday(&tv_end
, NULL
));
329 timersub(&tv_end
, &tv_start
, &tv_diff
);
333 return (double)(((double)len
* iterations
) / timeval2double(&tv_diff
));
336 int bench_mem_memcpy(int argc
, const char **argv
,
337 const char *prefix __maybe_unused
)
339 struct bench_mem_info info
= {
340 .routines
= memcpy_routines
,
341 .do_cycle
= do_memcpy_cycle
,
342 .do_gettimeofday
= do_memcpy_gettimeofday
,
343 .usage
= bench_mem_memcpy_usage
,
346 return bench_mem_common(argc
, argv
, prefix
, &info
);
349 static void memset_alloc_mem(void **dst
, size_t length
)
351 *dst
= zalloc(length
);
353 die("memory allocation failed - maybe length is too large?\n");
356 static u64
do_memset_cycle(const struct routine
*r
, size_t len
, bool prefault
)
358 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
359 memset_t fn
= r
->fn
.memset
;
363 memset_alloc_mem(&dst
, len
);
368 cycle_start
= get_cycle();
369 for (i
= 0; i
< iterations
; ++i
)
371 cycle_end
= get_cycle();
374 return cycle_end
- cycle_start
;
377 static double do_memset_gettimeofday(const struct routine
*r
, size_t len
,
380 struct timeval tv_start
, tv_end
, tv_diff
;
381 memset_t fn
= r
->fn
.memset
;
385 memset_alloc_mem(&dst
, len
);
390 BUG_ON(gettimeofday(&tv_start
, NULL
));
391 for (i
= 0; i
< iterations
; ++i
)
393 BUG_ON(gettimeofday(&tv_end
, NULL
));
395 timersub(&tv_end
, &tv_start
, &tv_diff
);
398 return (double)(((double)len
* iterations
) / timeval2double(&tv_diff
));
401 static const char * const bench_mem_memset_usage
[] = {
402 "perf bench mem memset <options>",
406 static const struct routine memset_routines
[] = {
408 .desc
= "Default memset() provided by glibc",
409 .fn
.memset
= memset
},
410 #ifdef HAVE_ARCH_X86_64_SUPPORT
412 #define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
413 #include "mem-memset-x86-64-asm-def.h"
423 int bench_mem_memset(int argc
, const char **argv
,
424 const char *prefix __maybe_unused
)
426 struct bench_mem_info info
= {
427 .routines
= memset_routines
,
428 .do_cycle
= do_memset_cycle
,
429 .do_gettimeofday
= do_memset_gettimeofday
,
430 .usage
= bench_mem_memset_usage
,
433 return bench_mem_common(argc
, argv
, prefix
, &info
);