perf bench: Add a multi-threaded synthesize benchmark

By default this isn't run as it reads /proc and may not have access.
For consistency, modify the single threaded benchmark to compute an
average time per event.

Committer testing:

  $ grep -m1 "model name" /proc/cpuinfo
  model name	: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
  $ grep "model name" /proc/cpuinfo  | wc -l
  8
  $
  $ perf bench internals synthesize -h
  # Running 'internals/synthesize' benchmark:

   Usage: perf bench internals synthesize <options>

      -I, --multi-iterations <n>
                            Number of iterations used to compute multi-threaded average
      -i, --single-iterations <n>
                            Number of iterations used to compute single-threaded average
      -M, --max-threads <n>
                            Maximum number of threads in multithreaded bench
      -m, --min-threads <n>
                            Minimum number of threads in multithreaded bench
      -s, --st              Run single threaded benchmark
      -t, --mt              Run multi-threaded benchmark

  $
  $ perf bench internals synthesize -t
  # Running 'internals/synthesize' benchmark:
  Computing performance of multi threaded perf event synthesis by
  synthesizing events on CPU 0:
    Number of synthesis threads: 1
      Average synthesis took: 65449.000 usec (+- 586.442 usec)
      Average num. events: 9405.400 (+- 0.306)
      Average time per event 6.959 usec
    Number of synthesis threads: 2
      Average synthesis took: 37838.300 usec (+- 130.259 usec)
      Average num. events: 9501.800 (+- 20.469)
      Average time per event 3.982 usec
    Number of synthesis threads: 3
      Average synthesis took: 48551.400 usec (+- 225.686 usec)
      Average num. events: 9544.000 (+- 0.000)
      Average time per event 5.087 usec
    Number of synthesis threads: 4
      Average synthesis took: 29632.500 usec (+- 50.808 usec)
      Average num. events: 9544.000 (+- 0.000)
      Average time per event 3.105 usec
    Number of synthesis threads: 5
      Average synthesis took: 33920.400 usec (+- 284.509 usec)
      Average num. events: 9544.000 (+- 0.000)
      Average time per event 3.554 usec
    Number of synthesis threads: 6
      Average synthesis took: 27604.100 usec (+- 72.344 usec)
      Average num. events: 9548.000 (+- 0.000)
      Average time per event 2.891 usec
    Number of synthesis threads: 7
      Average synthesis took: 25406.300 usec (+- 933.371 usec)
      Average num. events: 9545.500 (+- 0.167)
      Average time per event 2.662 usec
    Number of synthesis threads: 8
      Average synthesis took: 24110.400 usec (+- 73.229 usec)
      Average num. events: 9551.000 (+- 0.000)
      Average time per event 2.524 usec
  $

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andrey Zhizhikin <andrey.z@gmail.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lore.kernel.org/lkml/20200415054050.31645-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ian Rogers 2020-04-14 22:40:48 -07:00 committed by Arnaldo Carvalho de Melo
parent d99c22eabe
commit 13edc23720
1 changed files with 186 additions and 25 deletions

View File

@ -10,60 +10,105 @@
#include "bench.h" #include "bench.h"
#include "../util/debug.h" #include "../util/debug.h"
#include "../util/session.h" #include "../util/session.h"
#include "../util/stat.h"
#include "../util/synthetic-events.h" #include "../util/synthetic-events.h"
#include "../util/target.h" #include "../util/target.h"
#include "../util/thread_map.h" #include "../util/thread_map.h"
#include "../util/tool.h" #include "../util/tool.h"
#include "../util/util.h"
#include <linux/atomic.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/time64.h> #include <linux/time64.h>
#include <subcmd/parse-options.h> #include <subcmd/parse-options.h>
static unsigned int iterations = 10000; static unsigned int min_threads = 1;
static unsigned int max_threads = UINT_MAX;
static unsigned int single_iterations = 10000;
static unsigned int multi_iterations = 10;
static bool run_st;
static bool run_mt;
static const struct option options[] = { static const struct option options[] = {
OPT_UINTEGER('i', "iterations", &iterations, OPT_BOOLEAN('s', "st", &run_st, "Run single threaded benchmark"),
"Number of iterations used to compute average"), OPT_BOOLEAN('t', "mt", &run_mt, "Run multi-threaded benchmark"),
OPT_UINTEGER('m', "min-threads", &min_threads,
"Minimum number of threads in multithreaded bench"),
OPT_UINTEGER('M', "max-threads", &max_threads,
"Maximum number of threads in multithreaded bench"),
OPT_UINTEGER('i', "single-iterations", &single_iterations,
"Number of iterations used to compute single-threaded average"),
OPT_UINTEGER('I', "multi-iterations", &multi_iterations,
"Number of iterations used to compute multi-threaded average"),
OPT_END() OPT_END()
}; };
static const char *const usage[] = { static const char *const bench_usage[] = {
"perf bench internals synthesize <options>", "perf bench internals synthesize <options>",
NULL NULL
}; };
static atomic_t event_count;
static int do_synthesize(struct perf_session *session, static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
struct perf_thread_map *threads, union perf_event *event __maybe_unused,
struct target *target, bool data_mmap) struct perf_sample *sample __maybe_unused,
struct machine *machine __maybe_unused)
{
atomic_inc(&event_count);
return 0;
}
static int do_run_single_threaded(struct perf_session *session,
struct perf_thread_map *threads,
struct target *target, bool data_mmap)
{ {
const unsigned int nr_threads_synthesize = 1; const unsigned int nr_threads_synthesize = 1;
struct timeval start, end, diff; struct timeval start, end, diff;
u64 runtime_us; u64 runtime_us;
unsigned int i; unsigned int i;
double average; double time_average, time_stddev, event_average, event_stddev;
int err; int err;
struct stats time_stats, event_stats;
gettimeofday(&start, NULL); init_stats(&time_stats);
for (i = 0; i < iterations; i++) { init_stats(&event_stats);
err = machine__synthesize_threads(&session->machines.host,
target, threads, data_mmap, for (i = 0; i < single_iterations; i++) {
atomic_set(&event_count, 0);
gettimeofday(&start, NULL);
err = __machine__synthesize_threads(&session->machines.host,
NULL,
target, threads,
process_synthesized_event,
data_mmap,
nr_threads_synthesize); nr_threads_synthesize);
if (err) if (err)
return err; return err;
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
update_stats(&time_stats, runtime_us);
update_stats(&event_stats, atomic_read(&event_count));
} }
gettimeofday(&end, NULL); time_average = avg_stats(&time_stats);
timersub(&end, &start, &diff); time_stddev = stddev_stats(&time_stats);
runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec; printf(" Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
average = (double)runtime_us/(double)iterations; data_mmap ? "data " : "", time_average, time_stddev);
printf("Average %ssynthesis took: %f usec\n",
data_mmap ? "data " : "", average); event_average = avg_stats(&event_stats);
event_stddev = stddev_stats(&event_stats);
printf(" Average num. events: %.3f (+- %.3f)\n",
event_average, event_stddev);
printf(" Average time per event %.3f usec\n",
time_average / event_average);
return 0; return 0;
} }
int bench_synthesize(int argc, const char **argv) static int run_single_threaded(void)
{ {
struct perf_tool tool;
struct perf_session *session; struct perf_session *session;
struct target target = { struct target target = {
.pid = "self", .pid = "self",
@ -71,8 +116,7 @@ int bench_synthesize(int argc, const char **argv)
struct perf_thread_map *threads; struct perf_thread_map *threads;
int err; int err;
argc = parse_options(argc, argv, options, usage, 0); perf_set_singlethreaded();
session = perf_session__new(NULL, false, NULL); session = perf_session__new(NULL, false, NULL);
if (IS_ERR(session)) { if (IS_ERR(session)) {
pr_err("Session creation failed.\n"); pr_err("Session creation failed.\n");
@ -84,13 +128,16 @@ int bench_synthesize(int argc, const char **argv)
err = -ENOMEM; err = -ENOMEM;
goto err_out; goto err_out;
} }
perf_tool__fill_defaults(&tool);
err = do_synthesize(session, threads, &target, false); puts(
"Computing performance of single threaded perf event synthesis by\n"
"synthesizing events on the perf process itself:");
err = do_run_single_threaded(session, threads, &target, false);
if (err) if (err)
goto err_out; goto err_out;
err = do_synthesize(session, threads, &target, true); err = do_run_single_threaded(session, threads, &target, true);
err_out: err_out:
if (threads) if (threads)
@ -99,3 +146,117 @@ err_out:
perf_session__delete(session); perf_session__delete(session);
return err; return err;
} }
static int do_run_multi_threaded(struct target *target,
unsigned int nr_threads_synthesize)
{
struct timeval start, end, diff;
u64 runtime_us;
unsigned int i;
double time_average, time_stddev, event_average, event_stddev;
int err;
struct stats time_stats, event_stats;
struct perf_session *session;
init_stats(&time_stats);
init_stats(&event_stats);
for (i = 0; i < multi_iterations; i++) {
session = perf_session__new(NULL, false, NULL);
if (!session)
return -ENOMEM;
atomic_set(&event_count, 0);
gettimeofday(&start, NULL);
err = __machine__synthesize_threads(&session->machines.host,
NULL,
target, NULL,
process_synthesized_event,
false,
nr_threads_synthesize);
if (err) {
perf_session__delete(session);
return err;
}
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
update_stats(&time_stats, runtime_us);
update_stats(&event_stats, atomic_read(&event_count));
perf_session__delete(session);
}
time_average = avg_stats(&time_stats);
time_stddev = stddev_stats(&time_stats);
printf(" Average synthesis took: %.3f usec (+- %.3f usec)\n",
time_average, time_stddev);
event_average = avg_stats(&event_stats);
event_stddev = stddev_stats(&event_stats);
printf(" Average num. events: %.3f (+- %.3f)\n",
event_average, event_stddev);
printf(" Average time per event %.3f usec\n",
time_average / event_average);
return 0;
}
static int run_multi_threaded(void)
{
struct target target = {
.cpu_list = "0"
};
unsigned int nr_threads_synthesize;
int err;
if (max_threads == UINT_MAX)
max_threads = sysconf(_SC_NPROCESSORS_ONLN);
puts(
"Computing performance of multi threaded perf event synthesis by\n"
"synthesizing events on CPU 0:");
for (nr_threads_synthesize = min_threads;
nr_threads_synthesize <= max_threads;
nr_threads_synthesize++) {
if (nr_threads_synthesize == 1)
perf_set_singlethreaded();
else
perf_set_multithreaded();
printf(" Number of synthesis threads: %u\n",
nr_threads_synthesize);
err = do_run_multi_threaded(&target, nr_threads_synthesize);
if (err)
return err;
}
perf_set_singlethreaded();
return 0;
}
int bench_synthesize(int argc, const char **argv)
{
int err = 0;
argc = parse_options(argc, argv, options, bench_usage, 0);
if (argc) {
usage_with_options(bench_usage, options);
exit(EXIT_FAILURE);
}
/*
* If neither single threaded or multi-threaded are specified, default
* to running just single threaded.
*/
if (!run_st && !run_mt)
run_st = true;
if (run_st)
err = run_single_threaded();
if (!err && run_mt)
err = run_multi_threaded();
return err;
}