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/string.h"
14 #include "../util/header.h"
25 static const char *length_str
= "1MB";
26 static const char *routine
= "default";
27 static int use_clock
= 0;
30 static const struct option options
[] = {
31 OPT_STRING('l', "length", &length_str
, "1MB",
32 "Specify length of memory to copy. "
33 "available unit: B, MB, GB (upper and lower)"),
34 OPT_STRING('r', "routine", &routine
, "default",
35 "Specify routine to copy"),
36 OPT_BOOLEAN('c', "clock", &use_clock
,
37 "Use CPU clock for measuring"),
44 void * (*fn
)(void *dst
, const void *src
, size_t len
);
47 struct routine routines
[] = {
49 "Default memcpy() provided by glibc",
56 static const char * const bench_mem_memcpy_usage
[] = {
57 "perf bench mem memcpy <options>",
61 static struct perf_event_attr clock_attr
= {
62 .type
= PERF_TYPE_HARDWARE
,
63 .config
= PERF_COUNT_HW_CPU_CYCLES
66 static void init_clock(void)
68 clock_fd
= sys_perf_event_open(&clock_attr
, getpid(), -1, -1, 0);
70 if (clock_fd
< 0 && errno
== ENOSYS
)
71 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
76 static u64
get_clock(void)
81 ret
= read(clock_fd
, &clk
, sizeof(u64
));
82 BUG_ON(ret
!= sizeof(u64
));
87 static double timeval2double(struct timeval
*ts
)
89 return (double)ts
->tv_sec
+
90 (double)ts
->tv_usec
/ (double)1000000;
93 int bench_mem_memcpy(int argc
, const char **argv
,
94 const char *prefix __used
)
100 struct timeval tv_start
, tv_end
, tv_diff
;
101 u64 clock_start
, clock_end
, clock_diff
;
103 clock_start
= clock_end
= clock_diff
= 0ULL;
104 argc
= parse_options(argc
, argv
, options
,
105 bench_mem_memcpy_usage
, 0);
109 length
= (size_t)perf_atoll((char *)length_str
);
111 if ((s64
)length
<= 0) {
112 fprintf(stderr
, "Invalid length:%s\n", length_str
);
116 for (i
= 0; routines
[i
].name
; i
++) {
117 if (!strcmp(routines
[i
].name
, routine
))
120 if (!routines
[i
].name
) {
121 printf("Unknown routine:%s\n", routine
);
122 printf("Available routines...\n");
123 for (i
= 0; routines
[i
].name
; i
++) {
124 printf("\t%s ... %s\n",
125 routines
[i
].name
, routines
[i
].desc
);
130 dst
= zalloc(length
);
132 die("memory allocation failed - maybe length is too large?\n");
134 src
= zalloc(length
);
136 die("memory allocation failed - maybe length is too large?\n");
138 if (bench_format
== BENCH_FORMAT_DEFAULT
) {
139 printf("# Copying %s Bytes from %p to %p ...\n\n",
140 length_str
, src
, dst
);
145 clock_start
= get_clock();
147 BUG_ON(gettimeofday(&tv_start
, NULL
));
150 routines
[i
].fn(dst
, src
, length
);
153 clock_end
= get_clock();
154 clock_diff
= clock_end
- clock_start
;
156 BUG_ON(gettimeofday(&tv_end
, NULL
));
157 timersub(&tv_end
, &tv_start
, &tv_diff
);
158 bps
= (double)((double)length
/ timeval2double(&tv_diff
));
161 switch (bench_format
) {
162 case BENCH_FORMAT_DEFAULT
:
164 printf(" %14lf Clock/Byte\n",
165 (double)clock_diff
/ (double)length
);
168 printf(" %14lf B/Sec\n", bps
);
169 else if (bps
< K
* K
)
170 printf(" %14lfd KB/Sec\n", bps
/ 1024);
171 else if (bps
< K
* K
* K
)
172 printf(" %14lf MB/Sec\n", bps
/ 1024 / 1024);
174 printf(" %14lf GB/Sec\n",
175 bps
/ 1024 / 1024 / 1024);
179 case BENCH_FORMAT_SIMPLE
:
182 (double)clock_diff
/ (double)length
);
184 printf("%lf\n", bps
);
187 /* reaching this means there's some disaster: */
188 die("unknown format: %d\n", bench_format
);