blob: fd76bb0b18d1bded9ab45c25177ff23956b1d003 [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * 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 Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 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 Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
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 Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020055#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080056#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080057#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030059#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020060#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020061#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030062#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010063#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010064#include "util/tool.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030065#include "util/group.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010066#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020067
Andi Kleen44b1e602016-05-30 12:49:42 -030068#include <api/fs/fs.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020069#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020071#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070072#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020073
Stephane Eraniand7470b62010-12-01 18:49:05 +020074#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060075#define CNTR_NOT_SUPPORTED "<not supported>"
76#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020077
Jiri Olsad4f63a42015-06-26 11:29:26 +020078static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010079
Andi Kleen4cabc3d2013-08-21 16:47:26 -070080/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020081static const char *transaction_attrs = {
82 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070083 "{"
84 "instructions,"
85 "cycles,"
86 "cpu/cycles-t/,"
87 "cpu/tx-start/,"
88 "cpu/el-start/,"
89 "cpu/cycles-ct/"
90 "}"
91};
92
93/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020094static const char * transaction_limited_attrs = {
95 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070096 "{"
97 "instructions,"
98 "cycles,"
99 "cpu/cycles-t/,"
100 "cpu/tx-start/"
101 "}"
102};
103
Andi Kleen44b1e602016-05-30 12:49:42 -0300104static const char * topdown_attrs[] = {
105 "topdown-total-slots",
106 "topdown-slots-retired",
107 "topdown-recovery-bubbles",
108 "topdown-fetch-bubbles",
109 "topdown-slots-issued",
110 NULL,
111};
112
Robert Richter666e6d42012-04-05 18:26:27 +0200113static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200114
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300115static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900116 .uid = UINT_MAX,
117};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530118
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100119typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
120
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530121static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200122static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200123static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000124static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200125static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700126static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300127static bool topdown_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200128static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200129static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200130static const char *csv_sep = NULL;
131static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800132static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200133static const char *pre_cmd = NULL;
134static const char *post_cmd = NULL;
135static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700136static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100137static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500138static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800139static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300140static bool force_metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100141static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100142static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100143static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100144static bool append_file;
145static const char *output_name;
146static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200147
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100148struct perf_stat {
149 bool record;
150 struct perf_data_file file;
151 struct perf_session *session;
152 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100153 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100154 bool maps_allocated;
155 struct cpu_map *cpus;
156 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100157 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100158};
159
160static struct perf_stat perf_stat;
161#define STAT_RECORD perf_stat.record
162
Liming Wang60666c62009-12-31 16:05:50 +0800163static volatile int done = 0;
164
Jiri Olsa421a50f2015-07-21 14:31:22 +0200165static struct perf_stat_config stat_config = {
166 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200167 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200168};
169
Stephane Eranian13370a92013-01-29 12:47:44 +0100170static inline void diff_timespec(struct timespec *r, struct timespec *a,
171 struct timespec *b)
172{
173 r->tv_sec = a->tv_sec - b->tv_sec;
174 if (a->tv_nsec < b->tv_nsec) {
175 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
176 r->tv_sec--;
177 } else {
178 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
179 }
180}
181
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200182static void perf_stat__reset_stats(void)
183{
184 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200185 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200186}
187
Jiri Olsacac21422012-11-12 18:34:00 +0100188static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200189{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200190 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200191
Jiri Olsa711a5722015-07-21 14:31:23 +0200192 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200193 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
194 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200195
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200196 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300197
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100198 /*
199 * Some events get initialized with sample_(period/type) set,
200 * like tracepoints. Clear it up for counting.
201 */
202 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100203
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100204 /*
205 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
206 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100207 *
208 * However for pipe sessions we need to keep it zero,
209 * because script's perf_evsel__check_attr is triggered
210 * by attr->sample_type != 0, and we can't run it on
211 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100212 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100213 if (!(STAT_RECORD && perf_stat.file.is_pipe))
214 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100215
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100216 /*
217 * Disabling all counters initially, they will be enabled
218 * either manually by us or by kernel via enable_on_exec
219 * set later.
220 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100221 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100222 attr->disabled = 1;
223
Jiri Olsac8280cec2015-12-03 10:06:45 +0100224 /*
225 * In case of initial_delay we enable tracee
226 * events manually.
227 */
228 if (target__none(&target) && !initial_delay)
229 attr->enable_on_exec = 1;
230 }
231
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300232 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300233 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200234
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300235 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200236}
237
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200238/*
239 * Does the counter have nsecs as a unit?
240 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200241static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200242{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200243 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
244 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200245 return 1;
246
247 return 0;
248}
249
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100250static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100251 union perf_event *event,
252 struct perf_sample *sample __maybe_unused,
253 struct machine *machine __maybe_unused)
254{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100255 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
256 pr_err("failed to write perf data, error: %m\n");
257 return -1;
258 }
259
260 perf_stat.bytes_written += event->header.size;
261 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100262}
263
Jiri Olsa1975d362015-11-05 15:40:56 +0100264static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100265{
Jiri Olsa1975d362015-11-05 15:40:56 +0100266 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100267 process_synthesized_event,
268 NULL);
269}
270
271#define WRITE_STAT_ROUND_EVENT(time, interval) \
272 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
273
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100274#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
275
276static int
277perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
278 struct perf_counts_values *count)
279{
280 struct perf_sample_id *sid = SID(counter, cpu, thread);
281
282 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
283 process_synthesized_event, NULL);
284}
285
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200286/*
287 * Read out the results of a single counter:
288 * do not aggregate counts across CPUs in system-wide mode
289 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200290static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200291{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100292 int nthreads = thread_map__nr(evsel_list->threads);
293 int ncpus = perf_evsel__nr_cpus(counter);
294 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200295
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000296 if (!counter->supported)
297 return -ENOENT;
298
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100299 if (counter->system_wide)
300 nthreads = 1;
301
302 for (thread = 0; thread < nthreads; thread++) {
303 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200304 struct perf_counts_values *count;
305
306 count = perf_counts(counter->counts, cpu, thread);
307 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100308 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100309
310 if (STAT_RECORD) {
311 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
312 pr_err("failed to write stat event\n");
313 return -1;
314 }
315 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700316
317 if (verbose > 1) {
318 fprintf(stat_config.output,
319 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
320 perf_evsel__name(counter),
321 cpu,
322 count->val, count->ena, count->run);
323 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100324 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200325 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200326
327 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200328}
329
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200330static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200331{
332 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200333
334 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200335 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700336 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200337
Jiri Olsaf80010e2015-07-21 14:31:27 +0200338 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200339 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200340
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200341 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200342 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
343 thread_map__nr(evsel_list->threads));
344 }
345 }
346}
347
Jiri Olsaba411a92015-06-26 11:29:24 +0200348static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100349{
Stephane Eranian13370a92013-01-29 12:47:44 +0100350 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100351
Jiri Olsa106a94a2015-06-26 11:29:19 +0200352 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100353
Stephane Eranian13370a92013-01-29 12:47:44 +0100354 clock_gettime(CLOCK_MONOTONIC, &ts);
355 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100356
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100357 if (STAT_RECORD) {
358 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
359 pr_err("failed to write stat round event\n");
360 }
361
Jiri Olsad4f63a42015-06-26 11:29:26 +0200362 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100363}
364
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100365static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700366{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100367 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700368 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100369
370 /*
371 * We need to enable counters only if:
372 * - we don't have tracee (attaching to task or cpu)
373 * - we have initial delay configured
374 */
375 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100376 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700377}
378
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300379static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300380
381/*
382 * perf_evlist__prepare_workload will send a SIGUSR1
383 * if the fork fails, since we asked by setting its
384 * want_signal to true.
385 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300386static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
387 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300388{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300389 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300390}
391
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100392static bool has_unit(struct perf_evsel *counter)
393{
394 return counter->unit && *counter->unit;
395}
396
397static bool has_scale(struct perf_evsel *counter)
398{
399 return counter->scale != 1;
400}
401
Jiri Olsa664c98d2015-11-05 15:40:50 +0100402static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100403{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100404 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100405 int err;
406
Jiri Olsa664c98d2015-11-05 15:40:50 +0100407 if (is_pipe) {
408 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
409 process_synthesized_event);
410 if (err < 0) {
411 pr_err("Couldn't synthesize attrs.\n");
412 return err;
413 }
414 }
415
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100416 /*
417 * Synthesize other events stuff not carried within
418 * attr event - unit, scale, name
419 */
420 evlist__for_each(evsel_list, counter) {
421 if (!counter->supported)
422 continue;
423
424 /*
425 * Synthesize unit and scale only if it's defined.
426 */
427 if (has_unit(counter)) {
428 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
429 if (err < 0) {
430 pr_err("Couldn't synthesize evsel unit.\n");
431 return err;
432 }
433 }
434
435 if (has_scale(counter)) {
436 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
437 if (err < 0) {
438 pr_err("Couldn't synthesize evsel scale.\n");
439 return err;
440 }
441 }
442
443 if (counter->own_cpus) {
444 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
445 if (err < 0) {
446 pr_err("Couldn't synthesize evsel scale.\n");
447 return err;
448 }
449 }
450
451 /*
452 * Name is needed only for pipe output,
453 * perf.data carries event names.
454 */
455 if (is_pipe) {
456 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
457 if (err < 0) {
458 pr_err("Couldn't synthesize evsel name.\n");
459 return err;
460 }
461 }
462 }
463
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100464 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
465 process_synthesized_event,
466 NULL);
467 if (err < 0) {
468 pr_err("Couldn't synthesize thread map.\n");
469 return err;
470 }
471
472 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
473 process_synthesized_event, NULL);
474 if (err < 0) {
475 pr_err("Couldn't synthesize thread map.\n");
476 return err;
477 }
478
479 err = perf_event__synthesize_stat_config(NULL, &stat_config,
480 process_synthesized_event, NULL);
481 if (err < 0) {
482 pr_err("Couldn't synthesize config.\n");
483 return err;
484 }
485
486 return 0;
487}
488
Jiri Olsa2af46462015-11-05 15:40:49 +0100489#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
490
491static int __store_counter_ids(struct perf_evsel *counter,
492 struct cpu_map *cpus,
493 struct thread_map *threads)
494{
495 int cpu, thread;
496
497 for (cpu = 0; cpu < cpus->nr; cpu++) {
498 for (thread = 0; thread < threads->nr; thread++) {
499 int fd = FD(counter, cpu, thread);
500
501 if (perf_evlist__id_add_fd(evsel_list, counter,
502 cpu, thread, fd) < 0)
503 return -1;
504 }
505 }
506
507 return 0;
508}
509
510static int store_counter_ids(struct perf_evsel *counter)
511{
512 struct cpu_map *cpus = counter->cpus;
513 struct thread_map *threads = counter->threads;
514
515 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
516 return -ENOMEM;
517
518 return __store_counter_ids(counter, cpus, threads);
519}
520
Namhyung Kimacf28922013-03-11 16:43:18 +0900521static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200522{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200523 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300524 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200525 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100526 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100527 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100528 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200529 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300530 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100531 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200532
Stephane Eranian13370a92013-01-29 12:47:44 +0100533 if (interval) {
534 ts.tv_sec = interval / 1000;
535 ts.tv_nsec = (interval % 1000) * 1000000;
536 } else {
537 ts.tv_sec = 1;
538 ts.tv_nsec = 0;
539 }
540
Liming Wang60666c62009-12-31 16:05:50 +0800541 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100542 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300543 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900544 perror("failed to prepare workload");
545 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800546 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900547 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000548 }
549
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200550 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300551 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200552
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300553 evlist__for_each(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300554try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100555 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600556 /*
557 * PPC returns ENXIO for HW counters until 2.6.37
558 * (behavior changed with commit b0a873e).
559 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100560 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600561 errno == ENOENT || errno == EOPNOTSUPP ||
562 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600563 if (verbose)
564 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300565 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600566 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400567
568 if ((counter->leader != counter) ||
569 !(counter->leader->nr_members > 1))
570 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300571 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
572 if (verbose)
573 ui__warning("%s\n", msg);
574 goto try_again;
575 }
Ingo Molnarede70292011-04-28 08:48:42 +0200576
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300577 perf_evsel__open_strerror(counter, &target,
578 errno, msg, sizeof(msg));
579 ui__error("%s\n", msg);
580
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200581 if (child_pid != -1)
582 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600583
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200584 return -1;
585 }
David Ahern2cee77c2011-05-30 08:55:59 -0600586 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100587
588 l = strlen(counter->unit);
589 if (l > unit_width)
590 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100591
592 if (STAT_RECORD && store_counter_ids(counter))
593 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300594 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200595
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300596 if (perf_evlist__apply_filters(evsel_list, &counter)) {
597 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
598 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000599 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100600 return -1;
601 }
602
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100603 if (STAT_RECORD) {
604 int err, fd = perf_data_file__fd(&perf_stat.file);
605
Jiri Olsa664c98d2015-11-05 15:40:50 +0100606 if (is_pipe) {
607 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
608 } else {
609 err = perf_session__write_header(perf_stat.session, evsel_list,
610 fd, false);
611 }
612
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100613 if (err < 0)
614 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100615
Jiri Olsa664c98d2015-11-05 15:40:50 +0100616 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100617 if (err < 0)
618 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100619 }
620
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200621 /*
622 * Enable counters and exec the command:
623 */
624 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100625 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200626
Liming Wang60666c62009-12-31 16:05:50 +0800627 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900628 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100629 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900630
Stephane Eranian13370a92013-01-29 12:47:44 +0100631 if (interval) {
632 while (!waitpid(child_pid, &status, WNOHANG)) {
633 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200634 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100635 }
636 }
Liming Wang60666c62009-12-31 16:05:50 +0800637 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300638
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300639 if (workload_exec_errno) {
640 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
641 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300642 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300643 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300644
Andi Kleen33e49ea2011-09-15 14:31:40 -0700645 if (WIFSIGNALED(status))
646 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800647 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100648 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100649 while (!done) {
650 nanosleep(&ts, NULL);
651 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200652 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100653 }
Liming Wang60666c62009-12-31 16:05:50 +0800654 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200655
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200656 t1 = rdclock();
657
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200658 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200659
Jiri Olsa106a94a2015-06-26 11:29:19 +0200660 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200661
Ingo Molnar42202dd2009-06-13 14:57:28 +0200662 return WEXITSTATUS(status);
663}
664
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300665static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200666{
667 int ret;
668
669 if (pre_cmd) {
670 ret = system(pre_cmd);
671 if (ret)
672 return ret;
673 }
674
675 if (sync_run)
676 sync();
677
678 ret = __run_perf_stat(argc, argv);
679 if (ret)
680 return ret;
681
682 if (post_cmd) {
683 ret = system(post_cmd);
684 if (ret)
685 return ret;
686 }
687
688 return ret;
689}
690
Andi Kleend73515c2015-03-11 07:16:27 -0700691static void print_running(u64 run, u64 ena)
692{
693 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200694 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700695 csv_sep,
696 run,
697 csv_sep,
698 ena ? 100.0 * run / ena : 100.0);
699 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200700 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700701 }
702}
703
Ingo Molnarf99844c2011-04-27 05:35:39 +0200704static void print_noise_pct(double total, double avg)
705{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800706 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200707
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700708 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200709 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600710 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200711 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200712}
713
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200714static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200715{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200716 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200717
Peter Zijlstra849abde2009-09-04 18:23:38 +0200718 if (run_count == 1)
719 return;
720
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200721 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200722 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200723}
724
Stephane Eranian12c08a92013-02-14 13:57:29 +0100725static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200726{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200727 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100728 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200729 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100730 cpu_map__id_to_socket(id),
731 csv_output ? 0 : -8,
732 cpu_map__id_to_cpu(id),
733 csv_sep,
734 csv_output ? 0 : 4,
735 nr,
736 csv_sep);
737 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100738 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200739 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100740 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100741 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100742 csv_sep,
743 csv_output ? 0 : 4,
744 nr,
745 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100746 break;
747 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200748 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200749 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100750 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100751 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200752 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200753 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200754 csv_output ? 0 : 16,
755 thread_map__comm(evsel->threads, id),
756 csv_output ? 0 : -8,
757 thread_map__pid(evsel->threads, id),
758 csv_sep);
759 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100760 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200761 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100762 default:
763 break;
764 }
765}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200766
Andi Kleen140aead2016-01-30 09:06:49 -0800767struct outstate {
768 FILE *fh;
769 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800770 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800771 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800772 int id, nr;
773 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800774};
775
776#define METRIC_LEN 35
777
778static void new_line_std(void *ctx)
779{
780 struct outstate *os = ctx;
781
782 os->newline = true;
783}
784
785static void do_new_line_std(struct outstate *os)
786{
787 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800788 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800789 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800790 if (stat_config.aggr_mode == AGGR_NONE)
791 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800792 fprintf(os->fh, " ");
793}
794
795static void print_metric_std(void *ctx, const char *color, const char *fmt,
796 const char *unit, double val)
797{
798 struct outstate *os = ctx;
799 FILE *out = os->fh;
800 int n;
801 bool newline = os->newline;
802
803 os->newline = false;
804
805 if (unit == NULL || fmt == NULL) {
806 fprintf(out, "%-*s", METRIC_LEN, "");
807 return;
808 }
809
810 if (newline)
811 do_new_line_std(os);
812
813 n = fprintf(out, " # ");
814 if (color)
815 n += color_fprintf(out, color, fmt, val);
816 else
817 n += fprintf(out, fmt, val);
818 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
819}
820
Andi Kleen92a61f62016-02-29 14:36:21 -0800821static void new_line_csv(void *ctx)
822{
823 struct outstate *os = ctx;
824 int i;
825
826 fputc('\n', os->fh);
827 if (os->prefix)
828 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800829 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800830 for (i = 0; i < os->nfields; i++)
831 fputs(csv_sep, os->fh);
832}
833
834static void print_metric_csv(void *ctx,
835 const char *color __maybe_unused,
836 const char *fmt, const char *unit, double val)
837{
838 struct outstate *os = ctx;
839 FILE *out = os->fh;
840 char buf[64], *vals, *ends;
841
842 if (unit == NULL || fmt == NULL) {
843 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
844 return;
845 }
846 snprintf(buf, sizeof(buf), fmt, val);
847 vals = buf;
848 while (isspace(*vals))
849 vals++;
850 ends = vals;
851 while (isdigit(*ends) || *ends == '.')
852 ends++;
853 *ends = 0;
854 while (isspace(*unit))
855 unit++;
856 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
857}
858
Andi Kleen54b50912016-03-03 15:57:36 -0800859#define METRIC_ONLY_LEN 20
860
861/* Filter out some columns that don't work well in metrics only mode */
862
863static bool valid_only_metric(const char *unit)
864{
865 if (!unit)
866 return false;
867 if (strstr(unit, "/sec") ||
868 strstr(unit, "hz") ||
869 strstr(unit, "Hz") ||
870 strstr(unit, "CPUs utilized"))
871 return false;
872 return true;
873}
874
875static const char *fixunit(char *buf, struct perf_evsel *evsel,
876 const char *unit)
877{
878 if (!strncmp(unit, "of all", 6)) {
879 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
880 unit);
881 return buf;
882 }
883 return unit;
884}
885
886static void print_metric_only(void *ctx, const char *color, const char *fmt,
887 const char *unit, double val)
888{
889 struct outstate *os = ctx;
890 FILE *out = os->fh;
891 int n;
892 char buf[1024];
893 unsigned mlen = METRIC_ONLY_LEN;
894
895 if (!valid_only_metric(unit))
896 return;
897 unit = fixunit(buf, os->evsel, unit);
898 if (color)
899 n = color_fprintf(out, color, fmt, val);
900 else
901 n = fprintf(out, fmt, val);
902 if (n > METRIC_ONLY_LEN)
903 n = METRIC_ONLY_LEN;
904 if (mlen < strlen(unit))
905 mlen = strlen(unit) + 1;
906 fprintf(out, "%*s", mlen - n, "");
907}
908
909static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
910 const char *fmt,
911 const char *unit, double val)
912{
913 struct outstate *os = ctx;
914 FILE *out = os->fh;
915 char buf[64], *vals, *ends;
916 char tbuf[1024];
917
918 if (!valid_only_metric(unit))
919 return;
920 unit = fixunit(tbuf, os->evsel, unit);
921 snprintf(buf, sizeof buf, fmt, val);
922 vals = buf;
923 while (isspace(*vals))
924 vals++;
925 ends = vals;
926 while (isdigit(*ends) || *ends == '.')
927 ends++;
928 *ends = 0;
929 fprintf(out, "%s%s", vals, csv_sep);
930}
931
932static void new_line_metric(void *ctx __maybe_unused)
933{
934}
935
936static void print_metric_header(void *ctx, const char *color __maybe_unused,
937 const char *fmt __maybe_unused,
938 const char *unit, double val __maybe_unused)
939{
940 struct outstate *os = ctx;
941 char tbuf[1024];
942
943 if (!valid_only_metric(unit))
944 return;
945 unit = fixunit(tbuf, os->evsel, unit);
946 if (csv_output)
947 fprintf(os->fh, "%s%s", unit, csv_sep);
948 else
949 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
950}
951
Andi Kleenda88c7f2014-09-24 13:50:46 -0700952static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100953{
Jiri Olsa58215222015-07-21 14:31:24 +0200954 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100955 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100956 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600957 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100958
Stephane Eranian410136f2013-11-12 17:58:49 +0100959 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
960 fmt_n = csv_output ? "%s" : "%-25s";
961
Andi Kleenda88c7f2014-09-24 13:50:46 -0700962 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100963
David Ahern4bbe5a62013-09-28 14:28:00 -0600964 scnprintf(name, sizeof(name), "%s%s",
965 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100966
967 fprintf(output, fmt_v, msecs, csv_sep);
968
969 if (csv_output)
970 fprintf(output, "%s%s", evsel->unit, csv_sep);
971 else
972 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
973
974 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200975
Stephane Eranian023695d2011-02-14 11:20:01 +0200976 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200977 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200978}
979
Andi Kleen44d49a62016-02-29 14:36:22 -0800980static int first_shadow_cpu(struct perf_evsel *evsel, int id)
981{
982 int i;
983
984 if (!aggr_get_id)
985 return 0;
986
987 if (stat_config.aggr_mode == AGGR_NONE)
988 return id;
989
990 if (stat_config.aggr_mode == AGGR_GLOBAL)
991 return 0;
992
993 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
994 int cpu2 = perf_evsel__cpus(evsel)->map[i];
995
996 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
997 return cpu2;
998 }
999 return 0;
1000}
1001
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001002static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1003{
Jiri Olsa58215222015-07-21 14:31:24 +02001004 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001005 double sc = evsel->scale;
1006 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001007
1008 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001009 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001010 } else {
1011 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001012 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001013 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001014 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001015 }
1016
1017 aggr_printout(evsel, id, nr);
1018
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001019 fprintf(output, fmt, avg, csv_sep);
1020
1021 if (evsel->unit)
1022 fprintf(output, "%-*s%s",
1023 csv_output ? 0 : unit_width,
1024 evsel->unit, csv_sep);
1025
1026 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1027
1028 if (evsel->cgrp)
1029 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001030}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001031
Andi Kleenf9483392016-01-30 09:06:50 -08001032static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001033 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001034{
Andi Kleen140aead2016-01-30 09:06:49 -08001035 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001036 struct outstate os = {
1037 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001038 .prefix = prefix ? prefix : "",
1039 .id = id,
1040 .nr = nr,
1041 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001042 };
Andi Kleen140aead2016-01-30 09:06:49 -08001043 print_metric_t pm = print_metric_std;
1044 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001045
Andi Kleen54b50912016-03-03 15:57:36 -08001046 if (metric_only) {
1047 nl = new_line_metric;
1048 if (csv_output)
1049 pm = print_metric_only_csv;
1050 else
1051 pm = print_metric_only;
1052 } else
1053 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001054
Andi Kleen54b50912016-03-03 15:57:36 -08001055 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001056 static int aggr_fields[] = {
1057 [AGGR_GLOBAL] = 0,
1058 [AGGR_THREAD] = 1,
1059 [AGGR_NONE] = 1,
1060 [AGGR_SOCKET] = 2,
1061 [AGGR_CORE] = 2,
1062 };
1063
1064 pm = print_metric_csv;
1065 nl = new_line_csv;
1066 os.nfields = 3;
1067 os.nfields += aggr_fields[stat_config.aggr_mode];
1068 if (counter->cgrp)
1069 os.nfields++;
1070 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001071 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001072 if (metric_only) {
1073 pm(&os, NULL, "", "", 0);
1074 return;
1075 }
Andi Kleencb110f42016-01-30 09:06:51 -08001076 aggr_printout(counter, id, nr);
1077
1078 fprintf(stat_config.output, "%*s%s",
1079 csv_output ? 0 : 18,
1080 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1081 csv_sep);
1082
1083 fprintf(stat_config.output, "%-*s%s",
1084 csv_output ? 0 : unit_width,
1085 counter->unit, csv_sep);
1086
1087 fprintf(stat_config.output, "%*s",
1088 csv_output ? 0 : -25,
1089 perf_evsel__name(counter));
1090
1091 if (counter->cgrp)
1092 fprintf(stat_config.output, "%s%s",
1093 csv_sep, counter->cgrp->name);
1094
Andi Kleen92a61f62016-02-29 14:36:21 -08001095 if (!csv_output)
1096 pm(&os, NULL, NULL, "", 0);
1097 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001098 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001099 if (csv_output)
1100 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001101 return;
1102 }
1103
Andi Kleen54b50912016-03-03 15:57:36 -08001104 if (metric_only)
1105 /* nothing */;
1106 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001107 nsec_printout(id, nr, counter, uval);
1108 else
1109 abs_printout(id, nr, counter, uval);
1110
Andi Kleen140aead2016-01-30 09:06:49 -08001111 out.print_metric = pm;
1112 out.new_line = nl;
1113 out.ctx = &os;
1114
Andi Kleen54b50912016-03-03 15:57:36 -08001115 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001116 print_noise(counter, noise);
1117 print_running(run, ena);
1118 }
1119
1120 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001121 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001122 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001123 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001124 print_noise(counter, noise);
1125 print_running(run, ena);
1126 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001127}
1128
Andi Kleen44d49a62016-02-29 14:36:22 -08001129static void aggr_update_shadow(void)
1130{
1131 int cpu, s2, id, s;
1132 u64 val;
1133 struct perf_evsel *counter;
1134
1135 for (s = 0; s < aggr_map->nr; s++) {
1136 id = aggr_map->map[s];
1137 evlist__for_each(evsel_list, counter) {
1138 val = 0;
1139 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1140 s2 = aggr_get_id(evsel_list->cpus, cpu);
1141 if (s2 != id)
1142 continue;
1143 val += perf_counts(counter->counts, cpu, 0)->val;
1144 }
1145 val = val * counter->scale;
1146 perf_stat__update_shadow_stats(counter, &val,
1147 first_shadow_cpu(counter, id));
1148 }
1149 }
1150}
1151
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001152static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001153{
Jiri Olsa58215222015-07-21 14:31:24 +02001154 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001155 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -04001156 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001157 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001158 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001159 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001160
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001161 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001162 return;
1163
Andi Kleen44d49a62016-02-29 14:36:22 -08001164 aggr_update_shadow();
1165
Andi Kleen54b50912016-03-03 15:57:36 -08001166 /*
1167 * With metric_only everything is on a single line.
1168 * Without each counter has its own line.
1169 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001170 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleen54b50912016-03-03 15:57:36 -08001171 if (prefix && metric_only)
1172 fprintf(output, "%s", prefix);
1173
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001174 id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001175 first = true;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001176 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001177 val = ena = run = 0;
1178 nr = 0;
1179 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -04001180 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001181 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001182 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001183 val += perf_counts(counter->counts, cpu, 0)->val;
1184 ena += perf_counts(counter->counts, cpu, 0)->ena;
1185 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001186 nr++;
1187 }
Andi Kleen54b50912016-03-03 15:57:36 -08001188 if (first && metric_only) {
1189 first = false;
1190 aggr_printout(counter, id, nr);
1191 }
1192 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001193 fprintf(output, "%s", prefix);
1194
Stephane Eranian410136f2013-11-12 17:58:49 +01001195 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001196 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001197 if (!metric_only)
1198 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001199 }
Andi Kleen54b50912016-03-03 15:57:36 -08001200 if (metric_only)
1201 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001202 }
1203}
1204
Jiri Olsa32b8af82015-06-26 11:29:27 +02001205static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1206{
Jiri Olsa58215222015-07-21 14:31:24 +02001207 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001208 int nthreads = thread_map__nr(counter->threads);
1209 int ncpus = cpu_map__nr(counter->cpus);
1210 int cpu, thread;
1211 double uval;
1212
1213 for (thread = 0; thread < nthreads; thread++) {
1214 u64 ena = 0, run = 0, val = 0;
1215
1216 for (cpu = 0; cpu < ncpus; cpu++) {
1217 val += perf_counts(counter->counts, cpu, thread)->val;
1218 ena += perf_counts(counter->counts, cpu, thread)->ena;
1219 run += perf_counts(counter->counts, cpu, thread)->run;
1220 }
1221
1222 if (prefix)
1223 fprintf(output, "%s", prefix);
1224
1225 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001226 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001227 fputc('\n', output);
1228 }
1229}
1230
Ingo Molnar42202dd2009-06-13 14:57:28 +02001231/*
1232 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001233 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001234 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001235static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001236{
Jiri Olsa58215222015-07-21 14:31:24 +02001237 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +02001238 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001239 double avg = avg_stats(&ps->res_stats[0]);
Stephane Eranian410136f2013-11-12 17:58:49 +01001240 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -07001241 double avg_enabled, avg_running;
1242
1243 avg_enabled = avg_stats(&ps->res_stats[1]);
1244 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001245
Andi Kleen54b50912016-03-03 15:57:36 -08001246 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001247 fprintf(output, "%s", prefix);
1248
Stephane Eranian410136f2013-11-12 17:58:49 +01001249 uval = avg * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001250 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001251 if (!metric_only)
1252 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001253}
1254
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001255/*
1256 * Print out the results of a single counter:
1257 * does not use aggregated count in system-wide
1258 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001259static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001260{
Jiri Olsa58215222015-07-21 14:31:24 +02001261 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001262 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001263 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001264 int cpu;
1265
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001266 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001267 val = perf_counts(counter->counts, cpu, 0)->val;
1268 ena = perf_counts(counter->counts, cpu, 0)->ena;
1269 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001270
1271 if (prefix)
1272 fprintf(output, "%s", prefix);
1273
Stephane Eranian410136f2013-11-12 17:58:49 +01001274 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001275 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001276
Stephane Eranian4aa90152011-08-15 22:22:33 +02001277 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001278 }
1279}
1280
Andi Kleen206cab62016-03-03 15:57:37 -08001281static void print_no_aggr_metric(char *prefix)
1282{
1283 int cpu;
1284 int nrcpus = 0;
1285 struct perf_evsel *counter;
1286 u64 ena, run, val;
1287 double uval;
1288
1289 nrcpus = evsel_list->cpus->nr;
1290 for (cpu = 0; cpu < nrcpus; cpu++) {
1291 bool first = true;
1292
1293 if (prefix)
1294 fputs(prefix, stat_config.output);
1295 evlist__for_each(evsel_list, counter) {
1296 if (first) {
1297 aggr_printout(counter, cpu, 0);
1298 first = false;
1299 }
1300 val = perf_counts(counter->counts, cpu, 0)->val;
1301 ena = perf_counts(counter->counts, cpu, 0)->ena;
1302 run = perf_counts(counter->counts, cpu, 0)->run;
1303
1304 uval = val * counter->scale;
1305 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1306 }
1307 fputc('\n', stat_config.output);
1308 }
1309}
1310
Andi Kleen54b50912016-03-03 15:57:36 -08001311static int aggr_header_lens[] = {
1312 [AGGR_CORE] = 18,
1313 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001314 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001315 [AGGR_THREAD] = 24,
1316 [AGGR_GLOBAL] = 0,
1317};
1318
1319static void print_metric_headers(char *prefix)
1320{
1321 struct perf_stat_output_ctx out;
1322 struct perf_evsel *counter;
1323 struct outstate os = {
1324 .fh = stat_config.output
1325 };
1326
1327 if (prefix)
1328 fprintf(stat_config.output, "%s", prefix);
1329
1330 if (!csv_output)
1331 fprintf(stat_config.output, "%*s",
1332 aggr_header_lens[stat_config.aggr_mode], "");
1333
1334 /* Print metrics headers only */
1335 evlist__for_each(evsel_list, counter) {
1336 os.evsel = counter;
1337 out.ctx = &os;
1338 out.print_metric = print_metric_header;
1339 out.new_line = new_line_metric;
1340 os.evsel = counter;
1341 perf_stat__print_shadow_stats(counter, 0,
1342 0,
1343 &out);
1344 }
1345 fputc('\n', stat_config.output);
1346}
1347
Jiri Olsad4f63a42015-06-26 11:29:26 +02001348static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001349{
Jiri Olsa58215222015-07-21 14:31:24 +02001350 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001351 static int num_print_interval;
1352
1353 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1354
Andi Kleen54b50912016-03-03 15:57:36 -08001355 if (num_print_interval == 0 && !csv_output && !metric_only) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001356 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001357 case AGGR_SOCKET:
1358 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
1359 break;
1360 case AGGR_CORE:
1361 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
1362 break;
1363 case AGGR_NONE:
1364 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
1365 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001366 case AGGR_THREAD:
1367 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
1368 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001369 case AGGR_GLOBAL:
1370 default:
1371 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001372 case AGGR_UNSET:
1373 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001374 }
1375 }
1376
1377 if (++num_print_interval == 25)
1378 num_print_interval = 0;
1379}
1380
1381static void print_header(int argc, const char **argv)
1382{
Jiri Olsa58215222015-07-21 14:31:24 +02001383 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001384 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001385
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001386 fflush(stdout);
1387
Stephane Eraniand7470b62010-12-01 18:49:05 +02001388 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001389 fprintf(output, "\n");
1390 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001391 if (target.system_wide)
1392 fprintf(output, "\'system wide");
1393 else if (target.cpu_list)
1394 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001395 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001396 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1397 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001398 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001399 } else if (target.pid)
1400 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001401 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001402 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001403
Stephane Eranian4aa90152011-08-15 22:22:33 +02001404 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001405 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001406 fprintf(output, " (%d runs)", run_count);
1407 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001408 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001409}
1410
1411static void print_footer(void)
1412{
Jiri Olsa58215222015-07-21 14:31:24 +02001413 FILE *output = stat_config.output;
1414
Jiri Olsad4f63a42015-06-26 11:29:26 +02001415 if (!null_run)
1416 fprintf(output, "\n");
1417 fprintf(output, " %17.9f seconds time elapsed",
1418 avg_stats(&walltime_nsecs_stats)/1e9);
1419 if (run_count > 1) {
1420 fprintf(output, " ");
1421 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1422 avg_stats(&walltime_nsecs_stats));
1423 }
1424 fprintf(output, "\n\n");
1425}
1426
1427static void print_counters(struct timespec *ts, int argc, const char **argv)
1428{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001429 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001430 struct perf_evsel *counter;
1431 char buf[64], *prefix = NULL;
1432
Jiri Olsa664c98d2015-11-05 15:40:50 +01001433 /* Do not print anything if we record to the pipe. */
1434 if (STAT_RECORD && perf_stat.file.is_pipe)
1435 return;
1436
Jiri Olsad4f63a42015-06-26 11:29:26 +02001437 if (interval)
1438 print_interval(prefix = buf, ts);
1439 else
1440 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001441
Andi Kleen54b50912016-03-03 15:57:36 -08001442 if (metric_only) {
1443 static int num_print_iv;
1444
1445 if (num_print_iv == 0)
1446 print_metric_headers(prefix);
1447 if (num_print_iv++ == 25)
1448 num_print_iv = 0;
1449 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1450 fprintf(stat_config.output, "%s", prefix);
1451 }
1452
Jiri Olsa421a50f2015-07-21 14:31:22 +02001453 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001454 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001455 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001456 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001457 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001458 case AGGR_THREAD:
1459 evlist__for_each(evsel_list, counter)
1460 print_aggr_thread(counter, prefix);
1461 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001462 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001463 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001464 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001465 if (metric_only)
1466 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001467 break;
1468 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001469 if (metric_only)
1470 print_no_aggr_metric(prefix);
1471 else {
1472 evlist__for_each(evsel_list, counter)
1473 print_counter(counter, prefix);
1474 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001475 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001476 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001477 default:
1478 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001479 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001480
Jiri Olsad4f63a42015-06-26 11:29:26 +02001481 if (!interval && !csv_output)
1482 print_footer();
1483
Jiri Olsa58215222015-07-21 14:31:24 +02001484 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001485}
1486
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001487static volatile int signr = -1;
1488
Ingo Molnar52425192009-05-26 09:17:18 +02001489static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001490{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001491 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001492 done = 1;
1493
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001494 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001495 /*
1496 * render child_pid harmless
1497 * won't send SIGTERM to a random
1498 * process in case of race condition
1499 * and fast PID recycling
1500 */
1501 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001502}
1503
1504static void sig_atexit(void)
1505{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001506 sigset_t set, oset;
1507
1508 /*
1509 * avoid race condition with SIGCHLD handler
1510 * in skip_signal() which is modifying child_pid
1511 * goal is to avoid send SIGTERM to a random
1512 * process
1513 */
1514 sigemptyset(&set);
1515 sigaddset(&set, SIGCHLD);
1516 sigprocmask(SIG_BLOCK, &set, &oset);
1517
Chris Wilson933da832009-10-04 01:35:01 +01001518 if (child_pid != -1)
1519 kill(child_pid, SIGTERM);
1520
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001521 sigprocmask(SIG_SETMASK, &oset, NULL);
1522
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001523 if (signr == -1)
1524 return;
1525
1526 signal(signr, SIG_DFL);
1527 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001528}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001529
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001530static int stat__set_big_num(const struct option *opt __maybe_unused,
1531 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001532{
1533 big_num_opt = unset ? 0 : 1;
1534 return 0;
1535}
1536
Andi Kleen44b1e602016-05-30 12:49:42 -03001537static int enable_metric_only(const struct option *opt __maybe_unused,
1538 const char *s __maybe_unused, int unset)
1539{
1540 force_metric_only = true;
1541 metric_only = !unset;
1542 return 0;
1543}
1544
Jiri Olsae0547312015-11-05 15:40:45 +01001545static const struct option stat_options[] = {
1546 OPT_BOOLEAN('T', "transaction", &transaction_run,
1547 "hardware transaction statistics"),
1548 OPT_CALLBACK('e', "event", &evsel_list, "event",
1549 "event selector. use 'perf list' to list available events",
1550 parse_events_option),
1551 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1552 "event filter", parse_filter),
1553 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1554 "child tasks do not inherit counters"),
1555 OPT_STRING('p', "pid", &target.pid, "pid",
1556 "stat events on existing process id"),
1557 OPT_STRING('t', "tid", &target.tid, "tid",
1558 "stat events on existing thread id"),
1559 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1560 "system-wide collection from all CPUs"),
1561 OPT_BOOLEAN('g', "group", &group,
1562 "put the counters into a counter group"),
1563 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1564 OPT_INCR('v', "verbose", &verbose,
1565 "be more verbose (show counter open errors, etc)"),
1566 OPT_INTEGER('r', "repeat", &run_count,
1567 "repeat command and print average + stddev (max: 100, forever: 0)"),
1568 OPT_BOOLEAN('n', "null", &null_run,
1569 "null run - dont start any counters"),
1570 OPT_INCR('d', "detailed", &detailed_run,
1571 "detailed run - start a lot of events"),
1572 OPT_BOOLEAN('S', "sync", &sync_run,
1573 "call sync() before starting a run"),
1574 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1575 "print large numbers with thousands\' separators",
1576 stat__set_big_num),
1577 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1578 "list of cpus to monitor in system-wide"),
1579 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1580 "disable CPU count aggregation", AGGR_NONE),
1581 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1582 "print counts with custom separator"),
1583 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1584 "monitor event in cgroup name only", parse_cgroups),
1585 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1586 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1587 OPT_INTEGER(0, "log-fd", &output_fd,
1588 "log output to fd, instead of stderr"),
1589 OPT_STRING(0, "pre", &pre_cmd, "command",
1590 "command to run prior to the measured command"),
1591 OPT_STRING(0, "post", &post_cmd, "command",
1592 "command to run after to the measured command"),
1593 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1594 "print counts at regular interval in ms (>= 10)"),
1595 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1596 "aggregate counts per processor socket", AGGR_SOCKET),
1597 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1598 "aggregate counts per physical processor core", AGGR_CORE),
1599 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1600 "aggregate counts per thread", AGGR_THREAD),
1601 OPT_UINTEGER('D', "delay", &initial_delay,
1602 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001603 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1604 "Only print computed metrics. No raw values", enable_metric_only),
1605 OPT_BOOLEAN(0, "topdown", &topdown_run,
1606 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001607 OPT_END()
1608};
1609
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001610static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1611{
1612 return cpu_map__get_socket(map, cpu, NULL);
1613}
1614
1615static int perf_stat__get_core(struct cpu_map *map, int cpu)
1616{
1617 return cpu_map__get_core(map, cpu, NULL);
1618}
1619
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001620static int cpu_map__get_max(struct cpu_map *map)
1621{
1622 int i, max = -1;
1623
1624 for (i = 0; i < map->nr; i++) {
1625 if (map->map[i] > max)
1626 max = map->map[i];
1627 }
1628
1629 return max;
1630}
1631
1632static struct cpu_map *cpus_aggr_map;
1633
1634static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1635{
1636 int cpu;
1637
1638 if (idx >= map->nr)
1639 return -1;
1640
1641 cpu = map->map[idx];
1642
1643 if (cpus_aggr_map->map[cpu] == -1)
1644 cpus_aggr_map->map[cpu] = get_id(map, idx);
1645
1646 return cpus_aggr_map->map[cpu];
1647}
1648
1649static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1650{
1651 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1652}
1653
1654static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1655{
1656 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1657}
1658
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001659static int perf_stat_init_aggr_mode(void)
1660{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001661 int nr;
1662
Jiri Olsa421a50f2015-07-21 14:31:22 +02001663 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001664 case AGGR_SOCKET:
1665 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1666 perror("cannot build socket map");
1667 return -1;
1668 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001669 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001670 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001671 case AGGR_CORE:
1672 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1673 perror("cannot build core map");
1674 return -1;
1675 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001676 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001677 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001678 case AGGR_NONE:
1679 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001680 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001681 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001682 default:
1683 break;
1684 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001685
1686 /*
1687 * The evsel_list->cpus is the base we operate on,
1688 * taking the highest cpu number to be the size of
1689 * the aggregation translate cpumap.
1690 */
1691 nr = cpu_map__get_max(evsel_list->cpus);
1692 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1693 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001694}
1695
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001696static void perf_stat__exit_aggr_mode(void)
1697{
1698 cpu_map__put(aggr_map);
1699 cpu_map__put(cpus_aggr_map);
1700 aggr_map = NULL;
1701 cpus_aggr_map = NULL;
1702}
1703
Jiri Olsa68d702f2015-11-05 15:40:58 +01001704static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1705{
1706 int cpu;
1707
1708 if (idx > map->nr)
1709 return -1;
1710
1711 cpu = map->map[idx];
1712
1713 if (cpu >= env->nr_cpus_online)
1714 return -1;
1715
1716 return cpu;
1717}
1718
1719static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1720{
1721 struct perf_env *env = data;
1722 int cpu = perf_env__get_cpu(env, map, idx);
1723
1724 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1725}
1726
1727static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1728{
1729 struct perf_env *env = data;
1730 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1731
1732 if (cpu != -1) {
1733 int socket_id = env->cpu[cpu].socket_id;
1734
1735 /*
1736 * Encode socket in upper 16 bits
1737 * core_id is relative to socket, and
1738 * we need a global id. So we combine
1739 * socket + core id.
1740 */
1741 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1742 }
1743
1744 return core;
1745}
1746
1747static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1748 struct cpu_map **sockp)
1749{
1750 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1751}
1752
1753static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1754 struct cpu_map **corep)
1755{
1756 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1757}
1758
1759static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1760{
1761 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1762}
1763
1764static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1765{
1766 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1767}
1768
1769static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1770{
1771 struct perf_env *env = &st->session->header.env;
1772
1773 switch (stat_config.aggr_mode) {
1774 case AGGR_SOCKET:
1775 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1776 perror("cannot build socket map");
1777 return -1;
1778 }
1779 aggr_get_id = perf_stat__get_socket_file;
1780 break;
1781 case AGGR_CORE:
1782 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1783 perror("cannot build core map");
1784 return -1;
1785 }
1786 aggr_get_id = perf_stat__get_core_file;
1787 break;
1788 case AGGR_NONE:
1789 case AGGR_GLOBAL:
1790 case AGGR_THREAD:
1791 case AGGR_UNSET:
1792 default:
1793 break;
1794 }
1795
1796 return 0;
1797}
1798
Andi Kleen44b1e602016-05-30 12:49:42 -03001799static int topdown_filter_events(const char **attr, char **str, bool use_group)
1800{
1801 int off = 0;
1802 int i;
1803 int len = 0;
1804 char *s;
1805
1806 for (i = 0; attr[i]; i++) {
1807 if (pmu_have_event("cpu", attr[i])) {
1808 len += strlen(attr[i]) + 1;
1809 attr[i - off] = attr[i];
1810 } else
1811 off++;
1812 }
1813 attr[i - off] = NULL;
1814
1815 *str = malloc(len + 1 + 2);
1816 if (!*str)
1817 return -1;
1818 s = *str;
1819 if (i - off == 0) {
1820 *s = 0;
1821 return 0;
1822 }
1823 if (use_group)
1824 *s++ = '{';
1825 for (i = 0; attr[i]; i++) {
1826 strcpy(s, attr[i]);
1827 s += strlen(s);
1828 *s++ = ',';
1829 }
1830 if (use_group) {
1831 s[-1] = '}';
1832 *s = 0;
1833 } else
1834 s[-1] = 0;
1835 return 0;
1836}
1837
1838__weak bool arch_topdown_check_group(bool *warn)
1839{
1840 *warn = false;
1841 return false;
1842}
1843
1844__weak void arch_topdown_group_warn(void)
1845{
1846}
1847
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001848/*
1849 * Add default attributes, if there were no attributes specified or
1850 * if -d/--detailed, -d -d or -d -d -d is used:
1851 */
1852static int add_default_attributes(void)
1853{
Andi Kleen44b1e602016-05-30 12:49:42 -03001854 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08001855 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001856
1857 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1858 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1859 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1860 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1861
1862 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001863};
1864 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001865 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001866};
1867 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001868 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001869};
1870 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001871 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1872 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1873 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1874
1875};
1876
1877/*
1878 * Detailed stats (-d), covering the L1 and last level data caches:
1879 */
1880 struct perf_event_attr detailed_attrs[] = {
1881
1882 { .type = PERF_TYPE_HW_CACHE,
1883 .config =
1884 PERF_COUNT_HW_CACHE_L1D << 0 |
1885 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1886 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1887
1888 { .type = PERF_TYPE_HW_CACHE,
1889 .config =
1890 PERF_COUNT_HW_CACHE_L1D << 0 |
1891 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1892 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1893
1894 { .type = PERF_TYPE_HW_CACHE,
1895 .config =
1896 PERF_COUNT_HW_CACHE_LL << 0 |
1897 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1898 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1899
1900 { .type = PERF_TYPE_HW_CACHE,
1901 .config =
1902 PERF_COUNT_HW_CACHE_LL << 0 |
1903 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1904 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1905};
1906
1907/*
1908 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1909 */
1910 struct perf_event_attr very_detailed_attrs[] = {
1911
1912 { .type = PERF_TYPE_HW_CACHE,
1913 .config =
1914 PERF_COUNT_HW_CACHE_L1I << 0 |
1915 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1916 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1917
1918 { .type = PERF_TYPE_HW_CACHE,
1919 .config =
1920 PERF_COUNT_HW_CACHE_L1I << 0 |
1921 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1922 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1923
1924 { .type = PERF_TYPE_HW_CACHE,
1925 .config =
1926 PERF_COUNT_HW_CACHE_DTLB << 0 |
1927 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1928 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1929
1930 { .type = PERF_TYPE_HW_CACHE,
1931 .config =
1932 PERF_COUNT_HW_CACHE_DTLB << 0 |
1933 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1934 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1935
1936 { .type = PERF_TYPE_HW_CACHE,
1937 .config =
1938 PERF_COUNT_HW_CACHE_ITLB << 0 |
1939 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1940 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1941
1942 { .type = PERF_TYPE_HW_CACHE,
1943 .config =
1944 PERF_COUNT_HW_CACHE_ITLB << 0 |
1945 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1946 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1947
1948};
1949
1950/*
1951 * Very, very detailed stats (-d -d -d), adding prefetch events:
1952 */
1953 struct perf_event_attr very_very_detailed_attrs[] = {
1954
1955 { .type = PERF_TYPE_HW_CACHE,
1956 .config =
1957 PERF_COUNT_HW_CACHE_L1D << 0 |
1958 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1959 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1960
1961 { .type = PERF_TYPE_HW_CACHE,
1962 .config =
1963 PERF_COUNT_HW_CACHE_L1D << 0 |
1964 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1965 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1966};
1967
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001968 /* Set attrs if no event is selected and !null_run: */
1969 if (null_run)
1970 return 0;
1971
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001972 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001973 if (pmu_have_event("cpu", "cycles-ct") &&
1974 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001975 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001976 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001977 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1978 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001979 fprintf(stderr, "Cannot set up transaction events\n");
1980 return -1;
1981 }
1982 return 0;
1983 }
1984
Andi Kleen44b1e602016-05-30 12:49:42 -03001985 if (topdown_run) {
1986 char *str = NULL;
1987 bool warn = false;
1988
1989 if (stat_config.aggr_mode != AGGR_GLOBAL &&
1990 stat_config.aggr_mode != AGGR_CORE) {
1991 pr_err("top down event configuration requires --per-core mode\n");
1992 return -1;
1993 }
1994 stat_config.aggr_mode = AGGR_CORE;
1995 if (nr_cgroups || !target__has_cpu(&target)) {
1996 pr_err("top down event configuration requires system-wide mode (-a)\n");
1997 return -1;
1998 }
1999
2000 if (!force_metric_only)
2001 metric_only = true;
2002 if (topdown_filter_events(topdown_attrs, &str,
2003 arch_topdown_check_group(&warn)) < 0) {
2004 pr_err("Out of memory\n");
2005 return -1;
2006 }
2007 if (topdown_attrs[0] && str) {
2008 if (warn)
2009 arch_topdown_group_warn();
2010 err = parse_events(evsel_list, str, NULL);
2011 if (err) {
2012 fprintf(stderr,
2013 "Cannot set up top down events %s: %d\n",
2014 str, err);
2015 free(str);
2016 return -1;
2017 }
2018 } else {
2019 fprintf(stderr, "System does not support topdown\n");
2020 return -1;
2021 }
2022 free(str);
2023 }
2024
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002025 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002026 if (target__has_cpu(&target))
2027 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2028
Andi Kleen9dec4472016-02-26 16:27:56 -08002029 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2030 return -1;
2031 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2032 if (perf_evlist__add_default_attrs(evsel_list,
2033 frontend_attrs) < 0)
2034 return -1;
2035 }
2036 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2037 if (perf_evlist__add_default_attrs(evsel_list,
2038 backend_attrs) < 0)
2039 return -1;
2040 }
2041 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002042 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002043 }
2044
2045 /* Detailed events get appended to the event list: */
2046
2047 if (detailed_run < 1)
2048 return 0;
2049
2050 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002051 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002052 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002053
2054 if (detailed_run < 2)
2055 return 0;
2056
2057 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002058 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002059 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002060
2061 if (detailed_run < 3)
2062 return 0;
2063
2064 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002065 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002066}
2067
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002068static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002069 "perf stat record [<options>]",
2070 NULL,
2071};
2072
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002073static void init_features(struct perf_session *session)
2074{
2075 int feat;
2076
2077 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2078 perf_header__set_feat(&session->header, feat);
2079
2080 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2081 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2082 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2083 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2084}
2085
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002086static int __cmd_record(int argc, const char **argv)
2087{
2088 struct perf_session *session;
2089 struct perf_data_file *file = &perf_stat.file;
2090
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002091 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002092 PARSE_OPT_STOP_AT_NON_OPTION);
2093
2094 if (output_name)
2095 file->path = output_name;
2096
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002097 if (run_count != 1 || forever) {
2098 pr_err("Cannot use -r option with perf stat record.\n");
2099 return -1;
2100 }
2101
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002102 session = perf_session__new(file, false, NULL);
2103 if (session == NULL) {
2104 pr_err("Perf session creation failed.\n");
2105 return -1;
2106 }
2107
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002108 init_features(session);
2109
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002110 session->evlist = evsel_list;
2111 perf_stat.session = session;
2112 perf_stat.record = true;
2113 return argc;
2114}
2115
Jiri Olsaa56f9392015-11-05 15:40:59 +01002116static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2117 union perf_event *event,
2118 struct perf_session *session)
2119{
Andi Kleene3b03b62016-05-05 16:04:03 -07002120 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002121 struct perf_evsel *counter;
2122 struct timespec tsh, *ts = NULL;
2123 const char **argv = session->header.env.cmdline_argv;
2124 int argc = session->header.env.nr_cmdline;
2125
2126 evlist__for_each(evsel_list, counter)
2127 perf_stat_process_counter(&stat_config, counter);
2128
Andi Kleene3b03b62016-05-05 16:04:03 -07002129 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2130 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002131
Andi Kleene3b03b62016-05-05 16:04:03 -07002132 if (stat_config.interval && stat_round->time) {
2133 tsh.tv_sec = stat_round->time / NSECS_PER_SEC;
2134 tsh.tv_nsec = stat_round->time % NSECS_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002135 ts = &tsh;
2136 }
2137
2138 print_counters(ts, argc, argv);
2139 return 0;
2140}
2141
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002142static
2143int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2144 union perf_event *event,
2145 struct perf_session *session __maybe_unused)
2146{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002147 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2148
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002149 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002150
Jiri Olsa89af4e02015-11-05 15:41:02 +01002151 if (cpu_map__empty(st->cpus)) {
2152 if (st->aggr_mode != AGGR_UNSET)
2153 pr_warning("warning: processing task data, aggregation mode not set\n");
2154 return 0;
2155 }
2156
2157 if (st->aggr_mode != AGGR_UNSET)
2158 stat_config.aggr_mode = st->aggr_mode;
2159
Jiri Olsa68d702f2015-11-05 15:40:58 +01002160 if (perf_stat.file.is_pipe)
2161 perf_stat_init_aggr_mode();
2162 else
2163 perf_stat_init_aggr_mode_file(st);
2164
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002165 return 0;
2166}
2167
Jiri Olsa1975d362015-11-05 15:40:56 +01002168static int set_maps(struct perf_stat *st)
2169{
2170 if (!st->cpus || !st->threads)
2171 return 0;
2172
2173 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2174 return -EINVAL;
2175
2176 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2177
2178 if (perf_evlist__alloc_stats(evsel_list, true))
2179 return -ENOMEM;
2180
2181 st->maps_allocated = true;
2182 return 0;
2183}
2184
2185static
2186int process_thread_map_event(struct perf_tool *tool __maybe_unused,
2187 union perf_event *event,
2188 struct perf_session *session __maybe_unused)
2189{
2190 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2191
2192 if (st->threads) {
2193 pr_warning("Extra thread map event, ignoring.\n");
2194 return 0;
2195 }
2196
2197 st->threads = thread_map__new_event(&event->thread_map);
2198 if (!st->threads)
2199 return -ENOMEM;
2200
2201 return set_maps(st);
2202}
2203
2204static
2205int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2206 union perf_event *event,
2207 struct perf_session *session __maybe_unused)
2208{
2209 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2210 struct cpu_map *cpus;
2211
2212 if (st->cpus) {
2213 pr_warning("Extra cpu map event, ignoring.\n");
2214 return 0;
2215 }
2216
2217 cpus = cpu_map__new_data(&event->cpu_map.data);
2218 if (!cpus)
2219 return -ENOMEM;
2220
2221 st->cpus = cpus;
2222 return set_maps(st);
2223}
2224
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002225static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002226 "perf stat report [<options>]",
2227 NULL,
2228};
2229
2230static struct perf_stat perf_stat = {
2231 .tool = {
2232 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002233 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002234 .thread_map = process_thread_map_event,
2235 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002236 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002237 .stat = perf_event__process_stat_event,
2238 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002239 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002240 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002241};
2242
2243static int __cmd_report(int argc, const char **argv)
2244{
2245 struct perf_session *session;
2246 const struct option options[] = {
2247 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002248 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2249 "aggregate counts per processor socket", AGGR_SOCKET),
2250 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2251 "aggregate counts per physical processor core", AGGR_CORE),
2252 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2253 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002254 OPT_END()
2255 };
2256 struct stat st;
2257 int ret;
2258
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002259 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002260
2261 if (!input_name || !strlen(input_name)) {
2262 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2263 input_name = "-";
2264 else
2265 input_name = "perf.data";
2266 }
2267
2268 perf_stat.file.path = input_name;
2269 perf_stat.file.mode = PERF_DATA_MODE_READ;
2270
2271 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2272 if (session == NULL)
2273 return -1;
2274
2275 perf_stat.session = session;
2276 stat_config.output = stderr;
2277 evsel_list = session->evlist;
2278
2279 ret = perf_session__process_events(session);
2280 if (ret)
2281 return ret;
2282
2283 perf_session__delete(session);
2284 return 0;
2285}
2286
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002287int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02002288{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002289 const char * const stat_usage[] = {
2290 "perf stat [<options>] [<command>]",
2291 NULL
2292 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002293 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002294 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002295 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002296 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002297 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002298
Stephane Eranian5af52b52010-05-18 15:00:01 +02002299 setlocale(LC_ALL, "");
2300
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002301 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002302 if (evsel_list == NULL)
2303 return -ENOMEM;
2304
Wang Nan1669e502016-02-19 11:43:58 +00002305 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002306 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2307 (const char **) stat_usage,
2308 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002309 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002310
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002311 if (csv_sep) {
2312 csv_output = true;
2313 if (!strcmp(csv_sep, "\\t"))
2314 csv_sep = "\t";
2315 } else
2316 csv_sep = DEFAULT_SEPARATOR;
2317
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002318 if (argc && !strncmp(argv[0], "rec", 3)) {
2319 argc = __cmd_record(argc, argv);
2320 if (argc < 0)
2321 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002322 } else if (argc && !strncmp(argv[0], "rep", 3))
2323 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002324
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002325 interval = stat_config.interval;
2326
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002327 /*
2328 * For record command the -o is already taken care of.
2329 */
2330 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002331 output = NULL;
2332
Jim Cromie56f3bae2011-09-07 17:14:00 -06002333 if (output_name && output_fd) {
2334 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002335 parse_options_usage(stat_usage, stat_options, "o", 1);
2336 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002337 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002338 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002339
Andi Kleen54b50912016-03-03 15:57:36 -08002340 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2341 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2342 goto out;
2343 }
2344
Andi Kleen54b50912016-03-03 15:57:36 -08002345 if (metric_only && run_count > 1) {
2346 fprintf(stderr, "--metric-only is not supported with -r\n");
2347 goto out;
2348 }
2349
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002350 if (output_fd < 0) {
2351 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002352 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002353 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002354 }
2355
Stephane Eranian4aa90152011-08-15 22:22:33 +02002356 if (!output) {
2357 struct timespec tm;
2358 mode = append_file ? "a" : "w";
2359
2360 output = fopen(output_name, mode);
2361 if (!output) {
2362 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002363 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002364 }
2365 clock_gettime(CLOCK_REALTIME, &tm);
2366 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002367 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002368 mode = append_file ? "a" : "w";
2369 output = fdopen(output_fd, mode);
2370 if (!output) {
2371 perror("Failed opening logfd");
2372 return -errno;
2373 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002374 }
2375
Jiri Olsa58215222015-07-21 14:31:24 +02002376 stat_config.output = output;
2377
Stephane Eraniand7470b62010-12-01 18:49:05 +02002378 /*
2379 * let the spreadsheet do the pretty-printing
2380 */
2381 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002382 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002383 if (big_num_opt == 1) {
2384 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002385 parse_options_usage(stat_usage, stat_options, "B", 1);
2386 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002387 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002388 } else /* Nope, so disable big number formatting */
2389 big_num = false;
2390 } else if (big_num_opt == 0) /* User passed --no-big-num */
2391 big_num = false;
2392
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002393 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01002394 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06002395
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002396 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002397 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002398 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002399 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002400 } else if (run_count == 0) {
2401 forever = true;
2402 run_count = 1;
2403 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002404
Jiri Olsa421a50f2015-07-21 14:31:22 +02002405 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002406 fprintf(stderr, "The --per-thread option is only available "
2407 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002408 parse_options_usage(NULL, stat_options, "p", 1);
2409 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002410 goto out;
2411 }
2412
2413 /*
2414 * no_aggr, cgroup are for system-wide only
2415 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2416 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002417 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2418 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002419 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002420 fprintf(stderr, "both cgroup and no-aggregation "
2421 "modes only available in system-wide mode\n");
2422
Jiri Olsae0547312015-11-05 15:40:45 +01002423 parse_options_usage(stat_usage, stat_options, "G", 1);
2424 parse_options_usage(NULL, stat_options, "A", 1);
2425 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002426 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002427 }
2428
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002429 if (add_default_attributes())
2430 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002431
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002432 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002433
Namhyung Kim77a6f012012-05-07 14:09:04 +09002434 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002435 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002436 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002437 parse_options_usage(stat_usage, stat_options, "p", 1);
2438 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002439 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002440 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002441 parse_options_usage(stat_usage, stat_options, "C", 1);
2442 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002443 }
2444 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002445 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002446
2447 /*
2448 * Initialize thread_map with comm names,
2449 * so we could print it out on output.
2450 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002451 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002452 thread_map__read_comms(evsel_list->threads);
2453
Stephane Eranian13370a92013-01-29 12:47:44 +01002454 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002455 if (interval < 10) {
2456 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002457 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002458 goto out;
2459 } else
2460 pr_warning("print interval < 100ms. "
2461 "The overhead percentage could be high in some cases. "
2462 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002463 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002464
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002465 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002466 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002467
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002468 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002469 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002470
Ingo Molnar58d7e992009-05-15 11:03:23 +02002471 /*
2472 * We dont want to block the signals - that would cause
2473 * child tasks to inherit that and Ctrl-C would not work.
2474 * What we want is for Ctrl-C to work in the exec()-ed
2475 * task, but being ignored by perf stat itself:
2476 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002477 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002478 if (!forever)
2479 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002480 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002481 signal(SIGALRM, skip_signal);
2482 signal(SIGABRT, skip_signal);
2483
Ingo Molnar42202dd2009-06-13 14:57:28 +02002484 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002485 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02002486 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002487 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2488 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002489
Ingo Molnar42202dd2009-06-13 14:57:28 +02002490 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002491 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002492 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002493 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002494 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002495 }
2496
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002497 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002498 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002499
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002500 if (STAT_RECORD) {
2501 /*
2502 * We synthesize the kernel mmap record just so that older tools
2503 * don't emit warnings about not being able to resolve symbols
2504 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2505 * a saner message about no samples being in the perf.data file.
2506 *
2507 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002508 * in header.c at the moment 'perf stat record' gets introduced, which
2509 * is not really needed once we start adding the stat specific PERF_RECORD_
2510 * records, but the need to suppress the kptr_restrict messages in older
2511 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002512 */
2513 int fd = perf_data_file__fd(&perf_stat.file);
2514 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2515 process_synthesized_event,
2516 &perf_stat.session->machines.host);
2517 if (err) {
2518 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2519 "older tools may produce warnings about this file\n.");
2520 }
2521
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002522 if (!interval) {
2523 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2524 pr_err("failed to write stat round event\n");
2525 }
2526
Jiri Olsa664c98d2015-11-05 15:40:50 +01002527 if (!perf_stat.file.is_pipe) {
2528 perf_stat.session->header.data_size += perf_stat.bytes_written;
2529 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2530 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002531
2532 perf_session__delete(perf_stat.session);
2533 }
2534
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002535 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002536 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002537out:
2538 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002539 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002540}