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;
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>",
62 static struct perf_event_attr clock_attr = {
63 .type = PERF_TYPE_HARDWARE,
64 .config = PERF_COUNT_HW_CPU_CYCLES
67 static void init_clock(void)
69 clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
73 static u64 get_clock(void)
78 ret = read(clock_fd, &clk, sizeof(u64));
79 BUG_ON(ret != sizeof(u64));
84 static double timeval2double(struct timeval *ts)
86 return (double)ts->tv_sec +
87 (double)ts->tv_usec / (double)1000000;
90 int bench_mem_memcpy(int argc, const char **argv,
91 const char *prefix __used)
97 struct timeval tv_start, tv_end, tv_diff;
98 u64 clock_start, clock_end, clock_diff;
100 clock_start = clock_end = clock_diff = 0ULL;
101 argc = parse_options(argc, argv, options,
102 bench_mem_memcpy_usage, 0);
106 length = (size_t)perf_atoll((char *)length_str);
107 if ((long long int)length <= 0) {
108 fprintf(stderr, "Invalid length:%s\n", length_str);
112 for (i = 0; routines[i].name; i++) {
113 if (!strcmp(routines[i].name, routine))
116 if (!routines[i].name) {
117 printf("Unknown routine:%s\n", routine);
118 printf("Available routines...\n");
119 for (i = 0; routines[i].name; i++) {
120 printf("\t%s ... %s\n",
121 routines[i].name, routines[i].desc);
126 dst = calloc(length, sizeof(char));
128 src = calloc(length, sizeof(char));
131 if (bench_format == BENCH_FORMAT_DEFAULT) {
132 printf("# Copying %s Bytes from %p to %p ...\n\n",
133 length_str, src, dst);
138 clock_start = get_clock();
140 BUG_ON(gettimeofday(&tv_start, NULL));
142 routines[i].fn(dst, src, length);
145 clock_end = get_clock();
146 clock_diff = clock_end - clock_start;
148 BUG_ON(gettimeofday(&tv_end, NULL));
149 timersub(&tv_end, &tv_start, &tv_diff);
150 bps = (double)((double)length / timeval2double(&tv_diff));
153 switch (bench_format) {
154 case BENCH_FORMAT_DEFAULT:
156 printf(" %14lf Clock/Byte\n",
157 (double)clock_diff / (double)length);
160 printf(" %14lf B/Sec\n", bps);
161 else if (bps < K * K)
162 printf(" %14lfd KB/Sec\n", bps / 1024);
163 else if (bps < K * K * K)
164 printf(" %14lf MB/Sec\n", bps / 1024 / 1024);
166 printf(" %14lf GB/Sec\n",
167 bps / 1024 / 1024 / 1024);
171 case BENCH_FORMAT_SIMPLE:
174 (double)clock_diff / (double)length);
176 printf("%lf\n", bps);
179 /* reaching here is something disaster */
180 fprintf(stderr, "Unknown format:%d\n", bench_format);