]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/bench/mem-functions.c
btrfs: remove unused headers, statfs.h
[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
IM
108 const struct function *functions;
109 u64 (*do_cycles)(const struct function *r, size_t size);
110 double (*do_gettimeofday)(const struct function *r, size_t size);
308197b9
RV
111 const char *const *usage;
112};
113
2f211c84 114static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, double size_total)
827f3b49 115{
2f211c84 116 const struct function *r = &info->functions[r_idx];
6db175c7 117 double result_bps = 0.0;
b14f2d35 118 u64 result_cycles = 0;
49ce8fc6 119
2f211c84 120 printf("# function '%s' (%s)\n", r->name, r->desc);
827f3b49 121
49ce8fc6 122 if (bench_format == BENCH_FORMAT_DEFAULT)
13b1fdce 123 printf("# Copying %s bytes ...\n\n", size_str);
827f3b49 124
b14f2d35 125 if (use_cycles) {
a69b4f74 126 result_cycles = info->do_cycles(r, size);
827f3b49 127 } else {
a69b4f74 128 result_bps = info->do_gettimeofday(r, size);
827f3b49
HM
129 }
130
131 switch (bench_format) {
132 case BENCH_FORMAT_DEFAULT:
b14f2d35 133 if (use_cycles) {
13b1fdce 134 printf(" %14lf cycles/byte\n", (double)result_cycles/size_total);
49ce8fc6 135 } else {
6db175c7 136 print_bps(result_bps);
827f3b49
HM
137 }
138 break;
6db175c7 139
827f3b49 140 case BENCH_FORMAT_SIMPLE:
b14f2d35 141 if (use_cycles) {
a69b4f74 142 printf("%lf\n", (double)result_cycles/size_total);
49ce8fc6 143 } else {
6db175c7 144 printf("%lf\n", result_bps);
49ce8fc6 145 }
827f3b49 146 break;
6db175c7 147
827f3b49 148 default:
6db175c7 149 BUG_ON(1);
827f3b49
HM
150 break;
151 }
515e23f0
BP
152}
153
2946f59a 154static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
515e23f0
BP
155{
156 int i;
a69b4f74
IM
157 size_t size;
158 double size_total;
515e23f0 159
13839ec4 160 argc = parse_options(argc, argv, options, info->usage, 0);
515e23f0 161
c2a218c6
ACM
162 if (use_cycles) {
163 i = init_cycles();
164 if (i < 0) {
165 fprintf(stderr, "Failed to open cycles counter\n");
166 return i;
167 }
168 }
515e23f0 169
a69b4f74 170 size = (size_t)perf_atoll((char *)size_str);
b0d22e52 171 size_total = (double)size * nr_loops;
515e23f0 172
a69b4f74
IM
173 if ((s64)size <= 0) {
174 fprintf(stderr, "Invalid size:%s\n", size_str);
515e23f0
BP
175 return 1;
176 }
177
2f211c84
IM
178 if (!strncmp(function_str, "all", 3)) {
179 for (i = 0; info->functions[i].name; i++)
180 __bench_mem_function(info, i, size, size_total);
dfecb95c
BP
181 return 0;
182 }
183
2f211c84
IM
184 for (i = 0; info->functions[i].name; i++) {
185 if (!strcmp(info->functions[i].name, function_str))
515e23f0
BP
186 break;
187 }
2f211c84
IM
188 if (!info->functions[i].name) {
189 if (strcmp(function_str, "help") && strcmp(function_str, "h"))
190 printf("Unknown function: %s\n", function_str);
191 printf("Available functions:\n");
192 for (i = 0; info->functions[i].name; i++) {
515e23f0 193 printf("\t%s ... %s\n",
2f211c84 194 info->functions[i].name, info->functions[i].desc);
515e23f0
BP
195 }
196 return 1;
197 }
198
2f211c84 199 __bench_mem_function(info, i, size, size_total);
827f3b49
HM
200
201 return 0;
202}
308197b9 203
a69b4f74 204static void memcpy_alloc_mem(void **dst, void **src, size_t size)
308197b9 205{
a69b4f74 206 *dst = zalloc(size);
308197b9 207 if (!*dst)
a69b4f74 208 die("memory allocation failed - maybe size is too large?\n");
308197b9 209
a69b4f74 210 *src = zalloc(size);
308197b9 211 if (!*src)
a69b4f74 212 die("memory allocation failed - maybe size is too large?\n");
13839ec4
IM
213
214 /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
a69b4f74 215 memset(*src, 0, size);
308197b9
RV
216}
217
2f211c84 218static u64 do_memcpy_cycles(const struct function *r, size_t size)
308197b9
RV
219{
220 u64 cycle_start = 0ULL, cycle_end = 0ULL;
221 void *src = NULL, *dst = NULL;
222 memcpy_t fn = r->fn.memcpy;
223 int i;
224
a69b4f74 225 memcpy_alloc_mem(&dst, &src, size);
308197b9 226
6db175c7
IM
227 /*
228 * We prefault the freshly allocated memory range here,
229 * to not measure page fault overhead:
230 */
a69b4f74 231 fn(dst, src, size);
308197b9 232
b14f2d35 233 cycle_start = get_cycles();
b0d22e52 234 for (i = 0; i < nr_loops; ++i)
a69b4f74 235 fn(dst, src, size);
b14f2d35 236 cycle_end = get_cycles();
308197b9
RV
237
238 free(src);
239 free(dst);
240 return cycle_end - cycle_start;
241}
242
2f211c84 243static double do_memcpy_gettimeofday(const struct function *r, size_t size)
308197b9
RV
244{
245 struct timeval tv_start, tv_end, tv_diff;
246 memcpy_t fn = r->fn.memcpy;
247 void *src = NULL, *dst = NULL;
248 int i;
249
a69b4f74 250 memcpy_alloc_mem(&dst, &src, size);
308197b9 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
265 free(src);
266 free(dst);
6db175c7 267
b0d22e52 268 return (double)(((double)size * nr_loops) / timeval2double(&tv_diff));
308197b9
RV
269}
270
2f211c84 271struct function memcpy_functions[] = {
5dd93304
IM
272 { .name = "default",
273 .desc = "Default memcpy() provided by glibc",
274 .fn.memcpy = memcpy },
275
276#ifdef HAVE_ARCH_X86_64_SUPPORT
277# define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
278# include "mem-memcpy-x86-64-asm-def.h"
279# undef MEMCPY_FN
280#endif
281
a4c6a3e8 282 { .name = NULL, }
5dd93304
IM
283};
284
285static const char * const bench_mem_memcpy_usage[] = {
286 "perf bench mem memcpy <options>",
287 NULL
288};
289
2946f59a 290int bench_mem_memcpy(int argc, const char **argv, const char *prefix __maybe_unused)
308197b9
RV
291{
292 struct bench_mem_info info = {
2f211c84 293 .functions = memcpy_functions,
b14f2d35 294 .do_cycles = do_memcpy_cycles,
13839ec4
IM
295 .do_gettimeofday = do_memcpy_gettimeofday,
296 .usage = bench_mem_memcpy_usage,
308197b9
RV
297 };
298
2946f59a 299 return bench_mem_common(argc, argv, &info);
308197b9 300}
5bce1a57 301
a69b4f74 302static void memset_alloc_mem(void **dst, size_t size)
5bce1a57 303{
a69b4f74 304 *dst = zalloc(size);
5bce1a57 305 if (!*dst)
a69b4f74 306 die("memory allocation failed - maybe size is too large?\n");
5bce1a57
RV
307}
308
2f211c84 309static u64 do_memset_cycles(const struct function *r, size_t size)
5bce1a57
RV
310{
311 u64 cycle_start = 0ULL, cycle_end = 0ULL;
312 memset_t fn = r->fn.memset;
313 void *dst = NULL;
314 int i;
315
a69b4f74 316 memset_alloc_mem(&dst, size);
5bce1a57 317
6db175c7
IM
318 /*
319 * We prefault the freshly allocated memory range here,
320 * to not measure page fault overhead:
321 */
a69b4f74 322 fn(dst, -1, size);
5bce1a57 323
b14f2d35 324 cycle_start = get_cycles();
b0d22e52 325 for (i = 0; i < nr_loops; ++i)
a69b4f74 326 fn(dst, i, size);
b14f2d35 327 cycle_end = get_cycles();
5bce1a57
RV
328
329 free(dst);
330 return cycle_end - cycle_start;
331}
332
2f211c84 333static double do_memset_gettimeofday(const struct function *r, size_t size)
5bce1a57
RV
334{
335 struct timeval tv_start, tv_end, tv_diff;
336 memset_t fn = r->fn.memset;
337 void *dst = NULL;
338 int i;
339
a69b4f74 340 memset_alloc_mem(&dst, size);
5bce1a57 341
6db175c7
IM
342 /*
343 * We prefault the freshly allocated memory range here,
344 * to not measure page fault overhead:
345 */
a69b4f74 346 fn(dst, -1, size);
5bce1a57
RV
347
348 BUG_ON(gettimeofday(&tv_start, NULL));
b0d22e52 349 for (i = 0; i < nr_loops; ++i)
a69b4f74 350 fn(dst, i, size);
5bce1a57
RV
351 BUG_ON(gettimeofday(&tv_end, NULL));
352
353 timersub(&tv_end, &tv_start, &tv_diff);
354
355 free(dst);
b0d22e52 356 return (double)(((double)size * nr_loops) / timeval2double(&tv_diff));
5bce1a57
RV
357}
358
359static const char * const bench_mem_memset_usage[] = {
360 "perf bench mem memset <options>",
361 NULL
362};
363
2f211c84 364static const struct function memset_functions[] = {
13839ec4
IM
365 { .name = "default",
366 .desc = "Default memset() provided by glibc",
367 .fn.memset = memset },
5bce1a57 368
13839ec4
IM
369#ifdef HAVE_ARCH_X86_64_SUPPORT
370# define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
371# include "mem-memset-x86-64-asm-def.h"
372# undef MEMSET_FN
5bce1a57
RV
373#endif
374
a4c6a3e8 375 { .name = NULL, }
5bce1a57
RV
376};
377
13839ec4 378int bench_mem_memset(int argc, const char **argv, const char *prefix __maybe_unused)
5bce1a57
RV
379{
380 struct bench_mem_info info = {
2f211c84 381 .functions = memset_functions,
b14f2d35 382 .do_cycles = do_memset_cycles,
13839ec4
IM
383 .do_gettimeofday = do_memset_gettimeofday,
384 .usage = bench_mem_memset_usage,
5bce1a57
RV
385 };
386
2946f59a 387 return bench_mem_common(argc, argv, &info);
5bce1a57 388}