Skip to contentSkip to conversation

perf tool: improve error handling in perf_flag_probe()

PATCH In review Yann Droneaud 2 participants 4 messages Sep 13, 2014 – Sep 20, 2014 perf events

lore
Share & tools
lore
#0 lore raw

cover perf tool: improve error handling in perf_flag_probe()

Hi,

Following the EBUSY errors reported by Jiri Olsa [1], I've tryed to improve a bit the way perf_flag_probe() handle errors.

In case EBUSY is returned by perf_event_open(), testing the function again without PERF_FLAG_FD_CLOEXEC is meaningless: EBUSY is not related to close-on-exec flag, so there's nothing to confirm.

For other errors, not yet handled by perf_flag_probe(), it's pointless to report a second error message for the same error code:
the second check should not print an error if the error is the same as the one return for the first check.

[1] http://lkml.kernel.org/r/[email protected]

Yann Droneaud (2):
perf tools: shortcut PERF_FLAG_FD_CLOEXEC probing in case of EBUSY
error
perf tools: report PERF_FLAG_FD_CLOEXEC probing error once

 tools/perf/util/cloexec.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
Cc Adrian Hunter <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc William Cohen <[email protected]>
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/2 perf tools: shortcut PERF_FLAG_FD_CLOEXEC probing in case of EBUSY error

This patch is a simplification of the logic introduced as part of commit 63914aca8f7e7a75d0ee027af7b1755c69cc1e2c ('perf tools: Show better error message in case we fail to open counters due to EBUSY error'): if EBUSY is reported by perf_event_open(), it will not be possible to probe PERF_FLAG_FD_CLOEXEC, so it's safe to leave early.

Note that EBUSY errors are not really expected here since commit 038fa0b9739d7f375f3f61a2ce4f78ad44329f66 ('perf tools: Fix PERF_FLAG_FD_CLOEXEC flag probing event type open counters due to EBUSY error'): the perf event type used now should be safe to use regarding -EBUSY error.

Cc Adrian Hunter <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc William Cohen <[email protected]>
---
 tools/perf/util/cloexec.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/cloexec.c +6 -2
index 47b78b3f0325..6b948e05e5f0 100644
@@ -42,7 +42,11 @@ static int perf_flag_probe(void)
 		return 1;
 	}
 
-	WARN_ONCE(err != EINVAL && err != EBUSY,
+	/* ignore busy errors */
+	if (err == EBUSY)
+		return -1;
+
+	WARN_ONCE(err != EINVAL,
 		  "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
 		  err, strerror_r(err, sbuf, sizeof(sbuf)));
 
@@ -50,7 +54,7 @@ static int perf_flag_probe(void)
 	fd = sys_perf_event_open(&attr, pid, cpu, -1, 0);
 	err = errno;
 
-	if (WARN_ONCE(fd < 0 && err != EBUSY,
+	if (WARN_ONCE(fd < 0,
 		      "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
 		      err, strerror_r(err, sbuf, sizeof(sbuf))))
 		return -1;
Signature · 1 line
1.9.3
#2 lore raw

PATCH 2/2 perf tools: report PERF_FLAG_FD_CLOEXEC probing error once

In reply to Yann Droneaud

In case of failure, unrelated to PERF_FLAG_FD_CLOEXEC, perf_flag_probe() reports the error twice. For example:

  $ perf record ls
  Error:
  perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error 16 (Device or resource busy)
  perf_event_open(..., 0) failed unexpectedly with error 16 (Device or resource busy)

There's no need for the second error message, so this patch changes the function to only report a second error message when the two calls to perf_even_open(2) fail with different error codes.

---
 tools/perf/util/cloexec.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
tools/perf/util/cloexec.c +14 -12
index 6b948e05e5f0..91331b4ffc3d 100644
@@ -16,7 +16,7 @@ static int perf_flag_probe(void)
 		.exclude_kernel = 1,
 	};
 	int fd;
-	int err;
+	int err0, err1;
 	int cpu;
 	pid_t pid = -1;
 	char sbuf[STRERR_BUFSIZE];
@@ -35,7 +35,7 @@ static int perf_flag_probe(void)
 		}
 		break;
 	}
-	err = errno;
+	err0 = errno;
 
 	if (fd >= 0) {
 		close(fd);
@@ -43,25 +43,27 @@ static int perf_flag_probe(void)
 	}
 
 	/* ignore busy errors */
-	if (err == EBUSY)
+	if (err0 == EBUSY)
 		return -1;
 
-	WARN_ONCE(err != EINVAL,
+	WARN_ONCE(err0 != EINVAL,
 		  "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
-		  err, strerror_r(err, sbuf, sizeof(sbuf)));
+		  err0, strerror_r(err0, sbuf, sizeof(sbuf)));
 
 	/* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
 	fd = sys_perf_event_open(&attr, pid, cpu, -1, 0);
-	err = errno;
+	err1 = errno;
 
-	if (WARN_ONCE(fd < 0,
-		      "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
-		      err, strerror_r(err, sbuf, sizeof(sbuf))))
-		return -1;
+	if (fd >= 0) {
+		close(fd);
+		return 0;
+	}
 
-	close(fd);
+	WARN_ONCE(err0 != err1,
+		  "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
+		  err1, strerror_r(err1, sbuf, sizeof(sbuf)));
 
-	return 0;
+	return -1;
 }
 
 unsigned long perf_event_open_cloexec_flag(void)
Signature · 1 line
1.9.3
Acked-by on the v1 series
#3 lore raw
In reply to Yann Droneaud
Quoted 14 lines · Yann Droneaud

Hi,

Following the EBUSY errors reported by Jiri Olsa [1], I've tryed to improve a bit the way perf_flag_probe() handle errors.

In case EBUSY is returned by perf_event_open(), testing the function again without PERF_FLAG_FD_CLOEXEC is meaningless: EBUSY is not related to close-on-exec flag, so there's nothing to confirm.

For other errors, not yet handled by perf_flag_probe(), it's pointless to report a second error message for the same error code:
the second check should not print an error if the error is the same as the one return for the first check.

Quoted 6 lines

[1] http://lkml.kernel.org/r/[email protected]

Yann Droneaud (2):
perf tools: shortcut PERF_FLAG_FD_CLOEXEC probing in case of EBUSY
error
perf tools: report PERF_FLAG_FD_CLOEXEC probing error once

seems good to me, for the patchset:

thanks,
jirka