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"
24 static const char *length_str
= "1MB";
25 static const char *routine
= "default";
26 static bool use_clock
= false;
29 static const struct option options
[] = {
30 OPT_STRING('l', "length", &length_str
, "1MB",
31 "Specify length of memory to copy. "
32 "available unit: B, MB, GB (upper and lower)"),
33 OPT_STRING('r', "routine", &routine
, "default",
34 "Specify routine to copy"),
35 OPT_BOOLEAN('c', "clock", &use_clock
,
36 "Use CPU clock for measuring"),
43 void * (*fn
)(void *dst
, const void *src
, size_t len
);
46 struct routine routines
[] = {
48 "Default memcpy() provided by glibc",
55 static const char * const bench_mem_memcpy_usage
[] = {
56 "perf bench mem memcpy <options>",
60 static struct perf_event_attr clock_attr
= {
61 .type
= PERF_TYPE_HARDWARE
,
62 .config
= PERF_COUNT_HW_CPU_CYCLES
65 static void init_clock(void)
67 clock_fd
= sys_perf_event_open(&clock_attr
, getpid(), -1, -1, 0);
69 if (clock_fd
< 0 && errno
== ENOSYS
)
70 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
75 static u64
get_clock(void)
80 ret
= read(clock_fd
, &clk
, sizeof(u64
));
81 BUG_ON(ret
!= sizeof(u64
));
86 static double timeval2double(struct timeval
*ts
)
88 return (double)ts
->tv_sec
+
89 (double)ts
->tv_usec
/ (double)1000000;
92 int bench_mem_memcpy(int argc
, const char **argv
,
93 const char *prefix __used
)
99 struct timeval tv_start
, tv_end
, tv_diff
;
100 u64 clock_start
, clock_end
, clock_diff
;
102 clock_start
= clock_end
= clock_diff
= 0ULL;
103 argc
= parse_options(argc
, argv
, options
,
104 bench_mem_memcpy_usage
, 0);
108 length
= (size_t)perf_atoll((char *)length_str
);
110 if ((s64
)length
<= 0) {
111 fprintf(stderr
, "Invalid length:%s\n", length_str
);
115 for (i
= 0; routines
[i
].name
; i
++) {
116 if (!strcmp(routines
[i
].name
, routine
))
119 if (!routines
[i
].name
) {
120 printf("Unknown routine:%s\n", routine
);
121 printf("Available routines...\n");
122 for (i
= 0; routines
[i
].name
; i
++) {
123 printf("\t%s ... %s\n",
124 routines
[i
].name
, routines
[i
].desc
);
129 dst
= zalloc(length
);
131 die("memory allocation failed - maybe length is too large?\n");
133 src
= zalloc(length
);
135 die("memory allocation failed - maybe length is too large?\n");
137 if (bench_format
== BENCH_FORMAT_DEFAULT
) {
138 printf("# Copying %s Bytes from %p to %p ...\n\n",
139 length_str
, src
, dst
);
144 clock_start
= get_clock();
146 BUG_ON(gettimeofday(&tv_start
, NULL
));
149 routines
[i
].fn(dst
, src
, length
);
152 clock_end
= get_clock();
153 clock_diff
= clock_end
- clock_start
;
155 BUG_ON(gettimeofday(&tv_end
, NULL
));
156 timersub(&tv_end
, &tv_start
, &tv_diff
);
157 bps
= (double)((double)length
/ timeval2double(&tv_diff
));
160 switch (bench_format
) {
161 case BENCH_FORMAT_DEFAULT
:
163 printf(" %14lf Clock/Byte\n",
164 (double)clock_diff
/ (double)length
);
167 printf(" %14lf B/Sec\n", bps
);
168 else if (bps
< K
* K
)
169 printf(" %14lfd KB/Sec\n", bps
/ 1024);
170 else if (bps
< K
* K
* K
)
171 printf(" %14lf MB/Sec\n", bps
/ 1024 / 1024);
173 printf(" %14lf GB/Sec\n",
174 bps
/ 1024 / 1024 / 1024);
178 case BENCH_FORMAT_SIMPLE
:
181 (double)clock_diff
/ (double)length
);
183 printf("%lf\n", bps
);
186 /* reaching this means there's some disaster: */
187 die("unknown format: %d\n", bench_format
);