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