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"
25 static const char *length_str = "1MB";
26 static const char *routine = "default";
27 static int iterations = 1;
28 static bool use_cycle;
30 static bool only_prefault;
31 static bool no_prefault;
33 static const struct option options[] = {
34 OPT_STRING('l', "length", &length_str, "1MB",
35 "Specify length of memory to copy. "
36 "Available units: B, KB, MB, GB and TB (upper and lower)"),
37 OPT_STRING('r', "routine", &routine, "default",
38 "Specify routine to copy"),
39 OPT_INTEGER('i', "iterations", &iterations,
40 "repeat memcpy() invocation this number of times"),
41 OPT_BOOLEAN('c', "cycle", &use_cycle,
42 "Use cycles event instead of gettimeofday() for measuring"),
43 OPT_BOOLEAN('o', "only-prefault", &only_prefault,
44 "Show only the result with page faults before memcpy()"),
45 OPT_BOOLEAN('n', "no-prefault", &no_prefault,
46 "Show only the result without page faults before memcpy()"),
50 typedef void *(*memcpy_t)(void *, const void *, size_t);
58 struct routine routines[] = {
60 "Default memcpy() provided by glibc",
62 #ifdef HAVE_ARCH_X86_64_SUPPORT
64 #define MEMCPY_FN(fn, name, desc) { name, desc, fn },
65 #include "mem-memcpy-x86-64-asm-def.h"
75 static const char * const bench_mem_memcpy_usage[] = {
76 "perf bench mem memcpy <options>",
80 static struct perf_event_attr cycle_attr = {
81 .type = PERF_TYPE_HARDWARE,
82 .config = PERF_COUNT_HW_CPU_CYCLES
85 static void init_cycle(void)
87 cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1,
88 perf_event_open_cloexec_flag());
90 if (cycle_fd < 0 && errno == ENOSYS)
91 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
96 static u64 get_cycle(void)
101 ret = read(cycle_fd, &clk, sizeof(u64));
102 BUG_ON(ret != sizeof(u64));
107 static double timeval2double(struct timeval *ts)
109 return (double)ts->tv_sec +
110 (double)ts->tv_usec / (double)1000000;
113 static void alloc_mem(void **dst, void **src, size_t length)
115 *dst = zalloc(length);
117 die("memory allocation failed - maybe length is too large?\n");
119 *src = zalloc(length);
121 die("memory allocation failed - maybe length is too large?\n");
122 /* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */
123 memset(*src, 0, length);
126 static u64 do_memcpy_cycle(memcpy_t fn, size_t len, bool prefault)
128 u64 cycle_start = 0ULL, cycle_end = 0ULL;
129 void *src = NULL, *dst = NULL;
132 alloc_mem(&src, &dst, len);
137 cycle_start = get_cycle();
138 for (i = 0; i < iterations; ++i)
140 cycle_end = get_cycle();
144 return cycle_end - cycle_start;
147 static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
149 struct timeval tv_start, tv_end, tv_diff;
150 void *src = NULL, *dst = NULL;
153 alloc_mem(&src, &dst, len);
158 BUG_ON(gettimeofday(&tv_start, NULL));
159 for (i = 0; i < iterations; ++i)
161 BUG_ON(gettimeofday(&tv_end, NULL));
163 timersub(&tv_end, &tv_start, &tv_diff);
167 return (double)((double)len / timeval2double(&tv_diff));
170 #define pf (no_prefault ? 0 : 1)
172 #define print_bps(x) do { \
174 printf(" %14lf B/Sec", x); \
175 else if (x < K * K) \
176 printf(" %14lfd KB/Sec", x / K); \
177 else if (x < K * K * K) \
178 printf(" %14lf MB/Sec", x / K / K); \
180 printf(" %14lf GB/Sec", x / K / K / K); \
183 int bench_mem_memcpy(int argc, const char **argv,
184 const char *prefix __maybe_unused)
188 double result_bps[2];
191 argc = parse_options(argc, argv, options,
192 bench_mem_memcpy_usage, 0);
194 if (no_prefault && only_prefault) {
195 fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
202 len = (size_t)perf_atoll((char *)length_str);
204 result_cycle[0] = result_cycle[1] = 0ULL;
205 result_bps[0] = result_bps[1] = 0.0;
208 fprintf(stderr, "Invalid length:%s\n", length_str);
212 /* same to without specifying either of prefault and no-prefault */
213 if (only_prefault && no_prefault)
214 only_prefault = no_prefault = false;
216 for (i = 0; routines[i].name; i++) {
217 if (!strcmp(routines[i].name, routine))
220 if (!routines[i].name) {
221 printf("Unknown routine:%s\n", routine);
222 printf("Available routines...\n");
223 for (i = 0; routines[i].name; i++) {
224 printf("\t%s ... %s\n",
225 routines[i].name, routines[i].desc);
230 if (bench_format == BENCH_FORMAT_DEFAULT)
231 printf("# Copying %s Bytes ...\n\n", length_str);
233 if (!only_prefault && !no_prefault) {
234 /* show both of results */
237 do_memcpy_cycle(routines[i].fn, len, false);
239 do_memcpy_cycle(routines[i].fn, len, true);
242 do_memcpy_gettimeofday(routines[i].fn,
245 do_memcpy_gettimeofday(routines[i].fn,
251 do_memcpy_cycle(routines[i].fn,
255 do_memcpy_gettimeofday(routines[i].fn,
260 switch (bench_format) {
261 case BENCH_FORMAT_DEFAULT:
262 if (!only_prefault && !no_prefault) {
264 printf(" %14lf Cycle/Byte\n",
265 (double)result_cycle[0]
267 printf(" %14lf Cycle/Byte (with prefault)\n",
268 (double)result_cycle[1]
271 print_bps(result_bps[0]);
273 print_bps(result_bps[1]);
274 printf(" (with prefault)\n");
278 printf(" %14lf Cycle/Byte",
279 (double)result_cycle[pf]
282 print_bps(result_bps[pf]);
284 printf("%s\n", only_prefault ? " (with prefault)" : "");
287 case BENCH_FORMAT_SIMPLE:
288 if (!only_prefault && !no_prefault) {
291 (double)result_cycle[0] / (double)len,
292 (double)result_cycle[1] / (double)len);
295 result_bps[0], result_bps[1]);
299 printf("%lf\n", (double)result_cycle[pf]
302 printf("%lf\n", result_bps[pf]);
306 /* reaching this means there's some disaster: */
307 die("unknown format: %d\n", bench_format);