blob: c684910e5a482af6bee4ebd15c86708813ff6777 [file] [log] [blame]
Hitoshi Mitake827f3b42009-11-18 00:20:09 +09001/*
2 * mem-memcpy.c
3 *
Ingo Molnar13839ec2015-10-19 10:04:17 +02004 * Simple memcpy() and memset() benchmarks
Hitoshi Mitake827f3b42009-11-18 00:20:09 +09005 *
6 * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
7 */
Hitoshi Mitake827f3b42009-11-18 00:20:09 +09008
Arnaldo Carvalho de Meloc2a218c2016-04-26 13:27:23 -03009#include "debug.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090010#include "../perf.h"
11#include "../util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090013#include "../util/header.h"
Yann Droneaud57480d22014-06-30 22:28:47 +020014#include "../util/cloexec.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090015#include "bench.h"
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090016#include "mem-memcpy-arch.h"
Rabin Vincent5bce1a52014-12-02 16:50:40 +010017#include "mem-memset-arch.h"
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/time.h>
23#include <errno.h>
Arnaldo Carvalho de Melof2b91be2016-08-08 14:59:21 -030024#include <linux/time64.h>
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090025
26#define K 1024
27
Ingo Molnara69b4f72015-10-19 10:04:25 +020028static const char *size_str = "1MB";
Ingo Molnar2f211c82015-10-19 10:04:29 +020029static const char *function_str = "all";
Ingo Molnarb0d22e52015-10-19 10:04:28 +020030static int nr_loops = 1;
Ingo Molnarb14f2d32015-10-19 10:04:23 +020031static bool use_cycles;
32static int cycles_fd;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090033
34static const struct option options[] = {
Ingo Molnarb0d22e52015-10-19 10:04:28 +020035 OPT_STRING('s', "size", &size_str, "1MB",
Ingo Molnara69b4f72015-10-19 10:04:25 +020036 "Specify the size of the memory buffers. "
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020037 "Available units: B, KB, MB, GB and TB (case insensitive)"),
38
Ingo Molnar2f211c82015-10-19 10:04:29 +020039 OPT_STRING('f', "function", &function_str, "all",
40 "Specify the function to run, \"all\" runs all available functions, \"help\" lists them"),
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020041
Ingo Molnarb0d22e52015-10-19 10:04:28 +020042 OPT_INTEGER('l', "nr_loops", &nr_loops,
43 "Specify the number of loops to run. (default: 1)"),
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020044
Ingo Molnarb14f2d32015-10-19 10:04:23 +020045 OPT_BOOLEAN('c', "cycles", &use_cycles,
46 "Use a cycles event instead of gettimeofday() to measure performance"),
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020047
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090048 OPT_END()
49};
50
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090051typedef void *(*memcpy_t)(void *, const void *, size_t);
Rabin Vincent5bce1a52014-12-02 16:50:40 +010052typedef void *(*memset_t)(void *, int, size_t);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +090053
Ingo Molnar2f211c82015-10-19 10:04:29 +020054struct function {
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090055 const char *name;
56 const char *desc;
Rabin Vincent308197b2014-12-02 16:50:39 +010057 union {
58 memcpy_t memcpy;
Rabin Vincent5bce1a52014-12-02 16:50:40 +010059 memset_t memset;
Rabin Vincent308197b2014-12-02 16:50:39 +010060 } fn;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090061};
62
Hitoshi Mitake17d7a112012-07-02 22:46:17 +090063static struct perf_event_attr cycle_attr = {
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090064 .type = PERF_TYPE_HARDWARE,
65 .config = PERF_COUNT_HW_CPU_CYCLES
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090066};
67
Arnaldo Carvalho de Meloc2a218c2016-04-26 13:27:23 -030068static int init_cycles(void)
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090069{
Ingo Molnarb14f2d32015-10-19 10:04:23 +020070 cycles_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, perf_event_open_cloexec_flag());
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +090071
Arnaldo Carvalho de Meloc2a218c2016-04-26 13:27:23 -030072 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;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090078}
79
Ingo Molnarb14f2d32015-10-19 10:04:23 +020080static u64 get_cycles(void)
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090081{
82 int ret;
83 u64 clk;
84
Ingo Molnarb14f2d32015-10-19 10:04:23 +020085 ret = read(cycles_fd, &clk, sizeof(u64));
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090086 BUG_ON(ret != sizeof(u64));
87
88 return clk;
89}
90
91static double timeval2double(struct timeval *ts)
92{
Arnaldo Carvalho de Melof2b91be2016-08-08 14:59:21 -030093 return (double)ts->tv_sec + (double)ts->tv_usec / (double)USEC_PER_SEC;
Hitoshi Mitake827f3b42009-11-18 00:20:09 +090094}
95
Ingo Molnar6db175c2015-10-19 10:04:21 +020096#define print_bps(x) do { \
97 if (x < K) \
Ingo Molnar13b1fdc2015-10-19 10:04:26 +020098 printf(" %14lf bytes/sec\n", x); \
Ingo Molnar6db175c2015-10-19 10:04:21 +020099 else if (x < K * K) \
Ingo Molnar13b1fdc2015-10-19 10:04:26 +0200100 printf(" %14lfd KB/sec\n", x / K); \
Ingo Molnar6db175c2015-10-19 10:04:21 +0200101 else if (x < K * K * K) \
Ingo Molnar13b1fdc2015-10-19 10:04:26 +0200102 printf(" %14lf MB/sec\n", x / K / K); \
Ingo Molnar6db175c2015-10-19 10:04:21 +0200103 else \
Ingo Molnar13b1fdc2015-10-19 10:04:26 +0200104 printf(" %14lf GB/sec\n", x / K / K / K); \
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900105 } while (0)
106
Rabin Vincent308197b2014-12-02 16:50:39 +0100107struct bench_mem_info {
Ingo Molnar2f211c82015-10-19 10:04:29 +0200108 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);
Rabin Vincent308197b2014-12-02 16:50:39 +0100111 const char *const *usage;
112};
113
Ingo Molnar2f211c82015-10-19 10:04:29 +0200114static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, double size_total)
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900115{
Ingo Molnar2f211c82015-10-19 10:04:29 +0200116 const struct function *r = &info->functions[r_idx];
Ingo Molnar6db175c2015-10-19 10:04:21 +0200117 double result_bps = 0.0;
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200118 u64 result_cycles = 0;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900119
Ingo Molnar2f211c82015-10-19 10:04:29 +0200120 printf("# function '%s' (%s)\n", r->name, r->desc);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900121
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900122 if (bench_format == BENCH_FORMAT_DEFAULT)
Ingo Molnar13b1fdc2015-10-19 10:04:26 +0200123 printf("# Copying %s bytes ...\n\n", size_str);
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900124
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200125 if (use_cycles) {
Ingo Molnara69b4f72015-10-19 10:04:25 +0200126 result_cycles = info->do_cycles(r, size);
Hitoshi Mitake12eac0b2009-11-20 12:37:17 +0900127 } else {
Ingo Molnara69b4f72015-10-19 10:04:25 +0200128 result_bps = info->do_gettimeofday(r, size);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900129 }
130
131 switch (bench_format) {
132 case BENCH_FORMAT_DEFAULT:
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200133 if (use_cycles) {
Ingo Molnar13b1fdc2015-10-19 10:04:26 +0200134 printf(" %14lf cycles/byte\n", (double)result_cycles/size_total);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900135 } else {
Ingo Molnar6db175c2015-10-19 10:04:21 +0200136 print_bps(result_bps);
137 }
138 break;
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900139
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900140 case BENCH_FORMAT_SIMPLE:
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200141 if (use_cycles) {
Ingo Molnara69b4f72015-10-19 10:04:25 +0200142 printf("%lf\n", (double)result_cycles/size_total);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900143 } else {
Ingo Molnar6db175c2015-10-19 10:04:21 +0200144 printf("%lf\n", result_bps);
Hitoshi Mitake49ce8fc2010-11-25 16:04:52 +0900145 }
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900146 break;
Ingo Molnar6db175c2015-10-19 10:04:21 +0200147
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900148 default:
Ingo Molnar6db175c2015-10-19 10:04:21 +0200149 BUG_ON(1);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900150 break;
151 }
Borislav Petkov515e23f2015-02-26 18:51:37 +0100152}
153
Ingo Molnar2946f592015-10-19 10:04:19 +0200154static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
Borislav Petkov515e23f2015-02-26 18:51:37 +0100155{
156 int i;
Ingo Molnara69b4f72015-10-19 10:04:25 +0200157 size_t size;
158 double size_total;
Borislav Petkov515e23f2015-02-26 18:51:37 +0100159
Ingo Molnar13839ec2015-10-19 10:04:17 +0200160 argc = parse_options(argc, argv, options, info->usage, 0);
Borislav Petkov515e23f2015-02-26 18:51:37 +0100161
Arnaldo Carvalho de Meloc2a218c2016-04-26 13:27:23 -0300162 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 }
Borislav Petkov515e23f2015-02-26 18:51:37 +0100169
Ingo Molnara69b4f72015-10-19 10:04:25 +0200170 size = (size_t)perf_atoll((char *)size_str);
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200171 size_total = (double)size * nr_loops;
Borislav Petkov515e23f2015-02-26 18:51:37 +0100172
Ingo Molnara69b4f72015-10-19 10:04:25 +0200173 if ((s64)size <= 0) {
174 fprintf(stderr, "Invalid size:%s\n", size_str);
Borislav Petkov515e23f2015-02-26 18:51:37 +0100175 return 1;
176 }
177
Ingo Molnar2f211c82015-10-19 10:04:29 +0200178 if (!strncmp(function_str, "all", 3)) {
179 for (i = 0; info->functions[i].name; i++)
180 __bench_mem_function(info, i, size, size_total);
Borislav Petkovdfecb952015-02-26 19:02:43 +0100181 return 0;
182 }
183
Ingo Molnar2f211c82015-10-19 10:04:29 +0200184 for (i = 0; info->functions[i].name; i++) {
185 if (!strcmp(info->functions[i].name, function_str))
Borislav Petkov515e23f2015-02-26 18:51:37 +0100186 break;
187 }
Ingo Molnar2f211c82015-10-19 10:04:29 +0200188 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++) {
Borislav Petkov515e23f2015-02-26 18:51:37 +0100193 printf("\t%s ... %s\n",
Ingo Molnar2f211c82015-10-19 10:04:29 +0200194 info->functions[i].name, info->functions[i].desc);
Borislav Petkov515e23f2015-02-26 18:51:37 +0100195 }
196 return 1;
197 }
198
Ingo Molnar2f211c82015-10-19 10:04:29 +0200199 __bench_mem_function(info, i, size, size_total);
Hitoshi Mitake827f3b42009-11-18 00:20:09 +0900200
201 return 0;
202}
Rabin Vincent308197b2014-12-02 16:50:39 +0100203
Ingo Molnara69b4f72015-10-19 10:04:25 +0200204static void memcpy_alloc_mem(void **dst, void **src, size_t size)
Rabin Vincent308197b2014-12-02 16:50:39 +0100205{
Ingo Molnara69b4f72015-10-19 10:04:25 +0200206 *dst = zalloc(size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100207 if (!*dst)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200208 die("memory allocation failed - maybe size is too large?\n");
Rabin Vincent308197b2014-12-02 16:50:39 +0100209
Ingo Molnara69b4f72015-10-19 10:04:25 +0200210 *src = zalloc(size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100211 if (!*src)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200212 die("memory allocation failed - maybe size is too large?\n");
Ingo Molnar13839ec2015-10-19 10:04:17 +0200213
214 /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
Ingo Molnara69b4f72015-10-19 10:04:25 +0200215 memset(*src, 0, size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100216}
217
Ingo Molnar2f211c82015-10-19 10:04:29 +0200218static u64 do_memcpy_cycles(const struct function *r, size_t size)
Rabin Vincent308197b2014-12-02 16:50:39 +0100219{
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
Ingo Molnara69b4f72015-10-19 10:04:25 +0200225 memcpy_alloc_mem(&dst, &src, size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100226
Ingo Molnar6db175c2015-10-19 10:04:21 +0200227 /*
228 * We prefault the freshly allocated memory range here,
229 * to not measure page fault overhead:
230 */
Ingo Molnara69b4f72015-10-19 10:04:25 +0200231 fn(dst, src, size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100232
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200233 cycle_start = get_cycles();
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200234 for (i = 0; i < nr_loops; ++i)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200235 fn(dst, src, size);
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200236 cycle_end = get_cycles();
Rabin Vincent308197b2014-12-02 16:50:39 +0100237
238 free(src);
239 free(dst);
240 return cycle_end - cycle_start;
241}
242
Ingo Molnar2f211c82015-10-19 10:04:29 +0200243static double do_memcpy_gettimeofday(const struct function *r, size_t size)
Rabin Vincent308197b2014-12-02 16:50:39 +0100244{
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
Ingo Molnara69b4f72015-10-19 10:04:25 +0200250 memcpy_alloc_mem(&dst, &src, size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100251
Ingo Molnar6db175c2015-10-19 10:04:21 +0200252 /*
253 * We prefault the freshly allocated memory range here,
254 * to not measure page fault overhead:
255 */
Ingo Molnara69b4f72015-10-19 10:04:25 +0200256 fn(dst, src, size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100257
258 BUG_ON(gettimeofday(&tv_start, NULL));
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200259 for (i = 0; i < nr_loops; ++i)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200260 fn(dst, src, size);
Rabin Vincent308197b2014-12-02 16:50:39 +0100261 BUG_ON(gettimeofday(&tv_end, NULL));
262
263 timersub(&tv_end, &tv_start, &tv_diff);
264
265 free(src);
266 free(dst);
Ingo Molnar6db175c2015-10-19 10:04:21 +0200267
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200268 return (double)(((double)size * nr_loops) / timeval2double(&tv_diff));
Rabin Vincent308197b2014-12-02 16:50:39 +0100269}
270
Ingo Molnar2f211c82015-10-19 10:04:29 +0200271struct function memcpy_functions[] = {
Ingo Molnar5dd93302015-10-19 10:04:27 +0200272 { .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
Arnaldo Carvalho de Meloa4c6a3e2015-10-19 18:17:25 -0300282 { .name = NULL, }
Ingo Molnar5dd93302015-10-19 10:04:27 +0200283};
284
285static const char * const bench_mem_memcpy_usage[] = {
286 "perf bench mem memcpy <options>",
287 NULL
288};
289
Ingo Molnar2946f592015-10-19 10:04:19 +0200290int bench_mem_memcpy(int argc, const char **argv, const char *prefix __maybe_unused)
Rabin Vincent308197b2014-12-02 16:50:39 +0100291{
292 struct bench_mem_info info = {
Ingo Molnar2f211c82015-10-19 10:04:29 +0200293 .functions = memcpy_functions,
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200294 .do_cycles = do_memcpy_cycles,
Ingo Molnar13839ec2015-10-19 10:04:17 +0200295 .do_gettimeofday = do_memcpy_gettimeofday,
296 .usage = bench_mem_memcpy_usage,
Rabin Vincent308197b2014-12-02 16:50:39 +0100297 };
298
Ingo Molnar2946f592015-10-19 10:04:19 +0200299 return bench_mem_common(argc, argv, &info);
Rabin Vincent308197b2014-12-02 16:50:39 +0100300}
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100301
Ingo Molnara69b4f72015-10-19 10:04:25 +0200302static void memset_alloc_mem(void **dst, size_t size)
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100303{
Ingo Molnara69b4f72015-10-19 10:04:25 +0200304 *dst = zalloc(size);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100305 if (!*dst)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200306 die("memory allocation failed - maybe size is too large?\n");
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100307}
308
Ingo Molnar2f211c82015-10-19 10:04:29 +0200309static u64 do_memset_cycles(const struct function *r, size_t size)
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100310{
311 u64 cycle_start = 0ULL, cycle_end = 0ULL;
312 memset_t fn = r->fn.memset;
313 void *dst = NULL;
314 int i;
315
Ingo Molnara69b4f72015-10-19 10:04:25 +0200316 memset_alloc_mem(&dst, size);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100317
Ingo Molnar6db175c2015-10-19 10:04:21 +0200318 /*
319 * We prefault the freshly allocated memory range here,
320 * to not measure page fault overhead:
321 */
Ingo Molnara69b4f72015-10-19 10:04:25 +0200322 fn(dst, -1, size);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100323
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200324 cycle_start = get_cycles();
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200325 for (i = 0; i < nr_loops; ++i)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200326 fn(dst, i, size);
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200327 cycle_end = get_cycles();
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100328
329 free(dst);
330 return cycle_end - cycle_start;
331}
332
Ingo Molnar2f211c82015-10-19 10:04:29 +0200333static double do_memset_gettimeofday(const struct function *r, size_t size)
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100334{
335 struct timeval tv_start, tv_end, tv_diff;
336 memset_t fn = r->fn.memset;
337 void *dst = NULL;
338 int i;
339
Ingo Molnara69b4f72015-10-19 10:04:25 +0200340 memset_alloc_mem(&dst, size);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100341
Ingo Molnar6db175c2015-10-19 10:04:21 +0200342 /*
343 * We prefault the freshly allocated memory range here,
344 * to not measure page fault overhead:
345 */
Ingo Molnara69b4f72015-10-19 10:04:25 +0200346 fn(dst, -1, size);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100347
348 BUG_ON(gettimeofday(&tv_start, NULL));
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200349 for (i = 0; i < nr_loops; ++i)
Ingo Molnara69b4f72015-10-19 10:04:25 +0200350 fn(dst, i, size);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100351 BUG_ON(gettimeofday(&tv_end, NULL));
352
353 timersub(&tv_end, &tv_start, &tv_diff);
354
355 free(dst);
Ingo Molnarb0d22e52015-10-19 10:04:28 +0200356 return (double)(((double)size * nr_loops) / timeval2double(&tv_diff));
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100357}
358
359static const char * const bench_mem_memset_usage[] = {
360 "perf bench mem memset <options>",
361 NULL
362};
363
Ingo Molnar2f211c82015-10-19 10:04:29 +0200364static const struct function memset_functions[] = {
Ingo Molnar13839ec2015-10-19 10:04:17 +0200365 { .name = "default",
366 .desc = "Default memset() provided by glibc",
367 .fn.memset = memset },
368
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100369#ifdef HAVE_ARCH_X86_64_SUPPORT
Ingo Molnar13839ec2015-10-19 10:04:17 +0200370# 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
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100373#endif
374
Arnaldo Carvalho de Meloa4c6a3e2015-10-19 18:17:25 -0300375 { .name = NULL, }
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100376};
377
Ingo Molnar13839ec2015-10-19 10:04:17 +0200378int bench_mem_memset(int argc, const char **argv, const char *prefix __maybe_unused)
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100379{
380 struct bench_mem_info info = {
Ingo Molnar2f211c82015-10-19 10:04:29 +0200381 .functions = memset_functions,
Ingo Molnarb14f2d32015-10-19 10:04:23 +0200382 .do_cycles = do_memset_cycles,
Ingo Molnar13839ec2015-10-19 10:04:17 +0200383 .do_gettimeofday = do_memset_gettimeofday,
384 .usage = bench_mem_memset_usage,
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100385 };
386
Ingo Molnar2946f592015-10-19 10:04:19 +0200387 return bench_mem_common(argc, argv, &info);
Rabin Vincent5bce1a52014-12-02 16:50:40 +0100388}