linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH 0/7] libperf: Add overflow detection of sampling events
@ 2022-02-25 10:31 Shunsuke
  2022-02-25 10:31 ` [RFC PATCH 1/7] libperf tests: Fix typo in the error message "evsel" -> "evlist" Shunsuke
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users

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

This patch series adds sampling event overflow detection capability
to libperf.

First patch fixes a typo in the error message that I noticed.

Second patch  adds a interface to set PERF_FLAG_FD_CLOEXEC

Third patch adds a interface to perform IOC_REFRESH and IOC_PERIOD.

Fourth patch adds a interface to set the signal handler.

Fifth patch adds a interface to detect overflowed events.

Sixth and seventh patch adds tests.

Shunsuke Nakamura (7):
  libperf tests: Fix typo in the error message "evsel" -> "evlist"
  libperf: Add perf_evsel__set_close_on_exec() function
  libperf: Add perf_evsel__refresh()/period() functions
  libperf: Add perf_evsel__set_signal() functions
  libperf: Add perf_evsel__check_fd() functions
  libperf test: Add test_stat_overflow()
  libperf test: Add test_detect_overflow_event()

 tools/lib/perf/Documentation/libperf.txt |  12 ++
 tools/lib/perf/evsel.c                   | 215 ++++++++++++++++++++++-
 tools/lib/perf/include/internal/evsel.h  |   2 +
 tools/lib/perf/include/perf/evsel.h      |  14 ++
 tools/lib/perf/libperf.map               |   9 +
 tools/lib/perf/tests/test-evlist.c       | 127 ++++++++++++-
 tools/lib/perf/tests/test-evsel.c        | 110 ++++++++++++
 tools/perf/util/evsel.c                  |  16 +-
 tools/perf/util/evsel.h                  |   1 -
 9 files changed, 487 insertions(+), 19 deletions(-)

-- 
2.31.1


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

* [RFC PATCH 1/7] libperf tests: Fix typo in the error message "evsel" -> "evlist"
  2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
@ 2022-02-25 10:31 ` Shunsuke
  2022-02-25 10:31 ` [RFC PATCH 2/7] libperf: Add perf_evsel__set_close_on_exec() function Shunsuke
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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

This patch corrects a typo in the error message.

Signed-off-by: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
---
 tools/lib/perf/tests/test-evlist.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/lib/perf/tests/test-evlist.c b/tools/lib/perf/tests/test-evlist.c
index b3479dfa9a1c..fafc6b0f8687 100644
--- a/tools/lib/perf/tests/test-evlist.c
+++ b/tools/lib/perf/tests/test-evlist.c
@@ -68,7 +68,7 @@ static int test_stat_cpu(void)
 	perf_evlist__set_maps(evlist, cpus, NULL);
 
 	err = perf_evlist__open(evlist);
-	__T("failed to open evsel", err == 0);
+	__T("failed to open evlist", err == 0);
 
 	perf_evlist__for_each_evsel(evlist, evsel) {
 		cpus = perf_evsel__cpus(evsel);
@@ -129,7 +129,7 @@ static int test_stat_thread(void)
 	perf_evlist__set_maps(evlist, NULL, threads);
 
 	err = perf_evlist__open(evlist);
-	__T("failed to open evsel", err == 0);
+	__T("failed to open evlist", err == 0);
 
 	perf_evlist__for_each_evsel(evlist, evsel) {
 		perf_evsel__read(evsel, 0, 0, &counts);
@@ -186,7 +186,7 @@ static int test_stat_thread_enable(void)
 	perf_evlist__set_maps(evlist, NULL, threads);
 
 	err = perf_evlist__open(evlist);
-	__T("failed to open evsel", err == 0);
+	__T("failed to open evlist", err == 0);
 
 	perf_evlist__for_each_evsel(evlist, evsel) {
 		perf_evsel__read(evsel, 0, 0, &counts);
@@ -506,7 +506,7 @@ static int test_stat_multiplexing(void)
 	perf_evlist__set_maps(evlist, NULL, threads);
 
 	err = perf_evlist__open(evlist);
-	__T("failed to open evsel", err == 0);
+	__T("failed to open evlist", err == 0);
 
 	perf_evlist__enable(evlist);
 
-- 
2.31.1


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

* [RFC PATCH 2/7] libperf: Add perf_evsel__set_close_on_exec() function
  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 ` Shunsuke
  2022-02-25 10:31 ` [RFC PATCH 3/7] libperf: Add perf_evsel__refresh()/period() functions Shunsuke
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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

Move evsel::open_flags to perf_evsel::open_flags and add a function to
set PERF_FLAG_FD_CLOEXEC.

Signed-off-by: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
---
 tools/lib/perf/Documentation/libperf.txt |  1 +
 tools/lib/perf/evsel.c                   | 15 ++++++++++++++-
 tools/lib/perf/include/internal/evsel.h  |  2 ++
 tools/lib/perf/include/perf/evsel.h      |  1 +
 tools/lib/perf/libperf.map               |  1 +
 tools/perf/util/evsel.c                  | 16 +++++++++-------
 tools/perf/util/evsel.h                  |  1 -
 7 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
index 32c5051c24eb..96a451cd34eb 100644
--- a/tools/lib/perf/Documentation/libperf.txt
+++ b/tools/lib/perf/Documentation/libperf.txt
@@ -149,6 +149,7 @@ SYNOPSIS
   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);
+  int perf_evsel__set_close_on_exec(struct perf_evsel *evsel);
 --
 
 *API to handle maps (perf ring buffers):*
diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c
index 210ea7c06ce8..d22b1468a1ad 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -26,6 +26,7 @@ void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr,
 	evsel->attr = *attr;
 	evsel->idx  = idx;
 	evsel->leader = evsel;
+	evsel->open_flags = 0;
 }
 
 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
@@ -158,7 +159,7 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
 
 			fd = sys_perf_event_open(&evsel->attr,
 						 threads->map[thread].pid,
-						 cpu, group_fd, 0);
+						 cpu, group_fd, evsel->open_flags);
 
 			if (fd < 0)
 				return -errno;
@@ -454,3 +455,15 @@ void perf_counts_values__scale(struct perf_counts_values *count,
 	if (pscaled)
 		*pscaled = scaled;
 }
+
+int perf_evsel__set_close_on_exec(struct perf_evsel *evsel)
+{
+	int err = 0;
+
+	if (evsel)
+		evsel->open_flags |= PERF_FLAG_FD_CLOEXEC;
+	else
+		err = -1;
+
+	return err;
+}
diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/include/internal/evsel.h
index cfc9ebd7968e..37a99cf261b3 100644
--- a/tools/lib/perf/include/internal/evsel.h
+++ b/tools/lib/perf/include/internal/evsel.h
@@ -51,6 +51,8 @@ struct perf_evsel {
 	int			 nr_members;
 	bool			 system_wide;
 	int			 idx;
+
+	unsigned long		 open_flags;
 };
 
 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr,
diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/perf/evsel.h
index 2a9516b42d15..23b0334a4c57 100644
--- a/tools/lib/perf/include/perf/evsel.h
+++ b/tools/lib/perf/include/perf/evsel.h
@@ -43,5 +43,6 @@ 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);
 LIBPERF_API void perf_counts_values__scale(struct perf_counts_values *count,
 					   bool scale, __s8 *pscaled);
+LIBPERF_API int perf_evsel__set_close_on_exec(struct perf_evsel *evsel);
 
 #endif /* __LIBPERF_EVSEL_H */
diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map
index 93696affda2e..0f8d13f9009b 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -52,6 +52,7 @@ LIBPERF_0.0.1 {
 		perf_mmap__read_done;
 		perf_mmap__read_event;
 		perf_counts_values__scale;
+		perf_evsel__set_close_on_exec;
 	local:
 		*;
 };
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 22d3267ce294..52c34552ee76 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1794,9 +1794,9 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	    perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0)
 		return -ENOMEM;
 
-	evsel->open_flags = PERF_FLAG_FD_CLOEXEC;
+	evsel->core.open_flags = PERF_FLAG_FD_CLOEXEC;
 	if (evsel->cgrp)
-		evsel->open_flags |= PERF_FLAG_PID_CGROUP;
+		evsel->core.open_flags |= PERF_FLAG_PID_CGROUP;
 
 	return 0;
 }
@@ -1814,7 +1814,7 @@ static void evsel__disable_missing_features(struct evsel *evsel)
 		evsel->core.attr.clockid = 0;
 	}
 	if (perf_missing_features.cloexec)
-		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
+		evsel->core.open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
 	if (perf_missing_features.mmap2)
 		evsel->core.attr.mmap2 = 0;
 	if (evsel->pmu && evsel->pmu->missing_features.exclude_guest)
@@ -1902,7 +1902,8 @@ bool evsel__detect_missing_features(struct evsel *evsel)
 		perf_missing_features.clockid = true;
 		pr_debug2_peo("switching off use_clockid\n");
 		return true;
-	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
+	} else if (!perf_missing_features.cloexec &&
+		   (evsel->core.open_flags & PERF_FLAG_FD_CLOEXEC)) {
 		perf_missing_features.cloexec = true;
 		pr_debug2_peo("switching off cloexec flag\n");
 		return true;
@@ -2029,11 +2030,12 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 			test_attr__ready();
 
 			pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
-				pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags);
+				pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd,
+				evsel->core.open_flags);
 
 			fd = sys_perf_event_open(&evsel->core.attr, pid,
 						perf_cpu_map__cpu(cpus, idx).cpu,
-						group_fd, evsel->open_flags);
+						group_fd, evsel->core.open_flags);
 
 			FD(evsel, idx, thread) = fd;
 
@@ -2050,7 +2052,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 			if (unlikely(test_attr__enabled)) {
 				test_attr__open(&evsel->core.attr, pid,
 						perf_cpu_map__cpu(cpus, idx),
-						fd, group_fd, evsel->open_flags);
+						fd, group_fd, evsel->core.open_flags);
 			}
 
 			pr_debug2_peo(" = %d\n", fd);
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 041b42d33bf5..8a545954eec7 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -152,7 +152,6 @@ struct evsel {
 		struct bperf_leader_bpf *leader_skel;
 		struct bperf_follower_bpf *follower_skel;
 	};
-	unsigned long		open_flags;
 	int			precise_ip_original;
 
 	/* for missing_features */
-- 
2.31.1


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

* [RFC PATCH 3/7] libperf: Add perf_evsel__refresh()/period() functions
  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 ` Shunsuke
  2022-02-25 10:31 ` [RFC PATCH 4/7] libperf: Add perf_evsel__set_signal() functions Shunsuke
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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

Add the following functions:

  perf_evsel__refresh()
  perf_evsel__refresh_cpu()
  perf_evsel__period()
  perf_evsel__period_cpu()

to set the over flow limit and period.

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

diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
index 96a451cd34eb..8f523293fb9d 100644
--- a/tools/lib/perf/Documentation/libperf.txt
+++ b/tools/lib/perf/Documentation/libperf.txt
@@ -146,6 +146,11 @@ SYNOPSIS
   int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx);
   int perf_evsel__disable(struct perf_evsel *evsel);
   int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx);
+  int perf_evsel__refresh(struct perf_evsel *evsel, int refresh);
+  int perf_evsel__refresh_cpu(struct perf_evsel *evsel, int refresh,
+                              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);
   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 d22b1468a1ad..8348545874e4 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -330,7 +330,7 @@ int perf_evsel__read(struct perf_evsel *evsel, int cpu_map_idx, int thread,
 }
 
 static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
-				 int ioc,  void *arg,
+				 int ioc, unsigned long arg,
 				 int cpu_map_idx)
 {
 	int thread;
@@ -353,7 +353,7 @@ static int perf_evsel__run_ioctl(struct perf_evsel *evsel,
 
 int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
 {
-	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu_map_idx);
+	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, cpu_map_idx);
 }
 
 int perf_evsel__enable(struct perf_evsel *evsel)
@@ -362,13 +362,13 @@ int perf_evsel__enable(struct perf_evsel *evsel)
 	int err = 0;
 
 	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
-		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, i);
+		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, 0, i);
 	return err;
 }
 
 int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
 {
-	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, cpu_map_idx);
+	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0, cpu_map_idx);
 }
 
 int perf_evsel__disable(struct perf_evsel *evsel)
@@ -377,7 +377,63 @@ int perf_evsel__disable(struct perf_evsel *evsel)
 	int err = 0;
 
 	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
-		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, NULL, i);
+		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_DISABLE, 0, i);
+	return err;
+}
+
+int perf_evsel__refresh_cpu(struct perf_evsel *evsel, int refresh, int cpu_map_idx)
+{
+	return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_REFRESH, refresh, cpu_map_idx);
+}
+
+int perf_evsel__refresh(struct perf_evsel *evsel, int refresh)
+{
+	int i;
+	int err = 0;
+
+	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++)
+		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_REFRESH, refresh, i);
+	return err;
+}
+
+int perf_evsel__period_cpu(struct perf_evsel *evsel, __u64 period, int cpu_map_idx)
+{
+	int err = 0;
+	struct perf_event_attr *attr;
+
+	err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_PERIOD,
+				    (unsigned long)&period, cpu_map_idx);
+	if (err)
+		return err;
+
+	attr = perf_evsel__attr(evsel);
+	if (!attr)
+		return -1;
+
+	attr->sample_period = period;
+
+	return err;
+}
+
+int perf_evsel__period(struct perf_evsel *evsel, __u64 period)
+{
+	int i;
+	int err = 0;
+	struct perf_event_attr *attr;
+
+	for (i = 0; i < xyarray__max_x(evsel->fd) && !err; i++) {
+		err = perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_PERIOD,
+					    (unsigned long)&period, i);
+		if (err)
+			return err;
+	}
+
+	attr = perf_evsel__attr(evsel);
+	if (!attr)
+		return -1;
+
+	attr->sample_period = period;
+
 	return err;
 }
 
@@ -388,7 +444,7 @@ int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
 	for (i = 0; i < perf_cpu_map__nr(evsel->cpus) && !err; i++)
 		err = perf_evsel__run_ioctl(evsel,
 				     PERF_EVENT_IOC_SET_FILTER,
-				     (void *)filter, i);
+				     (unsigned long)filter, i);
 	return err;
 }
 
diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/perf/evsel.h
index 23b0334a4c57..9ec24a5f5f9f 100644
--- a/tools/lib/perf/include/perf/evsel.h
+++ b/tools/lib/perf/include/perf/evsel.h
@@ -38,6 +38,11 @@ LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel);
 LIBPERF_API int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx);
 LIBPERF_API int perf_evsel__disable(struct perf_evsel *evsel);
 LIBPERF_API int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx);
+LIBPERF_API int perf_evsel__refresh(struct perf_evsel *evsel, int refresh);
+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 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 0f8d13f9009b..83ecbce9cfbb 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -28,6 +28,10 @@ LIBPERF_0.0.1 {
 		perf_evsel__munmap;
 		perf_evsel__mmap_base;
 		perf_evsel__read;
+		perf_evsel__refresh;
+		perf_evsel__refresh_cpu;
+		perf_evsel__period;
+		perf_evsel__period_cpu;
 		perf_evsel__cpus;
 		perf_evsel__threads;
 		perf_evsel__attr;
-- 
2.31.1


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

* [RFC PATCH 4/7] libperf: Add perf_evsel__set_signal() functions
  2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
                   ` (2 preceding siblings ...)
  2022-02-25 10:31 ` [RFC PATCH 3/7] libperf: Add perf_evsel__refresh()/period() functions Shunsuke
@ 2022-02-25 10:31 ` Shunsuke
  2022-02-25 10:31 ` [RFC PATCH 5/7] libperf: Add perf_evsel__check_fd() functions Shunsuke
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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


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

* [RFC PATCH 5/7] libperf: Add perf_evsel__check_fd() functions
  2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
                   ` (3 preceding siblings ...)
  2022-02-25 10:31 ` [RFC PATCH 4/7] libperf: Add perf_evsel__set_signal() functions Shunsuke
@ 2022-02-25 10:31 ` Shunsuke
  2022-02-25 10:31 ` [RFC PATCH 6/7] libperf test: Add test_stat_overflow() Shunsuke
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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

Add the following functions:

  perf_evsel__check_fd()
  perf_evsel__check_fd_cpu()

to check for perf events with the file descriptor specified in the
argument.
These functions can be used in signal handlers to detect overflow.

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

diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
index d0aa57c6ca97..d3a1a9e77262 100644
--- a/tools/lib/perf/Documentation/libperf.txt
+++ b/tools/lib/perf/Documentation/libperf.txt
@@ -155,6 +155,8 @@ SYNOPSIS
                              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);
+  int perf_evsel__check_fd(struct perf_evsel *evsel, int sig_fd);
+  int perf_evsel__check_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx, int sig_fd);
   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 991ab04112b1..0c626f846980 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -542,6 +542,33 @@ int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type, unsigned in
 	return err;
 }
 
+int perf_evsel__check_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx, int sig_fd)
+{
+	int thread;
+	int *fd;
+
+	for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
+		fd = FD(evsel, cpu_map_idx, thread);
+		if (sig_fd <= 0 || !fd || *fd < 0)
+			return -1;
+
+		if (sig_fd == *fd)
+			return 0;
+	}
+
+	return -1;
+}
+
+int perf_evsel__check_fd(struct perf_evsel *evsel, int sig_fd)
+{
+	int i;
+	int err = 0;
+
+	for (i = 0; i < xyarray__max_x(evsel->fd); i++)
+		err = perf_evsel__check_fd_cpu(evsel, i, sig_fd);
+	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 15768fc91910..5d4b4912fec9 100644
--- a/tools/lib/perf/include/perf/evsel.h
+++ b/tools/lib/perf/include/perf/evsel.h
@@ -49,6 +49,8 @@ LIBPERF_API int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type,
 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 int perf_evsel__check_fd(struct perf_evsel *evsel, int sig_fd);
+LIBPERF_API int perf_evsel__check_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx, int sig_fd);
 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 5e04fe2b611c..edd60bc69822 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -34,6 +34,8 @@ LIBPERF_0.0.1 {
 		perf_evsel__period_cpu;
 		perf_evsel__set_signal;
 		perf_evsel__set_signal_cpu;
+		perf_evsel__check_fd;
+		perf_evsel__check_fd_cpu;
 		perf_evsel__cpus;
 		perf_evsel__threads;
 		perf_evsel__attr;
-- 
2.31.1


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

* [RFC PATCH 6/7] libperf test: Add test_stat_overflow()
  2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
                   ` (4 preceding siblings ...)
  2022-02-25 10:31 ` [RFC PATCH 5/7] libperf: Add perf_evsel__check_fd() functions Shunsuke
@ 2022-02-25 10:31 ` 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
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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

Added tests using refresh and period.

Confirmation
 - That the overflow occurs the number of times specified by
   perf_evse__refresh()
 - That the period can be updated by perf_evsel__period()

Committer testing:

  $ sudo make tests -C tools/lib/perf/ V=1
  make: Entering directory '/home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/perf'
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=. obj=libperf
  make -C /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/api/ O= libapi.a
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./fd obj=libapi
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./fs obj=libapi
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=. obj=tests
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./tests obj=tests
  running static:
  - running tests/test-cpumap.c...OK
  - running tests/test-threadmap.c...OK
  - running tests/test-evlist.c...

  <SNIP>

  OK
  - running tests/test-evsel.c...
          loop = 65536, count = 330818
          loop = 131072, count = 662958
          loop = 262144, count = 1316088
          loop = 524288, count = 2632699
          loop = 1048576, count = 5254501
          loop = 65536, count = 348709
          loop = 131072, count = 694239
          loop = 262144, count = 1370888
          loop = 524288, count = 2744824
          loop = 1048576, count = 5465047
          period = 1000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
          period = 2000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
          period = 1000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
          period = 2000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
  OK
  running dynamic:
  - running tests/test-cpumap.c...OK
  - running tests/test-threadmap.c...OK
  - running tests/test-evlist.c...

  <SNIP>

  OK
  - running tests/test-evsel.c...
          loop = 65536, count = 330819
          loop = 131072, count = 662959
          loop = 262144, count = 1323589
          loop = 524288, count = 2626809
          loop = 1048576, count = 5260090
          loop = 65536, count = 351641
          loop = 131072, count = 697661
          loop = 262144, count = 1373234
          loop = 524288, count = 2743662
          loop = 1048576, count = 5473195
          period = 1000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
          period = 2000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
          period = 1000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
          period = 2000000
          overflow limit = 3, overflow count = 3POLL_IN = 2, POLL_UP = 1, other signal event = 0
  OK
  make: Leaving directory '/home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/perf'

Signed-off-by: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
---
 tools/lib/perf/tests/test-evsel.c | 110 ++++++++++++++++++++++++++++++
 1 file changed, 110 insertions(+)

diff --git a/tools/lib/perf/tests/test-evsel.c b/tools/lib/perf/tests/test-evsel.c
index 89be89afb24d..9232b2f25082 100644
--- a/tools/lib/perf/tests/test-evsel.c
+++ b/tools/lib/perf/tests/test-evsel.c
@@ -1,6 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <inttypes.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+#include <fcntl.h>
 #include <linux/perf_event.h>
 #include <perf/cpumap.h>
 #include <perf/threadmap.h>
@@ -8,6 +16,15 @@
 #include <internal/tests.h>
 #include "tests.h"
 
+#define WAIT_COUNT 10000000UL
+static struct signal_counts {
+	int in;
+	int hup;
+	int others;
+	int overflow;
+} sig_count = {0, 0, 0};
+static struct perf_evsel *s_evsel;
+
 static int libperf_print(enum libperf_print_level level,
 			 const char *fmt, va_list ap)
 {
@@ -189,6 +206,97 @@ static int test_stat_user_read(int event)
 	return 0;
 }
 
+static void sig_handler(int signo, siginfo_t *info, void *uc)
+{
+	switch (info->si_code) {
+	case POLL_IN:
+		sig_count.in++;
+		break;
+	case POLL_HUP:
+		sig_count.hup++;
+		break;
+	default:
+		sig_count.others++;
+	}
+
+	sig_count.overflow++;
+}
+
+static int test_stat_refresh(int owner)
+{
+	static struct sigaction sig;
+	u64 period = 1000000;
+	int overflow_limit = 3;
+
+	struct perf_thread_map *threads;
+	struct perf_event_attr attr = {
+		.type           = PERF_TYPE_SOFTWARE,
+		.config         = PERF_COUNT_SW_TASK_CLOCK,
+		.sample_type    = PERF_SAMPLE_PERIOD,
+		.sample_period  = period,
+		.disabled       = 1,
+	};
+	struct perf_event_attr *tmp_attr;
+	int err, i;
+	unsigned int wait_count;
+
+	/* setup signal handler */
+	memset(&sig, 0, sizeof(struct sigaction));
+	sig.sa_sigaction = (void *)sig_handler;
+	sig.sa_flags = SA_SIGINFO;
+
+	threads = perf_thread_map__new_dummy();
+	__T("failed to create threads", threads);
+
+	perf_thread_map__set_pid(threads, 0, 0);
+
+	s_evsel = perf_evsel__new(&attr);
+	__T("failed to create evsel", s_evsel);
+
+	err = perf_evsel__set_close_on_exec(s_evsel);
+	__T("failed to set PERF_FLAG_FD_CLOEXEC flag to evsel", err == 0);
+
+	err = perf_evsel__open(s_evsel, NULL, threads);
+	__T("failed to open evsel", err == 0);
+
+	err = perf_evsel__set_signal(s_evsel, owner, SIGIO, &sig);
+	__T("failed to set signal", err == 0);
+
+	for (i = 0; i < 2; i++) {
+		period = period << i;
+		err = perf_evsel__period(s_evsel, period);
+		__T("failed to period evsel", err == 0);
+
+		tmp_attr = perf_evsel__attr(s_evsel);
+		__T_VERBOSE("\tperiod = %llu\n", tmp_attr->sample_period);
+
+		err = perf_evsel__refresh(s_evsel, overflow_limit);
+		__T("failed to refresh evsel", err == 0);
+
+		wait_count = WAIT_COUNT;
+		while (wait_count--)
+			;
+
+		__T_VERBOSE("\toverflow limit = %d, overflow count = %d",
+			    overflow_limit, sig_count.overflow);
+		__T_VERBOSE("POLL_IN = %d, POLL_UP = %d, other signal event = %d\n",
+			    sig_count.in, sig_count.hup, sig_count.others);
+
+		__T("failed to overflow count", overflow_limit == sig_count.overflow);
+
+		sig_count.in = 0;
+		sig_count.hup = 0;
+		sig_count.others = 0;
+		sig_count.overflow = 0;
+	}
+
+	perf_evsel__close(s_evsel);
+	perf_evsel__delete(s_evsel);
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
 int test_evsel(int argc, char **argv)
 {
 	__T_START;
@@ -200,6 +308,8 @@ int test_evsel(int argc, char **argv)
 	test_stat_thread_enable();
 	test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS);
 	test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES);
+	test_stat_refresh(F_OWNER_PID);
+	test_stat_refresh(F_OWNER_TID);
 
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
-- 
2.31.1


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

* [RFC PATCH 7/7] libperf test: Add test_detect_overflow_event()
  2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
                   ` (5 preceding siblings ...)
  2022-02-25 10:31 ` [RFC PATCH 6/7] libperf test: Add test_stat_overflow() Shunsuke
@ 2022-02-25 10:31 ` Shunsuke
  2022-02-26 17:23 ` [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Jiri Olsa
  7 siblings, 0 replies; 10+ messages in thread
From: Shunsuke @ 2022-02-25 10:31 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung
  Cc: linux-kernel, linux-perf-users, Shunsuke Nakamura

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

Add a test to detect overflowed events.

Committer testing:

  $ sudo make tests -C tools/lib/perf/ V=1
  make: Entering directory '/home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/perf'
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=. obj=libperf
  make -C /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/api/ O= libapi.a
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./fd obj=libapi
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./fs obj=libapi
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=. obj=tests
  make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./tests obj=tests
  running static:
  - running tests/test-cpumap.c...OK
  - running tests/test-threadmap.c...OK
  - running tests/test-evlist.c...

  <SNIP>

  Event  0 -- overflow flag = 0x1, POLL_UP = 1, other signal event = 0
  Event  1 -- overflow flag = 0x2, POLL_UP = 1, other signal event = 0
  Event  2 -- overflow flag = 0x4, POLL_UP = 1, other signal event = 0
  Event  3 -- overflow flag = 0x8, POLL_UP = 1, other signal event = 0
  OK
  - running tests/test-evsel.c...

  <SNIP>

  OK
  running dynamic:
  - running tests/test-cpumap.c...OK
  - running tests/test-threadmap.c...OK
  - running tests/test-evlist.c...

  <SNIP>

  Event  0 -- overflow flag = 0x1, POLL_UP = 1, other signal event = 0
  Event  1 -- overflow flag = 0x2, POLL_UP = 1, other signal event = 0
  Event  2 -- overflow flag = 0x4, POLL_UP = 1, other signal event = 0
  Event  3 -- overflow flag = 0x8, POLL_UP = 1, other signal event = 0
  OK
  - running tests/test-evsel.c...

  <SNIP>

  OK
  make: Leaving directory '/home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/perf'

Signed-off-by: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
---
 tools/lib/perf/tests/test-evlist.c | 119 +++++++++++++++++++++++++++++
 1 file changed, 119 insertions(+)

diff --git a/tools/lib/perf/tests/test-evlist.c b/tools/lib/perf/tests/test-evlist.c
index fafc6b0f8687..c0f0109c8b51 100644
--- a/tools/lib/perf/tests/test-evlist.c
+++ b/tools/lib/perf/tests/test-evlist.c
@@ -5,6 +5,8 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
 #include <linux/perf_event.h>
 #include <linux/limits.h>
 #include <sys/types.h>
@@ -24,6 +26,13 @@
 #define EVENT_NUM 15
 #define WAIT_COUNT 100000000UL
 
+static unsigned int overflow_flag;
+static struct signal_counts {
+	int hup;
+	int others;
+} sig_count = { 0, 0 };
+static struct perf_evlist *s_evlist;
+
 static int libperf_print(enum libperf_print_level level,
 			 const char *fmt, va_list ap)
 {
@@ -570,6 +579,115 @@ static int test_stat_multiplexing(void)
 	return 0;
 }
 
+static void sig_handler(int signo, siginfo_t *info, void *uc)
+{
+	int err;
+	int idx = 0;
+	struct perf_evsel *evsel;
+
+	switch (info->si_code) {
+	case POLL_HUP:
+		perf_evlist__for_each_evsel(s_evlist, evsel) {
+			err = perf_evsel__check_fd(evsel, info->si_fd);
+			if (!err) {
+				overflow_flag = (1U << idx);
+				sig_count.hup++;
+				break;
+			}
+			idx++;
+		}
+		break;
+	default:
+		perf_evlist__for_each_evsel(s_evlist, evsel) {
+			err = perf_evsel__check_fd(evsel, info->si_fd);
+			if (!err) {
+				overflow_flag = (1U << idx);
+				sig_count.others++;
+				break;
+			}
+			idx++;
+		}
+	}
+}
+
+static int test_stat_overflow_event(void)
+{
+	static struct sigaction sig;
+
+	struct perf_thread_map *threads;
+	struct perf_evsel *evsel;
+	struct perf_event_attr attr = {
+		.type		= PERF_TYPE_SOFTWARE,
+		.config		= PERF_COUNT_SW_CPU_CLOCK,
+		.sample_type    = PERF_SAMPLE_PERIOD,
+		.sample_period  = 100000,
+		.disabled       = 1,
+	};
+	int err, i, event_num = 4;
+	unsigned int wait_count;
+
+	/* setup signal handler */
+	memset(&sig, 0, sizeof(struct sigaction));
+	sig.sa_sigaction = (void *)sig_handler;
+	sig.sa_flags = SA_SIGINFO;
+
+	threads = perf_thread_map__new_dummy();
+	__T("failed to create threads", threads);
+
+	perf_thread_map__set_pid(threads, 0, 0);
+
+	s_evlist = perf_evlist__new();
+	__T("failed to create evlist", s_evlist);
+
+	for (i = 0; i < event_num; i++) {
+		evsel = perf_evsel__new(&attr);
+		__T("failed to create evsel", evsel);
+
+		err = perf_evsel__set_close_on_exec(evsel);
+		__T("failed to set PERF_FLAG_FD_CLOEXEC flag to evsel", err == 0);
+
+		perf_evlist__add(s_evlist, evsel);
+	}
+
+	perf_evlist__set_maps(s_evlist, NULL, threads);
+
+	err = perf_evlist__open(s_evlist);
+	__T("failed to open evlist", err == 0);
+
+	i = 0;
+	perf_evlist__for_each_evsel(s_evlist, evsel) {
+		err = perf_evsel__set_signal(evsel, F_OWNER_PID, SIGIO, &sig);
+		__T("failed to set signal", err == 0);
+
+		err = perf_evsel__refresh(evsel, 1);
+		__T("failed to refresh evsel", err == 0);
+
+		wait_count = WAIT_COUNT;
+		while (wait_count--)
+			;
+
+		__T_VERBOSE("Event %2d -- overflow flag = %#x, ",
+			    i, overflow_flag);
+		__T_VERBOSE("POLL_UP = %d, other signal event = %d\n",
+			    sig_count.hup, sig_count.others);
+
+		__T("unexpected event overflow detected", overflow_flag && (1U << i));
+		__T("unexpected signal event detected",
+		    sig_count.hup == 1 && sig_count.others == 0);
+
+		overflow_flag = 0;
+		sig_count.hup = 0;
+		sig_count.others = 0;
+		i++;
+	}
+
+	perf_evlist__close(s_evlist);
+	perf_evlist__delete(s_evlist);
+	perf_thread_map__put(threads);
+
+	return 0;
+}
+
 int test_evlist(int argc, char **argv)
 {
 	__T_START;
@@ -582,6 +700,7 @@ int test_evlist(int argc, char **argv)
 	test_mmap_thread();
 	test_mmap_cpus();
 	test_stat_multiplexing();
+	test_stat_overflow_event();
 
 	__T_END;
 	return tests_failed == 0 ? 0 : -1;
-- 
2.31.1


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

* Re: [RFC PATCH 0/7] libperf: Add overflow detection of sampling events
  2022-02-25 10:31 [RFC PATCH 0/7] libperf: Add overflow detection of sampling events Shunsuke
                   ` (6 preceding siblings ...)
  2022-02-25 10:31 ` [RFC PATCH 7/7] libperf test: Add test_detect_overflow_event() Shunsuke
@ 2022-02-26 17:23 ` Jiri Olsa
  2022-03-01  8:32   ` nakamura.shun
  7 siblings, 1 reply; 10+ messages in thread
From: Jiri Olsa @ 2022-02-26 17:23 UTC (permalink / raw)
  To: Shunsuke
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, linux-kernel, linux-perf-users

On Fri, Feb 25, 2022 at 07:31:07PM +0900, Shunsuke wrote:
> From: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
> 
> This patch series adds sampling event overflow detection capability
> to libperf.
> 
> First patch fixes a typo in the error message that I noticed.
> 
> Second patch  adds a interface to set PERF_FLAG_FD_CLOEXEC
> 
> Third patch adds a interface to perform IOC_REFRESH and IOC_PERIOD.
> 
> Fourth patch adds a interface to set the signal handler.
> 
> Fifth patch adds a interface to detect overflowed events.
> 
> Sixth and seventh patch adds tests.
> 
> Shunsuke Nakamura (7):
>   libperf tests: Fix typo in the error message "evsel" -> "evlist"
>   libperf: Add perf_evsel__set_close_on_exec() function
>   libperf: Add perf_evsel__refresh()/period() functions
>   libperf: Add perf_evsel__set_signal() functions
>   libperf: Add perf_evsel__check_fd() functions
>   libperf test: Add test_stat_overflow()
>   libperf test: Add test_detect_overflow_event()

hi,
I think the interface might be more clear if we use additional options
for new and open functions, same way like it's done in libbpf

how about adding following interface:

  - perf_evsel__new_opts would allow to setup needed attr fields before opening

    struct perf_evsel_new_opts {
      /* size of this struct, for forward/backward compatiblity */
      size_t sz;

      int open_flags;
      int sample_period;
    }
   
    struct perf_evsel *perf_evsel__new_opts(struct perf_event_attr *attr,
                                            struct perf_evsel_new_opts *opts);

  - perf_evlist__open_opts would do additional setup 'after' the event
    is open and we have file descriptor available


    struct perf_evsel_open_opts {
      /* size of this struct, for forward/backward compatiblity */
      size_t sz;

      int flags; /* fcntl flags */
      int signal;
      int owner_type;
    }

    int perf_evlist__open_opts(struct perf_evlist *evlist,
                               struct perf_evsel_open_opts *opts);)

    int perf_evsel__open_opts(struct perf_evsel *evsel,
                              struct perf_cpu_map *cpus,
                              struct perf_thread_map *threads,
                              struct perf_evsel_open_opts *opts)

    not sure we want special opts for evlist.. the evlist open
    is just a wrapper and I don't think there's special open
    info just for evlist

I did not analyze your usecases deeply, so I might be missing some
case where above API would need some adjustment, but from the quick
look this could fit and I think it's better than special functions
for each feature

there are additional macros like DECLARE_LIBBPF_OPTS that allows to
define the opts structures in backward compatible way, we'd need to
'borrow' that as well

thoughts?

thanks,
jirka

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

* RE: [RFC PATCH 0/7] libperf: Add overflow detection of sampling events
  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
  0 siblings, 0 replies; 10+ messages in thread
From: nakamura.shun @ 2022-03-01  8:32 UTC (permalink / raw)
  To: 'Jiri Olsa'
  Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
	jolsa@redhat.com, namhyung@kernel.org,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org

Hi jirka

> On Fri, Feb 25, 2022 at 07:31:07PM +0900, Shunsuke wrote:
> > From: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
> >
> > This patch series adds sampling event overflow detection capability to
> > libperf.
> >
> > First patch fixes a typo in the error message that I noticed.
> >
> > Second patch  adds a interface to set PERF_FLAG_FD_CLOEXEC
> >
> > Third patch adds a interface to perform IOC_REFRESH and IOC_PERIOD.
> >
> > Fourth patch adds a interface to set the signal handler.
> >
> > Fifth patch adds a interface to detect overflowed events.
> >
> > Sixth and seventh patch adds tests.
> >
> > Shunsuke Nakamura (7):
> >   libperf tests: Fix typo in the error message "evsel" -> "evlist"
> >   libperf: Add perf_evsel__set_close_on_exec() function
> >   libperf: Add perf_evsel__refresh()/period() functions
> >   libperf: Add perf_evsel__set_signal() functions
> >   libperf: Add perf_evsel__check_fd() functions
> >   libperf test: Add test_stat_overflow()
> >   libperf test: Add test_detect_overflow_event()
> 
> hi,
> I think the interface might be more clear if we use additional options for new and
> open functions, same way like it's done in libbpf
> 
> how about adding following interface:
> 
>   - perf_evsel__new_opts would allow to setup needed attr fields before opening
> 
>     struct perf_evsel_new_opts {
>       /* size of this struct, for forward/backward compatiblity */
>       size_t sz;
> 
>       int open_flags;
>       int sample_period;
>     }
> 
>     struct perf_evsel *perf_evsel__new_opts(struct perf_event_attr *attr,
>                                             struct perf_evsel_new_opts
> *opts);
> 
>   - perf_evlist__open_opts would do additional setup 'after' the event
>     is open and we have file descriptor available
> 
> 
>     struct perf_evsel_open_opts {
>       /* size of this struct, for forward/backward compatiblity */
>       size_t sz;
> 
>       int flags; /* fcntl flags */
>       int signal;
>       int owner_type;
>     }
> 
>     int perf_evlist__open_opts(struct perf_evlist *evlist,
>                                struct perf_evsel_open_opts *opts);)
> 
>     int perf_evsel__open_opts(struct perf_evsel *evsel,
>                               struct perf_cpu_map *cpus,
>                               struct perf_thread_map *threads,
>                               struct perf_evsel_open_opts *opts)
> 
>     not sure we want special opts for evlist.. the evlist open
>     is just a wrapper and I don't think there's special open
>     info just for evlist
> 
> I did not analyze your usecases deeply, so I might be missing some case where
> above API would need some adjustment, but from the quick look this could fit and
> I think it's better than special functions for each feature
> 
> there are additional macros like DECLARE_LIBBPF_OPTS that allows to define
> the opts structures in backward compatible way, we'd need to 'borrow' that as well
> 
> thoughts?

Thank you for your comment. I'll consider about it.

Best Regards
Shunsuke

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

end of thread, other threads:[~2022-03-01  8:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH 4/7] libperf: Add perf_evsel__set_signal() functions Shunsuke
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).