blob: 93c83e3cb4a7a350a439c691103ad008af5c9585 [file] [log] [blame]
Hitoshi Mitake827f3b42009-11-18 00:20:09 +09001/*
2 * mem-memcpy.c
3 *
4 * memcpy: Simple memory copy in various ways
5 *
6 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
7 */
Hitoshi Mitake827f3b42009-11-18 00:20:09 +09008
9#include "../perf.h"
10#include "../util/util.h"
11#include "../util/parse-options.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090012#include "../util/header.h"
13#include "bench.h"
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090014#include "mem-memcpy-arch.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090015
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/time.h>
20#include <errno.h>
21
22#define K 1024
23
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090024static const char *length_str = "1MB";
25static const char *routine = "default";
Jan Beuliche3e877e2012-01-18 13:29:59 +000026static int iterations = 1;
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090027static bool use_cycle;
28static int cycle_fd;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090029static bool only_prefault;
30static bool no_prefault;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090031
32static const struct option options[] = {
33 OPT_STRING('l', "length", &length_str, "1MB",
34 "Specify length of memory to copy. "
Namhyung Kim08942f62012-06-20 15:08:06 +090035 "Available units: B, KB, MB, GB and TB (upper and lower)"),
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090036 OPT_STRING('r', "routine", &routine, "default",
37 "Specify routine to copy"),
Jan Beuliche3e877e2012-01-18 13:29:59 +000038 OPT_INTEGER('i', "iterations", &iterations,
39 "repeat memcpy() invocation this number of times"),
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090040 OPT_BOOLEAN('c', "cycle", &use_cycle,
Namhyung Kim08942f62012-06-20 15:08:06 +090041 "Use cycles event instead of gettimeofday() for measuring"),
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090042 OPT_BOOLEAN('o', "only-prefault", &only_prefault,
43 "Show only the result with page faults before memcpy()"),
44 OPT_BOOLEAN('n', "no-prefault", &no_prefault,
45 "Show only the result without page faults before memcpy()"),
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090046 OPT_END()
47};
48
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090049typedef void *(*memcpy_t)(void *, const void *, size_t);
50
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090051struct routine {
52 const char *name;
53 const char *desc;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090054 memcpy_t fn;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090055};
56
57struct routine routines[] = {
58 { "default",
59 "Default memcpy() provided by glibc",
60 memcpy },
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090061#ifdef ARCH_X86_64
62
63#define MEMCPY_FN(fn, name, desc) { name, desc, fn },
64#include "mem-memcpy-x86-64-asm-def.h"
65#undef MEMCPY_FN
66
67#endif
68
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090069 { NULL,
70 NULL,
71 NULL }
72};
73
74static const char * const bench_mem_memcpy_usage[] = {
75 "perf bench mem memcpy <options>",
76 NULL
77};
78
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090079static struct perf_event_attr cycle_attr = {
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090080 .type = PERF_TYPE_HARDWARE,
81 .config = PERF_COUNT_HW_CPU_CYCLES
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090082};
83
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090084static void init_cycle(void)
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090085{
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090086 cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, 0);
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090087
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090088 if (cycle_fd < 0 && errno == ENOSYS)
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090089 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
90 else
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090091 BUG_ON(cycle_fd < 0);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090092}
93
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090094static u64 get_cycle(void)
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090095{
96 int ret;
97 u64 clk;
98
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090099 ret = read(cycle_fd, &clk, sizeof(u64));
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900100 BUG_ON(ret != sizeof(u64));
101
102 return clk;
103}
104
105static double timeval2double(struct timeval *ts)
106{
107 return (double)ts->tv_sec +
108 (double)ts->tv_usec / (double)1000000;
109}
110
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900111static void alloc_mem(void **dst, void **src, size_t length)
112{
113 *dst = zalloc(length);
114 if (!dst)
115 die("memory allocation failed - maybe length is too large?\n");
116
117 *src = zalloc(length);
118 if (!src)
119 die("memory allocation failed - maybe length is too large?\n");
120}
121
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900122static u64 do_memcpy_cycle(memcpy_t fn, size_t len, bool prefault)
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900123{
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900124 u64 cycle_start = 0ULL, cycle_end = 0ULL;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900125 void *src = NULL, *dst = NULL;
Jan Beuliche3e877e2012-01-18 13:29:59 +0000126 int i;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900127
128 alloc_mem(&src, &dst, len);
129
130 if (prefault)
131 fn(dst, src, len);
132
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900133 cycle_start = get_cycle();
Jan Beuliche3e877e2012-01-18 13:29:59 +0000134 for (i = 0; i < iterations; ++i)
135 fn(dst, src, len);
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900136 cycle_end = get_cycle();
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900137
138 free(src);
139 free(dst);
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900140 return cycle_end - cycle_start;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900141}
142
143static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
144{
145 struct timeval tv_start, tv_end, tv_diff;
146 void *src = NULL, *dst = NULL;
Jan Beuliche3e877e2012-01-18 13:29:59 +0000147 int i;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900148
149 alloc_mem(&src, &dst, len);
150
151 if (prefault)
152 fn(dst, src, len);
153
154 BUG_ON(gettimeofday(&tv_start, NULL));
Jan Beuliche3e877e2012-01-18 13:29:59 +0000155 for (i = 0; i < iterations; ++i)
156 fn(dst, src, len);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900157 BUG_ON(gettimeofday(&tv_end, NULL));
158
159 timersub(&tv_end, &tv_start, &tv_diff);
160
161 free(src);
162 free(dst);
163 return (double)((double)len / timeval2double(&tv_diff));
164}
165
166#define pf (no_prefault ? 0 : 1)
167
168#define print_bps(x) do { \
169 if (x < K) \
170 printf(" %14lf B/Sec", x); \
171 else if (x < K * K) \
172 printf(" %14lfd KB/Sec", x / K); \
173 else if (x < K * K * K) \
174 printf(" %14lf MB/Sec", x / K / K); \
175 else \
176 printf(" %14lf GB/Sec", x / K / K / K); \
177 } while (0)
178
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900179int bench_mem_memcpy(int argc, const char **argv,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300180 const char *prefix __maybe_unused)
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900181{
182 int i;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900183 size_t len;
184 double result_bps[2];
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900185 u64 result_cycle[2];
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900186
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900187 argc = parse_options(argc, argv, options,
188 bench_mem_memcpy_usage, 0);
189
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900190 if (use_cycle)
191 init_cycle();
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900192
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900193 len = (size_t)perf_atoll((char *)length_str);
194
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900195 result_cycle[0] = result_cycle[1] = 0ULL;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900196 result_bps[0] = result_bps[1] = 0.0;
197
198 if ((s64)len <= 0) {
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900199 fprintf(stderr, "Invalid length:%s\n", length_str);
200 return 1;
201 }
202
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900203 /* same to without specifying either of prefault and no-prefault */
204 if (only_prefault && no_prefault)
205 only_prefault = no_prefault = false;
206
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900207 for (i = 0; routines[i].name; i++) {
208 if (!strcmp(routines[i].name, routine))
209 break;
210 }
211 if (!routines[i].name) {
212 printf("Unknown routine:%s\n", routine);
213 printf("Available routines...\n");
214 for (i = 0; routines[i].name; i++) {
215 printf("\t%s ... %s\n",
216 routines[i].name, routines[i].desc);
217 }
218 return 1;
219 }
220
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900221 if (bench_format == BENCH_FORMAT_DEFAULT)
222 printf("# Copying %s Bytes ...\n\n", length_str);
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900223
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900224 if (!only_prefault && !no_prefault) {
225 /* show both of results */
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900226 if (use_cycle) {
227 result_cycle[0] =
228 do_memcpy_cycle(routines[i].fn, len, false);
229 result_cycle[1] =
230 do_memcpy_cycle(routines[i].fn, len, true);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900231 } else {
232 result_bps[0] =
233 do_memcpy_gettimeofday(routines[i].fn,
234 len, false);
235 result_bps[1] =
236 do_memcpy_gettimeofday(routines[i].fn,
237 len, true);
238 }
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900239 } else {
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900240 if (use_cycle) {
241 result_cycle[pf] =
242 do_memcpy_cycle(routines[i].fn,
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900243 len, only_prefault);
244 } else {
245 result_bps[pf] =
246 do_memcpy_gettimeofday(routines[i].fn,
247 len, only_prefault);
248 }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900249 }
250
251 switch (bench_format) {
252 case BENCH_FORMAT_DEFAULT:
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900253 if (!only_prefault && !no_prefault) {
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900254 if (use_cycle) {
255 printf(" %14lf Cycle/Byte\n",
256 (double)result_cycle[0]
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900257 / (double)len);
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900258 printf(" %14lf Cycle/Byte (with prefault)\n",
259 (double)result_cycle[1]
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900260 / (double)len);
261 } else {
262 print_bps(result_bps[0]);
263 printf("\n");
264 print_bps(result_bps[1]);
265 printf(" (with prefault)\n");
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900266 }
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900267 } else {
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900268 if (use_cycle) {
269 printf(" %14lf Cycle/Byte",
270 (double)result_cycle[pf]
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900271 / (double)len);
272 } else
273 print_bps(result_bps[pf]);
274
275 printf("%s\n", only_prefault ? " (with prefault)" : "");
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900276 }
277 break;
278 case BENCH_FORMAT_SIMPLE:
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900279 if (!only_prefault && !no_prefault) {
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900280 if (use_cycle) {
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900281 printf("%lf %lf\n",
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900282 (double)result_cycle[0] / (double)len,
283 (double)result_cycle[1] / (double)len);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900284 } else {
285 printf("%lf %lf\n",
286 result_bps[0], result_bps[1]);
287 }
288 } else {
Hitoshi Mitake17d7a112012-07-02 22:46:17 +0900289 if (use_cycle) {
290 printf("%lf\n", (double)result_cycle[pf]
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900291 / (double)len);
292 } else
293 printf("%lf\n", result_bps[pf]);
294 }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900295 break;
296 default:
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900297 /* reaching this means there's some disaster: */
298 die("unknown format: %d\n", bench_format);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900299 break;
300 }
301
302 return 0;
303}