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