From 69aad6f1ee69546dea8535ab8f3da9f445d57328 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 16:39:04 -0200 Subject: perf tools: Introduce event selectors Out of ad-hoc code and global arrays with hard coded sizes. This is the first step on having a library that will be first used on regression tests in the 'perf test' tool. [acme@felicio linux]$ size /tmp/perf.before text data bss dec hex filename 1273776 97384 5104416 6475576 62cf38 /tmp/perf.before [acme@felicio linux]$ size /tmp/perf.new text data bss dec hex filename 1275422 97416 1392416 2765254 2a31c6 /tmp/perf.new Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 175 ++++++++++++++++++++++++++++------------------ 1 file changed, 107 insertions(+), 68 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 7ff746da7e6..511ebaff9a6 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -43,6 +43,7 @@ #include "util/parse-options.h" #include "util/parse-events.h" #include "util/event.h" +#include "util/evsel.h" #include "util/debug.h" #include "util/header.h" #include "util/cpumap.h" @@ -52,6 +53,8 @@ #include #include +#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) + #define DEFAULT_SEPARATOR " " static struct perf_event_attr default_attrs[] = { @@ -90,16 +93,11 @@ static const char *cpu_list; static const char *csv_sep = NULL; static bool csv_output = false; - -static int *fd[MAX_NR_CPUS][MAX_COUNTERS]; - -static int event_scaled[MAX_COUNTERS]; - -static struct { +struct cpu_counts { u64 val; u64 ena; u64 run; -} cpu_counts[MAX_NR_CPUS][MAX_COUNTERS]; +}; static volatile int done = 0; @@ -108,6 +106,26 @@ struct stats double n, mean, M2; }; +struct perf_stat { + struct stats res_stats[3]; + int scaled; + struct cpu_counts cpu_counts[]; +}; + +static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel, int ncpus) +{ + size_t priv_size = (sizeof(struct perf_stat) + + (ncpus * sizeof(struct cpu_counts))); + evsel->priv = zalloc(priv_size); + return evsel->priv == NULL ? -ENOMEM : 0; +} + +static void perf_evsel__free_stat_priv(struct perf_evsel *evsel) +{ + free(evsel->priv); + evsel->priv = NULL; +} + static void update_stats(struct stats *stats, u64 val) { double delta; @@ -147,22 +165,21 @@ static double stddev_stats(struct stats *stats) return sqrt(variance_mean); } -struct stats event_res_stats[MAX_COUNTERS][3]; struct stats runtime_nsecs_stats[MAX_NR_CPUS]; struct stats runtime_cycles_stats[MAX_NR_CPUS]; struct stats runtime_branches_stats[MAX_NR_CPUS]; struct stats walltime_nsecs_stats; -#define MATCH_EVENT(t, c, counter) \ - (attrs[counter].type == PERF_TYPE_##t && \ - attrs[counter].config == PERF_COUNT_##c) +#define MATCH_EVENT(t, c, evsel) \ + (evsel->attr.type == PERF_TYPE_##t && \ + evsel->attr.config == PERF_COUNT_##c) #define ERR_PERF_OPEN \ "counter %d, sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information." -static int create_perf_stat_counter(int counter, bool *perm_err) +static int create_perf_stat_counter(struct perf_evsel *evsel, bool *perm_err) { - struct perf_event_attr *attr = attrs + counter; + struct perf_event_attr *attr = &evsel->attr; int thread; int ncreated = 0; @@ -174,13 +191,13 @@ static int create_perf_stat_counter(int counter, bool *perm_err) int cpu; for (cpu = 0; cpu < nr_cpus; cpu++) { - fd[cpu][counter][0] = sys_perf_event_open(attr, + FD(evsel, cpu, 0) = sys_perf_event_open(attr, -1, cpumap[cpu], -1, 0); - if (fd[cpu][counter][0] < 0) { + if (FD(evsel, cpu, 0) < 0) { if (errno == EPERM || errno == EACCES) *perm_err = true; - error(ERR_PERF_OPEN, counter, - fd[cpu][counter][0], strerror(errno)); + error(ERR_PERF_OPEN, evsel->idx, + FD(evsel, cpu, 0), strerror(errno)); } else { ++ncreated; } @@ -192,13 +209,13 @@ static int create_perf_stat_counter(int counter, bool *perm_err) attr->enable_on_exec = 1; } for (thread = 0; thread < thread_num; thread++) { - fd[0][counter][thread] = sys_perf_event_open(attr, + FD(evsel, 0, thread) = sys_perf_event_open(attr, all_tids[thread], -1, -1, 0); - if (fd[0][counter][thread] < 0) { + if (FD(evsel, 0, thread) < 0) { if (errno == EPERM || errno == EACCES) *perm_err = true; - error(ERR_PERF_OPEN, counter, - fd[0][counter][thread], + error(ERR_PERF_OPEN, evsel->idx, + FD(evsel, 0, thread), strerror(errno)); } else { ++ncreated; @@ -212,7 +229,7 @@ static int create_perf_stat_counter(int counter, bool *perm_err) /* * Does the counter have nsecs as a unit? */ -static inline int nsec_counter(int counter) +static inline int nsec_counter(struct perf_evsel *counter) { if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) || MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) @@ -225,8 +242,9 @@ static inline int nsec_counter(int counter) * Read out the results of a single counter: * aggregate counts across CPUs in system-wide mode */ -static void read_counter_aggr(int counter) +static void read_counter_aggr(struct perf_evsel *counter) { + struct perf_stat *ps = counter->priv; u64 count[3], single_count[3]; int cpu; size_t res, nv; @@ -238,15 +256,15 @@ static void read_counter_aggr(int counter) nv = scale ? 3 : 1; for (cpu = 0; cpu < nr_cpus; cpu++) { for (thread = 0; thread < thread_num; thread++) { - if (fd[cpu][counter][thread] < 0) + if (FD(counter, cpu, thread) < 0) continue; - res = read(fd[cpu][counter][thread], + res = read(FD(counter, cpu, thread), single_count, nv * sizeof(u64)); assert(res == nv * sizeof(u64)); - close(fd[cpu][counter][thread]); - fd[cpu][counter][thread] = -1; + close(FD(counter, cpu, thread)); + FD(counter, cpu, thread) = -1; count[0] += single_count[0]; if (scale) { @@ -259,20 +277,20 @@ static void read_counter_aggr(int counter) scaled = 0; if (scale) { if (count[2] == 0) { - event_scaled[counter] = -1; + ps->scaled = -1; count[0] = 0; return; } if (count[2] < count[1]) { - event_scaled[counter] = 1; + ps->scaled = 1; count[0] = (unsigned long long) ((double)count[0] * count[1] / count[2] + 0.5); } } for (i = 0; i < 3; i++) - update_stats(&event_res_stats[counter][i], count[i]); + update_stats(&ps->res_stats[i], count[i]); if (verbose) { fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter), @@ -294,8 +312,9 @@ static void read_counter_aggr(int counter) * Read out the results of a single counter: * do not aggregate counts across CPUs in system-wide mode */ -static void read_counter(int counter) +static void read_counter(struct perf_evsel *counter) { + struct cpu_counts *cpu_counts = counter->priv; u64 count[3]; int cpu; size_t res, nv; @@ -306,15 +325,15 @@ static void read_counter(int counter) for (cpu = 0; cpu < nr_cpus; cpu++) { - if (fd[cpu][counter][0] < 0) + if (FD(counter, cpu, 0) < 0) continue; - res = read(fd[cpu][counter][0], count, nv * sizeof(u64)); + res = read(FD(counter, cpu, 0), count, nv * sizeof(u64)); assert(res == nv * sizeof(u64)); - close(fd[cpu][counter][0]); - fd[cpu][counter][0] = -1; + close(FD(counter, cpu, 0)); + FD(counter, cpu, 0) = -1; if (scale) { if (count[2] == 0) { @@ -324,9 +343,9 @@ static void read_counter(int counter) ((double)count[0] * count[1] / count[2] + 0.5); } } - cpu_counts[cpu][counter].val = count[0]; /* scaled count */ - cpu_counts[cpu][counter].ena = count[1]; - cpu_counts[cpu][counter].run = count[2]; + cpu_counts[cpu].val = count[0]; /* scaled count */ + cpu_counts[cpu].ena = count[1]; + cpu_counts[cpu].run = count[2]; if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) update_stats(&runtime_nsecs_stats[cpu], count[0]); @@ -340,8 +359,9 @@ static void read_counter(int counter) static int run_perf_stat(int argc __used, const char **argv) { unsigned long long t0, t1; + struct perf_evsel *counter; int status = 0; - int counter, ncreated = 0; + int ncreated = 0; int child_ready_pipe[2], go_pipe[2]; bool perm_err = false; const bool forks = (argc > 0); @@ -401,7 +421,7 @@ static int run_perf_stat(int argc __used, const char **argv) close(child_ready_pipe[0]); } - for (counter = 0; counter < nr_counters; counter++) + list_for_each_entry(counter, &evsel_list, node) ncreated += create_perf_stat_counter(counter, &perm_err); if (ncreated < nr_counters) { @@ -433,25 +453,28 @@ static int run_perf_stat(int argc __used, const char **argv) update_stats(&walltime_nsecs_stats, t1 - t0); if (no_aggr) { - for (counter = 0; counter < nr_counters; counter++) + list_for_each_entry(counter, &evsel_list, node) read_counter(counter); } else { - for (counter = 0; counter < nr_counters; counter++) + list_for_each_entry(counter, &evsel_list, node) read_counter_aggr(counter); } return WEXITSTATUS(status); } -static void print_noise(int counter, double avg) +static void print_noise(struct perf_evsel *evsel, double avg) { + struct perf_stat *ps; + if (run_count == 1) return; + ps = evsel->priv; fprintf(stderr, " ( +- %7.3f%% )", - 100 * stddev_stats(&event_res_stats[counter][0]) / avg); + 100 * stddev_stats(&ps->res_stats[0]) / avg); } -static void nsec_printout(int cpu, int counter, double avg) +static void nsec_printout(int cpu, struct perf_evsel *counter, double avg) { double msecs = avg / 1e6; char cpustr[16] = { '\0', }; @@ -473,7 +496,7 @@ static void nsec_printout(int cpu, int counter, double avg) } } -static void abs_printout(int cpu, int counter, double avg) +static void abs_printout(int cpu, struct perf_evsel *counter, double avg) { double total, ratio = 0.0; char cpustr[16] = { '\0', }; @@ -528,10 +551,11 @@ static void abs_printout(int cpu, int counter, double avg) * Print out the results of a single counter: * aggregated counts in system-wide mode */ -static void print_counter_aggr(int counter) +static void print_counter_aggr(struct perf_evsel *counter) { - double avg = avg_stats(&event_res_stats[counter][0]); - int scaled = event_scaled[counter]; + struct perf_stat *ps = counter->priv; + double avg = avg_stats(&ps->res_stats[0]); + int scaled = ps->scaled; if (scaled == -1) { fprintf(stderr, "%*s%s%-24s\n", @@ -555,8 +579,8 @@ static void print_counter_aggr(int counter) if (scaled) { double avg_enabled, avg_running; - avg_enabled = avg_stats(&event_res_stats[counter][1]); - avg_running = avg_stats(&event_res_stats[counter][2]); + avg_enabled = avg_stats(&ps->res_stats[1]); + avg_running = avg_stats(&ps->res_stats[2]); fprintf(stderr, " (scaled from %.2f%%)", 100 * avg_running / avg_enabled); @@ -569,15 +593,16 @@ static void print_counter_aggr(int counter) * Print out the results of a single counter: * does not use aggregated count in system-wide */ -static void print_counter(int counter) +static void print_counter(struct perf_evsel *counter) { + struct perf_stat *ps = counter->priv; u64 ena, run, val; int cpu; for (cpu = 0; cpu < nr_cpus; cpu++) { - val = cpu_counts[cpu][counter].val; - ena = cpu_counts[cpu][counter].ena; - run = cpu_counts[cpu][counter].run; + val = ps->cpu_counts[cpu].val; + ena = ps->cpu_counts[cpu].ena; + run = ps->cpu_counts[cpu].run; if (run == 0 || ena == 0) { fprintf(stderr, "CPU%*d%s%*s%s%-24s", csv_output ? 0 : -4, @@ -609,7 +634,8 @@ static void print_counter(int counter) static void print_stat(int argc, const char **argv) { - int i, counter; + struct perf_evsel *counter; + int i; fflush(stdout); @@ -632,10 +658,10 @@ static void print_stat(int argc, const char **argv) } if (no_aggr) { - for (counter = 0; counter < nr_counters; counter++) + list_for_each_entry(counter, &evsel_list, node) print_counter(counter); } else { - for (counter = 0; counter < nr_counters; counter++) + list_for_each_entry(counter, &evsel_list, node) print_counter_aggr(counter); } @@ -720,8 +746,8 @@ static const struct option options[] = { int cmd_stat(int argc, const char **argv, const char *prefix __used) { - int status; - int i,j; + struct perf_evsel *pos; + int status = -ENOMEM; setlocale(LC_ALL, ""); @@ -757,8 +783,18 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) /* Set attrs and nr_counters if no event is selected and !null_run */ if (!null_run && !nr_counters) { - memcpy(attrs, default_attrs, sizeof(default_attrs)); + size_t c; + nr_counters = ARRAY_SIZE(default_attrs); + + for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) { + pos = perf_evsel__new(default_attrs[c].type, + default_attrs[c].config, + nr_counters); + if (pos == NULL) + goto out; + list_add(&pos->node, &evsel_list); + } } if (system_wide) @@ -786,12 +822,10 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) thread_num = 1; } - for (i = 0; i < MAX_NR_CPUS; i++) { - for (j = 0; j < MAX_COUNTERS; j++) { - fd[i][j] = malloc(sizeof(int)*thread_num); - if (!fd[i][j]) - return -ENOMEM; - } + list_for_each_entry(pos, &evsel_list, node) { + if (perf_evsel__alloc_stat_priv(pos, nr_cpus) < 0 || + perf_evsel__alloc_fd(pos, nr_cpus, thread_num) < 0) + goto out_free_fd; } /* @@ -814,6 +848,11 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) if (status != -1) print_stat(argc, argv); - +out_free_fd: + list_for_each_entry(pos, &evsel_list, node) { + perf_evsel__free_fd(pos); + perf_evsel__free_stat_priv(pos); + } +out: return status; } -- cgit v1.2.3-70-g09d2 From daec78a09de3df5fbfbbd167da0304d49d7fcfe5 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 16:49:44 -0200 Subject: perf evsel: Adopt MATCH_EVENT macro from 'stat' Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 37 ++++++++++++++++--------------------- tools/perf/util/evsel.h | 4 ++++ 2 files changed, 20 insertions(+), 21 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 511ebaff9a6..3e5f356a524 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -170,10 +170,6 @@ struct stats runtime_cycles_stats[MAX_NR_CPUS]; struct stats runtime_branches_stats[MAX_NR_CPUS]; struct stats walltime_nsecs_stats; -#define MATCH_EVENT(t, c, evsel) \ - (evsel->attr.type == PERF_TYPE_##t && \ - evsel->attr.config == PERF_COUNT_##c) - #define ERR_PERF_OPEN \ "counter %d, sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information." @@ -229,10 +225,10 @@ static int create_perf_stat_counter(struct perf_evsel *evsel, bool *perm_err) /* * Does the counter have nsecs as a unit? */ -static inline int nsec_counter(struct perf_evsel *counter) +static inline int nsec_counter(struct perf_evsel *evsel) { - if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) || - MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) + if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || + perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) return 1; return 0; @@ -300,11 +296,11 @@ static void read_counter_aggr(struct perf_evsel *counter) /* * Save the full runtime - to allow normalization during printout: */ - if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) + if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK)) update_stats(&runtime_nsecs_stats[0], count[0]); - if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) + if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) update_stats(&runtime_cycles_stats[0], count[0]); - if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter)) + if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) update_stats(&runtime_branches_stats[0], count[0]); } @@ -347,11 +343,11 @@ static void read_counter(struct perf_evsel *counter) cpu_counts[cpu].ena = count[1]; cpu_counts[cpu].run = count[2]; - if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) + if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK)) update_stats(&runtime_nsecs_stats[cpu], count[0]); - if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) + if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) update_stats(&runtime_cycles_stats[cpu], count[0]); - if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter)) + if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) update_stats(&runtime_branches_stats[cpu], count[0]); } } @@ -474,7 +470,7 @@ static void print_noise(struct perf_evsel *evsel, double avg) 100 * stddev_stats(&ps->res_stats[0]) / avg); } -static void nsec_printout(int cpu, struct perf_evsel *counter, double avg) +static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg) { double msecs = avg / 1e6; char cpustr[16] = { '\0', }; @@ -485,18 +481,17 @@ static void nsec_printout(int cpu, struct perf_evsel *counter, double avg) csv_output ? 0 : -4, cpumap[cpu], csv_sep); - fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(counter)); + fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel)); if (csv_output) return; - if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) { + if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) fprintf(stderr, " # %10.3f CPUs ", avg / avg_stats(&walltime_nsecs_stats)); - } } -static void abs_printout(int cpu, struct perf_evsel *counter, double avg) +static void abs_printout(int cpu, struct perf_evsel *evsel, double avg) { double total, ratio = 0.0; char cpustr[16] = { '\0', }; @@ -516,19 +511,19 @@ static void abs_printout(int cpu, struct perf_evsel *counter, double avg) else cpu = 0; - fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(counter)); + fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel)); if (csv_output) return; - if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) { + if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) { total = avg_stats(&runtime_cycles_stats[cpu]); if (total) ratio = avg / total; fprintf(stderr, " # %10.3f IPC ", ratio); - } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) && + } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) && runtime_branches_stats[cpu].n != 0) { total = avg_stats(&runtime_branches_stats[cpu]); diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 3eb3989a211..8a5cfb65667 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -21,4 +21,8 @@ void perf_evsel__delete(struct perf_evsel *evsel); int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads); void perf_evsel__free_fd(struct perf_evsel *evsel); +#define perf_evsel__match(evsel, t, c) \ + (evsel->attr.type == PERF_TYPE_##t && \ + evsel->attr.config == PERF_COUNT_##c) + #endif /* __PERF_EVSEL_H */ -- cgit v1.2.3-70-g09d2 From 70d544d0576775a2b3923a7e68cb49b0313d80c9 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 16:51:39 -0200 Subject: perf evsel: Delete the event selectors at exit Freeing all the possibly allocated resources, reducing complexity on each tool exit path. Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-record.c | 2 -- tools/perf/builtin-stat.c | 4 +--- tools/perf/builtin-top.c | 4 +--- tools/perf/perf.c | 2 ++ tools/perf/util/parse-events.c | 11 +++++++++++ tools/perf/util/parse-events.h | 1 + 6 files changed, 16 insertions(+), 8 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index e68aee33bc1..052de1780f7 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -965,8 +965,6 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) out_free_event_array: free(event_array); out_free_fd: - list_for_each_entry(pos, &evsel_list, node) - perf_evsel__free_fd(pos); free(all_tids); all_tids = NULL; out_symbol_exit: diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 3e5f356a524..589ba3a9242 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -844,10 +844,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) if (status != -1) print_stat(argc, argv); out_free_fd: - list_for_each_entry(pos, &evsel_list, node) { - perf_evsel__free_fd(pos); + list_for_each_entry(pos, &evsel_list, node) perf_evsel__free_stat_priv(pos); - } out: return status; } diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 13a836efa1e..27b9c14a0a0 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1495,10 +1495,8 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) status = __cmd_top(); out_free_fd: - list_for_each_entry(pos, &evsel_list, node) { - perf_evsel__free_fd(pos); + list_for_each_entry(pos, &evsel_list, node) perf_evsel__free_mmap(pos); - } return status; } diff --git a/tools/perf/perf.c b/tools/perf/perf.c index 595d0f4a710..5b1ecd66bb3 100644 --- a/tools/perf/perf.c +++ b/tools/perf/perf.c @@ -286,6 +286,8 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) status = p->fn(argc, argv, prefix); exit_browser(status); + perf_evsel_list__delete(); + if (status) return status & 0xff; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 2d948ad471f..3a142e90d60 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -982,3 +982,14 @@ int perf_evsel_list__create_default(void) ++nr_counters; return 0; } + +void perf_evsel_list__delete(void) +{ + struct perf_evsel *pos, *n; + + list_for_each_entry_safe(pos, n, &evsel_list, node) { + list_del_init(&pos->node); + perf_evsel__delete(pos); + } + nr_counters = 0; +} diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index 0f915a01a3f..0a0abc1d10e 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -12,6 +12,7 @@ struct perf_evsel; extern struct list_head evsel_list; int perf_evsel_list__create_default(void); +void perf_evsel_list__delete(void); struct option; -- cgit v1.2.3-70-g09d2 From c52b12ed2511e6c031a0295fd903ea72b93701fb Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 17:45:52 -0200 Subject: perf evsel: Steal the counter reading routines from stat Making them hopefully generic enough to be used in 'perf test', well see. Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 121 +++++++++++----------------------------------- tools/perf/util/evsel.c | 88 +++++++++++++++++++++++++++++++++ tools/perf/util/evsel.h | 79 ++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 92 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 589ba3a9242..a8b00b44b3c 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -93,12 +93,6 @@ static const char *cpu_list; static const char *csv_sep = NULL; static bool csv_output = false; -struct cpu_counts { - u64 val; - u64 ena; - u64 run; -}; - static volatile int done = 0; struct stats @@ -108,15 +102,11 @@ struct stats struct perf_stat { struct stats res_stats[3]; - int scaled; - struct cpu_counts cpu_counts[]; }; -static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel, int ncpus) +static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel) { - size_t priv_size = (sizeof(struct perf_stat) + - (ncpus * sizeof(struct cpu_counts))); - evsel->priv = zalloc(priv_size); + evsel->priv = zalloc(sizeof(struct perf_stat)); return evsel->priv == NULL ? -ENOMEM : 0; } @@ -238,52 +228,14 @@ static inline int nsec_counter(struct perf_evsel *evsel) * Read out the results of a single counter: * aggregate counts across CPUs in system-wide mode */ -static void read_counter_aggr(struct perf_evsel *counter) +static int read_counter_aggr(struct perf_evsel *counter) { struct perf_stat *ps = counter->priv; - u64 count[3], single_count[3]; - int cpu; - size_t res, nv; - int scaled; - int i, thread; - - count[0] = count[1] = count[2] = 0; - - nv = scale ? 3 : 1; - for (cpu = 0; cpu < nr_cpus; cpu++) { - for (thread = 0; thread < thread_num; thread++) { - if (FD(counter, cpu, thread) < 0) - continue; - - res = read(FD(counter, cpu, thread), - single_count, nv * sizeof(u64)); - assert(res == nv * sizeof(u64)); - - close(FD(counter, cpu, thread)); - FD(counter, cpu, thread) = -1; - - count[0] += single_count[0]; - if (scale) { - count[1] += single_count[1]; - count[2] += single_count[2]; - } - } - } - - scaled = 0; - if (scale) { - if (count[2] == 0) { - ps->scaled = -1; - count[0] = 0; - return; - } + u64 *count = counter->counts->aggr.values; + int i; - if (count[2] < count[1]) { - ps->scaled = 1; - count[0] = (unsigned long long) - ((double)count[0] * count[1] / count[2] + 0.5); - } - } + if (__perf_evsel__read(counter, nr_cpus, thread_num, scale) < 0) + return -1; for (i = 0; i < 3; i++) update_stats(&ps->res_stats[i], count[i]); @@ -302,46 +254,24 @@ static void read_counter_aggr(struct perf_evsel *counter) update_stats(&runtime_cycles_stats[0], count[0]); if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) update_stats(&runtime_branches_stats[0], count[0]); + + return 0; } /* * Read out the results of a single counter: * do not aggregate counts across CPUs in system-wide mode */ -static void read_counter(struct perf_evsel *counter) +static int read_counter(struct perf_evsel *counter) { - struct cpu_counts *cpu_counts = counter->priv; - u64 count[3]; + u64 *count; int cpu; - size_t res, nv; - - count[0] = count[1] = count[2] = 0; - - nv = scale ? 3 : 1; for (cpu = 0; cpu < nr_cpus; cpu++) { + if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0) + return -1; - if (FD(counter, cpu, 0) < 0) - continue; - - res = read(FD(counter, cpu, 0), count, nv * sizeof(u64)); - - assert(res == nv * sizeof(u64)); - - close(FD(counter, cpu, 0)); - FD(counter, cpu, 0) = -1; - - if (scale) { - if (count[2] == 0) { - count[0] = 0; - } else if (count[2] < count[1]) { - count[0] = (unsigned long long) - ((double)count[0] * count[1] / count[2] + 0.5); - } - } - cpu_counts[cpu].val = count[0]; /* scaled count */ - cpu_counts[cpu].ena = count[1]; - cpu_counts[cpu].run = count[2]; + count = counter->counts->cpu[cpu].values; if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK)) update_stats(&runtime_nsecs_stats[cpu], count[0]); @@ -350,6 +280,8 @@ static void read_counter(struct perf_evsel *counter) if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) update_stats(&runtime_branches_stats[cpu], count[0]); } + + return 0; } static int run_perf_stat(int argc __used, const char **argv) @@ -449,12 +381,17 @@ static int run_perf_stat(int argc __used, const char **argv) update_stats(&walltime_nsecs_stats, t1 - t0); if (no_aggr) { - list_for_each_entry(counter, &evsel_list, node) + list_for_each_entry(counter, &evsel_list, node) { read_counter(counter); + perf_evsel__close_fd(counter, nr_cpus, 1); + } } else { - list_for_each_entry(counter, &evsel_list, node) + list_for_each_entry(counter, &evsel_list, node) { read_counter_aggr(counter); + perf_evsel__close_fd(counter, nr_cpus, thread_num); + } } + return WEXITSTATUS(status); } @@ -550,7 +487,7 @@ static void print_counter_aggr(struct perf_evsel *counter) { struct perf_stat *ps = counter->priv; double avg = avg_stats(&ps->res_stats[0]); - int scaled = ps->scaled; + int scaled = counter->counts->scaled; if (scaled == -1) { fprintf(stderr, "%*s%s%-24s\n", @@ -590,14 +527,13 @@ static void print_counter_aggr(struct perf_evsel *counter) */ static void print_counter(struct perf_evsel *counter) { - struct perf_stat *ps = counter->priv; u64 ena, run, val; int cpu; for (cpu = 0; cpu < nr_cpus; cpu++) { - val = ps->cpu_counts[cpu].val; - ena = ps->cpu_counts[cpu].ena; - run = ps->cpu_counts[cpu].run; + val = counter->counts->cpu[cpu].val; + ena = counter->counts->cpu[cpu].ena; + run = counter->counts->cpu[cpu].run; if (run == 0 || ena == 0) { fprintf(stderr, "CPU%*d%s%*s%s%-24s", csv_output ? 0 : -4, @@ -818,7 +754,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) } list_for_each_entry(pos, &evsel_list, node) { - if (perf_evsel__alloc_stat_priv(pos, nr_cpus) < 0 || + if (perf_evsel__alloc_stat_priv(pos) < 0 || + perf_evsel__alloc_counts(pos, nr_cpus) < 0 || perf_evsel__alloc_fd(pos, nr_cpus, thread_num) < 0) goto out_free_fd; } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 6539ec912c7..3f5de519623 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1,6 +1,8 @@ #include "evsel.h" #include "util.h" +#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) + struct perf_evsel *perf_evsel__new(u32 type, u64 config, int idx) { struct perf_evsel *evsel = zalloc(sizeof(*evsel)); @@ -21,15 +23,101 @@ int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) return evsel->fd != NULL ? 0 : -ENOMEM; } +int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) +{ + evsel->counts = zalloc((sizeof(*evsel->counts) + + (ncpus * sizeof(struct perf_counts_values)))); + return evsel->counts != NULL ? 0 : -ENOMEM; +} + void perf_evsel__free_fd(struct perf_evsel *evsel) { xyarray__delete(evsel->fd); evsel->fd = NULL; } +void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) +{ + int cpu, thread; + + for (cpu = 0; cpu < ncpus; cpu++) + for (thread = 0; thread < nthreads; ++thread) { + close(FD(evsel, cpu, thread)); + FD(evsel, cpu, thread) = -1; + } +} + void perf_evsel__delete(struct perf_evsel *evsel) { assert(list_empty(&evsel->node)); xyarray__delete(evsel->fd); free(evsel); } + +int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, + int cpu, int thread, bool scale) +{ + struct perf_counts_values count; + size_t nv = scale ? 3 : 1; + + if (FD(evsel, cpu, thread) < 0) + return -EINVAL; + + if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) + return -errno; + + if (scale) { + if (count.run == 0) + count.val = 0; + else if (count.run < count.ena) + count.val = (u64)((double)count.val * count.ena / count.run + 0.5); + } else + count.ena = count.run = 0; + + evsel->counts->cpu[cpu] = count; + return 0; +} + +int __perf_evsel__read(struct perf_evsel *evsel, + int ncpus, int nthreads, bool scale) +{ + size_t nv = scale ? 3 : 1; + int cpu, thread; + struct perf_counts_values *aggr = &evsel->counts->aggr, count; + + aggr->val = 0; + + for (cpu = 0; cpu < ncpus; cpu++) { + for (thread = 0; thread < nthreads; thread++) { + if (FD(evsel, cpu, thread) < 0) + continue; + + if (readn(FD(evsel, cpu, thread), + &count, nv * sizeof(u64)) < 0) + return -errno; + + aggr->val += count.val; + if (scale) { + aggr->ena += count.ena; + aggr->run += count.run; + } + } + } + + evsel->counts->scaled = 0; + if (scale) { + if (aggr->run == 0) { + evsel->counts->scaled = -1; + aggr->val = 0; + return 0; + } + + if (aggr->run < aggr->ena) { + evsel->counts->scaled = 1; + aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5); + } + } else + aggr->ena = aggr->run = 0; + + return 0; +} diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 8a5cfb65667..8b48ef1e672 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -2,15 +2,34 @@ #define __PERF_EVSEL_H 1 #include +#include #include #include "types.h" #include "xyarray.h" + +struct perf_counts_values { + union { + struct { + u64 val; + u64 ena; + u64 run; + }; + u64 values[3]; + }; +}; + +struct perf_counts { + s8 scaled; + struct perf_counts_values aggr; + struct perf_counts_values cpu[]; +}; struct perf_evsel { struct list_head node; struct perf_event_attr attr; char *filter; struct xyarray *fd; + struct perf_counts *counts; int idx; void *priv; }; @@ -19,10 +38,70 @@ struct perf_evsel *perf_evsel__new(u32 type, u64 config, int idx); void perf_evsel__delete(struct perf_evsel *evsel); int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads); +int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus); void perf_evsel__free_fd(struct perf_evsel *evsel); +void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads); #define perf_evsel__match(evsel, t, c) \ (evsel->attr.type == PERF_TYPE_##t && \ evsel->attr.config == PERF_COUNT_##c) +int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, + int cpu, int thread, bool scale); + +/** + * perf_evsel__read_on_cpu - Read out the results on a CPU and thread + * + * @evsel - event selector to read value + * @cpu - CPU of interest + * @thread - thread of interest + */ +static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel, + int cpu, int thread) +{ + return __perf_evsel__read_on_cpu(evsel, cpu, thread, false); +} + +/** + * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled + * + * @evsel - event selector to read value + * @cpu - CPU of interest + * @thread - thread of interest + */ +static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel, + int cpu, int thread) +{ + return __perf_evsel__read_on_cpu(evsel, cpu, thread, true); +} + +int __perf_evsel__read(struct perf_evsel *evsel, int ncpus, int nthreads, + bool scale); + +/** + * perf_evsel__read - Read the aggregate results on all CPUs + * + * @evsel - event selector to read value + * @ncpus - Number of cpus affected, from zero + * @nthreads - Number of threads affected, from zero + */ +static inline int perf_evsel__read(struct perf_evsel *evsel, + int ncpus, int nthreads) +{ + return __perf_evsel__read(evsel, ncpus, nthreads, false); +} + +/** + * perf_evsel__read_scaled - Read the aggregate results on all CPUs, scaled + * + * @evsel - event selector to read value + * @ncpus - Number of cpus affected, from zero + * @nthreads - Number of threads affected, from zero + */ +static inline int perf_evsel__read_scaled(struct perf_evsel *evsel, + int ncpus, int nthreads) +{ + return __perf_evsel__read(evsel, ncpus, nthreads, true); +} + #endif /* __PERF_EVSEL_H */ -- cgit v1.2.3-70-g09d2 From 48290609c0d265f5dac0fca6fd4e3c5732542f67 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 17:48:12 -0200 Subject: perf evsel: Introduce per cpu and per thread open helpers Abstracting away the loops needed to create the various event fd handlers. The users have to pass a confiruged perf->evsel.attr field, which is already usable after perf_evsel__new (constructor) time, using defaults. Comes out of the ad-hoc routines in builtin-stat, that now uses it. Fixed a small silly bug where we were die()ing before killing our children, dysfunctional family this one 8-) Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 84 +++++++++++++++-------------------------------- tools/perf/util/evsel.c | 52 +++++++++++++++++++++++++++++ tools/perf/util/evsel.h | 5 +++ 3 files changed, 83 insertions(+), 58 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index a8b00b44b3c..065e79eb214 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -53,8 +53,6 @@ #include #include -#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) - #define DEFAULT_SEPARATOR " " static struct perf_event_attr default_attrs[] = { @@ -160,56 +158,24 @@ struct stats runtime_cycles_stats[MAX_NR_CPUS]; struct stats runtime_branches_stats[MAX_NR_CPUS]; struct stats walltime_nsecs_stats; -#define ERR_PERF_OPEN \ -"counter %d, sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information." - -static int create_perf_stat_counter(struct perf_evsel *evsel, bool *perm_err) +static int create_perf_stat_counter(struct perf_evsel *evsel) { struct perf_event_attr *attr = &evsel->attr; - int thread; - int ncreated = 0; if (scale) attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING; - if (system_wide) { - int cpu; - - for (cpu = 0; cpu < nr_cpus; cpu++) { - FD(evsel, cpu, 0) = sys_perf_event_open(attr, - -1, cpumap[cpu], -1, 0); - if (FD(evsel, cpu, 0) < 0) { - if (errno == EPERM || errno == EACCES) - *perm_err = true; - error(ERR_PERF_OPEN, evsel->idx, - FD(evsel, cpu, 0), strerror(errno)); - } else { - ++ncreated; - } - } - } else { - attr->inherit = !no_inherit; - if (target_pid == -1 && target_tid == -1) { - attr->disabled = 1; - attr->enable_on_exec = 1; - } - for (thread = 0; thread < thread_num; thread++) { - FD(evsel, 0, thread) = sys_perf_event_open(attr, - all_tids[thread], -1, -1, 0); - if (FD(evsel, 0, thread) < 0) { - if (errno == EPERM || errno == EACCES) - *perm_err = true; - error(ERR_PERF_OPEN, evsel->idx, - FD(evsel, 0, thread), - strerror(errno)); - } else { - ++ncreated; - } - } + if (system_wide) + return perf_evsel__open_per_cpu(evsel, nr_cpus, cpumap); + + attr->inherit = !no_inherit; + if (target_pid == -1 && target_tid == -1) { + attr->disabled = 1; + attr->enable_on_exec = 1; } - return ncreated; + return perf_evsel__open_per_thread(evsel, thread_num, all_tids); } /* @@ -289,9 +255,7 @@ static int run_perf_stat(int argc __used, const char **argv) unsigned long long t0, t1; struct perf_evsel *counter; int status = 0; - int ncreated = 0; int child_ready_pipe[2], go_pipe[2]; - bool perm_err = false; const bool forks = (argc > 0); char buf; @@ -349,19 +313,23 @@ static int run_perf_stat(int argc __used, const char **argv) close(child_ready_pipe[0]); } - list_for_each_entry(counter, &evsel_list, node) - ncreated += create_perf_stat_counter(counter, &perm_err); - - if (ncreated < nr_counters) { - if (perm_err) - 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 " : ""); - die("Not all events could be opened.\n"); - if (child_pid != -1) - kill(child_pid, SIGTERM); - return -1; + list_for_each_entry(counter, &evsel_list, node) { + if (create_perf_stat_counter(counter) < 0) { + if (errno == -EPERM || errno == -EACCES) { + 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 " : ""); + } else { + error("open_counter returned with %d (%s). " + "/bin/dmesg may provide additional information.\n", + errno, strerror(errno)); + } + if (child_pid != -1) + kill(child_pid, SIGTERM); + die("Not all events could be opened.\n"); + return -1; + } } /* diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 3f5de519623..e62cc5e050a 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1,4 +1,5 @@ #include "evsel.h" +#include "../perf.h" #include "util.h" #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) @@ -121,3 +122,54 @@ int __perf_evsel__read(struct perf_evsel *evsel, return 0; } + +int perf_evsel__open_per_cpu(struct perf_evsel *evsel, int ncpus, int *cpu_map) +{ + int cpu; + + for (cpu = 0; cpu < ncpus; cpu++) { + FD(evsel, cpu, 0) = sys_perf_event_open(&evsel->attr, -1, + cpu_map[cpu], -1, 0); + if (FD(evsel, cpu, 0) < 0) + goto out_close; + } + + return 0; + +out_close: + while (--cpu >= 0) { + close(FD(evsel, cpu, 0)); + FD(evsel, cpu, 0) = -1; + } + return -1; +} + +int perf_evsel__open_per_thread(struct perf_evsel *evsel, int nthreads, int *thread_map) +{ + int thread; + + for (thread = 0; thread < nthreads; thread++) { + FD(evsel, 0, thread) = sys_perf_event_open(&evsel->attr, + thread_map[thread], -1, -1, 0); + if (FD(evsel, 0, thread) < 0) + goto out_close; + } + + return 0; + +out_close: + while (--thread >= 0) { + close(FD(evsel, 0, thread)); + FD(evsel, 0, thread) = -1; + } + return -1; +} + +int perf_evsel__open(struct perf_evsel *evsel, int ncpus, int nthreads, + int *cpu_map, int *thread_map) +{ + if (nthreads < 0) + return perf_evsel__open_per_cpu(evsel, ncpus, cpu_map); + + return perf_evsel__open_per_thread(evsel, nthreads, thread_map); +} diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 8b48ef1e672..a62fb55cffa 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -42,6 +42,11 @@ int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus); void perf_evsel__free_fd(struct perf_evsel *evsel); void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads); +int perf_evsel__open_per_cpu(struct perf_evsel *evsel, int ncpus, int *cpu_map); +int perf_evsel__open_per_thread(struct perf_evsel *evsel, int nthreads, int *thread_map); +int perf_evsel__open(struct perf_evsel *evsel, int ncpus, int nthreads, + int *cpu_map, int *thread_map); + #define perf_evsel__match(evsel, t, c) \ (evsel->attr.type == PERF_TYPE_##t && \ evsel->attr.config == PERF_COUNT_##c) -- cgit v1.2.3-70-g09d2 From 60d567e2d9187379d642f6aba7c8a52b3fd5d261 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 17:49:48 -0200 Subject: perf tools: Refactor cpumap to hold nr and the map So that later, we can pass the cpu_map instance instead of (nr_cpus, cpu_map) for things like perf_evsel__open and friends. Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-record.c | 14 ++--- tools/perf/builtin-stat.c | 36 ++++++------- tools/perf/builtin-top.c | 22 ++++---- tools/perf/util/cpumap.c | 123 +++++++++++++++++++++++++++++++++----------- tools/perf/util/cpumap.h | 10 +++- 5 files changed, 138 insertions(+), 67 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 052de1780f7..220e6e7f0b9 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -39,7 +39,7 @@ static u64 user_interval = ULLONG_MAX; static u64 default_interval = 0; static u64 sample_type; -static int nr_cpus = 0; +static struct cpu_map *cpus; static unsigned int page_size; static unsigned int mmap_pages = 128; static unsigned int user_freq = UINT_MAX; @@ -670,8 +670,8 @@ static int __cmd_record(int argc, const char **argv) if (!system_wide && no_inherit && !cpu_list) { open_counters(-1); } else { - for (i = 0; i < nr_cpus; i++) - open_counters(cpumap[i]); + for (i = 0; i < cpus->nr; i++) + open_counters(cpus->map[i]); } perf_session__set_sample_type(session, sample_type); @@ -927,14 +927,14 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) thread_num = 1; } - nr_cpus = read_cpu_map(cpu_list); - if (nr_cpus < 1) { - perror("failed to collect number of CPUs"); + cpus = cpu_map__new(cpu_list); + if (cpus == NULL) { + perror("failed to parse CPUs map"); return -1; } list_for_each_entry(pos, &evsel_list, node) { - if (perf_evsel__alloc_fd(pos, nr_cpus, thread_num) < 0) + if (perf_evsel__alloc_fd(pos, cpus->nr, thread_num) < 0) goto out_free_fd; } event_array = malloc( diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 065e79eb214..3f4a431fb5a 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -72,7 +72,7 @@ static struct perf_event_attr default_attrs[] = { }; static bool system_wide = false; -static int nr_cpus = 0; +static struct cpu_map *cpus; static int run_idx = 0; static int run_count = 1; @@ -167,7 +167,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel) PERF_FORMAT_TOTAL_TIME_RUNNING; if (system_wide) - return perf_evsel__open_per_cpu(evsel, nr_cpus, cpumap); + return perf_evsel__open_per_cpu(evsel, cpus->nr, cpus->map); attr->inherit = !no_inherit; if (target_pid == -1 && target_tid == -1) { @@ -200,7 +200,7 @@ static int read_counter_aggr(struct perf_evsel *counter) u64 *count = counter->counts->aggr.values; int i; - if (__perf_evsel__read(counter, nr_cpus, thread_num, scale) < 0) + if (__perf_evsel__read(counter, cpus->nr, thread_num, scale) < 0) return -1; for (i = 0; i < 3; i++) @@ -233,7 +233,7 @@ static int read_counter(struct perf_evsel *counter) u64 *count; int cpu; - for (cpu = 0; cpu < nr_cpus; cpu++) { + for (cpu = 0; cpu < cpus->nr; cpu++) { if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0) return -1; @@ -259,9 +259,6 @@ static int run_perf_stat(int argc __used, const char **argv) const bool forks = (argc > 0); char buf; - if (!system_wide) - nr_cpus = 1; - if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { perror("failed to create pipes"); exit(1); @@ -351,12 +348,12 @@ static int run_perf_stat(int argc __used, const char **argv) if (no_aggr) { list_for_each_entry(counter, &evsel_list, node) { read_counter(counter); - perf_evsel__close_fd(counter, nr_cpus, 1); + perf_evsel__close_fd(counter, cpus->nr, 1); } } else { list_for_each_entry(counter, &evsel_list, node) { read_counter_aggr(counter); - perf_evsel__close_fd(counter, nr_cpus, thread_num); + perf_evsel__close_fd(counter, cpus->nr, thread_num); } } @@ -384,7 +381,7 @@ static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg) if (no_aggr) sprintf(cpustr, "CPU%*d%s", csv_output ? 0 : -4, - cpumap[cpu], csv_sep); + cpus->map[cpu], csv_sep); fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel)); @@ -412,7 +409,7 @@ static void abs_printout(int cpu, struct perf_evsel *evsel, double avg) if (no_aggr) sprintf(cpustr, "CPU%*d%s", csv_output ? 0 : -4, - cpumap[cpu], csv_sep); + cpus->map[cpu], csv_sep); else cpu = 0; @@ -498,14 +495,14 @@ static void print_counter(struct perf_evsel *counter) u64 ena, run, val; int cpu; - for (cpu = 0; cpu < nr_cpus; cpu++) { + for (cpu = 0; cpu < cpus->nr; cpu++) { val = counter->counts->cpu[cpu].val; ena = counter->counts->cpu[cpu].ena; run = counter->counts->cpu[cpu].run; if (run == 0 || ena == 0) { fprintf(stderr, "CPU%*d%s%*s%s%-24s", csv_output ? 0 : -4, - cpumap[cpu], csv_sep, + cpus->map[cpu], csv_sep, csv_output ? 0 : 18, "", csv_sep, event_name(counter)); @@ -697,12 +694,15 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) } if (system_wide) - nr_cpus = read_cpu_map(cpu_list); + cpus = cpu_map__new(cpu_list); else - nr_cpus = 1; + cpus = cpu_map__dummy_new(); - if (nr_cpus < 1) + if (cpus == NULL) { + perror("failed to parse CPUs map"); usage_with_options(stat_usage, options); + return -1; + } if (target_pid != -1) { target_tid = target_pid; @@ -723,8 +723,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) list_for_each_entry(pos, &evsel_list, node) { if (perf_evsel__alloc_stat_priv(pos) < 0 || - perf_evsel__alloc_counts(pos, nr_cpus) < 0 || - perf_evsel__alloc_fd(pos, nr_cpus, thread_num) < 0) + perf_evsel__alloc_counts(pos, cpus->nr) < 0 || + perf_evsel__alloc_fd(pos, cpus->nr, thread_num) < 0) goto out_free_fd; } diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 27b9c14a0a0..0e426665716 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -71,7 +71,7 @@ static int target_tid = -1; static pid_t *all_tids = NULL; static int thread_num = 0; static bool inherit = false; -static int nr_cpus = 0; +static struct cpu_map *cpus; static int realtime_prio = 0; static bool group = false; static unsigned int page_size; @@ -564,12 +564,12 @@ static void print_sym_table(void) printf(" (all"); if (cpu_list) - printf(", CPU%s: %s)\n", nr_cpus > 1 ? "s" : "", cpu_list); + printf(", CPU%s: %s)\n", cpus->nr > 1 ? "s" : "", cpu_list); else { if (target_tid != -1) printf(")\n"); else - printf(", %d CPU%s)\n", nr_cpus, nr_cpus > 1 ? "s" : ""); + printf(", %d CPU%s)\n", cpus->nr, cpus->nr > 1 ? "s" : ""); } printf("%-*.*s\n", win_width, win_width, graph_dotted_line); @@ -1197,7 +1197,7 @@ static void perf_session__mmap_read(struct perf_session *self) struct perf_evsel *counter; int i, thread_index; - for (i = 0; i < nr_cpus; i++) { + for (i = 0; i < cpus->nr; i++) { list_for_each_entry(counter, &evsel_list, node) { for (thread_index = 0; thread_index < thread_num; @@ -1221,7 +1221,7 @@ static void start_counter(int i, struct perf_evsel *evsel) int thread_index; if (target_tid == -1) - cpu = cpumap[i]; + cpu = cpus->map[i]; attr = &evsel->attr; @@ -1310,7 +1310,7 @@ static int __cmd_top(void) else event__synthesize_threads(event__process, session); - for (i = 0; i < nr_cpus; i++) { + for (i = 0; i < cpus->nr; i++) { group_fd = -1; list_for_each_entry(counter, &evsel_list, node) start_counter(i, counter); @@ -1460,16 +1460,16 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) } if (target_tid != -1) - nr_cpus = 1; + cpus = cpu_map__dummy_new(); else - nr_cpus = read_cpu_map(cpu_list); + cpus = cpu_map__new(cpu_list); - if (nr_cpus < 1) + if (cpus == NULL) usage_with_options(top_usage, options); list_for_each_entry(pos, &evsel_list, node) { - if (perf_evsel__alloc_mmap_per_thread(pos, nr_cpus, thread_num) < 0 || - perf_evsel__alloc_fd(pos, nr_cpus, thread_num) < 0) + if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, thread_num) < 0 || + perf_evsel__alloc_fd(pos, cpus->nr, thread_num) < 0) goto out_free_fd; /* * Fill in the ones not specifically initialized via -c: diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 0f9b8d7a7d7..3ccaa104338 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c @@ -4,32 +4,53 @@ #include #include -int cpumap[MAX_NR_CPUS]; - -static int default_cpu_map(void) +static struct cpu_map *cpu_map__default_new(void) { - int nr_cpus, i; + struct cpu_map *cpus; + int nr_cpus; nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); - assert(nr_cpus <= MAX_NR_CPUS); - assert((int)nr_cpus >= 0); + if (nr_cpus < 0) + return NULL; + + cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int)); + if (cpus != NULL) { + int i; + for (i = 0; i < nr_cpus; ++i) + cpus->map[i] = i; - for (i = 0; i < nr_cpus; ++i) - cpumap[i] = i; + cpus->nr = nr_cpus; + } - return nr_cpus; + return cpus; } -static int read_all_cpu_map(void) +static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus) { + size_t payload_size = nr_cpus * sizeof(int); + struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size); + + if (cpus != NULL) { + cpus->nr = nr_cpus; + memcpy(cpus->map, tmp_cpus, payload_size); + } + + return cpus; +} + +static struct cpu_map *cpu_map__read_all_cpu_map(void) +{ + struct cpu_map *cpus = NULL; FILE *onlnf; int nr_cpus = 0; + int *tmp_cpus = NULL, *tmp; + int max_entries = 0; int n, cpu, prev; char sep; onlnf = fopen("/sys/devices/system/cpu/online", "r"); if (!onlnf) - return default_cpu_map(); + return cpu_map__default_new(); sep = 0; prev = -1; @@ -38,12 +59,28 @@ static int read_all_cpu_map(void) if (n <= 0) break; if (prev >= 0) { - assert(nr_cpus + cpu - prev - 1 < MAX_NR_CPUS); + int new_max = nr_cpus + cpu - prev - 1; + + if (new_max >= max_entries) { + max_entries = new_max + MAX_NR_CPUS / 2; + tmp = realloc(tmp_cpus, max_entries * sizeof(int)); + if (tmp == NULL) + goto out_free_tmp; + tmp_cpus = tmp; + } + while (++prev < cpu) - cpumap[nr_cpus++] = prev; + tmp_cpus[nr_cpus++] = prev; + } + if (nr_cpus == max_entries) { + max_entries += MAX_NR_CPUS; + tmp = realloc(tmp_cpus, max_entries * sizeof(int)); + if (tmp == NULL) + goto out_free_tmp; + tmp_cpus = tmp; } - assert (nr_cpus < MAX_NR_CPUS); - cpumap[nr_cpus++] = cpu; + + tmp_cpus[nr_cpus++] = cpu; if (n == 2 && sep == '-') prev = cpu; else @@ -51,24 +88,31 @@ static int read_all_cpu_map(void) if (n == 1 || sep == '\n') break; } - fclose(onlnf); - if (nr_cpus > 0) - return nr_cpus; - return default_cpu_map(); + if (nr_cpus > 0) + cpus = cpu_map__trim_new(nr_cpus, tmp_cpus); + else + cpus = cpu_map__default_new(); +out_free_tmp: + free(tmp_cpus); + fclose(onlnf); + return cpus; } -int read_cpu_map(const char *cpu_list) +struct cpu_map *cpu_map__new(const char *cpu_list) { + struct cpu_map *cpus = NULL; unsigned long start_cpu, end_cpu = 0; char *p = NULL; int i, nr_cpus = 0; + int *tmp_cpus = NULL, *tmp; + int max_entries = 0; if (!cpu_list) - return read_all_cpu_map(); + return cpu_map__read_all_cpu_map(); if (!isdigit(*cpu_list)) - goto invalid; + goto out; while (isdigit(*cpu_list)) { p = NULL; @@ -94,21 +138,42 @@ int read_cpu_map(const char *cpu_list) for (; start_cpu <= end_cpu; start_cpu++) { /* check for duplicates */ for (i = 0; i < nr_cpus; i++) - if (cpumap[i] == (int)start_cpu) + if (tmp_cpus[i] == (int)start_cpu) goto invalid; - assert(nr_cpus < MAX_NR_CPUS); - cpumap[nr_cpus++] = (int)start_cpu; + if (nr_cpus == max_entries) { + max_entries += MAX_NR_CPUS; + tmp = realloc(tmp_cpus, max_entries * sizeof(int)); + if (tmp == NULL) + goto invalid; + tmp_cpus = tmp; + } + tmp_cpus[nr_cpus++] = (int)start_cpu; } if (*p) ++p; cpu_list = p; } - if (nr_cpus > 0) - return nr_cpus; - return default_cpu_map(); + if (nr_cpus > 0) + cpus = cpu_map__trim_new(nr_cpus, tmp_cpus); + else + cpus = cpu_map__default_new(); invalid: - return -1; + free(tmp_cpus); +out: + return cpus; +} + +struct cpu_map *cpu_map__dummy_new(void) +{ + struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int)); + + if (cpus != NULL) { + cpus->nr = 1; + cpus->map[0] = -1; + } + + return cpus; } diff --git a/tools/perf/util/cpumap.h b/tools/perf/util/cpumap.h index 3e60f56e490..f7a4f42f630 100644 --- a/tools/perf/util/cpumap.h +++ b/tools/perf/util/cpumap.h @@ -1,7 +1,13 @@ #ifndef __PERF_CPUMAP_H #define __PERF_CPUMAP_H -extern int read_cpu_map(const char *cpu_list); -extern int cpumap[]; +struct cpu_map { + int nr; + int map[]; +}; + +struct cpu_map *cpu_map__new(const char *cpu_list); +struct cpu_map *cpu_map__dummy_new(void); +void *cpu_map__delete(struct cpu_map *map); #endif /* __PERF_CPUMAP_H */ -- cgit v1.2.3-70-g09d2 From 5c98d466e49267a9221f30958d45cd06f794269a Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 17:53:33 -0200 Subject: perf tools: Refactor all_tids to hold nr and the map So that later, we can pass the thread_map instance instead of (thread_num, thread_map) for things like perf_evsel__open and friends, just like was done with cpu_map. Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-record.c | 39 +++++++++++++++------------------------ tools/perf/builtin-stat.c | 41 +++++++++++++++++------------------------ tools/perf/builtin-top.c | 35 +++++++++++++---------------------- tools/perf/util/thread.c | 43 +++++++++++++++++++++++++++++-------------- tools/perf/util/thread.h | 15 ++++++++++++++- 5 files changed, 88 insertions(+), 85 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 220e6e7f0b9..7bc04903548 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -54,8 +54,7 @@ static bool sample_id_all_avail = true; static bool system_wide = false; static pid_t target_pid = -1; static pid_t target_tid = -1; -static pid_t *all_tids = NULL; -static int thread_num = 0; +static struct thread_map *threads; static pid_t child_pid = -1; static bool no_inherit = false; static enum write_mode_t write_mode = WRITE_FORCE; @@ -318,9 +317,9 @@ static void create_counter(struct perf_evsel *evsel, int cpu) retry_sample_id: attr->sample_id_all = sample_id_all_avail ? 1 : 0; - for (thread_index = 0; thread_index < thread_num; thread_index++) { + for (thread_index = 0; thread_index < threads->nr; thread_index++) { try_again: - FD(evsel, nr_cpu, thread_index) = sys_perf_event_open(attr, all_tids[thread_index], cpu, group_fd, 0); + FD(evsel, nr_cpu, thread_index) = sys_perf_event_open(attr, threads->map[thread_index], cpu, group_fd, 0); if (FD(evsel, nr_cpu, thread_index) < 0) { int err = errno; @@ -653,7 +652,7 @@ static int __cmd_record(int argc, const char **argv) } if (!system_wide && target_tid == -1 && target_pid == -1) - all_tids[0] = child_pid; + threads->map[0] = child_pid; close(child_ready_pipe[1]); close(go_pipe[0]); @@ -793,7 +792,7 @@ static int __cmd_record(int argc, const char **argv) list_for_each_entry(pos, &evsel_list, node) { for (thread = 0; - thread < thread_num; + thread < threads->nr; thread++) ioctl(FD(pos, i, thread), PERF_EVENT_IOC_DISABLE); @@ -910,21 +909,13 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) goto out_symbol_exit; } - if (target_pid != -1) { + if (target_pid != -1) target_tid = target_pid; - thread_num = find_all_tid(target_pid, &all_tids); - if (thread_num <= 0) { - fprintf(stderr, "Can't find all threads of pid %d\n", - target_pid); - usage_with_options(record_usage, record_options); - } - } else { - all_tids=malloc(sizeof(pid_t)); - if (!all_tids) - goto out_symbol_exit; - all_tids[0] = target_tid; - thread_num = 1; + threads = thread_map__new(target_pid, target_tid); + if (threads == NULL) { + pr_err("Problems finding threads of monitor\n"); + usage_with_options(record_usage, record_options); } cpus = cpu_map__new(cpu_list); @@ -934,11 +925,11 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) } list_for_each_entry(pos, &evsel_list, node) { - if (perf_evsel__alloc_fd(pos, cpus->nr, thread_num) < 0) + if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) goto out_free_fd; } - event_array = malloc( - sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num); + event_array = malloc((sizeof(struct pollfd) * MAX_NR_CPUS * + MAX_COUNTERS * threads->nr)); if (!event_array) goto out_free_fd; @@ -965,8 +956,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) out_free_event_array: free(event_array); out_free_fd: - free(all_tids); - all_tids = NULL; + thread_map__delete(threads); + threads = NULL; out_symbol_exit: symbol__exit(); return err; diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 3f4a431fb5a..6b9146cd1ea 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -81,8 +81,7 @@ static bool scale = true; static bool no_aggr = false; static pid_t target_pid = -1; static pid_t target_tid = -1; -static pid_t *all_tids = NULL; -static int thread_num = 0; +static struct thread_map *threads; static pid_t child_pid = -1; static bool null_run = false; static bool big_num = true; @@ -175,7 +174,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel) attr->enable_on_exec = 1; } - return perf_evsel__open_per_thread(evsel, thread_num, all_tids); + return perf_evsel__open_per_thread(evsel, threads->nr, threads->map); } /* @@ -200,7 +199,7 @@ static int read_counter_aggr(struct perf_evsel *counter) u64 *count = counter->counts->aggr.values; int i; - if (__perf_evsel__read(counter, cpus->nr, thread_num, scale) < 0) + if (__perf_evsel__read(counter, cpus->nr, threads->nr, scale) < 0) return -1; for (i = 0; i < 3; i++) @@ -298,7 +297,7 @@ static int run_perf_stat(int argc __used, const char **argv) } if (target_tid == -1 && target_pid == -1 && !system_wide) - all_tids[0] = child_pid; + threads->map[0] = child_pid; /* * Wait for the child to be ready to exec. @@ -353,7 +352,7 @@ static int run_perf_stat(int argc __used, const char **argv) } else { list_for_each_entry(counter, &evsel_list, node) { read_counter_aggr(counter); - perf_evsel__close_fd(counter, cpus->nr, thread_num); + perf_evsel__close_fd(counter, cpus->nr, threads->nr); } } @@ -693,6 +692,15 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) } } + if (target_pid != -1) + target_tid = target_pid; + + threads = thread_map__new(target_pid, target_tid); + if (threads == NULL) { + pr_err("Problems finding threads of monitor\n"); + usage_with_options(stat_usage, options); + } + if (system_wide) cpus = cpu_map__new(cpu_list); else @@ -704,27 +712,10 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) return -1; } - if (target_pid != -1) { - target_tid = target_pid; - thread_num = find_all_tid(target_pid, &all_tids); - if (thread_num <= 0) { - fprintf(stderr, "Can't find all threads of pid %d\n", - target_pid); - usage_with_options(stat_usage, options); - } - } else { - all_tids=malloc(sizeof(pid_t)); - if (!all_tids) - return -ENOMEM; - - all_tids[0] = target_tid; - thread_num = 1; - } - list_for_each_entry(pos, &evsel_list, node) { if (perf_evsel__alloc_stat_priv(pos) < 0 || perf_evsel__alloc_counts(pos, cpus->nr) < 0 || - perf_evsel__alloc_fd(pos, cpus->nr, thread_num) < 0) + perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) goto out_free_fd; } @@ -752,5 +743,7 @@ out_free_fd: list_for_each_entry(pos, &evsel_list, node) perf_evsel__free_stat_priv(pos); out: + thread_map__delete(threads); + threads = NULL; return status; } diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 0e426665716..1e67ab9c7eb 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -68,8 +68,7 @@ static int print_entries; static int target_pid = -1; static int target_tid = -1; -static pid_t *all_tids = NULL; -static int thread_num = 0; +static struct thread_map *threads; static bool inherit = false; static struct cpu_map *cpus; static int realtime_prio = 0; @@ -1200,7 +1199,7 @@ static void perf_session__mmap_read(struct perf_session *self) for (i = 0; i < cpus->nr; i++) { list_for_each_entry(counter, &evsel_list, node) { for (thread_index = 0; - thread_index < thread_num; + thread_index < threads->nr; thread_index++) { perf_session__mmap_read_counter(self, counter, i, thread_index); @@ -1236,10 +1235,10 @@ static void start_counter(int i, struct perf_evsel *evsel) attr->inherit = (cpu < 0) && inherit; attr->mmap = 1; - for (thread_index = 0; thread_index < thread_num; thread_index++) { + for (thread_index = 0; thread_index < threads->nr; thread_index++) { try_again: FD(evsel, i, thread_index) = sys_perf_event_open(attr, - all_tids[thread_index], cpu, group_fd, 0); + threads->map[thread_index], cpu, group_fd, 0); if (FD(evsel, i, thread_index) < 0) { int err = errno; @@ -1410,25 +1409,17 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) if (argc) usage_with_options(top_usage, options); - if (target_pid != -1) { + if (target_pid != -1) target_tid = target_pid; - thread_num = find_all_tid(target_pid, &all_tids); - if (thread_num <= 0) { - fprintf(stderr, "Can't find all threads of pid %d\n", - target_pid); - usage_with_options(top_usage, options); - } - } else { - all_tids=malloc(sizeof(pid_t)); - if (!all_tids) - return -ENOMEM; - all_tids[0] = target_tid; - thread_num = 1; + threads = thread_map__new(target_pid, target_tid); + if (threads == NULL) { + pr_err("Problems finding threads of monitor\n"); + usage_with_options(top_usage, options); } - event_array = malloc( - sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num); + event_array = malloc((sizeof(struct pollfd) * + MAX_NR_CPUS * MAX_COUNTERS * threads->nr)); if (!event_array) return -ENOMEM; @@ -1468,8 +1459,8 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) usage_with_options(top_usage, options); list_for_each_entry(pos, &evsel_list, node) { - if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, thread_num) < 0 || - perf_evsel__alloc_fd(pos, cpus->nr, thread_num) < 0) + if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, threads->nr) < 0 || + perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) goto out_free_fd; /* * Fill in the ones not specifically initialized via -c: diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 8c72d888e44..00f4eade2e3 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -16,35 +16,50 @@ static int filter(const struct dirent *dir) return 1; } -int find_all_tid(int pid, pid_t ** all_tid) +struct thread_map *thread_map__new_by_pid(pid_t pid) { + struct thread_map *threads; char name[256]; int items; struct dirent **namelist = NULL; - int ret = 0; int i; sprintf(name, "/proc/%d/task", pid); items = scandir(name, &namelist, filter, NULL); if (items <= 0) - return -ENOENT; - *all_tid = malloc(sizeof(pid_t) * items); - if (!*all_tid) { - ret = -ENOMEM; - goto failure; - } - - for (i = 0; i < items; i++) - (*all_tid)[i] = atoi(namelist[i]->d_name); + return NULL; - ret = items; + threads = malloc(sizeof(*threads) + sizeof(pid_t) * items); + if (threads != NULL) { + for (i = 0; i < items; i++) + threads->map[i] = atoi(namelist[i]->d_name); + threads->nr = items; + } -failure: for (i=0; imap[0] = tid; + threads->nr = 1; + } + + return threads; +} + +struct thread_map *thread_map__new(pid_t pid, pid_t tid) +{ + if (pid != -1) + return thread_map__new_by_pid(pid); + return thread_map__new_by_tid(tid); } static struct thread *thread__new(pid_t pid) diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 688500ff826..d7574101054 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -18,11 +18,24 @@ struct thread { int comm_len; }; +struct thread_map { + int nr; + int map[]; +}; + struct perf_session; void thread__delete(struct thread *self); -int find_all_tid(int pid, pid_t ** all_tid); +struct thread_map *thread_map__new_by_pid(pid_t pid); +struct thread_map *thread_map__new_by_tid(pid_t tid); +struct thread_map *thread_map__new(pid_t pid, pid_t tid); + +static inline void thread_map__delete(struct thread_map *threads) +{ + free(threads); +} + int thread__set_comm(struct thread *self, const char *comm); int thread__comm_len(struct thread *self); struct thread *perf_session__findnew(struct perf_session *self, pid_t pid); -- cgit v1.2.3-70-g09d2 From 86bd5e8603b00b06189328c6d7034d2dc434d6bb Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 3 Jan 2011 23:09:46 -0200 Subject: perf evsel: Use {cpu,thread}_map to shorten list of parameters Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Mike Galbraith Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Tom Zanussi LKML-Reference: Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-stat.c | 4 ++-- tools/perf/util/evsel.c | 24 +++++++++++++----------- tools/perf/util/evsel.h | 11 +++++++---- 3 files changed, 22 insertions(+), 17 deletions(-) (limited to 'tools/perf/builtin-stat.c') diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 6b9146cd1ea..02b2d8013a6 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -166,7 +166,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel) PERF_FORMAT_TOTAL_TIME_RUNNING; if (system_wide) - return perf_evsel__open_per_cpu(evsel, cpus->nr, cpus->map); + return perf_evsel__open_per_cpu(evsel, cpus); attr->inherit = !no_inherit; if (target_pid == -1 && target_tid == -1) { @@ -174,7 +174,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel) attr->enable_on_exec = 1; } - return perf_evsel__open_per_thread(evsel, threads->nr, threads->map); + return perf_evsel__open_per_thread(evsel, threads); } /* diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index e62cc5e050a..e44be528c09 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1,6 +1,8 @@ #include "evsel.h" #include "../perf.h" #include "util.h" +#include "cpumap.h" +#include "thread.h" #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) @@ -123,13 +125,13 @@ int __perf_evsel__read(struct perf_evsel *evsel, return 0; } -int perf_evsel__open_per_cpu(struct perf_evsel *evsel, int ncpus, int *cpu_map) +int perf_evsel__open_per_cpu(struct perf_evsel *evsel, struct cpu_map *cpus) { int cpu; - for (cpu = 0; cpu < ncpus; cpu++) { + for (cpu = 0; cpu < cpus->nr; cpu++) { FD(evsel, cpu, 0) = sys_perf_event_open(&evsel->attr, -1, - cpu_map[cpu], -1, 0); + cpus->map[cpu], -1, 0); if (FD(evsel, cpu, 0) < 0) goto out_close; } @@ -144,13 +146,13 @@ out_close: return -1; } -int perf_evsel__open_per_thread(struct perf_evsel *evsel, int nthreads, int *thread_map) +int perf_evsel__open_per_thread(struct perf_evsel *evsel, struct thread_map *threads) { int thread; - for (thread = 0; thread < nthreads; thread++) { + for (thread = 0; thread < threads->nr; thread++) { FD(evsel, 0, thread) = sys_perf_event_open(&evsel->attr, - thread_map[thread], -1, -1, 0); + threads->map[thread], -1, -1, 0); if (FD(evsel, 0, thread) < 0) goto out_close; } @@ -165,11 +167,11 @@ out_close: return -1; } -int perf_evsel__open(struct perf_evsel *evsel, int ncpus, int nthreads, - int *cpu_map, int *thread_map) +int perf_evsel__open(struct perf_evsel *evsel, + struct cpu_map *cpus, struct thread_map *threads) { - if (nthreads < 0) - return perf_evsel__open_per_cpu(evsel, ncpus, cpu_map); + if (threads == NULL) + return perf_evsel__open_per_cpu(evsel, cpus); - return perf_evsel__open_per_thread(evsel, nthreads, thread_map); + return perf_evsel__open_per_thread(evsel, threads); } diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index a62fb55cffa..863d78d5ef1 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -34,6 +34,9 @@ struct perf_evsel { void *priv; }; +struct cpu_map; +struct thread_map; + struct perf_evsel *perf_evsel__new(u32 type, u64 config, int idx); void perf_evsel__delete(struct perf_evsel *evsel); @@ -42,10 +45,10 @@ int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus); void perf_evsel__free_fd(struct perf_evsel *evsel); void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads); -int perf_evsel__open_per_cpu(struct perf_evsel *evsel, int ncpus, int *cpu_map); -int perf_evsel__open_per_thread(struct perf_evsel *evsel, int nthreads, int *thread_map); -int perf_evsel__open(struct perf_evsel *evsel, int ncpus, int nthreads, - int *cpu_map, int *thread_map); +int perf_evsel__open_per_cpu(struct perf_evsel *evsel, struct cpu_map *cpus); +int perf_evsel__open_per_thread(struct perf_evsel *evsel, struct thread_map *threads); +int perf_evsel__open(struct perf_evsel *evsel, + struct cpu_map *cpus, struct thread_map *threads); #define perf_evsel__match(evsel, t, c) \ (evsel->attr.type == PERF_TYPE_##t && \ -- cgit v1.2.3-70-g09d2