Skip to contentSkip to conversation

perf record: We should fork only if a program was specified to run

PATCH In review Arnaldo Carvalho de Melo 2 participants 5 messages Dec 19, 2009 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf record: We should fork only if a program was specified to run

From: Arnaldo Carvalho de Melo <[email protected]>

IOW: Now 'perf record -a' works, this was a bug introduced in:

856e96608a72412d319e498a3a7c557571f811bd "perf record: Properly synchronize child creation"

Also fix the -C usage, i.e. allow for profiling all the tasks in one CPU.

Cc Frédéric Weisbecker <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
---
 tools/perf/builtin-record.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)
tools/perf/builtin-record.c +8 -6
index 63136d0..2654253 100644
@@ -402,7 +402,7 @@ static void atexit_header(void)
 	perf_header__write(&session->header, output, true);
 }
 
-static int __cmd_record(int argc __used, const char **argv)
+static int __cmd_record(int argc, const char **argv)
 {
 	int i, counter;
 	struct stat st;
@@ -411,6 +411,7 @@ static int __cmd_record(int argc __used, const char **argv)
 	int err;
 	unsigned long waking = 0;
 	int child_ready_pipe[2], go_pipe[2];
+	const bool forks = target_pid == -1 && argc > 0;
 	char buf;
 
 	page_size = sysconf(_SC_PAGE_SIZE);
@@ -422,7 +423,7 @@ static int __cmd_record(int argc __used, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGINT, sig_handler);
 
-	if (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0) {
+	if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
 		perror("failed to create pipes");
 		exit(-1);
 	}
@@ -483,7 +484,7 @@ static int __cmd_record(int argc __used, const char **argv)
 
 	atexit(atexit_header);
 
-	if (target_pid == -1) {
+	if (forks) {
 		pid = fork();
 		if (pid < 0) {
 			perror("failed to fork");
@@ -550,7 +551,7 @@ static int __cmd_record(int argc __used, const char **argv)
 			return err;
 	}
 
-	if (!system_wide)
+	if (!system_wide && profile_cpu == -1)
 		event__synthesize_thread(pid, process_synthesized_event,
 					 session);
 	else
@@ -569,7 +570,8 @@ static int __cmd_record(int argc __used, const char **argv)
 	/*
 	 * Let the child rip
 	 */
-	close(go_pipe[1]);
+	if (forks)
+		close(go_pipe[1]);
 
 	for (;;) {
 		int hits = samples;
@@ -667,7 +669,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
 
 	argc = parse_options(argc, argv, options, record_usage,
 			    PARSE_OPT_STOP_AT_NON_OPTION);
-	if (!argc && target_pid == -1 && (!system_wide || profile_cpu == -1))
+	if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1)
 		usage_with_options(record_usage, options);
 
 	symbol__init();
Signature · 1 line
1.6.2.5
maintainer
#1 lore raw

tools/perf/builtin-record.c static int __cmd_record(int argc __used, const char **argv) · line 556

Quoted 8 lines · Arnaldo Carvalho de Melo
tools/perf/builtin-record.c +1 -1
@@ -550,7 +551,7 @@ static int __cmd_record(int argc __used, const char **argv)
                        return err;
        }
 
-       if (!system_wide)
+       if (!system_wide && profile_cpu == -1)
                event__synthesize_thread(pid, process_synthesized_event,
                                         session); 

Not actually sure about this bit though, since we now have per-task-per-cpu things, the profile_cpu is also used to !system_wide.

#2 lore raw

Em Sat, Dec 19, 2009 at 10:39:01PM +0100, Peter Zijlstra escreveu:

Quoted 5 lines · Arnaldo Carvalho de Melo
Quoted 4 lines
tools/perf/builtin-record.c +0 -0
@@ -550,7 +551,7 @@ static int __cmd_record(int argc __used, const char **argv)
                        return err;
        }
 
Quoted 7 lines

tools/perf/builtin-record.c static int __cmd_record(int argc __used, const char **argv) · line 556

Quoted 4 lines
tools/perf/builtin-record.c +1 -1
-       if (!system_wide)
+       if (!system_wide && profile_cpu == -1)
                event__synthesize_thread(pid, process_synthesized_event,
                                         session); 

Not actually sure about this bit though, since we now have per-task-per-cpu things, the profile_cpu is also used to !system_wide.

this actually means: if it is not system wide and not for an specific cpu, it must be for an specific pid.

Not clear, right, you may want to profile an specific pid but only when it is running on an specific CPU... But at least this patch allows for specifiying either an specific CPU or an specific pid or systemwide.

- Arnaldo

maintainer
#3 lore raw
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Sat, Dec 19, 2009 at 10:39:01PM +0100, Peter Zijlstra escreveu:

Quoted 5 lines · Arnaldo Carvalho de Melo
Quoted 4 lines
tools/perf/builtin-record.c +0 -0
@@ -550,7 +551,7 @@ static int __cmd_record(int argc __used, const char **argv)
                        return err;
        }
 
Quoted 7 lines

tools/perf/builtin-record.c static int __cmd_record(int argc __used, const char **argv) · line 556

Quoted 4 lines
tools/perf/builtin-record.c +1 -1
-       if (!system_wide)
+       if (!system_wide && profile_cpu == -1)
                event__synthesize_thread(pid, process_synthesized_event,
                                         session); 

Not actually sure about this bit though, since we now have per-task-per-cpu things, the profile_cpu is also used to !system_wide.

Quoted 2 lines

this actually means: if it is not system wide and not for an specific cpu, it must be for an specific pid.

But it can be for a specific pid and specific cpu.

Quoted 3 lines

Not clear, right, you may want to profile an specific pid but only when it is running on an specific CPU... But at least this patch allows for specifiying either an specific CPU or an specific pid or systemwide.

Ah, right, not enough state to tell what we meant, fair enough.

maintainer
#4 lore raw
In reply to Arnaldo Carvalho de Melo

tools/perf/builtin-record.c static int __cmd_record(int argc __used, const char **argv) · line 429

Quoted 9 lines · Arnaldo Carvalho de Melo
tools/perf/builtin-record.c +1 -1
@@ -422,7 +423,7 @@ static int __cmd_record(int argc __used, const char **argv)
        signal(SIGCHLD, sig_handler);
        signal(SIGINT, sig_handler);
 
-       if (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0) {
+       if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
                perror("failed to create pipes");
                exit(-1);
        } 

you can avoid this by simply putting the whole thing into the if (forks) thing below.