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