]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - tools/perf/builtin-stat.c
Merge branch 'libnvdimm-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw...
[karo-tx-linux.git] / tools / perf / builtin-stat.c
index aa706fca410e531ce3e3c728a4d28631a2f2394a..37e301a32f437eb6004ba4f4ccc6b90c73b9aa5c 100644 (file)
 #define CNTR_NOT_SUPPORTED     "<not supported>"
 #define CNTR_NOT_COUNTED       "<not counted>"
 
-static void print_stat(int argc, const char **argv);
-static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
-static void print_counter(struct perf_evsel *counter, char *prefix);
-static void print_aggr(char *prefix);
+static void print_counters(struct timespec *ts, int argc, const char **argv);
 
 /* Default events used for perf stat -T */
 static const char *transaction_attrs = {
@@ -234,6 +231,7 @@ process_counter_values(struct perf_evsel *evsel, int cpu, int thread,
                count = &zero;
 
        switch (aggr_mode) {
+       case AGGR_THREAD:
        case AGGR_CORE:
        case AGGR_SOCKET:
        case AGGR_NONE:
@@ -284,7 +282,7 @@ static int process_counter(struct perf_evsel *counter)
        int i, ret;
 
        aggr->val = aggr->ena = aggr->run = 0;
-       memset(ps->res_stats, 0, sizeof(ps->res_stats));
+       init_stats(ps->res_stats);
 
        if (counter->per_pkg)
                zero_per_pkg(counter);
@@ -365,53 +363,14 @@ static void read_counters(bool close)
 
 static void process_interval(void)
 {
-       static int num_print_interval;
-       struct perf_evsel *counter;
        struct timespec ts, rs;
-       char prefix[64];
 
        read_counters(false);
 
        clock_gettime(CLOCK_MONOTONIC, &ts);
        diff_timespec(&rs, &ts, &ref_time);
-       sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
-
-       if (num_print_interval == 0 && !csv_output) {
-               switch (aggr_mode) {
-               case AGGR_SOCKET:
-                       fprintf(output, "#           time socket cpus             counts %*s events\n", unit_width, "unit");
-                       break;
-               case AGGR_CORE:
-                       fprintf(output, "#           time core         cpus             counts %*s events\n", unit_width, "unit");
-                       break;
-               case AGGR_NONE:
-                       fprintf(output, "#           time CPU                counts %*s events\n", unit_width, "unit");
-                       break;
-               case AGGR_GLOBAL:
-               default:
-                       fprintf(output, "#           time             counts %*s events\n", unit_width, "unit");
-               }
-       }
-
-       if (++num_print_interval == 25)
-               num_print_interval = 0;
-
-       switch (aggr_mode) {
-       case AGGR_CORE:
-       case AGGR_SOCKET:
-               print_aggr(prefix);
-               break;
-       case AGGR_NONE:
-               evlist__for_each(evsel_list, counter)
-                       print_counter(counter, prefix);
-               break;
-       case AGGR_GLOBAL:
-       default:
-               evlist__for_each(evsel_list, counter)
-                       print_counter_aggr(counter, prefix);
-       }
 
-       fflush(output);
+       print_counters(&rs, 0, NULL);
 }
 
 static void handle_initial_delay(void)
@@ -644,6 +603,14 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
                        csv_output ? 0 : -4,
                        perf_evsel__cpus(evsel)->map[id], csv_sep);
                break;
+       case AGGR_THREAD:
+               fprintf(output, "%*s-%*d%s",
+                       csv_output ? 0 : 16,
+                       thread_map__comm(evsel->threads, id),
+                       csv_output ? 0 : -8,
+                       thread_map__pid(evsel->threads, id),
+                       csv_sep);
+               break;
        case AGGR_GLOBAL:
        default:
                break;
@@ -792,6 +759,40 @@ static void print_aggr(char *prefix)
        }
 }
 
+static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
+{
+       int nthreads = thread_map__nr(counter->threads);
+       int ncpus = cpu_map__nr(counter->cpus);
+       int cpu, thread;
+       double uval;
+
+       for (thread = 0; thread < nthreads; thread++) {
+               u64 ena = 0, run = 0, val = 0;
+
+               for (cpu = 0; cpu < ncpus; cpu++) {
+                       val += perf_counts(counter->counts, cpu, thread)->val;
+                       ena += perf_counts(counter->counts, cpu, thread)->ena;
+                       run += perf_counts(counter->counts, cpu, thread)->run;
+               }
+
+               if (prefix)
+                       fprintf(output, "%s", prefix);
+
+               uval = val * counter->scale;
+
+               if (nsec_counter(counter))
+                       nsec_printout(thread, 0, counter, uval);
+               else
+                       abs_printout(thread, 0, counter, uval);
+
+               if (!csv_output)
+                       print_noise(counter, 1.0);
+
+               print_running(run, ena);
+               fputc('\n', output);
+       }
+}
+
 /*
  * Print out the results of a single counter:
  * aggregated counts in system-wide mode
@@ -901,9 +902,38 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
        }
 }
 
-static void print_stat(int argc, const char **argv)
+static void print_interval(char *prefix, struct timespec *ts)
+{
+       static int num_print_interval;
+
+       sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
+
+       if (num_print_interval == 0 && !csv_output) {
+               switch (aggr_mode) {
+               case AGGR_SOCKET:
+                       fprintf(output, "#           time socket cpus             counts %*s events\n", unit_width, "unit");
+                       break;
+               case AGGR_CORE:
+                       fprintf(output, "#           time core         cpus             counts %*s events\n", unit_width, "unit");
+                       break;
+               case AGGR_NONE:
+                       fprintf(output, "#           time CPU                counts %*s events\n", unit_width, "unit");
+                       break;
+               case AGGR_THREAD:
+                       fprintf(output, "#           time             comm-pid                  counts %*s events\n", unit_width, "unit");
+                       break;
+               case AGGR_GLOBAL:
+               default:
+                       fprintf(output, "#           time             counts %*s events\n", unit_width, "unit");
+               }
+       }
+
+       if (++num_print_interval == 25)
+               num_print_interval = 0;
+}
+
+static void print_header(int argc, const char **argv)
 {
-       struct perf_evsel *counter;
        int i;
 
        fflush(stdout);
@@ -929,36 +959,57 @@ static void print_stat(int argc, const char **argv)
                        fprintf(output, " (%d runs)", run_count);
                fprintf(output, ":\n\n");
        }
+}
+
+static void print_footer(void)
+{
+       if (!null_run)
+               fprintf(output, "\n");
+       fprintf(output, " %17.9f seconds time elapsed",
+                       avg_stats(&walltime_nsecs_stats)/1e9);
+       if (run_count > 1) {
+               fprintf(output, "                                        ");
+               print_noise_pct(stddev_stats(&walltime_nsecs_stats),
+                               avg_stats(&walltime_nsecs_stats));
+       }
+       fprintf(output, "\n\n");
+}
+
+static void print_counters(struct timespec *ts, int argc, const char **argv)
+{
+       struct perf_evsel *counter;
+       char buf[64], *prefix = NULL;
+
+       if (interval)
+               print_interval(prefix = buf, ts);
+       else
+               print_header(argc, argv);
 
        switch (aggr_mode) {
        case AGGR_CORE:
        case AGGR_SOCKET:
-               print_aggr(NULL);
+               print_aggr(prefix);
+               break;
+       case AGGR_THREAD:
+               evlist__for_each(evsel_list, counter)
+                       print_aggr_thread(counter, prefix);
                break;
        case AGGR_GLOBAL:
                evlist__for_each(evsel_list, counter)
-                       print_counter_aggr(counter, NULL);
+                       print_counter_aggr(counter, prefix);
                break;
        case AGGR_NONE:
                evlist__for_each(evsel_list, counter)
-                       print_counter(counter, NULL);
+                       print_counter(counter, prefix);
                break;
        default:
                break;
        }
 
-       if (!csv_output) {
-               if (!null_run)
-                       fprintf(output, "\n");
-               fprintf(output, " %17.9f seconds time elapsed",
-                               avg_stats(&walltime_nsecs_stats)/1e9);
-               if (run_count > 1) {
-                       fprintf(output, "                                        ");
-                       print_noise_pct(stddev_stats(&walltime_nsecs_stats),
-                                       avg_stats(&walltime_nsecs_stats));
-               }
-               fprintf(output, "\n\n");
-       }
+       if (!interval && !csv_output)
+               print_footer();
+
+       fflush(output);
 }
 
 static volatile int signr = -1;
@@ -1030,6 +1081,7 @@ static int perf_stat_init_aggr_mode(void)
                break;
        case AGGR_NONE:
        case AGGR_GLOBAL:
+       case AGGR_THREAD:
        default:
                break;
        }
@@ -1254,6 +1306,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
                     "aggregate counts per processor socket", AGGR_SOCKET),
        OPT_SET_UINT(0, "per-core", &aggr_mode,
                     "aggregate counts per physical processor core", AGGR_CORE),
+       OPT_SET_UINT(0, "per-thread", &aggr_mode,
+                    "aggregate counts per thread", AGGR_THREAD),
        OPT_UINTEGER('D', "delay", &initial_delay,
                     "ms to wait before starting measurement after program start"),
        OPT_END()
@@ -1345,8 +1399,19 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
                run_count = 1;
        }
 
-       /* no_aggr, cgroup are for system-wide only */
-       if ((aggr_mode != AGGR_GLOBAL || nr_cgroups) &&
+       if ((aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
+               fprintf(stderr, "The --per-thread option is only available "
+                       "when monitoring via -p -t options.\n");
+               parse_options_usage(NULL, options, "p", 1);
+               parse_options_usage(NULL, options, "t", 1);
+               goto out;
+       }
+
+       /*
+        * no_aggr, cgroup are for system-wide only
+        * --per-thread is aggregated per thread, we dont mix it with cpu mode
+        */
+       if (((aggr_mode != AGGR_GLOBAL && aggr_mode != AGGR_THREAD) || nr_cgroups) &&
            !target__has_cpu(&target)) {
                fprintf(stderr, "both cgroup and no-aggregation "
                        "modes only available in system-wide mode\n");
@@ -1374,6 +1439,14 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
                }
                goto out;
        }
+
+       /*
+        * Initialize thread_map with comm names,
+        * so we could print it out on output.
+        */
+       if (aggr_mode == AGGR_THREAD)
+               thread_map__read_comms(evsel_list->threads);
+
        if (interval && interval < 100) {
                pr_err("print interval must be >= 100ms\n");
                parse_options_usage(stat_usage, options, "I", 1);
@@ -1407,13 +1480,13 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 
                status = run_perf_stat(argc, argv);
                if (forever && status != -1) {
-                       print_stat(argc, argv);
+                       print_counters(NULL, argc, argv);
                        perf_stat__reset_stats();
                }
        }
 
        if (!forever && status != -1 && !interval)
-               print_stat(argc, argv);
+               print_counters(NULL, argc, argv);
 
        perf_evlist__free_stats(evsel_list);
 out: