Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 2 | * builtin-stat.c |
| 3 | * |
| 4 | * Builtin stat command: Give a precise performance counters summary |
| 5 | * overview about any workload, CPU or specific PID. |
| 6 | * |
| 7 | * Sample output: |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 8 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 9 | $ perf stat ./hackbench 10 |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 10 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 11 | Time: 0.118 |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 12 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 13 | Performance counter stats for './hackbench 10': |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 14 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 15 | 1708.761321 task-clock # 11.037 CPUs utilized |
| 16 | 41,190 context-switches # 0.024 M/sec |
| 17 | 6,735 CPU-migrations # 0.004 M/sec |
| 18 | 17,318 page-faults # 0.010 M/sec |
| 19 | 5,205,202,243 cycles # 3.046 GHz |
| 20 | 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle |
| 21 | 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle |
| 22 | 2,603,501,247 instructions # 0.50 insns per cycle |
| 23 | # 1.48 stalled cycles per insn |
| 24 | 484,357,498 branches # 283.455 M/sec |
| 25 | 6,388,934 branch-misses # 1.32% of all branches |
| 26 | |
| 27 | 0.154822978 seconds time elapsed |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 28 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 29 | * |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 30 | * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 31 | * |
| 32 | * Improvements and fixes by: |
| 33 | * |
| 34 | * Arjan van de Ven <arjan@linux.intel.com> |
| 35 | * Yanmin Zhang <yanmin.zhang@intel.com> |
| 36 | * Wu Fengguang <fengguang.wu@intel.com> |
| 37 | * Mike Galbraith <efault@gmx.de> |
| 38 | * Paul Mackerras <paulus@samba.org> |
Jaswinder Singh Rajput | 6e750a8f | 2009-06-27 03:02:07 +0530 | [diff] [blame] | 39 | * Jaswinder Singh Rajput <jaswinder@kernel.org> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 40 | * |
| 41 | * Released under the GPL v2. (and only v2, not any later version) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 42 | */ |
| 43 | |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 44 | #include "perf.h" |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 45 | #include "builtin.h" |
Arnaldo Carvalho de Melo | f14d570 | 2014-10-17 12:17:40 -0300 | [diff] [blame] | 46 | #include "util/cgroup.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 47 | #include "util/util.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 48 | #include <subcmd/parse-options.h> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 49 | #include "util/parse-events.h" |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 50 | #include "util/pmu.h" |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 51 | #include "util/event.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 52 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 53 | #include "util/evsel.h" |
Frederic Weisbecker | 8f28827a | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 54 | #include "util/debug.h" |
Ingo Molnar | a5d243d | 2011-04-27 05:39:24 +0200 | [diff] [blame] | 55 | #include "util/color.h" |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 56 | #include "util/stat.h" |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 57 | #include "util/header.h" |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 58 | #include "util/cpumap.h" |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 59 | #include "util/thread.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 60 | #include "util/thread_map.h" |
Jiri Olsa | d809560 | 2015-08-07 12:51:03 +0200 | [diff] [blame] | 61 | #include "util/counts.h" |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 62 | #include "util/session.h" |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 63 | #include "util/tool.h" |
| 64 | #include "asm/bug.h" |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 65 | |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 66 | #include <stdlib.h> |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 67 | #include <sys/prctl.h> |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 68 | #include <locale.h> |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 69 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 70 | #define DEFAULT_SEPARATOR " " |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 71 | #define CNTR_NOT_SUPPORTED "<not supported>" |
| 72 | #define CNTR_NOT_COUNTED "<not counted>" |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 73 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 74 | static void print_counters(struct timespec *ts, int argc, const char **argv); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 75 | |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 76 | /* Default events used for perf stat -T */ |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 77 | static const char *transaction_attrs = { |
| 78 | "task-clock," |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 79 | "{" |
| 80 | "instructions," |
| 81 | "cycles," |
| 82 | "cpu/cycles-t/," |
| 83 | "cpu/tx-start/," |
| 84 | "cpu/el-start/," |
| 85 | "cpu/cycles-ct/" |
| 86 | "}" |
| 87 | }; |
| 88 | |
| 89 | /* More limited version when the CPU does not have all events. */ |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 90 | static const char * transaction_limited_attrs = { |
| 91 | "task-clock," |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 92 | "{" |
| 93 | "instructions," |
| 94 | "cycles," |
| 95 | "cpu/cycles-t/," |
| 96 | "cpu/tx-start/" |
| 97 | "}" |
| 98 | }; |
| 99 | |
Robert Richter | 666e6d4 | 2012-04-05 18:26:27 +0200 | [diff] [blame] | 100 | static struct perf_evlist *evsel_list; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 101 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 102 | static struct target target = { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 103 | .uid = UINT_MAX, |
| 104 | }; |
Jaswinder Singh Rajput | 3d63259 | 2009-06-24 18:19:34 +0530 | [diff] [blame] | 105 | |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 106 | typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu); |
| 107 | |
Jaswinder Singh Rajput | 3d63259 | 2009-06-24 18:19:34 +0530 | [diff] [blame] | 108 | static int run_count = 1; |
Stephane Eranian | 2e6cdf9 | 2010-05-12 10:40:01 +0200 | [diff] [blame] | 109 | static bool no_inherit = false; |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 110 | static volatile pid_t child_pid = -1; |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 111 | static bool null_run = false; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 112 | static int detailed_run = 0; |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 113 | static bool transaction_run; |
Arnaldo Carvalho de Melo | 201e0b0 | 2010-12-01 17:53:27 -0200 | [diff] [blame] | 114 | static bool big_num = true; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 115 | static int big_num_opt = -1; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 116 | static const char *csv_sep = NULL; |
| 117 | static bool csv_output = false; |
Lin Ming | 43bece7 | 2011-08-17 18:42:07 +0800 | [diff] [blame] | 118 | static bool group = false; |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 119 | static const char *pre_cmd = NULL; |
| 120 | static const char *post_cmd = NULL; |
| 121 | static bool sync_run = false; |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 122 | static unsigned int initial_delay = 0; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 123 | static unsigned int unit_width = 4; /* strlen("unit") */ |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 124 | static bool forever = false; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 125 | static struct timespec ref_time; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 126 | static struct cpu_map *aggr_map; |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 127 | static aggr_get_id_t aggr_get_id; |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 128 | static bool append_file; |
| 129 | static const char *output_name; |
| 130 | static int output_fd; |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 131 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 132 | struct perf_stat { |
| 133 | bool record; |
| 134 | struct perf_data_file file; |
| 135 | struct perf_session *session; |
| 136 | u64 bytes_written; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 137 | struct perf_tool tool; |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 138 | bool maps_allocated; |
| 139 | struct cpu_map *cpus; |
| 140 | struct thread_map *threads; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static struct perf_stat perf_stat; |
| 144 | #define STAT_RECORD perf_stat.record |
| 145 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 146 | static volatile int done = 0; |
| 147 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 148 | static struct perf_stat_config stat_config = { |
| 149 | .aggr_mode = AGGR_GLOBAL, |
Jiri Olsa | 711a572 | 2015-07-21 14:31:23 +0200 | [diff] [blame] | 150 | .scale = true, |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 153 | static inline void diff_timespec(struct timespec *r, struct timespec *a, |
| 154 | struct timespec *b) |
| 155 | { |
| 156 | r->tv_sec = a->tv_sec - b->tv_sec; |
| 157 | if (a->tv_nsec < b->tv_nsec) { |
| 158 | r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec; |
| 159 | r->tv_sec--; |
| 160 | } else { |
| 161 | r->tv_nsec = a->tv_nsec - b->tv_nsec ; |
| 162 | } |
| 163 | } |
| 164 | |
Jiri Olsa | 254ecbc | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 165 | static void perf_stat__reset_stats(void) |
| 166 | { |
| 167 | perf_evlist__reset_stats(evsel_list); |
Jiri Olsa | f87027b | 2015-06-03 16:25:59 +0200 | [diff] [blame] | 168 | perf_stat__reset_shadow_stats(); |
Jiri Olsa | 1eda3b2 | 2015-06-03 16:25:55 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 171 | static int create_perf_stat_counter(struct perf_evsel *evsel) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 172 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 173 | struct perf_event_attr *attr = &evsel->attr; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 174 | |
Jiri Olsa | 711a572 | 2015-07-21 14:31:23 +0200 | [diff] [blame] | 175 | if (stat_config.scale) |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 176 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 177 | PERF_FORMAT_TOTAL_TIME_RUNNING; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 178 | |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 179 | attr->inherit = !no_inherit; |
Arnaldo Carvalho de Melo | 5d2cd90 | 2011-04-14 11:20:14 -0300 | [diff] [blame] | 180 | |
Jiri Olsa | 6acd8e9 | 2015-11-25 16:36:54 +0100 | [diff] [blame] | 181 | /* |
| 182 | * Some events get initialized with sample_(period/type) set, |
| 183 | * like tracepoints. Clear it up for counting. |
| 184 | */ |
| 185 | attr->sample_period = 0; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 186 | /* |
| 187 | * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless |
| 188 | * while avoiding that older tools show confusing messages. |
| 189 | */ |
| 190 | attr->sample_type = PERF_SAMPLE_IDENTIFIER; |
Jiri Olsa | 6acd8e9 | 2015-11-25 16:36:54 +0100 | [diff] [blame] | 191 | |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 192 | /* |
| 193 | * Disabling all counters initially, they will be enabled |
| 194 | * either manually by us or by kernel via enable_on_exec |
| 195 | * set later. |
| 196 | */ |
Jiri Olsa | c8280ce | 2015-12-03 10:06:45 +0100 | [diff] [blame] | 197 | if (perf_evsel__is_group_leader(evsel)) { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 198 | attr->disabled = 1; |
| 199 | |
Jiri Olsa | c8280ce | 2015-12-03 10:06:45 +0100 | [diff] [blame] | 200 | /* |
| 201 | * In case of initial_delay we enable tracee |
| 202 | * events manually. |
| 203 | */ |
| 204 | if (target__none(&target) && !initial_delay) |
| 205 | attr->enable_on_exec = 1; |
| 206 | } |
| 207 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 208 | if (target__has_cpu(&target)) |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 209 | return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); |
Stephane Eranian | 5622c07 | 2012-04-27 14:45:38 +0200 | [diff] [blame] | 210 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 211 | return perf_evsel__open_per_thread(evsel, evsel_list->threads); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Ingo Molnar | c04f5e5 | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 214 | /* |
| 215 | * Does the counter have nsecs as a unit? |
| 216 | */ |
Arnaldo Carvalho de Melo | daec78a | 2011-01-03 16:49:44 -0200 | [diff] [blame] | 217 | static inline int nsec_counter(struct perf_evsel *evsel) |
Ingo Molnar | c04f5e5 | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 218 | { |
Arnaldo Carvalho de Melo | daec78a | 2011-01-03 16:49:44 -0200 | [diff] [blame] | 219 | if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || |
| 220 | perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) |
Ingo Molnar | c04f5e5 | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 221 | return 1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 226 | static int process_synthesized_event(struct perf_tool *tool __maybe_unused, |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 227 | union perf_event *event, |
| 228 | struct perf_sample *sample __maybe_unused, |
| 229 | struct machine *machine __maybe_unused) |
| 230 | { |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 231 | if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) { |
| 232 | pr_err("failed to write perf data, error: %m\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | perf_stat.bytes_written += event->header.size; |
| 237 | return 0; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 240 | static int write_stat_round_event(u64 tm, u64 type) |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 241 | { |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 242 | return perf_event__synthesize_stat_round(NULL, tm, type, |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 243 | process_synthesized_event, |
| 244 | NULL); |
| 245 | } |
| 246 | |
| 247 | #define WRITE_STAT_ROUND_EVENT(time, interval) \ |
| 248 | write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval) |
| 249 | |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 250 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
| 251 | |
| 252 | static int |
| 253 | perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread, |
| 254 | struct perf_counts_values *count) |
| 255 | { |
| 256 | struct perf_sample_id *sid = SID(counter, cpu, thread); |
| 257 | |
| 258 | return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count, |
| 259 | process_synthesized_event, NULL); |
| 260 | } |
| 261 | |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 262 | /* |
| 263 | * Read out the results of a single counter: |
| 264 | * do not aggregate counts across CPUs in system-wide mode |
| 265 | */ |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 266 | static int read_counter(struct perf_evsel *counter) |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 267 | { |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 268 | int nthreads = thread_map__nr(evsel_list->threads); |
| 269 | int ncpus = perf_evsel__nr_cpus(counter); |
| 270 | int cpu, thread; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 271 | |
Suzuki K. Poulose | 3b4331d | 2015-02-13 18:40:58 +0000 | [diff] [blame] | 272 | if (!counter->supported) |
| 273 | return -ENOENT; |
| 274 | |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 275 | if (counter->system_wide) |
| 276 | nthreads = 1; |
| 277 | |
| 278 | for (thread = 0; thread < nthreads; thread++) { |
| 279 | for (cpu = 0; cpu < ncpus; cpu++) { |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 280 | struct perf_counts_values *count; |
| 281 | |
| 282 | count = perf_counts(counter->counts, cpu, thread); |
| 283 | if (perf_evsel__read(counter, cpu, thread, count)) |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 284 | return -1; |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 285 | |
| 286 | if (STAT_RECORD) { |
| 287 | if (perf_evsel__write_stat_event(counter, cpu, thread, count)) { |
| 288 | pr_err("failed to write stat event\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | } |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 292 | } |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 293 | } |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 294 | |
| 295 | return 0; |
Ingo Molnar | 2996f5d | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Jiri Olsa | 5fc472a | 2015-07-08 13:17:31 +0200 | [diff] [blame] | 298 | static void read_counters(bool close_counters) |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 299 | { |
| 300 | struct perf_evsel *counter; |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 301 | |
| 302 | evlist__for_each(evsel_list, counter) { |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 303 | if (read_counter(counter)) |
Andi Kleen | 245bad8 | 2015-09-01 15:52:46 -0700 | [diff] [blame] | 304 | pr_debug("failed to read counter %s\n", counter->name); |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 305 | |
Jiri Olsa | f80010e | 2015-07-21 14:31:27 +0200 | [diff] [blame] | 306 | if (perf_stat_process_counter(&stat_config, counter)) |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 307 | pr_warning("failed to process counter %s\n", counter->name); |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 308 | |
Jiri Olsa | 5fc472a | 2015-07-08 13:17:31 +0200 | [diff] [blame] | 309 | if (close_counters) { |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 310 | perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), |
| 311 | thread_map__nr(evsel_list->threads)); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Jiri Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 316 | static void process_interval(void) |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 317 | { |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 318 | struct timespec ts, rs; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 319 | |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 320 | read_counters(false); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 321 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 322 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 323 | diff_timespec(&rs, &ts, &ref_time); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 324 | |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 325 | if (STAT_RECORD) { |
| 326 | if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL)) |
| 327 | pr_err("failed to write stat round event\n"); |
| 328 | } |
| 329 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 330 | print_counters(&rs, 0, NULL); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 333 | static void enable_counters(void) |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 334 | { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 335 | if (initial_delay) |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 336 | usleep(initial_delay * 1000); |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * We need to enable counters only if: |
| 340 | * - we don't have tracee (attaching to task or cpu) |
| 341 | * - we have initial delay configured |
| 342 | */ |
| 343 | if (!target__none(&target) || initial_delay) |
Jiri Olsa | ab46db0 | 2015-12-03 10:06:43 +0100 | [diff] [blame] | 344 | perf_evlist__enable(evsel_list); |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 347 | static volatile int workload_exec_errno; |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * perf_evlist__prepare_workload will send a SIGUSR1 |
| 351 | * if the fork fails, since we asked by setting its |
| 352 | * want_signal to true. |
| 353 | */ |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 354 | static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, |
| 355 | void *ucontext __maybe_unused) |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 356 | { |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 357 | workload_exec_errno = info->si_value.sival_int; |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 358 | } |
| 359 | |
Jiri Olsa | 7b60a7e | 2015-11-05 15:40:54 +0100 | [diff] [blame] | 360 | static bool has_unit(struct perf_evsel *counter) |
| 361 | { |
| 362 | return counter->unit && *counter->unit; |
| 363 | } |
| 364 | |
| 365 | static bool has_scale(struct perf_evsel *counter) |
| 366 | { |
| 367 | return counter->scale != 1; |
| 368 | } |
| 369 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 370 | static int perf_stat_synthesize_config(bool is_pipe) |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 371 | { |
Jiri Olsa | 7b60a7e | 2015-11-05 15:40:54 +0100 | [diff] [blame] | 372 | struct perf_evsel *counter; |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 373 | int err; |
| 374 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 375 | if (is_pipe) { |
| 376 | err = perf_event__synthesize_attrs(NULL, perf_stat.session, |
| 377 | process_synthesized_event); |
| 378 | if (err < 0) { |
| 379 | pr_err("Couldn't synthesize attrs.\n"); |
| 380 | return err; |
| 381 | } |
| 382 | } |
| 383 | |
Jiri Olsa | 7b60a7e | 2015-11-05 15:40:54 +0100 | [diff] [blame] | 384 | /* |
| 385 | * Synthesize other events stuff not carried within |
| 386 | * attr event - unit, scale, name |
| 387 | */ |
| 388 | evlist__for_each(evsel_list, counter) { |
| 389 | if (!counter->supported) |
| 390 | continue; |
| 391 | |
| 392 | /* |
| 393 | * Synthesize unit and scale only if it's defined. |
| 394 | */ |
| 395 | if (has_unit(counter)) { |
| 396 | err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event); |
| 397 | if (err < 0) { |
| 398 | pr_err("Couldn't synthesize evsel unit.\n"); |
| 399 | return err; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (has_scale(counter)) { |
| 404 | err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event); |
| 405 | if (err < 0) { |
| 406 | pr_err("Couldn't synthesize evsel scale.\n"); |
| 407 | return err; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (counter->own_cpus) { |
| 412 | err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event); |
| 413 | if (err < 0) { |
| 414 | pr_err("Couldn't synthesize evsel scale.\n"); |
| 415 | return err; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Name is needed only for pipe output, |
| 421 | * perf.data carries event names. |
| 422 | */ |
| 423 | if (is_pipe) { |
| 424 | err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event); |
| 425 | if (err < 0) { |
| 426 | pr_err("Couldn't synthesize evsel name.\n"); |
| 427 | return err; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 432 | err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads, |
| 433 | process_synthesized_event, |
| 434 | NULL); |
| 435 | if (err < 0) { |
| 436 | pr_err("Couldn't synthesize thread map.\n"); |
| 437 | return err; |
| 438 | } |
| 439 | |
| 440 | err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus, |
| 441 | process_synthesized_event, NULL); |
| 442 | if (err < 0) { |
| 443 | pr_err("Couldn't synthesize thread map.\n"); |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | err = perf_event__synthesize_stat_config(NULL, &stat_config, |
| 448 | process_synthesized_event, NULL); |
| 449 | if (err < 0) { |
| 450 | pr_err("Couldn't synthesize config.\n"); |
| 451 | return err; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 457 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
| 458 | |
| 459 | static int __store_counter_ids(struct perf_evsel *counter, |
| 460 | struct cpu_map *cpus, |
| 461 | struct thread_map *threads) |
| 462 | { |
| 463 | int cpu, thread; |
| 464 | |
| 465 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
| 466 | for (thread = 0; thread < threads->nr; thread++) { |
| 467 | int fd = FD(counter, cpu, thread); |
| 468 | |
| 469 | if (perf_evlist__id_add_fd(evsel_list, counter, |
| 470 | cpu, thread, fd) < 0) |
| 471 | return -1; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static int store_counter_ids(struct perf_evsel *counter) |
| 479 | { |
| 480 | struct cpu_map *cpus = counter->cpus; |
| 481 | struct thread_map *threads = counter->threads; |
| 482 | |
| 483 | if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr)) |
| 484 | return -ENOMEM; |
| 485 | |
| 486 | return __store_counter_ids(counter, cpus, threads); |
| 487 | } |
| 488 | |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 489 | static int __run_perf_stat(int argc, const char **argv) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 490 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 491 | int interval = stat_config.interval; |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 492 | char msg[512]; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 493 | unsigned long long t0, t1; |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 494 | struct perf_evsel *counter; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 495 | struct timespec ts; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 496 | size_t l; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 497 | int status = 0; |
Zhang, Yanmin | 6be2850 | 2010-03-18 11:36:03 -0300 | [diff] [blame] | 498 | const bool forks = (argc > 0); |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 499 | bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 500 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 501 | if (interval) { |
| 502 | ts.tv_sec = interval / 1000; |
| 503 | ts.tv_nsec = (interval % 1000) * 1000000; |
| 504 | } else { |
| 505 | ts.tv_sec = 1; |
| 506 | ts.tv_nsec = 0; |
| 507 | } |
| 508 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 509 | if (forks) { |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 510 | if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 511 | workload_exec_failed_signal) < 0) { |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 512 | perror("failed to prepare workload"); |
| 513 | return -1; |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 514 | } |
Namhyung Kim | d20a47e | 2013-09-30 18:01:11 +0900 | [diff] [blame] | 515 | child_pid = evsel_list->workload.pid; |
Paul Mackerras | 051ae7f | 2009-06-29 21:13:21 +1000 | [diff] [blame] | 516 | } |
| 517 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 518 | if (group) |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 519 | perf_evlist__set_leader(evsel_list); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 520 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 521 | evlist__for_each(evsel_list, counter) { |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 522 | if (create_perf_stat_counter(counter) < 0) { |
David Ahern | 979987a | 2012-05-08 09:29:16 -0600 | [diff] [blame] | 523 | /* |
| 524 | * PPC returns ENXIO for HW counters until 2.6.37 |
| 525 | * (behavior changed with commit b0a873e). |
| 526 | */ |
Anton Blanchard | 38f6ae1 | 2011-12-02 09:38:33 +1100 | [diff] [blame] | 527 | if (errno == EINVAL || errno == ENOSYS || |
David Ahern | 979987a | 2012-05-08 09:29:16 -0600 | [diff] [blame] | 528 | errno == ENOENT || errno == EOPNOTSUPP || |
| 529 | errno == ENXIO) { |
David Ahern | c63ca0c | 2011-04-29 16:04:15 -0600 | [diff] [blame] | 530 | if (verbose) |
| 531 | ui__warning("%s event is not supported by the kernel.\n", |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 532 | perf_evsel__name(counter)); |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 533 | counter->supported = false; |
Kan Liang | cb5ef60 | 2015-06-11 02:32:40 -0400 | [diff] [blame] | 534 | |
| 535 | if ((counter->leader != counter) || |
| 536 | !(counter->leader->nr_members > 1)) |
| 537 | continue; |
David Ahern | c63ca0c | 2011-04-29 16:04:15 -0600 | [diff] [blame] | 538 | } |
Ingo Molnar | ede7029 | 2011-04-28 08:48:42 +0200 | [diff] [blame] | 539 | |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 540 | perf_evsel__open_strerror(counter, &target, |
| 541 | errno, msg, sizeof(msg)); |
| 542 | ui__error("%s\n", msg); |
| 543 | |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 544 | if (child_pid != -1) |
| 545 | kill(child_pid, SIGTERM); |
David Ahern | fceda7f | 2012-08-26 12:24:44 -0600 | [diff] [blame] | 546 | |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 547 | return -1; |
| 548 | } |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 549 | counter->supported = true; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 550 | |
| 551 | l = strlen(counter->unit); |
| 552 | if (l > unit_width) |
| 553 | unit_width = l; |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 554 | |
| 555 | if (STAT_RECORD && store_counter_ids(counter)) |
| 556 | return -1; |
Arnaldo Carvalho de Melo | 084ab9f | 2010-03-22 13:10:28 -0300 | [diff] [blame] | 557 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 558 | |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 559 | if (perf_evlist__apply_filters(evsel_list, &counter)) { |
| 560 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", |
| 561 | counter->filter, perf_evsel__name(counter), errno, |
Masami Hiramatsu | 759e612 | 2014-08-14 02:22:55 +0000 | [diff] [blame] | 562 | strerror_r(errno, msg, sizeof(msg))); |
Frederic Weisbecker | cfd748a | 2011-03-14 16:40:30 +0100 | [diff] [blame] | 563 | return -1; |
| 564 | } |
| 565 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 566 | if (STAT_RECORD) { |
| 567 | int err, fd = perf_data_file__fd(&perf_stat.file); |
| 568 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 569 | if (is_pipe) { |
| 570 | err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file)); |
| 571 | } else { |
| 572 | err = perf_session__write_header(perf_stat.session, evsel_list, |
| 573 | fd, false); |
| 574 | } |
| 575 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 576 | if (err < 0) |
| 577 | return err; |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 578 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 579 | err = perf_stat_synthesize_config(is_pipe); |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 580 | if (err < 0) |
| 581 | return err; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 584 | /* |
| 585 | * Enable counters and exec the command: |
| 586 | */ |
| 587 | t0 = rdclock(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 588 | clock_gettime(CLOCK_MONOTONIC, &ref_time); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 589 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 590 | if (forks) { |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 591 | perf_evlist__start_workload(evsel_list); |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 592 | enable_counters(); |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 593 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 594 | if (interval) { |
| 595 | while (!waitpid(child_pid, &status, WNOHANG)) { |
| 596 | nanosleep(&ts, NULL); |
Jiri Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 597 | process_interval(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 598 | } |
| 599 | } |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 600 | wait(&status); |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 601 | |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 602 | if (workload_exec_errno) { |
| 603 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); |
| 604 | pr_err("Workload failed: %s\n", emsg); |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 605 | return -1; |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 606 | } |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 607 | |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 608 | if (WIFSIGNALED(status)) |
| 609 | psignal(WTERMSIG(status), argv[0]); |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 610 | } else { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 611 | enable_counters(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 612 | while (!done) { |
| 613 | nanosleep(&ts, NULL); |
| 614 | if (interval) |
Jiri Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 615 | process_interval(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 616 | } |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 617 | } |
Ingo Molnar | 44db76c | 2009-06-03 19:36:07 +0200 | [diff] [blame] | 618 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 619 | t1 = rdclock(); |
| 620 | |
Peter Zijlstra | 9e9772c | 2009-09-04 15:36:08 +0200 | [diff] [blame] | 621 | update_stats(&walltime_nsecs_stats, t1 - t0); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 622 | |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 623 | read_counters(true); |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 624 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 625 | return WEXITSTATUS(status); |
| 626 | } |
| 627 | |
Arnaldo Carvalho de Melo | 41cde47 | 2014-01-03 17:34:42 -0300 | [diff] [blame] | 628 | static int run_perf_stat(int argc, const char **argv) |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 629 | { |
| 630 | int ret; |
| 631 | |
| 632 | if (pre_cmd) { |
| 633 | ret = system(pre_cmd); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | if (sync_run) |
| 639 | sync(); |
| 640 | |
| 641 | ret = __run_perf_stat(argc, argv); |
| 642 | if (ret) |
| 643 | return ret; |
| 644 | |
| 645 | if (post_cmd) { |
| 646 | ret = system(post_cmd); |
| 647 | if (ret) |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | return ret; |
| 652 | } |
| 653 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 654 | static void print_running(u64 run, u64 ena) |
| 655 | { |
| 656 | if (csv_output) { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 657 | fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f", |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 658 | csv_sep, |
| 659 | run, |
| 660 | csv_sep, |
| 661 | ena ? 100.0 * run / ena : 100.0); |
| 662 | } else if (run != ena) { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 663 | fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena); |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 667 | static void print_noise_pct(double total, double avg) |
| 668 | { |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 669 | double pct = rel_stddev_stats(total, avg); |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 670 | |
Zhengyu He | 3ae9a34d | 2011-06-23 13:45:42 -0700 | [diff] [blame] | 671 | if (csv_output) |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 672 | fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct); |
Jim Cromie | a1bca6c | 2011-09-07 17:14:02 -0600 | [diff] [blame] | 673 | else if (pct) |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 674 | fprintf(stat_config.output, " ( +-%6.2f%% )", pct); |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 677 | static void print_noise(struct perf_evsel *evsel, double avg) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 678 | { |
Jiri Olsa | 581cc8a | 2015-10-16 12:41:03 +0200 | [diff] [blame] | 679 | struct perf_stat_evsel *ps; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 680 | |
Peter Zijlstra | 849abde | 2009-09-04 18:23:38 +0200 | [diff] [blame] | 681 | if (run_count == 1) |
| 682 | return; |
| 683 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 684 | ps = evsel->priv; |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 685 | print_noise_pct(stddev_stats(&ps->res_stats[0]), avg); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 688 | static void aggr_printout(struct perf_evsel *evsel, int id, int nr) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 689 | { |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 690 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 691 | case AGGR_CORE: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 692 | fprintf(stat_config.output, "S%d-C%*d%s%*d%s", |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 693 | cpu_map__id_to_socket(id), |
| 694 | csv_output ? 0 : -8, |
| 695 | cpu_map__id_to_cpu(id), |
| 696 | csv_sep, |
| 697 | csv_output ? 0 : 4, |
| 698 | nr, |
| 699 | csv_sep); |
| 700 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 701 | case AGGR_SOCKET: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 702 | fprintf(stat_config.output, "S%*d%s%*d%s", |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 703 | csv_output ? 0 : -5, |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 704 | id, |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 705 | csv_sep, |
| 706 | csv_output ? 0 : 4, |
| 707 | nr, |
| 708 | csv_sep); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 709 | break; |
| 710 | case AGGR_NONE: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 711 | fprintf(stat_config.output, "CPU%*d%s", |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 712 | csv_output ? 0 : -4, |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 713 | perf_evsel__cpus(evsel)->map[id], csv_sep); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 714 | break; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 715 | case AGGR_THREAD: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 716 | fprintf(stat_config.output, "%*s-%*d%s", |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 717 | csv_output ? 0 : 16, |
| 718 | thread_map__comm(evsel->threads, id), |
| 719 | csv_output ? 0 : -8, |
| 720 | thread_map__pid(evsel->threads, id), |
| 721 | csv_sep); |
| 722 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 723 | case AGGR_GLOBAL: |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 724 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 725 | default: |
| 726 | break; |
| 727 | } |
| 728 | } |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 729 | |
Andi Kleen | da88c7f | 2014-09-24 13:50:46 -0700 | [diff] [blame] | 730 | static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 731 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 732 | FILE *output = stat_config.output; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 733 | double msecs = avg / 1e6; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 734 | const char *fmt_v, *fmt_n; |
David Ahern | 4bbe5a6 | 2013-09-28 14:28:00 -0600 | [diff] [blame] | 735 | char name[25]; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 736 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 737 | fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; |
| 738 | fmt_n = csv_output ? "%s" : "%-25s"; |
| 739 | |
Andi Kleen | da88c7f | 2014-09-24 13:50:46 -0700 | [diff] [blame] | 740 | aggr_printout(evsel, id, nr); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 741 | |
David Ahern | 4bbe5a6 | 2013-09-28 14:28:00 -0600 | [diff] [blame] | 742 | scnprintf(name, sizeof(name), "%s%s", |
| 743 | perf_evsel__name(evsel), csv_output ? "" : " (msec)"); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 744 | |
| 745 | fprintf(output, fmt_v, msecs, csv_sep); |
| 746 | |
| 747 | if (csv_output) |
| 748 | fprintf(output, "%s%s", evsel->unit, csv_sep); |
| 749 | else |
| 750 | fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep); |
| 751 | |
| 752 | fprintf(output, fmt_n, name); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 753 | |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 754 | if (evsel->cgrp) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 755 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 758 | static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
| 759 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 760 | FILE *output = stat_config.output; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 761 | double sc = evsel->scale; |
| 762 | const char *fmt; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 763 | |
| 764 | if (csv_output) { |
| 765 | fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s"; |
| 766 | } else { |
| 767 | if (big_num) |
| 768 | fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s"; |
| 769 | else |
| 770 | fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s"; |
| 771 | } |
| 772 | |
| 773 | aggr_printout(evsel, id, nr); |
| 774 | |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 775 | fprintf(output, fmt, avg, csv_sep); |
| 776 | |
| 777 | if (evsel->unit) |
| 778 | fprintf(output, "%-*s%s", |
| 779 | csv_output ? 0 : unit_width, |
| 780 | evsel->unit, csv_sep); |
| 781 | |
| 782 | fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel)); |
| 783 | |
| 784 | if (evsel->cgrp) |
| 785 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 786 | } |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 787 | |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 788 | static void printout(int id, int nr, struct perf_evsel *counter, double uval) |
| 789 | { |
| 790 | int cpu = cpu_map__id_to_cpu(id); |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 791 | |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 792 | if (stat_config.aggr_mode == AGGR_GLOBAL) |
| 793 | cpu = 0; |
| 794 | |
| 795 | if (nsec_counter(counter)) |
| 796 | nsec_printout(id, nr, counter, uval); |
| 797 | else |
| 798 | abs_printout(id, nr, counter, uval); |
| 799 | |
| 800 | if (!csv_output && !stat_config.interval) |
| 801 | perf_stat__print_shadow_stats(stat_config.output, counter, |
| 802 | uval, cpu, |
| 803 | stat_config.aggr_mode); |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 806 | static void print_aggr(char *prefix) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 807 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 808 | FILE *output = stat_config.output; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 809 | struct perf_evsel *counter; |
Kan Liang | 601083c | 2015-07-02 03:08:43 -0400 | [diff] [blame] | 810 | int cpu, s, s2, id, nr; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 811 | double uval; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 812 | u64 ena, run, val; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 813 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 814 | if (!(aggr_map || aggr_get_id)) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 815 | return; |
| 816 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 817 | for (s = 0; s < aggr_map->nr; s++) { |
| 818 | id = aggr_map->map[s]; |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 819 | evlist__for_each(evsel_list, counter) { |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 820 | val = ena = run = 0; |
| 821 | nr = 0; |
| 822 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
Kan Liang | 601083c | 2015-07-02 03:08:43 -0400 | [diff] [blame] | 823 | s2 = aggr_get_id(perf_evsel__cpus(counter), cpu); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 824 | if (s2 != id) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 825 | continue; |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 826 | val += perf_counts(counter->counts, cpu, 0)->val; |
| 827 | ena += perf_counts(counter->counts, cpu, 0)->ena; |
| 828 | run += perf_counts(counter->counts, cpu, 0)->run; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 829 | nr++; |
| 830 | } |
| 831 | if (prefix) |
| 832 | fprintf(output, "%s", prefix); |
| 833 | |
| 834 | if (run == 0 || ena == 0) { |
Stephane Eranian | 582ec082 | 2013-07-05 19:06:45 +0200 | [diff] [blame] | 835 | aggr_printout(counter, id, nr); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 836 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 837 | fprintf(output, "%*s%s", |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 838 | csv_output ? 0 : 18, |
| 839 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 840 | csv_sep); |
| 841 | |
| 842 | fprintf(output, "%-*s%s", |
| 843 | csv_output ? 0 : unit_width, |
| 844 | counter->unit, csv_sep); |
| 845 | |
| 846 | fprintf(output, "%*s", |
| 847 | csv_output ? 0 : -25, |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 848 | perf_evsel__name(counter)); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 849 | |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 850 | if (counter->cgrp) |
| 851 | fprintf(output, "%s%s", |
| 852 | csv_sep, counter->cgrp->name); |
| 853 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 854 | print_running(run, ena); |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 855 | fputc('\n', output); |
| 856 | continue; |
| 857 | } |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 858 | uval = val * counter->scale; |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 859 | printout(id, nr, counter, uval); |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 860 | if (!csv_output) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 861 | print_noise(counter, 1.0); |
| 862 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 863 | print_running(run, ena); |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 864 | fputc('\n', output); |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 869 | static void print_aggr_thread(struct perf_evsel *counter, char *prefix) |
| 870 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 871 | FILE *output = stat_config.output; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 872 | int nthreads = thread_map__nr(counter->threads); |
| 873 | int ncpus = cpu_map__nr(counter->cpus); |
| 874 | int cpu, thread; |
| 875 | double uval; |
| 876 | |
| 877 | for (thread = 0; thread < nthreads; thread++) { |
| 878 | u64 ena = 0, run = 0, val = 0; |
| 879 | |
| 880 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 881 | val += perf_counts(counter->counts, cpu, thread)->val; |
| 882 | ena += perf_counts(counter->counts, cpu, thread)->ena; |
| 883 | run += perf_counts(counter->counts, cpu, thread)->run; |
| 884 | } |
| 885 | |
| 886 | if (prefix) |
| 887 | fprintf(output, "%s", prefix); |
| 888 | |
| 889 | uval = val * counter->scale; |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 890 | printout(thread, 0, counter, uval); |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 891 | |
| 892 | if (!csv_output) |
| 893 | print_noise(counter, 1.0); |
| 894 | |
| 895 | print_running(run, ena); |
| 896 | fputc('\n', output); |
| 897 | } |
| 898 | } |
| 899 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 900 | /* |
| 901 | * Print out the results of a single counter: |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 902 | * aggregated counts in system-wide mode |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 903 | */ |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 904 | static void print_counter_aggr(struct perf_evsel *counter, char *prefix) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 905 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 906 | FILE *output = stat_config.output; |
Jiri Olsa | 581cc8a | 2015-10-16 12:41:03 +0200 | [diff] [blame] | 907 | struct perf_stat_evsel *ps = counter->priv; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 908 | double avg = avg_stats(&ps->res_stats[0]); |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 909 | int scaled = counter->counts->scaled; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 910 | double uval; |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 911 | double avg_enabled, avg_running; |
| 912 | |
| 913 | avg_enabled = avg_stats(&ps->res_stats[1]); |
| 914 | avg_running = avg_stats(&ps->res_stats[2]); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 915 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 916 | if (prefix) |
| 917 | fprintf(output, "%s", prefix); |
| 918 | |
Suzuki K. Poulose | 3b4331d | 2015-02-13 18:40:58 +0000 | [diff] [blame] | 919 | if (scaled == -1 || !counter->supported) { |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 920 | fprintf(output, "%*s%s", |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 921 | csv_output ? 0 : 18, |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 922 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 923 | csv_sep); |
| 924 | fprintf(output, "%-*s%s", |
| 925 | csv_output ? 0 : unit_width, |
| 926 | counter->unit, csv_sep); |
| 927 | fprintf(output, "%*s", |
| 928 | csv_output ? 0 : -25, |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 929 | perf_evsel__name(counter)); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 930 | |
| 931 | if (counter->cgrp) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 932 | fprintf(output, "%s%s", csv_sep, counter->cgrp->name); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 933 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 934 | print_running(avg_running, avg_enabled); |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 935 | fputc('\n', output); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 936 | return; |
| 937 | } |
| 938 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 939 | uval = avg * counter->scale; |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 940 | printout(-1, 0, counter, uval); |
Peter Zijlstra | 849abde | 2009-09-04 18:23:38 +0200 | [diff] [blame] | 941 | |
Zhengyu He | 3ae9a34d | 2011-06-23 13:45:42 -0700 | [diff] [blame] | 942 | print_noise(counter, avg); |
| 943 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 944 | print_running(avg_running, avg_enabled); |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 945 | fprintf(output, "\n"); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 948 | /* |
| 949 | * Print out the results of a single counter: |
| 950 | * does not use aggregated count in system-wide |
| 951 | */ |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 952 | static void print_counter(struct perf_evsel *counter, char *prefix) |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 953 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 954 | FILE *output = stat_config.output; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 955 | u64 ena, run, val; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 956 | double uval; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 957 | int cpu; |
| 958 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 959 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 960 | val = perf_counts(counter->counts, cpu, 0)->val; |
| 961 | ena = perf_counts(counter->counts, cpu, 0)->ena; |
| 962 | run = perf_counts(counter->counts, cpu, 0)->run; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 963 | |
| 964 | if (prefix) |
| 965 | fprintf(output, "%s", prefix); |
| 966 | |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 967 | if (run == 0 || ena == 0) { |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 968 | fprintf(output, "CPU%*d%s%*s%s", |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 969 | csv_output ? 0 : -4, |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 970 | perf_evsel__cpus(counter)->map[cpu], csv_sep, |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 971 | csv_output ? 0 : 18, |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 972 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 973 | csv_sep); |
| 974 | |
| 975 | fprintf(output, "%-*s%s", |
| 976 | csv_output ? 0 : unit_width, |
| 977 | counter->unit, csv_sep); |
| 978 | |
| 979 | fprintf(output, "%*s", |
| 980 | csv_output ? 0 : -25, |
| 981 | perf_evsel__name(counter)); |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 982 | |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 983 | if (counter->cgrp) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 984 | fprintf(output, "%s%s", |
| 985 | csv_sep, counter->cgrp->name); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 986 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 987 | print_running(run, ena); |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 988 | fputc('\n', output); |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 989 | continue; |
| 990 | } |
| 991 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 992 | uval = val * counter->scale; |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 993 | printout(cpu, 0, counter, uval); |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 994 | if (!csv_output) |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 995 | print_noise(counter, 1.0); |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 996 | print_running(run, ena); |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 997 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 998 | fputc('\n', output); |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1002 | static void print_interval(char *prefix, struct timespec *ts) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1003 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1004 | FILE *output = stat_config.output; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1005 | static int num_print_interval; |
| 1006 | |
| 1007 | sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep); |
| 1008 | |
| 1009 | if (num_print_interval == 0 && !csv_output) { |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1010 | switch (stat_config.aggr_mode) { |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1011 | case AGGR_SOCKET: |
| 1012 | fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit"); |
| 1013 | break; |
| 1014 | case AGGR_CORE: |
| 1015 | fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit"); |
| 1016 | break; |
| 1017 | case AGGR_NONE: |
| 1018 | fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit"); |
| 1019 | break; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1020 | case AGGR_THREAD: |
| 1021 | fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit"); |
| 1022 | break; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1023 | case AGGR_GLOBAL: |
| 1024 | default: |
| 1025 | fprintf(output, "# time counts %*s events\n", unit_width, "unit"); |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1026 | case AGGR_UNSET: |
| 1027 | break; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if (++num_print_interval == 25) |
| 1032 | num_print_interval = 0; |
| 1033 | } |
| 1034 | |
| 1035 | static void print_header(int argc, const char **argv) |
| 1036 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1037 | FILE *output = stat_config.output; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1038 | int i; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1039 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1040 | fflush(stdout); |
| 1041 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1042 | if (!csv_output) { |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1043 | fprintf(output, "\n"); |
| 1044 | fprintf(output, " Performance counter stats for "); |
David Ahern | 62d3b61 | 2013-09-28 14:27:58 -0600 | [diff] [blame] | 1045 | if (target.system_wide) |
| 1046 | fprintf(output, "\'system wide"); |
| 1047 | else if (target.cpu_list) |
| 1048 | fprintf(output, "\'CPU(s) %s", target.cpu_list); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1049 | else if (!target__has_task(&target)) { |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1050 | fprintf(output, "\'%s", argv ? argv[0] : "pipe"); |
| 1051 | for (i = 1; argv && (i < argc); i++) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1052 | fprintf(output, " %s", argv[i]); |
Namhyung Kim | 20f946b | 2012-04-26 14:15:16 +0900 | [diff] [blame] | 1053 | } else if (target.pid) |
| 1054 | fprintf(output, "process id \'%s", target.pid); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1055 | else |
Namhyung Kim | 20f946b | 2012-04-26 14:15:16 +0900 | [diff] [blame] | 1056 | fprintf(output, "thread id \'%s", target.tid); |
Ingo Molnar | 44db76c | 2009-06-03 19:36:07 +0200 | [diff] [blame] | 1057 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1058 | fprintf(output, "\'"); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1059 | if (run_count > 1) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1060 | fprintf(output, " (%d runs)", run_count); |
| 1061 | fprintf(output, ":\n\n"); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1062 | } |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | static void print_footer(void) |
| 1066 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1067 | FILE *output = stat_config.output; |
| 1068 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1069 | if (!null_run) |
| 1070 | fprintf(output, "\n"); |
| 1071 | fprintf(output, " %17.9f seconds time elapsed", |
| 1072 | avg_stats(&walltime_nsecs_stats)/1e9); |
| 1073 | if (run_count > 1) { |
| 1074 | fprintf(output, " "); |
| 1075 | print_noise_pct(stddev_stats(&walltime_nsecs_stats), |
| 1076 | avg_stats(&walltime_nsecs_stats)); |
| 1077 | } |
| 1078 | fprintf(output, "\n\n"); |
| 1079 | } |
| 1080 | |
| 1081 | static void print_counters(struct timespec *ts, int argc, const char **argv) |
| 1082 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 1083 | int interval = stat_config.interval; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1084 | struct perf_evsel *counter; |
| 1085 | char buf[64], *prefix = NULL; |
| 1086 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 1087 | /* Do not print anything if we record to the pipe. */ |
| 1088 | if (STAT_RECORD && perf_stat.file.is_pipe) |
| 1089 | return; |
| 1090 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1091 | if (interval) |
| 1092 | print_interval(prefix = buf, ts); |
| 1093 | else |
| 1094 | print_header(argc, argv); |
Ingo Molnar | 2996f5d | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 1095 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1096 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1097 | case AGGR_CORE: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1098 | case AGGR_SOCKET: |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1099 | print_aggr(prefix); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1100 | break; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1101 | case AGGR_THREAD: |
| 1102 | evlist__for_each(evsel_list, counter) |
| 1103 | print_aggr_thread(counter, prefix); |
| 1104 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1105 | case AGGR_GLOBAL: |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1106 | evlist__for_each(evsel_list, counter) |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1107 | print_counter_aggr(counter, prefix); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1108 | break; |
| 1109 | case AGGR_NONE: |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1110 | evlist__for_each(evsel_list, counter) |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1111 | print_counter(counter, prefix); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1112 | break; |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1113 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1114 | default: |
| 1115 | break; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1116 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1117 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1118 | if (!interval && !csv_output) |
| 1119 | print_footer(); |
| 1120 | |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1121 | fflush(stat_config.output); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1124 | static volatile int signr = -1; |
| 1125 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1126 | static void skip_signal(int signo) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1127 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 1128 | if ((child_pid == -1) || stat_config.interval) |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 1129 | done = 1; |
| 1130 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1131 | signr = signo; |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1132 | /* |
| 1133 | * render child_pid harmless |
| 1134 | * won't send SIGTERM to a random |
| 1135 | * process in case of race condition |
| 1136 | * and fast PID recycling |
| 1137 | */ |
| 1138 | child_pid = -1; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static void sig_atexit(void) |
| 1142 | { |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1143 | sigset_t set, oset; |
| 1144 | |
| 1145 | /* |
| 1146 | * avoid race condition with SIGCHLD handler |
| 1147 | * in skip_signal() which is modifying child_pid |
| 1148 | * goal is to avoid send SIGTERM to a random |
| 1149 | * process |
| 1150 | */ |
| 1151 | sigemptyset(&set); |
| 1152 | sigaddset(&set, SIGCHLD); |
| 1153 | sigprocmask(SIG_BLOCK, &set, &oset); |
| 1154 | |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 1155 | if (child_pid != -1) |
| 1156 | kill(child_pid, SIGTERM); |
| 1157 | |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1158 | sigprocmask(SIG_SETMASK, &oset, NULL); |
| 1159 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1160 | if (signr == -1) |
| 1161 | return; |
| 1162 | |
| 1163 | signal(signr, SIG_DFL); |
| 1164 | kill(getpid(), signr); |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1165 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1166 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1167 | static int stat__set_big_num(const struct option *opt __maybe_unused, |
| 1168 | const char *s __maybe_unused, int unset) |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1169 | { |
| 1170 | big_num_opt = unset ? 0 : 1; |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1174 | static const struct option stat_options[] = { |
| 1175 | OPT_BOOLEAN('T', "transaction", &transaction_run, |
| 1176 | "hardware transaction statistics"), |
| 1177 | OPT_CALLBACK('e', "event", &evsel_list, "event", |
| 1178 | "event selector. use 'perf list' to list available events", |
| 1179 | parse_events_option), |
| 1180 | OPT_CALLBACK(0, "filter", &evsel_list, "filter", |
| 1181 | "event filter", parse_filter), |
| 1182 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, |
| 1183 | "child tasks do not inherit counters"), |
| 1184 | OPT_STRING('p', "pid", &target.pid, "pid", |
| 1185 | "stat events on existing process id"), |
| 1186 | OPT_STRING('t', "tid", &target.tid, "tid", |
| 1187 | "stat events on existing thread id"), |
| 1188 | OPT_BOOLEAN('a', "all-cpus", &target.system_wide, |
| 1189 | "system-wide collection from all CPUs"), |
| 1190 | OPT_BOOLEAN('g', "group", &group, |
| 1191 | "put the counters into a counter group"), |
| 1192 | OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"), |
| 1193 | OPT_INCR('v', "verbose", &verbose, |
| 1194 | "be more verbose (show counter open errors, etc)"), |
| 1195 | OPT_INTEGER('r', "repeat", &run_count, |
| 1196 | "repeat command and print average + stddev (max: 100, forever: 0)"), |
| 1197 | OPT_BOOLEAN('n', "null", &null_run, |
| 1198 | "null run - dont start any counters"), |
| 1199 | OPT_INCR('d', "detailed", &detailed_run, |
| 1200 | "detailed run - start a lot of events"), |
| 1201 | OPT_BOOLEAN('S', "sync", &sync_run, |
| 1202 | "call sync() before starting a run"), |
| 1203 | OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, |
| 1204 | "print large numbers with thousands\' separators", |
| 1205 | stat__set_big_num), |
| 1206 | OPT_STRING('C', "cpu", &target.cpu_list, "cpu", |
| 1207 | "list of cpus to monitor in system-wide"), |
| 1208 | OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, |
| 1209 | "disable CPU count aggregation", AGGR_NONE), |
| 1210 | OPT_STRING('x', "field-separator", &csv_sep, "separator", |
| 1211 | "print counts with custom separator"), |
| 1212 | OPT_CALLBACK('G', "cgroup", &evsel_list, "name", |
| 1213 | "monitor event in cgroup name only", parse_cgroups), |
| 1214 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
| 1215 | OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), |
| 1216 | OPT_INTEGER(0, "log-fd", &output_fd, |
| 1217 | "log output to fd, instead of stderr"), |
| 1218 | OPT_STRING(0, "pre", &pre_cmd, "command", |
| 1219 | "command to run prior to the measured command"), |
| 1220 | OPT_STRING(0, "post", &post_cmd, "command", |
| 1221 | "command to run after to the measured command"), |
| 1222 | OPT_UINTEGER('I', "interval-print", &stat_config.interval, |
| 1223 | "print counts at regular interval in ms (>= 10)"), |
| 1224 | OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, |
| 1225 | "aggregate counts per processor socket", AGGR_SOCKET), |
| 1226 | OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, |
| 1227 | "aggregate counts per physical processor core", AGGR_CORE), |
| 1228 | OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, |
| 1229 | "aggregate counts per thread", AGGR_THREAD), |
| 1230 | OPT_UINTEGER('D', "delay", &initial_delay, |
| 1231 | "ms to wait before starting measurement after program start"), |
| 1232 | OPT_END() |
| 1233 | }; |
| 1234 | |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 1235 | static int perf_stat__get_socket(struct cpu_map *map, int cpu) |
| 1236 | { |
| 1237 | return cpu_map__get_socket(map, cpu, NULL); |
| 1238 | } |
| 1239 | |
| 1240 | static int perf_stat__get_core(struct cpu_map *map, int cpu) |
| 1241 | { |
| 1242 | return cpu_map__get_core(map, cpu, NULL); |
| 1243 | } |
| 1244 | |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1245 | static int cpu_map__get_max(struct cpu_map *map) |
| 1246 | { |
| 1247 | int i, max = -1; |
| 1248 | |
| 1249 | for (i = 0; i < map->nr; i++) { |
| 1250 | if (map->map[i] > max) |
| 1251 | max = map->map[i]; |
| 1252 | } |
| 1253 | |
| 1254 | return max; |
| 1255 | } |
| 1256 | |
| 1257 | static struct cpu_map *cpus_aggr_map; |
| 1258 | |
| 1259 | static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx) |
| 1260 | { |
| 1261 | int cpu; |
| 1262 | |
| 1263 | if (idx >= map->nr) |
| 1264 | return -1; |
| 1265 | |
| 1266 | cpu = map->map[idx]; |
| 1267 | |
| 1268 | if (cpus_aggr_map->map[cpu] == -1) |
| 1269 | cpus_aggr_map->map[cpu] = get_id(map, idx); |
| 1270 | |
| 1271 | return cpus_aggr_map->map[cpu]; |
| 1272 | } |
| 1273 | |
| 1274 | static int perf_stat__get_socket_cached(struct cpu_map *map, int idx) |
| 1275 | { |
| 1276 | return perf_stat__get_aggr(perf_stat__get_socket, map, idx); |
| 1277 | } |
| 1278 | |
| 1279 | static int perf_stat__get_core_cached(struct cpu_map *map, int idx) |
| 1280 | { |
| 1281 | return perf_stat__get_aggr(perf_stat__get_core, map, idx); |
| 1282 | } |
| 1283 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1284 | static int perf_stat_init_aggr_mode(void) |
| 1285 | { |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1286 | int nr; |
| 1287 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1288 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1289 | case AGGR_SOCKET: |
| 1290 | if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) { |
| 1291 | perror("cannot build socket map"); |
| 1292 | return -1; |
| 1293 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1294 | aggr_get_id = perf_stat__get_socket_cached; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1295 | break; |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1296 | case AGGR_CORE: |
| 1297 | if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) { |
| 1298 | perror("cannot build core map"); |
| 1299 | return -1; |
| 1300 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1301 | aggr_get_id = perf_stat__get_core_cached; |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1302 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1303 | case AGGR_NONE: |
| 1304 | case AGGR_GLOBAL: |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1305 | case AGGR_THREAD: |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1306 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1307 | default: |
| 1308 | break; |
| 1309 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1310 | |
| 1311 | /* |
| 1312 | * The evsel_list->cpus is the base we operate on, |
| 1313 | * taking the highest cpu number to be the size of |
| 1314 | * the aggregation translate cpumap. |
| 1315 | */ |
| 1316 | nr = cpu_map__get_max(evsel_list->cpus); |
| 1317 | cpus_aggr_map = cpu_map__empty_new(nr + 1); |
| 1318 | return cpus_aggr_map ? 0 : -ENOMEM; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 1321 | static void perf_stat__exit_aggr_mode(void) |
| 1322 | { |
| 1323 | cpu_map__put(aggr_map); |
| 1324 | cpu_map__put(cpus_aggr_map); |
| 1325 | aggr_map = NULL; |
| 1326 | cpus_aggr_map = NULL; |
| 1327 | } |
| 1328 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1329 | /* |
| 1330 | * Add default attributes, if there were no attributes specified or |
| 1331 | * if -d/--detailed, -d -d or -d -d -d is used: |
| 1332 | */ |
| 1333 | static int add_default_attributes(void) |
| 1334 | { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1335 | struct perf_event_attr default_attrs[] = { |
| 1336 | |
| 1337 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, |
| 1338 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, |
| 1339 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, |
| 1340 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, |
| 1341 | |
| 1342 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, |
| 1343 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, |
| 1344 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, |
| 1345 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, |
| 1346 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
| 1347 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, |
| 1348 | |
| 1349 | }; |
| 1350 | |
| 1351 | /* |
| 1352 | * Detailed stats (-d), covering the L1 and last level data caches: |
| 1353 | */ |
| 1354 | struct perf_event_attr detailed_attrs[] = { |
| 1355 | |
| 1356 | { .type = PERF_TYPE_HW_CACHE, |
| 1357 | .config = |
| 1358 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 1359 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1360 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 1361 | |
| 1362 | { .type = PERF_TYPE_HW_CACHE, |
| 1363 | .config = |
| 1364 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 1365 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1366 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 1367 | |
| 1368 | { .type = PERF_TYPE_HW_CACHE, |
| 1369 | .config = |
| 1370 | PERF_COUNT_HW_CACHE_LL << 0 | |
| 1371 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1372 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 1373 | |
| 1374 | { .type = PERF_TYPE_HW_CACHE, |
| 1375 | .config = |
| 1376 | PERF_COUNT_HW_CACHE_LL << 0 | |
| 1377 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1378 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 1379 | }; |
| 1380 | |
| 1381 | /* |
| 1382 | * Very detailed stats (-d -d), covering the instruction cache and the TLB caches: |
| 1383 | */ |
| 1384 | struct perf_event_attr very_detailed_attrs[] = { |
| 1385 | |
| 1386 | { .type = PERF_TYPE_HW_CACHE, |
| 1387 | .config = |
| 1388 | PERF_COUNT_HW_CACHE_L1I << 0 | |
| 1389 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1390 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 1391 | |
| 1392 | { .type = PERF_TYPE_HW_CACHE, |
| 1393 | .config = |
| 1394 | PERF_COUNT_HW_CACHE_L1I << 0 | |
| 1395 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1396 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 1397 | |
| 1398 | { .type = PERF_TYPE_HW_CACHE, |
| 1399 | .config = |
| 1400 | PERF_COUNT_HW_CACHE_DTLB << 0 | |
| 1401 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1402 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 1403 | |
| 1404 | { .type = PERF_TYPE_HW_CACHE, |
| 1405 | .config = |
| 1406 | PERF_COUNT_HW_CACHE_DTLB << 0 | |
| 1407 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1408 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 1409 | |
| 1410 | { .type = PERF_TYPE_HW_CACHE, |
| 1411 | .config = |
| 1412 | PERF_COUNT_HW_CACHE_ITLB << 0 | |
| 1413 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1414 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 1415 | |
| 1416 | { .type = PERF_TYPE_HW_CACHE, |
| 1417 | .config = |
| 1418 | PERF_COUNT_HW_CACHE_ITLB << 0 | |
| 1419 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 1420 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 1421 | |
| 1422 | }; |
| 1423 | |
| 1424 | /* |
| 1425 | * Very, very detailed stats (-d -d -d), adding prefetch events: |
| 1426 | */ |
| 1427 | struct perf_event_attr very_very_detailed_attrs[] = { |
| 1428 | |
| 1429 | { .type = PERF_TYPE_HW_CACHE, |
| 1430 | .config = |
| 1431 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 1432 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | |
| 1433 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 1434 | |
| 1435 | { .type = PERF_TYPE_HW_CACHE, |
| 1436 | .config = |
| 1437 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 1438 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | |
| 1439 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 1440 | }; |
| 1441 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1442 | /* Set attrs if no event is selected and !null_run: */ |
| 1443 | if (null_run) |
| 1444 | return 0; |
| 1445 | |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1446 | if (transaction_run) { |
| 1447 | int err; |
| 1448 | if (pmu_have_event("cpu", "cycles-ct") && |
| 1449 | pmu_have_event("cpu", "el-start")) |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 1450 | err = parse_events(evsel_list, transaction_attrs, NULL); |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1451 | else |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 1452 | err = parse_events(evsel_list, transaction_limited_attrs, NULL); |
| 1453 | if (err) { |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1454 | fprintf(stderr, "Cannot set up transaction events\n"); |
| 1455 | return -1; |
| 1456 | } |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1460 | if (!evsel_list->nr_entries) { |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 1461 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 1462 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* Detailed events get appended to the event list: */ |
| 1466 | |
| 1467 | if (detailed_run < 1) |
| 1468 | return 0; |
| 1469 | |
| 1470 | /* Append detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 1471 | if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 1472 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1473 | |
| 1474 | if (detailed_run < 2) |
| 1475 | return 0; |
| 1476 | |
| 1477 | /* Append very detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 1478 | if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 1479 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1480 | |
| 1481 | if (detailed_run < 3) |
| 1482 | return 0; |
| 1483 | |
| 1484 | /* Append very, very detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 1485 | return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1488 | static const char * const recort_usage[] = { |
| 1489 | "perf stat record [<options>]", |
| 1490 | NULL, |
| 1491 | }; |
| 1492 | |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 1493 | static void init_features(struct perf_session *session) |
| 1494 | { |
| 1495 | int feat; |
| 1496 | |
| 1497 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) |
| 1498 | perf_header__set_feat(&session->header, feat); |
| 1499 | |
| 1500 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); |
| 1501 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); |
| 1502 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); |
| 1503 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); |
| 1504 | } |
| 1505 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1506 | static int __cmd_record(int argc, const char **argv) |
| 1507 | { |
| 1508 | struct perf_session *session; |
| 1509 | struct perf_data_file *file = &perf_stat.file; |
| 1510 | |
| 1511 | argc = parse_options(argc, argv, stat_options, record_usage, |
| 1512 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1513 | |
| 1514 | if (output_name) |
| 1515 | file->path = output_name; |
| 1516 | |
Jiri Olsa | e9d6db8e8 | 2015-11-05 15:40:53 +0100 | [diff] [blame] | 1517 | if (run_count != 1 || forever) { |
| 1518 | pr_err("Cannot use -r option with perf stat record.\n"); |
| 1519 | return -1; |
| 1520 | } |
| 1521 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1522 | session = perf_session__new(file, false, NULL); |
| 1523 | if (session == NULL) { |
| 1524 | pr_err("Perf session creation failed.\n"); |
| 1525 | return -1; |
| 1526 | } |
| 1527 | |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 1528 | init_features(session); |
| 1529 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1530 | session->evlist = evsel_list; |
| 1531 | perf_stat.session = session; |
| 1532 | perf_stat.record = true; |
| 1533 | return argc; |
| 1534 | } |
| 1535 | |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame^] | 1536 | static |
| 1537 | int process_stat_config_event(struct perf_tool *tool __maybe_unused, |
| 1538 | union perf_event *event, |
| 1539 | struct perf_session *session __maybe_unused) |
| 1540 | { |
| 1541 | perf_event__read_stat_config(&stat_config, &event->stat_config); |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1545 | static int set_maps(struct perf_stat *st) |
| 1546 | { |
| 1547 | if (!st->cpus || !st->threads) |
| 1548 | return 0; |
| 1549 | |
| 1550 | if (WARN_ONCE(st->maps_allocated, "stats double allocation\n")) |
| 1551 | return -EINVAL; |
| 1552 | |
| 1553 | perf_evlist__set_maps(evsel_list, st->cpus, st->threads); |
| 1554 | |
| 1555 | if (perf_evlist__alloc_stats(evsel_list, true)) |
| 1556 | return -ENOMEM; |
| 1557 | |
| 1558 | st->maps_allocated = true; |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | static |
| 1563 | int process_thread_map_event(struct perf_tool *tool __maybe_unused, |
| 1564 | union perf_event *event, |
| 1565 | struct perf_session *session __maybe_unused) |
| 1566 | { |
| 1567 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
| 1568 | |
| 1569 | if (st->threads) { |
| 1570 | pr_warning("Extra thread map event, ignoring.\n"); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | st->threads = thread_map__new_event(&event->thread_map); |
| 1575 | if (!st->threads) |
| 1576 | return -ENOMEM; |
| 1577 | |
| 1578 | return set_maps(st); |
| 1579 | } |
| 1580 | |
| 1581 | static |
| 1582 | int process_cpu_map_event(struct perf_tool *tool __maybe_unused, |
| 1583 | union perf_event *event, |
| 1584 | struct perf_session *session __maybe_unused) |
| 1585 | { |
| 1586 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
| 1587 | struct cpu_map *cpus; |
| 1588 | |
| 1589 | if (st->cpus) { |
| 1590 | pr_warning("Extra cpu map event, ignoring.\n"); |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | cpus = cpu_map__new_data(&event->cpu_map.data); |
| 1595 | if (!cpus) |
| 1596 | return -ENOMEM; |
| 1597 | |
| 1598 | st->cpus = cpus; |
| 1599 | return set_maps(st); |
| 1600 | } |
| 1601 | |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1602 | static const char * const report_usage[] = { |
| 1603 | "perf stat report [<options>]", |
| 1604 | NULL, |
| 1605 | }; |
| 1606 | |
| 1607 | static struct perf_stat perf_stat = { |
| 1608 | .tool = { |
| 1609 | .attr = perf_event__process_attr, |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1610 | .thread_map = process_thread_map_event, |
| 1611 | .cpu_map = process_cpu_map_event, |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame^] | 1612 | .stat_config = process_stat_config_event, |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1613 | }, |
| 1614 | }; |
| 1615 | |
| 1616 | static int __cmd_report(int argc, const char **argv) |
| 1617 | { |
| 1618 | struct perf_session *session; |
| 1619 | const struct option options[] = { |
| 1620 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
| 1621 | OPT_END() |
| 1622 | }; |
| 1623 | struct stat st; |
| 1624 | int ret; |
| 1625 | |
| 1626 | argc = parse_options(argc, argv, options, report_usage, 0); |
| 1627 | |
| 1628 | if (!input_name || !strlen(input_name)) { |
| 1629 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) |
| 1630 | input_name = "-"; |
| 1631 | else |
| 1632 | input_name = "perf.data"; |
| 1633 | } |
| 1634 | |
| 1635 | perf_stat.file.path = input_name; |
| 1636 | perf_stat.file.mode = PERF_DATA_MODE_READ; |
| 1637 | |
| 1638 | session = perf_session__new(&perf_stat.file, false, &perf_stat.tool); |
| 1639 | if (session == NULL) |
| 1640 | return -1; |
| 1641 | |
| 1642 | perf_stat.session = session; |
| 1643 | stat_config.output = stderr; |
| 1644 | evsel_list = session->evlist; |
| 1645 | |
| 1646 | ret = perf_session__process_events(session); |
| 1647 | if (ret) |
| 1648 | return ret; |
| 1649 | |
| 1650 | perf_session__delete(session); |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1654 | int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1655 | { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1656 | const char * const stat_usage[] = { |
| 1657 | "perf stat [<options>] [<command>]", |
| 1658 | NULL |
| 1659 | }; |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1660 | int status = -EINVAL, run_idx; |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1661 | const char *mode; |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1662 | FILE *output = stderr; |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 1663 | unsigned int interval; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1664 | const char * const stat_subcommands[] = { "record", "report" }; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1665 | |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 1666 | setlocale(LC_ALL, ""); |
| 1667 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 1668 | evsel_list = perf_evlist__new(); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1669 | if (evsel_list == NULL) |
| 1670 | return -ENOMEM; |
| 1671 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1672 | argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands, |
| 1673 | (const char **) stat_usage, |
| 1674 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1675 | |
| 1676 | if (argc && !strncmp(argv[0], "rec", 3)) { |
| 1677 | argc = __cmd_record(argc, argv); |
| 1678 | if (argc < 0) |
| 1679 | return -1; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1680 | } else if (argc && !strncmp(argv[0], "rep", 3)) |
| 1681 | return __cmd_report(argc, argv); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1682 | |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 1683 | interval = stat_config.interval; |
| 1684 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1685 | /* |
| 1686 | * For record command the -o is already taken care of. |
| 1687 | */ |
| 1688 | if (!STAT_RECORD && output_name && strcmp(output_name, "-")) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1689 | output = NULL; |
| 1690 | |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 1691 | if (output_name && output_fd) { |
| 1692 | fprintf(stderr, "cannot use both --output and --log-fd\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1693 | parse_options_usage(stat_usage, stat_options, "o", 1); |
| 1694 | parse_options_usage(NULL, stat_options, "log-fd", 0); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1695 | goto out; |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 1696 | } |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 1697 | |
| 1698 | if (output_fd < 0) { |
| 1699 | fprintf(stderr, "argument to --log-fd must be a > 0\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1700 | parse_options_usage(stat_usage, stat_options, "log-fd", 0); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1701 | goto out; |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 1702 | } |
| 1703 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1704 | if (!output) { |
| 1705 | struct timespec tm; |
| 1706 | mode = append_file ? "a" : "w"; |
| 1707 | |
| 1708 | output = fopen(output_name, mode); |
| 1709 | if (!output) { |
| 1710 | perror("failed to create output file"); |
David Ahern | fceda7f | 2012-08-26 12:24:44 -0600 | [diff] [blame] | 1711 | return -1; |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1712 | } |
| 1713 | clock_gettime(CLOCK_REALTIME, &tm); |
| 1714 | fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 1715 | } else if (output_fd > 0) { |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 1716 | mode = append_file ? "a" : "w"; |
| 1717 | output = fdopen(output_fd, mode); |
| 1718 | if (!output) { |
| 1719 | perror("Failed opening logfd"); |
| 1720 | return -errno; |
| 1721 | } |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1724 | stat_config.output = output; |
| 1725 | |
Jim Cromie | d4ffd04 | 2011-09-07 17:14:03 -0600 | [diff] [blame] | 1726 | if (csv_sep) { |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1727 | csv_output = true; |
Jim Cromie | d4ffd04 | 2011-09-07 17:14:03 -0600 | [diff] [blame] | 1728 | if (!strcmp(csv_sep, "\\t")) |
| 1729 | csv_sep = "\t"; |
| 1730 | } else |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1731 | csv_sep = DEFAULT_SEPARATOR; |
| 1732 | |
| 1733 | /* |
| 1734 | * let the spreadsheet do the pretty-printing |
| 1735 | */ |
| 1736 | if (csv_output) { |
Jim Cromie | 61a9f32 | 2011-09-07 17:14:04 -0600 | [diff] [blame] | 1737 | /* User explicitly passed -B? */ |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1738 | if (big_num_opt == 1) { |
| 1739 | fprintf(stderr, "-B option not supported with -x\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1740 | parse_options_usage(stat_usage, stat_options, "B", 1); |
| 1741 | parse_options_usage(NULL, stat_options, "x", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1742 | goto out; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1743 | } else /* Nope, so disable big number formatting */ |
| 1744 | big_num = false; |
| 1745 | } else if (big_num_opt == 0) /* User passed --no-big-num */ |
| 1746 | big_num = false; |
| 1747 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1748 | if (!argc && target__none(&target)) |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1749 | usage_with_options(stat_usage, stat_options); |
David Ahern | ac3063b | 2013-09-30 07:37:37 -0600 | [diff] [blame] | 1750 | |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1751 | if (run_count < 0) { |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1752 | pr_err("Run count must be a positive number\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1753 | parse_options_usage(stat_usage, stat_options, "r", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1754 | goto out; |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1755 | } else if (run_count == 0) { |
| 1756 | forever = true; |
| 1757 | run_count = 1; |
| 1758 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1759 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1760 | if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) { |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1761 | fprintf(stderr, "The --per-thread option is only available " |
| 1762 | "when monitoring via -p -t options.\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1763 | parse_options_usage(NULL, stat_options, "p", 1); |
| 1764 | parse_options_usage(NULL, stat_options, "t", 1); |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1765 | goto out; |
| 1766 | } |
| 1767 | |
| 1768 | /* |
| 1769 | * no_aggr, cgroup are for system-wide only |
| 1770 | * --per-thread is aggregated per thread, we dont mix it with cpu mode |
| 1771 | */ |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1772 | if (((stat_config.aggr_mode != AGGR_GLOBAL && |
| 1773 | stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) && |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1774 | !target__has_cpu(&target)) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1775 | fprintf(stderr, "both cgroup and no-aggregation " |
| 1776 | "modes only available in system-wide mode\n"); |
| 1777 | |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1778 | parse_options_usage(stat_usage, stat_options, "G", 1); |
| 1779 | parse_options_usage(NULL, stat_options, "A", 1); |
| 1780 | parse_options_usage(NULL, stat_options, "a", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1781 | goto out; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1784 | if (add_default_attributes()) |
| 1785 | goto out; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1786 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1787 | target__validate(&target); |
Arnaldo Carvalho de Melo | 5c98d466 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 1788 | |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 1789 | if (perf_evlist__create_maps(evsel_list, &target) < 0) { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1790 | if (target__has_task(&target)) { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 1791 | pr_err("Problems finding threads of monitor\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1792 | parse_options_usage(stat_usage, stat_options, "p", 1); |
| 1793 | parse_options_usage(NULL, stat_options, "t", 1); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1794 | } else if (target__has_cpu(&target)) { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 1795 | perror("failed to parse CPUs map"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1796 | parse_options_usage(stat_usage, stat_options, "C", 1); |
| 1797 | parse_options_usage(NULL, stat_options, "a", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 1798 | } |
| 1799 | goto out; |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 1800 | } |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1801 | |
| 1802 | /* |
| 1803 | * Initialize thread_map with comm names, |
| 1804 | * so we could print it out on output. |
| 1805 | */ |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1806 | if (stat_config.aggr_mode == AGGR_THREAD) |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1807 | thread_map__read_comms(evsel_list->threads); |
| 1808 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1809 | if (interval && interval < 100) { |
Kan Liang | 19afd10 | 2015-10-02 05:04:34 -0400 | [diff] [blame] | 1810 | if (interval < 10) { |
| 1811 | pr_err("print interval must be >= 10ms\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1812 | parse_options_usage(stat_usage, stat_options, "I", 1); |
Kan Liang | 19afd10 | 2015-10-02 05:04:34 -0400 | [diff] [blame] | 1813 | goto out; |
| 1814 | } else |
| 1815 | pr_warning("print interval < 100ms. " |
| 1816 | "The overhead percentage could be high in some cases. " |
| 1817 | "Please proceed with caution.\n"); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1818 | } |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 1819 | |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 1820 | if (perf_evlist__alloc_stats(evsel_list, interval)) |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 1821 | goto out; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 1822 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1823 | if (perf_stat_init_aggr_mode()) |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 1824 | goto out; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1825 | |
Ingo Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 1826 | /* |
| 1827 | * We dont want to block the signals - that would cause |
| 1828 | * child tasks to inherit that and Ctrl-C would not work. |
| 1829 | * What we want is for Ctrl-C to work in the exec()-ed |
| 1830 | * task, but being ignored by perf stat itself: |
| 1831 | */ |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1832 | atexit(sig_atexit); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1833 | if (!forever) |
| 1834 | signal(SIGINT, skip_signal); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1835 | signal(SIGCHLD, skip_signal); |
Ingo Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 1836 | signal(SIGALRM, skip_signal); |
| 1837 | signal(SIGABRT, skip_signal); |
| 1838 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1839 | status = 0; |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1840 | for (run_idx = 0; forever || run_idx < run_count; run_idx++) { |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1841 | if (run_count != 1 && verbose) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1842 | fprintf(output, "[ perf stat: executing run #%d ... ]\n", |
| 1843 | run_idx + 1); |
Ingo Molnar | f9cef0a | 2011-04-28 18:17:11 +0200 | [diff] [blame] | 1844 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1845 | status = run_perf_stat(argc, argv); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1846 | if (forever && status != -1) { |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1847 | print_counters(NULL, argc, argv); |
Jiri Olsa | 254ecbc | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 1848 | perf_stat__reset_stats(); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1849 | } |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 1852 | if (!forever && status != -1 && !interval) |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1853 | print_counters(NULL, argc, argv); |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 1854 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1855 | if (STAT_RECORD) { |
| 1856 | /* |
| 1857 | * We synthesize the kernel mmap record just so that older tools |
| 1858 | * don't emit warnings about not being able to resolve symbols |
| 1859 | * due to /proc/sys/kernel/kptr_restrict settings and instear provide |
| 1860 | * a saner message about no samples being in the perf.data file. |
| 1861 | * |
| 1862 | * This also serves to suppress a warning about f_header.data.size == 0 |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 1863 | * in header.c at the moment 'perf stat record' gets introduced, which |
| 1864 | * is not really needed once we start adding the stat specific PERF_RECORD_ |
| 1865 | * records, but the need to suppress the kptr_restrict messages in older |
| 1866 | * tools remain -acme |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1867 | */ |
| 1868 | int fd = perf_data_file__fd(&perf_stat.file); |
| 1869 | int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat, |
| 1870 | process_synthesized_event, |
| 1871 | &perf_stat.session->machines.host); |
| 1872 | if (err) { |
| 1873 | pr_warning("Couldn't synthesize the kernel mmap record, harmless, " |
| 1874 | "older tools may produce warnings about this file\n."); |
| 1875 | } |
| 1876 | |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 1877 | if (!interval) { |
| 1878 | if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL)) |
| 1879 | pr_err("failed to write stat round event\n"); |
| 1880 | } |
| 1881 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 1882 | if (!perf_stat.file.is_pipe) { |
| 1883 | perf_stat.session->header.data_size += perf_stat.bytes_written; |
| 1884 | perf_session__write_header(perf_stat.session, evsel_list, fd, true); |
| 1885 | } |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1886 | |
| 1887 | perf_session__delete(perf_stat.session); |
| 1888 | } |
| 1889 | |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 1890 | perf_stat__exit_aggr_mode(); |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 1891 | perf_evlist__free_stats(evsel_list); |
Arnaldo Carvalho de Melo | 0015e2e | 2011-02-01 16:18:10 -0200 | [diff] [blame] | 1892 | out: |
| 1893 | perf_evlist__delete(evsel_list); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1894 | return status; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1895 | } |