Skip to contentSkip to conversation

perf, tools: Stop perf stat -p when profiled process exits

PATCH In review Andi Kleen 2 participants 2 messages Sep 12, 2012 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 perf, tools: Stop perf stat -p when profiled process exits

From: Andi Kleen <[email protected]>

When counting a process with perf stat -p check if the process died and exit collection if yes.

---
 tools/perf/builtin-stat.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
tools/perf/builtin-stat.c +8 -1
index 861f0ae..1313bb5 100644
@@ -536,7 +536,14 @@ static int run_perf_stat(int argc __used, const char **argv)
 		if (WIFSIGNALED(status))
 			psignal(WTERMSIG(status), argv[0]);
 	} else {
-		while(!done) sleep(1);
+		char piddir[40];
+		snprintf(piddir, sizeof piddir, "/proc/%d", atoi(target.pid));
+
+		while(!done) {
+			sleep(1);
+			if (target.pid && access(piddir, X_OK) < 0 && errno == ENOENT)
+				break;
+		}
 	}
 
 	t1 = rdclock();
Signature · 1 line
1.7.7.6
maintainer
#1 lore raw

Hi Andi,

Quoted 4 lines · Andi Kleen

From: Andi Kleen <[email protected]>

When counting a process with perf stat -p check if the process died and exit collection if yes.

I guess we need something similar for perf record as well.

Quoted 12 lines
---
 tools/perf/builtin-stat.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
tools/perf/builtin-stat.c +0 -0
index 861f0ae..1313bb5 100644
@@ -536,7 +536,14 @@ static int run_perf_stat(int argc __used, const char **argv)
 		if (WIFSIGNALED(status))

tools/perf/builtin-stat.c static int run_perf_stat(int argc __used, const char **argv) · line 545

Quoted 10 lines
tools/perf/builtin-stat.c +7 -1
 			psignal(WTERMSIG(status), argv[0]);
 	} else {
-		while(!done) sleep(1);
+		char piddir[40];
+		snprintf(piddir, sizeof piddir, "/proc/%d", atoi(target.pid));
+
+		while(!done) {
+			sleep(1);
+			if (target.pid && access(piddir, X_OK) < 0 && errno == ENOENT)
+				break;

The target.pid might have multiple pid's - e.g. perf stat -p 1234,5678

Thanks,
Namhyung