Skip to contentSkip to conversation

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

PATCH In review Andi Kleen 3 participants 3 messages Sep 12, 2012 – Sep 13, 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 v2

From: Andi Kleen <[email protected]>

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

v2: Add more checks, handle non -p again. Handle /proc not there.

---
 tools/perf/builtin-stat.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
tools/perf/builtin-stat.c +9 -1
index 861f0ae..2e5b4b3 100644
@@ -536,7 +536,15 @@ 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];
+		if (target.pid && access("/proc", X_OK) == 0)
+			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
#1 lore raw

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

Quoted 4 lines · Andi Kleen
tools/perf/builtin-stat.c +3 -1
-		while(!done) sleep(1);
+		char piddir[40];
+		if (target.pid && access("/proc", X_OK) == 0)
+			snprintf(piddir, sizeof piddir, "/proc/%d", atoi(target.pid));

else path has piddir not set. Also as Namyhung pointed out target.pid can have multiple pids in it, so this fails if a user specified -p pid1,pid2 -- it will only check if the first process died.

Perhaps a more generic perf_target__is_alive function is needed that can check if the given tid(s) or pid(s) have terminated.

David

#2 lore raw
Quoted 7 lines · David Ahern

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

Quoted 4 lines · Andi Kleen
tools/perf/builtin-stat.c +3 -1
-		while(!done) sleep(1);
+		char piddir[40];
+		if (target.pid && access("/proc", X_OK) == 0)
+			snprintf(piddir, sizeof piddir, "/proc/%d", atoi(target.pid));

else path has piddir not set.

It doesn't need to be set for else?

Quoted 3 lines

target.pid can have multiple pids in it, so this fails if a user specified -p pid1,pid2 -- it will only check if the first process died.

Ok. I suppose can make it simply not check for that case.

-Andi