From f3054c778e367d624ca0dfe68b8498b5c463a9d1 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Mon, 23 Apr 2012 09:04:29 -0400 Subject: perf tools: Cleanup realloc use The if branch is completely unnecessary since 'realloc' handles NULL pointers for the first parameter. This is really only a cleanup and submitted mainly to prevent proliferation of bad practices. Signed-off-by: Ulrich Drepper Cc: David Ahern Cc: Ingo Molnar Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Robert Richter Cc: Stephane Eranian Link: http://lkml.kernel.org/r/201204231304.q3ND4TFe020805@drepperk.user.openhosting.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 4c7c2d7..6e618ba 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -31,21 +31,16 @@ static const char **header_argv; int perf_header__push_event(u64 id, const char *name) { + struct perf_trace_event_type *nevents; + if (strlen(name) > MAX_EVENT_NAME) pr_warning("Event %s will be truncated\n", name); - if (!events) { - events = malloc(sizeof(struct perf_trace_event_type)); - if (events == NULL) - return -ENOMEM; - } else { - struct perf_trace_event_type *nevents; + nevents = realloc(events, (event_count + 1) * sizeof(*events)); + if (nevents == NULL) + return -ENOMEM; + events = nevents; - nevents = realloc(events, (event_count + 1) * sizeof(*events)); - if (nevents == NULL) - return -ENOMEM; - events = nevents; - } memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); events[event_count].event_id = id; strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); -- cgit v0.10.2 From 23080e4cd224013c06444e7850fe0bcb55c129ff Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Thu, 26 Apr 2012 16:22:09 -0300 Subject: perf test: Make the rdpmc test honour 'verbose' mode It was unconditionally printing debug stuff when in non -v mode we should just print the name and result of the test. Now: [root@sandy ~]# perf test rdpmc 6: x86 rdpmc test: Ok [root@sandy ~]# perf test -v rdpmc 6: x86 rdpmc test: --- start --- 0: 6030 1: 60030 2: 600050 3: 6000056 4: 60000070 5: 600000266 ---- end ---- x86 rdpmc test: Ok [root@sandy ~]# Cc: David Ahern Cc: Frederic Weisbecker Cc: Mike Galbraith Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lkml.kernel.org/n/tip-0tjedaozsy9oarq30nvzg74b@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-test.c b/tools/perf/builtin-test.c index 1c5b980..5502a4a 100644 --- a/tools/perf/builtin-test.c +++ b/tools/perf/builtin-test.c @@ -1549,8 +1549,6 @@ static int __test__rdpmc(void) sa.sa_sigaction = segfault_handler; sigaction(SIGSEGV, &sa, NULL); - fprintf(stderr, "\n\n"); - fd = sys_perf_event_open(&attr, 0, -1, -1, 0); if (fd < 0) { die("Error: sys_perf_event_open() syscall returned " @@ -1575,7 +1573,7 @@ static int __test__rdpmc(void) loops *= 10; delta = now - stamp; - fprintf(stderr, "%14d: %14Lu\n", n, (long long)delta); + pr_debug("%14d: %14Lu\n", n, (long long)delta); delta_sum += delta; } @@ -1583,7 +1581,7 @@ static int __test__rdpmc(void) munmap(addr, page_size); close(fd); - fprintf(stderr, " "); + pr_debug(" "); if (!delta_sum) return -1; -- cgit v0.10.2 From c651214e90e5c150015f7524a6bfc298ad61435f Mon Sep 17 00:00:00 2001 From: Robert Richter Date: Thu, 5 Apr 2012 18:26:25 +0200 Subject: perf tools: Fix include header files in util/parse-events.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Include header fixes for ... bool: util/parse-events.h:31: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘have_tracepoints’ ... and types.h: util/parse-events.h:28: error: expected ‘)’ before ‘config’ util/parse-events.h:34: error: expected declaration specifiers or ‘...’ before ‘u64’ util/parse-events.h:45: error: expected ‘)’ before ‘type’ This happens if now other include files are included before util/parse-events.h. Signed-off-by: Robert Richter Cc: Ingo Molnar Link: http://lkml.kernel.org/r/1333643188-26895-2-git-send-email-robert.richter@amd.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index ca069f8..5cb0028 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -4,7 +4,9 @@ * Parse symbolic events/counts passed in as options: */ +#include #include "../../../include/linux/perf_event.h" +#include "types.h" struct list_head; struct perf_evsel; -- cgit v0.10.2 From bea0340582dc47b447a014f5bf9f460925afdaf4 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:15 +0900 Subject: perf tools: Introduce struct perf_target The perf_target struct will be used for taking care of cpu/thread maps based on user's input. Since it is used on various subcommands it'd better factoring it out. Thanks to Arnaldo for suggesting the better name. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-2-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 10b1f1f..4dcf270 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -44,7 +44,6 @@ struct perf_record { struct perf_evlist *evlist; struct perf_session *session; const char *progname; - const char *uid_str; int output; unsigned int page_size; int realtime_prio; @@ -218,7 +217,7 @@ try_again: if (err == EPERM || err == EACCES) { ui__error_paranoid(); exit(EXIT_FAILURE); - } else if (err == ENODEV && opts->cpu_list) { + } else if (err == ENODEV && opts->target.cpu_list) { die("No such device - did you specify" " an out-of-range profile CPU?\n"); } else if (err == EINVAL) { @@ -578,7 +577,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv) perf_session__process_machines(session, tool, perf_event__synthesize_guest_os); - if (!opts->system_wide) + if (!opts->target.system_wide) perf_event__synthesize_thread_map(tool, evsel_list->threads, process_synthesized_event, machine); @@ -765,9 +764,9 @@ const struct option record_options[] = { parse_events_option), OPT_CALLBACK(0, "filter", &record.evlist, "filter", "event filter", parse_filter), - OPT_STRING('p', "pid", &record.opts.target_pid, "pid", + OPT_STRING('p', "pid", &record.opts.target.pid, "pid", "record events on existing process id"), - OPT_STRING('t', "tid", &record.opts.target_tid, "tid", + OPT_STRING('t', "tid", &record.opts.target.tid, "tid", "record events on existing thread id"), OPT_INTEGER('r', "realtime", &record.realtime_prio, "collect data with this RT SCHED_FIFO priority"), @@ -775,11 +774,11 @@ const struct option record_options[] = { "collect data without buffering"), OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, "collect raw sample records from all opened counters"), - OPT_BOOLEAN('a', "all-cpus", &record.opts.system_wide, + OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, "system-wide collection from all CPUs"), OPT_BOOLEAN('A', "append", &record.append_file, "append to the output file to do incremental profiling"), - OPT_STRING('C', "cpu", &record.opts.cpu_list, "cpu", + OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", "list of cpus to monitor"), OPT_BOOLEAN('f', "force", &record.force, "overwrite existing data file (deprecated)"), @@ -813,7 +812,8 @@ const struct option record_options[] = { OPT_CALLBACK('G', "cgroup", &record.evlist, "name", "monitor event in cgroup name only", parse_cgroups), - OPT_STRING('u', "uid", &record.uid_str, "user", "user to profile"), + OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", + "user to profile"), OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, "branch any", "sample any taken branches", @@ -842,8 +842,9 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) argc = parse_options(argc, argv, record_options, record_usage, PARSE_OPT_STOP_AT_NON_OPTION); - if (!argc && !rec->opts.target_pid && !rec->opts.target_tid && - !rec->opts.system_wide && !rec->opts.cpu_list && !rec->uid_str) + if (!argc && !rec->opts.target.pid && !rec->opts.target.tid && + !rec->opts.target.system_wide && !rec->opts.target.cpu_list && + !rec->opts.target.uid_str) usage_with_options(record_usage, record_options); if (rec->force && rec->append_file) { @@ -856,7 +857,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) rec->write_mode = WRITE_FORCE; } - if (nr_cgroups && !rec->opts.system_wide) { + if (nr_cgroups && !rec->opts.target.system_wide) { fprintf(stderr, "cgroup monitoring only available in" " system-wide mode\n"); usage_with_options(record_usage, record_options); @@ -883,17 +884,19 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) goto out_symbol_exit; } - rec->opts.uid = parse_target_uid(rec->uid_str, rec->opts.target_tid, - rec->opts.target_pid); - if (rec->uid_str != NULL && rec->opts.uid == UINT_MAX - 1) + rec->opts.target.uid = parse_target_uid(rec->opts.target.uid_str, + rec->opts.target.tid, + rec->opts.target.pid); + if (rec->opts.target.uid_str != NULL && + rec->opts.target.uid == UINT_MAX - 1) goto out_free_fd; - if (rec->opts.target_pid) - rec->opts.target_tid = rec->opts.target_pid; + if (rec->opts.target.pid) + rec->opts.target.tid = rec->opts.target.pid; - if (perf_evlist__create_maps(evsel_list, rec->opts.target_pid, - rec->opts.target_tid, rec->opts.uid, - rec->opts.cpu_list) < 0) + if (perf_evlist__create_maps(evsel_list, rec->opts.target.pid, + rec->opts.target.tid, rec->opts.target.uid, + rec->opts.target.cpu_list) < 0) usage_with_options(record_usage, record_options); list_for_each_entry(pos, &evsel_list->entries, node) { diff --git a/tools/perf/builtin-test.c b/tools/perf/builtin-test.c index 5502a4a..27882d8 100644 --- a/tools/perf/builtin-test.c +++ b/tools/perf/builtin-test.c @@ -1207,8 +1207,9 @@ static int test__PERF_RECORD(void) * perf_evlist__prepare_workload we'll fill in the only thread * we're monitoring, the one forked there. */ - err = perf_evlist__create_maps(evlist, opts.target_pid, - opts.target_tid, UINT_MAX, opts.cpu_list); + err = perf_evlist__create_maps(evlist, opts.target.pid, + opts.target.tid, UINT_MAX, + opts.target.cpu_list); if (err < 0) { pr_debug("Not enough memory to create thread/cpu maps\n"); goto out_delete_evlist; diff --git a/tools/perf/perf.h b/tools/perf/perf.h index 89e3355..7e226c0 100644 --- a/tools/perf/perf.h +++ b/tools/perf/perf.h @@ -207,10 +207,17 @@ extern const char perf_version_string[]; void pthread__unblock_sigwinch(void); -struct perf_record_opts { - const char *target_pid; - const char *target_tid; +struct perf_target { + const char *pid; + const char *tid; + const char *cpu_list; + const char *uid_str; uid_t uid; + bool system_wide; +}; + +struct perf_record_opts { + struct perf_target target; bool call_graph; bool group; bool inherit_stat; @@ -223,7 +230,6 @@ struct perf_record_opts { bool sample_time; bool sample_id_all_missing; bool exclude_guest_missing; - bool system_wide; bool period; unsigned int freq; unsigned int mmap_pages; @@ -231,7 +237,6 @@ struct perf_record_opts { int branch_stack; u64 default_interval; u64 user_interval; - const char *cpu_list; }; #endif diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 1986d80..7080901 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -827,7 +827,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, exit(-1); } - if (!opts->system_wide && !opts->target_tid && !opts->target_pid) + if (!opts->target.system_wide && !opts->target.tid && !opts->target.pid) evlist->threads->map[0] = evlist->workload.pid; close(child_ready_pipe[1]); diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 8c13dbc..d90598e 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -106,15 +106,15 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, if (opts->call_graph) attr->sample_type |= PERF_SAMPLE_CALLCHAIN; - if (opts->system_wide) + if (opts->target.system_wide) attr->sample_type |= PERF_SAMPLE_CPU; if (opts->period) attr->sample_type |= PERF_SAMPLE_PERIOD; if (!opts->sample_id_all_missing && - (opts->sample_time || opts->system_wide || - !opts->no_inherit || opts->cpu_list)) + (opts->sample_time || opts->target.system_wide || + !opts->no_inherit || opts->target.cpu_list)) attr->sample_type |= PERF_SAMPLE_TIME; if (opts->raw_samples) { @@ -135,8 +135,8 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, attr->mmap = track; attr->comm = track; - if (!opts->target_pid && !opts->target_tid && !opts->system_wide && - (!opts->group || evsel == first)) { + if (!opts->target.pid && !opts->target.tid && + !opts->target.system_wide && (!opts->group || evsel == first)) { attr->disabled = 1; attr->enable_on_exec = 1; } -- cgit v0.10.2 From 20f946b4a49dfd89c1c4ddeb55c0632893332674 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:16 +0900 Subject: perf stat: Convert to struct perf_target Use struct perf_target as it is introduced by previous patch. This is a preparation of further changes. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-3-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index dde9e17..1ca767d 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -175,22 +175,19 @@ static struct perf_event_attr very_very_detailed_attrs[] = { static struct perf_evlist *evsel_list; -static bool system_wide = false; -static int run_idx = 0; +static struct perf_target target; +static int run_idx = 0; static int run_count = 1; static bool no_inherit = false; static bool scale = true; static bool no_aggr = false; -static const char *target_pid; -static const char *target_tid; static pid_t child_pid = -1; static bool null_run = false; static int detailed_run = 0; static bool sync_run = false; static bool big_num = true; static int big_num_opt = -1; -static const char *cpu_list; static const char *csv_sep = NULL; static bool csv_output = false; static bool group = false; @@ -293,10 +290,10 @@ static int create_perf_stat_counter(struct perf_evsel *evsel, attr->inherit = !no_inherit; - if (system_wide) + if (target.system_wide) return perf_evsel__open_per_cpu(evsel, evsel_list->cpus, group, group_fd); - if (!target_pid && !target_tid && (!group || evsel == first)) { + if (!target.pid && !target.tid && (!group || evsel == first)) { attr->disabled = 1; attr->enable_on_exec = 1; } @@ -446,7 +443,7 @@ static int run_perf_stat(int argc __used, const char **argv) exit(-1); } - if (!target_tid && !target_pid && !system_wide) + if (!target.tid && !target.pid && !target.system_wide) evsel_list->threads->map[0] = child_pid; /* @@ -476,7 +473,7 @@ static int run_perf_stat(int argc __used, const char **argv) error("You may not have permission to collect %sstats.\n" "\t Consider tweaking" " /proc/sys/kernel/perf_event_paranoid or running as root.", - system_wide ? "system-wide " : ""); + target.system_wide ? "system-wide " : ""); } else { error("open_counter returned with %d (%s). " "/bin/dmesg may provide additional information.\n", @@ -968,14 +965,14 @@ static void print_stat(int argc, const char **argv) if (!csv_output) { fprintf(output, "\n"); fprintf(output, " Performance counter stats for "); - if (!target_pid && !target_tid) { + if (!target.pid && !target.tid) { fprintf(output, "\'%s", argv[0]); for (i = 1; i < argc; i++) fprintf(output, " %s", argv[i]); - } else if (target_pid) - fprintf(output, "process id \'%s", target_pid); + } else if (target.pid) + fprintf(output, "process id \'%s", target.pid); else - fprintf(output, "thread id \'%s", target_tid); + fprintf(output, "thread id \'%s", target.tid); fprintf(output, "\'"); if (run_count > 1) @@ -1049,11 +1046,11 @@ static const struct option options[] = { "event filter", parse_filter), OPT_BOOLEAN('i', "no-inherit", &no_inherit, "child tasks do not inherit counters"), - OPT_STRING('p', "pid", &target_pid, "pid", + OPT_STRING('p', "pid", &target.pid, "pid", "stat events on existing process id"), - OPT_STRING('t', "tid", &target_tid, "tid", + OPT_STRING('t', "tid", &target.tid, "tid", "stat events on existing thread id"), - OPT_BOOLEAN('a', "all-cpus", &system_wide, + OPT_BOOLEAN('a', "all-cpus", &target.system_wide, "system-wide collection from all CPUs"), OPT_BOOLEAN('g', "group", &group, "put the counters into a counter group"), @@ -1072,7 +1069,7 @@ static const struct option options[] = { OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, "print large numbers with thousands\' separators", stat__set_big_num), - OPT_STRING('C', "cpu", &cpu_list, "cpu", + OPT_STRING('C', "cpu", &target.cpu_list, "cpu", "list of cpus to monitor in system-wide"), OPT_BOOLEAN('A', "no-aggr", &no_aggr, "disable CPU count aggregation"), @@ -1190,13 +1187,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) } else if (big_num_opt == 0) /* User passed --no-big-num */ big_num = false; - if (!argc && !target_pid && !target_tid) + if (!argc && !target.pid && !target.tid) usage_with_options(stat_usage, options); if (run_count <= 0) usage_with_options(stat_usage, options); /* no_aggr, cgroup are for system-wide only */ - if ((no_aggr || nr_cgroups) && !system_wide) { + if ((no_aggr || nr_cgroups) && !target.system_wide) { fprintf(stderr, "both cgroup and no-aggregation " "modes only available in system-wide mode\n"); @@ -1206,18 +1203,18 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) if (add_default_attributes()) goto out; - if (target_pid) - target_tid = target_pid; + if (target.pid) + target.tid = target.pid; - evsel_list->threads = thread_map__new_str(target_pid, - target_tid, UINT_MAX); + evsel_list->threads = thread_map__new_str(target.pid, + target.tid, UINT_MAX); if (evsel_list->threads == NULL) { pr_err("Problems finding threads of monitor\n"); usage_with_options(stat_usage, options); } - if (system_wide) - evsel_list->cpus = cpu_map__new(cpu_list); + if (target.system_wide) + evsel_list->cpus = cpu_map__new(target.cpu_list); else evsel_list->cpus = cpu_map__dummy_new(); -- cgit v0.10.2 From fe9d18a71d2018f8021fd2bd2aaf5137954ef839 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:17 +0900 Subject: perf top: Convert to struct perf_target Use struct perf_target as it is introduced by previous patch. This is a preparation of further changes. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-4-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 8ef59f8..2c1c207 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -588,7 +588,7 @@ static void *display_thread_tui(void *arg) * via --uid. */ list_for_each_entry(pos, &top->evlist->entries, node) - pos->hists.uid_filter_str = top->uid_str; + pos->hists.uid_filter_str = top->target.uid_str; perf_evlist__tui_browse_hists(top->evlist, help, perf_top__sort_new_samples, @@ -1016,7 +1016,7 @@ static int __cmd_top(struct perf_top *top) if (ret) goto out_delete; - if (top->target_tid || top->uid != UINT_MAX) + if (top->target.tid || top->target.uid != UINT_MAX) perf_event__synthesize_thread_map(&top->tool, top->evlist->threads, perf_event__process, &top->session->host_machine); @@ -1154,7 +1154,6 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) struct perf_top top = { .count_filter = 5, .delay_secs = 2, - .uid = UINT_MAX, .freq = 1000, /* 1 KHz */ .mmap_pages = 128, .sym_pcnt_filter = 5, @@ -1166,13 +1165,13 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) parse_events_option), OPT_INTEGER('c', "count", &top.default_interval, "event period to sample"), - OPT_STRING('p', "pid", &top.target_pid, "pid", + OPT_STRING('p', "pid", &top.target.pid, "pid", "profile events on existing process id"), - OPT_STRING('t', "tid", &top.target_tid, "tid", + OPT_STRING('t', "tid", &top.target.tid, "tid", "profile events on existing thread id"), - OPT_BOOLEAN('a', "all-cpus", &top.system_wide, + OPT_BOOLEAN('a', "all-cpus", &top.target.system_wide, "system-wide collection from all CPUs"), - OPT_STRING('C', "cpu", &top.cpu_list, "cpu", + OPT_STRING('C', "cpu", &top.target.cpu_list, "cpu", "list of cpus to monitor"), OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, "file", "vmlinux pathname"), @@ -1227,7 +1226,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) "Display raw encoding of assembly instructions (default)"), OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", "Specify disassembler style (e.g. -M intel for intel syntax)"), - OPT_STRING('u', "uid", &top.uid_str, "user", "user to profile"), + OPT_STRING('u', "uid", &top.target.uid_str, "user", "user to profile"), OPT_END() }; @@ -1253,22 +1252,24 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) setup_browser(false); - top.uid = parse_target_uid(top.uid_str, top.target_tid, top.target_pid); - if (top.uid_str != NULL && top.uid == UINT_MAX - 1) + top.target.uid = parse_target_uid(top.target.uid_str, top.target.tid, + top.target.pid); + if (top.target.uid_str != NULL && top.target.uid == UINT_MAX - 1) goto out_delete_evlist; /* CPU and PID are mutually exclusive */ - if (top.target_tid && top.cpu_list) { + if (top.target.tid && top.target.cpu_list) { printf("WARNING: PID switch overriding CPU\n"); sleep(1); - top.cpu_list = NULL; + top.target.cpu_list = NULL; } - if (top.target_pid) - top.target_tid = top.target_pid; + if (top.target.pid) + top.target.tid = top.target.pid; - if (perf_evlist__create_maps(top.evlist, top.target_pid, - top.target_tid, top.uid, top.cpu_list) < 0) + if (perf_evlist__create_maps(top.evlist, top.target.pid, + top.target.tid, top.target.uid, + top.target.cpu_list) < 0) usage_with_options(top_usage, options); if (!top.evlist->nr_entries && diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c index 09fe579..abe0e8e 100644 --- a/tools/perf/util/top.c +++ b/tools/perf/util/top.c @@ -69,23 +69,24 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) ret += SNPRINTF(bf + ret, size - ret, "], "); - if (top->target_pid) + if (top->target.pid) ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s", - top->target_pid); - else if (top->target_tid) + top->target.pid); + else if (top->target.tid) ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s", - top->target_tid); - else if (top->uid_str != NULL) + top->target.tid); + else if (top->target.uid_str != NULL) ret += SNPRINTF(bf + ret, size - ret, " (uid: %s", - top->uid_str); + top->target.uid_str); else ret += SNPRINTF(bf + ret, size - ret, " (all"); - if (top->cpu_list) + if (top->target.cpu_list) ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)", - top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list); + top->evlist->cpus->nr > 1 ? "s" : "", + top->target.cpu_list); else { - if (top->target_tid) + if (top->target.tid) ret += SNPRINTF(bf + ret, size - ret, ")"); else ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)", diff --git a/tools/perf/util/top.h b/tools/perf/util/top.h index ce61cb2..33347ca 100644 --- a/tools/perf/util/top.h +++ b/tools/perf/util/top.h @@ -13,6 +13,7 @@ struct perf_session; struct perf_top { struct perf_tool tool; struct perf_evlist *evlist; + struct perf_target target; /* * Symbols will be added here in perf_event__process_sample and will * get out after decayed. @@ -23,10 +24,7 @@ struct perf_top { u64 guest_us_samples, guest_kernel_samples; int print_entries, count_filter, delay_secs; int freq; - const char *target_pid, *target_tid; - uid_t uid; bool hide_kernel_symbols, hide_user_symbols, zero; - bool system_wide; bool use_tui, use_stdio; bool sort_has_symbols; bool dont_use_callchains; @@ -37,7 +35,6 @@ struct perf_top { bool sample_id_all_missing; bool exclude_guest_missing; bool dump_symtab; - const char *cpu_list; struct hist_entry *sym_filter_entry; struct perf_evsel *sym_evsel; struct perf_session *session; @@ -47,7 +44,6 @@ struct perf_top { int realtime_prio; int sym_pcnt_filter; const char *sym_filter; - const char *uid_str; }; size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size); -- cgit v0.10.2 From 4bd0f2d2c0cf14de9c84c2fe689120c6b0f667c8 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:18 +0900 Subject: perf tools: Introduce perf_target__validate() helper The perf_target__validate function is used to check given PID/TID/UID/CPU target options and warn if some combination is impossible. Also this can make some arguments of parse_target_uid() function useless as it is checked before the call via our new helper. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-5-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 4dcf270..3596cca 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -884,16 +884,13 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) goto out_symbol_exit; } - rec->opts.target.uid = parse_target_uid(rec->opts.target.uid_str, - rec->opts.target.tid, - rec->opts.target.pid); + perf_target__validate(&rec->opts.target); + + rec->opts.target.uid = parse_target_uid(rec->opts.target.uid_str); if (rec->opts.target.uid_str != NULL && rec->opts.target.uid == UINT_MAX - 1) goto out_free_fd; - if (rec->opts.target.pid) - rec->opts.target.tid = rec->opts.target.pid; - if (perf_evlist__create_maps(evsel_list, rec->opts.target.pid, rec->opts.target.tid, rec->opts.target.uid, rec->opts.target.cpu_list) < 0) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 1ca767d..bb77232 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1203,8 +1203,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) if (add_default_attributes()) goto out; - if (target.pid) - target.tid = target.pid; + perf_target__validate(&target); evsel_list->threads = thread_map__new_str(target.pid, target.tid, UINT_MAX); diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 2c1c207..4f47952 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1252,21 +1252,12 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) setup_browser(false); - top.target.uid = parse_target_uid(top.target.uid_str, top.target.tid, - top.target.pid); + perf_target__validate(&top.target); + + top.target.uid = parse_target_uid(top.target.uid_str); if (top.target.uid_str != NULL && top.target.uid == UINT_MAX - 1) goto out_delete_evlist; - /* CPU and PID are mutually exclusive */ - if (top.target.tid && top.target.cpu_list) { - printf("WARNING: PID switch overriding CPU\n"); - sleep(1); - top.target.cpu_list = NULL; - } - - if (top.target.pid) - top.target.tid = top.target.pid; - if (perf_evlist__create_maps(top.evlist, top.target.pid, top.target.tid, top.target.uid, top.target.cpu_list) < 0) diff --git a/tools/perf/util/usage.c b/tools/perf/util/usage.c index 52bb07c..0a1a885 100644 --- a/tools/perf/util/usage.c +++ b/tools/perf/util/usage.c @@ -83,7 +83,7 @@ void warning(const char *warn, ...) va_end(params); } -uid_t parse_target_uid(const char *str, const char *tid, const char *pid) +uid_t parse_target_uid(const char *str) { struct passwd pwd, *result; char buf[1024]; @@ -91,13 +91,6 @@ uid_t parse_target_uid(const char *str, const char *tid, const char *pid) if (str == NULL) return UINT_MAX; - /* UID and PID are mutually exclusive */ - if (tid || pid) { - ui__warning("PID/TID switch overriding UID\n"); - sleep(1); - return UINT_MAX; - } - getpwnam_r(str, &pwd, buf, sizeof(buf), &result); if (result == NULL) { @@ -120,3 +113,23 @@ uid_t parse_target_uid(const char *str, const char *tid, const char *pid) return result->pw_uid; } + +void perf_target__validate(struct perf_target *target) +{ + if (target->pid) + target->tid = target->pid; + + /* CPU and PID are mutually exclusive */ + if (target->tid && target->cpu_list) { + ui__warning("WARNING: PID switch overriding CPU\n"); + sleep(1); + target->cpu_list = NULL; + } + + /* UID and PID are mutually exclusive */ + if (target->tid && target->uid_str) { + ui__warning("PID/TID switch overriding UID\n"); + sleep(1); + target->uid_str = NULL; + } +} diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 0f99f39..3f05d62 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -246,10 +246,12 @@ unsigned long convert_unit(unsigned long value, char *unit); int readn(int fd, void *buf, size_t size); struct perf_event_attr; +struct perf_target; void event_attr_init(struct perf_event_attr *attr); -uid_t parse_target_uid(const char *str, const char *tid, const char *pid); +uid_t parse_target_uid(const char *str); +void perf_target__validate(struct perf_target *target); #define _STR(x) #x #define STR(x) _STR(x) -- cgit v0.10.2 From b809ac100e2f12ebf1b58ff522dba15651a77d27 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:19 +0900 Subject: perf evlist: Make create_maps() take struct perf_target Now we have all information that needed to create cpu/thread maps in struct perf_target, it'd be better using it as an argument. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-6-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 3596cca..d165909 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -891,9 +891,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) rec->opts.target.uid == UINT_MAX - 1) goto out_free_fd; - if (perf_evlist__create_maps(evsel_list, rec->opts.target.pid, - rec->opts.target.tid, rec->opts.target.uid, - rec->opts.target.cpu_list) < 0) + if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0) usage_with_options(record_usage, record_options); list_for_each_entry(pos, &evsel_list->entries, node) { diff --git a/tools/perf/builtin-test.c b/tools/perf/builtin-test.c index 27882d8..9d9abbb 100644 --- a/tools/perf/builtin-test.c +++ b/tools/perf/builtin-test.c @@ -1165,6 +1165,9 @@ realloc: static int test__PERF_RECORD(void) { struct perf_record_opts opts = { + .target = { + .uid = UINT_MAX, + }, .no_delay = true, .freq = 10, .mmap_pages = 256, @@ -1207,9 +1210,7 @@ static int test__PERF_RECORD(void) * perf_evlist__prepare_workload we'll fill in the only thread * we're monitoring, the one forked there. */ - err = perf_evlist__create_maps(evlist, opts.target.pid, - opts.target.tid, UINT_MAX, - opts.target.cpu_list); + err = perf_evlist__create_maps(evlist, &opts.target); if (err < 0) { pr_debug("Not enough memory to create thread/cpu maps\n"); goto out_delete_evlist; diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 4f47952..2a0ec09 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1258,9 +1258,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) if (top.target.uid_str != NULL && top.target.uid == UINT_MAX - 1) goto out_delete_evlist; - if (perf_evlist__create_maps(top.evlist, top.target.pid, - top.target.tid, top.target.uid, - top.target.cpu_list) < 0) + if (perf_evlist__create_maps(top.evlist, &top.target) < 0) usage_with_options(top_usage, options); if (!top.evlist->nr_entries && diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 7080901..a43e2c5 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -599,18 +599,20 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, return perf_evlist__mmap_per_cpu(evlist, prot, mask); } -int perf_evlist__create_maps(struct perf_evlist *evlist, const char *target_pid, - const char *target_tid, uid_t uid, const char *cpu_list) +int perf_evlist__create_maps(struct perf_evlist *evlist, + struct perf_target *target) { - evlist->threads = thread_map__new_str(target_pid, target_tid, uid); + evlist->threads = thread_map__new_str(target->pid, target->tid, + target->uid); if (evlist->threads == NULL) return -1; - if (uid != UINT_MAX || (cpu_list == NULL && target_tid)) + if (target->uid != UINT_MAX || + (target->cpu_list == NULL && target->tid)) evlist->cpus = cpu_map__dummy_new(); else - evlist->cpus = cpu_map__new(cpu_list); + evlist->cpus = cpu_map__new(target->cpu_list); if (evlist->cpus == NULL) goto out_delete_threads; diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index 21f1c9e..58abb63 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -106,8 +106,8 @@ static inline void perf_evlist__set_maps(struct perf_evlist *evlist, evlist->threads = threads; } -int perf_evlist__create_maps(struct perf_evlist *evlist, const char *target_pid, - const char *tid, uid_t uid, const char *cpu_list); +int perf_evlist__create_maps(struct perf_evlist *evlist, + struct perf_target *target); void perf_evlist__delete_maps(struct perf_evlist *evlist); int perf_evlist__set_filters(struct perf_evlist *evlist); -- cgit v0.10.2 From 770a34a38b74982724dbb099225944b415f90281 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:20 +0900 Subject: perf tools: Check more combinations of PID/TID, UID and CPU switches There were some combinations of these switches that are not so appropriate IMHO. Since there are implicit priorities between them and they worked well anyway, but it ends up opening useless duplicated events. For example, 'perf stat -t -a' will open multiple events for the thread instead of one. Add explicit checks and warn user in perf_target__validate(). Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-7-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/util/usage.c b/tools/perf/util/usage.c index 0a1a885..228f0a5 100644 --- a/tools/perf/util/usage.c +++ b/tools/perf/util/usage.c @@ -132,4 +132,18 @@ void perf_target__validate(struct perf_target *target) sleep(1); target->uid_str = NULL; } + + /* UID and CPU are mutually exclusive */ + if (target->uid_str && target->cpu_list) { + ui__warning("UID switch overriding CPU\n"); + sleep(1); + target->cpu_list = NULL; + } + + /* PID/UID and SYSTEM are mutually exclusive */ + if ((target->tid || target->uid_str) && target->system_wide) { + ui__warning("PID/TID/UID switch overriding CPU\n"); + sleep(1); + target->system_wide = false; + } } -- cgit v0.10.2 From 12864b31583bcbd26789ebe68c612688f9ee2e30 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Thu, 26 Apr 2012 14:15:22 +0900 Subject: perf target: Split out perf_target handling code For further work on perf_target, it'd be better off splitting the code into a separate file. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335417327-11796-9-git-send-email-namhyung.kim@lge.com [ committer note: Fixed perl build by using stdbool and types.h in target.h ] Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/Makefile b/tools/perf/Makefile index e98e14c..4122a66 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile @@ -300,6 +300,7 @@ LIB_H += util/cpumap.h LIB_H += util/top.h LIB_H += $(ARCH_INCLUDE) LIB_H += util/cgroup.h +LIB_H += util/target.h LIB_OBJS += $(OUTPUT)util/abspath.o LIB_OBJS += $(OUTPUT)util/alias.o @@ -361,6 +362,7 @@ LIB_OBJS += $(OUTPUT)util/util.o LIB_OBJS += $(OUTPUT)util/xyarray.o LIB_OBJS += $(OUTPUT)util/cpumap.o LIB_OBJS += $(OUTPUT)util/cgroup.o +LIB_OBJS += $(OUTPUT)util/target.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o diff --git a/tools/perf/perf.h b/tools/perf/perf.h index 7e226c0..14f1034 100644 --- a/tools/perf/perf.h +++ b/tools/perf/perf.h @@ -207,14 +207,7 @@ extern const char perf_version_string[]; void pthread__unblock_sigwinch(void); -struct perf_target { - const char *pid; - const char *tid; - const char *cpu_list; - const char *uid_str; - uid_t uid; - bool system_wide; -}; +#include "util/target.h" struct perf_record_opts { struct perf_target target; diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index a43e2c5..3032862 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -11,6 +11,7 @@ #include #include "cpumap.h" #include "thread_map.h" +#include "target.h" #include "evlist.h" #include "evsel.h" #include diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index d90598e..bb785a0 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -14,6 +14,7 @@ #include "util.h" #include "cpumap.h" #include "thread_map.h" +#include "target.h" #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0)) diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c new file mode 100644 index 0000000..3fadf85 --- /dev/null +++ b/tools/perf/util/target.c @@ -0,0 +1,45 @@ +/* + * Helper functions for handling target threads/cpus + * + * Copyright (C) 2012, LG Electronics, Namhyung Kim + * + * Released under the GPL v2. + */ + +#include "target.h" +#include "debug.h" + + +void perf_target__validate(struct perf_target *target) +{ + if (target->pid) + target->tid = target->pid; + + /* CPU and PID are mutually exclusive */ + if (target->tid && target->cpu_list) { + ui__warning("WARNING: PID switch overriding CPU\n"); + sleep(1); + target->cpu_list = NULL; + } + + /* UID and PID are mutually exclusive */ + if (target->tid && target->uid_str) { + ui__warning("PID/TID switch overriding UID\n"); + sleep(1); + target->uid_str = NULL; + } + + /* UID and CPU are mutually exclusive */ + if (target->uid_str && target->cpu_list) { + ui__warning("UID switch overriding CPU\n"); + sleep(1); + target->cpu_list = NULL; + } + + /* PID/UID and SYSTEM are mutually exclusive */ + if ((target->tid || target->uid_str) && target->system_wide) { + ui__warning("PID/TID/UID switch overriding CPU\n"); + sleep(1); + target->system_wide = false; + } +} diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h new file mode 100644 index 0000000..218291f --- /dev/null +++ b/tools/perf/util/target.h @@ -0,0 +1,18 @@ +#ifndef _PERF_TARGET_H +#define _PERF_TARGET_H + +#include +#include + +struct perf_target { + const char *pid; + const char *tid; + const char *cpu_list; + const char *uid_str; + uid_t uid; + bool system_wide; +}; + +void perf_target__validate(struct perf_target *target); + +#endif /* _PERF_TARGET_H */ diff --git a/tools/perf/util/usage.c b/tools/perf/util/usage.c index 228f0a5..e851abc 100644 --- a/tools/perf/util/usage.c +++ b/tools/perf/util/usage.c @@ -113,37 +113,3 @@ uid_t parse_target_uid(const char *str) return result->pw_uid; } - -void perf_target__validate(struct perf_target *target) -{ - if (target->pid) - target->tid = target->pid; - - /* CPU and PID are mutually exclusive */ - if (target->tid && target->cpu_list) { - ui__warning("WARNING: PID switch overriding CPU\n"); - sleep(1); - target->cpu_list = NULL; - } - - /* UID and PID are mutually exclusive */ - if (target->tid && target->uid_str) { - ui__warning("PID/TID switch overriding UID\n"); - sleep(1); - target->uid_str = NULL; - } - - /* UID and CPU are mutually exclusive */ - if (target->uid_str && target->cpu_list) { - ui__warning("UID switch overriding CPU\n"); - sleep(1); - target->cpu_list = NULL; - } - - /* PID/UID and SYSTEM are mutually exclusive */ - if ((target->tid || target->uid_str) && target->system_wide) { - ui__warning("PID/TID/UID switch overriding CPU\n"); - sleep(1); - target->system_wide = false; - } -} diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 3f05d62..52be74c 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -246,12 +246,10 @@ unsigned long convert_unit(unsigned long value, char *unit); int readn(int fd, void *buf, size_t size); struct perf_event_attr; -struct perf_target; void event_attr_init(struct perf_event_attr *attr); uid_t parse_target_uid(const char *str); -void perf_target__validate(struct perf_target *target); #define _STR(x) #x #define STR(x) _STR(x) -- cgit v0.10.2 From ca09b2e1b307724666577859eb460ac6d4c67330 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 30 Apr 2012 13:55:05 +0900 Subject: perf ui: Make setup_browser() generic The setup_browser contained newt-related codes in it. As gtk front-end added recently, it should be more generic to handle both cases properly. So move newt codes to the ui__init() for now. Signed-off-by: Namhyung Kim Acked-by: Pekka Enberg Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Pekka Enberg Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335761711-31403-2-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/ui/setup.c b/tools/perf/ui/setup.c index 85a69fa..becdcd0 100644 --- a/tools/perf/ui/setup.c +++ b/tools/perf/ui/setup.c @@ -93,14 +93,37 @@ static void newt_suspend(void *d __used) newtResume(); } +static void ui__exit(void); + +static void ui__signal(int sig) +{ + ui__exit(); + psignal(sig, "perf"); + exit(0); +} + static int ui__init(void) { - int err = SLkp_init(); + int err; - if (err < 0) + newtInit(); + err = SLkp_init(); + if (err < 0) { + pr_err("TUI initialization failed.\n"); goto out; + } SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB); + + newtSetSuspendCallback(newt_suspend, NULL); + ui_helpline__init(); + ui_browser__init(); + + signal(SIGSEGV, ui__signal); + signal(SIGFPE, ui__signal); + signal(SIGINT, ui__signal); + signal(SIGQUIT, ui__signal); + signal(SIGTERM, ui__signal); out: return err; } @@ -113,13 +136,6 @@ static void ui__exit(void) SLang_reset_tty(); } -static void ui__signal(int sig) -{ - ui__exit(); - psignal(sig, "perf"); - exit(0); -} - void setup_browser(bool fallback_to_pager) { if (!isatty(1) || !use_browser || dump_trace) { @@ -130,17 +146,7 @@ void setup_browser(bool fallback_to_pager) } use_browser = 1; - newtInit(); ui__init(); - newtSetSuspendCallback(newt_suspend, NULL); - ui_helpline__init(); - ui_browser__init(); - - signal(SIGSEGV, ui__signal); - signal(SIGFPE, ui__signal); - signal(SIGINT, ui__signal); - signal(SIGQUIT, ui__signal); - signal(SIGTERM, ui__signal); } void exit_browser(bool wait_for_ok) -- cgit v0.10.2 From 7706f966323f32f3ea13121b5918851432876ae5 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 30 Apr 2012 13:55:06 +0900 Subject: perf ui gtk: Drop arg[cv] arguments from perf_gtk_setup_browser() As perf doesn't allow to specify gtk command-line option, drop the arguments and pass NULL to gtk_init(). This makes the function easier to be called from setup_browser(). Signed-off-by: Namhyung Kim Acked-by: Pekka Enberg Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Pekka Enberg Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335761711-31403-3-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index cec2b8c..2b20001 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -678,7 +678,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __used) if (strcmp(report.input_name, "-") != 0) { if (report.use_gtk) - perf_gtk_setup_browser(argc, argv, true); + perf_gtk_setup_browser(true); else setup_browser(true); } else { diff --git a/tools/perf/ui/gtk/browser.c b/tools/perf/ui/gtk/browser.c index 258352a..a1a83de 100644 --- a/tools/perf/ui/gtk/browser.c +++ b/tools/perf/ui/gtk/browser.c @@ -9,10 +9,9 @@ #define MAX_COLUMNS 32 -void perf_gtk_setup_browser(int argc, const char *argv[], - bool fallback_to_pager __used) +void perf_gtk_setup_browser(bool fallback_to_pager __used) { - gtk_init(&argc, (char ***)&argv); + gtk_init(NULL, NULL); } void perf_gtk_exit_browser(bool wait_for_ok __used) diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h index 8dd224d..d22ca68 100644 --- a/tools/perf/util/cache.h +++ b/tools/perf/util/cache.h @@ -46,14 +46,14 @@ void exit_browser(bool wait_for_ok); #endif #ifdef NO_GTK2_SUPPORT -static inline void perf_gtk_setup_browser(int argc __used, const char *argv[] __used, bool fallback_to_pager) +static inline void perf_gtk_setup_browser(bool fallback_to_pager) { if (fallback_to_pager) setup_pager(); } static inline void perf_gtk_exit_browser(bool wait_for_ok __used) {} #else -void perf_gtk_setup_browser(int argc, const char *argv[], bool fallback_to_pager); +void perf_gtk_setup_browser(bool fallback_to_pager); void perf_gtk_exit_browser(bool wait_for_ok); #endif -- cgit v0.10.2 From 28e62b90d95a4ed8ae2ba93879003665051581a6 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 30 Apr 2012 13:55:07 +0900 Subject: perf ui gtk: Rename functions for consistency We use double underscore characters to distinguish its subsystem and actual function name. Signed-off-by: Namhyung Kim Acked-by: Pekka Enberg Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Pekka Enberg Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335761711-31403-4-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 2b20001..06115ff 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -678,7 +678,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __used) if (strcmp(report.input_name, "-") != 0) { if (report.use_gtk) - perf_gtk_setup_browser(true); + perf_gtk__setup_browser(true); else setup_browser(true); } else { diff --git a/tools/perf/ui/gtk/browser.c b/tools/perf/ui/gtk/browser.c index a1a83de..5eafd9b 100644 --- a/tools/perf/ui/gtk/browser.c +++ b/tools/perf/ui/gtk/browser.c @@ -9,23 +9,23 @@ #define MAX_COLUMNS 32 -void perf_gtk_setup_browser(bool fallback_to_pager __used) +void perf_gtk__setup_browser(bool fallback_to_pager __used) { gtk_init(NULL, NULL); } -void perf_gtk_exit_browser(bool wait_for_ok __used) +void perf_gtk__exit_browser(bool wait_for_ok __used) { gtk_main_quit(); } -static void perf_gtk_signal(int sig) +static void perf_gtk__signal(int sig) { psignal(sig, "perf"); gtk_main_quit(); } -static void perf_gtk_resize_window(GtkWidget *window) +static void perf_gtk__resize_window(GtkWidget *window) { GdkRectangle rect; GdkScreen *screen; @@ -45,7 +45,7 @@ static void perf_gtk_resize_window(GtkWidget *window) gtk_window_resize(GTK_WINDOW(window), width, height); } -static void perf_gtk_show_hists(GtkWidget *window, struct hists *hists) +static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists) { GType col_types[MAX_COLUMNS]; GtkCellRenderer *renderer; @@ -141,11 +141,11 @@ int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist, GtkWidget *notebook; GtkWidget *window; - signal(SIGSEGV, perf_gtk_signal); - signal(SIGFPE, perf_gtk_signal); - signal(SIGINT, perf_gtk_signal); - signal(SIGQUIT, perf_gtk_signal); - signal(SIGTERM, perf_gtk_signal); + signal(SIGSEGV, perf_gtk__signal); + signal(SIGFPE, perf_gtk__signal); + signal(SIGINT, perf_gtk__signal); + signal(SIGQUIT, perf_gtk__signal); + signal(SIGTERM, perf_gtk__signal); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); @@ -167,7 +167,7 @@ int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); - perf_gtk_show_hists(scrolled_window, hists); + perf_gtk__show_hists(scrolled_window, hists); tab_label = gtk_label_new(evname); @@ -178,7 +178,7 @@ int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist, gtk_widget_show_all(window); - perf_gtk_resize_window(window); + perf_gtk__resize_window(window); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h index d22ca68..3428b77 100644 --- a/tools/perf/util/cache.h +++ b/tools/perf/util/cache.h @@ -46,15 +46,15 @@ void exit_browser(bool wait_for_ok); #endif #ifdef NO_GTK2_SUPPORT -static inline void perf_gtk_setup_browser(bool fallback_to_pager) +static inline void perf_gtk__setup_browser(bool fallback_to_pager) { if (fallback_to_pager) setup_pager(); } -static inline void perf_gtk_exit_browser(bool wait_for_ok __used) {} +static inline void perf_gtk__exit_browser(bool wait_for_ok __used) {} #else -void perf_gtk_setup_browser(bool fallback_to_pager); -void perf_gtk_exit_browser(bool wait_for_ok); +void perf_gtk__setup_browser(bool fallback_to_pager); +void perf_gtk__exit_browser(bool wait_for_ok); #endif char *alias_lookup(const char *alias); -- cgit v0.10.2 From 281ef544a8476f750b9f378593c42b3e8a0b8788 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 30 Apr 2012 13:55:08 +0900 Subject: perf ui: Add gtk2 support into setup_browser() Now setup_browser can handle gtk2 front-end so split the TUI code to ui/tui/setup.c in order to remove dependency. To this end, make ui__init/exit global symbols and take an argument. Also split gtk code to ui/gtk/setup.c. Signed-off-by: Namhyung Kim Acked-by: Pekka Enberg Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Pekka Enberg Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335761711-31403-5-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 4122a66..4734f41 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile @@ -483,6 +483,7 @@ else LIB_OBJS += $(OUTPUT)ui/helpline.o LIB_OBJS += $(OUTPUT)ui/progress.o LIB_OBJS += $(OUTPUT)ui/util.o + LIB_OBJS += $(OUTPUT)ui/tui/setup.o LIB_H += ui/browser.h LIB_H += ui/browsers/map.h LIB_H += ui/helpline.h @@ -505,6 +506,11 @@ else BASIC_CFLAGS += $(shell pkg-config --cflags gtk+-2.0) EXTLIBS += $(shell pkg-config --libs gtk+-2.0) LIB_OBJS += $(OUTPUT)ui/gtk/browser.o + LIB_OBJS += $(OUTPUT)ui/gtk/setup.o + # Make sure that it'd be included only once. + ifneq ($(findstring -DNO_NEWT_SUPPORT,$(BASIC_CFLAGS)),) + LIB_OBJS += $(OUTPUT)ui/setup.o + endif endif endif diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 06115ff..5df829f 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -676,14 +676,10 @@ int cmd_report(int argc, const char **argv, const char *prefix __used) } - if (strcmp(report.input_name, "-") != 0) { - if (report.use_gtk) - perf_gtk__setup_browser(true); - else - setup_browser(true); - } else { + if (strcmp(report.input_name, "-") != 0) + setup_browser(true); + else use_browser = 0; - } /* * Only in the newt browser we are doing integrated annotation, diff --git a/tools/perf/ui/gtk/browser.c b/tools/perf/ui/gtk/browser.c index 5eafd9b..0656c38 100644 --- a/tools/perf/ui/gtk/browser.c +++ b/tools/perf/ui/gtk/browser.c @@ -9,16 +9,6 @@ #define MAX_COLUMNS 32 -void perf_gtk__setup_browser(bool fallback_to_pager __used) -{ - gtk_init(NULL, NULL); -} - -void perf_gtk__exit_browser(bool wait_for_ok __used) -{ - gtk_main_quit(); -} - static void perf_gtk__signal(int sig) { psignal(sig, "perf"); diff --git a/tools/perf/ui/gtk/setup.c b/tools/perf/ui/gtk/setup.c new file mode 100644 index 0000000..8c3b573 --- /dev/null +++ b/tools/perf/ui/gtk/setup.c @@ -0,0 +1,12 @@ +#include "gtk.h" +#include "../../util/cache.h" + +void perf_gtk__init(bool fallback_to_pager __used) +{ + gtk_init(NULL, NULL); +} + +void perf_gtk__exit(bool wait_for_ok __used) +{ + gtk_main_quit(); +} diff --git a/tools/perf/ui/setup.c b/tools/perf/ui/setup.c index becdcd0..98130e09 100644 --- a/tools/perf/ui/setup.c +++ b/tools/perf/ui/setup.c @@ -1,161 +1,44 @@ -#include -#include -#include - #include "../cache.h" #include "../debug.h" -#include "browser.h" -#include "helpline.h" -#include "ui.h" -#include "util.h" -#include "libslang.h" -#include "keysyms.h" - -pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER; - -static volatile int ui__need_resize; - -void ui__refresh_dimensions(bool force) -{ - if (force || ui__need_resize) { - ui__need_resize = 0; - pthread_mutex_lock(&ui__lock); - SLtt_get_screen_size(); - SLsmg_reinit_smg(); - pthread_mutex_unlock(&ui__lock); - } -} - -static void ui__sigwinch(int sig __used) -{ - ui__need_resize = 1; -} - -static void ui__setup_sigwinch(void) -{ - static bool done; - - if (done) - return; - - done = true; - pthread__unblock_sigwinch(); - signal(SIGWINCH, ui__sigwinch); -} - -int ui__getch(int delay_secs) -{ - struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL; - fd_set read_set; - int err, key; - - ui__setup_sigwinch(); - - FD_ZERO(&read_set); - FD_SET(0, &read_set); - - if (delay_secs) { - timeout.tv_sec = delay_secs; - timeout.tv_usec = 0; - } - - err = select(1, &read_set, NULL, NULL, ptimeout); - - if (err == 0) - return K_TIMER; - - if (err == -1) { - if (errno == EINTR) - return K_RESIZE; - return K_ERROR; - } - key = SLang_getkey(); - if (key != K_ESC) - return key; - FD_ZERO(&read_set); - FD_SET(0, &read_set); - timeout.tv_sec = 0; - timeout.tv_usec = 20; - err = select(1, &read_set, NULL, NULL, &timeout); - if (err == 0) - return K_ESC; - - SLang_ungetkey(key); - return SLkp_getkey(); -} - -static void newt_suspend(void *d __used) -{ - newtSuspend(); - raise(SIGTSTP); - newtResume(); -} - -static void ui__exit(void); - -static void ui__signal(int sig) -{ - ui__exit(); - psignal(sig, "perf"); - exit(0); -} - -static int ui__init(void) +void setup_browser(bool fallback_to_pager) { - int err; - - newtInit(); - err = SLkp_init(); - if (err < 0) { - pr_err("TUI initialization failed.\n"); - goto out; - } - - SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB); + if (!isatty(1) || dump_trace) + use_browser = 0; - newtSetSuspendCallback(newt_suspend, NULL); - ui_helpline__init(); - ui_browser__init(); + /* default to TUI */ + if (use_browser < 0) + use_browser = 1; - signal(SIGSEGV, ui__signal); - signal(SIGFPE, ui__signal); - signal(SIGINT, ui__signal); - signal(SIGQUIT, ui__signal); - signal(SIGTERM, ui__signal); -out: - return err; -} + switch (use_browser) { + case 2: + perf_gtk__init(fallback_to_pager); + break; -static void ui__exit(void) -{ - SLtt_set_cursor_visibility(1); - SLsmg_refresh(); - SLsmg_reset_smg(); - SLang_reset_tty(); -} + case 1: + ui__init(fallback_to_pager); + break; -void setup_browser(bool fallback_to_pager) -{ - if (!isatty(1) || !use_browser || dump_trace) { - use_browser = 0; + default: if (fallback_to_pager) setup_pager(); - return; + break; } - - use_browser = 1; - ui__init(); } void exit_browser(bool wait_for_ok) { - if (use_browser > 0) { - if (wait_for_ok) - ui__question_window("Fatal Error", - ui_helpline__last_msg, - "Press any key...", 0); - ui__exit(); + switch (use_browser) { + case 2: + perf_gtk__exit(wait_for_ok); + break; + + case 1: + ui__exit(wait_for_ok); + break; + + default: + break; } } diff --git a/tools/perf/ui/tui/setup.c b/tools/perf/ui/tui/setup.c new file mode 100644 index 0000000..0194cea --- /dev/null +++ b/tools/perf/ui/tui/setup.c @@ -0,0 +1,140 @@ +#include +#include +#include + +#include "../../util/cache.h" +#include "../../util/debug.h" +#include "../browser.h" +#include "../helpline.h" +#include "../ui.h" +#include "../util.h" +#include "../libslang.h" +#include "../keysyms.h" + +pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER; + +static volatile int ui__need_resize; + +void ui__refresh_dimensions(bool force) +{ + if (force || ui__need_resize) { + ui__need_resize = 0; + pthread_mutex_lock(&ui__lock); + SLtt_get_screen_size(); + SLsmg_reinit_smg(); + pthread_mutex_unlock(&ui__lock); + } +} + +static void ui__sigwinch(int sig __used) +{ + ui__need_resize = 1; +} + +static void ui__setup_sigwinch(void) +{ + static bool done; + + if (done) + return; + + done = true; + pthread__unblock_sigwinch(); + signal(SIGWINCH, ui__sigwinch); +} + +int ui__getch(int delay_secs) +{ + struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL; + fd_set read_set; + int err, key; + + ui__setup_sigwinch(); + + FD_ZERO(&read_set); + FD_SET(0, &read_set); + + if (delay_secs) { + timeout.tv_sec = delay_secs; + timeout.tv_usec = 0; + } + + err = select(1, &read_set, NULL, NULL, ptimeout); + + if (err == 0) + return K_TIMER; + + if (err == -1) { + if (errno == EINTR) + return K_RESIZE; + return K_ERROR; + } + + key = SLang_getkey(); + if (key != K_ESC) + return key; + + FD_ZERO(&read_set); + FD_SET(0, &read_set); + timeout.tv_sec = 0; + timeout.tv_usec = 20; + err = select(1, &read_set, NULL, NULL, &timeout); + if (err == 0) + return K_ESC; + + SLang_ungetkey(key); + return SLkp_getkey(); +} + +static void newt_suspend(void *d __used) +{ + newtSuspend(); + raise(SIGTSTP); + newtResume(); +} + +static void ui__signal(int sig) +{ + ui__exit(false); + psignal(sig, "perf"); + exit(0); +} + +int ui__init(bool fallback_to_pager __used) +{ + int err; + + newtInit(); + err = SLkp_init(); + if (err < 0) { + pr_err("TUI initialization failed.\n"); + goto out; + } + + SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB); + + newtSetSuspendCallback(newt_suspend, NULL); + ui_helpline__init(); + ui_browser__init(); + + signal(SIGSEGV, ui__signal); + signal(SIGFPE, ui__signal); + signal(SIGINT, ui__signal); + signal(SIGQUIT, ui__signal); + signal(SIGTERM, ui__signal); +out: + return err; +} + +void ui__exit(bool wait_for_ok) +{ + if (wait_for_ok) + ui__question_window("Fatal Error", + ui_helpline__last_msg, + "Press any key...", 0); + + SLtt_set_cursor_visibility(1); + SLsmg_refresh(); + SLsmg_reset_smg(); + SLang_reset_tty(); +} diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h index 3428b77..761d4e9 100644 --- a/tools/perf/util/cache.h +++ b/tools/perf/util/cache.h @@ -33,7 +33,7 @@ extern int pager_use_color; extern int use_browser; -#ifdef NO_NEWT_SUPPORT +#if defined(NO_NEWT_SUPPORT) && defined(NO_GTK2_SUPPORT) static inline void setup_browser(bool fallback_to_pager) { if (fallback_to_pager) @@ -43,19 +43,32 @@ static inline void exit_browser(bool wait_for_ok __used) {} #else void setup_browser(bool fallback_to_pager); void exit_browser(bool wait_for_ok); + +#ifdef NO_NEWT_SUPPORT +static inline int ui__init(bool fallback_to_pager) +{ + if (fallback_to_pager) + setup_pager(); + return 0; +} +static inline void ui__exit(bool wait_for_ok __used) {} +#else +int ui__init(bool fallback_to_pager); +void ui__exit(bool wait_for_ok); #endif #ifdef NO_GTK2_SUPPORT -static inline void perf_gtk__setup_browser(bool fallback_to_pager) +static inline void perf_gtk__init(bool fallback_to_pager) { if (fallback_to_pager) setup_pager(); } -static inline void perf_gtk__exit_browser(bool wait_for_ok __used) {} +static inline void perf_gtk__exit(bool wait_for_ok __used) {} #else -void perf_gtk__setup_browser(bool fallback_to_pager); -void perf_gtk__exit_browser(bool wait_for_ok); +void perf_gtk__init(bool fallback_to_pager); +void perf_gtk__exit(bool wait_for_ok); #endif +#endif /* NO_NEWT_SUPPORT && NO_GTK2_SUPPORT */ char *alias_lookup(const char *alias); int split_cmdline(char *cmdline, const char ***argv); -- cgit v0.10.2 From dc41b9b8f02dbe2228ae787d525dac43beebb7fa Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 30 Apr 2012 13:55:09 +0900 Subject: perf ui: Change fallback policy of setup_browser() If gtk2 support is not enabled (or failed for some reason) try TUI again instead of falling directly back to the stdio interface. Signed-off-by: Namhyung Kim Acked-by: Pekka Enberg Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Pekka Enberg Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1335761711-31403-6-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/ui/gtk/setup.c b/tools/perf/ui/gtk/setup.c index 8c3b573..8295299 100644 --- a/tools/perf/ui/gtk/setup.c +++ b/tools/perf/ui/gtk/setup.c @@ -1,9 +1,9 @@ #include "gtk.h" #include "../../util/cache.h" -void perf_gtk__init(bool fallback_to_pager __used) +int perf_gtk__init(void) { - gtk_init(NULL, NULL); + return gtk_init_check(NULL, NULL) ? 0 : -1; } void perf_gtk__exit(bool wait_for_ok __used) diff --git a/tools/perf/ui/setup.c b/tools/perf/ui/setup.c index 98130e09..9f5f888 100644 --- a/tools/perf/ui/setup.c +++ b/tools/perf/ui/setup.c @@ -13,13 +13,14 @@ void setup_browser(bool fallback_to_pager) switch (use_browser) { case 2: - perf_gtk__init(fallback_to_pager); - break; - + if (perf_gtk__init() == 0) + break; + /* fall through */ case 1: - ui__init(fallback_to_pager); - break; - + use_browser = 1; + if (ui__init() == 0) + break; + /* fall through */ default: if (fallback_to_pager) setup_pager(); diff --git a/tools/perf/ui/tui/setup.c b/tools/perf/ui/tui/setup.c index 0194cea..d33e943 100644 --- a/tools/perf/ui/tui/setup.c +++ b/tools/perf/ui/tui/setup.c @@ -100,7 +100,7 @@ static void ui__signal(int sig) exit(0); } -int ui__init(bool fallback_to_pager __used) +int ui__init(void) { int err; diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h index 761d4e9..cff18c6 100644 --- a/tools/perf/util/cache.h +++ b/tools/perf/util/cache.h @@ -45,27 +45,24 @@ void setup_browser(bool fallback_to_pager); void exit_browser(bool wait_for_ok); #ifdef NO_NEWT_SUPPORT -static inline int ui__init(bool fallback_to_pager) +static inline int ui__init(void) { - if (fallback_to_pager) - setup_pager(); - return 0; + return -1; } static inline void ui__exit(bool wait_for_ok __used) {} #else -int ui__init(bool fallback_to_pager); +int ui__init(void); void ui__exit(bool wait_for_ok); #endif #ifdef NO_GTK2_SUPPORT -static inline void perf_gtk__init(bool fallback_to_pager) +static inline int perf_gtk__init(void) { - if (fallback_to_pager) - setup_pager(); + return -1; } static inline void perf_gtk__exit(bool wait_for_ok __used) {} #else -void perf_gtk__init(bool fallback_to_pager); +int perf_gtk__init(void); void perf_gtk__exit(bool wait_for_ok); #endif #endif /* NO_NEWT_SUPPORT && NO_GTK2_SUPPORT */ -- cgit v0.10.2 From 9389a46043c8f091dc8f8d8e25a5c1355f8bcc9b Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Mon, 16 Apr 2012 20:42:51 +0200 Subject: perf session: Fail on processing event with unknown size Currently if we cannot decide the size of the event, we guess next event possition by: "... check alignment, and increment a single u64 in the hope to catch on again 'soon'" This usually ends up with segfault or endless loop. It's better to admit the failure right away, then pretend nothing happened. It makes the life easier ;) Signed-off-by: Jiri Olsa Acked-by: Peter Zijlstra Cc: Corey Ashford Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/20120416184251.GA11503@m.brq.redhat.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 9412e3b..f992ae3 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1100,16 +1100,10 @@ more: } if ((skip = perf_session__process_event(self, &event, tool, head)) < 0) { - dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n", - head, event.header.size, event.header.type); - /* - * assume we lost track of the stream, check alignment, and - * increment a single u64 in the hope to catch on again 'soon'. - */ - if (unlikely(head & 7)) - head &= ~7ULL; - - size = 8; + pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", + head, event.header.size, event.header.type); + err = -EINVAL; + goto out_err; } head += size; @@ -1218,17 +1212,11 @@ more: if (size == 0 || perf_session__process_event(session, event, tool, file_pos) < 0) { - dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n", - file_offset + head, event->header.size, - event->header.type); - /* - * assume we lost track of the stream, check alignment, and - * increment a single u64 in the hope to catch on again 'soon'. - */ - if (unlikely(head & 7)) - head &= ~7ULL; - - size = 8; + pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", + file_offset + head, event->header.size, + event->header.type); + err = -EINVAL; + goto out_err; } head += size; -- cgit v0.10.2 From 10b47d54154ce711e4c4438aff10f0215b2ab8eb Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 7 May 2012 16:33:56 -0300 Subject: perf top: Set target.system_wide Check if neither of --pid, --tid or --uid was specified and if so, set system_wide appropriately. Namhyung's patch would make using any of the above target specifiers emit a warning in perf_target__validate, since it would see target.system_wide set and one of the others as well. So set system_wide after validation. Suggested-by: David Ahern Cc: David Ahern Cc: Frederic Weisbecker Cc: Mike Galbraith Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lkml.kernel.org/n/tip-6e4zrji1uw0rinfyoitl0wi4@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 2a0ec09..e40f86e 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1258,6 +1258,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) if (top.target.uid_str != NULL && top.target.uid == UINT_MAX - 1) goto out_delete_evlist; + if (top.target.tid == 0 && top.target.pid == 0 && + top.target.uid_str == NULL) + top.target.system_wide = true; + if (perf_evlist__create_maps(top.evlist, &top.target) < 0) usage_with_options(top_usage, options); -- cgit v0.10.2 From 55261f46702cec96911a81aacfb3cba13434d304 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 7 May 2012 14:08:59 +0900 Subject: perf evlist: Fix creation of cpu map Currently, 'perf record -- sleep 1' creates a cpu map for all online cpus since it turns out calling cpu_map__new(NULL). Fix it. Also it is guaranteed that cpu_list is NULL if PID/TID is given by calling perf_target__validate(), so we can make the conditional bit simpler. This also fixes perf test 7 (Validate) failure on my 6 core machine: $ cat /sys/devices/system/cpu/online 0-11 $ ./perf test -v 7 7: Validate PERF_RECORD_* events & perf_sample fields: --- start --- perf_evlist__mmap: Operation not permitted ---- end ---- Validate PERF_RECORD_* events & perf_sample fields: FAILED! Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336367344-28071-3-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 3032862..183b199 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -609,8 +609,9 @@ int perf_evlist__create_maps(struct perf_evlist *evlist, if (evlist->threads == NULL) return -1; - if (target->uid != UINT_MAX || - (target->cpu_list == NULL && target->tid)) + if (target->uid != UINT_MAX || target->tid) + evlist->cpus = cpu_map__dummy_new(); + else if (!target->system_wide && target->cpu_list == NULL) evlist->cpus = cpu_map__dummy_new(); else evlist->cpus = cpu_map__new(target->cpu_list); -- cgit v0.10.2 From 60bbddaaa33865633efa2800702e3b02495a0e94 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 7 May 2012 14:09:00 +0900 Subject: perf target: Introduce perf_target_errno The perf_target_errno enumerations are used to indicate specific error cases on perf target operations. It'd help libperf being a more generic library. Signed-off-by: Namhyung Kim Suggested-by: Arnaldo Carvalho de Melo Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336367344-28071-4-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c index 3fadf85..5c59dcf 100644 --- a/tools/perf/util/target.c +++ b/tools/perf/util/target.c @@ -10,36 +10,47 @@ #include "debug.h" -void perf_target__validate(struct perf_target *target) +enum perf_target_errno perf_target__validate(struct perf_target *target) { + enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS; + if (target->pid) target->tid = target->pid; /* CPU and PID are mutually exclusive */ if (target->tid && target->cpu_list) { - ui__warning("WARNING: PID switch overriding CPU\n"); - sleep(1); target->cpu_list = NULL; + if (ret == PERF_ERRNO_TARGET__SUCCESS) + ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU; } /* UID and PID are mutually exclusive */ if (target->tid && target->uid_str) { - ui__warning("PID/TID switch overriding UID\n"); - sleep(1); target->uid_str = NULL; + if (ret == PERF_ERRNO_TARGET__SUCCESS) + ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID; } /* UID and CPU are mutually exclusive */ if (target->uid_str && target->cpu_list) { - ui__warning("UID switch overriding CPU\n"); - sleep(1); target->cpu_list = NULL; + if (ret == PERF_ERRNO_TARGET__SUCCESS) + ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU; } - /* PID/UID and SYSTEM are mutually exclusive */ - if ((target->tid || target->uid_str) && target->system_wide) { - ui__warning("PID/TID/UID switch overriding CPU\n"); - sleep(1); + /* PID and SYSTEM are mutually exclusive */ + if (target->tid && target->system_wide) { target->system_wide = false; + if (ret == PERF_ERRNO_TARGET__SUCCESS) + ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM; } + + /* UID and SYSTEM are mutually exclusive */ + if (target->uid_str && target->system_wide) { + target->system_wide = false; + if (ret == PERF_ERRNO_TARGET__SUCCESS) + ret = PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM; + } + + return ret; } diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h index 218291f..eb0d210 100644 --- a/tools/perf/util/target.h +++ b/tools/perf/util/target.h @@ -13,6 +13,29 @@ struct perf_target { bool system_wide; }; -void perf_target__validate(struct perf_target *target); +enum perf_target_errno { + PERF_ERRNO_TARGET__SUCCESS = 0, + + /* + * Choose an arbitrary negative big number not to clash with standard + * errno since SUS requires the errno has distinct positive values. + * See 'Issue 6' in the link below. + * + * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html + */ + __PERF_ERRNO_TARGET__START = -10000, + + + /* for perf_target__validate() */ + PERF_ERRNO_TARGET__PID_OVERRIDE_CPU = __PERF_ERRNO_TARGET__START, + PERF_ERRNO_TARGET__PID_OVERRIDE_UID, + PERF_ERRNO_TARGET__UID_OVERRIDE_CPU, + PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM, + PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM, + + __PERF_ERRNO_TARGET__END, +}; + +enum perf_target_errno perf_target__validate(struct perf_target *target); #endif /* _PERF_TARGET_H */ -- cgit v0.10.2 From dfe78adaaca90417ece98edbd3eb1c9661334406 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 7 May 2012 14:09:01 +0900 Subject: perf target: Introduce perf_target__parse_uid() Add and use the modern perf_target__parse_uid() and get rid of the old parse_target_uid(). Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336367344-28071-5-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index d165909..d26a279 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -886,9 +886,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) perf_target__validate(&rec->opts.target); - rec->opts.target.uid = parse_target_uid(rec->opts.target.uid_str); - if (rec->opts.target.uid_str != NULL && - rec->opts.target.uid == UINT_MAX - 1) + if (perf_target__parse_uid(&rec->opts.target) < 0) goto out_free_fd; if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index e40f86e..c9137ba 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1254,8 +1254,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) perf_target__validate(&top.target); - top.target.uid = parse_target_uid(top.target.uid_str); - if (top.target.uid_str != NULL && top.target.uid == UINT_MAX - 1) + if (perf_target__parse_uid(&top.target) < 0) goto out_delete_evlist; if (top.target.tid == 0 && top.target.pid == 0 && diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c index 5c59dcf..02a6bed 100644 --- a/tools/perf/util/target.c +++ b/tools/perf/util/target.c @@ -9,6 +9,8 @@ #include "target.h" #include "debug.h" +#include + enum perf_target_errno perf_target__validate(struct perf_target *target) { @@ -54,3 +56,36 @@ enum perf_target_errno perf_target__validate(struct perf_target *target) return ret; } + +enum perf_target_errno perf_target__parse_uid(struct perf_target *target) +{ + struct passwd pwd, *result; + char buf[1024]; + const char *str = target->uid_str; + + target->uid = UINT_MAX; + if (str == NULL) + return PERF_ERRNO_TARGET__SUCCESS; + + /* Try user name first */ + getpwnam_r(str, &pwd, buf, sizeof(buf), &result); + + if (result == NULL) { + /* + * The user name not found. Maybe it's a UID number. + */ + char *endptr; + int uid = strtol(str, &endptr, 10); + + if (*endptr != '\0') + return PERF_ERRNO_TARGET__INVALID_UID; + + getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); + + if (result == NULL) + return PERF_ERRNO_TARGET__USER_NOT_FOUND; + } + + target->uid = result->pw_uid; + return PERF_ERRNO_TARGET__SUCCESS; +} diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h index eb0d210..d4aabda 100644 --- a/tools/perf/util/target.h +++ b/tools/perf/util/target.h @@ -33,9 +33,14 @@ enum perf_target_errno { PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM, PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM, + /* for perf_target__parse_uid() */ + PERF_ERRNO_TARGET__INVALID_UID, + PERF_ERRNO_TARGET__USER_NOT_FOUND, + __PERF_ERRNO_TARGET__END, }; enum perf_target_errno perf_target__validate(struct perf_target *target); +enum perf_target_errno perf_target__parse_uid(struct perf_target *target); #endif /* _PERF_TARGET_H */ diff --git a/tools/perf/util/usage.c b/tools/perf/util/usage.c index e851abc..4007aca 100644 --- a/tools/perf/util/usage.c +++ b/tools/perf/util/usage.c @@ -82,34 +82,3 @@ void warning(const char *warn, ...) warn_routine(warn, params); va_end(params); } - -uid_t parse_target_uid(const char *str) -{ - struct passwd pwd, *result; - char buf[1024]; - - if (str == NULL) - return UINT_MAX; - - getpwnam_r(str, &pwd, buf, sizeof(buf), &result); - - if (result == NULL) { - char *endptr; - int uid = strtol(str, &endptr, 10); - - if (*endptr != '\0') { - ui__error("Invalid user %s\n", str); - return UINT_MAX - 1; - } - - getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); - - if (result == NULL) { - ui__error("Problems obtaining information for user %s\n", - str); - return UINT_MAX - 1; - } - } - - return result->pw_uid; -} diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 52be74c..27a11a7 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -74,7 +74,6 @@ #include #include #include -#include #include #include "../../../include/linux/magic.h" #include "types.h" @@ -249,8 +248,6 @@ struct perf_event_attr; void event_attr_init(struct perf_event_attr *attr); -uid_t parse_target_uid(const char *str); - #define _STR(x) #x #define STR(x) _STR(x) -- cgit v0.10.2 From 16ad2ffb822cd28e2330284a60fdfec8bb90bbb0 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 7 May 2012 14:09:02 +0900 Subject: perf tools: Introduce perf_target__strerror() The perf_target__strerror() sets @buf to a string that describes the (perf_target-specific) error condition that is passed via @errnum. This is similar to strerror_r() and does same thing if @errnum has a standard errno value. Signed-off-by: Namhyung Kim Suggested-by: Arnaldo Carvalho de Melo Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336367344-28071-6-git-send-email-namhyung.kim@lge.com [ committer note: No need to use PERF_ERRNO_TARGET__SUCCESS, use shorter idiom ] Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index d26a279..c8bf6ea 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -831,6 +831,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) struct perf_evsel *pos; struct perf_evlist *evsel_list; struct perf_record *rec = &record; + char errbuf[BUFSIZ]; perf_header__set_cmdline(argc, argv); @@ -884,11 +885,24 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) goto out_symbol_exit; } - perf_target__validate(&rec->opts.target); + err = perf_target__validate(&rec->opts.target); + if (err) { + perf_target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); + ui__warning("%s", errbuf); + } + + err = perf_target__parse_uid(&rec->opts.target); + if (err) { + int saved_errno = errno; - if (perf_target__parse_uid(&rec->opts.target) < 0) + perf_target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); + ui__warning("%s", errbuf); + + err = -saved_errno; goto out_free_fd; + } + err = -ENOMEM; if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0) usage_with_options(record_usage, record_options); diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index c9137ba..7ba0f03 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1150,7 +1150,8 @@ static const char * const top_usage[] = { int cmd_top(int argc, const char **argv, const char *prefix __used) { struct perf_evsel *pos; - int status = -ENOMEM; + int status; + char errbuf[BUFSIZ]; struct perf_top top = { .count_filter = 5, .delay_secs = 2, @@ -1252,10 +1253,22 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) setup_browser(false); - perf_target__validate(&top.target); + status = perf_target__validate(&top.target); + if (status) { + perf_target__strerror(&top.target, status, errbuf, BUFSIZ); + ui__warning("%s", errbuf); + } + + status = perf_target__parse_uid(&top.target); + if (status) { + int saved_errno = errno; - if (perf_target__parse_uid(&top.target) < 0) + perf_target__strerror(&top.target, status, errbuf, BUFSIZ); + ui__warning("%s", errbuf); + + status = -saved_errno; goto out_delete_evlist; + } if (top.target.tid == 0 && top.target.pid == 0 && top.target.uid_str == NULL) diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index 26817da..efb1fce 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -11,6 +11,7 @@ #include "event.h" #include "debug.h" #include "util.h" +#include "target.h" int verbose; bool dump_trace = false, quiet = false; diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c index 02a6bed..1064d5b 100644 --- a/tools/perf/util/target.c +++ b/tools/perf/util/target.c @@ -10,6 +10,7 @@ #include "debug.h" #include +#include enum perf_target_errno perf_target__validate(struct perf_target *target) @@ -89,3 +90,53 @@ enum perf_target_errno perf_target__parse_uid(struct perf_target *target) target->uid = result->pw_uid; return PERF_ERRNO_TARGET__SUCCESS; } + +/* + * This must have a same ordering as the enum perf_target_errno. + */ +static const char *perf_target__error_str[] = { + "PID/TID switch overriding CPU", + "PID/TID switch overriding UID", + "UID switch overriding CPU", + "PID/TID switch overriding SYSTEM", + "UID switch overriding SYSTEM", + "Invalid User: %s", + "Problems obtaining information for user %s", +}; + +int perf_target__strerror(struct perf_target *target, int errnum, + char *buf, size_t buflen) +{ + int idx; + const char *msg; + + if (errnum >= 0) { + strerror_r(errnum, buf, buflen); + return 0; + } + + if (errnum < __PERF_ERRNO_TARGET__START || + errnum >= __PERF_ERRNO_TARGET__END) + return -1; + + idx = errnum - __PERF_ERRNO_TARGET__START; + msg = perf_target__error_str[idx]; + + switch (errnum) { + case PERF_ERRNO_TARGET__PID_OVERRIDE_CPU + ... PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM: + snprintf(buf, buflen, "%s", msg); + break; + + case PERF_ERRNO_TARGET__INVALID_UID: + case PERF_ERRNO_TARGET__USER_NOT_FOUND: + snprintf(buf, buflen, msg, target->uid_str); + break; + + default: + /* cannot reach here */ + break; + } + + return 0; +} diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h index d4aabda..6fcd01c 100644 --- a/tools/perf/util/target.h +++ b/tools/perf/util/target.h @@ -43,4 +43,7 @@ enum perf_target_errno { enum perf_target_errno perf_target__validate(struct perf_target *target); enum perf_target_errno perf_target__parse_uid(struct perf_target *target); +int perf_target__strerror(struct perf_target *target, int errnum, char *buf, + size_t buflen); + #endif /* _PERF_TARGET_H */ -- cgit v0.10.2 From d67356e7f80f5c2ef487bedc11a91d5fe18c5a15 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 7 May 2012 14:09:03 +0900 Subject: perf target: Consolidate target task/cpu checking There are places that check whether target task/cpu is given or not and some of them didn't check newly introduced uid or cpu list. Add and use three of helper functions to treat them properly. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336367344-28071-7-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index c8bf6ea..42e2414 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -843,9 +843,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) argc = parse_options(argc, argv, record_options, record_usage, PARSE_OPT_STOP_AT_NON_OPTION); - if (!argc && !rec->opts.target.pid && !rec->opts.target.tid && - !rec->opts.target.system_wide && !rec->opts.target.cpu_list && - !rec->opts.target.uid_str) + if (!argc && perf_target__none(&rec->opts.target)) usage_with_options(record_usage, record_options); if (rec->force && rec->append_file) { diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index bb77232..d9ff246 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -290,10 +290,10 @@ static int create_perf_stat_counter(struct perf_evsel *evsel, attr->inherit = !no_inherit; - if (target.system_wide) + if (!perf_target__no_cpu(&target)) return perf_evsel__open_per_cpu(evsel, evsel_list->cpus, group, group_fd); - if (!target.pid && !target.tid && (!group || evsel == first)) { + if (perf_target__no_task(&target) && (!group || evsel == first)) { attr->disabled = 1; attr->enable_on_exec = 1; } @@ -443,7 +443,7 @@ static int run_perf_stat(int argc __used, const char **argv) exit(-1); } - if (!target.tid && !target.pid && !target.system_wide) + if (perf_target__none(&target)) evsel_list->threads->map[0] = child_pid; /* @@ -965,7 +965,7 @@ static void print_stat(int argc, const char **argv) if (!csv_output) { fprintf(output, "\n"); fprintf(output, " Performance counter stats for "); - if (!target.pid && !target.tid) { + if (perf_target__no_task(&target)) { fprintf(output, "\'%s", argv[0]); for (i = 1; i < argc; i++) fprintf(output, " %s", argv[i]); @@ -1187,13 +1187,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) } else if (big_num_opt == 0) /* User passed --no-big-num */ big_num = false; - if (!argc && !target.pid && !target.tid) + if (!argc && perf_target__no_task(&target)) usage_with_options(stat_usage, options); if (run_count <= 0) usage_with_options(stat_usage, options); /* no_aggr, cgroup are for system-wide only */ - if ((no_aggr || nr_cgroups) && !target.system_wide) { + if ((no_aggr || nr_cgroups) && perf_target__no_cpu(&target)) { fprintf(stderr, "both cgroup and no-aggregation " "modes only available in system-wide mode\n"); diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 7ba0f03..e4ca827 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1016,7 +1016,7 @@ static int __cmd_top(struct perf_top *top) if (ret) goto out_delete; - if (top->target.tid || top->target.uid != UINT_MAX) + if (!perf_target__no_task(&top->target)) perf_event__synthesize_thread_map(&top->tool, top->evlist->threads, perf_event__process, &top->session->host_machine); diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 183b199..1201daf 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -609,12 +609,10 @@ int perf_evlist__create_maps(struct perf_evlist *evlist, if (evlist->threads == NULL) return -1; - if (target->uid != UINT_MAX || target->tid) - evlist->cpus = cpu_map__dummy_new(); - else if (!target->system_wide && target->cpu_list == NULL) - evlist->cpus = cpu_map__dummy_new(); - else + if (!perf_target__no_cpu(target)) evlist->cpus = cpu_map__new(target->cpu_list); + else + evlist->cpus = cpu_map__dummy_new(); if (evlist->cpus == NULL) goto out_delete_threads; @@ -831,7 +829,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, exit(-1); } - if (!opts->target.system_wide && !opts->target.tid && !opts->target.pid) + if (perf_target__none(&opts->target)) evlist->threads->map[0] = evlist->workload.pid; close(child_ready_pipe[1]); diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index bb785a0..21eaab2 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -114,8 +114,8 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, attr->sample_type |= PERF_SAMPLE_PERIOD; if (!opts->sample_id_all_missing && - (opts->sample_time || opts->target.system_wide || - !opts->no_inherit || opts->target.cpu_list)) + (opts->sample_time || !opts->no_inherit || + !perf_target__no_cpu(&opts->target))) attr->sample_type |= PERF_SAMPLE_TIME; if (opts->raw_samples) { @@ -136,8 +136,8 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, attr->mmap = track; attr->comm = track; - if (!opts->target.pid && !opts->target.tid && - !opts->target.system_wide && (!opts->group || evsel == first)) { + if (perf_target__none(&opts->target) && + (!opts->group || evsel == first)) { attr->disabled = 1; attr->enable_on_exec = 1; } diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h index 6fcd01c..127cff3 100644 --- a/tools/perf/util/target.h +++ b/tools/perf/util/target.h @@ -46,4 +46,19 @@ enum perf_target_errno perf_target__parse_uid(struct perf_target *target); int perf_target__strerror(struct perf_target *target, int errnum, char *buf, size_t buflen); +static inline bool perf_target__no_task(struct perf_target *target) +{ + return !target->pid && !target->tid && !target->uid_str; +} + +static inline bool perf_target__no_cpu(struct perf_target *target) +{ + return !target->system_wide && !target->cpu_list; +} + +static inline bool perf_target__none(struct perf_target *target) +{ + return perf_target__no_task(target) && perf_target__no_cpu(target); +} + #endif /* _PERF_TARGET_H */ -- cgit v0.10.2 From 77a6f014e9ae330c747c66bebfddf29abf9b89e9 Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Mon, 7 May 2012 14:09:04 +0900 Subject: perf stat: Use perf_evlist__create_maps Use same function with perf record and top to share the code checks combinations of different switches. Signed-off-by: Namhyung Kim Reviewed-by: David Ahern Cc: David Ahern Cc: Ingo Molnar Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336367344-28071-8-git-send-email-namhyung.kim@lge.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index d9ff246..e720ba7 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -175,7 +175,9 @@ static struct perf_event_attr very_very_detailed_attrs[] = { static struct perf_evlist *evsel_list; -static struct perf_target target; +static struct perf_target target = { + .uid = UINT_MAX, +}; static int run_idx = 0; static int run_count = 1; @@ -1205,20 +1207,12 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) perf_target__validate(&target); - evsel_list->threads = thread_map__new_str(target.pid, - target.tid, UINT_MAX); - if (evsel_list->threads == NULL) { - pr_err("Problems finding threads of monitor\n"); - usage_with_options(stat_usage, options); - } - - if (target.system_wide) - evsel_list->cpus = cpu_map__new(target.cpu_list); - else - evsel_list->cpus = cpu_map__dummy_new(); + if (perf_evlist__create_maps(evsel_list, &target) < 0) { + if (!perf_target__no_task(&target)) + pr_err("Problems finding threads of monitor\n"); + if (!perf_target__no_cpu(&target)) + perror("failed to parse CPUs map"); - if (evsel_list->cpus == NULL) { - perror("failed to parse CPUs map"); usage_with_options(stat_usage, options); return -1; } -- cgit v0.10.2 From 80eebd94d2090cf9e13ecdc305a0cf7996fa0070 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Tue, 8 May 2012 10:47:09 -0300 Subject: perf top: Default to system wide using perf_target methods Additionally we were not checking if a cpu list had been provided by the user. Fix that. Reported-by: David Ahern Reported-by: Namhyung Kim Cc: David Ahern Cc: Frederic Weisbecker Cc: Mike Galbraith Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lkml.kernel.org/n/tip-ao3zrouylwmt7h9ikj0krubi@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index e4ca827..c53cdab 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1270,8 +1270,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) goto out_delete_evlist; } - if (top.target.tid == 0 && top.target.pid == 0 && - top.target.uid_str == NULL) + if (perf_target__none(&top.target)) top.target.system_wide = true; if (perf_evlist__create_maps(top.evlist, &top.target) < 0) -- cgit v0.10.2 From 04480d01105324dc5b77ca3fbdf85037a7d80dbb Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Wed, 2 May 2012 13:37:07 +0200 Subject: perf report: Fix format string for x86-32 compilation Using PRIu64 for printing out u64 nr_events to fix compilation for x86 32 bits. Cc: Arun Sharma Cc: Corey Ashford Cc: Cyrill Gorcunov Cc: Frank C. Eigler Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Masami Hiramatsu Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Robert Richter Cc: Stephane Eranian Cc: Tom Zanussi Cc: Ulrich Drepper Link: http://lkml.kernel.org/r/1335958638-5160-7-git-send-email-jolsa@redhat.com Signed-off-by: Jiri Olsa Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 5df829f..7477655 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -304,7 +304,7 @@ static size_t hists__fprintf_nr_sample_events(struct hists *self, if (evname != NULL) ret += fprintf(fp, " of event '%s'", evname); - ret += fprintf(fp, "\n# Event count (approx.): %lu", nr_events); + ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events); return ret + fprintf(fp, "\n#\n"); } -- cgit v0.10.2 From 028d455b12719a48b1c4b51ce07a074135726f8f Mon Sep 17 00:00:00 2001 From: David Ahern Date: Tue, 8 May 2012 09:28:57 -0600 Subject: perf record: Fix fallback to cpu-clock on ppc perf-record on PPC is not falling back to cpu-clock: $ perf record -ag -fo /tmp/perf.data -- sleep 1 Error: sys_perf_event_open() syscall returned with 6 (No such device or address). /bin/dmesg may provide additional information. Fatal: No CONFIG_PERF_EVENTS=y kernel support configured? The problem is that until 2.6.37 (behavior changed with commit b0a873e) perf on PPC returns ENXIO when hw_perf_event_init() fails. With this patch we get the expected behavior: $ perf record -ag -fo /tmp/perf.data -v -- sleep 1 Old kernel, cannot exclude guest or host samples. The cycles event is not supported, trying to fall back to cpu-clock-ticks [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.151 MB /tmp/perf.data (~6592 samples) ] Signed-off-by: David Ahern Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336490937-57106-1-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 42e2414..1a9098c 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -242,9 +242,13 @@ try_again: /* * If it's cycles then fall back to hrtimer * based cpu-clock-tick sw counter, which - * is always available even if no PMU support: + * is always available even if no PMU support. + * + * PPC returns ENXIO until 2.6.37 (behavior changed + * with commit b0a873e). */ - if (err == ENOENT && attr->type == PERF_TYPE_HARDWARE + if ((err == ENOENT || err == ENXIO) + && attr->type == PERF_TYPE_HARDWARE && attr->config == PERF_COUNT_HW_CPU_CYCLES) { if (verbose) -- cgit v0.10.2 From 979987a567d9e666fe719f337409b2fbb6418f5f Mon Sep 17 00:00:00 2001 From: David Ahern Date: Tue, 8 May 2012 09:29:16 -0600 Subject: perf stat: handle ENXIO error for perf_event_open perf stat on PPC currently fails to run: $ perf stat -- sleep 1 Error: open_counter returned with 6 (No such device or address). /bin/dmesg may provide additional information. Fatal: Not all events could be opened. The problem is that until 2.6.37 (behavior changed with commit b0a873e) perf on PPC returns ENXIO when hw_perf_event_init() fails. With this patch we get the expected behavior: $ perf stat -v -- sleep 1 cycles event is not supported by the kernel. stalled-cycles-frontend event is not supported by the kernel. stalled-cycles-backend event is not supported by the kernel. instructions event is not supported by the kernel. branches event is not supported by the kernel. branch-misses event is not supported by the kernel. ... Signed-off-by: David Ahern Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/1336490956-57145-1-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index e720ba7..d060568 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -462,8 +462,13 @@ static int run_perf_stat(int argc __used, const char **argv) list_for_each_entry(counter, &evsel_list->entries, node) { if (create_perf_stat_counter(counter, first) < 0) { + /* + * PPC returns ENXIO for HW counters until 2.6.37 + * (behavior changed with commit b0a873e). + */ if (errno == EINVAL || errno == ENOSYS || - errno == ENOENT || errno == EOPNOTSUPP) { + errno == ENOENT || errno == EOPNOTSUPP || + errno == ENXIO) { if (verbose) ui__warning("%s event is not supported by the kernel.\n", event_name(counter)); -- cgit v0.10.2 From 40491eaa46a693e8c6ef94102350a747c63e584d Mon Sep 17 00:00:00 2001 From: David Ahern Date: Tue, 8 May 2012 10:49:49 -0600 Subject: perf top: Update event name when falling back to cpu-clock The 'perf top' command falls back to cpu-clock if the H/W cycles event is not supported, but the event name is not updated leading to a misleading header: PerfTop: 8 irqs/sec kernel:75.0% exact: 0.0% [1000Hz cycles], ... Update the event name when the event type is changed so that the header displays correctly: PerfTop: 794 irqs/sec kernel:100.0% exact: 0.0% [1000Hz cpu-clock], ... Signed-off-by: David Ahern Link: http://lkml.kernel.org/r/1336495789-58420-1-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index c53cdab..4eb6171 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -948,6 +948,10 @@ try_again: attr->type = PERF_TYPE_SOFTWARE; attr->config = PERF_COUNT_SW_CPU_CLOCK; + if (counter->name) { + free(counter->name); + counter->name = strdup(event_name(counter)); + } goto try_again; } -- cgit v0.10.2 From d1cae34d6fda59391e1b06ac1642ef4a740ba3ef Mon Sep 17 00:00:00 2001 From: David Ahern Date: Tue, 8 May 2012 10:50:11 -0600 Subject: perf record: Reset event name when falling back to cpu-clock perf-record defaults to the H/W cycles event and if it is not supported falls back to cpu-clock. Reset the event name as well. Signed-off-by: David Ahern Link: http://lkml.kernel.org/r/1336495811-58461-1-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 1a9098c..d19058a 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -256,6 +256,10 @@ try_again: "trying to fall back to cpu-clock-ticks\n"); attr->type = PERF_TYPE_SOFTWARE; attr->config = PERF_COUNT_SW_CPU_CLOCK; + if (pos->name) { + free(pos->name); + pos->name = NULL; + } goto try_again; } -- cgit v0.10.2 From f6c1be2711333b40d7940ff87e3509b4cd278195 Mon Sep 17 00:00:00 2001 From: David Ahern Date: Tue, 8 May 2012 15:01:19 -0600 Subject: perf annotate: shorten helpline so it fits in visible space Additional toggles have pushed the help line out of view on a modestly sized terminal (120 columns wide). Shorten it to just reminders. Signed-off-by: David Ahern Link: http://lkml.kernel.org/r/1336510879-64610-1-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/annotate.c index 4db5186..a299290 100644 --- a/tools/perf/ui/browsers/annotate.c +++ b/tools/perf/ui/browsers/annotate.c @@ -491,9 +491,9 @@ static int annotate_browser__run(struct annotate_browser *self, int evidx, struct map_symbol *ms = self->b.priv; struct symbol *sym = ms->sym; const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, " - "H: Go to hottest line, ->/ENTER: Line action, " - "O: Toggle offset view, " - "S: Toggle source code view"; + "H: Hottest line, ->/ENTER: Line action, " + "O: Offset view, " + "S: Source view"; int key; if (ui_browser__show(&self->b, sym->name, help) < 0) -- cgit v0.10.2 From 5a5626b1b4bf8467891c9297ffda979db97ed5ec Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Wed, 9 May 2012 12:21:22 -0300 Subject: perf hists browser: Use '/' for search/filter instead of 's' That is what is used in vi and mutt, and as well on the 'annotate' browser. Eventually we can have keymappings to make people used to other key associations more confortable. Suggested-by: Ingo Molnar Cc: David Ahern Cc: Frederic Weisbecker Cc: Mike Galbraith Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lkml.kernel.org/n/tip-fyln9286b8gx5q4n277l0djs@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c index 466827e..a372a4b 100644 --- a/tools/perf/ui/browsers/hists.c +++ b/tools/perf/ui/browsers/hists.c @@ -941,7 +941,7 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events, goto zoom_dso; case 't': goto zoom_thread; - case 's': + case '/': if (ui_browser__input_window("Symbol to show", "Please enter the name of symbol you want to see", buf, "ENTER: OK, ESC: Cancel", @@ -969,7 +969,7 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events, "E Expand all callchains\n" "d Zoom into current DSO\n" "t Zoom into current Thread\n" - "s Filter symbol by name"); + "/ Filter symbol by name"); continue; case K_ENTER: case K_RIGHT: -- cgit v0.10.2