perf bench: Add memcpy() benchmark
[safe/jmp/linux-2.6] / tools / perf / bench / mem-memcpy.c
1 /*
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  */
8 #include <ctype.h>
9
10 #include "../perf.h"
11 #include "../util/util.h"
12 #include "../util/parse-options.h"
13 #include "../util/string.h"
14 #include "../util/header.h"
15 #include "bench.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <errno.h>
22
23 #define K 1024
24
25 static const char *length_str = "1MB";
26 static const char *routine    = "default";
27 static int use_clock = 0;
28
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"),
37         OPT_END()
38 };
39
40 struct routine {
41         const char *name;
42         const char *desc;
43         void * (*fn)(void *dst, const void *src, size_t len);
44 };
45
46 struct routine routines[] = {
47         { "default",
48           "Default memcpy() provided by glibc",
49           memcpy },
50         { NULL,
51           NULL,
52           NULL   }
53 };
54
55 static const char * const bench_mem_memcpy_usage[] = {
56         "perf bench mem memcpy <options>",
57         NULL
58 };
59
60 static int clock_fd;
61
62 static struct perf_event_attr clock_attr = {
63         .type = PERF_TYPE_HARDWARE,
64         .config = PERF_COUNT_HW_CPU_CYCLES
65 };
66
67 static void init_clock(void)
68 {
69         clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
70         BUG_ON(clock_fd < 0);
71 }
72
73 static u64 get_clock(void)
74 {
75         int ret;
76         u64 clk;
77
78         ret = read(clock_fd, &clk, sizeof(u64));
79         BUG_ON(ret != sizeof(u64));
80
81         return clk;
82 }
83
84 static double timeval2double(struct timeval *ts)
85 {
86         return (double)ts->tv_sec +
87                 (double)ts->tv_usec / (double)1000000;
88 }
89
90 int bench_mem_memcpy(int argc, const char **argv,
91                      const char *prefix __used)
92 {
93         int i;
94         void *dst, *src;
95         size_t length;
96         double bps = 0.0;
97         struct timeval tv_start, tv_end, tv_diff;
98         u64 clock_start, clock_end, clock_diff;
99
100         clock_start = clock_end = clock_diff = 0ULL;
101         argc = parse_options(argc, argv, options,
102                              bench_mem_memcpy_usage, 0);
103
104         tv_diff.tv_sec = 0;
105         tv_diff.tv_usec = 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);
109                 return 1;
110         }
111
112         for (i = 0; routines[i].name; i++) {
113                 if (!strcmp(routines[i].name, routine))
114                         break;
115         }
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);
122                 }
123                 return 1;
124         }
125
126         dst = calloc(length, sizeof(char));
127         assert(dst);
128         src = calloc(length, sizeof(char));
129         assert(src);
130
131         if (bench_format == BENCH_FORMAT_DEFAULT) {
132                 printf("# Copying %s Bytes from %p to %p ...\n\n",
133                        length_str, src, dst);
134         }
135
136         if (use_clock) {
137                 init_clock();
138                 clock_start = get_clock();
139         } else
140                 BUG_ON(gettimeofday(&tv_start, NULL));
141
142         routines[i].fn(dst, src, length);
143
144         if (use_clock) {
145                 clock_end = get_clock();
146                 clock_diff = clock_end - clock_start;
147         } else {
148                 BUG_ON(gettimeofday(&tv_end, NULL));
149                 timersub(&tv_end, &tv_start, &tv_diff);
150                 bps = (double)((double)length / timeval2double(&tv_diff));
151         }
152
153         switch (bench_format) {
154         case BENCH_FORMAT_DEFAULT:
155                 if (use_clock) {
156                         printf(" %14lf Clock/Byte\n",
157                                (double)clock_diff / (double)length);
158                 } else {
159                         if (bps < K)
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);
165                         else {
166                                 printf(" %14lf GB/Sec\n",
167                                        bps / 1024 / 1024 / 1024);
168                         }
169                 }
170                 break;
171         case BENCH_FORMAT_SIMPLE:
172                 if (use_clock) {
173                         printf("%14lf\n",
174                                (double)clock_diff / (double)length);
175                 } else
176                         printf("%lf\n", bps);
177                 break;
178         default:
179                 /* reaching here is something disaster */
180                 fprintf(stderr, "Unknown format:%d\n", bench_format);
181                 exit(1);
182                 break;
183         }
184
185         return 0;
186 }