perf bench mem: Rename 'routine' to 'routine_str'
[deliverable/linux.git] / tools / perf / bench / mem-functions.c
CommitLineData
827f3b49
HM
1/*
2 * mem-memcpy.c
3 *
13839ec4 4 * Simple memcpy() and memset() benchmarks
827f3b49
HM
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 12#include "../util/header.h"
57480d2c 13#include "../util/cloexec.h"
827f3b49 14#include "bench.h"
49ce8fc6 15#include "mem-memcpy-arch.h"
5bce1a57 16#include "mem-memset-arch.h"
827f3b49
HM
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <sys/time.h>
22#include <errno.h>
23
24#define K 1024
25
12eac0bf 26static const char *length_str = "1MB";
e815e327 27static const char *routine_str = "all";
e3e877e7 28static int iterations = 1;
b14f2d35
IM
29static bool use_cycles;
30static int cycles_fd;
827f3b49
HM
31
32static 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)"),
e815e327
IM
36 OPT_STRING('r', "routine", &routine_str, "all",
37 "Specify the routine to run, \"all\" runs all available routines"),
e3e877e7
JB
38 OPT_INTEGER('i', "iterations", &iterations,
39 "repeat memcpy() invocation this number of times"),
b14f2d35
IM
40 OPT_BOOLEAN('c', "cycles", &use_cycles,
41 "Use a cycles event instead of gettimeofday() to measure performance"),
827f3b49
HM
42 OPT_END()
43};
44
49ce8fc6 45typedef void *(*memcpy_t)(void *, const void *, size_t);
5bce1a57 46typedef void *(*memset_t)(void *, int, size_t);
49ce8fc6 47
827f3b49
HM
48struct routine {
49 const char *name;
50 const char *desc;
308197b9
RV
51 union {
52 memcpy_t memcpy;
5bce1a57 53 memset_t memset;
308197b9 54 } fn;
827f3b49
HM
55};
56
308197b9 57struct routine memcpy_routines[] = {
13839ec4
IM
58 { .name = "default",
59 .desc = "Default memcpy() provided by glibc",
60 .fn.memcpy = memcpy },
49ce8fc6 61
13839ec4
IM
62#ifdef HAVE_ARCH_X86_64_SUPPORT
63# define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
64# include "mem-memcpy-x86-64-asm-def.h"
65# undef MEMCPY_FN
49ce8fc6
HM
66#endif
67
13839ec4 68 { NULL, }
827f3b49
HM
69};
70
71static const char * const bench_mem_memcpy_usage[] = {
72 "perf bench mem memcpy <options>",
73 NULL
74};
75
17d7a112 76static struct perf_event_attr cycle_attr = {
12eac0bf
HM
77 .type = PERF_TYPE_HARDWARE,
78 .config = PERF_COUNT_HW_CPU_CYCLES
827f3b49
HM
79};
80
b14f2d35 81static void init_cycles(void)
827f3b49 82{
b14f2d35 83 cycles_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, perf_event_open_cloexec_flag());
12eac0bf 84
b14f2d35 85 if (cycles_fd < 0 && errno == ENOSYS)
12eac0bf
HM
86 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
87 else
b14f2d35 88 BUG_ON(cycles_fd < 0);
827f3b49
HM
89}
90
b14f2d35 91static u64 get_cycles(void)
827f3b49
HM
92{
93 int ret;
94 u64 clk;
95
b14f2d35 96 ret = read(cycles_fd, &clk, sizeof(u64));
827f3b49
HM
97 BUG_ON(ret != sizeof(u64));
98
99 return clk;
100}
101
102static double timeval2double(struct timeval *ts)
103{
13839ec4 104 return (double)ts->tv_sec + (double)ts->tv_usec / (double)1000000;
827f3b49
HM
105}
106
6db175c7
IM
107#define print_bps(x) do { \
108 if (x < K) \
109 printf(" %14lf B/Sec\n", x); \
110 else if (x < K * K) \
111 printf(" %14lfd KB/Sec\n", x / K); \
112 else if (x < K * K * K) \
113 printf(" %14lf MB/Sec\n", x / K / K); \
114 else \
115 printf(" %14lf GB/Sec\n", x / K / K / K); \
49ce8fc6
HM
116 } while (0)
117
308197b9
RV
118struct bench_mem_info {
119 const struct routine *routines;
b14f2d35 120 u64 (*do_cycles)(const struct routine *r, size_t len);
6db175c7 121 double (*do_gettimeofday)(const struct routine *r, size_t len);
308197b9
RV
122 const char *const *usage;
123};
124
515e23f0 125static void __bench_mem_routine(struct bench_mem_info *info, int r_idx, size_t len, double totallen)
827f3b49 126{
515e23f0 127 const struct routine *r = &info->routines[r_idx];
6db175c7 128 double result_bps = 0.0;
b14f2d35 129 u64 result_cycles = 0;
49ce8fc6 130
dfecb95c 131 printf("Routine %s (%s)\n", r->name, r->desc);
827f3b49 132
49ce8fc6
HM
133 if (bench_format == BENCH_FORMAT_DEFAULT)
134 printf("# Copying %s Bytes ...\n\n", length_str);
827f3b49 135
b14f2d35
IM
136 if (use_cycles) {
137 result_cycles = info->do_cycles(r, len);
827f3b49 138 } else {
6db175c7 139 result_bps = info->do_gettimeofday(r, len);
827f3b49
HM
140 }
141
142 switch (bench_format) {
143 case BENCH_FORMAT_DEFAULT:
b14f2d35
IM
144 if (use_cycles) {
145 printf(" %14lf cycles/Byte\n", (double)result_cycles/totallen);
49ce8fc6 146 } else {
6db175c7 147 print_bps(result_bps);
827f3b49
HM
148 }
149 break;
6db175c7 150
827f3b49 151 case BENCH_FORMAT_SIMPLE:
b14f2d35
IM
152 if (use_cycles) {
153 printf("%lf\n", (double)result_cycles/totallen);
49ce8fc6 154 } else {
6db175c7 155 printf("%lf\n", result_bps);
49ce8fc6 156 }
827f3b49 157 break;
6db175c7 158
827f3b49 159 default:
6db175c7 160 BUG_ON(1);
827f3b49
HM
161 break;
162 }
515e23f0
BP
163}
164
2946f59a 165static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
515e23f0
BP
166{
167 int i;
168 size_t len;
169 double totallen;
170
13839ec4 171 argc = parse_options(argc, argv, options, info->usage, 0);
515e23f0 172
b14f2d35
IM
173 if (use_cycles)
174 init_cycles();
515e23f0
BP
175
176 len = (size_t)perf_atoll((char *)length_str);
177 totallen = (double)len * iterations;
178
179 if ((s64)len <= 0) {
180 fprintf(stderr, "Invalid length:%s\n", length_str);
181 return 1;
182 }
183
e815e327 184 if (!strncmp(routine_str, "all", 3)) {
dfecb95c
BP
185 for (i = 0; info->routines[i].name; i++)
186 __bench_mem_routine(info, i, len, totallen);
187 return 0;
188 }
189
515e23f0 190 for (i = 0; info->routines[i].name; i++) {
e815e327 191 if (!strcmp(info->routines[i].name, routine_str))
515e23f0
BP
192 break;
193 }
194 if (!info->routines[i].name) {
e815e327 195 printf("Unknown routine: %s\n", routine_str);
515e23f0
BP
196 printf("Available routines...\n");
197 for (i = 0; info->routines[i].name; i++) {
198 printf("\t%s ... %s\n",
199 info->routines[i].name, info->routines[i].desc);
200 }
201 return 1;
202 }
203
204 __bench_mem_routine(info, i, len, totallen);
827f3b49
HM
205
206 return 0;
207}
308197b9
RV
208
209static void memcpy_alloc_mem(void **dst, void **src, size_t length)
210{
211 *dst = zalloc(length);
212 if (!*dst)
213 die("memory allocation failed - maybe length is too large?\n");
214
215 *src = zalloc(length);
216 if (!*src)
217 die("memory allocation failed - maybe length is too large?\n");
13839ec4
IM
218
219 /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
308197b9
RV
220 memset(*src, 0, length);
221}
222
b14f2d35 223static u64 do_memcpy_cycles(const struct routine *r, size_t len)
308197b9
RV
224{
225 u64 cycle_start = 0ULL, cycle_end = 0ULL;
226 void *src = NULL, *dst = NULL;
227 memcpy_t fn = r->fn.memcpy;
228 int i;
229
e17fdaea 230 memcpy_alloc_mem(&dst, &src, len);
308197b9 231
6db175c7
IM
232 /*
233 * We prefault the freshly allocated memory range here,
234 * to not measure page fault overhead:
235 */
236 fn(dst, src, len);
308197b9 237
b14f2d35 238 cycle_start = get_cycles();
308197b9
RV
239 for (i = 0; i < iterations; ++i)
240 fn(dst, src, len);
b14f2d35 241 cycle_end = get_cycles();
308197b9
RV
242
243 free(src);
244 free(dst);
245 return cycle_end - cycle_start;
246}
247
6db175c7 248static double do_memcpy_gettimeofday(const struct routine *r, size_t len)
308197b9
RV
249{
250 struct timeval tv_start, tv_end, tv_diff;
251 memcpy_t fn = r->fn.memcpy;
252 void *src = NULL, *dst = NULL;
253 int i;
254
e17fdaea 255 memcpy_alloc_mem(&dst, &src, len);
308197b9 256
6db175c7
IM
257 /*
258 * We prefault the freshly allocated memory range here,
259 * to not measure page fault overhead:
260 */
261 fn(dst, src, len);
308197b9
RV
262
263 BUG_ON(gettimeofday(&tv_start, NULL));
264 for (i = 0; i < iterations; ++i)
265 fn(dst, src, len);
266 BUG_ON(gettimeofday(&tv_end, NULL));
267
268 timersub(&tv_end, &tv_start, &tv_diff);
269
270 free(src);
271 free(dst);
6db175c7 272
1182f883 273 return (double)(((double)len * iterations) / timeval2double(&tv_diff));
308197b9
RV
274}
275
2946f59a 276int bench_mem_memcpy(int argc, const char **argv, const char *prefix __maybe_unused)
308197b9
RV
277{
278 struct bench_mem_info info = {
13839ec4 279 .routines = memcpy_routines,
b14f2d35 280 .do_cycles = do_memcpy_cycles,
13839ec4
IM
281 .do_gettimeofday = do_memcpy_gettimeofday,
282 .usage = bench_mem_memcpy_usage,
308197b9
RV
283 };
284
2946f59a 285 return bench_mem_common(argc, argv, &info);
308197b9 286}
5bce1a57
RV
287
288static void memset_alloc_mem(void **dst, size_t length)
289{
290 *dst = zalloc(length);
291 if (!*dst)
292 die("memory allocation failed - maybe length is too large?\n");
293}
294
b14f2d35 295static u64 do_memset_cycles(const struct routine *r, size_t len)
5bce1a57
RV
296{
297 u64 cycle_start = 0ULL, cycle_end = 0ULL;
298 memset_t fn = r->fn.memset;
299 void *dst = NULL;
300 int i;
301
302 memset_alloc_mem(&dst, len);
303
6db175c7
IM
304 /*
305 * We prefault the freshly allocated memory range here,
306 * to not measure page fault overhead:
307 */
308 fn(dst, -1, len);
5bce1a57 309
b14f2d35 310 cycle_start = get_cycles();
5bce1a57
RV
311 for (i = 0; i < iterations; ++i)
312 fn(dst, i, len);
b14f2d35 313 cycle_end = get_cycles();
5bce1a57
RV
314
315 free(dst);
316 return cycle_end - cycle_start;
317}
318
6db175c7 319static double do_memset_gettimeofday(const struct routine *r, size_t len)
5bce1a57
RV
320{
321 struct timeval tv_start, tv_end, tv_diff;
322 memset_t fn = r->fn.memset;
323 void *dst = NULL;
324 int i;
325
326 memset_alloc_mem(&dst, len);
327
6db175c7
IM
328 /*
329 * We prefault the freshly allocated memory range here,
330 * to not measure page fault overhead:
331 */
332 fn(dst, -1, len);
5bce1a57
RV
333
334 BUG_ON(gettimeofday(&tv_start, NULL));
335 for (i = 0; i < iterations; ++i)
336 fn(dst, i, len);
337 BUG_ON(gettimeofday(&tv_end, NULL));
338
339 timersub(&tv_end, &tv_start, &tv_diff);
340
341 free(dst);
1182f883 342 return (double)(((double)len * iterations) / timeval2double(&tv_diff));
5bce1a57
RV
343}
344
345static const char * const bench_mem_memset_usage[] = {
346 "perf bench mem memset <options>",
347 NULL
348};
349
350static const struct routine memset_routines[] = {
13839ec4
IM
351 { .name = "default",
352 .desc = "Default memset() provided by glibc",
353 .fn.memset = memset },
5bce1a57 354
13839ec4
IM
355#ifdef HAVE_ARCH_X86_64_SUPPORT
356# define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
357# include "mem-memset-x86-64-asm-def.h"
358# undef MEMSET_FN
5bce1a57
RV
359#endif
360
13839ec4 361 { NULL, }
5bce1a57
RV
362};
363
13839ec4 364int bench_mem_memset(int argc, const char **argv, const char *prefix __maybe_unused)
5bce1a57
RV
365{
366 struct bench_mem_info info = {
13839ec4 367 .routines = memset_routines,
b14f2d35 368 .do_cycles = do_memset_cycles,
13839ec4
IM
369 .do_gettimeofday = do_memset_gettimeofday,
370 .usage = bench_mem_memset_usage,
5bce1a57
RV
371 };
372
2946f59a 373 return bench_mem_common(argc, argv, &info);
5bce1a57 374}
This page took 0.252283 seconds and 5 git commands to generate.