blob: 6ad2b1c6b27b1d5b1961963df14676939403e14b [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 */
8#include <ctype.h>
9
10#include "../perf.h"
11#include "../util/util.h"
12#include "../util/parse-options.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090013#include "../util/header.h"
14#include "bench.h"
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090015#include "mem-memcpy-arch.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090016
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
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090025static const char *length_str = "1MB";
26static const char *routine = "default";
Jan Beuliche3e877e2012-01-18 13:29:59 +000027static int iterations = 1;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090028static bool use_clock;
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090029static int clock_fd;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090030static bool only_prefault;
31static bool no_prefault;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090032
33static const struct option options[] = {
34 OPT_STRING('l', "length", &length_str, "1MB",
35 "Specify length of memory to copy. "
36 "available unit: B, MB, GB (upper and lower)"),
37 OPT_STRING('r', "routine", &routine, "default",
38 "Specify routine to copy"),
Jan Beuliche3e877e2012-01-18 13:29:59 +000039 OPT_INTEGER('i', "iterations", &iterations,
40 "repeat memcpy() invocation this number of times"),
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090041 OPT_BOOLEAN('c', "clock", &use_clock,
42 "Use CPU clock for measuring"),
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090043 OPT_BOOLEAN('o', "only-prefault", &only_prefault,
44 "Show only the result with page faults before memcpy()"),
45 OPT_BOOLEAN('n', "no-prefault", &no_prefault,
46 "Show only the result without page faults before memcpy()"),
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090047 OPT_END()
48};
49
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090050typedef void *(*memcpy_t)(void *, const void *, size_t);
51
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090052struct routine {
53 const char *name;
54 const char *desc;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090055 memcpy_t fn;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090056};
57
58struct routine routines[] = {
59 { "default",
60 "Default memcpy() provided by glibc",
61 memcpy },
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090062#ifdef ARCH_X86_64
63
64#define MEMCPY_FN(fn, name, desc) { name, desc, fn },
65#include "mem-memcpy-x86-64-asm-def.h"
66#undef MEMCPY_FN
67
68#endif
69
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090070 { NULL,
71 NULL,
72 NULL }
73};
74
75static const char * const bench_mem_memcpy_usage[] = {
76 "perf bench mem memcpy <options>",
77 NULL
78};
79
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090080static struct perf_event_attr clock_attr = {
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090081 .type = PERF_TYPE_HARDWARE,
82 .config = PERF_COUNT_HW_CPU_CYCLES
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090083};
84
85static void init_clock(void)
86{
87 clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090088
89 if (clock_fd < 0 && errno == ENOSYS)
90 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
91 else
92 BUG_ON(clock_fd < 0);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090093}
94
95static u64 get_clock(void)
96{
97 int ret;
98 u64 clk;
99
100 ret = read(clock_fd, &clk, sizeof(u64));
101 BUG_ON(ret != sizeof(u64));
102
103 return clk;
104}
105
106static double timeval2double(struct timeval *ts)
107{
108 return (double)ts->tv_sec +
109 (double)ts->tv_usec / (double)1000000;
110}
111
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900112static void alloc_mem(void **dst, void **src, size_t length)
113{
114 *dst = zalloc(length);
115 if (!dst)
116 die("memory allocation failed - maybe length is too large?\n");
117
118 *src = zalloc(length);
119 if (!src)
120 die("memory allocation failed - maybe length is too large?\n");
121}
122
123static u64 do_memcpy_clock(memcpy_t fn, size_t len, bool prefault)
124{
125 u64 clock_start = 0ULL, clock_end = 0ULL;
126 void *src = NULL, *dst = NULL;
Jan Beuliche3e877e2012-01-18 13:29:59 +0000127 int i;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900128
129 alloc_mem(&src, &dst, len);
130
131 if (prefault)
132 fn(dst, src, len);
133
134 clock_start = get_clock();
Jan Beuliche3e877e2012-01-18 13:29:59 +0000135 for (i = 0; i < iterations; ++i)
136 fn(dst, src, len);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900137 clock_end = get_clock();
138
139 free(src);
140 free(dst);
141 return clock_end - clock_start;
142}
143
144static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
145{
146 struct timeval tv_start, tv_end, tv_diff;
147 void *src = NULL, *dst = NULL;
Jan Beuliche3e877e2012-01-18 13:29:59 +0000148 int i;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900149
150 alloc_mem(&src, &dst, len);
151
152 if (prefault)
153 fn(dst, src, len);
154
155 BUG_ON(gettimeofday(&tv_start, NULL));
Jan Beuliche3e877e2012-01-18 13:29:59 +0000156 for (i = 0; i < iterations; ++i)
157 fn(dst, src, len);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900158 BUG_ON(gettimeofday(&tv_end, NULL));
159
160 timersub(&tv_end, &tv_start, &tv_diff);
161
162 free(src);
163 free(dst);
164 return (double)((double)len / timeval2double(&tv_diff));
165}
166
167#define pf (no_prefault ? 0 : 1)
168
169#define print_bps(x) do { \
170 if (x < K) \
171 printf(" %14lf B/Sec", x); \
172 else if (x < K * K) \
173 printf(" %14lfd KB/Sec", x / K); \
174 else if (x < K * K * K) \
175 printf(" %14lf MB/Sec", x / K / K); \
176 else \
177 printf(" %14lf GB/Sec", x / K / K / K); \
178 } while (0)
179
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900180int bench_mem_memcpy(int argc, const char **argv,
181 const char *prefix __used)
182{
183 int i;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900184 size_t len;
185 double result_bps[2];
186 u64 result_clock[2];
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900187
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900188 argc = parse_options(argc, argv, options,
189 bench_mem_memcpy_usage, 0);
190
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900191 if (use_clock)
192 init_clock();
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900193
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900194 len = (size_t)perf_atoll((char *)length_str);
195
196 result_clock[0] = result_clock[1] = 0ULL;
197 result_bps[0] = result_bps[1] = 0.0;
198
199 if ((s64)len <= 0) {
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900200 fprintf(stderr, "Invalid length:%s\n", length_str);
201 return 1;
202 }
203
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900204 /* same to without specifying either of prefault and no-prefault */
205 if (only_prefault && no_prefault)
206 only_prefault = no_prefault = false;
207
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900208 for (i = 0; routines[i].name; i++) {
209 if (!strcmp(routines[i].name, routine))
210 break;
211 }
212 if (!routines[i].name) {
213 printf("Unknown routine:%s\n", routine);
214 printf("Available routines...\n");
215 for (i = 0; routines[i].name; i++) {
216 printf("\t%s ... %s\n",
217 routines[i].name, routines[i].desc);
218 }
219 return 1;
220 }
221
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900222 if (bench_format == BENCH_FORMAT_DEFAULT)
223 printf("# Copying %s Bytes ...\n\n", length_str);
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900224
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900225 if (!only_prefault && !no_prefault) {
226 /* show both of results */
227 if (use_clock) {
228 result_clock[0] =
229 do_memcpy_clock(routines[i].fn, len, false);
230 result_clock[1] =
231 do_memcpy_clock(routines[i].fn, len, true);
232 } else {
233 result_bps[0] =
234 do_memcpy_gettimeofday(routines[i].fn,
235 len, false);
236 result_bps[1] =
237 do_memcpy_gettimeofday(routines[i].fn,
238 len, true);
239 }
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900240 } else {
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900241 if (use_clock) {
242 result_clock[pf] =
243 do_memcpy_clock(routines[i].fn,
244 len, only_prefault);
245 } else {
246 result_bps[pf] =
247 do_memcpy_gettimeofday(routines[i].fn,
248 len, only_prefault);
249 }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900250 }
251
252 switch (bench_format) {
253 case BENCH_FORMAT_DEFAULT:
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900254 if (!only_prefault && !no_prefault) {
255 if (use_clock) {
256 printf(" %14lf Clock/Byte\n",
257 (double)result_clock[0]
258 / (double)len);
259 printf(" %14lf Clock/Byte (with prefault)\n",
260 (double)result_clock[1]
261 / (double)len);
262 } else {
263 print_bps(result_bps[0]);
264 printf("\n");
265 print_bps(result_bps[1]);
266 printf(" (with prefault)\n");
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900267 }
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900268 } else {
269 if (use_clock) {
270 printf(" %14lf Clock/Byte",
271 (double)result_clock[pf]
272 / (double)len);
273 } else
274 print_bps(result_bps[pf]);
275
276 printf("%s\n", only_prefault ? " (with prefault)" : "");
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900277 }
278 break;
279 case BENCH_FORMAT_SIMPLE:
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900280 if (!only_prefault && !no_prefault) {
281 if (use_clock) {
282 printf("%lf %lf\n",
283 (double)result_clock[0] / (double)len,
284 (double)result_clock[1] / (double)len);
285 } else {
286 printf("%lf %lf\n",
287 result_bps[0], result_bps[1]);
288 }
289 } else {
290 if (use_clock) {
291 printf("%lf\n", (double)result_clock[pf]
292 / (double)len);
293 } else
294 printf("%lf\n", result_bps[pf]);
295 }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900296 break;
297 default:
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900298 /* reaching this means there's some disaster: */
299 die("unknown format: %d\n", bench_format);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900300 break;
301 }
302
303 return 0;
304}