4 * memcpy: Simple memory copy in various ways
6 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
11 #include "../util/util.h"
12 #include "../util/parse-options.h"
13 #include "../util/header.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_clock
;
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 unit: B, MB, GB (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', "clock", &use_clock
,
42 "Use CPU clock 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",
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 clock_attr
= {
81 .type
= PERF_TYPE_HARDWARE
,
82 .config
= PERF_COUNT_HW_CPU_CYCLES
85 static void init_clock(void)
87 clock_fd
= sys_perf_event_open(&clock_attr
, getpid(), -1, -1, 0);
89 if (clock_fd
< 0 && errno
== ENOSYS
)
90 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
95 static u64
get_clock(void)
100 ret
= read(clock_fd
, &clk
, sizeof(u64
));
101 BUG_ON(ret
!= sizeof(u64
));
106 static double timeval2double(struct timeval
*ts
)
108 return (double)ts
->tv_sec
+
109 (double)ts
->tv_usec
/ (double)1000000;
112 static void alloc_mem(void **dst
, void **src
, size_t length
)
114 *dst
= zalloc(length
);
116 die("memory allocation failed - maybe length is too large?\n");
118 *src
= zalloc(length
);
120 die("memory allocation failed - maybe length is too large?\n");
123 static u64
do_memcpy_clock(memcpy_t fn
, size_t len
, bool prefault
)
125 u64 clock_start
= 0ULL, clock_end
= 0ULL;
126 void *src
= NULL
, *dst
= NULL
;
129 alloc_mem(&src
, &dst
, len
);
134 clock_start
= get_clock();
135 for (i
= 0; i
< iterations
; ++i
)
137 clock_end
= get_clock();
141 return clock_end
- clock_start
;
144 static double do_memcpy_gettimeofday(memcpy_t fn
, size_t len
, bool prefault
)
146 struct timeval tv_start
, tv_end
, tv_diff
;
147 void *src
= NULL
, *dst
= NULL
;
150 alloc_mem(&src
, &dst
, len
);
155 BUG_ON(gettimeofday(&tv_start
, NULL
));
156 for (i
= 0; i
< iterations
; ++i
)
158 BUG_ON(gettimeofday(&tv_end
, NULL
));
160 timersub(&tv_end
, &tv_start
, &tv_diff
);
164 return (double)((double)len
/ timeval2double(&tv_diff
));
167 #define pf (no_prefault ? 0 : 1)
169 #define print_bps(x) do { \
171 printf(" %14lf B/Sec", x); \
172 else if (x < K * K) \
173 printf(" %14lfd KB/Sec", x / K); \
174 else if (x < K * K * K) \
175 printf(" %14lf MB/Sec", x / K / K); \
177 printf(" %14lf GB/Sec", x / K / K / K); \
180 int bench_mem_memcpy(int argc
, const char **argv
,
181 const char *prefix __used
)
185 double result_bps
[2];
188 argc
= parse_options(argc
, argv
, options
,
189 bench_mem_memcpy_usage
, 0);
194 len
= (size_t)perf_atoll((char *)length_str
);
196 result_clock
[0] = result_clock
[1] = 0ULL;
197 result_bps
[0] = result_bps
[1] = 0.0;
200 fprintf(stderr
, "Invalid length:%s\n", length_str
);
204 /* same to without specifying either of prefault and no-prefault */
205 if (only_prefault
&& no_prefault
)
206 only_prefault
= no_prefault
= false;
208 for (i
= 0; routines
[i
].name
; i
++) {
209 if (!strcmp(routines
[i
].name
, routine
))
212 if (!routines
[i
].name
) {
213 printf("Unknown routine:%s\n", routine
);
214 printf("Available routines...\n");
215 for (i
= 0; routines
[i
].name
; i
++) {
216 printf("\t%s ... %s\n",
217 routines
[i
].name
, routines
[i
].desc
);
222 if (bench_format
== BENCH_FORMAT_DEFAULT
)
223 printf("# Copying %s Bytes ...\n\n", length_str
);
225 if (!only_prefault
&& !no_prefault
) {
226 /* show both of results */
229 do_memcpy_clock(routines
[i
].fn
, len
, false);
231 do_memcpy_clock(routines
[i
].fn
, len
, true);
234 do_memcpy_gettimeofday(routines
[i
].fn
,
237 do_memcpy_gettimeofday(routines
[i
].fn
,
243 do_memcpy_clock(routines
[i
].fn
,
247 do_memcpy_gettimeofday(routines
[i
].fn
,
252 switch (bench_format
) {
253 case BENCH_FORMAT_DEFAULT
:
254 if (!only_prefault
&& !no_prefault
) {
256 printf(" %14lf Clock/Byte\n",
257 (double)result_clock
[0]
259 printf(" %14lf Clock/Byte (with prefault)\n",
260 (double)result_clock
[1]
263 print_bps(result_bps
[0]);
265 print_bps(result_bps
[1]);
266 printf(" (with prefault)\n");
270 printf(" %14lf Clock/Byte",
271 (double)result_clock
[pf
]
274 print_bps(result_bps
[pf
]);
276 printf("%s\n", only_prefault
? " (with prefault)" : "");
279 case BENCH_FORMAT_SIMPLE
:
280 if (!only_prefault
&& !no_prefault
) {
283 (double)result_clock
[0] / (double)len
,
284 (double)result_clock
[1] / (double)len
);
287 result_bps
[0], result_bps
[1]);
291 printf("%lf\n", (double)result_clock
[pf
]
294 printf("%lf\n", result_bps
[pf
]);
298 /* reaching this means there's some disaster: */
299 die("unknown format: %d\n", bench_format
);