mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shunsuke <nakamura.shun@fujitsu.com>
To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
	jolsa@redhat.com, namhyung@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Subject: [RFC PATCH 4/7] libperf: Add perf_evsel__set_signal() functions
Date: Fri, 25 Feb 2022 19:31:11 +0900	[thread overview]
Message-ID: <20220225103114.144239-5-nakamura.shun@fujitsu.com> (raw)
In-Reply-To: <20220225103114.144239-1-nakamura.shun@fujitsu.com>

From: Shunsuke Nakamura <nakamura.shun@fujitsu.com>

Add the following functions:

  perf_evsel__set_signal()
  perf_evsel__set_signal_cpu()

to set the parameters to get the overflow signal.

Signed-off-by: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
---
 tools/lib/perf/Documentation/libperf.txt |   4 +
 tools/lib/perf/evsel.c                   | 105 +++++++++++++++++++++++
 tools/lib/perf/include/perf/evsel.h      |   6 ++
 tools/lib/perf/libperf.map               |   2 +
 4 files changed, 117 insertions(+)

diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
index 8f523293fb9d..d0aa57c6ca97 100644
--- a/tools/lib/perf/Documentation/libperf.txt
+++ b/tools/lib/perf/Documentation/libperf.txt
@@ -151,6 +151,10 @@ SYNOPSIS
                               int cpu_map_idx);
   int perf_evsel__period(struct perf_evsel *evsel, int period);
   int perf_evsel__period_cpu(struct perf_evsel *evsel, int period, int cpu_map_idx);
+  int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type, unsigned int signal,
+                             struct sigaction *sig);
+  int perf_evsel__set_signal_cpu(struct perf_evsel *evsel, int owner_type, unsigned int signal,
+                                 struct sigaction *sig, int cpu_map_idx);
   struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel);
   struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel);
   struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel);
diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c
index 8348545874e4..991ab04112b1 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -1,4 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
 #include <errno.h>
 #include <unistd.h>
 #include <sys/syscall.h>
@@ -16,6 +19,9 @@
 #include <internal/lib.h>
 #include <linux/string.h>
 #include <sys/ioctl.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <sys/types.h>
 #include <sys/mman.h>
 #include <asm/bug.h>
 
@@ -351,6 +357,28 @@ static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
 	return 0;
 }
 
+static int perf_evsel__run_fcntl(struct perf_evsel *evsel,
+				 unsigned int cmd, unsigned long arg,
+				 int cpu_map_idx)
+{
+	int thread;
+
+	for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) {
+		int err;
+		int *fd = FD(evsel, cpu_map_idx, thread);
+
+		if (!fd || *fd < 0)
+			return -1;
+
+		err = fcntl(*fd, cmd, arg);
+
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
 {
 	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, cpu_map_idx);
@@ -437,6 +465,83 @@ int perf_evsel__period(struct perf_evsel *evsel, __u64 period)
 	return err;
 }
 
+int perf_evsel__set_signal_cpu(struct perf_evsel *evsel, int owner_type, unsigned int signal,
+			       struct sigaction *sig, int cpu_map_idx)
+{
+	int err = 0;
+	struct f_owner_ex owner;
+
+	switch (owner_type) {
+	case F_OWNER_PID:
+		owner.pid = getpid();
+		break;
+	case F_OWNER_TID:
+		owner.pid = syscall(SYS_gettid);
+		break;
+	default:
+		return -1;
+	}
+
+	err = perf_evsel__run_fcntl(evsel, F_SETFL, (O_RDWR | O_NONBLOCK | O_ASYNC), cpu_map_idx);
+	if (err)
+		return err;
+
+	err = perf_evsel__run_fcntl(evsel, F_SETSIG, signal, cpu_map_idx);
+	if (err)
+		return err;
+
+	err = perf_evsel__run_fcntl(evsel, F_SETOWN_EX, (unsigned long)&owner, cpu_map_idx);
+	if (err)
+		return err;
+
+	err = sigaction(signal, sig, NULL);
+	if (err)
+		return err;
+
+	return err;
+}
+
+int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type, unsigned int signal,
+			   struct sigaction *sig)
+{
+	int i;
+	int err = 0;
+	struct f_owner_ex owner;
+
+	switch (owner_type) {
+	case F_OWNER_PID:
+		owner.pid = getpid();
+		break;
+	case F_OWNER_TID:
+		owner.pid = syscall(SYS_gettid);
+		break;
+	default:
+		return -1;
+	}
+
+	owner.type = owner_type;
+
+	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++) {
+		err = perf_evsel__run_fcntl(evsel, F_SETFL, (O_RDWR | O_NONBLOCK | O_ASYNC), i);
+		if (err)
+			return err;
+
+		err = perf_evsel__run_fcntl(evsel, F_SETSIG, signal, i);
+		if (err)
+			return err;
+
+		err = perf_evsel__run_fcntl(evsel, F_SETOWN_EX, (unsigned long)&owner, i);
+		if (err)
+			return err;
+	}
+
+	err = sigaction(signal, sig, NULL);
+	if (err)
+		return err;
+
+	return err;
+}
+
 int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
 {
 	int err = 0, i;
diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/perf/evsel.h
index 9ec24a5f5f9f..15768fc91910 100644
--- a/tools/lib/perf/include/perf/evsel.h
+++ b/tools/lib/perf/include/perf/evsel.h
@@ -5,6 +5,7 @@
 #include <stdint.h>
 #include <perf/core.h>
 #include <stdbool.h>
+#include <signal.h>
 #include <linux/types.h>
 
 struct perf_evsel;
@@ -43,6 +44,11 @@ LIBPERF_API int perf_evsel__refresh_cpu(struct perf_evsel *evsel, int refresh,
 					int cpu_map_idx);
 LIBPERF_API int perf_evsel__period(struct perf_evsel *evsel, __u64 period);
 LIBPERF_API int perf_evsel__period_cpu(struct perf_evsel *evsel, __u64 period, int cpu_map_idx);
+LIBPERF_API int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type,
+				       unsigned int signal, struct sigaction *sig);
+LIBPERF_API int perf_evsel__set_signal_cpu(struct perf_evsel *evsel, int owner_type,
+					   unsigned int signal, struct sigaction *sig,
+					   int cpu_map_idx);
 LIBPERF_API struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel);
 LIBPERF_API struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel);
 LIBPERF_API struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel);
diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map
index 83ecbce9cfbb..5e04fe2b611c 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -32,6 +32,8 @@ LIBPERF_0.0.1 {
 		perf_evsel__refresh_cpu;
 		perf_evsel__period;
 		perf_evsel__period_cpu;
+		perf_evsel__set_signal;
+		perf_evsel__set_signal_cpu;
 		perf_evsel__cpus;
 		perf_evsel__threads;
 		perf_evsel__attr;
-- 
2.31.1


  parent reply	other threads:[~2022-02-25 10:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
2022-02-25 10:31 ` [RFC PATCH 1/7] libperf tests: Fix typo in the error message "evsel" -> "evlist" Shunsuke
2022-02-25 10:31 ` [RFC PATCH 2/7] libperf: Add perf_evsel__set_close_on_exec() function Shunsuke
2022-02-25 10:31 ` [RFC PATCH 3/7] libperf: Add perf_evsel__refresh()/period() functions Shunsuke
2022-02-25 10:31 ` Shunsuke [this message]
2022-02-25 10:31 ` [RFC PATCH 5/7] libperf: Add perf_evsel__check_fd() functions Shunsuke
2022-02-25 10:31 ` [RFC PATCH 6/7] libperf test: Add test_stat_overflow() Shunsuke
2022-02-25 10:31 ` [RFC PATCH 7/7] libperf test: Add test_detect_overflow_event() Shunsuke
2022-02-26 17:23 ` [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Jiri Olsa
2022-03-01  8:32   ` nakamura.shun

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=20220225103114.144239-5-nakamura.shun@fujitsu.com \
    --to=nakamura.shun@fujitsu.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@redhat.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 \
    /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®