All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools/perf: add breakpoint benchmarks
@ 2022-05-05 15:57 Dmitry Vyukov
  2022-05-06  7:58 ` Dmitry Vyukov
  2022-05-11 15:34 ` Ian Rogers
  0 siblings, 2 replies; 7+ messages in thread
From: Dmitry Vyukov @ 2022-05-05 15:57 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa, namhyung
  Cc: elver, Dmitry Vyukov, linux-perf-users, linux-kernel

Add 2 benchmarks:
1. Performance of thread creation/exiting in presence of breakpoints.
2. Performance of breakpoint modification in presence of threads.

The benchmarks capture use cases that we are interested in:
using inheritable breakpoints in large highly-threaded applications.
The benchmarks show significant slowdown imposed by breakpoints
(even when they don't fire).

Testing on Intel 8173M with 112 HW threads show:

perf bench --repeat=56 breakpoint thread --breakpoints=0 --parallelism=56 --threads=20
      78.675000 usecs/op
perf bench --repeat=56 breakpoint thread --breakpoints=4 --parallelism=56 --threads=20
   12967.135714 usecs/op
That's 165x slowdown due to presence of the breakpoints.

perf bench --repeat=20000 breakpoint enable --passive=0 --active=0
       1.433250 usecs/op
perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
     585.318400 usecs/op
perf bench --repeat=20000 breakpoint enable --passive=0 --active=111
     635.953000 usecs/op
That's 408x and 444x slowdown due to presence of threads.

Profiles show some overhead in toggle_bp_slot,
but also very high contention:

    90.83%  breakpoint-thre  [kernel.kallsyms]  [k] osq_lock
     4.69%  breakpoint-thre  [kernel.kallsyms]  [k] mutex_spin_on_owner
     2.06%  breakpoint-thre  [kernel.kallsyms]  [k] __reserve_bp_slot
     2.04%  breakpoint-thre  [kernel.kallsyms]  [k] toggle_bp_slot

    79.01%  breakpoint-enab  [kernel.kallsyms]  [k] smp_call_function_single
     9.94%  breakpoint-enab  [kernel.kallsyms]  [k] llist_add_batch
     5.70%  breakpoint-enab  [kernel.kallsyms]  [k] _raw_spin_lock_irq
     1.84%  breakpoint-enab  [kernel.kallsyms]  [k] event_function_call
     1.12%  breakpoint-enab  [kernel.kallsyms]  [k] send_call_function_single_ipi
     0.37%  breakpoint-enab  [kernel.kallsyms]  [k] generic_exec_single
     0.24%  breakpoint-enab  [kernel.kallsyms]  [k] __perf_event_disable
     0.20%  breakpoint-enab  [kernel.kallsyms]  [k] _perf_event_enable
     0.18%  breakpoint-enab  [kernel.kallsyms]  [k] toggle_bp_slot

Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
Cc: linux-perf-users@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 tools/perf/bench/Build        |   1 +
 tools/perf/bench/bench.h      |   2 +
 tools/perf/bench/breakpoint.c | 235 ++++++++++++++++++++++++++++++++++
 tools/perf/builtin-bench.c    |   8 ++
 4 files changed, 246 insertions(+)
 create mode 100644 tools/perf/bench/breakpoint.c

diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build
index 61d45fcb4057c..6b6155a8ad096 100644
--- a/tools/perf/bench/Build
+++ b/tools/perf/bench/Build
@@ -14,6 +14,7 @@ perf-y += kallsyms-parse.o
 perf-y += find-bit-bench.o
 perf-y += inject-buildid.o
 perf-y += evlist-open-close.o
+perf-y += breakpoint.o
 
 perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
 perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
index b3480bc33fe84..6cefb4315d75e 100644
--- a/tools/perf/bench/bench.h
+++ b/tools/perf/bench/bench.h
@@ -49,6 +49,8 @@ int bench_synthesize(int argc, const char **argv);
 int bench_kallsyms_parse(int argc, const char **argv);
 int bench_inject_build_id(int argc, const char **argv);
 int bench_evlist_open_close(int argc, const char **argv);
+int bench_breakpoint_thread(int argc, const char **argv);
+int bench_breakpoint_enable(int argc, const char **argv);
 
 #define BENCH_FORMAT_DEFAULT_STR	"default"
 #define BENCH_FORMAT_DEFAULT		0
diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
new file mode 100644
index 0000000000000..56936fea246d7
--- /dev/null
+++ b/tools/perf/bench/breakpoint.c
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <subcmd/parse-options.h>
+#include <linux/hw_breakpoint.h>
+#include <linux/perf_event.h>
+#include <linux/time64.h>
+#include <sys/syscall.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <pthread.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include "bench.h"
+#include "futex.h"
+
+struct {
+	unsigned int nbreakpoints;
+	unsigned int nparallel;
+	unsigned int nthreads;
+} thread_params = {
+	.nbreakpoints = 1,
+	.nparallel = 1,
+	.nthreads = 1,
+};
+
+static const struct option thread_options[] = {
+	OPT_UINTEGER('b', "breakpoints", &thread_params.nbreakpoints,
+		"Specify amount of breakpoints"),
+	OPT_UINTEGER('p', "parallelism", &thread_params.nparallel, "Specify amount of parallelism"),
+	OPT_UINTEGER('t', "threads", &thread_params.nthreads, "Specify amount of threads"),
+	OPT_END()
+};
+
+static const char * const thread_usage[] = {
+	"perf bench breakpoint thread <options>",
+	NULL
+};
+
+struct breakpoint {
+	int fd;
+	char watched;
+};
+
+static int breakpoint_setup(void *addr)
+{
+	struct perf_event_attr attr = {0};
+
+	attr.type = PERF_TYPE_BREAKPOINT;
+	attr.size = sizeof(attr);
+	attr.inherit = 1;
+	attr.exclude_kernel = 1;
+	attr.exclude_hv = 1;
+	attr.bp_addr = (uint64_t)addr;
+	attr.bp_type = HW_BREAKPOINT_RW;
+	attr.bp_len = HW_BREAKPOINT_LEN_1;
+	return syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
+}
+
+static void *passive_thread(void *arg)
+{
+	unsigned int *done = (unsigned int *)arg;
+
+	while (!__atomic_load_n(done, __ATOMIC_RELAXED))
+		futex_wait(done, 0, NULL, 0);
+	return NULL;
+}
+
+static void *active_thread(void *arg)
+{
+	unsigned int *done = (unsigned int *)arg;
+
+	while (!__atomic_load_n(done, __ATOMIC_RELAXED));
+	return NULL;
+}
+
+static void *breakpoint_thread(void *arg)
+{
+	unsigned int i, done;
+	int *repeat = (int *)arg;
+	pthread_t *threads;
+
+	threads = calloc(thread_params.nthreads, sizeof(threads[0]));
+	while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
+		done = 0;
+		for (i = 0; i < thread_params.nthreads; i++) {
+			if (pthread_create(&threads[i], NULL, passive_thread, &done))
+				exit((perror("pthread_create"), EXIT_FAILURE));
+		}
+		__atomic_store_n(&done, 1, __ATOMIC_RELAXED);
+		futex_wake(&done, thread_params.nthreads, 0);
+		for (i = 0; i < thread_params.nthreads; i++)
+			pthread_join(threads[i], NULL);
+	}
+	free(threads);
+	return NULL;
+}
+
+// The benchmark creates nbreakpoints inheritable breakpoints,
+// then starts nparallel threads which create and join bench_repeat batches of nthreads threads.
+int bench_breakpoint_thread(int argc, const char **argv)
+{
+	unsigned int i, result_usec;
+	int repeat = bench_repeat;
+	struct breakpoint *breakpoints;
+	pthread_t *parallel;
+	struct timeval start, stop, diff;
+
+	if (parse_options(argc, argv, thread_options, thread_usage, 0)) {
+		usage_with_options(thread_usage, thread_options);
+		exit(EXIT_FAILURE);
+	}
+	breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
+	parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
+	for (i = 0; i < thread_params.nbreakpoints; i++) {
+		breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
+		if (breakpoints[i].fd == -1)
+			exit((perror("perf_event_open"), EXIT_FAILURE));
+	}
+	gettimeofday(&start, NULL);
+	for (i = 0; i < thread_params.nparallel; i++) {
+		if (pthread_create(&parallel[i], NULL, breakpoint_thread, &repeat))
+			exit((perror("pthread_create"), EXIT_FAILURE));
+	}
+	for (i = 0; i < thread_params.nparallel; i++)
+		pthread_join(parallel[i], NULL);
+	gettimeofday(&stop, NULL);
+	timersub(&stop, &start, &diff);
+	for (i = 0; i < thread_params.nbreakpoints; i++)
+		close(breakpoints[i].fd);
+	free(parallel);
+	free(breakpoints);
+	switch (bench_format) {
+	case BENCH_FORMAT_DEFAULT:
+		printf("# Created/joined %d threads with %d breakpoints and %d parallelism\n",
+			bench_repeat, thread_params.nbreakpoints, thread_params.nparallel);
+		printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
+			(long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
+		result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
+		printf(" %14lf usecs/op\n",
+			(double)result_usec / bench_repeat / thread_params.nthreads);
+		printf(" %14lf usecs/op/cpu\n",
+			(double)result_usec / bench_repeat /
+			thread_params.nthreads * thread_params.nparallel);
+		break;
+	case BENCH_FORMAT_SIMPLE:
+		printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
+		break;
+	default:
+		fprintf(stderr, "Unknown format: %d\n", bench_format);
+		exit(EXIT_FAILURE);
+	}
+	return 0;
+}
+
+struct {
+	unsigned int npassive;
+	unsigned int nactive;
+} enable_params = {
+	.nactive = 0,
+	.npassive = 0,
+};
+
+static const struct option enable_options[] = {
+	OPT_UINTEGER('p', "passive", &enable_params.npassive, "Specify amount of passive threads"),
+	OPT_UINTEGER('a', "active", &enable_params.nactive, "Specify amount of active threads"),
+	OPT_END()
+};
+
+static const char * const enable_usage[] = {
+	"perf bench breakpoint enable <options>",
+	NULL
+};
+
+// The benchmark creates an inheritable breakpoint,
+// then starts npassive threads that block and nactive threads that actively spin
+// and then disables and enables the breakpoint bench_repeat times.
+int bench_breakpoint_enable(int argc, const char **argv)
+{
+	unsigned int i, nthreads, result_usec, done = 0;
+	char watched;
+	int fd;
+	pthread_t *threads;
+	struct timeval start, stop, diff;
+
+	if (parse_options(argc, argv, enable_options, enable_usage, 0)) {
+		usage_with_options(enable_usage, enable_options);
+		exit(EXIT_FAILURE);
+	}
+	fd = breakpoint_setup(&watched);
+	if (fd == -1)
+		exit((perror("perf_event_open"), EXIT_FAILURE));
+	nthreads = enable_params.npassive + enable_params.nactive;
+	threads = calloc(nthreads, sizeof(threads[0]));
+	for (i = 0; i < nthreads; i++) {
+		if (pthread_create(&threads[i], NULL,
+			i < enable_params.npassive ? passive_thread : active_thread, &done))
+			exit((perror("pthread_create"), EXIT_FAILURE));
+	}
+	usleep(10000);  // let the threads block
+	gettimeofday(&start, NULL);
+	for (i = 0; i < bench_repeat; i++) {
+		if (ioctl(fd, PERF_EVENT_IOC_DISABLE, 0))
+			exit((perror("ioctl(PERF_EVENT_IOC_DISABLE)"), EXIT_FAILURE));
+		if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0))
+			exit((perror("ioctl(PERF_EVENT_IOC_ENABLE)"), EXIT_FAILURE));
+	}
+	gettimeofday(&stop, NULL);
+	timersub(&stop, &start, &diff);
+	__atomic_store_n(&done, 1, __ATOMIC_RELAXED);
+	futex_wake(&done, enable_params.npassive, 0);
+	for (i = 0; i < nthreads; i++)
+		pthread_join(threads[i], NULL);
+	free(threads);
+	close(fd);
+	switch (bench_format) {
+	case BENCH_FORMAT_DEFAULT:
+		printf("# Enabled/disabled breakpoint %d time with %d passive and %d active threads\n",
+			bench_repeat, enable_params.npassive, enable_params.nactive);
+		printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
+			(long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
+		result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
+		printf(" %14lf usecs/op\n", (double)result_usec / bench_repeat);
+		break;
+	case BENCH_FORMAT_SIMPLE:
+		printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
+		break;
+	default:
+		fprintf(stderr, "Unknown format: %d\n", bench_format);
+		exit(EXIT_FAILURE);
+	}
+	return 0;
+}
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
index d291f3a8af5f2..334ab897aae3b 100644
--- a/tools/perf/builtin-bench.c
+++ b/tools/perf/builtin-bench.c
@@ -92,6 +92,13 @@ static struct bench internals_benchmarks[] = {
 	{ NULL,		NULL,					NULL			}
 };
 
+static struct bench breakpoint_benchmarks[] = {
+	{ "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
+	{ "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
+	{ "all", "Run all breakpoint benchmarks", NULL},
+	{ NULL,	NULL, NULL },
+};
+
 struct collection {
 	const char	*name;
 	const char	*summary;
@@ -110,6 +117,7 @@ static struct collection collections[] = {
 	{"epoll",       "Epoll stressing benchmarks",                   epoll_benchmarks        },
 #endif
 	{ "internals",	"Perf-internals benchmarks",			internals_benchmarks	},
+	{ "breakpoint",	"Breakpoint benchmarks",			breakpoint_benchmarks	},
 	{ "all",	"All benchmarks",				NULL			},
 	{ NULL,		NULL,						NULL			}
 };

base-commit: bd24325684029a48f20a188b899eb84900d0bc9c
-- 
2.36.0.464.gb9c8b46e94-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] tools/perf: add breakpoint benchmarks
  2022-05-05 15:57 [PATCH] tools/perf: add breakpoint benchmarks Dmitry Vyukov
@ 2022-05-06  7:58 ` Dmitry Vyukov
  2022-05-11 15:34 ` Ian Rogers
  1 sibling, 0 replies; 7+ messages in thread
From: Dmitry Vyukov @ 2022-05-06  7:58 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa, namhyung
  Cc: elver, linux-perf-users, linux-kernel

On Thu, 5 May 2022 at 17:57, Dmitry Vyukov <dvyukov@google.com> wrote:
>
> Add 2 benchmarks:
> 1. Performance of thread creation/exiting in presence of breakpoints.
> 2. Performance of breakpoint modification in presence of threads.
>
> The benchmarks capture use cases that we are interested in:
> using inheritable breakpoints in large highly-threaded applications.
> The benchmarks show significant slowdown imposed by breakpoints
> (even when they don't fire).

Question to perf maintainers:
Do you know any low-hanging fruits re breakpoint performance? Or did
you want to improve something, but never got to actually implementing
it?

Do you know what would be a good way to remove the global nr_bp_mutex?
Do you know of any opportunities to reduce the number of IPIs on
breakpoint modifications?

Thanks in advance

> Testing on Intel 8173M with 112 HW threads show:
>
> perf bench --repeat=56 breakpoint thread --breakpoints=0 --parallelism=56 --threads=20
>       78.675000 usecs/op
> perf bench --repeat=56 breakpoint thread --breakpoints=4 --parallelism=56 --threads=20
>    12967.135714 usecs/op
> That's 165x slowdown due to presence of the breakpoints.
>
> perf bench --repeat=20000 breakpoint enable --passive=0 --active=0
>        1.433250 usecs/op
> perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
>      585.318400 usecs/op
> perf bench --repeat=20000 breakpoint enable --passive=0 --active=111
>      635.953000 usecs/op
> That's 408x and 444x slowdown due to presence of threads.
>
> Profiles show some overhead in toggle_bp_slot,
> but also very high contention:
>
>     90.83%  breakpoint-thre  [kernel.kallsyms]  [k] osq_lock
>      4.69%  breakpoint-thre  [kernel.kallsyms]  [k] mutex_spin_on_owner
>      2.06%  breakpoint-thre  [kernel.kallsyms]  [k] __reserve_bp_slot
>      2.04%  breakpoint-thre  [kernel.kallsyms]  [k] toggle_bp_slot
>
>     79.01%  breakpoint-enab  [kernel.kallsyms]  [k] smp_call_function_single
>      9.94%  breakpoint-enab  [kernel.kallsyms]  [k] llist_add_batch
>      5.70%  breakpoint-enab  [kernel.kallsyms]  [k] _raw_spin_lock_irq
>      1.84%  breakpoint-enab  [kernel.kallsyms]  [k] event_function_call
>      1.12%  breakpoint-enab  [kernel.kallsyms]  [k] send_call_function_single_ipi
>      0.37%  breakpoint-enab  [kernel.kallsyms]  [k] generic_exec_single
>      0.24%  breakpoint-enab  [kernel.kallsyms]  [k] __perf_event_disable
>      0.20%  breakpoint-enab  [kernel.kallsyms]  [k] _perf_event_enable
>      0.18%  breakpoint-enab  [kernel.kallsyms]  [k] toggle_bp_slot
>
> Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
> Cc: linux-perf-users@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  tools/perf/bench/Build        |   1 +
>  tools/perf/bench/bench.h      |   2 +
>  tools/perf/bench/breakpoint.c | 235 ++++++++++++++++++++++++++++++++++
>  tools/perf/builtin-bench.c    |   8 ++
>  4 files changed, 246 insertions(+)
>  create mode 100644 tools/perf/bench/breakpoint.c
>
> diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build
> index 61d45fcb4057c..6b6155a8ad096 100644
> --- a/tools/perf/bench/Build
> +++ b/tools/perf/bench/Build
> @@ -14,6 +14,7 @@ perf-y += kallsyms-parse.o
>  perf-y += find-bit-bench.o
>  perf-y += inject-buildid.o
>  perf-y += evlist-open-close.o
> +perf-y += breakpoint.o
>
>  perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
>  perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
> diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
> index b3480bc33fe84..6cefb4315d75e 100644
> --- a/tools/perf/bench/bench.h
> +++ b/tools/perf/bench/bench.h
> @@ -49,6 +49,8 @@ int bench_synthesize(int argc, const char **argv);
>  int bench_kallsyms_parse(int argc, const char **argv);
>  int bench_inject_build_id(int argc, const char **argv);
>  int bench_evlist_open_close(int argc, const char **argv);
> +int bench_breakpoint_thread(int argc, const char **argv);
> +int bench_breakpoint_enable(int argc, const char **argv);
>
>  #define BENCH_FORMAT_DEFAULT_STR       "default"
>  #define BENCH_FORMAT_DEFAULT           0
> diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
> new file mode 100644
> index 0000000000000..56936fea246d7
> --- /dev/null
> +++ b/tools/perf/bench/breakpoint.c
> @@ -0,0 +1,235 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <subcmd/parse-options.h>
> +#include <linux/hw_breakpoint.h>
> +#include <linux/perf_event.h>
> +#include <linux/time64.h>
> +#include <sys/syscall.h>
> +#include <sys/ioctl.h>
> +#include <sys/time.h>
> +#include <pthread.h>
> +#include <stddef.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <stdio.h>
> +#include <errno.h>
> +#include "bench.h"
> +#include "futex.h"
> +
> +struct {
> +       unsigned int nbreakpoints;
> +       unsigned int nparallel;
> +       unsigned int nthreads;
> +} thread_params = {
> +       .nbreakpoints = 1,
> +       .nparallel = 1,
> +       .nthreads = 1,
> +};
> +
> +static const struct option thread_options[] = {
> +       OPT_UINTEGER('b', "breakpoints", &thread_params.nbreakpoints,
> +               "Specify amount of breakpoints"),
> +       OPT_UINTEGER('p', "parallelism", &thread_params.nparallel, "Specify amount of parallelism"),
> +       OPT_UINTEGER('t', "threads", &thread_params.nthreads, "Specify amount of threads"),
> +       OPT_END()
> +};
> +
> +static const char * const thread_usage[] = {
> +       "perf bench breakpoint thread <options>",
> +       NULL
> +};
> +
> +struct breakpoint {
> +       int fd;
> +       char watched;
> +};
> +
> +static int breakpoint_setup(void *addr)
> +{
> +       struct perf_event_attr attr = {0};
> +
> +       attr.type = PERF_TYPE_BREAKPOINT;
> +       attr.size = sizeof(attr);
> +       attr.inherit = 1;
> +       attr.exclude_kernel = 1;
> +       attr.exclude_hv = 1;
> +       attr.bp_addr = (uint64_t)addr;
> +       attr.bp_type = HW_BREAKPOINT_RW;
> +       attr.bp_len = HW_BREAKPOINT_LEN_1;
> +       return syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
> +}
> +
> +static void *passive_thread(void *arg)
> +{
> +       unsigned int *done = (unsigned int *)arg;
> +
> +       while (!__atomic_load_n(done, __ATOMIC_RELAXED))
> +               futex_wait(done, 0, NULL, 0);
> +       return NULL;
> +}
> +
> +static void *active_thread(void *arg)
> +{
> +       unsigned int *done = (unsigned int *)arg;
> +
> +       while (!__atomic_load_n(done, __ATOMIC_RELAXED));
> +       return NULL;
> +}
> +
> +static void *breakpoint_thread(void *arg)
> +{
> +       unsigned int i, done;
> +       int *repeat = (int *)arg;
> +       pthread_t *threads;
> +
> +       threads = calloc(thread_params.nthreads, sizeof(threads[0]));
> +       while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
> +               done = 0;
> +               for (i = 0; i < thread_params.nthreads; i++) {
> +                       if (pthread_create(&threads[i], NULL, passive_thread, &done))
> +                               exit((perror("pthread_create"), EXIT_FAILURE));
> +               }
> +               __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
> +               futex_wake(&done, thread_params.nthreads, 0);
> +               for (i = 0; i < thread_params.nthreads; i++)
> +                       pthread_join(threads[i], NULL);
> +       }
> +       free(threads);
> +       return NULL;
> +}
> +
> +// The benchmark creates nbreakpoints inheritable breakpoints,
> +// then starts nparallel threads which create and join bench_repeat batches of nthreads threads.
> +int bench_breakpoint_thread(int argc, const char **argv)
> +{
> +       unsigned int i, result_usec;
> +       int repeat = bench_repeat;
> +       struct breakpoint *breakpoints;
> +       pthread_t *parallel;
> +       struct timeval start, stop, diff;
> +
> +       if (parse_options(argc, argv, thread_options, thread_usage, 0)) {
> +               usage_with_options(thread_usage, thread_options);
> +               exit(EXIT_FAILURE);
> +       }
> +       breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
> +       parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
> +       for (i = 0; i < thread_params.nbreakpoints; i++) {
> +               breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
> +               if (breakpoints[i].fd == -1)
> +                       exit((perror("perf_event_open"), EXIT_FAILURE));
> +       }
> +       gettimeofday(&start, NULL);
> +       for (i = 0; i < thread_params.nparallel; i++) {
> +               if (pthread_create(&parallel[i], NULL, breakpoint_thread, &repeat))
> +                       exit((perror("pthread_create"), EXIT_FAILURE));
> +       }
> +       for (i = 0; i < thread_params.nparallel; i++)
> +               pthread_join(parallel[i], NULL);
> +       gettimeofday(&stop, NULL);
> +       timersub(&stop, &start, &diff);
> +       for (i = 0; i < thread_params.nbreakpoints; i++)
> +               close(breakpoints[i].fd);
> +       free(parallel);
> +       free(breakpoints);
> +       switch (bench_format) {
> +       case BENCH_FORMAT_DEFAULT:
> +               printf("# Created/joined %d threads with %d breakpoints and %d parallelism\n",
> +                       bench_repeat, thread_params.nbreakpoints, thread_params.nparallel);
> +               printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
> +                       (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> +               printf(" %14lf usecs/op\n",
> +                       (double)result_usec / bench_repeat / thread_params.nthreads);
> +               printf(" %14lf usecs/op/cpu\n",
> +                       (double)result_usec / bench_repeat /
> +                       thread_params.nthreads * thread_params.nparallel);
> +               break;
> +       case BENCH_FORMAT_SIMPLE:
> +               printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               break;
> +       default:
> +               fprintf(stderr, "Unknown format: %d\n", bench_format);
> +               exit(EXIT_FAILURE);
> +       }
> +       return 0;
> +}
> +
> +struct {
> +       unsigned int npassive;
> +       unsigned int nactive;
> +} enable_params = {
> +       .nactive = 0,
> +       .npassive = 0,
> +};
> +
> +static const struct option enable_options[] = {
> +       OPT_UINTEGER('p', "passive", &enable_params.npassive, "Specify amount of passive threads"),
> +       OPT_UINTEGER('a', "active", &enable_params.nactive, "Specify amount of active threads"),
> +       OPT_END()
> +};
> +
> +static const char * const enable_usage[] = {
> +       "perf bench breakpoint enable <options>",
> +       NULL
> +};
> +
> +// The benchmark creates an inheritable breakpoint,
> +// then starts npassive threads that block and nactive threads that actively spin
> +// and then disables and enables the breakpoint bench_repeat times.
> +int bench_breakpoint_enable(int argc, const char **argv)
> +{
> +       unsigned int i, nthreads, result_usec, done = 0;
> +       char watched;
> +       int fd;
> +       pthread_t *threads;
> +       struct timeval start, stop, diff;
> +
> +       if (parse_options(argc, argv, enable_options, enable_usage, 0)) {
> +               usage_with_options(enable_usage, enable_options);
> +               exit(EXIT_FAILURE);
> +       }
> +       fd = breakpoint_setup(&watched);
> +       if (fd == -1)
> +               exit((perror("perf_event_open"), EXIT_FAILURE));
> +       nthreads = enable_params.npassive + enable_params.nactive;
> +       threads = calloc(nthreads, sizeof(threads[0]));
> +       for (i = 0; i < nthreads; i++) {
> +               if (pthread_create(&threads[i], NULL,
> +                       i < enable_params.npassive ? passive_thread : active_thread, &done))
> +                       exit((perror("pthread_create"), EXIT_FAILURE));
> +       }
> +       usleep(10000);  // let the threads block
> +       gettimeofday(&start, NULL);
> +       for (i = 0; i < bench_repeat; i++) {
> +               if (ioctl(fd, PERF_EVENT_IOC_DISABLE, 0))
> +                       exit((perror("ioctl(PERF_EVENT_IOC_DISABLE)"), EXIT_FAILURE));
> +               if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0))
> +                       exit((perror("ioctl(PERF_EVENT_IOC_ENABLE)"), EXIT_FAILURE));
> +       }
> +       gettimeofday(&stop, NULL);
> +       timersub(&stop, &start, &diff);
> +       __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
> +       futex_wake(&done, enable_params.npassive, 0);
> +       for (i = 0; i < nthreads; i++)
> +               pthread_join(threads[i], NULL);
> +       free(threads);
> +       close(fd);
> +       switch (bench_format) {
> +       case BENCH_FORMAT_DEFAULT:
> +               printf("# Enabled/disabled breakpoint %d time with %d passive and %d active threads\n",
> +                       bench_repeat, enable_params.npassive, enable_params.nactive);
> +               printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
> +                       (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> +               printf(" %14lf usecs/op\n", (double)result_usec / bench_repeat);
> +               break;
> +       case BENCH_FORMAT_SIMPLE:
> +               printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               break;
> +       default:
> +               fprintf(stderr, "Unknown format: %d\n", bench_format);
> +               exit(EXIT_FAILURE);
> +       }
> +       return 0;
> +}
> diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
> index d291f3a8af5f2..334ab897aae3b 100644
> --- a/tools/perf/builtin-bench.c
> +++ b/tools/perf/builtin-bench.c
> @@ -92,6 +92,13 @@ static struct bench internals_benchmarks[] = {
>         { NULL,         NULL,                                   NULL                    }
>  };
>
> +static struct bench breakpoint_benchmarks[] = {
> +       { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
> +       { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
> +       { "all", "Run all breakpoint benchmarks", NULL},
> +       { NULL, NULL, NULL },
> +};
> +
>  struct collection {
>         const char      *name;
>         const char      *summary;
> @@ -110,6 +117,7 @@ static struct collection collections[] = {
>         {"epoll",       "Epoll stressing benchmarks",                   epoll_benchmarks        },
>  #endif
>         { "internals",  "Perf-internals benchmarks",                    internals_benchmarks    },
> +       { "breakpoint", "Breakpoint benchmarks",                        breakpoint_benchmarks   },
>         { "all",        "All benchmarks",                               NULL                    },
>         { NULL,         NULL,                                           NULL                    }
>  };
>
> base-commit: bd24325684029a48f20a188b899eb84900d0bc9c
> --
> 2.36.0.464.gb9c8b46e94-goog
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tools/perf: add breakpoint benchmarks
  2022-05-05 15:57 [PATCH] tools/perf: add breakpoint benchmarks Dmitry Vyukov
  2022-05-06  7:58 ` Dmitry Vyukov
@ 2022-05-11 15:34 ` Ian Rogers
  2022-05-11 18:08   ` Arnaldo Carvalho de Melo
  2022-05-12  8:04   ` Dmitry Vyukov
  1 sibling, 2 replies; 7+ messages in thread
From: Ian Rogers @ 2022-05-11 15:34 UTC (permalink / raw)
  To: Dmitry Vyukov
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, elver, linux-perf-users, linux-kernel

On Thu, May 5, 2022 at 8:58 AM Dmitry Vyukov <dvyukov@google.com> wrote:
>
> Add 2 benchmarks:
> 1. Performance of thread creation/exiting in presence of breakpoints.
> 2. Performance of breakpoint modification in presence of threads.
>
> The benchmarks capture use cases that we are interested in:
> using inheritable breakpoints in large highly-threaded applications.
> The benchmarks show significant slowdown imposed by breakpoints
> (even when they don't fire).
>
> Testing on Intel 8173M with 112 HW threads show:
>
> perf bench --repeat=56 breakpoint thread --breakpoints=0 --parallelism=56 --threads=20
>       78.675000 usecs/op
> perf bench --repeat=56 breakpoint thread --breakpoints=4 --parallelism=56 --threads=20
>    12967.135714 usecs/op
> That's 165x slowdown due to presence of the breakpoints.
>
> perf bench --repeat=20000 breakpoint enable --passive=0 --active=0
>        1.433250 usecs/op
> perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
>      585.318400 usecs/op
> perf bench --repeat=20000 breakpoint enable --passive=0 --active=111
>      635.953000 usecs/op
> That's 408x and 444x slowdown due to presence of threads.
>
> Profiles show some overhead in toggle_bp_slot,
> but also very high contention:
>
>     90.83%  breakpoint-thre  [kernel.kallsyms]  [k] osq_lock
>      4.69%  breakpoint-thre  [kernel.kallsyms]  [k] mutex_spin_on_owner
>      2.06%  breakpoint-thre  [kernel.kallsyms]  [k] __reserve_bp_slot
>      2.04%  breakpoint-thre  [kernel.kallsyms]  [k] toggle_bp_slot
>
>     79.01%  breakpoint-enab  [kernel.kallsyms]  [k] smp_call_function_single
>      9.94%  breakpoint-enab  [kernel.kallsyms]  [k] llist_add_batch
>      5.70%  breakpoint-enab  [kernel.kallsyms]  [k] _raw_spin_lock_irq
>      1.84%  breakpoint-enab  [kernel.kallsyms]  [k] event_function_call
>      1.12%  breakpoint-enab  [kernel.kallsyms]  [k] send_call_function_single_ipi
>      0.37%  breakpoint-enab  [kernel.kallsyms]  [k] generic_exec_single
>      0.24%  breakpoint-enab  [kernel.kallsyms]  [k] __perf_event_disable
>      0.20%  breakpoint-enab  [kernel.kallsyms]  [k] _perf_event_enable
>      0.18%  breakpoint-enab  [kernel.kallsyms]  [k] toggle_bp_slot
>
> Signed-off-by: Dmitry Vyukov <dvyukov@google.com>

Acked-by: Ian Rogers <irogers@google.com>

> Cc: linux-perf-users@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  tools/perf/bench/Build        |   1 +
>  tools/perf/bench/bench.h      |   2 +
>  tools/perf/bench/breakpoint.c | 235 ++++++++++++++++++++++++++++++++++
>  tools/perf/builtin-bench.c    |   8 ++
>  4 files changed, 246 insertions(+)
>  create mode 100644 tools/perf/bench/breakpoint.c
>
> diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build
> index 61d45fcb4057c..6b6155a8ad096 100644
> --- a/tools/perf/bench/Build
> +++ b/tools/perf/bench/Build
> @@ -14,6 +14,7 @@ perf-y += kallsyms-parse.o
>  perf-y += find-bit-bench.o
>  perf-y += inject-buildid.o
>  perf-y += evlist-open-close.o
> +perf-y += breakpoint.o
>
>  perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
>  perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
> diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
> index b3480bc33fe84..6cefb4315d75e 100644
> --- a/tools/perf/bench/bench.h
> +++ b/tools/perf/bench/bench.h
> @@ -49,6 +49,8 @@ int bench_synthesize(int argc, const char **argv);
>  int bench_kallsyms_parse(int argc, const char **argv);
>  int bench_inject_build_id(int argc, const char **argv);
>  int bench_evlist_open_close(int argc, const char **argv);
> +int bench_breakpoint_thread(int argc, const char **argv);
> +int bench_breakpoint_enable(int argc, const char **argv);
>
>  #define BENCH_FORMAT_DEFAULT_STR       "default"
>  #define BENCH_FORMAT_DEFAULT           0
> diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
> new file mode 100644
> index 0000000000000..56936fea246d7
> --- /dev/null
> +++ b/tools/perf/bench/breakpoint.c
> @@ -0,0 +1,235 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <subcmd/parse-options.h>
> +#include <linux/hw_breakpoint.h>
> +#include <linux/perf_event.h>
> +#include <linux/time64.h>
> +#include <sys/syscall.h>
> +#include <sys/ioctl.h>
> +#include <sys/time.h>
> +#include <pthread.h>
> +#include <stddef.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <stdio.h>
> +#include <errno.h>
> +#include "bench.h"
> +#include "futex.h"
> +
> +struct {
> +       unsigned int nbreakpoints;
> +       unsigned int nparallel;
> +       unsigned int nthreads;
> +} thread_params = {
> +       .nbreakpoints = 1,
> +       .nparallel = 1,
> +       .nthreads = 1,
> +};
> +
> +static const struct option thread_options[] = {
> +       OPT_UINTEGER('b', "breakpoints", &thread_params.nbreakpoints,
> +               "Specify amount of breakpoints"),
> +       OPT_UINTEGER('p', "parallelism", &thread_params.nparallel, "Specify amount of parallelism"),
> +       OPT_UINTEGER('t', "threads", &thread_params.nthreads, "Specify amount of threads"),
> +       OPT_END()
> +};
> +
> +static const char * const thread_usage[] = {
> +       "perf bench breakpoint thread <options>",
> +       NULL
> +};
> +
> +struct breakpoint {
> +       int fd;
> +       char watched;
> +};
> +
> +static int breakpoint_setup(void *addr)
> +{
> +       struct perf_event_attr attr = {0};
> +
> +       attr.type = PERF_TYPE_BREAKPOINT;
> +       attr.size = sizeof(attr);
> +       attr.inherit = 1;
> +       attr.exclude_kernel = 1;
> +       attr.exclude_hv = 1;
> +       attr.bp_addr = (uint64_t)addr;
> +       attr.bp_type = HW_BREAKPOINT_RW;
> +       attr.bp_len = HW_BREAKPOINT_LEN_1;
> +       return syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
> +}
> +
> +static void *passive_thread(void *arg)
> +{
> +       unsigned int *done = (unsigned int *)arg;
> +
> +       while (!__atomic_load_n(done, __ATOMIC_RELAXED))

Note, this may be the first change with atomics since Linux moved to
C11 which may make the use of stdatomic.h more idiomatic. Just
flagging for potential future cleanup.

Thanks,
Ian

> +               futex_wait(done, 0, NULL, 0);
> +       return NULL;
> +}
> +
> +static void *active_thread(void *arg)
> +{
> +       unsigned int *done = (unsigned int *)arg;
> +
> +       while (!__atomic_load_n(done, __ATOMIC_RELAXED));
> +       return NULL;
> +}
> +
> +static void *breakpoint_thread(void *arg)
> +{
> +       unsigned int i, done;
> +       int *repeat = (int *)arg;
> +       pthread_t *threads;
> +
> +       threads = calloc(thread_params.nthreads, sizeof(threads[0]));
> +       while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
> +               done = 0;
> +               for (i = 0; i < thread_params.nthreads; i++) {
> +                       if (pthread_create(&threads[i], NULL, passive_thread, &done))
> +                               exit((perror("pthread_create"), EXIT_FAILURE));
> +               }
> +               __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
> +               futex_wake(&done, thread_params.nthreads, 0);
> +               for (i = 0; i < thread_params.nthreads; i++)
> +                       pthread_join(threads[i], NULL);
> +       }
> +       free(threads);
> +       return NULL;
> +}
> +
> +// The benchmark creates nbreakpoints inheritable breakpoints,
> +// then starts nparallel threads which create and join bench_repeat batches of nthreads threads.
> +int bench_breakpoint_thread(int argc, const char **argv)
> +{
> +       unsigned int i, result_usec;
> +       int repeat = bench_repeat;
> +       struct breakpoint *breakpoints;
> +       pthread_t *parallel;
> +       struct timeval start, stop, diff;
> +
> +       if (parse_options(argc, argv, thread_options, thread_usage, 0)) {
> +               usage_with_options(thread_usage, thread_options);
> +               exit(EXIT_FAILURE);
> +       }
> +       breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
> +       parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
> +       for (i = 0; i < thread_params.nbreakpoints; i++) {
> +               breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
> +               if (breakpoints[i].fd == -1)
> +                       exit((perror("perf_event_open"), EXIT_FAILURE));
> +       }
> +       gettimeofday(&start, NULL);
> +       for (i = 0; i < thread_params.nparallel; i++) {
> +               if (pthread_create(&parallel[i], NULL, breakpoint_thread, &repeat))
> +                       exit((perror("pthread_create"), EXIT_FAILURE));
> +       }
> +       for (i = 0; i < thread_params.nparallel; i++)
> +               pthread_join(parallel[i], NULL);
> +       gettimeofday(&stop, NULL);
> +       timersub(&stop, &start, &diff);
> +       for (i = 0; i < thread_params.nbreakpoints; i++)
> +               close(breakpoints[i].fd);
> +       free(parallel);
> +       free(breakpoints);
> +       switch (bench_format) {
> +       case BENCH_FORMAT_DEFAULT:
> +               printf("# Created/joined %d threads with %d breakpoints and %d parallelism\n",
> +                       bench_repeat, thread_params.nbreakpoints, thread_params.nparallel);
> +               printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
> +                       (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> +               printf(" %14lf usecs/op\n",
> +                       (double)result_usec / bench_repeat / thread_params.nthreads);
> +               printf(" %14lf usecs/op/cpu\n",
> +                       (double)result_usec / bench_repeat /
> +                       thread_params.nthreads * thread_params.nparallel);
> +               break;
> +       case BENCH_FORMAT_SIMPLE:
> +               printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               break;
> +       default:
> +               fprintf(stderr, "Unknown format: %d\n", bench_format);
> +               exit(EXIT_FAILURE);
> +       }
> +       return 0;
> +}
> +
> +struct {
> +       unsigned int npassive;
> +       unsigned int nactive;
> +} enable_params = {
> +       .nactive = 0,
> +       .npassive = 0,
> +};
> +
> +static const struct option enable_options[] = {
> +       OPT_UINTEGER('p', "passive", &enable_params.npassive, "Specify amount of passive threads"),
> +       OPT_UINTEGER('a', "active", &enable_params.nactive, "Specify amount of active threads"),
> +       OPT_END()
> +};
> +
> +static const char * const enable_usage[] = {
> +       "perf bench breakpoint enable <options>",
> +       NULL
> +};
> +
> +// The benchmark creates an inheritable breakpoint,
> +// then starts npassive threads that block and nactive threads that actively spin
> +// and then disables and enables the breakpoint bench_repeat times.
> +int bench_breakpoint_enable(int argc, const char **argv)
> +{
> +       unsigned int i, nthreads, result_usec, done = 0;
> +       char watched;
> +       int fd;
> +       pthread_t *threads;
> +       struct timeval start, stop, diff;
> +
> +       if (parse_options(argc, argv, enable_options, enable_usage, 0)) {
> +               usage_with_options(enable_usage, enable_options);
> +               exit(EXIT_FAILURE);
> +       }
> +       fd = breakpoint_setup(&watched);
> +       if (fd == -1)
> +               exit((perror("perf_event_open"), EXIT_FAILURE));
> +       nthreads = enable_params.npassive + enable_params.nactive;
> +       threads = calloc(nthreads, sizeof(threads[0]));
> +       for (i = 0; i < nthreads; i++) {
> +               if (pthread_create(&threads[i], NULL,
> +                       i < enable_params.npassive ? passive_thread : active_thread, &done))
> +                       exit((perror("pthread_create"), EXIT_FAILURE));
> +       }
> +       usleep(10000);  // let the threads block
> +       gettimeofday(&start, NULL);
> +       for (i = 0; i < bench_repeat; i++) {
> +               if (ioctl(fd, PERF_EVENT_IOC_DISABLE, 0))
> +                       exit((perror("ioctl(PERF_EVENT_IOC_DISABLE)"), EXIT_FAILURE));
> +               if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0))
> +                       exit((perror("ioctl(PERF_EVENT_IOC_ENABLE)"), EXIT_FAILURE));
> +       }
> +       gettimeofday(&stop, NULL);
> +       timersub(&stop, &start, &diff);
> +       __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
> +       futex_wake(&done, enable_params.npassive, 0);
> +       for (i = 0; i < nthreads; i++)
> +               pthread_join(threads[i], NULL);
> +       free(threads);
> +       close(fd);
> +       switch (bench_format) {
> +       case BENCH_FORMAT_DEFAULT:
> +               printf("# Enabled/disabled breakpoint %d time with %d passive and %d active threads\n",
> +                       bench_repeat, enable_params.npassive, enable_params.nactive);
> +               printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
> +                       (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> +               printf(" %14lf usecs/op\n", (double)result_usec / bench_repeat);
> +               break;
> +       case BENCH_FORMAT_SIMPLE:
> +               printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> +               break;
> +       default:
> +               fprintf(stderr, "Unknown format: %d\n", bench_format);
> +               exit(EXIT_FAILURE);
> +       }
> +       return 0;
> +}
> diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
> index d291f3a8af5f2..334ab897aae3b 100644
> --- a/tools/perf/builtin-bench.c
> +++ b/tools/perf/builtin-bench.c
> @@ -92,6 +92,13 @@ static struct bench internals_benchmarks[] = {
>         { NULL,         NULL,                                   NULL                    }
>  };
>
> +static struct bench breakpoint_benchmarks[] = {
> +       { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
> +       { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
> +       { "all", "Run all breakpoint benchmarks", NULL},
> +       { NULL, NULL, NULL },
> +};
> +
>  struct collection {
>         const char      *name;
>         const char      *summary;
> @@ -110,6 +117,7 @@ static struct collection collections[] = {
>         {"epoll",       "Epoll stressing benchmarks",                   epoll_benchmarks        },
>  #endif
>         { "internals",  "Perf-internals benchmarks",                    internals_benchmarks    },
> +       { "breakpoint", "Breakpoint benchmarks",                        breakpoint_benchmarks   },
>         { "all",        "All benchmarks",                               NULL                    },
>         { NULL,         NULL,                                           NULL                    }
>  };
>
> base-commit: bd24325684029a48f20a188b899eb84900d0bc9c
> --
> 2.36.0.464.gb9c8b46e94-goog
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tools/perf: add breakpoint benchmarks
  2022-05-11 15:34 ` Ian Rogers
@ 2022-05-11 18:08   ` Arnaldo Carvalho de Melo
  2022-05-11 18:16     ` Arnaldo Carvalho de Melo
  2022-05-12  8:04   ` Dmitry Vyukov
  1 sibling, 1 reply; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-05-11 18:08 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Dmitry Vyukov, peterz, mingo, mark.rutland, alexander.shishkin,
	jolsa, namhyung, elver, linux-perf-users, linux-kernel

Em Wed, May 11, 2022 at 08:34:58AM -0700, Ian Rogers escreveu:
> On Thu, May 5, 2022 at 8:58 AM Dmitry Vyukov <dvyukov@google.com> wrote:
> >
> > Add 2 benchmarks:
> > 1. Performance of thread creation/exiting in presence of breakpoints.
> > 2. Performance of breakpoint modification in presence of threads.
> >
> > The benchmarks capture use cases that we are interested in:
> > using inheritable breakpoints in large highly-threaded applications.
> > The benchmarks show significant slowdown imposed by breakpoints
> > (even when they don't fire).
> >
> > Testing on Intel 8173M with 112 HW threads show:
> >
> > perf bench --repeat=56 breakpoint thread --breakpoints=0 --parallelism=56 --threads=20
> >       78.675000 usecs/op
> > perf bench --repeat=56 breakpoint thread --breakpoints=4 --parallelism=56 --threads=20
> >    12967.135714 usecs/op
> > That's 165x slowdown due to presence of the breakpoints.
> >
> > perf bench --repeat=20000 breakpoint enable --passive=0 --active=0
> >        1.433250 usecs/op
> > perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
> >      585.318400 usecs/op
> > perf bench --repeat=20000 breakpoint enable --passive=0 --active=111
> >      635.953000 usecs/op
> > That's 408x and 444x slowdown due to presence of threads.
> >
> > Profiles show some overhead in toggle_bp_slot,
> > but also very high contention:
> >
> >     90.83%  breakpoint-thre  [kernel.kallsyms]  [k] osq_lock
> >      4.69%  breakpoint-thre  [kernel.kallsyms]  [k] mutex_spin_on_owner
> >      2.06%  breakpoint-thre  [kernel.kallsyms]  [k] __reserve_bp_slot
> >      2.04%  breakpoint-thre  [kernel.kallsyms]  [k] toggle_bp_slot
> >
> >     79.01%  breakpoint-enab  [kernel.kallsyms]  [k] smp_call_function_single
> >      9.94%  breakpoint-enab  [kernel.kallsyms]  [k] llist_add_batch
> >      5.70%  breakpoint-enab  [kernel.kallsyms]  [k] _raw_spin_lock_irq
> >      1.84%  breakpoint-enab  [kernel.kallsyms]  [k] event_function_call
> >      1.12%  breakpoint-enab  [kernel.kallsyms]  [k] send_call_function_single_ipi
> >      0.37%  breakpoint-enab  [kernel.kallsyms]  [k] generic_exec_single
> >      0.24%  breakpoint-enab  [kernel.kallsyms]  [k] __perf_event_disable
> >      0.20%  breakpoint-enab  [kernel.kallsyms]  [k] _perf_event_enable
> >      0.18%  breakpoint-enab  [kernel.kallsyms]  [k] toggle_bp_slot
> >
> > Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
> 
> Acked-by: Ian Rogers <irogers@google.com>

Thanks, applied.

- Arnaldo

 
> > Cc: linux-perf-users@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > ---
> >  tools/perf/bench/Build        |   1 +
> >  tools/perf/bench/bench.h      |   2 +
> >  tools/perf/bench/breakpoint.c | 235 ++++++++++++++++++++++++++++++++++
> >  tools/perf/builtin-bench.c    |   8 ++
> >  4 files changed, 246 insertions(+)
> >  create mode 100644 tools/perf/bench/breakpoint.c
> >
> > diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build
> > index 61d45fcb4057c..6b6155a8ad096 100644
> > --- a/tools/perf/bench/Build
> > +++ b/tools/perf/bench/Build
> > @@ -14,6 +14,7 @@ perf-y += kallsyms-parse.o
> >  perf-y += find-bit-bench.o
> >  perf-y += inject-buildid.o
> >  perf-y += evlist-open-close.o
> > +perf-y += breakpoint.o
> >
> >  perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
> >  perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
> > diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
> > index b3480bc33fe84..6cefb4315d75e 100644
> > --- a/tools/perf/bench/bench.h
> > +++ b/tools/perf/bench/bench.h
> > @@ -49,6 +49,8 @@ int bench_synthesize(int argc, const char **argv);
> >  int bench_kallsyms_parse(int argc, const char **argv);
> >  int bench_inject_build_id(int argc, const char **argv);
> >  int bench_evlist_open_close(int argc, const char **argv);
> > +int bench_breakpoint_thread(int argc, const char **argv);
> > +int bench_breakpoint_enable(int argc, const char **argv);
> >
> >  #define BENCH_FORMAT_DEFAULT_STR       "default"
> >  #define BENCH_FORMAT_DEFAULT           0
> > diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
> > new file mode 100644
> > index 0000000000000..56936fea246d7
> > --- /dev/null
> > +++ b/tools/perf/bench/breakpoint.c
> > @@ -0,0 +1,235 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#include <subcmd/parse-options.h>
> > +#include <linux/hw_breakpoint.h>
> > +#include <linux/perf_event.h>
> > +#include <linux/time64.h>
> > +#include <sys/syscall.h>
> > +#include <sys/ioctl.h>
> > +#include <sys/time.h>
> > +#include <pthread.h>
> > +#include <stddef.h>
> > +#include <stdlib.h>
> > +#include <unistd.h>
> > +#include <stdio.h>
> > +#include <errno.h>
> > +#include "bench.h"
> > +#include "futex.h"
> > +
> > +struct {
> > +       unsigned int nbreakpoints;
> > +       unsigned int nparallel;
> > +       unsigned int nthreads;
> > +} thread_params = {
> > +       .nbreakpoints = 1,
> > +       .nparallel = 1,
> > +       .nthreads = 1,
> > +};
> > +
> > +static const struct option thread_options[] = {
> > +       OPT_UINTEGER('b', "breakpoints", &thread_params.nbreakpoints,
> > +               "Specify amount of breakpoints"),
> > +       OPT_UINTEGER('p', "parallelism", &thread_params.nparallel, "Specify amount of parallelism"),
> > +       OPT_UINTEGER('t', "threads", &thread_params.nthreads, "Specify amount of threads"),
> > +       OPT_END()
> > +};
> > +
> > +static const char * const thread_usage[] = {
> > +       "perf bench breakpoint thread <options>",
> > +       NULL
> > +};
> > +
> > +struct breakpoint {
> > +       int fd;
> > +       char watched;
> > +};
> > +
> > +static int breakpoint_setup(void *addr)
> > +{
> > +       struct perf_event_attr attr = {0};
> > +
> > +       attr.type = PERF_TYPE_BREAKPOINT;
> > +       attr.size = sizeof(attr);
> > +       attr.inherit = 1;
> > +       attr.exclude_kernel = 1;
> > +       attr.exclude_hv = 1;
> > +       attr.bp_addr = (uint64_t)addr;
> > +       attr.bp_type = HW_BREAKPOINT_RW;
> > +       attr.bp_len = HW_BREAKPOINT_LEN_1;
> > +       return syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
> > +}
> > +
> > +static void *passive_thread(void *arg)
> > +{
> > +       unsigned int *done = (unsigned int *)arg;
> > +
> > +       while (!__atomic_load_n(done, __ATOMIC_RELAXED))
> 
> Note, this may be the first change with atomics since Linux moved to
> C11 which may make the use of stdatomic.h more idiomatic. Just
> flagging for potential future cleanup.
> 
> Thanks,
> Ian
> 
> > +               futex_wait(done, 0, NULL, 0);
> > +       return NULL;
> > +}
> > +
> > +static void *active_thread(void *arg)
> > +{
> > +       unsigned int *done = (unsigned int *)arg;
> > +
> > +       while (!__atomic_load_n(done, __ATOMIC_RELAXED));
> > +       return NULL;
> > +}
> > +
> > +static void *breakpoint_thread(void *arg)
> > +{
> > +       unsigned int i, done;
> > +       int *repeat = (int *)arg;
> > +       pthread_t *threads;
> > +
> > +       threads = calloc(thread_params.nthreads, sizeof(threads[0]));
> > +       while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
> > +               done = 0;
> > +               for (i = 0; i < thread_params.nthreads; i++) {
> > +                       if (pthread_create(&threads[i], NULL, passive_thread, &done))
> > +                               exit((perror("pthread_create"), EXIT_FAILURE));
> > +               }
> > +               __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
> > +               futex_wake(&done, thread_params.nthreads, 0);
> > +               for (i = 0; i < thread_params.nthreads; i++)
> > +                       pthread_join(threads[i], NULL);
> > +       }
> > +       free(threads);
> > +       return NULL;
> > +}
> > +
> > +// The benchmark creates nbreakpoints inheritable breakpoints,
> > +// then starts nparallel threads which create and join bench_repeat batches of nthreads threads.
> > +int bench_breakpoint_thread(int argc, const char **argv)
> > +{
> > +       unsigned int i, result_usec;
> > +       int repeat = bench_repeat;
> > +       struct breakpoint *breakpoints;
> > +       pthread_t *parallel;
> > +       struct timeval start, stop, diff;
> > +
> > +       if (parse_options(argc, argv, thread_options, thread_usage, 0)) {
> > +               usage_with_options(thread_usage, thread_options);
> > +               exit(EXIT_FAILURE);
> > +       }
> > +       breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
> > +       parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
> > +       for (i = 0; i < thread_params.nbreakpoints; i++) {
> > +               breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
> > +               if (breakpoints[i].fd == -1)
> > +                       exit((perror("perf_event_open"), EXIT_FAILURE));
> > +       }
> > +       gettimeofday(&start, NULL);
> > +       for (i = 0; i < thread_params.nparallel; i++) {
> > +               if (pthread_create(&parallel[i], NULL, breakpoint_thread, &repeat))
> > +                       exit((perror("pthread_create"), EXIT_FAILURE));
> > +       }
> > +       for (i = 0; i < thread_params.nparallel; i++)
> > +               pthread_join(parallel[i], NULL);
> > +       gettimeofday(&stop, NULL);
> > +       timersub(&stop, &start, &diff);
> > +       for (i = 0; i < thread_params.nbreakpoints; i++)
> > +               close(breakpoints[i].fd);
> > +       free(parallel);
> > +       free(breakpoints);
> > +       switch (bench_format) {
> > +       case BENCH_FORMAT_DEFAULT:
> > +               printf("# Created/joined %d threads with %d breakpoints and %d parallelism\n",
> > +                       bench_repeat, thread_params.nbreakpoints, thread_params.nparallel);
> > +               printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
> > +                       (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> > +               result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> > +               printf(" %14lf usecs/op\n",
> > +                       (double)result_usec / bench_repeat / thread_params.nthreads);
> > +               printf(" %14lf usecs/op/cpu\n",
> > +                       (double)result_usec / bench_repeat /
> > +                       thread_params.nthreads * thread_params.nparallel);
> > +               break;
> > +       case BENCH_FORMAT_SIMPLE:
> > +               printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> > +               break;
> > +       default:
> > +               fprintf(stderr, "Unknown format: %d\n", bench_format);
> > +               exit(EXIT_FAILURE);
> > +       }
> > +       return 0;
> > +}
> > +
> > +struct {
> > +       unsigned int npassive;
> > +       unsigned int nactive;
> > +} enable_params = {
> > +       .nactive = 0,
> > +       .npassive = 0,
> > +};
> > +
> > +static const struct option enable_options[] = {
> > +       OPT_UINTEGER('p', "passive", &enable_params.npassive, "Specify amount of passive threads"),
> > +       OPT_UINTEGER('a', "active", &enable_params.nactive, "Specify amount of active threads"),
> > +       OPT_END()
> > +};
> > +
> > +static const char * const enable_usage[] = {
> > +       "perf bench breakpoint enable <options>",
> > +       NULL
> > +};
> > +
> > +// The benchmark creates an inheritable breakpoint,
> > +// then starts npassive threads that block and nactive threads that actively spin
> > +// and then disables and enables the breakpoint bench_repeat times.
> > +int bench_breakpoint_enable(int argc, const char **argv)
> > +{
> > +       unsigned int i, nthreads, result_usec, done = 0;
> > +       char watched;
> > +       int fd;
> > +       pthread_t *threads;
> > +       struct timeval start, stop, diff;
> > +
> > +       if (parse_options(argc, argv, enable_options, enable_usage, 0)) {
> > +               usage_with_options(enable_usage, enable_options);
> > +               exit(EXIT_FAILURE);
> > +       }
> > +       fd = breakpoint_setup(&watched);
> > +       if (fd == -1)
> > +               exit((perror("perf_event_open"), EXIT_FAILURE));
> > +       nthreads = enable_params.npassive + enable_params.nactive;
> > +       threads = calloc(nthreads, sizeof(threads[0]));
> > +       for (i = 0; i < nthreads; i++) {
> > +               if (pthread_create(&threads[i], NULL,
> > +                       i < enable_params.npassive ? passive_thread : active_thread, &done))
> > +                       exit((perror("pthread_create"), EXIT_FAILURE));
> > +       }
> > +       usleep(10000);  // let the threads block
> > +       gettimeofday(&start, NULL);
> > +       for (i = 0; i < bench_repeat; i++) {
> > +               if (ioctl(fd, PERF_EVENT_IOC_DISABLE, 0))
> > +                       exit((perror("ioctl(PERF_EVENT_IOC_DISABLE)"), EXIT_FAILURE));
> > +               if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0))
> > +                       exit((perror("ioctl(PERF_EVENT_IOC_ENABLE)"), EXIT_FAILURE));
> > +       }
> > +       gettimeofday(&stop, NULL);
> > +       timersub(&stop, &start, &diff);
> > +       __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
> > +       futex_wake(&done, enable_params.npassive, 0);
> > +       for (i = 0; i < nthreads; i++)
> > +               pthread_join(threads[i], NULL);
> > +       free(threads);
> > +       close(fd);
> > +       switch (bench_format) {
> > +       case BENCH_FORMAT_DEFAULT:
> > +               printf("# Enabled/disabled breakpoint %d time with %d passive and %d active threads\n",
> > +                       bench_repeat, enable_params.npassive, enable_params.nactive);
> > +               printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
> > +                       (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> > +               result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> > +               printf(" %14lf usecs/op\n", (double)result_usec / bench_repeat);
> > +               break;
> > +       case BENCH_FORMAT_SIMPLE:
> > +               printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
> > +               break;
> > +       default:
> > +               fprintf(stderr, "Unknown format: %d\n", bench_format);
> > +               exit(EXIT_FAILURE);
> > +       }
> > +       return 0;
> > +}
> > diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
> > index d291f3a8af5f2..334ab897aae3b 100644
> > --- a/tools/perf/builtin-bench.c
> > +++ b/tools/perf/builtin-bench.c
> > @@ -92,6 +92,13 @@ static struct bench internals_benchmarks[] = {
> >         { NULL,         NULL,                                   NULL                    }
> >  };
> >
> > +static struct bench breakpoint_benchmarks[] = {
> > +       { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread},
> > +       { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable},
> > +       { "all", "Run all breakpoint benchmarks", NULL},
> > +       { NULL, NULL, NULL },
> > +};
> > +
> >  struct collection {
> >         const char      *name;
> >         const char      *summary;
> > @@ -110,6 +117,7 @@ static struct collection collections[] = {
> >         {"epoll",       "Epoll stressing benchmarks",                   epoll_benchmarks        },
> >  #endif
> >         { "internals",  "Perf-internals benchmarks",                    internals_benchmarks    },
> > +       { "breakpoint", "Breakpoint benchmarks",                        breakpoint_benchmarks   },
> >         { "all",        "All benchmarks",                               NULL                    },
> >         { NULL,         NULL,                                           NULL                    }
> >  };
> >
> > base-commit: bd24325684029a48f20a188b899eb84900d0bc9c
> > --
> > 2.36.0.464.gb9c8b46e94-goog
> >

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tools/perf: add breakpoint benchmarks
  2022-05-11 18:08   ` Arnaldo Carvalho de Melo
@ 2022-05-11 18:16     ` Arnaldo Carvalho de Melo
  2022-05-12  8:03       ` Dmitry Vyukov
  0 siblings, 1 reply; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-05-11 18:16 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Dmitry Vyukov, peterz, mingo, mark.rutland, alexander.shishkin,
	jolsa, namhyung, elver, linux-perf-users, linux-kernel

Em Wed, May 11, 2022 at 03:08:02PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, May 11, 2022 at 08:34:58AM -0700, Ian Rogers escreveu:
> > On Thu, May 5, 2022 at 8:58 AM Dmitry Vyukov <dvyukov@google.com> wrote:
> > >
> > > Add 2 benchmarks:
> > > 1. Performance of thread creation/exiting in presence of breakpoints.
> > > 2. Performance of breakpoint modification in presence of threads.
> > >
> > > The benchmarks capture use cases that we are interested in:
> > > using inheritable breakpoints in large highly-threaded applications.
> > > The benchmarks show significant slowdown imposed by breakpoints
> > > (even when they don't fire).
> > >
> > > Testing on Intel 8173M with 112 HW threads show:
> > >
> > > perf bench --repeat=56 breakpoint thread --breakpoints=0 --parallelism=56 --threads=20
> > >       78.675000 usecs/op
> > > perf bench --repeat=56 breakpoint thread --breakpoints=4 --parallelism=56 --threads=20
> > >    12967.135714 usecs/op
> > > That's 165x slowdown due to presence of the breakpoints.
> > >
> > > perf bench --repeat=20000 breakpoint enable --passive=0 --active=0
> > >        1.433250 usecs/op
> > > perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
> > >      585.318400 usecs/op
> > > perf bench --repeat=20000 breakpoint enable --passive=0 --active=111
> > >      635.953000 usecs/op
> > > That's 408x and 444x slowdown due to presence of threads.
> > >
> > > Profiles show some overhead in toggle_bp_slot,
> > > but also very high contention:
> > >
> > >     90.83%  breakpoint-thre  [kernel.kallsyms]  [k] osq_lock
> > >      4.69%  breakpoint-thre  [kernel.kallsyms]  [k] mutex_spin_on_owner
> > >      2.06%  breakpoint-thre  [kernel.kallsyms]  [k] __reserve_bp_slot
> > >      2.04%  breakpoint-thre  [kernel.kallsyms]  [k] toggle_bp_slot
> > >
> > >     79.01%  breakpoint-enab  [kernel.kallsyms]  [k] smp_call_function_single
> > >      9.94%  breakpoint-enab  [kernel.kallsyms]  [k] llist_add_batch
> > >      5.70%  breakpoint-enab  [kernel.kallsyms]  [k] _raw_spin_lock_irq
> > >      1.84%  breakpoint-enab  [kernel.kallsyms]  [k] event_function_call
> > >      1.12%  breakpoint-enab  [kernel.kallsyms]  [k] send_call_function_single_ipi
> > >      0.37%  breakpoint-enab  [kernel.kallsyms]  [k] generic_exec_single
> > >      0.24%  breakpoint-enab  [kernel.kallsyms]  [k] __perf_event_disable
> > >      0.20%  breakpoint-enab  [kernel.kallsyms]  [k] _perf_event_enable
> > >      0.18%  breakpoint-enab  [kernel.kallsyms]  [k] toggle_bp_slot
> > >
> > > Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
> > 
> > Acked-by: Ian Rogers <irogers@google.com>
> 
> Thanks, applied.

But I'll add some error checks, etc, running as !root, in a toolbox in
Fedora Silverblue produces:

⬢[acme@toolbox perf]$ perf bench breakpoint all
# Running breakpoint/thread benchmark...
# Created/joined 10 threads with 1 breakpoints and 1 parallelism
     Total time: 0.000 [sec]

      54.600000 usecs/op
      54.600000 usecs/op/cpu

# Running breakpoint/enable benchmark...
# Enabled/disabled breakpoint 10 time with 0 passive and 0 active threads
     Total time: 0.000 [sec]

       1.100000 usecs/op

⬢[acme@toolbox perf]$

⬢[acme@toolbox perf]$ perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
# Running 'breakpoint/enable' benchmark:
# Enabled/disabled breakpoint 20000 time with 224 passive and 0 active threads
     Total time: 8.933 [sec]

     446.674950 usecs/op
⬢[acme@toolbox perf]$

$ grep -m1 "model name" /proc/cpuinfo
model name	: AMD Ryzen 9 5950X 16-Core Processor
 
Diff:

diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
index 56936fea246d73c2..d2c074bba06a3d1f 100644
--- a/tools/perf/bench/breakpoint.c
+++ b/tools/perf/bench/breakpoint.c
@@ -83,6 +83,9 @@ static void *breakpoint_thread(void *arg)
 	pthread_t *threads;
 
 	threads = calloc(thread_params.nthreads, sizeof(threads[0]));
+	if (!threads)
+		exit((perror("calloc"), EXIT_FAILURE));
+
 	while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
 		done = 0;
 		for (i = 0; i < thread_params.nthreads; i++) {
@@ -114,6 +117,9 @@ int bench_breakpoint_thread(int argc, const char **argv)
 	}
 	breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
 	parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
+	if (!breakpoints || !parallel)
+		exit((perror("calloc"), EXIT_FAILURE));
+
 	for (i = 0; i < thread_params.nbreakpoints; i++) {
 		breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
 		if (breakpoints[i].fd == -1)
@@ -194,6 +200,9 @@ int bench_breakpoint_enable(int argc, const char **argv)
 		exit((perror("perf_event_open"), EXIT_FAILURE));
 	nthreads = enable_params.npassive + enable_params.nactive;
 	threads = calloc(nthreads, sizeof(threads[0]));
+	if (!threads)
+		exit((perror("calloc"), EXIT_FAILURE));
+
 	for (i = 0; i < nthreads; i++) {
 		if (pthread_create(&threads[i], NULL,
 			i < enable_params.npassive ? passive_thread : active_thread, &done))

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] tools/perf: add breakpoint benchmarks
  2022-05-11 18:16     ` Arnaldo Carvalho de Melo
@ 2022-05-12  8:03       ` Dmitry Vyukov
  0 siblings, 0 replies; 7+ messages in thread
From: Dmitry Vyukov @ 2022-05-12  8:03 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, peterz, mingo, mark.rutland, alexander.shishkin,
	jolsa, namhyung, elver, linux-perf-users, linux-kernel

On Wed, 11 May 2022 at 20:17, Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
> > >
> > > Acked-by: Ian Rogers <irogers@google.com>
> >
> > Thanks, applied.
>
> But I'll add some error checks, etc, running as !root, in a toolbox in
> Fedora Silverblue produces:

Thanks!

> ⬢[acme@toolbox perf]$ perf bench breakpoint all
> # Running breakpoint/thread benchmark...
> # Created/joined 10 threads with 1 breakpoints and 1 parallelism
>      Total time: 0.000 [sec]
>
>       54.600000 usecs/op
>       54.600000 usecs/op/cpu
>
> # Running breakpoint/enable benchmark...
> # Enabled/disabled breakpoint 10 time with 0 passive and 0 active threads
>      Total time: 0.000 [sec]
>
>        1.100000 usecs/op
>
> ⬢[acme@toolbox perf]$
>
> ⬢[acme@toolbox perf]$ perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
> # Running 'breakpoint/enable' benchmark:
> # Enabled/disabled breakpoint 20000 time with 224 passive and 0 active threads
>      Total time: 8.933 [sec]
>
>      446.674950 usecs/op
> ⬢[acme@toolbox perf]$
>
> $ grep -m1 "model name" /proc/cpuinfo
> model name      : AMD Ryzen 9 5950X 16-Core Processor
>
> Diff:
>
> diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
> index 56936fea246d73c2..d2c074bba06a3d1f 100644
> --- a/tools/perf/bench/breakpoint.c
> +++ b/tools/perf/bench/breakpoint.c
> @@ -83,6 +83,9 @@ static void *breakpoint_thread(void *arg)
>         pthread_t *threads;
>
>         threads = calloc(thread_params.nthreads, sizeof(threads[0]));
> +       if (!threads)
> +               exit((perror("calloc"), EXIT_FAILURE));
> +
>         while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
>                 done = 0;
>                 for (i = 0; i < thread_params.nthreads; i++) {
> @@ -114,6 +117,9 @@ int bench_breakpoint_thread(int argc, const char **argv)
>         }
>         breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
>         parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
> +       if (!breakpoints || !parallel)
> +               exit((perror("calloc"), EXIT_FAILURE));
> +
>         for (i = 0; i < thread_params.nbreakpoints; i++) {
>                 breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
>                 if (breakpoints[i].fd == -1)
> @@ -194,6 +200,9 @@ int bench_breakpoint_enable(int argc, const char **argv)
>                 exit((perror("perf_event_open"), EXIT_FAILURE));
>         nthreads = enable_params.npassive + enable_params.nactive;
>         threads = calloc(nthreads, sizeof(threads[0]));
> +       if (!threads)
> +               exit((perror("calloc"), EXIT_FAILURE));
> +
>         for (i = 0; i < nthreads; i++) {
>                 if (pthread_create(&threads[i], NULL,
>                         i < enable_params.npassive ? passive_thread : active_thread, &done))

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] tools/perf: add breakpoint benchmarks
  2022-05-11 15:34 ` Ian Rogers
  2022-05-11 18:08   ` Arnaldo Carvalho de Melo
@ 2022-05-12  8:04   ` Dmitry Vyukov
  1 sibling, 0 replies; 7+ messages in thread
From: Dmitry Vyukov @ 2022-05-12  8:04 UTC (permalink / raw)
  To: Ian Rogers
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, elver, linux-perf-users, linux-kernel

 On Wed, 11 May 2022 at 17:35, Ian Rogers <irogers@google.com> wrote:
>
> On Thu, May 5, 2022 at 8:58 AM Dmitry Vyukov <dvyukov@google.com> wrote:
> >
> > Add 2 benchmarks:
> > 1. Performance of thread creation/exiting in presence of breakpoints.
> > 2. Performance of breakpoint modification in presence of threads.
> >
> > The benchmarks capture use cases that we are interested in:
> > using inheritable breakpoints in large highly-threaded applications.
> > The benchmarks show significant slowdown imposed by breakpoints
> > (even when they don't fire).
> >
> > Testing on Intel 8173M with 112 HW threads show:
> >
> > perf bench --repeat=56 breakpoint thread --breakpoints=0 --parallelism=56 --threads=20
> >       78.675000 usecs/op
> > perf bench --repeat=56 breakpoint thread --breakpoints=4 --parallelism=56 --threads=20
> >    12967.135714 usecs/op
> > That's 165x slowdown due to presence of the breakpoints.
> >
> > perf bench --repeat=20000 breakpoint enable --passive=0 --active=0
> >        1.433250 usecs/op
> > perf bench --repeat=20000 breakpoint enable --passive=224 --active=0
> >      585.318400 usecs/op
> > perf bench --repeat=20000 breakpoint enable --passive=0 --active=111
> >      635.953000 usecs/op
> > That's 408x and 444x slowdown due to presence of threads.
> >
> > Profiles show some overhead in toggle_bp_slot,
> > but also very high contention:
> >
> >     90.83%  breakpoint-thre  [kernel.kallsyms]  [k] osq_lock
> >      4.69%  breakpoint-thre  [kernel.kallsyms]  [k] mutex_spin_on_owner
> >      2.06%  breakpoint-thre  [kernel.kallsyms]  [k] __reserve_bp_slot
> >      2.04%  breakpoint-thre  [kernel.kallsyms]  [k] toggle_bp_slot
> >
> >     79.01%  breakpoint-enab  [kernel.kallsyms]  [k] smp_call_function_single
> >      9.94%  breakpoint-enab  [kernel.kallsyms]  [k] llist_add_batch
> >      5.70%  breakpoint-enab  [kernel.kallsyms]  [k] _raw_spin_lock_irq
> >      1.84%  breakpoint-enab  [kernel.kallsyms]  [k] event_function_call
> >      1.12%  breakpoint-enab  [kernel.kallsyms]  [k] send_call_function_single_ipi
> >      0.37%  breakpoint-enab  [kernel.kallsyms]  [k] generic_exec_single
> >      0.24%  breakpoint-enab  [kernel.kallsyms]  [k] __perf_event_disable
> >      0.20%  breakpoint-enab  [kernel.kallsyms]  [k] _perf_event_enable
> >      0.18%  breakpoint-enab  [kernel.kallsyms]  [k] toggle_bp_slot
> >
> > Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
>
> Acked-by: Ian Rogers <irogers@google.com>
>
> > Cc: linux-perf-users@vger.kernel.org
> > Cc: linux-kernel@vger.kernel.org
> > ---
> >  tools/perf/bench/Build        |   1 +
> >  tools/perf/bench/bench.h      |   2 +
> >  tools/perf/bench/breakpoint.c | 235 ++++++++++++++++++++++++++++++++++
> >  tools/perf/builtin-bench.c    |   8 ++
> >  4 files changed, 246 insertions(+)
> >  create mode 100644 tools/perf/bench/breakpoint.c
> >
> > diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build
> > index 61d45fcb4057c..6b6155a8ad096 100644
> > --- a/tools/perf/bench/Build
> > +++ b/tools/perf/bench/Build
> > @@ -14,6 +14,7 @@ perf-y += kallsyms-parse.o
> >  perf-y += find-bit-bench.o
> >  perf-y += inject-buildid.o
> >  perf-y += evlist-open-close.o
> > +perf-y += breakpoint.o
> >
> >  perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
> >  perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
> > diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
> > index b3480bc33fe84..6cefb4315d75e 100644
> > --- a/tools/perf/bench/bench.h
> > +++ b/tools/perf/bench/bench.h
> > @@ -49,6 +49,8 @@ int bench_synthesize(int argc, const char **argv);
> >  int bench_kallsyms_parse(int argc, const char **argv);
> >  int bench_inject_build_id(int argc, const char **argv);
> >  int bench_evlist_open_close(int argc, const char **argv);
> > +int bench_breakpoint_thread(int argc, const char **argv);
> > +int bench_breakpoint_enable(int argc, const char **argv);
> >
> >  #define BENCH_FORMAT_DEFAULT_STR       "default"
> >  #define BENCH_FORMAT_DEFAULT           0
> > diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
> > new file mode 100644
> > index 0000000000000..56936fea246d7
> > --- /dev/null
> > +++ b/tools/perf/bench/breakpoint.c
> > @@ -0,0 +1,235 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#include <subcmd/parse-options.h>
> > +#include <linux/hw_breakpoint.h>
> > +#include <linux/perf_event.h>
> > +#include <linux/time64.h>
> > +#include <sys/syscall.h>
> > +#include <sys/ioctl.h>
> > +#include <sys/time.h>
> > +#include <pthread.h>
> > +#include <stddef.h>
> > +#include <stdlib.h>
> > +#include <unistd.h>
> > +#include <stdio.h>
> > +#include <errno.h>
> > +#include "bench.h"
> > +#include "futex.h"
> > +
> > +struct {
> > +       unsigned int nbreakpoints;
> > +       unsigned int nparallel;
> > +       unsigned int nthreads;
> > +} thread_params = {
> > +       .nbreakpoints = 1,
> > +       .nparallel = 1,
> > +       .nthreads = 1,
> > +};
> > +
> > +static const struct option thread_options[] = {
> > +       OPT_UINTEGER('b', "breakpoints", &thread_params.nbreakpoints,
> > +               "Specify amount of breakpoints"),
> > +       OPT_UINTEGER('p', "parallelism", &thread_params.nparallel, "Specify amount of parallelism"),
> > +       OPT_UINTEGER('t', "threads", &thread_params.nthreads, "Specify amount of threads"),
> > +       OPT_END()
> > +};
> > +
> > +static const char * const thread_usage[] = {
> > +       "perf bench breakpoint thread <options>",
> > +       NULL
> > +};
> > +
> > +struct breakpoint {
> > +       int fd;
> > +       char watched;
> > +};
> > +
> > +static int breakpoint_setup(void *addr)
> > +{
> > +       struct perf_event_attr attr = {0};
> > +
> > +       attr.type = PERF_TYPE_BREAKPOINT;
> > +       attr.size = sizeof(attr);
> > +       attr.inherit = 1;
> > +       attr.exclude_kernel = 1;
> > +       attr.exclude_hv = 1;
> > +       attr.bp_addr = (uint64_t)addr;
> > +       attr.bp_type = HW_BREAKPOINT_RW;
> > +       attr.bp_len = HW_BREAKPOINT_LEN_1;
> > +       return syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
> > +}
> > +
> > +static void *passive_thread(void *arg)
> > +{
> > +       unsigned int *done = (unsigned int *)arg;
> > +
> > +       while (!__atomic_load_n(done, __ATOMIC_RELAXED))
>
> Note, this may be the first change with atomics since Linux moved to
> C11 which may make the use of stdatomic.h more idiomatic. Just
> flagging for potential future cleanup.

I am ready to send a follow up with stdatomic.h if that's considered a
better option.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-05-12  8:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-05 15:57 [PATCH] tools/perf: add breakpoint benchmarks Dmitry Vyukov
2022-05-06  7:58 ` Dmitry Vyukov
2022-05-11 15:34 ` Ian Rogers
2022-05-11 18:08   ` Arnaldo Carvalho de Melo
2022-05-11 18:16     ` Arnaldo Carvalho de Melo
2022-05-12  8:03       ` Dmitry Vyukov
2022-05-12  8:04   ` Dmitry Vyukov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.