From d8f66248d6f25f7c935cc5307c43bf394db07272 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Sun, 13 Dec 2009 19:50:24 -0200 Subject: perf session: Pass the perf_session to the event handling operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They will need it to get the right threads list, etc. Signed-off-by: Arnaldo Carvalho de Melo Cc: Frédéric Weisbecker Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Paul Mackerras LKML-Reference: <1260741029-4430-1-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 4decbd14eaed..b7e15a1b1ec2 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -123,7 +123,8 @@ static void write_event(event_t *buf, size_t size) write_output(buf, size); } -static int process_synthesized_event(event_t *event) +static int process_synthesized_event(event_t *event, + struct perf_session *self __used) { write_event(event, event->header.size); return 0; @@ -488,9 +489,10 @@ static int __cmd_record(int argc, const char **argv) } if (!system_wide) - event__synthesize_thread(pid, process_synthesized_event); + event__synthesize_thread(pid, process_synthesized_event, + session); else - event__synthesize_threads(process_synthesized_event); + event__synthesize_threads(process_synthesized_event, session); if (target_pid == -1 && argc) { pid = fork(); @@ -510,7 +512,8 @@ static int __cmd_record(int argc, const char **argv) */ usleep(1000); event__synthesize_thread(pid, - process_synthesized_event); + process_synthesized_event, + session); } child_pid = pid; -- cgit v1.2.1 From 4aa65636411ccb12f006a6ad593930655c445ff6 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Sun, 13 Dec 2009 19:50:29 -0200 Subject: perf session: Move kmaps to perf_session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is still some more work to do to disentangle map creation from DSO loading, but this happens only for the kernel, and for the early adopters of perf diff, where this disentanglement matters most, we'll be testing different kernels, so no problem here. Further clarification: right now we create the kernel maps for the various modules and discontiguous kernel text maps when loading the DSO, we should do it as a two step process, first creating the maps, for multiple mappings with the same DSO store, then doing the dso load just once, for the first hit on one of the maps sharing this DSO backing store. Signed-off-by: Arnaldo Carvalho de Melo Cc: Frédéric Weisbecker Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Paul Mackerras LKML-Reference: <1260741029-4430-6-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index b7e15a1b1ec2..a66a58d52818 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -442,7 +442,7 @@ static int __cmd_record(int argc, const char **argv) exit(-1); } - session = perf_session__new(output_name, O_WRONLY, force); + session = perf_session__new(output_name, O_WRONLY, force, NULL); if (session == NULL) { pr_err("Not enough memory for reading perf file header\n"); return -1; -- cgit v1.2.1 From b38d34645cc52136b6c99623fef7ded26742e573 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 14 Dec 2009 20:09:30 -0200 Subject: perf record: Rename perf.data to perf.data.old if --force/-f is used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Suggested-by: Steven Rostedt Signed-off-by: Arnaldo Carvalho de Melo Cc: Frédéric Weisbecker Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Paul Mackerras LKML-Reference: <1260828571-3613-2-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index a66a58d52818..66979a5553b8 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -421,10 +421,19 @@ static int __cmd_record(int argc, const char **argv) signal(SIGINT, sig_handler); if (!stat(output_name, &st) && st.st_size) { - if (!force && !append_file) { - fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", - output_name); - exit(-1); + if (!force) { + if (!append_file) { + pr_err("Error, output file %s exists, use -A " + "to append or -f to overwrite.\n", + output_name); + exit(-1); + } + } else { + char oldname[PATH_MAX]; + snprintf(oldname, sizeof(oldname), "%s.old", + output_name); + unlink(oldname); + rename(output_name, oldname); } } else { append_file = 0; -- cgit v1.2.1 From 75be6cf48738aec68aac49b428423569492cfba3 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Tue, 15 Dec 2009 20:04:39 -0200 Subject: perf symbols: Make symbol_conf global MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This simplifies a lot of functions, less stuff to be done by tool writers. Signed-off-by: Arnaldo Carvalho de Melo Cc: Frédéric Weisbecker Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Paul Mackerras LKML-Reference: <1260914682-29652-1-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 66979a5553b8..1da48a8fe9cc 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -451,7 +451,7 @@ static int __cmd_record(int argc, const char **argv) exit(-1); } - session = perf_session__new(output_name, O_WRONLY, force, NULL); + session = perf_session__new(output_name, O_WRONLY, force); if (session == NULL) { pr_err("Not enough memory for reading perf file header\n"); return -1; @@ -632,7 +632,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) { int counter; - symbol__init(0); + symbol__init(); argc = parse_options(argc, argv, options, record_usage, PARSE_OPT_STOP_AT_NON_OPTION); -- cgit v1.2.1 From 655000e7c75a559681ee7f15f6fa870c80ae3194 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Tue, 15 Dec 2009 20:04:40 -0200 Subject: perf symbols: Adopt the strlists for dso, comm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Will be used in perf diff too. Signed-off-by: Arnaldo Carvalho de Melo Cc: Frédéric Weisbecker Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Paul Mackerras LKML-Reference: <1260914682-29652-2-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 1da48a8fe9cc..65301c5ca1de 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -632,13 +632,13 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) { int counter; - symbol__init(); - argc = parse_options(argc, argv, options, record_usage, - PARSE_OPT_STOP_AT_NON_OPTION); + PARSE_OPT_STOP_AT_NON_OPTION); if (!argc && target_pid == -1 && !system_wide) usage_with_options(record_usage, options); + symbol__init(); + if (!nr_counters) { nr_counters = 1; attrs[0].type = PERF_TYPE_HARDWARE; -- cgit v1.2.1 From 856e96608a72412d319e498a3a7c557571f811bd Mon Sep 17 00:00:00 2001 From: Peter Zijlstra Date: Wed, 16 Dec 2009 17:55:55 +0100 Subject: perf record: Properly synchronize child creation Remove that ugly usleep and provide proper serialization between parent and child just like perf-stat does. Signed-off-by: Peter Zijlstra Cc: Arnaldo Carvalho de Melo Cc: fweisbec@gmail.com Cc: Paul Mackerras LKML-Reference: <20091216165904.908184135@chello.nl> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 109 +++++++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 38 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 65301c5ca1de..9b7c6d887d5a 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -402,7 +402,7 @@ static void atexit_header(void) perf_header__write(&session->header, output, true); } -static int __cmd_record(int argc, const char **argv) +static int __cmd_record(int argc __used, const char **argv) { int i, counter; struct stat st; @@ -410,6 +410,8 @@ static int __cmd_record(int argc, const char **argv) int flags; int err; unsigned long waking = 0; + int child_ready_pipe[2], go_pipe[2]; + char buf; page_size = sysconf(_SC_PAGE_SIZE); nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); @@ -420,6 +422,11 @@ static int __cmd_record(int argc, const char **argv) signal(SIGCHLD, sig_handler); signal(SIGINT, sig_handler); + if (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0) { + perror("failed to create pipes"); + exit(-1); + } + if (!stat(output_name, &st) && st.st_size) { if (!force) { if (!append_file) { @@ -476,19 +483,65 @@ static int __cmd_record(int argc, const char **argv) atexit(atexit_header); - if (!system_wide) { - pid = target_pid; - if (pid == -1) - pid = getpid(); + if (target_pid == -1) { + pid = fork(); + if (pid < 0) { + perror("failed to fork"); + exit(-1); + } - open_counters(profile_cpu, pid); - } else { - if (profile_cpu != -1) { - open_counters(profile_cpu, target_pid); - } else { - for (i = 0; i < nr_cpus; i++) - open_counters(i, target_pid); + if (!pid) { + close(child_ready_pipe[0]); + close(go_pipe[1]); + fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); + + /* + * Do a dummy execvp to get the PLT entry resolved, + * so we avoid the resolver overhead on the real + * execvp call. + */ + execvp("", (char **)argv); + + /* + * Tell the parent we're ready to go + */ + close(child_ready_pipe[1]); + + /* + * Wait until the parent tells us to go. + */ + if (read(go_pipe[0], &buf, 1) == -1) + perror("unable to read pipe"); + + execvp(argv[0], (char **)argv); + + perror(argv[0]); + exit(-1); } + + child_pid = pid; + + if (!system_wide) + target_pid = pid; + + close(child_ready_pipe[1]); + close(go_pipe[0]); + /* + * wait for child to settle + */ + if (read(child_ready_pipe[0], &buf, 1) == -1) { + perror("unable to read pipe"); + exit(-1); + } + close(child_ready_pipe[0]); + } + + + if (!system_wide || profile_cpu != -1) { + open_counters(profile_cpu, target_pid); + } else { + for (i = 0; i < nr_cpus; i++) + open_counters(i, target_pid); } if (file_new) { @@ -503,31 +556,6 @@ static int __cmd_record(int argc, const char **argv) else event__synthesize_threads(process_synthesized_event, session); - if (target_pid == -1 && argc) { - pid = fork(); - if (pid < 0) - die("failed to fork"); - - if (!pid) { - if (execvp(argv[0], (char **)argv)) { - perror(argv[0]); - exit(-1); - } - } else { - /* - * Wait a bit for the execv'ed child to appear - * and be updated in /proc - * FIXME: Do you know a less heuristical solution? - */ - usleep(1000); - event__synthesize_thread(pid, - process_synthesized_event, - session); - } - - child_pid = pid; - } - if (realtime_prio) { struct sched_param param; @@ -538,6 +566,11 @@ static int __cmd_record(int argc, const char **argv) } } + /* + * Let the child rip + */ + close(go_pipe[1]); + for (;;) { int hits = samples; @@ -634,7 +667,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) + if (!argc && target_pid == -1 && (!system_wide || profile_cpu == -1)) usage_with_options(record_usage, options); symbol__init(); -- cgit v1.2.1 From 60ab271617cec607380099f3ed8e84916e48323b Mon Sep 17 00:00:00 2001 From: Peter Zijlstra Date: Wed, 16 Dec 2009 17:55:56 +0100 Subject: perf record: Use per-task-per-cpu events for inherited events Create events with a pid and cpu contraint for inherited events so that we get a stream per cpu, instead of all cpus contending on a single stream. Signed-off-by: Peter Zijlstra Cc: Arnaldo Carvalho de Melo Cc: fweisbec@gmail.com Cc: Paul Mackerras LKML-Reference: <20091216165904.987643843@chello.nl> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 9b7c6d887d5a..63136d0534d4 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -278,7 +278,7 @@ static void create_counter(int counter, int cpu, pid_t pid) attr->mmap = track; attr->comm = track; - attr->inherit = (cpu < 0) && inherit; + attr->inherit = inherit; attr->disabled = 1; try_again: @@ -537,7 +537,7 @@ static int __cmd_record(int argc __used, const char **argv) } - if (!system_wide || profile_cpu != -1) { + if ((!system_wide && !inherit) || profile_cpu != -1) { open_counters(profile_cpu, target_pid); } else { for (i = 0; i < nr_cpus; i++) -- cgit v1.2.1 From d4db3f164529013b11a171bb1bd38e8681ddaec8 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Sun, 27 Dec 2009 21:36:57 -0200 Subject: perf record: We should fork only if a program was specified to run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Reported-by: Pekka Enberg Signed-off-by: Arnaldo Carvalho de Melo Cc: Frédéric Weisbecker Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Paul Mackerras LKML-Reference: <1261957026-15580-1-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar --- tools/perf/builtin-record.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'tools/perf/builtin-record.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 63136d0534d4..265425322734 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -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(); -- cgit v1.2.1