mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kyle Huey <me@kylehuey.com>
To: Kyle Huey <khuey@kylehuey.com>,
	Robert O'Callahan <robert@ocallahan.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] perf test: Test FASYNC with watermark wakeups.
Date: Wed, 21 Feb 2024 09:52:10 -0800	[thread overview]
Message-ID: <20240221175210.19936-2-khuey@kylehuey.com> (raw)
In-Reply-To: <20240221175210.19936-1-khuey@kylehuey.com>

The test uses PERF_RECORD_SWITCH records to fill the ring buffer and
trigger the watermark wakeup, which in turn should trigger an IO
signal.

Signed-off-by: Kyle Huey <khuey@kylehuey.com>
---
 tools/perf/tests/Build              |   1 +
 tools/perf/tests/builtin-test.c     |   1 +
 tools/perf/tests/tests.h            |   1 +
 tools/perf/tests/watermark_signal.c | 123 ++++++++++++++++++++++++++++
 4 files changed, 126 insertions(+)
 create mode 100644 tools/perf/tests/watermark_signal.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 53ba9c3e20e0..de43eb60b280 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -67,6 +67,7 @@ perf-y += sigtrap.o
 perf-y += event_groups.o
 perf-y += symbols.o
 perf-y += util.o
+perf-y += watermark_signal.o
 
 ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc))
 perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 4a5973f9bb9b..715c01a2172a 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -124,6 +124,7 @@ static struct test_suite *generic_tests[] = {
 	&suite__event_groups,
 	&suite__symbols,
 	&suite__util,
+	&suite__watermark_signal,
 	NULL,
 };
 
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index dad3d7414142..7ef4e0d0a77b 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -146,6 +146,7 @@ DECLARE_SUITE(sigtrap);
 DECLARE_SUITE(event_groups);
 DECLARE_SUITE(symbols);
 DECLARE_SUITE(util);
+DECLARE_SUITE(watermark_signal);
 
 /*
  * PowerPC and S390 do not support creation of instruction breakpoints using the
diff --git a/tools/perf/tests/watermark_signal.c b/tools/perf/tests/watermark_signal.c
new file mode 100644
index 000000000000..ae4abedc4b7c
--- /dev/null
+++ b/tools/perf/tests/watermark_signal.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stddef.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "tests.h"
+#include "debug.h"
+#include "event.h"
+#include "../perf-sys.h"
+#include "cloexec.h"
+#include <internal/lib.h> // page_size
+
+static int sigio_count;
+
+static void handle_sigio(int sig __always_unused)
+{
+	++sigio_count;
+}
+
+static void do_child(void)
+{
+	for (int i = 0; i < 20; ++i)
+		sleep(1);
+
+	exit(0);
+}
+
+static int test__watermark_signal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+	struct perf_event_attr attr;
+	struct perf_event_mmap_page *p = NULL;
+	sighandler_t previous_sigio = SIG_ERR;
+	pid_t child = -1;
+	int fd = -1;
+	int ret = TEST_FAIL;
+
+	previous_sigio = signal(SIGIO, handle_sigio);
+	if (previous_sigio == SIG_ERR) {
+		pr_debug("failed setting SIGIO handler\n");
+		goto cleanup;
+	}
+
+	memset(&attr, 0, sizeof(attr));
+	attr.size = sizeof(attr);
+	attr.type = PERF_TYPE_SOFTWARE;
+	attr.config = PERF_COUNT_SW_DUMMY;
+	attr.sample_period = 1;
+	attr.disabled = 1;
+	attr.watermark = 1;
+	attr.context_switch = 1;
+	attr.wakeup_watermark = 1;
+
+	child = fork();
+	if (child == 0)
+		do_child();
+	else if (child < 0) {
+		pr_debug("failed fork() %d\n", errno);
+		goto cleanup;
+	}
+
+	fd = sys_perf_event_open(&attr, child, -1, -1,
+				 perf_event_open_cloexec_flag());
+	if (fd < 0) {
+		pr_debug("failed opening event %llx\n", attr.config);
+		goto cleanup;
+	}
+
+	if (fcntl(fd, F_SETFL, FASYNC)) {
+		pr_debug("failed F_SETFL FASYNC %d\n", errno);
+		goto cleanup;
+	}
+
+	if (fcntl(fd, F_SETOWN, getpid())) {
+		pr_debug("failed F_SETOWN getpid() %d\n", errno);
+		goto cleanup;
+	}
+
+	if (fcntl(fd, F_SETSIG, SIGIO)) {
+		pr_debug("failed F_SETSIG SIGIO %d\n", errno);
+		goto cleanup;
+	}
+
+	p = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	if (p == NULL) {
+		pr_debug("failed to mmap\n");
+		goto cleanup;
+	}
+
+	if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0)) {
+		pr_debug("failed PERF_EVENT_IOC_ENABLE %d\n", errno);
+		goto cleanup;
+	}
+
+	sleep(30);
+
+	ret = (sigio_count == 1) ? TEST_OK : TEST_FAIL;
+
+cleanup:
+	if (p != NULL)
+		munmap(p, 2 * page_size);
+
+	if (fd >= 0)
+		close(fd);
+
+	if (child > 0) {
+		kill(child, SIGTERM);
+		waitpid(child, NULL, 0);
+	}
+
+	if (previous_sigio != SIG_ERR)
+		signal(SIGIO, previous_sigio);
+
+	return ret;
+}
+
+DEFINE_SUITE("Watermark signal handler", watermark_signal);
-- 
2.34.1


  reply	other threads:[~2024-02-21 17:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21 17:52 [PATCH 1/2] perf/ring_buffer: Trigger FASYNC signals for " Kyle Huey
2024-02-21 17:52 ` Kyle Huey [this message]
2024-02-21 18:35   ` [PATCH 2/2] perf test: Test FASYNC with " Ian Rogers
2024-02-22 17:55     ` Kyle Huey
2024-02-22 18:32       ` Ian Rogers
2024-02-22 18:41         ` Kyle Huey
2024-02-22 19:44           ` Ian Rogers
2024-02-22 19:54       ` Arnaldo Carvalho de Melo
2024-02-23 17:35         ` Kyle Huey
2024-02-23 17:59           ` Arnaldo Carvalho de Melo
2024-02-23 21:43             ` Robert O'Callahan
2024-02-24  2:27               ` Namhyung Kim
2024-02-24 15:58               ` Arnaldo Carvalho de Melo
2024-02-23 18:01           ` Ian Rogers
2024-02-23 18:17             ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240221175210.19936-2-khuey@kylehuey.com \
    --to=me@kylehuey.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=khuey@kylehuey.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=robert@ocallahan.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®