Commit 49ce8fc6 authored by Hitoshi Mitake's avatar Hitoshi Mitake Committed by Ingo Molnar

perf bench: Print both of prefaulted and no prefaulted results by default

After applying this patch, perf bench mem memcpy prints
both of prefualted and without prefaulted score of memcpy().

New options --no-prefault and --only-prefault are added
to print single result, mainly for scripting usage.

Usage example:

 | mitake@X201i:~/linux/.../tools/perf% ./perf bench mem memcpy -l 500MB
 | # Running mem/memcpy benchmark...
 | # Copying 500MB Bytes ...
 |
 |      634.969014 MB/Sec
 |        4.828062 GB/Sec (with prefault)
 | mitake@X201i:~/linux/.../tools/perf% ./perf bench mem memcpy -l 500MB --only-prefault
 | # Running mem/memcpy benchmark...
 | # Copying 500MB Bytes ...
 |
 |        4.705192 GB/Sec (with prefault)
 | mitake@X201i:~/linux/.../tools/perf% ./perf bench mem memcpy -l 500MB --no-prefault
 | # Running mem/memcpy benchmark...
 | # Copying 500MB Bytes ...
 |
 |      642.725568 MB/Sec
Signed-off-by: default avatarHitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
Cc: h.mitake@gmail.com
Cc: Miao Xie <miaox@cn.fujitsu.com>
Cc: Ma Ling <ling.ma@intel.com>
Cc: Zhao Yakui <yakui.zhao@intel.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Andi Kleen <andi@firstfloor.org>
LKML-Reference: <1290668693-27068-1-git-send-email-mitake@dcl.info.waseda.ac.jp>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent d9cf837e
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "../util/parse-options.h" #include "../util/parse-options.h"
#include "../util/header.h" #include "../util/header.h"
#include "bench.h" #include "bench.h"
#include "mem-memcpy-arch.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -23,8 +24,10 @@ ...@@ -23,8 +24,10 @@
static const char *length_str = "1MB"; static const char *length_str = "1MB";
static const char *routine = "default"; static const char *routine = "default";
static bool use_clock = false; static bool use_clock;
static int clock_fd; static int clock_fd;
static bool only_prefault;
static bool no_prefault;
static const struct option options[] = { static const struct option options[] = {
OPT_STRING('l', "length", &length_str, "1MB", OPT_STRING('l', "length", &length_str, "1MB",
...@@ -34,19 +37,33 @@ static const struct option options[] = { ...@@ -34,19 +37,33 @@ static const struct option options[] = {
"Specify routine to copy"), "Specify routine to copy"),
OPT_BOOLEAN('c', "clock", &use_clock, OPT_BOOLEAN('c', "clock", &use_clock,
"Use CPU clock for measuring"), "Use CPU clock for measuring"),
OPT_BOOLEAN('o', "only-prefault", &only_prefault,
"Show only the result with page faults before memcpy()"),
OPT_BOOLEAN('n', "no-prefault", &no_prefault,
"Show only the result without page faults before memcpy()"),
OPT_END() OPT_END()
}; };
typedef void *(*memcpy_t)(void *, const void *, size_t);
struct routine { struct routine {
const char *name; const char *name;
const char *desc; const char *desc;
void * (*fn)(void *dst, const void *src, size_t len); memcpy_t fn;
}; };
struct routine routines[] = { struct routine routines[] = {
{ "default", { "default",
"Default memcpy() provided by glibc", "Default memcpy() provided by glibc",
memcpy }, memcpy },
#ifdef ARCH_X86_64
#define MEMCPY_FN(fn, name, desc) { name, desc, fn },
#include "mem-memcpy-x86-64-asm-def.h"
#undef MEMCPY_FN
#endif
{ NULL, { NULL,
NULL, NULL,
NULL } NULL }
...@@ -89,29 +106,98 @@ static double timeval2double(struct timeval *ts) ...@@ -89,29 +106,98 @@ static double timeval2double(struct timeval *ts)
(double)ts->tv_usec / (double)1000000; (double)ts->tv_usec / (double)1000000;
} }
static void 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");
}
static u64 do_memcpy_clock(memcpy_t fn, size_t len, bool prefault)
{
u64 clock_start = 0ULL, clock_end = 0ULL;
void *src = NULL, *dst = NULL;
alloc_mem(&src, &dst, len);
if (prefault)
fn(dst, src, len);
clock_start = get_clock();
fn(dst, src, len);
clock_end = get_clock();
free(src);
free(dst);
return clock_end - clock_start;
}
static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
{
struct timeval tv_start, tv_end, tv_diff;
void *src = NULL, *dst = NULL;
alloc_mem(&src, &dst, len);
if (prefault)
fn(dst, src, len);
BUG_ON(gettimeofday(&tv_start, NULL));
fn(dst, src, len);
BUG_ON(gettimeofday(&tv_end, NULL));
timersub(&tv_end, &tv_start, &tv_diff);
free(src);
free(dst);
return (double)((double)len / timeval2double(&tv_diff));
}
#define pf (no_prefault ? 0 : 1)
#define print_bps(x) do { \
if (x < K) \
printf(" %14lf B/Sec", x); \
else if (x < K * K) \
printf(" %14lfd KB/Sec", x / K); \
else if (x < K * K * K) \
printf(" %14lf MB/Sec", x / K / K); \
else \
printf(" %14lf GB/Sec", x / K / K / K); \
} while (0)
int bench_mem_memcpy(int argc, const char **argv, int bench_mem_memcpy(int argc, const char **argv,
const char *prefix __used) const char *prefix __used)
{ {
int i; int i;
void *dst, *src; size_t len;
size_t length; double result_bps[2];
double bps = 0.0; u64 result_clock[2];
struct timeval tv_start, tv_end, tv_diff;
u64 clock_start, clock_end, clock_diff;
clock_start = clock_end = clock_diff = 0ULL;
argc = parse_options(argc, argv, options, argc = parse_options(argc, argv, options,
bench_mem_memcpy_usage, 0); bench_mem_memcpy_usage, 0);
tv_diff.tv_sec = 0; if (use_clock)
tv_diff.tv_usec = 0; init_clock();
length = (size_t)perf_atoll((char *)length_str);
len = (size_t)perf_atoll((char *)length_str);
if ((s64)length <= 0) { result_clock[0] = result_clock[1] = 0ULL;
result_bps[0] = result_bps[1] = 0.0;
if ((s64)len <= 0) {
fprintf(stderr, "Invalid length:%s\n", length_str); fprintf(stderr, "Invalid length:%s\n", length_str);
return 1; return 1;
} }
/* same to without specifying either of prefault and no-prefault */
if (only_prefault && no_prefault)
only_prefault = no_prefault = false;
for (i = 0; routines[i].name; i++) { for (i = 0; routines[i].name; i++) {
if (!strcmp(routines[i].name, routine)) if (!strcmp(routines[i].name, routine))
break; break;
...@@ -126,61 +212,80 @@ int bench_mem_memcpy(int argc, const char **argv, ...@@ -126,61 +212,80 @@ int bench_mem_memcpy(int argc, const char **argv,
return 1; return 1;
} }
dst = zalloc(length); if (bench_format == BENCH_FORMAT_DEFAULT)
if (!dst) printf("# Copying %s Bytes ...\n\n", length_str);
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");
if (bench_format == BENCH_FORMAT_DEFAULT) {
printf("# Copying %s Bytes from %p to %p ...\n\n",
length_str, src, dst);
}
if (!only_prefault && !no_prefault) {
/* show both of results */
if (use_clock) { if (use_clock) {
init_clock(); result_clock[0] =
clock_start = get_clock(); do_memcpy_clock(routines[i].fn, len, false);
result_clock[1] =
do_memcpy_clock(routines[i].fn, len, true);
} else { } else {
BUG_ON(gettimeofday(&tv_start, NULL)); result_bps[0] =
do_memcpy_gettimeofday(routines[i].fn,
len, false);
result_bps[1] =
do_memcpy_gettimeofday(routines[i].fn,
len, true);
} }
} else {
routines[i].fn(dst, src, length);
if (use_clock) { if (use_clock) {
clock_end = get_clock(); result_clock[pf] =
clock_diff = clock_end - clock_start; do_memcpy_clock(routines[i].fn,
len, only_prefault);
} else { } else {
BUG_ON(gettimeofday(&tv_end, NULL)); result_bps[pf] =
timersub(&tv_end, &tv_start, &tv_diff); do_memcpy_gettimeofday(routines[i].fn,
bps = (double)((double)length / timeval2double(&tv_diff)); len, only_prefault);
}
} }
switch (bench_format) { switch (bench_format) {
case BENCH_FORMAT_DEFAULT: case BENCH_FORMAT_DEFAULT:
if (!only_prefault && !no_prefault) {
if (use_clock) { if (use_clock) {
printf(" %14lf Clock/Byte\n", printf(" %14lf Clock/Byte\n",
(double)clock_diff / (double)length); (double)result_clock[0]
/ (double)len);
printf(" %14lf Clock/Byte (with prefault)\n",
(double)result_clock[1]
/ (double)len);
} else { } else {
if (bps < K) print_bps(result_bps[0]);
printf(" %14lf B/Sec\n", bps); printf("\n");
else if (bps < K * K) print_bps(result_bps[1]);
printf(" %14lfd KB/Sec\n", bps / 1024); printf(" (with prefault)\n");
else if (bps < K * K * K)
printf(" %14lf MB/Sec\n", bps / 1024 / 1024);
else {
printf(" %14lf GB/Sec\n",
bps / 1024 / 1024 / 1024);
} }
} else {
if (use_clock) {
printf(" %14lf Clock/Byte",
(double)result_clock[pf]
/ (double)len);
} else
print_bps(result_bps[pf]);
printf("%s\n", only_prefault ? " (with prefault)" : "");
} }
break; break;
case BENCH_FORMAT_SIMPLE: case BENCH_FORMAT_SIMPLE:
if (!only_prefault && !no_prefault) {
if (use_clock) {
printf("%lf %lf\n",
(double)result_clock[0] / (double)len,
(double)result_clock[1] / (double)len);
} else {
printf("%lf %lf\n",
result_bps[0], result_bps[1]);
}
} else {
if (use_clock) { if (use_clock) {
printf("%14lf\n", printf("%lf\n", (double)result_clock[pf]
(double)clock_diff / (double)length); / (double)len);
} else } else
printf("%lf\n", bps); printf("%lf\n", result_bps[pf]);
}
break; break;
default: default:
/* reaching this means there's some disaster: */ /* reaching this means there's some disaster: */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment