SUNRPC: Report TCP errors to the caller
[linux/fpc-iii.git] / tools / perf / bench / mem-memcpy.c
blobd3dfb7936dcdfd688d20ae680b3b0c0009cdeb55
1 /*
2 * mem-memcpy.c
4 * memcpy: Simple memory copy in various ways
6 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
7 */
9 #include "../perf.h"
10 #include "../util/util.h"
11 #include "../util/parse-options.h"
12 #include "../util/header.h"
13 #include "../util/cloexec.h"
14 #include "bench.h"
15 #include "mem-memcpy-arch.h"
16 #include "mem-memset-arch.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <errno.h>
24 #define K 1024
26 static const char *length_str = "1MB";
27 static const char *routine = "default";
28 static int iterations = 1;
29 static bool use_cycle;
30 static int cycle_fd;
31 static bool only_prefault;
32 static bool no_prefault;
34 static const struct option options[] = {
35 OPT_STRING('l', "length", &length_str, "1MB",
36 "Specify length of memory to copy. "
37 "Available units: B, KB, MB, GB and TB (upper and lower)"),
38 OPT_STRING('r', "routine", &routine, "default",
39 "Specify routine to copy, \"all\" runs all available routines"),
40 OPT_INTEGER('i', "iterations", &iterations,
41 "repeat memcpy() invocation this number of times"),
42 OPT_BOOLEAN('c', "cycle", &use_cycle,
43 "Use cycles event instead of gettimeofday() for measuring"),
44 OPT_BOOLEAN('o', "only-prefault", &only_prefault,
45 "Show only the result with page faults before memcpy()"),
46 OPT_BOOLEAN('n', "no-prefault", &no_prefault,
47 "Show only the result without page faults before memcpy()"),
48 OPT_END()
51 typedef void *(*memcpy_t)(void *, const void *, size_t);
52 typedef void *(*memset_t)(void *, int, size_t);
54 struct routine {
55 const char *name;
56 const char *desc;
57 union {
58 memcpy_t memcpy;
59 memset_t memset;
60 } fn;
63 struct routine memcpy_routines[] = {
64 { .name = "default",
65 .desc = "Default memcpy() provided by glibc",
66 .fn.memcpy = memcpy },
67 #ifdef HAVE_ARCH_X86_64_SUPPORT
69 #define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
70 #include "mem-memcpy-x86-64-asm-def.h"
71 #undef MEMCPY_FN
73 #endif
75 { NULL,
76 NULL,
77 {NULL} }
80 static const char * const bench_mem_memcpy_usage[] = {
81 "perf bench mem memcpy <options>",
82 NULL
85 static struct perf_event_attr cycle_attr = {
86 .type = PERF_TYPE_HARDWARE,
87 .config = PERF_COUNT_HW_CPU_CYCLES
90 static void init_cycle(void)
92 cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1,
93 perf_event_open_cloexec_flag());
95 if (cycle_fd < 0 && errno == ENOSYS)
96 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
97 else
98 BUG_ON(cycle_fd < 0);
101 static u64 get_cycle(void)
103 int ret;
104 u64 clk;
106 ret = read(cycle_fd, &clk, sizeof(u64));
107 BUG_ON(ret != sizeof(u64));
109 return clk;
112 static double timeval2double(struct timeval *ts)
114 return (double)ts->tv_sec +
115 (double)ts->tv_usec / (double)1000000;
118 #define pf (no_prefault ? 0 : 1)
120 #define print_bps(x) do { \
121 if (x < K) \
122 printf(" %14lf B/Sec", x); \
123 else if (x < K * K) \
124 printf(" %14lfd KB/Sec", x / K); \
125 else if (x < K * K * K) \
126 printf(" %14lf MB/Sec", x / K / K); \
127 else \
128 printf(" %14lf GB/Sec", x / K / K / K); \
129 } while (0)
131 struct bench_mem_info {
132 const struct routine *routines;
133 u64 (*do_cycle)(const struct routine *r, size_t len, bool prefault);
134 double (*do_gettimeofday)(const struct routine *r, size_t len, bool prefault);
135 const char *const *usage;
138 static void __bench_mem_routine(struct bench_mem_info *info, int r_idx, size_t len, double totallen)
140 const struct routine *r = &info->routines[r_idx];
141 double result_bps[2];
142 u64 result_cycle[2];
144 result_cycle[0] = result_cycle[1] = 0ULL;
145 result_bps[0] = result_bps[1] = 0.0;
147 printf("Routine %s (%s)\n", r->name, r->desc);
149 if (bench_format == BENCH_FORMAT_DEFAULT)
150 printf("# Copying %s Bytes ...\n\n", length_str);
152 if (!only_prefault && !no_prefault) {
153 /* show both of results */
154 if (use_cycle) {
155 result_cycle[0] = info->do_cycle(r, len, false);
156 result_cycle[1] = info->do_cycle(r, len, true);
157 } else {
158 result_bps[0] = info->do_gettimeofday(r, len, false);
159 result_bps[1] = info->do_gettimeofday(r, len, true);
161 } else {
162 if (use_cycle)
163 result_cycle[pf] = info->do_cycle(r, len, only_prefault);
164 else
165 result_bps[pf] = info->do_gettimeofday(r, len, only_prefault);
168 switch (bench_format) {
169 case BENCH_FORMAT_DEFAULT:
170 if (!only_prefault && !no_prefault) {
171 if (use_cycle) {
172 printf(" %14lf Cycle/Byte\n",
173 (double)result_cycle[0]
174 / totallen);
175 printf(" %14lf Cycle/Byte (with prefault)\n",
176 (double)result_cycle[1]
177 / totallen);
178 } else {
179 print_bps(result_bps[0]);
180 printf("\n");
181 print_bps(result_bps[1]);
182 printf(" (with prefault)\n");
184 } else {
185 if (use_cycle) {
186 printf(" %14lf Cycle/Byte",
187 (double)result_cycle[pf]
188 / totallen);
189 } else
190 print_bps(result_bps[pf]);
192 printf("%s\n", only_prefault ? " (with prefault)" : "");
194 break;
195 case BENCH_FORMAT_SIMPLE:
196 if (!only_prefault && !no_prefault) {
197 if (use_cycle) {
198 printf("%lf %lf\n",
199 (double)result_cycle[0] / totallen,
200 (double)result_cycle[1] / totallen);
201 } else {
202 printf("%lf %lf\n",
203 result_bps[0], result_bps[1]);
205 } else {
206 if (use_cycle) {
207 printf("%lf\n", (double)result_cycle[pf]
208 / totallen);
209 } else
210 printf("%lf\n", result_bps[pf]);
212 break;
213 default:
214 /* reaching this means there's some disaster: */
215 die("unknown format: %d\n", bench_format);
216 break;
220 static int bench_mem_common(int argc, const char **argv,
221 const char *prefix __maybe_unused,
222 struct bench_mem_info *info)
224 int i;
225 size_t len;
226 double totallen;
228 argc = parse_options(argc, argv, options,
229 info->usage, 0);
231 if (no_prefault && only_prefault) {
232 fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
233 return 1;
236 if (use_cycle)
237 init_cycle();
239 len = (size_t)perf_atoll((char *)length_str);
240 totallen = (double)len * iterations;
242 if ((s64)len <= 0) {
243 fprintf(stderr, "Invalid length:%s\n", length_str);
244 return 1;
247 /* same to without specifying either of prefault and no-prefault */
248 if (only_prefault && no_prefault)
249 only_prefault = no_prefault = false;
251 if (!strncmp(routine, "all", 3)) {
252 for (i = 0; info->routines[i].name; i++)
253 __bench_mem_routine(info, i, len, totallen);
254 return 0;
257 for (i = 0; info->routines[i].name; i++) {
258 if (!strcmp(info->routines[i].name, routine))
259 break;
261 if (!info->routines[i].name) {
262 printf("Unknown routine:%s\n", routine);
263 printf("Available routines...\n");
264 for (i = 0; info->routines[i].name; i++) {
265 printf("\t%s ... %s\n",
266 info->routines[i].name, info->routines[i].desc);
268 return 1;
271 __bench_mem_routine(info, i, len, totallen);
273 return 0;
276 static void memcpy_alloc_mem(void **dst, void **src, size_t length)
278 *dst = zalloc(length);
279 if (!*dst)
280 die("memory allocation failed - maybe length is too large?\n");
282 *src = zalloc(length);
283 if (!*src)
284 die("memory allocation failed - maybe length is too large?\n");
285 /* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */
286 memset(*src, 0, length);
289 static u64 do_memcpy_cycle(const struct routine *r, size_t len, bool prefault)
291 u64 cycle_start = 0ULL, cycle_end = 0ULL;
292 void *src = NULL, *dst = NULL;
293 memcpy_t fn = r->fn.memcpy;
294 int i;
296 memcpy_alloc_mem(&dst, &src, len);
298 if (prefault)
299 fn(dst, src, len);
301 cycle_start = get_cycle();
302 for (i = 0; i < iterations; ++i)
303 fn(dst, src, len);
304 cycle_end = get_cycle();
306 free(src);
307 free(dst);
308 return cycle_end - cycle_start;
311 static double do_memcpy_gettimeofday(const struct routine *r, size_t len,
312 bool prefault)
314 struct timeval tv_start, tv_end, tv_diff;
315 memcpy_t fn = r->fn.memcpy;
316 void *src = NULL, *dst = NULL;
317 int i;
319 memcpy_alloc_mem(&dst, &src, len);
321 if (prefault)
322 fn(dst, src, len);
324 BUG_ON(gettimeofday(&tv_start, NULL));
325 for (i = 0; i < iterations; ++i)
326 fn(dst, src, len);
327 BUG_ON(gettimeofday(&tv_end, NULL));
329 timersub(&tv_end, &tv_start, &tv_diff);
331 free(src);
332 free(dst);
333 return (double)(((double)len * iterations) / timeval2double(&tv_diff));
336 int bench_mem_memcpy(int argc, const char **argv,
337 const char *prefix __maybe_unused)
339 struct bench_mem_info info = {
340 .routines = memcpy_routines,
341 .do_cycle = do_memcpy_cycle,
342 .do_gettimeofday = do_memcpy_gettimeofday,
343 .usage = bench_mem_memcpy_usage,
346 return bench_mem_common(argc, argv, prefix, &info);
349 static void memset_alloc_mem(void **dst, size_t length)
351 *dst = zalloc(length);
352 if (!*dst)
353 die("memory allocation failed - maybe length is too large?\n");
356 static u64 do_memset_cycle(const struct routine *r, size_t len, bool prefault)
358 u64 cycle_start = 0ULL, cycle_end = 0ULL;
359 memset_t fn = r->fn.memset;
360 void *dst = NULL;
361 int i;
363 memset_alloc_mem(&dst, len);
365 if (prefault)
366 fn(dst, -1, len);
368 cycle_start = get_cycle();
369 for (i = 0; i < iterations; ++i)
370 fn(dst, i, len);
371 cycle_end = get_cycle();
373 free(dst);
374 return cycle_end - cycle_start;
377 static double do_memset_gettimeofday(const struct routine *r, size_t len,
378 bool prefault)
380 struct timeval tv_start, tv_end, tv_diff;
381 memset_t fn = r->fn.memset;
382 void *dst = NULL;
383 int i;
385 memset_alloc_mem(&dst, len);
387 if (prefault)
388 fn(dst, -1, len);
390 BUG_ON(gettimeofday(&tv_start, NULL));
391 for (i = 0; i < iterations; ++i)
392 fn(dst, i, len);
393 BUG_ON(gettimeofday(&tv_end, NULL));
395 timersub(&tv_end, &tv_start, &tv_diff);
397 free(dst);
398 return (double)(((double)len * iterations) / timeval2double(&tv_diff));
401 static const char * const bench_mem_memset_usage[] = {
402 "perf bench mem memset <options>",
403 NULL
406 static const struct routine memset_routines[] = {
407 { .name ="default",
408 .desc = "Default memset() provided by glibc",
409 .fn.memset = memset },
410 #ifdef HAVE_ARCH_X86_64_SUPPORT
412 #define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
413 #include "mem-memset-x86-64-asm-def.h"
414 #undef MEMSET_FN
416 #endif
418 { .name = NULL,
419 .desc = NULL,
420 .fn.memset = NULL }
423 int bench_mem_memset(int argc, const char **argv,
424 const char *prefix __maybe_unused)
426 struct bench_mem_info info = {
427 .routines = memset_routines,
428 .do_cycle = do_memset_cycle,
429 .do_gettimeofday = do_memset_gettimeofday,
430 .usage = bench_mem_memset_usage,
433 return bench_mem_common(argc, argv, prefix, &info);