linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 0/2] Minor fixes error handling of perf stat
@ 2024-09-25 11:08 Levi Yun
  2024-09-25 11:08 ` [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed Levi Yun
  2024-09-25 11:08 ` [PATCH v5 2/2] perf stat: Stop repeating when ref_perf_stat() returns -1 Levi Yun
  0 siblings, 2 replies; 6+ messages in thread
From: Levi Yun @ 2024-09-25 11:08 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, james.clark, howardchu95
  Cc: nd, linux-perf-users, linux-kernel, Levi Yun

This patchset fixes two issues that were seen when running
  "perf stat -r" with perf_event_paranoid=3

   1. failed with Too many open files.
     $ perf stat -r 1044 -- false
     ...
     failed to create 'go' pipe: Too many open files
     failed to prepare workload: Too many open files
     ...

   2. repating error message
     $ perf stat -r 1044 -- false
     Error:
     Access to performance monitoring and observability operations is limited.
     ...
     (repating with same error message 1044 times).
v5:
  - add error handling label.

v4:
  - Move comments to run_perf_stat().

v3:
  - Fix comments.

v2:
  - Add some comments.

Levi Yun (2):
  perf stat: Close cork_fd when create_perf_stat_counter() failed
  perf stat: Stop repeating when ref_perf_stat() returns -1

 tools/perf/builtin-stat.c | 11 ++++++++++-
 tools/perf/util/evlist.c  | 14 +++++++++++++-
 tools/perf/util/evlist.h  |  1 +
 3 files changed, 24 insertions(+), 2 deletions(-)

--
LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}


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

* [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed
  2024-09-25 11:08 [PATCH v5 0/2] Minor fixes error handling of perf stat Levi Yun
@ 2024-09-25 11:08 ` Levi Yun
  2024-09-25 12:15   ` Andi Kleen
  2024-09-25 11:08 ` [PATCH v5 2/2] perf stat: Stop repeating when ref_perf_stat() returns -1 Levi Yun
  1 sibling, 1 reply; 6+ messages in thread
From: Levi Yun @ 2024-09-25 11:08 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, james.clark, howardchu95
  Cc: nd, linux-perf-users, linux-kernel, Levi Yun

When create_perf_stat_counter() failed, it doesn't close workload.cork_fd
open in evlist__prepare_workload(). This could make too many open file
error while __run_perf_stat() repeats.

Introduce evlist__cancel_workload to close workload.cork_fd and
wait workload.child_pid until exit to clear child process
when create_perf_stat_counter() is failed.

Signed-off-by: Levi Yun <yeoreum.yun@arm.com>
Reviewed-by: James Clark <james.clark@linaro.org>
---
 tools/perf/builtin-stat.c | 50 +++++++++++++++++++++++++++------------
 tools/perf/util/evlist.c  | 15 +++++++++++-
 tools/perf/util/evlist.h  |  1 +
 3 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 689a3d43c258..2c46bdbd9914 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -716,15 +716,19 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	}

 	if (!cpu_map__is_dummy(evsel_list->core.user_requested_cpus)) {
-		if (affinity__setup(&saved_affinity) < 0)
-			return -1;
+		if (affinity__setup(&saved_affinity) < 0) {
+			err = -1;
+			goto err_out;
+		}
 		affinity = &saved_affinity;
 	}

 	evlist__for_each_entry(evsel_list, counter) {
 		counter->reset_group = false;
-		if (bpf_counter__load(counter, &target))
-			return -1;
+		if (bpf_counter__load(counter, &target)) {
+			err = -1;
+			goto err_out;
+		}
 		if (!(evsel__is_bperf(counter)))
 			all_counters_use_bpf = false;
 	}
@@ -767,7 +771,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)

 			switch (stat_handle_error(counter)) {
 			case COUNTER_FATAL:
-				return -1;
+				err = -1;
+				goto err_out;
 			case COUNTER_RETRY:
 				goto try_again;
 			case COUNTER_SKIP:
@@ -808,7 +813,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)

 				switch (stat_handle_error(counter)) {
 				case COUNTER_FATAL:
-					return -1;
+					err = -1;
+					goto err_out;
 				case COUNTER_RETRY:
 					goto try_again_reset;
 				case COUNTER_SKIP:
@@ -833,8 +839,10 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 			stat_config.unit_width = l;

 		if (evsel__should_store_id(counter) &&
-		    evsel__store_ids(counter, evsel_list))
-			return -1;
+		    evsel__store_ids(counter, evsel_list)) {
+			err = -1;
+			goto err_out;
+		}
 	}

 	if (evlist__apply_filters(evsel_list, &counter, &target)) {
@@ -855,20 +863,23 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		}

 		if (err < 0)
-			return err;
+			goto err_out;

 		err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
 							 process_synthesized_event, is_pipe);
 		if (err < 0)
-			return err;
+			goto err_out;
+
 	}

 	if (target.initial_delay) {
 		pr_info(EVLIST_DISABLED_MSG);
 	} else {
 		err = enable_counters();
-		if (err)
-			return -1;
+		if (err) {
+			err = -1;
+			goto err_out;
+		}
 	}

 	/* Exec the command, if any */
@@ -878,8 +889,10 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	if (target.initial_delay > 0) {
 		usleep(target.initial_delay * USEC_PER_MSEC);
 		err = enable_counters();
-		if (err)
-			return -1;
+		if (err) {
+			err = -1;
+			goto err_out;
+		}

 		pr_info(EVLIST_ENABLED_MSG);
 	}
@@ -899,7 +912,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		if (workload_exec_errno) {
 			const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
 			pr_err("Workload failed: %s\n", emsg);
-			return -1;
+			err = -1;
+			goto err_out;
 		}

 		if (WIFSIGNALED(status))
@@ -946,6 +960,12 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		evlist__close(evsel_list);

 	return WEXITSTATUS(status);
+
+err_out:
+	if (forks)
+		evlist__cancel_workload(evsel_list);
+
+	return err;
 }

 static int run_perf_stat(int argc, const char **argv, int run_idx)
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index f14b7e6ff1dc..a37112174535 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -48,6 +48,7 @@
 #include <sys/mman.h>
 #include <sys/prctl.h>
 #include <sys/timerfd.h>
+#include <sys/wait.h>

 #include <linux/bitops.h>
 #include <linux/hash.h>
@@ -1536,7 +1537,7 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
 		 * For cancelling the workload without actually running it,
 		 * the parent will just close workload.cork_fd, without writing
 		 * anything, i.e. read will return zero and we just exit()
-		 * here.
+		 * here (See evlist__cancel_workload()).
 		 */
 		if (ret != 1) {
 			if (ret == -1)
@@ -1611,12 +1612,24 @@ int evlist__start_workload(struct evlist *evlist)
 			perror("unable to write to pipe");

 		close(evlist->workload.cork_fd);
+		evlist->workload.cork_fd = -1;
 		return ret;
 	}

 	return 0;
 }

+void evlist__cancel_workload(struct evlist *evlist)
+{
+	int status;
+
+	if (evlist->workload.cork_fd > 0) {
+		close(evlist->workload.cork_fd);
+		evlist->workload.cork_fd = -1;
+		waitpid(evlist->workload.pid, &status, WNOHANG);
+	}
+}
+
 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
 {
 	struct evsel *evsel = evlist__event2evsel(evlist, event);
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index bcc1c6984bb5..888fda751e1a 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -186,6 +186,7 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target,
 			     const char *argv[], bool pipe_output,
 			     void (*exec_error)(int signo, siginfo_t *info, void *ucontext));
 int evlist__start_workload(struct evlist *evlist);
+void evlist__cancel_workload(struct evlist *evlist);

 struct option;

--
LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}


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

* [PATCH v5 2/2] perf stat: Stop repeating when ref_perf_stat() returns -1
  2024-09-25 11:08 [PATCH v5 0/2] Minor fixes error handling of perf stat Levi Yun
  2024-09-25 11:08 ` [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed Levi Yun
@ 2024-09-25 11:08 ` Levi Yun
  1 sibling, 0 replies; 6+ messages in thread
From: Levi Yun @ 2024-09-25 11:08 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, james.clark, howardchu95
  Cc: nd, linux-perf-users, linux-kernel, Levi Yun

Exit when run_perf_stat() returns an error to avoid continuously
repeating the same error message. It's not expected that COUNTER_FATAL
or internal errors are recoverable so there's no point in retrying.

This fixes the following flood of error messages for permission issues,
for example when perf_event_paranoid==3:
  perf stat -r 1044 -- false

  Error:
  Access to performance monitoring and observability operations is limited.
  ...
  Error:
  Access to performance monitoring and observability operations is limited.
  ...
  (repeating for 1044 times).

Signed-off-by: Levi Yun <yeoreum.yun@arm.com>
Reviewed-by: James Clark <james.clark@linaro.org>
---
 tools/perf/builtin-stat.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 2c46bdbd9914..6cad3435b198 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -968,6 +968,14 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	return err;
 }

+/*
+ * Returns -1 for fatal errors which signifies to not continue
+ * when in repeat mode.
+ *
+ * Returns < -1 error codes when stat record is used. These
+ * result in the stat information being displayed, but writing
+ * to the file fails and is non fatal.
+ */
 static int run_perf_stat(int argc, const char **argv, int run_idx)
 {
 	int ret;
@@ -2899,7 +2907,10 @@ int cmd_stat(int argc, const char **argv)
 			evlist__reset_prev_raw_counts(evsel_list);

 		status = run_perf_stat(argc, argv, run_idx);
-		if (forever && status != -1 && !interval) {
+		if (status == -1)
+			break;
+
+		if (forever && !interval) {
 			print_counters(NULL, argc, argv);
 			perf_stat__reset_stats();
 		}
--
LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}


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

* Re: [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed
  2024-09-25 11:08 ` [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed Levi Yun
@ 2024-09-25 12:15   ` Andi Kleen
  2024-09-25 12:40     ` Yeo Reum Yun
  0 siblings, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2024-09-25 12:15 UTC (permalink / raw)
  To: Levi Yun
  Cc: peterz, mingo, acme, namhyung, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, james.clark, howardchu95, nd,
	linux-perf-users, linux-kernel

Levi Yun <yeoreum.yun@arm.com> writes:
> +void evlist__cancel_workload(struct evlist *evlist)
> +{
> +	int status;
> +
> +	if (evlist->workload.cork_fd > 0) {

Technically 0 is a valid file descriptor. Check for >= 0
And make sure the field is initialized with -1

The rest looks fine to me.

Reviewed-by: Andi Kleen <ak@linux.intel.com>

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

* Re: [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed
  2024-09-25 12:15   ` Andi Kleen
@ 2024-09-25 12:40     ` Yeo Reum Yun
  2024-09-25 13:17       ` Andi Kleen
  0 siblings, 1 reply; 6+ messages in thread
From: Yeo Reum Yun @ 2024-09-25 12:40 UTC (permalink / raw)
  To: Andi Kleen
  Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	namhyung@kernel.org, Mark Rutland,
	alexander.shishkin@linux.intel.com, jolsa@kernel.org,
	irogers@google.com, adrian.hunter@intel.com,
	james.clark@linaro.org, howardchu95@gmail.com, nd,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org

Hi Andi,

> Technically 0 is a valid file descriptor. Check for >= 0

Right. However, when I see the event, evlist__start_workload(),
it checks cork_fd with

    if (evlist->workload.cork_fd > 0) {

Would you like to change this with this patchset too?

Thanks.

________________________________________
From: Andi Kleen <ak@linux.intel.com>
Sent: 25 September 2024 13:15
To: Yeo Reum Yun
Cc: peterz@infradead.org; mingo@redhat.com; acme@kernel.org; namhyung@kernel.org; Mark Rutland; alexander.shishkin@linux.intel.com; jolsa@kernel.org; irogers@google.com; adrian.hunter@intel.com; james.clark@linaro.org; howardchu95@gmail.com; nd; linux-perf-users@vger.kernel.org; linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed

Levi Yun <yeoreum.yun@arm.com> writes:
> +void evlist__cancel_workload(struct evlist *evlist)
> +{
> +     int status;
> +
> +     if (evlist->workload.cork_fd > 0) {

Technically 0 is a valid file descriptor. Check for >= 0
And make sure the field is initialized with -1

The rest looks fine to me.

Reviewed-by: Andi Kleen <ak@linux.intel.com>

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

* Re: [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed
  2024-09-25 12:40     ` Yeo Reum Yun
@ 2024-09-25 13:17       ` Andi Kleen
  0 siblings, 0 replies; 6+ messages in thread
From: Andi Kleen @ 2024-09-25 13:17 UTC (permalink / raw)
  To: Yeo Reum Yun
  Cc: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	namhyung@kernel.org, Mark Rutland,
	alexander.shishkin@linux.intel.com, jolsa@kernel.org,
	irogers@google.com, adrian.hunter@intel.com,
	james.clark@linaro.org, howardchu95@gmail.com, nd,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org

On Wed, Sep 25, 2024 at 12:40:44PM +0000, Yeo Reum Yun wrote:
> Hi Andi,
> 
> > Technically 0 is a valid file descriptor. Check for >= 0
> 
> Right. However, when I see the event, evlist__start_workload(),
> it checks cork_fd with
> 
>     if (evlist->workload.cork_fd > 0) {
> 
> Would you like to change this with this patchset too?

Yes please.

-Andi

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

end of thread, other threads:[~2024-09-25 13:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-25 11:08 [PATCH v5 0/2] Minor fixes error handling of perf stat Levi Yun
2024-09-25 11:08 ` [PATCH v5 1/2] perf stat: Close cork_fd when create_perf_stat_counter() failed Levi Yun
2024-09-25 12:15   ` Andi Kleen
2024-09-25 12:40     ` Yeo Reum Yun
2024-09-25 13:17       ` Andi Kleen
2024-09-25 11:08 ` [PATCH v5 2/2] perf stat: Stop repeating when ref_perf_stat() returns -1 Levi Yun

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).