Commit | Line | Data |
---|---|---|
827f3b49 HM |
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 | */ | |
827f3b49 HM |
8 | |
9 | #include "../perf.h" | |
10 | #include "../util/util.h" | |
11 | #include "../util/parse-options.h" | |
827f3b49 HM |
12 | #include "../util/header.h" |
13 | #include "bench.h" | |
49ce8fc6 | 14 | #include "mem-memcpy-arch.h" |
827f3b49 HM |
15 | |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <sys/time.h> | |
20 | #include <errno.h> | |
21 | ||
22 | #define K 1024 | |
23 | ||
12eac0bf HM |
24 | static const char *length_str = "1MB"; |
25 | static const char *routine = "default"; | |
e3e877e7 | 26 | static int iterations = 1; |
17d7a112 HM |
27 | static bool use_cycle; |
28 | static int cycle_fd; | |
49ce8fc6 HM |
29 | static bool only_prefault; |
30 | static bool no_prefault; | |
827f3b49 HM |
31 | |
32 | static const struct option options[] = { | |
33 | OPT_STRING('l', "length", &length_str, "1MB", | |
34 | "Specify length of memory to copy. " | |
08942f6d | 35 | "Available units: B, KB, MB, GB and TB (upper and lower)"), |
827f3b49 HM |
36 | OPT_STRING('r', "routine", &routine, "default", |
37 | "Specify routine to copy"), | |
e3e877e7 JB |
38 | OPT_INTEGER('i', "iterations", &iterations, |
39 | "repeat memcpy() invocation this number of times"), | |
17d7a112 | 40 | OPT_BOOLEAN('c', "cycle", &use_cycle, |
08942f6d | 41 | "Use cycles event instead of gettimeofday() for measuring"), |
49ce8fc6 HM |
42 | OPT_BOOLEAN('o', "only-prefault", &only_prefault, |
43 | "Show only the result with page faults before memcpy()"), | |
44 | OPT_BOOLEAN('n', "no-prefault", &no_prefault, | |
45 | "Show only the result without page faults before memcpy()"), | |
827f3b49 HM |
46 | OPT_END() |
47 | }; | |
48 | ||
49ce8fc6 HM |
49 | typedef void *(*memcpy_t)(void *, const void *, size_t); |
50 | ||
827f3b49 HM |
51 | struct routine { |
52 | const char *name; | |
53 | const char *desc; | |
49ce8fc6 | 54 | memcpy_t fn; |
827f3b49 HM |
55 | }; |
56 | ||
57 | struct routine routines[] = { | |
58 | { "default", | |
59 | "Default memcpy() provided by glibc", | |
60 | memcpy }, | |
89fe808a | 61 | #ifdef HAVE_ARCH_X86_64_SUPPORT |
49ce8fc6 HM |
62 | |
63 | #define MEMCPY_FN(fn, name, desc) { name, desc, fn }, | |
64 | #include "mem-memcpy-x86-64-asm-def.h" | |
65 | #undef MEMCPY_FN | |
66 | ||
67 | #endif | |
68 | ||
827f3b49 HM |
69 | { NULL, |
70 | NULL, | |
71 | NULL } | |
72 | }; | |
73 | ||
74 | static const char * const bench_mem_memcpy_usage[] = { | |
75 | "perf bench mem memcpy <options>", | |
76 | NULL | |
77 | }; | |
78 | ||
17d7a112 | 79 | static struct perf_event_attr cycle_attr = { |
12eac0bf HM |
80 | .type = PERF_TYPE_HARDWARE, |
81 | .config = PERF_COUNT_HW_CPU_CYCLES | |
827f3b49 HM |
82 | }; |
83 | ||
17d7a112 | 84 | static void init_cycle(void) |
827f3b49 | 85 | { |
17d7a112 | 86 | cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, 0); |
12eac0bf | 87 | |
17d7a112 | 88 | if (cycle_fd < 0 && errno == ENOSYS) |
12eac0bf HM |
89 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
90 | else | |
17d7a112 | 91 | BUG_ON(cycle_fd < 0); |
827f3b49 HM |
92 | } |
93 | ||
17d7a112 | 94 | static u64 get_cycle(void) |
827f3b49 HM |
95 | { |
96 | int ret; | |
97 | u64 clk; | |
98 | ||
17d7a112 | 99 | ret = read(cycle_fd, &clk, sizeof(u64)); |
827f3b49 HM |
100 | BUG_ON(ret != sizeof(u64)); |
101 | ||
102 | return clk; | |
103 | } | |
104 | ||
105 | static double timeval2double(struct timeval *ts) | |
106 | { | |
107 | return (double)ts->tv_sec + | |
108 | (double)ts->tv_usec / (double)1000000; | |
109 | } | |
110 | ||
49ce8fc6 HM |
111 | static void alloc_mem(void **dst, void **src, size_t length) |
112 | { | |
113 | *dst = zalloc(length); | |
13966721 | 114 | if (!*dst) |
49ce8fc6 HM |
115 | die("memory allocation failed - maybe length is too large?\n"); |
116 | ||
117 | *src = zalloc(length); | |
13966721 | 118 | if (!*src) |
49ce8fc6 | 119 | die("memory allocation failed - maybe length is too large?\n"); |
a198996c AK |
120 | /* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */ |
121 | memset(*src, 0, length); | |
49ce8fc6 HM |
122 | } |
123 | ||
17d7a112 | 124 | static u64 do_memcpy_cycle(memcpy_t fn, size_t len, bool prefault) |
49ce8fc6 | 125 | { |
17d7a112 | 126 | u64 cycle_start = 0ULL, cycle_end = 0ULL; |
49ce8fc6 | 127 | void *src = NULL, *dst = NULL; |
e3e877e7 | 128 | int i; |
49ce8fc6 HM |
129 | |
130 | alloc_mem(&src, &dst, len); | |
131 | ||
132 | if (prefault) | |
133 | fn(dst, src, len); | |
134 | ||
17d7a112 | 135 | cycle_start = get_cycle(); |
e3e877e7 JB |
136 | for (i = 0; i < iterations; ++i) |
137 | fn(dst, src, len); | |
17d7a112 | 138 | cycle_end = get_cycle(); |
49ce8fc6 HM |
139 | |
140 | free(src); | |
141 | free(dst); | |
17d7a112 | 142 | return cycle_end - cycle_start; |
49ce8fc6 HM |
143 | } |
144 | ||
145 | static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault) | |
146 | { | |
147 | struct timeval tv_start, tv_end, tv_diff; | |
148 | void *src = NULL, *dst = NULL; | |
e3e877e7 | 149 | int i; |
49ce8fc6 HM |
150 | |
151 | alloc_mem(&src, &dst, len); | |
152 | ||
153 | if (prefault) | |
154 | fn(dst, src, len); | |
155 | ||
156 | BUG_ON(gettimeofday(&tv_start, NULL)); | |
e3e877e7 JB |
157 | for (i = 0; i < iterations; ++i) |
158 | fn(dst, src, len); | |
49ce8fc6 HM |
159 | BUG_ON(gettimeofday(&tv_end, NULL)); |
160 | ||
161 | timersub(&tv_end, &tv_start, &tv_diff); | |
162 | ||
163 | free(src); | |
164 | free(dst); | |
165 | return (double)((double)len / timeval2double(&tv_diff)); | |
166 | } | |
167 | ||
168 | #define pf (no_prefault ? 0 : 1) | |
169 | ||
170 | #define print_bps(x) do { \ | |
171 | if (x < K) \ | |
172 | printf(" %14lf B/Sec", x); \ | |
173 | else if (x < K * K) \ | |
174 | printf(" %14lfd KB/Sec", x / K); \ | |
175 | else if (x < K * K * K) \ | |
176 | printf(" %14lf MB/Sec", x / K / K); \ | |
177 | else \ | |
178 | printf(" %14lf GB/Sec", x / K / K / K); \ | |
179 | } while (0) | |
180 | ||
827f3b49 | 181 | int bench_mem_memcpy(int argc, const char **argv, |
1d037ca1 | 182 | const char *prefix __maybe_unused) |
827f3b49 HM |
183 | { |
184 | int i; | |
49ce8fc6 HM |
185 | size_t len; |
186 | double result_bps[2]; | |
17d7a112 | 187 | u64 result_cycle[2]; |
827f3b49 | 188 | |
827f3b49 HM |
189 | argc = parse_options(argc, argv, options, |
190 | bench_mem_memcpy_usage, 0); | |
191 | ||
424e9634 DB |
192 | if (no_prefault && only_prefault) { |
193 | fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n"); | |
194 | return 1; | |
195 | } | |
196 | ||
17d7a112 HM |
197 | if (use_cycle) |
198 | init_cycle(); | |
49ce8fc6 HM |
199 | |
200 | len = (size_t)perf_atoll((char *)length_str); | |
12eac0bf | 201 | |
17d7a112 | 202 | result_cycle[0] = result_cycle[1] = 0ULL; |
49ce8fc6 HM |
203 | result_bps[0] = result_bps[1] = 0.0; |
204 | ||
205 | if ((s64)len <= 0) { | |
827f3b49 HM |
206 | fprintf(stderr, "Invalid length:%s\n", length_str); |
207 | return 1; | |
208 | } | |
209 | ||
49ce8fc6 HM |
210 | /* same to without specifying either of prefault and no-prefault */ |
211 | if (only_prefault && no_prefault) | |
212 | only_prefault = no_prefault = false; | |
213 | ||
827f3b49 HM |
214 | for (i = 0; routines[i].name; i++) { |
215 | if (!strcmp(routines[i].name, routine)) | |
216 | break; | |
217 | } | |
218 | if (!routines[i].name) { | |
219 | printf("Unknown routine:%s\n", routine); | |
220 | printf("Available routines...\n"); | |
221 | for (i = 0; routines[i].name; i++) { | |
222 | printf("\t%s ... %s\n", | |
223 | routines[i].name, routines[i].desc); | |
224 | } | |
225 | return 1; | |
226 | } | |
227 | ||
49ce8fc6 HM |
228 | if (bench_format == BENCH_FORMAT_DEFAULT) |
229 | printf("# Copying %s Bytes ...\n\n", length_str); | |
827f3b49 | 230 | |
49ce8fc6 HM |
231 | if (!only_prefault && !no_prefault) { |
232 | /* show both of results */ | |
17d7a112 HM |
233 | if (use_cycle) { |
234 | result_cycle[0] = | |
235 | do_memcpy_cycle(routines[i].fn, len, false); | |
236 | result_cycle[1] = | |
237 | do_memcpy_cycle(routines[i].fn, len, true); | |
49ce8fc6 HM |
238 | } else { |
239 | result_bps[0] = | |
240 | do_memcpy_gettimeofday(routines[i].fn, | |
241 | len, false); | |
242 | result_bps[1] = | |
243 | do_memcpy_gettimeofday(routines[i].fn, | |
244 | len, true); | |
245 | } | |
827f3b49 | 246 | } else { |
17d7a112 HM |
247 | if (use_cycle) { |
248 | result_cycle[pf] = | |
249 | do_memcpy_cycle(routines[i].fn, | |
49ce8fc6 HM |
250 | len, only_prefault); |
251 | } else { | |
252 | result_bps[pf] = | |
253 | do_memcpy_gettimeofday(routines[i].fn, | |
254 | len, only_prefault); | |
255 | } | |
827f3b49 HM |
256 | } |
257 | ||
258 | switch (bench_format) { | |
259 | case BENCH_FORMAT_DEFAULT: | |
49ce8fc6 | 260 | if (!only_prefault && !no_prefault) { |
17d7a112 HM |
261 | if (use_cycle) { |
262 | printf(" %14lf Cycle/Byte\n", | |
263 | (double)result_cycle[0] | |
49ce8fc6 | 264 | / (double)len); |
17d7a112 HM |
265 | printf(" %14lf Cycle/Byte (with prefault)\n", |
266 | (double)result_cycle[1] | |
49ce8fc6 HM |
267 | / (double)len); |
268 | } else { | |
269 | print_bps(result_bps[0]); | |
270 | printf("\n"); | |
271 | print_bps(result_bps[1]); | |
272 | printf(" (with prefault)\n"); | |
827f3b49 | 273 | } |
49ce8fc6 | 274 | } else { |
17d7a112 HM |
275 | if (use_cycle) { |
276 | printf(" %14lf Cycle/Byte", | |
277 | (double)result_cycle[pf] | |
49ce8fc6 HM |
278 | / (double)len); |
279 | } else | |
280 | print_bps(result_bps[pf]); | |
281 | ||
282 | printf("%s\n", only_prefault ? " (with prefault)" : ""); | |
827f3b49 HM |
283 | } |
284 | break; | |
285 | case BENCH_FORMAT_SIMPLE: | |
49ce8fc6 | 286 | if (!only_prefault && !no_prefault) { |
17d7a112 | 287 | if (use_cycle) { |
49ce8fc6 | 288 | printf("%lf %lf\n", |
17d7a112 HM |
289 | (double)result_cycle[0] / (double)len, |
290 | (double)result_cycle[1] / (double)len); | |
49ce8fc6 HM |
291 | } else { |
292 | printf("%lf %lf\n", | |
293 | result_bps[0], result_bps[1]); | |
294 | } | |
295 | } else { | |
17d7a112 HM |
296 | if (use_cycle) { |
297 | printf("%lf\n", (double)result_cycle[pf] | |
49ce8fc6 HM |
298 | / (double)len); |
299 | } else | |
300 | printf("%lf\n", result_bps[pf]); | |
301 | } | |
827f3b49 HM |
302 | break; |
303 | default: | |
12eac0bf HM |
304 | /* reaching this means there's some disaster: */ |
305 | die("unknown format: %d\n", bench_format); | |
827f3b49 HM |
306 | break; |
307 | } | |
308 | ||
309 | return 0; | |
310 | } |