blob: 69523ed55894338cb202804db6ffbf202410b74f [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"
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -060055#include "util/drv_configs.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020056#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080057#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080058#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030060#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020061#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020062#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030063#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010064#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010065#include "util/tool.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030066#include "util/group.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030067#include "util/string2.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010068#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020069
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030070#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030071#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030072#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030073#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020074#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020075#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030076#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020077#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070078#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030079#include <sys/types.h>
80#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030081#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030082#include <unistd.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020083
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030084#include "sane_ctype.h"
85
Stephane Eraniand7470b62010-12-01 18:49:05 +020086#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060087#define CNTR_NOT_SUPPORTED "<not supported>"
88#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070089#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020090
Jiri Olsad4f63a42015-06-26 11:29:26 +020091static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010092
Andi Kleen4cabc3d2013-08-21 16:47:26 -070093/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020094static const char *transaction_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 "cpu/el-start/,"
102 "cpu/cycles-ct/"
103 "}"
104};
105
106/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200107static const char * transaction_limited_attrs = {
108 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700109 "{"
110 "instructions,"
111 "cycles,"
112 "cpu/cycles-t/,"
113 "cpu/tx-start/"
114 "}"
115};
116
Andi Kleen44b1e602016-05-30 12:49:42 -0300117static const char * topdown_attrs[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
123 NULL,
124};
125
Kan Liangdaefd0b2017-05-26 12:05:38 -0700126static const char *smi_cost_attrs = {
127 "{"
128 "msr/aperf/,"
129 "msr/smi/,"
130 "cycles"
131 "}"
132};
133
Robert Richter666e6d42012-04-05 18:26:27 +0200134static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200135
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300136static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900137 .uid = UINT_MAX,
138};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530139
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100140typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
141
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530142static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200143static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200144static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000145static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200146static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700147static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300148static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700149static bool smi_cost = false;
150static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200151static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200152static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200153static const char *csv_sep = NULL;
154static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800155static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200156static const char *pre_cmd = NULL;
157static const char *post_cmd = NULL;
158static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700159static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100160static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500161static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800162static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300163static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700164static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100165static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100166static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100167static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100168static bool append_file;
169static const char *output_name;
170static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100171static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200172
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100173struct perf_stat {
174 bool record;
175 struct perf_data_file file;
176 struct perf_session *session;
177 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100178 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100179 bool maps_allocated;
180 struct cpu_map *cpus;
181 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100182 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100183};
184
185static struct perf_stat perf_stat;
186#define STAT_RECORD perf_stat.record
187
Liming Wang60666c62009-12-31 16:05:50 +0800188static volatile int done = 0;
189
Jiri Olsa421a50f2015-07-21 14:31:22 +0200190static struct perf_stat_config stat_config = {
191 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200192 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200193};
194
Stephane Eranian13370a92013-01-29 12:47:44 +0100195static inline void diff_timespec(struct timespec *r, struct timespec *a,
196 struct timespec *b)
197{
198 r->tv_sec = a->tv_sec - b->tv_sec;
199 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300200 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100201 r->tv_sec--;
202 } else {
203 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
204 }
205}
206
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200207static void perf_stat__reset_stats(void)
208{
209 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200210 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200211}
212
Jiri Olsacac21422012-11-12 18:34:00 +0100213static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200214{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200215 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200216 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200217
Jiri Olsa82bf3112017-07-26 14:02:06 +0200218 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200219 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
220 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200221 }
222
223 /*
224 * The event is part of non trivial group, let's enable
225 * the group read (for leader) and ID retrieval for all
226 * members.
227 */
228 if (leader->nr_members > 1)
229 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200230
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200231 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300232
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100233 /*
234 * Some events get initialized with sample_(period/type) set,
235 * like tracepoints. Clear it up for counting.
236 */
237 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100238
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100239 /*
240 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
241 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100242 *
243 * However for pipe sessions we need to keep it zero,
244 * because script's perf_evsel__check_attr is triggered
245 * by attr->sample_type != 0, and we can't run it on
246 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100247 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100248 if (!(STAT_RECORD && perf_stat.file.is_pipe))
249 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100250
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100251 /*
252 * Disabling all counters initially, they will be enabled
253 * either manually by us or by kernel via enable_on_exec
254 * set later.
255 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100256 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100257 attr->disabled = 1;
258
Jiri Olsac8280ce2015-12-03 10:06:45 +0100259 /*
260 * In case of initial_delay we enable tracee
261 * events manually.
262 */
263 if (target__none(&target) && !initial_delay)
264 attr->enable_on_exec = 1;
265 }
266
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300267 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300268 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200269
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300270 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200271}
272
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200273/*
274 * Does the counter have nsecs as a unit?
275 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200276static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200277{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200278 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
279 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200280 return 1;
281
282 return 0;
283}
284
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100285static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100286 union perf_event *event,
287 struct perf_sample *sample __maybe_unused,
288 struct machine *machine __maybe_unused)
289{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100290 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
291 pr_err("failed to write perf data, error: %m\n");
292 return -1;
293 }
294
295 perf_stat.bytes_written += event->header.size;
296 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100297}
298
Jiri Olsa1975d362015-11-05 15:40:56 +0100299static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100300{
Jiri Olsa1975d362015-11-05 15:40:56 +0100301 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100302 process_synthesized_event,
303 NULL);
304}
305
306#define WRITE_STAT_ROUND_EVENT(time, interval) \
307 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
308
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100309#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
310
311static int
312perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
313 struct perf_counts_values *count)
314{
315 struct perf_sample_id *sid = SID(counter, cpu, thread);
316
317 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
318 process_synthesized_event, NULL);
319}
320
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200321/*
322 * Read out the results of a single counter:
323 * do not aggregate counts across CPUs in system-wide mode
324 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200325static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200326{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100327 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100328 int ncpus, cpu, thread;
329
330 if (target__has_cpu(&target))
331 ncpus = perf_evsel__nr_cpus(counter);
332 else
333 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200334
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000335 if (!counter->supported)
336 return -ENOENT;
337
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100338 if (counter->system_wide)
339 nthreads = 1;
340
341 for (thread = 0; thread < nthreads; thread++) {
342 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200343 struct perf_counts_values *count;
344
345 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200346
347 /*
348 * The leader's group read loads data into its group members
349 * (via perf_evsel__read_counter) and sets threir count->loaded.
350 */
351 if (!count->loaded &&
352 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700353 counter->counts->scaled = -1;
354 perf_counts(counter->counts, cpu, thread)->ena = 0;
355 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100356 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700357 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100358
Jiri Olsa82bf3112017-07-26 14:02:06 +0200359 count->loaded = false;
360
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100361 if (STAT_RECORD) {
362 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
363 pr_err("failed to write stat event\n");
364 return -1;
365 }
366 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700367
368 if (verbose > 1) {
369 fprintf(stat_config.output,
370 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
371 perf_evsel__name(counter),
372 cpu,
373 count->val, count->ena, count->run);
374 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100375 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200376 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200377
378 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200379}
380
Mark Rutland3df33ef2016-08-09 14:04:29 +0100381static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200382{
383 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700384 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200385
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300386 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700387 ret = read_counter(counter);
388 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700389 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200390
Stephane Eraniandb49a712017-04-12 11:23:01 -0700391 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200392 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200393 }
394}
395
Jiri Olsaba411a92015-06-26 11:29:24 +0200396static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100397{
Stephane Eranian13370a92013-01-29 12:47:44 +0100398 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100399
Mark Rutland3df33ef2016-08-09 14:04:29 +0100400 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100401
Stephane Eranian13370a92013-01-29 12:47:44 +0100402 clock_gettime(CLOCK_MONOTONIC, &ts);
403 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100404
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100405 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300406 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100407 pr_err("failed to write stat round event\n");
408 }
409
Jiri Olsad4f63a42015-06-26 11:29:26 +0200410 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100411}
412
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100413static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700414{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100415 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300416 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100417
418 /*
419 * We need to enable counters only if:
420 * - we don't have tracee (attaching to task or cpu)
421 * - we have initial delay configured
422 */
423 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100424 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700425}
426
Mark Rutland3df33ef2016-08-09 14:04:29 +0100427static void disable_counters(void)
428{
429 /*
430 * If we don't have tracee (attaching to task or cpu), counters may
431 * still be running. To get accurate group ratios, we must stop groups
432 * from counting before reading their constituent counters.
433 */
434 if (!target__none(&target))
435 perf_evlist__disable(evsel_list);
436}
437
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300438static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300439
440/*
441 * perf_evlist__prepare_workload will send a SIGUSR1
442 * if the fork fails, since we asked by setting its
443 * want_signal to true.
444 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300445static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
446 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300447{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300448 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300449}
450
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100451static bool has_unit(struct perf_evsel *counter)
452{
453 return counter->unit && *counter->unit;
454}
455
456static bool has_scale(struct perf_evsel *counter)
457{
458 return counter->scale != 1;
459}
460
Jiri Olsa664c98d2015-11-05 15:40:50 +0100461static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100462{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100463 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100464 int err;
465
Jiri Olsa664c98d2015-11-05 15:40:50 +0100466 if (is_pipe) {
467 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
468 process_synthesized_event);
469 if (err < 0) {
470 pr_err("Couldn't synthesize attrs.\n");
471 return err;
472 }
473 }
474
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100475 /*
476 * Synthesize other events stuff not carried within
477 * attr event - unit, scale, name
478 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300479 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100480 if (!counter->supported)
481 continue;
482
483 /*
484 * Synthesize unit and scale only if it's defined.
485 */
486 if (has_unit(counter)) {
487 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
488 if (err < 0) {
489 pr_err("Couldn't synthesize evsel unit.\n");
490 return err;
491 }
492 }
493
494 if (has_scale(counter)) {
495 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
496 if (err < 0) {
497 pr_err("Couldn't synthesize evsel scale.\n");
498 return err;
499 }
500 }
501
502 if (counter->own_cpus) {
503 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
504 if (err < 0) {
505 pr_err("Couldn't synthesize evsel scale.\n");
506 return err;
507 }
508 }
509
510 /*
511 * Name is needed only for pipe output,
512 * perf.data carries event names.
513 */
514 if (is_pipe) {
515 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
516 if (err < 0) {
517 pr_err("Couldn't synthesize evsel name.\n");
518 return err;
519 }
520 }
521 }
522
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100523 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
524 process_synthesized_event,
525 NULL);
526 if (err < 0) {
527 pr_err("Couldn't synthesize thread map.\n");
528 return err;
529 }
530
531 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
532 process_synthesized_event, NULL);
533 if (err < 0) {
534 pr_err("Couldn't synthesize thread map.\n");
535 return err;
536 }
537
538 err = perf_event__synthesize_stat_config(NULL, &stat_config,
539 process_synthesized_event, NULL);
540 if (err < 0) {
541 pr_err("Couldn't synthesize config.\n");
542 return err;
543 }
544
545 return 0;
546}
547
Jiri Olsa2af46462015-11-05 15:40:49 +0100548#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
549
550static int __store_counter_ids(struct perf_evsel *counter,
551 struct cpu_map *cpus,
552 struct thread_map *threads)
553{
554 int cpu, thread;
555
556 for (cpu = 0; cpu < cpus->nr; cpu++) {
557 for (thread = 0; thread < threads->nr; thread++) {
558 int fd = FD(counter, cpu, thread);
559
560 if (perf_evlist__id_add_fd(evsel_list, counter,
561 cpu, thread, fd) < 0)
562 return -1;
563 }
564 }
565
566 return 0;
567}
568
569static int store_counter_ids(struct perf_evsel *counter)
570{
571 struct cpu_map *cpus = counter->cpus;
572 struct thread_map *threads = counter->threads;
573
574 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
575 return -ENOMEM;
576
577 return __store_counter_ids(counter, cpus, threads);
578}
579
Jiri Olsa82bf3112017-07-26 14:02:06 +0200580static bool perf_evsel__should_store_id(struct perf_evsel *counter)
581{
582 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
583}
584
Namhyung Kimacf28922013-03-11 16:43:18 +0900585static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200586{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200587 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300588 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200589 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100590 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100591 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100592 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200593 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300594 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100595 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600596 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200597
Stephane Eranian13370a92013-01-29 12:47:44 +0100598 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300599 ts.tv_sec = interval / USEC_PER_MSEC;
600 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100601 } else {
602 ts.tv_sec = 1;
603 ts.tv_nsec = 0;
604 }
605
Liming Wang60666c62009-12-31 16:05:50 +0800606 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100607 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300608 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900609 perror("failed to prepare workload");
610 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800611 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900612 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000613 }
614
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200615 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300616 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200617
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300618 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300619try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100620 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600621 /*
622 * PPC returns ENXIO for HW counters until 2.6.37
623 * (behavior changed with commit b0a873e).
624 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100625 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600626 errno == ENOENT || errno == EOPNOTSUPP ||
627 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900628 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600629 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300630 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600631 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400632
633 if ((counter->leader != counter) ||
634 !(counter->leader->nr_members > 1))
635 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300636 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900637 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300638 ui__warning("%s\n", msg);
639 goto try_again;
640 }
Ingo Molnarede70292011-04-28 08:48:42 +0200641
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300642 perf_evsel__open_strerror(counter, &target,
643 errno, msg, sizeof(msg));
644 ui__error("%s\n", msg);
645
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200646 if (child_pid != -1)
647 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600648
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200649 return -1;
650 }
David Ahern2cee77c2011-05-30 08:55:59 -0600651 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100652
653 l = strlen(counter->unit);
654 if (l > unit_width)
655 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100656
Jiri Olsa82bf3112017-07-26 14:02:06 +0200657 if (perf_evsel__should_store_id(counter) &&
658 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100659 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300660 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200661
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300662 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300663 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300664 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300665 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100666 return -1;
667 }
668
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600669 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300670 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600671 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
672 str_error_r(errno, msg, sizeof(msg)));
673 return -1;
674 }
675
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100676 if (STAT_RECORD) {
677 int err, fd = perf_data_file__fd(&perf_stat.file);
678
Jiri Olsa664c98d2015-11-05 15:40:50 +0100679 if (is_pipe) {
680 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
681 } else {
682 err = perf_session__write_header(perf_stat.session, evsel_list,
683 fd, false);
684 }
685
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100686 if (err < 0)
687 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100688
Jiri Olsa664c98d2015-11-05 15:40:50 +0100689 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100690 if (err < 0)
691 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100692 }
693
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200694 /*
695 * Enable counters and exec the command:
696 */
697 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100698 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200699
Liming Wang60666c62009-12-31 16:05:50 +0800700 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900701 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100702 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900703
Stephane Eranian13370a92013-01-29 12:47:44 +0100704 if (interval) {
705 while (!waitpid(child_pid, &status, WNOHANG)) {
706 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200707 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100708 }
709 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200710 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300711
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300712 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300713 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300714 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300715 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300716 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300717
Andi Kleen33e49ea2011-09-15 14:31:40 -0700718 if (WIFSIGNALED(status))
719 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800720 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100721 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100722 while (!done) {
723 nanosleep(&ts, NULL);
724 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200725 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100726 }
Liming Wang60666c62009-12-31 16:05:50 +0800727 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200728
Mark Rutland3df33ef2016-08-09 14:04:29 +0100729 disable_counters();
730
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200731 t1 = rdclock();
732
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200733 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200734
Mark Rutland3df33ef2016-08-09 14:04:29 +0100735 /*
736 * Closing a group leader splits the group, and as we only disable
737 * group leaders, results in remaining events becoming enabled. To
738 * avoid arbitrary skew, we must read all counters before closing any
739 * group leaders.
740 */
741 read_counters();
742 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200743
Ingo Molnar42202dd2009-06-13 14:57:28 +0200744 return WEXITSTATUS(status);
745}
746
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300747static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200748{
749 int ret;
750
751 if (pre_cmd) {
752 ret = system(pre_cmd);
753 if (ret)
754 return ret;
755 }
756
757 if (sync_run)
758 sync();
759
760 ret = __run_perf_stat(argc, argv);
761 if (ret)
762 return ret;
763
764 if (post_cmd) {
765 ret = system(post_cmd);
766 if (ret)
767 return ret;
768 }
769
770 return ret;
771}
772
Andi Kleend73515c2015-03-11 07:16:27 -0700773static void print_running(u64 run, u64 ena)
774{
775 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200776 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700777 csv_sep,
778 run,
779 csv_sep,
780 ena ? 100.0 * run / ena : 100.0);
781 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200782 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700783 }
784}
785
Ingo Molnarf99844c2011-04-27 05:35:39 +0200786static void print_noise_pct(double total, double avg)
787{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800788 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200789
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700790 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200791 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600792 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200793 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200794}
795
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200796static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200797{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200798 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200799
Peter Zijlstra849abde2009-09-04 18:23:38 +0200800 if (run_count == 1)
801 return;
802
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200803 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200804 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200805}
806
Stephane Eranian12c08a92013-02-14 13:57:29 +0100807static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200808{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200809 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100810 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200811 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100812 cpu_map__id_to_socket(id),
813 csv_output ? 0 : -8,
814 cpu_map__id_to_cpu(id),
815 csv_sep,
816 csv_output ? 0 : 4,
817 nr,
818 csv_sep);
819 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100820 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200821 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100822 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100823 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100824 csv_sep,
825 csv_output ? 0 : 4,
826 nr,
827 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100828 break;
829 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200830 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200831 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100832 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100833 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200834 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200835 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200836 csv_output ? 0 : 16,
837 thread_map__comm(evsel->threads, id),
838 csv_output ? 0 : -8,
839 thread_map__pid(evsel->threads, id),
840 csv_sep);
841 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100842 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200843 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100844 default:
845 break;
846 }
847}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200848
Andi Kleen140aead2016-01-30 09:06:49 -0800849struct outstate {
850 FILE *fh;
851 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800852 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800853 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800854 int id, nr;
855 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800856};
857
858#define METRIC_LEN 35
859
860static void new_line_std(void *ctx)
861{
862 struct outstate *os = ctx;
863
864 os->newline = true;
865}
866
867static void do_new_line_std(struct outstate *os)
868{
869 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800870 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800871 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800872 if (stat_config.aggr_mode == AGGR_NONE)
873 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800874 fprintf(os->fh, " ");
875}
876
877static void print_metric_std(void *ctx, const char *color, const char *fmt,
878 const char *unit, double val)
879{
880 struct outstate *os = ctx;
881 FILE *out = os->fh;
882 int n;
883 bool newline = os->newline;
884
885 os->newline = false;
886
887 if (unit == NULL || fmt == NULL) {
888 fprintf(out, "%-*s", METRIC_LEN, "");
889 return;
890 }
891
892 if (newline)
893 do_new_line_std(os);
894
895 n = fprintf(out, " # ");
896 if (color)
897 n += color_fprintf(out, color, fmt, val);
898 else
899 n += fprintf(out, fmt, val);
900 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
901}
902
Andi Kleen92a61f62016-02-29 14:36:21 -0800903static void new_line_csv(void *ctx)
904{
905 struct outstate *os = ctx;
906 int i;
907
908 fputc('\n', os->fh);
909 if (os->prefix)
910 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800911 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800912 for (i = 0; i < os->nfields; i++)
913 fputs(csv_sep, os->fh);
914}
915
916static void print_metric_csv(void *ctx,
917 const char *color __maybe_unused,
918 const char *fmt, const char *unit, double val)
919{
920 struct outstate *os = ctx;
921 FILE *out = os->fh;
922 char buf[64], *vals, *ends;
923
924 if (unit == NULL || fmt == NULL) {
925 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
926 return;
927 }
928 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900929 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800930 while (isdigit(*ends) || *ends == '.')
931 ends++;
932 *ends = 0;
933 while (isspace(*unit))
934 unit++;
935 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
936}
937
Andi Kleen54b50912016-03-03 15:57:36 -0800938#define METRIC_ONLY_LEN 20
939
940/* Filter out some columns that don't work well in metrics only mode */
941
942static bool valid_only_metric(const char *unit)
943{
944 if (!unit)
945 return false;
946 if (strstr(unit, "/sec") ||
947 strstr(unit, "hz") ||
948 strstr(unit, "Hz") ||
949 strstr(unit, "CPUs utilized"))
950 return false;
951 return true;
952}
953
954static const char *fixunit(char *buf, struct perf_evsel *evsel,
955 const char *unit)
956{
957 if (!strncmp(unit, "of all", 6)) {
958 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
959 unit);
960 return buf;
961 }
962 return unit;
963}
964
965static void print_metric_only(void *ctx, const char *color, const char *fmt,
966 const char *unit, double val)
967{
968 struct outstate *os = ctx;
969 FILE *out = os->fh;
970 int n;
971 char buf[1024];
972 unsigned mlen = METRIC_ONLY_LEN;
973
974 if (!valid_only_metric(unit))
975 return;
976 unit = fixunit(buf, os->evsel, unit);
977 if (color)
978 n = color_fprintf(out, color, fmt, val);
979 else
980 n = fprintf(out, fmt, val);
981 if (n > METRIC_ONLY_LEN)
982 n = METRIC_ONLY_LEN;
983 if (mlen < strlen(unit))
984 mlen = strlen(unit) + 1;
985 fprintf(out, "%*s", mlen - n, "");
986}
987
988static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
989 const char *fmt,
990 const char *unit, double val)
991{
992 struct outstate *os = ctx;
993 FILE *out = os->fh;
994 char buf[64], *vals, *ends;
995 char tbuf[1024];
996
997 if (!valid_only_metric(unit))
998 return;
999 unit = fixunit(tbuf, os->evsel, unit);
1000 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001001 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001002 while (isdigit(*ends) || *ends == '.')
1003 ends++;
1004 *ends = 0;
1005 fprintf(out, "%s%s", vals, csv_sep);
1006}
1007
1008static void new_line_metric(void *ctx __maybe_unused)
1009{
1010}
1011
1012static void print_metric_header(void *ctx, const char *color __maybe_unused,
1013 const char *fmt __maybe_unused,
1014 const char *unit, double val __maybe_unused)
1015{
1016 struct outstate *os = ctx;
1017 char tbuf[1024];
1018
1019 if (!valid_only_metric(unit))
1020 return;
1021 unit = fixunit(tbuf, os->evsel, unit);
1022 if (csv_output)
1023 fprintf(os->fh, "%s%s", unit, csv_sep);
1024 else
1025 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1026}
1027
Andi Kleenda88c7f2014-09-24 13:50:46 -07001028static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001029{
Jiri Olsa58215222015-07-21 14:31:24 +02001030 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001031 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001032 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001033 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001034
Stephane Eranian410136f2013-11-12 17:58:49 +01001035 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1036 fmt_n = csv_output ? "%s" : "%-25s";
1037
Andi Kleenda88c7f2014-09-24 13:50:46 -07001038 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001039
David Ahern4bbe5a62013-09-28 14:28:00 -06001040 scnprintf(name, sizeof(name), "%s%s",
1041 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001042
1043 fprintf(output, fmt_v, msecs, csv_sep);
1044
1045 if (csv_output)
1046 fprintf(output, "%s%s", evsel->unit, csv_sep);
1047 else
1048 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1049
1050 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001051
Stephane Eranian023695d2011-02-14 11:20:01 +02001052 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001053 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001054}
1055
Andi Kleen44d49a62016-02-29 14:36:22 -08001056static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1057{
1058 int i;
1059
1060 if (!aggr_get_id)
1061 return 0;
1062
1063 if (stat_config.aggr_mode == AGGR_NONE)
1064 return id;
1065
1066 if (stat_config.aggr_mode == AGGR_GLOBAL)
1067 return 0;
1068
1069 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1070 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1071
1072 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1073 return cpu2;
1074 }
1075 return 0;
1076}
1077
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001078static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1079{
Jiri Olsa58215222015-07-21 14:31:24 +02001080 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001081 double sc = evsel->scale;
1082 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001083
1084 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001085 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001086 } else {
1087 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001088 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001089 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001090 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001091 }
1092
1093 aggr_printout(evsel, id, nr);
1094
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001095 fprintf(output, fmt, avg, csv_sep);
1096
1097 if (evsel->unit)
1098 fprintf(output, "%-*s%s",
1099 csv_output ? 0 : unit_width,
1100 evsel->unit, csv_sep);
1101
1102 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1103
1104 if (evsel->cgrp)
1105 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001106}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001107
Andi Kleenf9483392016-01-30 09:06:50 -08001108static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001109 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001110{
Andi Kleen140aead2016-01-30 09:06:49 -08001111 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001112 struct outstate os = {
1113 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001114 .prefix = prefix ? prefix : "",
1115 .id = id,
1116 .nr = nr,
1117 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001118 };
Andi Kleen140aead2016-01-30 09:06:49 -08001119 print_metric_t pm = print_metric_std;
1120 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001121
Andi Kleen54b50912016-03-03 15:57:36 -08001122 if (metric_only) {
1123 nl = new_line_metric;
1124 if (csv_output)
1125 pm = print_metric_only_csv;
1126 else
1127 pm = print_metric_only;
1128 } else
1129 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001130
Andi Kleen54b50912016-03-03 15:57:36 -08001131 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001132 static int aggr_fields[] = {
1133 [AGGR_GLOBAL] = 0,
1134 [AGGR_THREAD] = 1,
1135 [AGGR_NONE] = 1,
1136 [AGGR_SOCKET] = 2,
1137 [AGGR_CORE] = 2,
1138 };
1139
1140 pm = print_metric_csv;
1141 nl = new_line_csv;
1142 os.nfields = 3;
1143 os.nfields += aggr_fields[stat_config.aggr_mode];
1144 if (counter->cgrp)
1145 os.nfields++;
1146 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001147 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001148 if (metric_only) {
1149 pm(&os, NULL, "", "", 0);
1150 return;
1151 }
Andi Kleencb110f42016-01-30 09:06:51 -08001152 aggr_printout(counter, id, nr);
1153
1154 fprintf(stat_config.output, "%*s%s",
1155 csv_output ? 0 : 18,
1156 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1157 csv_sep);
1158
Borislav Petkov02d492e2017-02-07 01:40:05 +01001159 if (counter->supported)
1160 print_free_counters_hint = 1;
1161
Andi Kleencb110f42016-01-30 09:06:51 -08001162 fprintf(stat_config.output, "%-*s%s",
1163 csv_output ? 0 : unit_width,
1164 counter->unit, csv_sep);
1165
1166 fprintf(stat_config.output, "%*s",
1167 csv_output ? 0 : -25,
1168 perf_evsel__name(counter));
1169
1170 if (counter->cgrp)
1171 fprintf(stat_config.output, "%s%s",
1172 csv_sep, counter->cgrp->name);
1173
Andi Kleen92a61f62016-02-29 14:36:21 -08001174 if (!csv_output)
1175 pm(&os, NULL, NULL, "", 0);
1176 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001177 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001178 if (csv_output)
1179 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001180 return;
1181 }
1182
Andi Kleen54b50912016-03-03 15:57:36 -08001183 if (metric_only)
1184 /* nothing */;
1185 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001186 nsec_printout(id, nr, counter, uval);
1187 else
1188 abs_printout(id, nr, counter, uval);
1189
Andi Kleen140aead2016-01-30 09:06:49 -08001190 out.print_metric = pm;
1191 out.new_line = nl;
1192 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001193 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001194
Andi Kleen54b50912016-03-03 15:57:36 -08001195 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001196 print_noise(counter, noise);
1197 print_running(run, ena);
1198 }
1199
1200 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001201 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001202 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001203 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001204 print_noise(counter, noise);
1205 print_running(run, ena);
1206 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001207}
1208
Andi Kleen44d49a62016-02-29 14:36:22 -08001209static void aggr_update_shadow(void)
1210{
1211 int cpu, s2, id, s;
1212 u64 val;
1213 struct perf_evsel *counter;
1214
1215 for (s = 0; s < aggr_map->nr; s++) {
1216 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001217 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001218 val = 0;
1219 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1220 s2 = aggr_get_id(evsel_list->cpus, cpu);
1221 if (s2 != id)
1222 continue;
1223 val += perf_counts(counter->counts, cpu, 0)->val;
1224 }
1225 val = val * counter->scale;
1226 perf_stat__update_shadow_stats(counter, &val,
1227 first_shadow_cpu(counter, id));
1228 }
1229 }
1230}
1231
Andi Kleen430daf22017-03-20 13:17:00 -07001232static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001233 void (*cb)(struct perf_evsel *counter, void *data,
1234 bool first),
1235 void *data)
1236{
Andi Kleen430daf22017-03-20 13:17:00 -07001237 struct perf_evsel *alias;
1238
1239 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1240 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1241 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1242 alias->scale != counter->scale ||
1243 alias->cgrp != counter->cgrp ||
1244 strcmp(alias->unit, counter->unit) ||
1245 nsec_counter(alias) != nsec_counter(counter))
1246 break;
1247 alias->merged_stat = true;
1248 cb(alias, data, false);
1249 }
1250}
1251
1252static bool collect_data(struct perf_evsel *counter,
1253 void (*cb)(struct perf_evsel *counter, void *data,
1254 bool first),
1255 void *data)
1256{
1257 if (counter->merged_stat)
1258 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001259 cb(counter, data, true);
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001260 if (!no_merge && counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001261 collect_all_aliases(counter, cb, data);
1262 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001263}
1264
1265struct aggr_data {
1266 u64 ena, run, val;
1267 int id;
1268 int nr;
1269 int cpu;
1270};
1271
1272static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1273{
1274 struct aggr_data *ad = data;
1275 int cpu, s2;
1276
1277 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1278 struct perf_counts_values *counts;
1279
1280 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1281 if (s2 != ad->id)
1282 continue;
1283 if (first)
1284 ad->nr++;
1285 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001286 /*
1287 * When any result is bad, make them all to give
1288 * consistent output in interval mode.
1289 */
1290 if (counts->ena == 0 || counts->run == 0 ||
1291 counter->counts->scaled == -1) {
1292 ad->ena = 0;
1293 ad->run = 0;
1294 break;
1295 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001296 ad->val += counts->val;
1297 ad->ena += counts->ena;
1298 ad->run += counts->run;
1299 }
1300}
1301
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001302static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001303{
Jiri Olsa58215222015-07-21 14:31:24 +02001304 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001305 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001306 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001307 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001308 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001309 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001310
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001311 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001312 return;
1313
Andi Kleen44d49a62016-02-29 14:36:22 -08001314 aggr_update_shadow();
1315
Andi Kleen54b50912016-03-03 15:57:36 -08001316 /*
1317 * With metric_only everything is on a single line.
1318 * Without each counter has its own line.
1319 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001320 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001321 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001322 if (prefix && metric_only)
1323 fprintf(output, "%s", prefix);
1324
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001325 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001326 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001327 evlist__for_each_entry(evsel_list, counter) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001328 ad.val = ad.ena = ad.run = 0;
1329 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001330 if (!collect_data(counter, aggr_cb, &ad))
1331 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001332 nr = ad.nr;
1333 ena = ad.ena;
1334 run = ad.run;
1335 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001336 if (first && metric_only) {
1337 first = false;
1338 aggr_printout(counter, id, nr);
1339 }
1340 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001341 fprintf(output, "%s", prefix);
1342
Stephane Eranian410136f2013-11-12 17:58:49 +01001343 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001344 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001345 if (!metric_only)
1346 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001347 }
Andi Kleen54b50912016-03-03 15:57:36 -08001348 if (metric_only)
1349 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001350 }
1351}
1352
Jiri Olsa32b8af82015-06-26 11:29:27 +02001353static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1354{
Jiri Olsa58215222015-07-21 14:31:24 +02001355 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001356 int nthreads = thread_map__nr(counter->threads);
1357 int ncpus = cpu_map__nr(counter->cpus);
1358 int cpu, thread;
1359 double uval;
1360
1361 for (thread = 0; thread < nthreads; thread++) {
1362 u64 ena = 0, run = 0, val = 0;
1363
1364 for (cpu = 0; cpu < ncpus; cpu++) {
1365 val += perf_counts(counter->counts, cpu, thread)->val;
1366 ena += perf_counts(counter->counts, cpu, thread)->ena;
1367 run += perf_counts(counter->counts, cpu, thread)->run;
1368 }
1369
1370 if (prefix)
1371 fprintf(output, "%s", prefix);
1372
1373 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001374 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001375 fputc('\n', output);
1376 }
1377}
1378
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001379struct caggr_data {
1380 double avg, avg_enabled, avg_running;
1381};
1382
1383static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1384 bool first __maybe_unused)
1385{
1386 struct caggr_data *cd = data;
1387 struct perf_stat_evsel *ps = counter->priv;
1388
1389 cd->avg += avg_stats(&ps->res_stats[0]);
1390 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1391 cd->avg_running += avg_stats(&ps->res_stats[2]);
1392}
1393
Ingo Molnar42202dd2009-06-13 14:57:28 +02001394/*
1395 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001396 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001397 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001398static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001399{
Jiri Olsa58215222015-07-21 14:31:24 +02001400 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001401 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001402 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001403
Andi Kleen430daf22017-03-20 13:17:00 -07001404 if (!collect_data(counter, counter_aggr_cb, &cd))
1405 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001406
Andi Kleen54b50912016-03-03 15:57:36 -08001407 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001408 fprintf(output, "%s", prefix);
1409
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001410 uval = cd.avg * counter->scale;
1411 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001412 if (!metric_only)
1413 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001414}
1415
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001416static void counter_cb(struct perf_evsel *counter, void *data,
1417 bool first __maybe_unused)
1418{
1419 struct aggr_data *ad = data;
1420
1421 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1422 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1423 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1424}
1425
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001426/*
1427 * Print out the results of a single counter:
1428 * does not use aggregated count in system-wide
1429 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001430static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001431{
Jiri Olsa58215222015-07-21 14:31:24 +02001432 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001433 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001434 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001435 int cpu;
1436
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001437 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001438 struct aggr_data ad = { .cpu = cpu };
1439
Andi Kleen430daf22017-03-20 13:17:00 -07001440 if (!collect_data(counter, counter_cb, &ad))
1441 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001442 val = ad.val;
1443 ena = ad.ena;
1444 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001445
1446 if (prefix)
1447 fprintf(output, "%s", prefix);
1448
Stephane Eranian410136f2013-11-12 17:58:49 +01001449 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001450 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001451
Stephane Eranian4aa90152011-08-15 22:22:33 +02001452 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001453 }
1454}
1455
Andi Kleen206cab62016-03-03 15:57:37 -08001456static void print_no_aggr_metric(char *prefix)
1457{
1458 int cpu;
1459 int nrcpus = 0;
1460 struct perf_evsel *counter;
1461 u64 ena, run, val;
1462 double uval;
1463
1464 nrcpus = evsel_list->cpus->nr;
1465 for (cpu = 0; cpu < nrcpus; cpu++) {
1466 bool first = true;
1467
1468 if (prefix)
1469 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001470 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001471 if (first) {
1472 aggr_printout(counter, cpu, 0);
1473 first = false;
1474 }
1475 val = perf_counts(counter->counts, cpu, 0)->val;
1476 ena = perf_counts(counter->counts, cpu, 0)->ena;
1477 run = perf_counts(counter->counts, cpu, 0)->run;
1478
1479 uval = val * counter->scale;
1480 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1481 }
1482 fputc('\n', stat_config.output);
1483 }
1484}
1485
Andi Kleen54b50912016-03-03 15:57:36 -08001486static int aggr_header_lens[] = {
1487 [AGGR_CORE] = 18,
1488 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001489 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001490 [AGGR_THREAD] = 24,
1491 [AGGR_GLOBAL] = 0,
1492};
1493
Andi Kleenc51fd632016-05-24 12:52:39 -07001494static const char *aggr_header_csv[] = {
1495 [AGGR_CORE] = "core,cpus,",
1496 [AGGR_SOCKET] = "socket,cpus",
1497 [AGGR_NONE] = "cpu,",
1498 [AGGR_THREAD] = "comm-pid,",
1499 [AGGR_GLOBAL] = ""
1500};
1501
Andi Kleen41c8ca22016-05-24 12:52:38 -07001502static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001503{
1504 struct perf_stat_output_ctx out;
1505 struct perf_evsel *counter;
1506 struct outstate os = {
1507 .fh = stat_config.output
1508 };
1509
1510 if (prefix)
1511 fprintf(stat_config.output, "%s", prefix);
1512
Andi Kleen41c8ca22016-05-24 12:52:38 -07001513 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001514 fprintf(stat_config.output, "%*s",
1515 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001516 if (csv_output) {
1517 if (stat_config.interval)
1518 fputs("time,", stat_config.output);
1519 fputs(aggr_header_csv[stat_config.aggr_mode],
1520 stat_config.output);
1521 }
Andi Kleen54b50912016-03-03 15:57:36 -08001522
1523 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001524 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001525 os.evsel = counter;
1526 out.ctx = &os;
1527 out.print_metric = print_metric_header;
1528 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001529 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001530 os.evsel = counter;
1531 perf_stat__print_shadow_stats(counter, 0,
1532 0,
1533 &out);
1534 }
1535 fputc('\n', stat_config.output);
1536}
1537
Jiri Olsad4f63a42015-06-26 11:29:26 +02001538static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001539{
Jiri Olsa58215222015-07-21 14:31:24 +02001540 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001541 static int num_print_interval;
1542
1543 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1544
Andi Kleen41c8ca22016-05-24 12:52:38 -07001545 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001546 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001547 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001548 fprintf(output, "# time socket cpus");
1549 if (!metric_only)
1550 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001551 break;
1552 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001553 fprintf(output, "# time core cpus");
1554 if (!metric_only)
1555 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001556 break;
1557 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001558 fprintf(output, "# time CPU");
1559 if (!metric_only)
1560 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001561 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001562 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001563 fprintf(output, "# time comm-pid");
1564 if (!metric_only)
1565 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001566 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001567 case AGGR_GLOBAL:
1568 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001569 fprintf(output, "# time");
1570 if (!metric_only)
1571 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001572 case AGGR_UNSET:
1573 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001574 }
1575 }
1576
Andi Kleen41c8ca22016-05-24 12:52:38 -07001577 if (num_print_interval == 0 && metric_only)
1578 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001579 if (++num_print_interval == 25)
1580 num_print_interval = 0;
1581}
1582
1583static void print_header(int argc, const char **argv)
1584{
Jiri Olsa58215222015-07-21 14:31:24 +02001585 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001586 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001587
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001588 fflush(stdout);
1589
Stephane Eraniand7470b62010-12-01 18:49:05 +02001590 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001591 fprintf(output, "\n");
1592 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001593 if (target.system_wide)
1594 fprintf(output, "\'system wide");
1595 else if (target.cpu_list)
1596 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001597 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001598 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1599 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001600 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001601 } else if (target.pid)
1602 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001603 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001604 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001605
Stephane Eranian4aa90152011-08-15 22:22:33 +02001606 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001607 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001608 fprintf(output, " (%d runs)", run_count);
1609 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001610 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001611}
1612
1613static void print_footer(void)
1614{
Jiri Olsa58215222015-07-21 14:31:24 +02001615 FILE *output = stat_config.output;
Andi Kleen918c7b02017-05-22 18:00:16 -07001616 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001617
Jiri Olsad4f63a42015-06-26 11:29:26 +02001618 if (!null_run)
1619 fprintf(output, "\n");
1620 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001621 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001622 if (run_count > 1) {
1623 fprintf(output, " ");
1624 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1625 avg_stats(&walltime_nsecs_stats));
1626 }
1627 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001628
Andi Kleen918c7b02017-05-22 18:00:16 -07001629 if (print_free_counters_hint &&
1630 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1631 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001632 fprintf(output,
1633"Some events weren't counted. Try disabling the NMI watchdog:\n"
1634" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1635" perf stat ...\n"
1636" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001637}
1638
1639static void print_counters(struct timespec *ts, int argc, const char **argv)
1640{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001641 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001642 struct perf_evsel *counter;
1643 char buf[64], *prefix = NULL;
1644
Jiri Olsa664c98d2015-11-05 15:40:50 +01001645 /* Do not print anything if we record to the pipe. */
1646 if (STAT_RECORD && perf_stat.file.is_pipe)
1647 return;
1648
Jiri Olsad4f63a42015-06-26 11:29:26 +02001649 if (interval)
1650 print_interval(prefix = buf, ts);
1651 else
1652 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001653
Andi Kleen54b50912016-03-03 15:57:36 -08001654 if (metric_only) {
1655 static int num_print_iv;
1656
Andi Kleen41c8ca22016-05-24 12:52:38 -07001657 if (num_print_iv == 0 && !interval)
1658 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001659 if (num_print_iv++ == 25)
1660 num_print_iv = 0;
1661 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1662 fprintf(stat_config.output, "%s", prefix);
1663 }
1664
Jiri Olsa421a50f2015-07-21 14:31:22 +02001665 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001666 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001667 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001668 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001669 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001670 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001671 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001672 print_aggr_thread(counter, prefix);
1673 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001674 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001675 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001676 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001677 if (metric_only)
1678 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001679 break;
1680 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001681 if (metric_only)
1682 print_no_aggr_metric(prefix);
1683 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001684 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001685 print_counter(counter, prefix);
1686 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001687 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001688 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001689 default:
1690 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001691 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001692
Jiri Olsad4f63a42015-06-26 11:29:26 +02001693 if (!interval && !csv_output)
1694 print_footer();
1695
Jiri Olsa58215222015-07-21 14:31:24 +02001696 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001697}
1698
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001699static volatile int signr = -1;
1700
Ingo Molnar52425192009-05-26 09:17:18 +02001701static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001702{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001703 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001704 done = 1;
1705
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001706 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001707 /*
1708 * render child_pid harmless
1709 * won't send SIGTERM to a random
1710 * process in case of race condition
1711 * and fast PID recycling
1712 */
1713 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001714}
1715
1716static void sig_atexit(void)
1717{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001718 sigset_t set, oset;
1719
1720 /*
1721 * avoid race condition with SIGCHLD handler
1722 * in skip_signal() which is modifying child_pid
1723 * goal is to avoid send SIGTERM to a random
1724 * process
1725 */
1726 sigemptyset(&set);
1727 sigaddset(&set, SIGCHLD);
1728 sigprocmask(SIG_BLOCK, &set, &oset);
1729
Chris Wilson933da832009-10-04 01:35:01 +01001730 if (child_pid != -1)
1731 kill(child_pid, SIGTERM);
1732
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001733 sigprocmask(SIG_SETMASK, &oset, NULL);
1734
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001735 if (signr == -1)
1736 return;
1737
1738 signal(signr, SIG_DFL);
1739 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001740}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001741
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001742static int stat__set_big_num(const struct option *opt __maybe_unused,
1743 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001744{
1745 big_num_opt = unset ? 0 : 1;
1746 return 0;
1747}
1748
Andi Kleen44b1e602016-05-30 12:49:42 -03001749static int enable_metric_only(const struct option *opt __maybe_unused,
1750 const char *s __maybe_unused, int unset)
1751{
1752 force_metric_only = true;
1753 metric_only = !unset;
1754 return 0;
1755}
1756
Jiri Olsae0547312015-11-05 15:40:45 +01001757static const struct option stat_options[] = {
1758 OPT_BOOLEAN('T', "transaction", &transaction_run,
1759 "hardware transaction statistics"),
1760 OPT_CALLBACK('e', "event", &evsel_list, "event",
1761 "event selector. use 'perf list' to list available events",
1762 parse_events_option),
1763 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1764 "event filter", parse_filter),
1765 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1766 "child tasks do not inherit counters"),
1767 OPT_STRING('p', "pid", &target.pid, "pid",
1768 "stat events on existing process id"),
1769 OPT_STRING('t', "tid", &target.tid, "tid",
1770 "stat events on existing thread id"),
1771 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1772 "system-wide collection from all CPUs"),
1773 OPT_BOOLEAN('g', "group", &group,
1774 "put the counters into a counter group"),
1775 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1776 OPT_INCR('v', "verbose", &verbose,
1777 "be more verbose (show counter open errors, etc)"),
1778 OPT_INTEGER('r', "repeat", &run_count,
1779 "repeat command and print average + stddev (max: 100, forever: 0)"),
1780 OPT_BOOLEAN('n', "null", &null_run,
1781 "null run - dont start any counters"),
1782 OPT_INCR('d', "detailed", &detailed_run,
1783 "detailed run - start a lot of events"),
1784 OPT_BOOLEAN('S', "sync", &sync_run,
1785 "call sync() before starting a run"),
1786 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1787 "print large numbers with thousands\' separators",
1788 stat__set_big_num),
1789 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1790 "list of cpus to monitor in system-wide"),
1791 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1792 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001793 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001794 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1795 "print counts with custom separator"),
1796 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1797 "monitor event in cgroup name only", parse_cgroups),
1798 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1799 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1800 OPT_INTEGER(0, "log-fd", &output_fd,
1801 "log output to fd, instead of stderr"),
1802 OPT_STRING(0, "pre", &pre_cmd, "command",
1803 "command to run prior to the measured command"),
1804 OPT_STRING(0, "post", &post_cmd, "command",
1805 "command to run after to the measured command"),
1806 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1807 "print counts at regular interval in ms (>= 10)"),
1808 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1809 "aggregate counts per processor socket", AGGR_SOCKET),
1810 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1811 "aggregate counts per physical processor core", AGGR_CORE),
1812 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1813 "aggregate counts per thread", AGGR_THREAD),
1814 OPT_UINTEGER('D', "delay", &initial_delay,
1815 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001816 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1817 "Only print computed metrics. No raw values", enable_metric_only),
1818 OPT_BOOLEAN(0, "topdown", &topdown_run,
1819 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001820 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1821 "measure SMI cost"),
Jiri Olsae0547312015-11-05 15:40:45 +01001822 OPT_END()
1823};
1824
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001825static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1826{
1827 return cpu_map__get_socket(map, cpu, NULL);
1828}
1829
1830static int perf_stat__get_core(struct cpu_map *map, int cpu)
1831{
1832 return cpu_map__get_core(map, cpu, NULL);
1833}
1834
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001835static int cpu_map__get_max(struct cpu_map *map)
1836{
1837 int i, max = -1;
1838
1839 for (i = 0; i < map->nr; i++) {
1840 if (map->map[i] > max)
1841 max = map->map[i];
1842 }
1843
1844 return max;
1845}
1846
1847static struct cpu_map *cpus_aggr_map;
1848
1849static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1850{
1851 int cpu;
1852
1853 if (idx >= map->nr)
1854 return -1;
1855
1856 cpu = map->map[idx];
1857
1858 if (cpus_aggr_map->map[cpu] == -1)
1859 cpus_aggr_map->map[cpu] = get_id(map, idx);
1860
1861 return cpus_aggr_map->map[cpu];
1862}
1863
1864static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1865{
1866 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1867}
1868
1869static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1870{
1871 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1872}
1873
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001874static int perf_stat_init_aggr_mode(void)
1875{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001876 int nr;
1877
Jiri Olsa421a50f2015-07-21 14:31:22 +02001878 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001879 case AGGR_SOCKET:
1880 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1881 perror("cannot build socket map");
1882 return -1;
1883 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001884 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001885 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001886 case AGGR_CORE:
1887 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1888 perror("cannot build core map");
1889 return -1;
1890 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001891 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001892 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001893 case AGGR_NONE:
1894 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001895 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001896 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001897 default:
1898 break;
1899 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001900
1901 /*
1902 * The evsel_list->cpus is the base we operate on,
1903 * taking the highest cpu number to be the size of
1904 * the aggregation translate cpumap.
1905 */
1906 nr = cpu_map__get_max(evsel_list->cpus);
1907 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1908 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001909}
1910
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001911static void perf_stat__exit_aggr_mode(void)
1912{
1913 cpu_map__put(aggr_map);
1914 cpu_map__put(cpus_aggr_map);
1915 aggr_map = NULL;
1916 cpus_aggr_map = NULL;
1917}
1918
Jiri Olsa68d702f2015-11-05 15:40:58 +01001919static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1920{
1921 int cpu;
1922
1923 if (idx > map->nr)
1924 return -1;
1925
1926 cpu = map->map[idx];
1927
Jan Stancekda8a58b2017-02-17 12:10:26 +01001928 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001929 return -1;
1930
1931 return cpu;
1932}
1933
1934static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1935{
1936 struct perf_env *env = data;
1937 int cpu = perf_env__get_cpu(env, map, idx);
1938
1939 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1940}
1941
1942static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1943{
1944 struct perf_env *env = data;
1945 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1946
1947 if (cpu != -1) {
1948 int socket_id = env->cpu[cpu].socket_id;
1949
1950 /*
1951 * Encode socket in upper 16 bits
1952 * core_id is relative to socket, and
1953 * we need a global id. So we combine
1954 * socket + core id.
1955 */
1956 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1957 }
1958
1959 return core;
1960}
1961
1962static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1963 struct cpu_map **sockp)
1964{
1965 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1966}
1967
1968static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1969 struct cpu_map **corep)
1970{
1971 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1972}
1973
1974static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1975{
1976 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1977}
1978
1979static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1980{
1981 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1982}
1983
1984static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1985{
1986 struct perf_env *env = &st->session->header.env;
1987
1988 switch (stat_config.aggr_mode) {
1989 case AGGR_SOCKET:
1990 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1991 perror("cannot build socket map");
1992 return -1;
1993 }
1994 aggr_get_id = perf_stat__get_socket_file;
1995 break;
1996 case AGGR_CORE:
1997 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1998 perror("cannot build core map");
1999 return -1;
2000 }
2001 aggr_get_id = perf_stat__get_core_file;
2002 break;
2003 case AGGR_NONE:
2004 case AGGR_GLOBAL:
2005 case AGGR_THREAD:
2006 case AGGR_UNSET:
2007 default:
2008 break;
2009 }
2010
2011 return 0;
2012}
2013
Andi Kleen44b1e602016-05-30 12:49:42 -03002014static int topdown_filter_events(const char **attr, char **str, bool use_group)
2015{
2016 int off = 0;
2017 int i;
2018 int len = 0;
2019 char *s;
2020
2021 for (i = 0; attr[i]; i++) {
2022 if (pmu_have_event("cpu", attr[i])) {
2023 len += strlen(attr[i]) + 1;
2024 attr[i - off] = attr[i];
2025 } else
2026 off++;
2027 }
2028 attr[i - off] = NULL;
2029
2030 *str = malloc(len + 1 + 2);
2031 if (!*str)
2032 return -1;
2033 s = *str;
2034 if (i - off == 0) {
2035 *s = 0;
2036 return 0;
2037 }
2038 if (use_group)
2039 *s++ = '{';
2040 for (i = 0; attr[i]; i++) {
2041 strcpy(s, attr[i]);
2042 s += strlen(s);
2043 *s++ = ',';
2044 }
2045 if (use_group) {
2046 s[-1] = '}';
2047 *s = 0;
2048 } else
2049 s[-1] = 0;
2050 return 0;
2051}
2052
2053__weak bool arch_topdown_check_group(bool *warn)
2054{
2055 *warn = false;
2056 return false;
2057}
2058
2059__weak void arch_topdown_group_warn(void)
2060{
2061}
2062
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002063/*
2064 * Add default attributes, if there were no attributes specified or
2065 * if -d/--detailed, -d -d or -d -d -d is used:
2066 */
2067static int add_default_attributes(void)
2068{
Andi Kleen44b1e602016-05-30 12:49:42 -03002069 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002070 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002071
2072 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2073 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2074 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2075 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2076
2077 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002078};
2079 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002080 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002081};
2082 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002083 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002084};
2085 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002086 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2087 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2088 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2089
2090};
2091
2092/*
2093 * Detailed stats (-d), covering the L1 and last level data caches:
2094 */
2095 struct perf_event_attr detailed_attrs[] = {
2096
2097 { .type = PERF_TYPE_HW_CACHE,
2098 .config =
2099 PERF_COUNT_HW_CACHE_L1D << 0 |
2100 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2101 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2102
2103 { .type = PERF_TYPE_HW_CACHE,
2104 .config =
2105 PERF_COUNT_HW_CACHE_L1D << 0 |
2106 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2107 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2108
2109 { .type = PERF_TYPE_HW_CACHE,
2110 .config =
2111 PERF_COUNT_HW_CACHE_LL << 0 |
2112 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2113 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2114
2115 { .type = PERF_TYPE_HW_CACHE,
2116 .config =
2117 PERF_COUNT_HW_CACHE_LL << 0 |
2118 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2119 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2120};
2121
2122/*
2123 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2124 */
2125 struct perf_event_attr very_detailed_attrs[] = {
2126
2127 { .type = PERF_TYPE_HW_CACHE,
2128 .config =
2129 PERF_COUNT_HW_CACHE_L1I << 0 |
2130 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2132
2133 { .type = PERF_TYPE_HW_CACHE,
2134 .config =
2135 PERF_COUNT_HW_CACHE_L1I << 0 |
2136 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2137 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2138
2139 { .type = PERF_TYPE_HW_CACHE,
2140 .config =
2141 PERF_COUNT_HW_CACHE_DTLB << 0 |
2142 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2143 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2144
2145 { .type = PERF_TYPE_HW_CACHE,
2146 .config =
2147 PERF_COUNT_HW_CACHE_DTLB << 0 |
2148 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2149 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2150
2151 { .type = PERF_TYPE_HW_CACHE,
2152 .config =
2153 PERF_COUNT_HW_CACHE_ITLB << 0 |
2154 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2155 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2156
2157 { .type = PERF_TYPE_HW_CACHE,
2158 .config =
2159 PERF_COUNT_HW_CACHE_ITLB << 0 |
2160 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2161 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2162
2163};
2164
2165/*
2166 * Very, very detailed stats (-d -d -d), adding prefetch events:
2167 */
2168 struct perf_event_attr very_very_detailed_attrs[] = {
2169
2170 { .type = PERF_TYPE_HW_CACHE,
2171 .config =
2172 PERF_COUNT_HW_CACHE_L1D << 0 |
2173 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2174 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2175
2176 { .type = PERF_TYPE_HW_CACHE,
2177 .config =
2178 PERF_COUNT_HW_CACHE_L1D << 0 |
2179 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2180 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2181};
2182
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002183 /* Set attrs if no event is selected and !null_run: */
2184 if (null_run)
2185 return 0;
2186
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002187 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002188 if (pmu_have_event("cpu", "cycles-ct") &&
2189 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002190 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002191 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002192 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2193 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002194 fprintf(stderr, "Cannot set up transaction events\n");
2195 return -1;
2196 }
2197 return 0;
2198 }
2199
Kan Liangdaefd0b2017-05-26 12:05:38 -07002200 if (smi_cost) {
2201 int smi;
2202
2203 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2204 fprintf(stderr, "freeze_on_smi is not supported.\n");
2205 return -1;
2206 }
2207
2208 if (!smi) {
2209 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2210 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2211 return -1;
2212 }
2213 smi_reset = true;
2214 }
2215
2216 if (pmu_have_event("msr", "aperf") &&
2217 pmu_have_event("msr", "smi")) {
2218 if (!force_metric_only)
2219 metric_only = true;
2220 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2221 } else {
2222 fprintf(stderr, "To measure SMI cost, it needs "
2223 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2224 return -1;
2225 }
2226 if (err) {
2227 fprintf(stderr, "Cannot set up SMI cost events\n");
2228 return -1;
2229 }
2230 return 0;
2231 }
2232
Andi Kleen44b1e602016-05-30 12:49:42 -03002233 if (topdown_run) {
2234 char *str = NULL;
2235 bool warn = false;
2236
2237 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2238 stat_config.aggr_mode != AGGR_CORE) {
2239 pr_err("top down event configuration requires --per-core mode\n");
2240 return -1;
2241 }
2242 stat_config.aggr_mode = AGGR_CORE;
2243 if (nr_cgroups || !target__has_cpu(&target)) {
2244 pr_err("top down event configuration requires system-wide mode (-a)\n");
2245 return -1;
2246 }
2247
2248 if (!force_metric_only)
2249 metric_only = true;
2250 if (topdown_filter_events(topdown_attrs, &str,
2251 arch_topdown_check_group(&warn)) < 0) {
2252 pr_err("Out of memory\n");
2253 return -1;
2254 }
2255 if (topdown_attrs[0] && str) {
2256 if (warn)
2257 arch_topdown_group_warn();
2258 err = parse_events(evsel_list, str, NULL);
2259 if (err) {
2260 fprintf(stderr,
2261 "Cannot set up top down events %s: %d\n",
2262 str, err);
2263 free(str);
2264 return -1;
2265 }
2266 } else {
2267 fprintf(stderr, "System does not support topdown\n");
2268 return -1;
2269 }
2270 free(str);
2271 }
2272
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002273 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002274 if (target__has_cpu(&target))
2275 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2276
Andi Kleen9dec4472016-02-26 16:27:56 -08002277 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2278 return -1;
2279 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2280 if (perf_evlist__add_default_attrs(evsel_list,
2281 frontend_attrs) < 0)
2282 return -1;
2283 }
2284 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2285 if (perf_evlist__add_default_attrs(evsel_list,
2286 backend_attrs) < 0)
2287 return -1;
2288 }
2289 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002290 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002291 }
2292
2293 /* Detailed events get appended to the event list: */
2294
2295 if (detailed_run < 1)
2296 return 0;
2297
2298 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002299 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002300 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002301
2302 if (detailed_run < 2)
2303 return 0;
2304
2305 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002306 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002307 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002308
2309 if (detailed_run < 3)
2310 return 0;
2311
2312 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002313 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002314}
2315
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002316static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002317 "perf stat record [<options>]",
2318 NULL,
2319};
2320
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002321static void init_features(struct perf_session *session)
2322{
2323 int feat;
2324
2325 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2326 perf_header__set_feat(&session->header, feat);
2327
2328 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2329 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2330 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2331 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2332}
2333
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002334static int __cmd_record(int argc, const char **argv)
2335{
2336 struct perf_session *session;
2337 struct perf_data_file *file = &perf_stat.file;
2338
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002339 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002340 PARSE_OPT_STOP_AT_NON_OPTION);
2341
2342 if (output_name)
2343 file->path = output_name;
2344
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002345 if (run_count != 1 || forever) {
2346 pr_err("Cannot use -r option with perf stat record.\n");
2347 return -1;
2348 }
2349
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002350 session = perf_session__new(file, false, NULL);
2351 if (session == NULL) {
2352 pr_err("Perf session creation failed.\n");
2353 return -1;
2354 }
2355
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002356 init_features(session);
2357
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002358 session->evlist = evsel_list;
2359 perf_stat.session = session;
2360 perf_stat.record = true;
2361 return argc;
2362}
2363
Jiri Olsaa56f9392015-11-05 15:40:59 +01002364static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2365 union perf_event *event,
2366 struct perf_session *session)
2367{
Andi Kleene3b03b62016-05-05 16:04:03 -07002368 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002369 struct perf_evsel *counter;
2370 struct timespec tsh, *ts = NULL;
2371 const char **argv = session->header.env.cmdline_argv;
2372 int argc = session->header.env.nr_cmdline;
2373
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002374 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002375 perf_stat_process_counter(&stat_config, counter);
2376
Andi Kleene3b03b62016-05-05 16:04:03 -07002377 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2378 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002379
Andi Kleene3b03b62016-05-05 16:04:03 -07002380 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002381 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2382 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002383 ts = &tsh;
2384 }
2385
2386 print_counters(ts, argc, argv);
2387 return 0;
2388}
2389
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002390static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002391int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002392 union perf_event *event,
2393 struct perf_session *session __maybe_unused)
2394{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002395 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2396
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002397 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002398
Jiri Olsa89af4e02015-11-05 15:41:02 +01002399 if (cpu_map__empty(st->cpus)) {
2400 if (st->aggr_mode != AGGR_UNSET)
2401 pr_warning("warning: processing task data, aggregation mode not set\n");
2402 return 0;
2403 }
2404
2405 if (st->aggr_mode != AGGR_UNSET)
2406 stat_config.aggr_mode = st->aggr_mode;
2407
Jiri Olsa68d702f2015-11-05 15:40:58 +01002408 if (perf_stat.file.is_pipe)
2409 perf_stat_init_aggr_mode();
2410 else
2411 perf_stat_init_aggr_mode_file(st);
2412
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002413 return 0;
2414}
2415
Jiri Olsa1975d362015-11-05 15:40:56 +01002416static int set_maps(struct perf_stat *st)
2417{
2418 if (!st->cpus || !st->threads)
2419 return 0;
2420
2421 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2422 return -EINVAL;
2423
2424 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2425
2426 if (perf_evlist__alloc_stats(evsel_list, true))
2427 return -ENOMEM;
2428
2429 st->maps_allocated = true;
2430 return 0;
2431}
2432
2433static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002434int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002435 union perf_event *event,
2436 struct perf_session *session __maybe_unused)
2437{
2438 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2439
2440 if (st->threads) {
2441 pr_warning("Extra thread map event, ignoring.\n");
2442 return 0;
2443 }
2444
2445 st->threads = thread_map__new_event(&event->thread_map);
2446 if (!st->threads)
2447 return -ENOMEM;
2448
2449 return set_maps(st);
2450}
2451
2452static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002453int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002454 union perf_event *event,
2455 struct perf_session *session __maybe_unused)
2456{
2457 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2458 struct cpu_map *cpus;
2459
2460 if (st->cpus) {
2461 pr_warning("Extra cpu map event, ignoring.\n");
2462 return 0;
2463 }
2464
2465 cpus = cpu_map__new_data(&event->cpu_map.data);
2466 if (!cpus)
2467 return -ENOMEM;
2468
2469 st->cpus = cpus;
2470 return set_maps(st);
2471}
2472
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002473static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002474 "perf stat report [<options>]",
2475 NULL,
2476};
2477
2478static struct perf_stat perf_stat = {
2479 .tool = {
2480 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002481 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002482 .thread_map = process_thread_map_event,
2483 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002484 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002485 .stat = perf_event__process_stat_event,
2486 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002487 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002488 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002489};
2490
2491static int __cmd_report(int argc, const char **argv)
2492{
2493 struct perf_session *session;
2494 const struct option options[] = {
2495 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002496 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2497 "aggregate counts per processor socket", AGGR_SOCKET),
2498 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2499 "aggregate counts per physical processor core", AGGR_CORE),
2500 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2501 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002502 OPT_END()
2503 };
2504 struct stat st;
2505 int ret;
2506
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002507 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002508
2509 if (!input_name || !strlen(input_name)) {
2510 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2511 input_name = "-";
2512 else
2513 input_name = "perf.data";
2514 }
2515
2516 perf_stat.file.path = input_name;
2517 perf_stat.file.mode = PERF_DATA_MODE_READ;
2518
2519 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2520 if (session == NULL)
2521 return -1;
2522
2523 perf_stat.session = session;
2524 stat_config.output = stderr;
2525 evsel_list = session->evlist;
2526
2527 ret = perf_session__process_events(session);
2528 if (ret)
2529 return ret;
2530
2531 perf_session__delete(session);
2532 return 0;
2533}
2534
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002535static void setup_system_wide(int forks)
2536{
2537 /*
2538 * Make system wide (-a) the default target if
2539 * no target was specified and one of following
2540 * conditions is met:
2541 *
2542 * - there's no workload specified
2543 * - there is workload specified but all requested
2544 * events are system wide events
2545 */
2546 if (!target__none(&target))
2547 return;
2548
2549 if (!forks)
2550 target.system_wide = true;
2551 else {
2552 struct perf_evsel *counter;
2553
2554 evlist__for_each_entry(evsel_list, counter) {
2555 if (!counter->system_wide)
2556 return;
2557 }
2558
2559 if (evsel_list->nr_entries)
2560 target.system_wide = true;
2561 }
2562}
2563
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002564int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002565{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002566 const char * const stat_usage[] = {
2567 "perf stat [<options>] [<command>]",
2568 NULL
2569 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002570 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002571 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002572 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002573 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002574 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002575
Stephane Eranian5af52b52010-05-18 15:00:01 +02002576 setlocale(LC_ALL, "");
2577
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002578 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002579 if (evsel_list == NULL)
2580 return -ENOMEM;
2581
Wang Nan1669e502016-02-19 11:43:58 +00002582 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002583 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2584 (const char **) stat_usage,
2585 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002586 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002587 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002588
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002589 if (csv_sep) {
2590 csv_output = true;
2591 if (!strcmp(csv_sep, "\\t"))
2592 csv_sep = "\t";
2593 } else
2594 csv_sep = DEFAULT_SEPARATOR;
2595
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002596 if (argc && !strncmp(argv[0], "rec", 3)) {
2597 argc = __cmd_record(argc, argv);
2598 if (argc < 0)
2599 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002600 } else if (argc && !strncmp(argv[0], "rep", 3))
2601 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002602
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002603 interval = stat_config.interval;
2604
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002605 /*
2606 * For record command the -o is already taken care of.
2607 */
2608 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002609 output = NULL;
2610
Jim Cromie56f3bae2011-09-07 17:14:00 -06002611 if (output_name && output_fd) {
2612 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002613 parse_options_usage(stat_usage, stat_options, "o", 1);
2614 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002615 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002616 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002617
Andi Kleen54b50912016-03-03 15:57:36 -08002618 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2619 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2620 goto out;
2621 }
2622
Andi Kleen54b50912016-03-03 15:57:36 -08002623 if (metric_only && run_count > 1) {
2624 fprintf(stderr, "--metric-only is not supported with -r\n");
2625 goto out;
2626 }
2627
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002628 if (output_fd < 0) {
2629 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002630 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002631 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002632 }
2633
Stephane Eranian4aa90152011-08-15 22:22:33 +02002634 if (!output) {
2635 struct timespec tm;
2636 mode = append_file ? "a" : "w";
2637
2638 output = fopen(output_name, mode);
2639 if (!output) {
2640 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002641 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002642 }
2643 clock_gettime(CLOCK_REALTIME, &tm);
2644 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002645 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002646 mode = append_file ? "a" : "w";
2647 output = fdopen(output_fd, mode);
2648 if (!output) {
2649 perror("Failed opening logfd");
2650 return -errno;
2651 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002652 }
2653
Jiri Olsa58215222015-07-21 14:31:24 +02002654 stat_config.output = output;
2655
Stephane Eraniand7470b62010-12-01 18:49:05 +02002656 /*
2657 * let the spreadsheet do the pretty-printing
2658 */
2659 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002660 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002661 if (big_num_opt == 1) {
2662 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002663 parse_options_usage(stat_usage, stat_options, "B", 1);
2664 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002665 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002666 } else /* Nope, so disable big number formatting */
2667 big_num = false;
2668 } else if (big_num_opt == 0) /* User passed --no-big-num */
2669 big_num = false;
2670
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002671 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002672
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002673 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002674 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002675 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002676 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002677 } else if (run_count == 0) {
2678 forever = true;
2679 run_count = 1;
2680 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002681
Jiri Olsa421a50f2015-07-21 14:31:22 +02002682 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002683 fprintf(stderr, "The --per-thread option is only available "
2684 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002685 parse_options_usage(NULL, stat_options, "p", 1);
2686 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002687 goto out;
2688 }
2689
2690 /*
2691 * no_aggr, cgroup are for system-wide only
2692 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2693 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002694 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2695 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002696 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002697 fprintf(stderr, "both cgroup and no-aggregation "
2698 "modes only available in system-wide mode\n");
2699
Jiri Olsae0547312015-11-05 15:40:45 +01002700 parse_options_usage(stat_usage, stat_options, "G", 1);
2701 parse_options_usage(NULL, stat_options, "A", 1);
2702 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002703 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002704 }
2705
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002706 if (add_default_attributes())
2707 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002708
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002709 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002710
Namhyung Kim77a6f012012-05-07 14:09:04 +09002711 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002712 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002713 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002714 parse_options_usage(stat_usage, stat_options, "p", 1);
2715 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002716 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002717 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002718 parse_options_usage(stat_usage, stat_options, "C", 1);
2719 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002720 }
2721 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002722 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002723
2724 /*
2725 * Initialize thread_map with comm names,
2726 * so we could print it out on output.
2727 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002728 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002729 thread_map__read_comms(evsel_list->threads);
2730
Stephane Eranian13370a92013-01-29 12:47:44 +01002731 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002732 if (interval < 10) {
2733 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002734 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002735 goto out;
2736 } else
2737 pr_warning("print interval < 100ms. "
2738 "The overhead percentage could be high in some cases. "
2739 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002740 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002741
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002742 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002743 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002744
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002745 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002746 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002747
Ingo Molnar58d7e992009-05-15 11:03:23 +02002748 /*
2749 * We dont want to block the signals - that would cause
2750 * child tasks to inherit that and Ctrl-C would not work.
2751 * What we want is for Ctrl-C to work in the exec()-ed
2752 * task, but being ignored by perf stat itself:
2753 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002754 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002755 if (!forever)
2756 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002757 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002758 signal(SIGALRM, skip_signal);
2759 signal(SIGABRT, skip_signal);
2760
Ingo Molnar42202dd2009-06-13 14:57:28 +02002761 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002762 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002763 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002764 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2765 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002766
Ingo Molnar42202dd2009-06-13 14:57:28 +02002767 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002768 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002769 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002770 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002771 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002772 }
2773
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002774 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002775 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002776
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002777 if (STAT_RECORD) {
2778 /*
2779 * We synthesize the kernel mmap record just so that older tools
2780 * don't emit warnings about not being able to resolve symbols
2781 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2782 * a saner message about no samples being in the perf.data file.
2783 *
2784 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002785 * in header.c at the moment 'perf stat record' gets introduced, which
2786 * is not really needed once we start adding the stat specific PERF_RECORD_
2787 * records, but the need to suppress the kptr_restrict messages in older
2788 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002789 */
2790 int fd = perf_data_file__fd(&perf_stat.file);
2791 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2792 process_synthesized_event,
2793 &perf_stat.session->machines.host);
2794 if (err) {
2795 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2796 "older tools may produce warnings about this file\n.");
2797 }
2798
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002799 if (!interval) {
2800 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2801 pr_err("failed to write stat round event\n");
2802 }
2803
Jiri Olsa664c98d2015-11-05 15:40:50 +01002804 if (!perf_stat.file.is_pipe) {
2805 perf_stat.session->header.data_size += perf_stat.bytes_written;
2806 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2807 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002808
2809 perf_session__delete(perf_stat.session);
2810 }
2811
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002812 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002813 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002814out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07002815 if (smi_cost && smi_reset)
2816 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2817
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002818 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002819 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002820}