2009-11-17 23:20:09 +08:00
|
|
|
/*
|
|
|
|
* mem-memcpy.c
|
|
|
|
*
|
2015-10-19 16:04:17 +08:00
|
|
|
* Simple memcpy() and memset() benchmarks
|
2009-11-17 23:20:09 +08:00
|
|
|
*
|
|
|
|
* Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../perf.h"
|
|
|
|
#include "../util/util.h"
|
|
|
|
#include "../util/parse-options.h"
|
|
|
|
#include "../util/header.h"
|
2014-07-01 04:28:47 +08:00
|
|
|
#include "../util/cloexec.h"
|
2009-11-17 23:20:09 +08:00
|
|
|
#include "bench.h"
|
2010-11-25 15:04:52 +08:00
|
|
|
#include "mem-memcpy-arch.h"
|
2014-12-02 23:50:40 +08:00
|
|
|
#include "mem-memset-arch.h"
|
2009-11-17 23:20:09 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define K 1024
|
|
|
|
|
2009-11-20 11:37:17 +08:00
|
|
|
static const char *length_str = "1MB";
|
2015-10-19 16:04:24 +08:00
|
|
|
static const char *routine_str = "all";
|
2012-01-18 21:29:59 +08:00
|
|
|
static int iterations = 1;
|
2015-10-19 16:04:23 +08:00
|
|
|
static bool use_cycles;
|
|
|
|
static int cycles_fd;
|
2009-11-17 23:20:09 +08:00
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
OPT_STRING('l', "length", &length_str, "1MB",
|
|
|
|
"Specify length of memory to copy. "
|
2012-06-20 14:08:06 +08:00
|
|
|
"Available units: B, KB, MB, GB and TB (upper and lower)"),
|
2015-10-19 16:04:24 +08:00
|
|
|
OPT_STRING('r', "routine", &routine_str, "all",
|
|
|
|
"Specify the routine to run, \"all\" runs all available routines"),
|
2012-01-18 21:29:59 +08:00
|
|
|
OPT_INTEGER('i', "iterations", &iterations,
|
|
|
|
"repeat memcpy() invocation this number of times"),
|
2015-10-19 16:04:23 +08:00
|
|
|
OPT_BOOLEAN('c', "cycles", &use_cycles,
|
|
|
|
"Use a cycles event instead of gettimeofday() to measure performance"),
|
2009-11-17 23:20:09 +08:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2010-11-25 15:04:52 +08:00
|
|
|
typedef void *(*memcpy_t)(void *, const void *, size_t);
|
2014-12-02 23:50:40 +08:00
|
|
|
typedef void *(*memset_t)(void *, int, size_t);
|
2010-11-25 15:04:52 +08:00
|
|
|
|
2009-11-17 23:20:09 +08:00
|
|
|
struct routine {
|
|
|
|
const char *name;
|
|
|
|
const char *desc;
|
2014-12-02 23:50:39 +08:00
|
|
|
union {
|
|
|
|
memcpy_t memcpy;
|
2014-12-02 23:50:40 +08:00
|
|
|
memset_t memset;
|
2014-12-02 23:50:39 +08:00
|
|
|
} fn;
|
2009-11-17 23:20:09 +08:00
|
|
|
};
|
|
|
|
|
2014-12-02 23:50:39 +08:00
|
|
|
struct routine memcpy_routines[] = {
|
2015-10-19 16:04:17 +08:00
|
|
|
{ .name = "default",
|
|
|
|
.desc = "Default memcpy() provided by glibc",
|
|
|
|
.fn.memcpy = memcpy },
|
2010-11-25 15:04:52 +08:00
|
|
|
|
2015-10-19 16:04:17 +08:00
|
|
|
#ifdef HAVE_ARCH_X86_64_SUPPORT
|
|
|
|
# define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
|
|
|
|
# include "mem-memcpy-x86-64-asm-def.h"
|
|
|
|
# undef MEMCPY_FN
|
2010-11-25 15:04:52 +08:00
|
|
|
#endif
|
|
|
|
|
2015-10-19 16:04:17 +08:00
|
|
|
{ NULL, }
|
2009-11-17 23:20:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const bench_mem_memcpy_usage[] = {
|
|
|
|
"perf bench mem memcpy <options>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-07-02 21:46:17 +08:00
|
|
|
static struct perf_event_attr cycle_attr = {
|
2009-11-20 11:37:17 +08:00
|
|
|
.type = PERF_TYPE_HARDWARE,
|
|
|
|
.config = PERF_COUNT_HW_CPU_CYCLES
|
2009-11-17 23:20:09 +08:00
|
|
|
};
|
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
static void init_cycles(void)
|
2009-11-17 23:20:09 +08:00
|
|
|
{
|
2015-10-19 16:04:23 +08:00
|
|
|
cycles_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, perf_event_open_cloexec_flag());
|
2009-11-20 11:37:17 +08:00
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
if (cycles_fd < 0 && errno == ENOSYS)
|
2009-11-20 11:37:17 +08:00
|
|
|
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
|
|
|
|
else
|
2015-10-19 16:04:23 +08:00
|
|
|
BUG_ON(cycles_fd < 0);
|
2009-11-17 23:20:09 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
static u64 get_cycles(void)
|
2009-11-17 23:20:09 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u64 clk;
|
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
ret = read(cycles_fd, &clk, sizeof(u64));
|
2009-11-17 23:20:09 +08:00
|
|
|
BUG_ON(ret != sizeof(u64));
|
|
|
|
|
|
|
|
return clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double timeval2double(struct timeval *ts)
|
|
|
|
{
|
2015-10-19 16:04:17 +08:00
|
|
|
return (double)ts->tv_sec + (double)ts->tv_usec / (double)1000000;
|
2009-11-17 23:20:09 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
#define print_bps(x) do { \
|
|
|
|
if (x < K) \
|
|
|
|
printf(" %14lf B/Sec\n", x); \
|
|
|
|
else if (x < K * K) \
|
|
|
|
printf(" %14lfd KB/Sec\n", x / K); \
|
|
|
|
else if (x < K * K * K) \
|
|
|
|
printf(" %14lf MB/Sec\n", x / K / K); \
|
|
|
|
else \
|
|
|
|
printf(" %14lf GB/Sec\n", x / K / K / K); \
|
2010-11-25 15:04:52 +08:00
|
|
|
} while (0)
|
|
|
|
|
2014-12-02 23:50:39 +08:00
|
|
|
struct bench_mem_info {
|
|
|
|
const struct routine *routines;
|
2015-10-19 16:04:23 +08:00
|
|
|
u64 (*do_cycles)(const struct routine *r, size_t len);
|
2015-10-19 16:04:21 +08:00
|
|
|
double (*do_gettimeofday)(const struct routine *r, size_t len);
|
2014-12-02 23:50:39 +08:00
|
|
|
const char *const *usage;
|
|
|
|
};
|
|
|
|
|
2015-02-27 01:51:37 +08:00
|
|
|
static void __bench_mem_routine(struct bench_mem_info *info, int r_idx, size_t len, double totallen)
|
2009-11-17 23:20:09 +08:00
|
|
|
{
|
2015-02-27 01:51:37 +08:00
|
|
|
const struct routine *r = &info->routines[r_idx];
|
2015-10-19 16:04:21 +08:00
|
|
|
double result_bps = 0.0;
|
2015-10-19 16:04:23 +08:00
|
|
|
u64 result_cycles = 0;
|
2010-11-25 15:04:52 +08:00
|
|
|
|
2015-02-27 02:02:43 +08:00
|
|
|
printf("Routine %s (%s)\n", r->name, r->desc);
|
2009-11-17 23:20:09 +08:00
|
|
|
|
2010-11-25 15:04:52 +08:00
|
|
|
if (bench_format == BENCH_FORMAT_DEFAULT)
|
|
|
|
printf("# Copying %s Bytes ...\n\n", length_str);
|
2009-11-17 23:20:09 +08:00
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
if (use_cycles) {
|
|
|
|
result_cycles = info->do_cycles(r, len);
|
2009-11-17 23:20:09 +08:00
|
|
|
} else {
|
2015-10-19 16:04:21 +08:00
|
|
|
result_bps = info->do_gettimeofday(r, len);
|
2009-11-17 23:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (bench_format) {
|
|
|
|
case BENCH_FORMAT_DEFAULT:
|
2015-10-19 16:04:23 +08:00
|
|
|
if (use_cycles) {
|
|
|
|
printf(" %14lf cycles/Byte\n", (double)result_cycles/totallen);
|
2010-11-25 15:04:52 +08:00
|
|
|
} else {
|
2015-10-19 16:04:21 +08:00
|
|
|
print_bps(result_bps);
|
2009-11-17 23:20:09 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-10-19 16:04:21 +08:00
|
|
|
|
2009-11-17 23:20:09 +08:00
|
|
|
case BENCH_FORMAT_SIMPLE:
|
2015-10-19 16:04:23 +08:00
|
|
|
if (use_cycles) {
|
|
|
|
printf("%lf\n", (double)result_cycles/totallen);
|
2010-11-25 15:04:52 +08:00
|
|
|
} else {
|
2015-10-19 16:04:21 +08:00
|
|
|
printf("%lf\n", result_bps);
|
2010-11-25 15:04:52 +08:00
|
|
|
}
|
2009-11-17 23:20:09 +08:00
|
|
|
break;
|
2015-10-19 16:04:21 +08:00
|
|
|
|
2009-11-17 23:20:09 +08:00
|
|
|
default:
|
2015-10-19 16:04:21 +08:00
|
|
|
BUG_ON(1);
|
2009-11-17 23:20:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-02-27 01:51:37 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:19 +08:00
|
|
|
static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
|
2015-02-27 01:51:37 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t len;
|
|
|
|
double totallen;
|
|
|
|
|
2015-10-19 16:04:17 +08:00
|
|
|
argc = parse_options(argc, argv, options, info->usage, 0);
|
2015-02-27 01:51:37 +08:00
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
if (use_cycles)
|
|
|
|
init_cycles();
|
2015-02-27 01:51:37 +08:00
|
|
|
|
|
|
|
len = (size_t)perf_atoll((char *)length_str);
|
|
|
|
totallen = (double)len * iterations;
|
|
|
|
|
|
|
|
if ((s64)len <= 0) {
|
|
|
|
fprintf(stderr, "Invalid length:%s\n", length_str);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:24 +08:00
|
|
|
if (!strncmp(routine_str, "all", 3)) {
|
2015-02-27 02:02:43 +08:00
|
|
|
for (i = 0; info->routines[i].name; i++)
|
|
|
|
__bench_mem_routine(info, i, len, totallen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-27 01:51:37 +08:00
|
|
|
for (i = 0; info->routines[i].name; i++) {
|
2015-10-19 16:04:24 +08:00
|
|
|
if (!strcmp(info->routines[i].name, routine_str))
|
2015-02-27 01:51:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!info->routines[i].name) {
|
2015-10-19 16:04:24 +08:00
|
|
|
printf("Unknown routine: %s\n", routine_str);
|
2015-02-27 01:51:37 +08:00
|
|
|
printf("Available routines...\n");
|
|
|
|
for (i = 0; info->routines[i].name; i++) {
|
|
|
|
printf("\t%s ... %s\n",
|
|
|
|
info->routines[i].name, info->routines[i].desc);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bench_mem_routine(info, i, len, totallen);
|
2009-11-17 23:20:09 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-02 23:50:39 +08:00
|
|
|
|
|
|
|
static void memcpy_alloc_mem(void **dst, void **src, size_t length)
|
|
|
|
{
|
|
|
|
*dst = zalloc(length);
|
|
|
|
if (!*dst)
|
|
|
|
die("memory allocation failed - maybe length is too large?\n");
|
|
|
|
|
|
|
|
*src = zalloc(length);
|
|
|
|
if (!*src)
|
|
|
|
die("memory allocation failed - maybe length is too large?\n");
|
2015-10-19 16:04:17 +08:00
|
|
|
|
|
|
|
/* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
|
2014-12-02 23:50:39 +08:00
|
|
|
memset(*src, 0, length);
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
static u64 do_memcpy_cycles(const struct routine *r, size_t len)
|
2014-12-02 23:50:39 +08:00
|
|
|
{
|
|
|
|
u64 cycle_start = 0ULL, cycle_end = 0ULL;
|
|
|
|
void *src = NULL, *dst = NULL;
|
|
|
|
memcpy_t fn = r->fn.memcpy;
|
|
|
|
int i;
|
|
|
|
|
2015-01-15 17:20:22 +08:00
|
|
|
memcpy_alloc_mem(&dst, &src, len);
|
2014-12-02 23:50:39 +08:00
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
/*
|
|
|
|
* We prefault the freshly allocated memory range here,
|
|
|
|
* to not measure page fault overhead:
|
|
|
|
*/
|
|
|
|
fn(dst, src, len);
|
2014-12-02 23:50:39 +08:00
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
cycle_start = get_cycles();
|
2014-12-02 23:50:39 +08:00
|
|
|
for (i = 0; i < iterations; ++i)
|
|
|
|
fn(dst, src, len);
|
2015-10-19 16:04:23 +08:00
|
|
|
cycle_end = get_cycles();
|
2014-12-02 23:50:39 +08:00
|
|
|
|
|
|
|
free(src);
|
|
|
|
free(dst);
|
|
|
|
return cycle_end - cycle_start;
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
static double do_memcpy_gettimeofday(const struct routine *r, size_t len)
|
2014-12-02 23:50:39 +08:00
|
|
|
{
|
|
|
|
struct timeval tv_start, tv_end, tv_diff;
|
|
|
|
memcpy_t fn = r->fn.memcpy;
|
|
|
|
void *src = NULL, *dst = NULL;
|
|
|
|
int i;
|
|
|
|
|
2015-01-15 17:20:22 +08:00
|
|
|
memcpy_alloc_mem(&dst, &src, len);
|
2014-12-02 23:50:39 +08:00
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
/*
|
|
|
|
* We prefault the freshly allocated memory range here,
|
|
|
|
* to not measure page fault overhead:
|
|
|
|
*/
|
|
|
|
fn(dst, src, len);
|
2014-12-02 23:50:39 +08:00
|
|
|
|
|
|
|
BUG_ON(gettimeofday(&tv_start, NULL));
|
|
|
|
for (i = 0; i < iterations; ++i)
|
|
|
|
fn(dst, src, len);
|
|
|
|
BUG_ON(gettimeofday(&tv_end, NULL));
|
|
|
|
|
|
|
|
timersub(&tv_end, &tv_start, &tv_diff);
|
|
|
|
|
|
|
|
free(src);
|
|
|
|
free(dst);
|
2015-10-19 16:04:21 +08:00
|
|
|
|
2014-12-02 23:50:41 +08:00
|
|
|
return (double)(((double)len * iterations) / timeval2double(&tv_diff));
|
2014-12-02 23:50:39 +08:00
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:19 +08:00
|
|
|
int bench_mem_memcpy(int argc, const char **argv, const char *prefix __maybe_unused)
|
2014-12-02 23:50:39 +08:00
|
|
|
{
|
|
|
|
struct bench_mem_info info = {
|
2015-10-19 16:04:17 +08:00
|
|
|
.routines = memcpy_routines,
|
2015-10-19 16:04:23 +08:00
|
|
|
.do_cycles = do_memcpy_cycles,
|
2015-10-19 16:04:17 +08:00
|
|
|
.do_gettimeofday = do_memcpy_gettimeofday,
|
|
|
|
.usage = bench_mem_memcpy_usage,
|
2014-12-02 23:50:39 +08:00
|
|
|
};
|
|
|
|
|
2015-10-19 16:04:19 +08:00
|
|
|
return bench_mem_common(argc, argv, &info);
|
2014-12-02 23:50:39 +08:00
|
|
|
}
|
2014-12-02 23:50:40 +08:00
|
|
|
|
|
|
|
static void memset_alloc_mem(void **dst, size_t length)
|
|
|
|
{
|
|
|
|
*dst = zalloc(length);
|
|
|
|
if (!*dst)
|
|
|
|
die("memory allocation failed - maybe length is too large?\n");
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
static u64 do_memset_cycles(const struct routine *r, size_t len)
|
2014-12-02 23:50:40 +08:00
|
|
|
{
|
|
|
|
u64 cycle_start = 0ULL, cycle_end = 0ULL;
|
|
|
|
memset_t fn = r->fn.memset;
|
|
|
|
void *dst = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset_alloc_mem(&dst, len);
|
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
/*
|
|
|
|
* We prefault the freshly allocated memory range here,
|
|
|
|
* to not measure page fault overhead:
|
|
|
|
*/
|
|
|
|
fn(dst, -1, len);
|
2014-12-02 23:50:40 +08:00
|
|
|
|
2015-10-19 16:04:23 +08:00
|
|
|
cycle_start = get_cycles();
|
2014-12-02 23:50:40 +08:00
|
|
|
for (i = 0; i < iterations; ++i)
|
|
|
|
fn(dst, i, len);
|
2015-10-19 16:04:23 +08:00
|
|
|
cycle_end = get_cycles();
|
2014-12-02 23:50:40 +08:00
|
|
|
|
|
|
|
free(dst);
|
|
|
|
return cycle_end - cycle_start;
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
static double do_memset_gettimeofday(const struct routine *r, size_t len)
|
2014-12-02 23:50:40 +08:00
|
|
|
{
|
|
|
|
struct timeval tv_start, tv_end, tv_diff;
|
|
|
|
memset_t fn = r->fn.memset;
|
|
|
|
void *dst = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset_alloc_mem(&dst, len);
|
|
|
|
|
2015-10-19 16:04:21 +08:00
|
|
|
/*
|
|
|
|
* We prefault the freshly allocated memory range here,
|
|
|
|
* to not measure page fault overhead:
|
|
|
|
*/
|
|
|
|
fn(dst, -1, len);
|
2014-12-02 23:50:40 +08:00
|
|
|
|
|
|
|
BUG_ON(gettimeofday(&tv_start, NULL));
|
|
|
|
for (i = 0; i < iterations; ++i)
|
|
|
|
fn(dst, i, len);
|
|
|
|
BUG_ON(gettimeofday(&tv_end, NULL));
|
|
|
|
|
|
|
|
timersub(&tv_end, &tv_start, &tv_diff);
|
|
|
|
|
|
|
|
free(dst);
|
2014-12-02 23:50:41 +08:00
|
|
|
return (double)(((double)len * iterations) / timeval2double(&tv_diff));
|
2014-12-02 23:50:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const bench_mem_memset_usage[] = {
|
|
|
|
"perf bench mem memset <options>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct routine memset_routines[] = {
|
2015-10-19 16:04:17 +08:00
|
|
|
{ .name = "default",
|
|
|
|
.desc = "Default memset() provided by glibc",
|
|
|
|
.fn.memset = memset },
|
2014-12-02 23:50:40 +08:00
|
|
|
|
2015-10-19 16:04:17 +08:00
|
|
|
#ifdef HAVE_ARCH_X86_64_SUPPORT
|
|
|
|
# define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
|
|
|
|
# include "mem-memset-x86-64-asm-def.h"
|
|
|
|
# undef MEMSET_FN
|
2014-12-02 23:50:40 +08:00
|
|
|
#endif
|
|
|
|
|
2015-10-19 16:04:17 +08:00
|
|
|
{ NULL, }
|
2014-12-02 23:50:40 +08:00
|
|
|
};
|
|
|
|
|
2015-10-19 16:04:17 +08:00
|
|
|
int bench_mem_memset(int argc, const char **argv, const char *prefix __maybe_unused)
|
2014-12-02 23:50:40 +08:00
|
|
|
{
|
|
|
|
struct bench_mem_info info = {
|
2015-10-19 16:04:17 +08:00
|
|
|
.routines = memset_routines,
|
2015-10-19 16:04:23 +08:00
|
|
|
.do_cycles = do_memset_cycles,
|
2015-10-19 16:04:17 +08:00
|
|
|
.do_gettimeofday = do_memset_gettimeofday,
|
|
|
|
.usage = bench_mem_memset_usage,
|
2014-12-02 23:50:40 +08:00
|
|
|
};
|
|
|
|
|
2015-10-19 16:04:19 +08:00
|
|
|
return bench_mem_common(argc, argv, &info);
|
2014-12-02 23:50:40 +08:00
|
|
|
}
|