blob: 2f606f76b66a868b5c497b547860be309d7b0e12 [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"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030066#include "util/string2.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070067#include "util/metricgroup.h"
Jiri Olsa9660e082018-06-07 00:15:06 +020068#include "util/top.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010069#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030071#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030072#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030073#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030074#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020075#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020076#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030077#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020078#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070079#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030080#include <sys/types.h>
81#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030082#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030083#include <unistd.h>
Jiri Olsa0ce2da12018-06-05 14:13:13 +020084#include <sys/time.h>
85#include <sys/resource.h>
86#include <sys/wait.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020087
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030088#include "sane_ctype.h"
89
Stephane Eraniand7470b62010-12-01 18:49:05 +020090#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060091#define CNTR_NOT_SUPPORTED "<not supported>"
92#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070093#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020094
Jiri Olsad4f63a42015-06-26 11:29:26 +020095static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010096
Andi Kleen4cabc3d2013-08-21 16:47:26 -070097/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020098static const char *transaction_attrs = {
99 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700100 "{"
101 "instructions,"
102 "cycles,"
103 "cpu/cycles-t/,"
104 "cpu/tx-start/,"
105 "cpu/el-start/,"
106 "cpu/cycles-ct/"
107 "}"
108};
109
110/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200111static const char * transaction_limited_attrs = {
112 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700113 "{"
114 "instructions,"
115 "cycles,"
116 "cpu/cycles-t/,"
117 "cpu/tx-start/"
118 "}"
119};
120
Andi Kleen44b1e602016-05-30 12:49:42 -0300121static const char * topdown_attrs[] = {
122 "topdown-total-slots",
123 "topdown-slots-retired",
124 "topdown-recovery-bubbles",
125 "topdown-fetch-bubbles",
126 "topdown-slots-issued",
127 NULL,
128};
129
Kan Liangdaefd0b2017-05-26 12:05:38 -0700130static const char *smi_cost_attrs = {
131 "{"
132 "msr/aperf/,"
133 "msr/smi/,"
134 "cycles"
135 "}"
136};
137
Robert Richter666e6d42012-04-05 18:26:27 +0200138static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200139
Andi Kleenb18f3e32017-08-31 12:40:31 -0700140static struct rblist metric_events;
141
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300142static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900143 .uid = UINT_MAX,
144};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530145
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200146#define METRIC_ONLY_LEN 20
147
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200148static volatile pid_t child_pid = -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200149static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700150static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300151static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700152static bool smi_cost = false;
153static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200154static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200155static int big_num_opt = -1;
Lin Ming43bece72011-08-17 18:42:07 +0800156static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200157static const char *pre_cmd = NULL;
158static const char *post_cmd = NULL;
159static bool sync_run = false;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500160static bool forever = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300161static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700162static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200163static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100164static struct timespec ref_time;
Jiri Olsae0547312015-11-05 15:40:45 +0100165static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100166static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100167static const char *output_name;
168static int output_fd;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200169static u64 *walltime_run;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200170
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100171struct perf_stat {
172 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100173 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100174 struct perf_session *session;
175 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100176 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100177 bool maps_allocated;
178 struct cpu_map *cpus;
179 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100180 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100181};
182
183static struct perf_stat perf_stat;
184#define STAT_RECORD perf_stat.record
185
Liming Wang60666c62009-12-31 16:05:50 +0800186static volatile int done = 0;
187
Jiri Olsa421a50f2015-07-21 14:31:22 +0200188static struct perf_stat_config stat_config = {
Jiri Olsa26893a62018-08-30 08:32:40 +0200189 .aggr_mode = AGGR_GLOBAL,
190 .scale = true,
191 .unit_width = 4, /* strlen("unit") */
192 .run_count = 1,
193 .metric_only_len = METRIC_ONLY_LEN,
194 .walltime_nsecs_stats = &walltime_nsecs_stats,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200195};
196
Andi Kleene864c5c2017-08-31 12:40:35 -0700197static bool is_duration_time(struct perf_evsel *evsel)
198{
199 return !strcmp(evsel->name, "duration_time");
200}
201
Stephane Eranian13370a92013-01-29 12:47:44 +0100202static inline void diff_timespec(struct timespec *r, struct timespec *a,
203 struct timespec *b)
204{
205 r->tv_sec = a->tv_sec - b->tv_sec;
206 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300207 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100208 r->tv_sec--;
209 } else {
210 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
211 }
212}
213
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200214static void perf_stat__reset_stats(void)
215{
Jin Yao56739442017-12-05 22:03:07 +0800216 int i;
217
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200218 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200219 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800220
221 for (i = 0; i < stat_config.stats_num; i++)
222 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200223}
224
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100225static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100226 union perf_event *event,
227 struct perf_sample *sample __maybe_unused,
228 struct machine *machine __maybe_unused)
229{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100230 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100231 pr_err("failed to write perf data, error: %m\n");
232 return -1;
233 }
234
235 perf_stat.bytes_written += event->header.size;
236 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100237}
238
Jiri Olsa1975d362015-11-05 15:40:56 +0100239static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100240{
Jiri Olsa1975d362015-11-05 15:40:56 +0100241 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100242 process_synthesized_event,
243 NULL);
244}
245
246#define WRITE_STAT_ROUND_EVENT(time, interval) \
247 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
248
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100249#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
250
251static int
252perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
253 struct perf_counts_values *count)
254{
255 struct perf_sample_id *sid = SID(counter, cpu, thread);
256
257 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
258 process_synthesized_event, NULL);
259}
260
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200261/*
262 * Read out the results of a single counter:
263 * do not aggregate counts across CPUs in system-wide mode
264 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200265static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200266{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100267 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100268 int ncpus, cpu, thread;
269
Jin Yao1d9f8d12017-12-05 22:03:10 +0800270 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100271 ncpus = perf_evsel__nr_cpus(counter);
272 else
273 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200274
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000275 if (!counter->supported)
276 return -ENOENT;
277
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100278 if (counter->system_wide)
279 nthreads = 1;
280
281 for (thread = 0; thread < nthreads; thread++) {
282 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200283 struct perf_counts_values *count;
284
285 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200286
287 /*
288 * The leader's group read loads data into its group members
289 * (via perf_evsel__read_counter) and sets threir count->loaded.
290 */
291 if (!count->loaded &&
292 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700293 counter->counts->scaled = -1;
294 perf_counts(counter->counts, cpu, thread)->ena = 0;
295 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100296 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700297 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100298
Jiri Olsa82bf3112017-07-26 14:02:06 +0200299 count->loaded = false;
300
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100301 if (STAT_RECORD) {
302 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
303 pr_err("failed to write stat event\n");
304 return -1;
305 }
306 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700307
308 if (verbose > 1) {
309 fprintf(stat_config.output,
310 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
311 perf_evsel__name(counter),
312 cpu,
313 count->val, count->ena, count->run);
314 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100315 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200316 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200317
318 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200319}
320
Mark Rutland3df33ef2016-08-09 14:04:29 +0100321static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200322{
323 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700324 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200325
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300326 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700327 ret = read_counter(counter);
328 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700329 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200330
Stephane Eraniandb49a712017-04-12 11:23:01 -0700331 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200332 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200333 }
334}
335
Jiri Olsaba411a92015-06-26 11:29:24 +0200336static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100337{
Stephane Eranian13370a92013-01-29 12:47:44 +0100338 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100339
Mark Rutland3df33ef2016-08-09 14:04:29 +0100340 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100341
Stephane Eranian13370a92013-01-29 12:47:44 +0100342 clock_gettime(CLOCK_MONOTONIC, &ts);
343 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100344
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100345 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300346 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100347 pr_err("failed to write stat round event\n");
348 }
349
Andi Kleenb90f1332017-08-31 12:40:36 -0700350 init_stats(&walltime_nsecs_stats);
351 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200352 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100353}
354
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100355static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700356{
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200357 if (stat_config.initial_delay)
358 usleep(stat_config.initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100359
360 /*
361 * We need to enable counters only if:
362 * - we don't have tracee (attaching to task or cpu)
363 * - we have initial delay configured
364 */
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200365 if (!target__none(&target) || stat_config.initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100366 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700367}
368
Mark Rutland3df33ef2016-08-09 14:04:29 +0100369static void disable_counters(void)
370{
371 /*
372 * If we don't have tracee (attaching to task or cpu), counters may
373 * still be running. To get accurate group ratios, we must stop groups
374 * from counting before reading their constituent counters.
375 */
376 if (!target__none(&target))
377 perf_evlist__disable(evsel_list);
378}
379
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300380static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300381
382/*
383 * perf_evlist__prepare_workload will send a SIGUSR1
384 * if the fork fails, since we asked by setting its
385 * want_signal to true.
386 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300387static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
388 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300389{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300390 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300391}
392
Jiri Olsa82bf3112017-07-26 14:02:06 +0200393static bool perf_evsel__should_store_id(struct perf_evsel *counter)
394{
395 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
396}
397
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700398static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
399{
400 struct perf_evsel *c2, *leader;
401 bool is_open = true;
402
403 leader = evsel->leader;
404 pr_debug("Weak group for %s/%d failed\n",
405 leader->name, leader->nr_members);
406
407 /*
408 * for_each_group_member doesn't work here because it doesn't
409 * include the first entry.
410 */
411 evlist__for_each_entry(evsel_list, c2) {
412 if (c2 == evsel)
413 is_open = false;
414 if (c2->leader == leader) {
415 if (is_open)
416 perf_evsel__close(c2);
417 c2->leader = c2;
418 c2->nr_members = 0;
419 }
420 }
421 return leader;
422}
423
Jiri Olsae55c14a2018-04-23 11:08:21 +0200424static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200425{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200426 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100427 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100428 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300429 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200430 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100431 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100432 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100433 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200434 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300435 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100436 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600437 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200438
Stephane Eranian13370a92013-01-29 12:47:44 +0100439 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300440 ts.tv_sec = interval / USEC_PER_MSEC;
441 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100442 } else if (timeout) {
443 ts.tv_sec = timeout / USEC_PER_MSEC;
444 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100445 } else {
446 ts.tv_sec = 1;
447 ts.tv_nsec = 0;
448 }
449
Liming Wang60666c62009-12-31 16:05:50 +0800450 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100451 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300452 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900453 perror("failed to prepare workload");
454 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800455 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900456 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000457 }
458
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200459 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300460 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200461
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300462 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300463try_again:
Jiri Olsad09cefd2018-08-30 08:32:17 +0200464 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700465
466 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700467 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700468 counter->leader != counter &&
469 counter->weak_group) {
470 counter = perf_evsel__reset_weak_group(counter);
471 goto try_again;
472 }
473
David Ahern979987a2012-05-08 09:29:16 -0600474 /*
475 * PPC returns ENXIO for HW counters until 2.6.37
476 * (behavior changed with commit b0a873e).
477 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100478 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600479 errno == ENOENT || errno == EOPNOTSUPP ||
480 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900481 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600482 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300483 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600484 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400485
486 if ((counter->leader != counter) ||
487 !(counter->leader->nr_members > 1))
488 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300489 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900490 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300491 ui__warning("%s\n", msg);
492 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800493 } else if (target__has_per_thread(&target) &&
494 evsel_list->threads &&
495 evsel_list->threads->err_thread != -1) {
496 /*
497 * For global --per-thread case, skip current
498 * error thread.
499 */
500 if (!thread_map__remove(evsel_list->threads,
501 evsel_list->threads->err_thread)) {
502 evsel_list->threads->err_thread = -1;
503 goto try_again;
504 }
505 }
Ingo Molnarede70292011-04-28 08:48:42 +0200506
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300507 perf_evsel__open_strerror(counter, &target,
508 errno, msg, sizeof(msg));
509 ui__error("%s\n", msg);
510
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200511 if (child_pid != -1)
512 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600513
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200514 return -1;
515 }
David Ahern2cee77c2011-05-30 08:55:59 -0600516 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100517
518 l = strlen(counter->unit);
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200519 if (l > stat_config.unit_width)
520 stat_config.unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100521
Jiri Olsa82bf3112017-07-26 14:02:06 +0200522 if (perf_evsel__should_store_id(counter) &&
Jiri Olsa650d6222018-08-30 08:32:16 +0200523 perf_evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100524 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300525 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200526
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300527 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300528 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300529 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300530 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100531 return -1;
532 }
533
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600534 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300535 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600536 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
537 str_error_r(errno, msg, sizeof(msg)));
538 return -1;
539 }
540
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100541 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100542 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100543
Jiri Olsa664c98d2015-11-05 15:40:50 +0100544 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100545 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100546 } else {
547 err = perf_session__write_header(perf_stat.session, evsel_list,
548 fd, false);
549 }
550
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100551 if (err < 0)
552 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100553
Jiri Olsa1c21e982018-08-30 08:32:21 +0200554 err = perf_stat_synthesize_config(&stat_config, NULL, evsel_list,
Jiri Olsac2c247f2018-08-30 08:32:22 +0200555 process_synthesized_event, is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100556 if (err < 0)
557 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100558 }
559
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200560 /*
561 * Enable counters and exec the command:
562 */
563 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100564 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200565
Liming Wang60666c62009-12-31 16:05:50 +0800566 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900567 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100568 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900569
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100570 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100571 while (!waitpid(child_pid, &status, WNOHANG)) {
572 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100573 if (timeout)
574 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200575 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100576 if (interval_count && !(--times))
577 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100578 }
579 }
Jiri Olsa8897a892018-08-30 08:32:44 +0200580 wait4(child_pid, &status, 0, &stat_config.ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300581
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300582 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300583 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300584 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300585 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300586 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300587
Andi Kleen33e49ea2011-09-15 14:31:40 -0700588 if (WIFSIGNALED(status))
589 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800590 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100591 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100592 while (!done) {
593 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100594 if (timeout)
595 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100596 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200597 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100598 if (interval_count && !(--times))
599 break;
600 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100601 }
Liming Wang60666c62009-12-31 16:05:50 +0800602 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200603
Mark Rutland3df33ef2016-08-09 14:04:29 +0100604 disable_counters();
605
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200606 t1 = rdclock();
607
Jiri Olsae55c14a2018-04-23 11:08:21 +0200608 if (walltime_run_table)
609 walltime_run[run_idx] = t1 - t0;
610
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200611 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200612
Mark Rutland3df33ef2016-08-09 14:04:29 +0100613 /*
614 * Closing a group leader splits the group, and as we only disable
615 * group leaders, results in remaining events becoming enabled. To
616 * avoid arbitrary skew, we must read all counters before closing any
617 * group leaders.
618 */
619 read_counters();
620 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200621
Ingo Molnar42202dd2009-06-13 14:57:28 +0200622 return WEXITSTATUS(status);
623}
624
Jiri Olsae55c14a2018-04-23 11:08:21 +0200625static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200626{
627 int ret;
628
629 if (pre_cmd) {
630 ret = system(pre_cmd);
631 if (ret)
632 return ret;
633 }
634
635 if (sync_run)
636 sync();
637
Jiri Olsae55c14a2018-04-23 11:08:21 +0200638 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200639 if (ret)
640 return ret;
641
642 if (post_cmd) {
643 ret = system(post_cmd);
644 if (ret)
645 return ret;
646 }
647
648 return ret;
649}
650
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200651static void print_running(struct perf_stat_config *config,
652 u64 run, u64 ena)
Andi Kleend73515c2015-03-11 07:16:27 -0700653{
Jiri Olsafa7070a2018-08-30 08:32:29 +0200654 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200655 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200656 config->csv_sep,
Andi Kleend73515c2015-03-11 07:16:27 -0700657 run,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200658 config->csv_sep,
Andi Kleend73515c2015-03-11 07:16:27 -0700659 ena ? 100.0 * run / ena : 100.0);
660 } else if (run != ena) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200661 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700662 }
663}
664
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200665static void print_noise_pct(struct perf_stat_config *config,
666 double total, double avg)
Ingo Molnarf99844c2011-04-27 05:35:39 +0200667{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800668 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200669
Jiri Olsafa7070a2018-08-30 08:32:29 +0200670 if (config->csv_output)
671 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600672 else if (pct)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200673 fprintf(config->output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200674}
675
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200676static void print_noise(struct perf_stat_config *config,
677 struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200678{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200679 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200680
Jiri Olsad97ae042018-08-30 08:32:36 +0200681 if (config->run_count == 1)
Peter Zijlstra849abde2009-09-04 18:23:38 +0200682 return;
683
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300684 ps = evsel->stats;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200685 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200686}
687
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200688static void aggr_printout(struct perf_stat_config *config,
689 struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200690{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200691 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100692 case AGGR_CORE:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200693 fprintf(config->output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100694 cpu_map__id_to_socket(id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200695 config->csv_output ? 0 : -8,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100696 cpu_map__id_to_cpu(id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200697 config->csv_sep,
698 config->csv_output ? 0 : 4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100699 nr,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200700 config->csv_sep);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100701 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100702 case AGGR_SOCKET:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200703 fprintf(config->output, "S%*d%s%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200704 config->csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100705 id,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200706 config->csv_sep,
707 config->csv_output ? 0 : 4,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100708 nr,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200709 config->csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100710 break;
711 case AGGR_NONE:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200712 fprintf(config->output, "CPU%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200713 config->csv_output ? 0 : -4,
714 perf_evsel__cpus(evsel)->map[id], config->csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100715 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200716 case AGGR_THREAD:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200717 fprintf(config->output, "%*s-%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200718 config->csv_output ? 0 : 16,
Jiri Olsa32b8af82015-06-26 11:29:27 +0200719 thread_map__comm(evsel->threads, id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200720 config->csv_output ? 0 : -8,
Jiri Olsa32b8af82015-06-26 11:29:27 +0200721 thread_map__pid(evsel->threads, id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200722 config->csv_sep);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200723 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100724 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200725 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100726 default:
727 break;
728 }
729}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200730
Andi Kleen140aead2016-01-30 09:06:49 -0800731struct outstate {
732 FILE *fh;
733 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800734 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800735 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800736 int id, nr;
737 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800738};
739
740#define METRIC_LEN 35
741
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200742static void new_line_std(struct perf_stat_config *config __maybe_unused,
743 void *ctx)
Andi Kleen140aead2016-01-30 09:06:49 -0800744{
745 struct outstate *os = ctx;
746
747 os->newline = true;
748}
749
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200750static void do_new_line_std(struct perf_stat_config *config,
751 struct outstate *os)
Andi Kleen140aead2016-01-30 09:06:49 -0800752{
753 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800754 fputs(os->prefix, os->fh);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200755 aggr_printout(config, os->evsel, os->id, os->nr);
756 if (config->aggr_mode == AGGR_NONE)
Andi Kleen140aead2016-01-30 09:06:49 -0800757 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800758 fprintf(os->fh, " ");
759}
760
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200761static void print_metric_std(struct perf_stat_config *config,
762 void *ctx, const char *color, const char *fmt,
Andi Kleen140aead2016-01-30 09:06:49 -0800763 const char *unit, double val)
764{
765 struct outstate *os = ctx;
766 FILE *out = os->fh;
767 int n;
768 bool newline = os->newline;
769
770 os->newline = false;
771
772 if (unit == NULL || fmt == NULL) {
773 fprintf(out, "%-*s", METRIC_LEN, "");
774 return;
775 }
776
777 if (newline)
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200778 do_new_line_std(config, os);
Andi Kleen140aead2016-01-30 09:06:49 -0800779
780 n = fprintf(out, " # ");
781 if (color)
782 n += color_fprintf(out, color, fmt, val);
783 else
784 n += fprintf(out, fmt, val);
785 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
786}
787
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200788static void new_line_csv(struct perf_stat_config *config, void *ctx)
Andi Kleen92a61f62016-02-29 14:36:21 -0800789{
790 struct outstate *os = ctx;
791 int i;
792
793 fputc('\n', os->fh);
794 if (os->prefix)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200795 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200796 aggr_printout(config, os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800797 for (i = 0; i < os->nfields; i++)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200798 fputs(config->csv_sep, os->fh);
Andi Kleen92a61f62016-02-29 14:36:21 -0800799}
800
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200801static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
802 void *ctx,
Andi Kleen92a61f62016-02-29 14:36:21 -0800803 const char *color __maybe_unused,
804 const char *fmt, const char *unit, double val)
805{
806 struct outstate *os = ctx;
807 FILE *out = os->fh;
808 char buf[64], *vals, *ends;
809
810 if (unit == NULL || fmt == NULL) {
Jiri Olsafa7070a2018-08-30 08:32:29 +0200811 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800812 return;
813 }
814 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900815 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800816 while (isdigit(*ends) || *ends == '.')
817 ends++;
818 *ends = 0;
819 while (isspace(*unit))
820 unit++;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200821 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit);
Andi Kleen92a61f62016-02-29 14:36:21 -0800822}
823
Andi Kleen54b50912016-03-03 15:57:36 -0800824/* Filter out some columns that don't work well in metrics only mode */
825
826static bool valid_only_metric(const char *unit)
827{
828 if (!unit)
829 return false;
830 if (strstr(unit, "/sec") ||
831 strstr(unit, "hz") ||
832 strstr(unit, "Hz") ||
833 strstr(unit, "CPUs utilized"))
834 return false;
835 return true;
836}
837
838static const char *fixunit(char *buf, struct perf_evsel *evsel,
839 const char *unit)
840{
841 if (!strncmp(unit, "of all", 6)) {
842 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
843 unit);
844 return buf;
845 }
846 return unit;
847}
848
Jiri Olsaee1760e2018-08-30 08:32:37 +0200849static void print_metric_only(struct perf_stat_config *config,
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200850 void *ctx, const char *color, const char *fmt,
Andi Kleen54b50912016-03-03 15:57:36 -0800851 const char *unit, double val)
852{
853 struct outstate *os = ctx;
854 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200855 char buf[1024], str[1024];
Jiri Olsaee1760e2018-08-30 08:32:37 +0200856 unsigned mlen = config->metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800857
858 if (!valid_only_metric(unit))
859 return;
860 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800861 if (mlen < strlen(unit))
862 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200863
864 if (color)
865 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
866
867 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
868 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800869}
870
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200871static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
872 void *ctx, const char *color __maybe_unused,
Andi Kleen54b50912016-03-03 15:57:36 -0800873 const char *fmt,
874 const char *unit, double val)
875{
876 struct outstate *os = ctx;
877 FILE *out = os->fh;
878 char buf[64], *vals, *ends;
879 char tbuf[1024];
880
881 if (!valid_only_metric(unit))
882 return;
883 unit = fixunit(tbuf, os->evsel, unit);
884 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900885 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800886 while (isdigit(*ends) || *ends == '.')
887 ends++;
888 *ends = 0;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200889 fprintf(out, "%s%s", vals, config->csv_sep);
Andi Kleen54b50912016-03-03 15:57:36 -0800890}
891
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200892static void new_line_metric(struct perf_stat_config *config __maybe_unused,
893 void *ctx __maybe_unused)
Andi Kleen54b50912016-03-03 15:57:36 -0800894{
895}
896
Jiri Olsaee1760e2018-08-30 08:32:37 +0200897static void print_metric_header(struct perf_stat_config *config,
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200898 void *ctx, const char *color __maybe_unused,
Andi Kleen54b50912016-03-03 15:57:36 -0800899 const char *fmt __maybe_unused,
900 const char *unit, double val __maybe_unused)
901{
902 struct outstate *os = ctx;
903 char tbuf[1024];
904
905 if (!valid_only_metric(unit))
906 return;
907 unit = fixunit(tbuf, os->evsel, unit);
Jiri Olsafa7070a2018-08-30 08:32:29 +0200908 if (config->csv_output)
909 fprintf(os->fh, "%s%s", unit, config->csv_sep);
Andi Kleen54b50912016-03-03 15:57:36 -0800910 else
Jiri Olsaee1760e2018-08-30 08:32:37 +0200911 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800912}
913
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200914static int first_shadow_cpu(struct perf_stat_config *config,
915 struct perf_evsel *evsel, int id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800916{
917 int i;
918
Jiri Olsa6f6b6592018-08-30 08:32:45 +0200919 if (!config->aggr_get_id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800920 return 0;
921
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200922 if (config->aggr_mode == AGGR_NONE)
Andi Kleen44d49a62016-02-29 14:36:22 -0800923 return id;
924
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200925 if (config->aggr_mode == AGGR_GLOBAL)
Andi Kleen44d49a62016-02-29 14:36:22 -0800926 return 0;
927
928 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
929 int cpu2 = perf_evsel__cpus(evsel)->map[i];
930
Jiri Olsa6f6b6592018-08-30 08:32:45 +0200931 if (config->aggr_get_id(config, evsel_list->cpus, cpu2) == id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800932 return cpu2;
933 }
934 return 0;
935}
936
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200937static void abs_printout(struct perf_stat_config *config,
938 int id, int nr, struct perf_evsel *evsel, double avg)
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200939{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200940 FILE *output = config->output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200941 double sc = evsel->scale;
942 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200943
Jiri Olsafa7070a2018-08-30 08:32:29 +0200944 if (config->csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700945 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200946 } else {
947 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700948 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200949 else
Andi Kleene3b03b62016-05-05 16:04:03 -0700950 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200951 }
952
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200953 aggr_printout(config, evsel, id, nr);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200954
Jiri Olsafa7070a2018-08-30 08:32:29 +0200955 fprintf(output, fmt, avg, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200956
957 if (evsel->unit)
958 fprintf(output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200959 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200960 evsel->unit, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200961
Jiri Olsafa7070a2018-08-30 08:32:29 +0200962 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200963
964 if (evsel->cgrp)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200965 fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800966}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200967
Kan Liang30060ea2018-04-24 11:20:11 -0700968static bool is_mixed_hw_group(struct perf_evsel *counter)
969{
970 struct perf_evlist *evlist = counter->evlist;
971 u32 pmu_type = counter->attr.type;
972 struct perf_evsel *pos;
973
974 if (counter->nr_members < 2)
975 return false;
976
977 evlist__for_each_entry(evlist, pos) {
978 /* software events can be part of any hardware group */
979 if (pos->attr.type == PERF_TYPE_SOFTWARE)
980 continue;
981 if (pmu_type == PERF_TYPE_SOFTWARE) {
982 pmu_type = pos->attr.type;
983 continue;
984 }
985 if (pmu_type != pos->attr.type)
986 return true;
987 }
988
989 return false;
990}
991
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200992static void printout(struct perf_stat_config *config, int id, int nr,
993 struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +0800994 char *prefix, u64 run, u64 ena, double noise,
995 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -0800996{
Andi Kleen140aead2016-01-30 09:06:49 -0800997 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -0800998 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200999 .fh = config->output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001000 .prefix = prefix ? prefix : "",
1001 .id = id,
1002 .nr = nr,
1003 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001004 };
Andi Kleen140aead2016-01-30 09:06:49 -08001005 print_metric_t pm = print_metric_std;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001006 new_line_t nl;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001007
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001008 if (config->metric_only) {
Andi Kleen54b50912016-03-03 15:57:36 -08001009 nl = new_line_metric;
Jiri Olsafa7070a2018-08-30 08:32:29 +02001010 if (config->csv_output)
Andi Kleen54b50912016-03-03 15:57:36 -08001011 pm = print_metric_only_csv;
1012 else
1013 pm = print_metric_only;
1014 } else
1015 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001016
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001017 if (config->csv_output && !config->metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001018 static int aggr_fields[] = {
1019 [AGGR_GLOBAL] = 0,
1020 [AGGR_THREAD] = 1,
1021 [AGGR_NONE] = 1,
1022 [AGGR_SOCKET] = 2,
1023 [AGGR_CORE] = 2,
1024 };
1025
1026 pm = print_metric_csv;
1027 nl = new_line_csv;
1028 os.nfields = 3;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001029 os.nfields += aggr_fields[config->aggr_mode];
Andi Kleen92a61f62016-02-29 14:36:21 -08001030 if (counter->cgrp)
1031 os.nfields++;
1032 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001033 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001034 if (config->metric_only) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001035 pm(config, &os, NULL, "", "", 0);
Andi Kleen54b50912016-03-03 15:57:36 -08001036 return;
1037 }
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001038 aggr_printout(config, counter, id, nr);
Andi Kleencb110f42016-01-30 09:06:51 -08001039
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001040 fprintf(config->output, "%*s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001041 config->csv_output ? 0 : 18,
Andi Kleencb110f42016-01-30 09:06:51 -08001042 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001043 config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001044
Kan Liang30060ea2018-04-24 11:20:11 -07001045 if (counter->supported) {
Jiri Olsa31084122018-08-30 08:32:42 +02001046 config->print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001047 if (is_mixed_hw_group(counter))
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +02001048 config->print_mixed_hw_group_error = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001049 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001050
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001051 fprintf(config->output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001052 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001053 counter->unit, config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001054
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001055 fprintf(config->output, "%*s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001056 config->csv_output ? 0 : -25,
Andi Kleencb110f42016-01-30 09:06:51 -08001057 perf_evsel__name(counter));
1058
1059 if (counter->cgrp)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001060 fprintf(config->output, "%s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001061 config->csv_sep, counter->cgrp->name);
Andi Kleencb110f42016-01-30 09:06:51 -08001062
Jiri Olsafa7070a2018-08-30 08:32:29 +02001063 if (!config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001064 pm(config, &os, NULL, NULL, "", 0);
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001065 print_noise(config, counter, noise);
1066 print_running(config, run, ena);
Jiri Olsafa7070a2018-08-30 08:32:29 +02001067 if (config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001068 pm(config, &os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001069 return;
1070 }
1071
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001072 if (!config->metric_only)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001073 abs_printout(config, id, nr, counter, uval);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001074
Andi Kleen140aead2016-01-30 09:06:49 -08001075 out.print_metric = pm;
1076 out.new_line = nl;
1077 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001078 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001079
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001080 if (config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001081 print_noise(config, counter, noise);
1082 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001083 }
1084
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001085 perf_stat__print_shadow_stats(config, counter, uval,
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001086 first_shadow_cpu(config, counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001087 &out, &metric_events, st);
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001088 if (!config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001089 print_noise(config, counter, noise);
1090 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001091 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001092}
1093
Jiri Olsa77e0faf2018-08-30 08:32:39 +02001094static void aggr_update_shadow(struct perf_stat_config *config,
1095 struct perf_evlist *evlist)
Andi Kleen44d49a62016-02-29 14:36:22 -08001096{
1097 int cpu, s2, id, s;
1098 u64 val;
1099 struct perf_evsel *counter;
1100
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001101 for (s = 0; s < config->aggr_map->nr; s++) {
1102 id = config->aggr_map->map[s];
Jiri Olsa77e0faf2018-08-30 08:32:39 +02001103 evlist__for_each_entry(evlist, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001104 val = 0;
1105 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001106 s2 = config->aggr_get_id(config, evsel_list->cpus, cpu);
Andi Kleen44d49a62016-02-29 14:36:22 -08001107 if (s2 != id)
1108 continue;
1109 val += perf_counts(counter->counts, cpu, 0)->val;
1110 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001111 perf_stat__update_shadow_stats(counter, val,
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001112 first_shadow_cpu(config, counter, id),
Jin Yao1fcd0392017-12-05 22:03:04 +08001113 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001114 }
1115 }
1116}
1117
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001118static void uniquify_event_name(struct perf_evsel *counter)
1119{
1120 char *new_name;
1121 char *config;
1122
Kan Liang80ee8c52018-04-24 11:20:14 -07001123 if (counter->uniquified_name ||
1124 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001125 strlen(counter->pmu_name)))
1126 return;
1127
1128 config = strchr(counter->name, '/');
1129 if (config) {
1130 if (asprintf(&new_name,
1131 "%s%s", counter->pmu_name, config) > 0) {
1132 free(counter->name);
1133 counter->name = new_name;
1134 }
1135 } else {
1136 if (asprintf(&new_name,
1137 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1138 free(counter->name);
1139 counter->name = new_name;
1140 }
1141 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001142
1143 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001144}
1145
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001146static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter,
1147 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001148 bool first),
1149 void *data)
1150{
Jiri Olsa0c538a92018-08-30 08:32:35 +02001151 struct perf_evlist *evlist = counter->evlist;
Andi Kleen430daf22017-03-20 13:17:00 -07001152 struct perf_evsel *alias;
1153
Jiri Olsa0c538a92018-08-30 08:32:35 +02001154 alias = list_prepare_entry(counter, &(evlist->entries), node);
1155 list_for_each_entry_continue (alias, &evlist->entries, node) {
Andi Kleen430daf22017-03-20 13:17:00 -07001156 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1157 alias->scale != counter->scale ||
1158 alias->cgrp != counter->cgrp ||
1159 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001160 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001161 break;
1162 alias->merged_stat = true;
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001163 cb(config, alias, data, false);
Andi Kleen430daf22017-03-20 13:17:00 -07001164 }
1165}
1166
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001167static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter,
1168 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
Andi Kleen430daf22017-03-20 13:17:00 -07001169 bool first),
1170 void *data)
1171{
1172 if (counter->merged_stat)
1173 return false;
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001174 cb(config, counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001175 if (no_merge)
1176 uniquify_event_name(counter);
1177 else if (counter->auto_merge_stats)
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001178 collect_all_aliases(config, counter, cb, data);
Andi Kleen430daf22017-03-20 13:17:00 -07001179 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001180}
1181
1182struct aggr_data {
1183 u64 ena, run, val;
1184 int id;
1185 int nr;
1186 int cpu;
1187};
1188
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001189static void aggr_cb(struct perf_stat_config *config,
1190 struct perf_evsel *counter, void *data, bool first)
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001191{
1192 struct aggr_data *ad = data;
1193 int cpu, s2;
1194
1195 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1196 struct perf_counts_values *counts;
1197
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001198 s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu);
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001199 if (s2 != ad->id)
1200 continue;
1201 if (first)
1202 ad->nr++;
1203 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001204 /*
1205 * When any result is bad, make them all to give
1206 * consistent output in interval mode.
1207 */
1208 if (counts->ena == 0 || counts->run == 0 ||
1209 counter->counts->scaled == -1) {
1210 ad->ena = 0;
1211 ad->run = 0;
1212 break;
1213 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001214 ad->val += counts->val;
1215 ad->ena += counts->ena;
1216 ad->run += counts->run;
1217 }
1218}
1219
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001220static void print_aggr(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001221 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001222 char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001223{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001224 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001225 FILE *output = config->output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001226 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001227 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001228 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001229 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001230 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001231
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001232 if (!(config->aggr_map || config->aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001233 return;
1234
Jiri Olsa77e0faf2018-08-30 08:32:39 +02001235 aggr_update_shadow(config, evlist);
Andi Kleen44d49a62016-02-29 14:36:22 -08001236
Andi Kleen54b50912016-03-03 15:57:36 -08001237 /*
1238 * With metric_only everything is on a single line.
1239 * Without each counter has its own line.
1240 */
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001241 for (s = 0; s < config->aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001242 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001243 if (prefix && metric_only)
1244 fprintf(output, "%s", prefix);
1245
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001246 ad.id = id = config->aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001247 first = true;
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001248 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001249 if (is_duration_time(counter))
1250 continue;
1251
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001252 ad.val = ad.ena = ad.run = 0;
1253 ad.nr = 0;
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001254 if (!collect_data(config, counter, aggr_cb, &ad))
Andi Kleen430daf22017-03-20 13:17:00 -07001255 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001256 nr = ad.nr;
1257 ena = ad.ena;
1258 run = ad.run;
1259 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001260 if (first && metric_only) {
1261 first = false;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001262 aggr_printout(config, counter, id, nr);
Andi Kleen54b50912016-03-03 15:57:36 -08001263 }
1264 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001265 fprintf(output, "%s", prefix);
1266
Stephane Eranian410136f2013-11-12 17:58:49 +01001267 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001268 printout(config, id, nr, counter, uval, prefix,
1269 run, ena, 1.0, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001270 if (!metric_only)
1271 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001272 }
Andi Kleen54b50912016-03-03 15:57:36 -08001273 if (metric_only)
1274 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001275 }
1276}
1277
Jin Yao29734552017-12-05 22:03:11 +08001278static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001279{
Jin Yao29734552017-12-05 22:03:11 +08001280 return ((struct perf_aggr_thread_value *)b)->val -
1281 ((struct perf_aggr_thread_value *)a)->val;
1282}
1283
1284static struct perf_aggr_thread_value *sort_aggr_thread(
1285 struct perf_evsel *counter,
1286 int nthreads, int ncpus,
1287 int *ret)
1288{
1289 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001290 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001291 struct perf_aggr_thread_value *buf;
1292
1293 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1294 if (!buf)
1295 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001296
1297 for (thread = 0; thread < nthreads; thread++) {
1298 u64 ena = 0, run = 0, val = 0;
1299
1300 for (cpu = 0; cpu < ncpus; cpu++) {
1301 val += perf_counts(counter->counts, cpu, thread)->val;
1302 ena += perf_counts(counter->counts, cpu, thread)->ena;
1303 run += perf_counts(counter->counts, cpu, thread)->run;
1304 }
1305
Jin Yao29734552017-12-05 22:03:11 +08001306 uval = val * counter->scale;
1307
1308 /*
1309 * Skip value 0 when enabling --per-thread globally,
1310 * otherwise too many 0 output.
1311 */
1312 if (uval == 0.0 && target__has_per_thread(&target))
1313 continue;
1314
1315 buf[i].counter = counter;
1316 buf[i].id = thread;
1317 buf[i].uval = uval;
1318 buf[i].val = val;
1319 buf[i].run = run;
1320 buf[i].ena = ena;
1321 i++;
1322 }
1323
1324 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1325
1326 if (ret)
1327 *ret = i;
1328
1329 return buf;
1330}
1331
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001332static void print_aggr_thread(struct perf_stat_config *config,
1333 struct perf_evsel *counter, char *prefix)
Jin Yao29734552017-12-05 22:03:11 +08001334{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001335 FILE *output = config->output;
Jin Yao29734552017-12-05 22:03:11 +08001336 int nthreads = thread_map__nr(counter->threads);
1337 int ncpus = cpu_map__nr(counter->cpus);
1338 int thread, sorted_threads, id;
1339 struct perf_aggr_thread_value *buf;
1340
1341 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1342 if (!buf) {
1343 perror("cannot sort aggr thread");
1344 return;
1345 }
1346
1347 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001348 if (prefix)
1349 fprintf(output, "%s", prefix);
1350
Jin Yao29734552017-12-05 22:03:11 +08001351 id = buf[thread].id;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001352 if (config->stats)
1353 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001354 prefix, buf[thread].run, buf[thread].ena, 1.0,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001355 &config->stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001356 else
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001357 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001358 prefix, buf[thread].run, buf[thread].ena, 1.0,
1359 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001360 fputc('\n', output);
1361 }
Jin Yao29734552017-12-05 22:03:11 +08001362
1363 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001364}
1365
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001366struct caggr_data {
1367 double avg, avg_enabled, avg_running;
1368};
1369
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001370static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
1371 struct perf_evsel *counter, void *data,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001372 bool first __maybe_unused)
1373{
1374 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001375 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001376
1377 cd->avg += avg_stats(&ps->res_stats[0]);
1378 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1379 cd->avg_running += avg_stats(&ps->res_stats[2]);
1380}
1381
Ingo Molnar42202dd2009-06-13 14:57:28 +02001382/*
1383 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001384 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001385 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001386static void print_counter_aggr(struct perf_stat_config *config,
1387 struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001388{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001389 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001390 FILE *output = config->output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001391 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001392 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001393
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001394 if (!collect_data(config, counter, counter_aggr_cb, &cd))
Andi Kleen430daf22017-03-20 13:17:00 -07001395 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001396
Andi Kleen54b50912016-03-03 15:57:36 -08001397 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001398 fprintf(output, "%s", prefix);
1399
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001400 uval = cd.avg * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001401 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
Jin Yaoe0128b32017-12-05 22:03:05 +08001402 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001403 if (!metric_only)
1404 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001405}
1406
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001407static void counter_cb(struct perf_stat_config *config __maybe_unused,
1408 struct perf_evsel *counter, void *data,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001409 bool first __maybe_unused)
1410{
1411 struct aggr_data *ad = data;
1412
1413 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1414 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1415 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1416}
1417
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001418/*
1419 * Print out the results of a single counter:
1420 * does not use aggregated count in system-wide
1421 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001422static void print_counter(struct perf_stat_config *config,
1423 struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001424{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001425 FILE *output = config->output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001426 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001427 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001428 int cpu;
1429
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001430 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001431 struct aggr_data ad = { .cpu = cpu };
1432
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001433 if (!collect_data(config, counter, counter_cb, &ad))
Andi Kleen430daf22017-03-20 13:17:00 -07001434 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001435 val = ad.val;
1436 ena = ad.ena;
1437 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001438
1439 if (prefix)
1440 fprintf(output, "%s", prefix);
1441
Stephane Eranian410136f2013-11-12 17:58:49 +01001442 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001443 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001444 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001445
Stephane Eranian4aa90152011-08-15 22:22:33 +02001446 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001447 }
1448}
1449
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001450static void print_no_aggr_metric(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001451 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001452 char *prefix)
Andi Kleen206cab62016-03-03 15:57:37 -08001453{
1454 int cpu;
1455 int nrcpus = 0;
1456 struct perf_evsel *counter;
1457 u64 ena, run, val;
1458 double uval;
1459
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001460 nrcpus = evlist->cpus->nr;
Andi Kleen206cab62016-03-03 15:57:37 -08001461 for (cpu = 0; cpu < nrcpus; cpu++) {
1462 bool first = true;
1463
1464 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001465 fputs(prefix, config->output);
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001466 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001467 if (is_duration_time(counter))
1468 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001469 if (first) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001470 aggr_printout(config, counter, cpu, 0);
Andi Kleen206cab62016-03-03 15:57:37 -08001471 first = false;
1472 }
1473 val = perf_counts(counter->counts, cpu, 0)->val;
1474 ena = perf_counts(counter->counts, cpu, 0)->ena;
1475 run = perf_counts(counter->counts, cpu, 0)->run;
1476
1477 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001478 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001479 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001480 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001481 fputc('\n', config->output);
Andi Kleen206cab62016-03-03 15:57:37 -08001482 }
1483}
1484
Andi Kleen54b50912016-03-03 15:57:36 -08001485static int aggr_header_lens[] = {
1486 [AGGR_CORE] = 18,
1487 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001488 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001489 [AGGR_THREAD] = 24,
1490 [AGGR_GLOBAL] = 0,
1491};
1492
Andi Kleenc51fd632016-05-24 12:52:39 -07001493static const char *aggr_header_csv[] = {
1494 [AGGR_CORE] = "core,cpus,",
1495 [AGGR_SOCKET] = "socket,cpus",
1496 [AGGR_NONE] = "cpu,",
1497 [AGGR_THREAD] = "comm-pid,",
1498 [AGGR_GLOBAL] = ""
1499};
1500
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001501static void print_metric_headers(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001502 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001503 const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001504{
1505 struct perf_stat_output_ctx out;
1506 struct perf_evsel *counter;
1507 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001508 .fh = config->output
Andi Kleen54b50912016-03-03 15:57:36 -08001509 };
1510
1511 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001512 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001513
Jiri Olsafa7070a2018-08-30 08:32:29 +02001514 if (!config->csv_output && !no_indent)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001515 fprintf(config->output, "%*s",
1516 aggr_header_lens[config->aggr_mode], "");
Jiri Olsafa7070a2018-08-30 08:32:29 +02001517 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001518 if (config->interval)
1519 fputs("time,", config->output);
1520 fputs(aggr_header_csv[config->aggr_mode], config->output);
Andi Kleenc51fd632016-05-24 12:52:39 -07001521 }
Andi Kleen54b50912016-03-03 15:57:36 -08001522
1523 /* Print metrics headers only */
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001524 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001525 if (is_duration_time(counter))
1526 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001527 os.evsel = counter;
1528 out.ctx = &os;
1529 out.print_metric = print_metric_header;
1530 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001531 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001532 os.evsel = counter;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001533 perf_stat__print_shadow_stats(config, counter, 0,
Andi Kleen54b50912016-03-03 15:57:36 -08001534 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001535 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001536 &metric_events,
1537 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001538 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001539 fputc('\n', config->output);
Andi Kleen54b50912016-03-03 15:57:36 -08001540}
1541
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001542static void print_interval(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001543 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001544 char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001545{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001546 bool metric_only = config->metric_only;
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001547 unsigned int unit_width = config->unit_width;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001548 FILE *output = config->output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001549 static int num_print_interval;
1550
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001551 if (config->interval_clear)
Jiri Olsa9660e082018-06-07 00:15:06 +02001552 puts(CONSOLE_CLEAR);
1553
Jiri Olsafa7070a2018-08-30 08:32:29 +02001554 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001555
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001556 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001557 switch (config->aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001558 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001559 fprintf(output, "# time socket cpus");
1560 if (!metric_only)
1561 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001562 break;
1563 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001564 fprintf(output, "# time core cpus");
1565 if (!metric_only)
1566 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001567 break;
1568 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001569 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001570 if (!metric_only)
1571 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001572 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001573 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001574 fprintf(output, "# time comm-pid");
1575 if (!metric_only)
1576 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001577 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001578 case AGGR_GLOBAL:
1579 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001580 fprintf(output, "# time");
1581 if (!metric_only)
1582 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001583 case AGGR_UNSET:
1584 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001585 }
1586 }
1587
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001588 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001589 print_metric_headers(config, evlist, " ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001590 if (++num_print_interval == 25)
1591 num_print_interval = 0;
1592}
1593
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001594static void print_header(struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001595 struct target *_target,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001596 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001597{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001598 FILE *output = config->output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001599 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001600
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001601 fflush(stdout);
1602
Jiri Olsafa7070a2018-08-30 08:32:29 +02001603 if (!config->csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001604 fprintf(output, "\n");
1605 fprintf(output, " Performance counter stats for ");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001606 if (_target->system_wide)
David Ahern62d3b612013-09-28 14:27:58 -06001607 fprintf(output, "\'system wide");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001608 else if (_target->cpu_list)
1609 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1610 else if (!target__has_task(_target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001611 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1612 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001613 fprintf(output, " %s", argv[i]);
Jiri Olsac512e0e2018-08-30 08:32:33 +02001614 } else if (_target->pid)
1615 fprintf(output, "process id \'%s", _target->pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001616 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001617 fprintf(output, "thread id \'%s", _target->tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001618
Stephane Eranian4aa90152011-08-15 22:22:33 +02001619 fprintf(output, "\'");
Jiri Olsad97ae042018-08-30 08:32:36 +02001620 if (config->run_count > 1)
1621 fprintf(output, " (%d runs)", config->run_count);
Stephane Eranian4aa90152011-08-15 22:22:33 +02001622 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001623 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001624}
1625
Jiri Olsabc22de92018-04-23 11:08:20 +02001626static int get_precision(double num)
1627{
1628 if (num > 1)
1629 return 0;
1630
1631 return lround(ceil(-log10(num)));
1632}
1633
Jiri Olsad97ae042018-08-30 08:32:36 +02001634static void print_table(struct perf_stat_config *config,
1635 FILE *output, int precision, double avg)
Jiri Olsae55c14a2018-04-23 11:08:21 +02001636{
1637 char tmp[64];
1638 int idx, indent = 0;
1639
1640 scnprintf(tmp, 64, " %17.*f", precision, avg);
1641 while (tmp[indent] == ' ')
1642 indent++;
1643
1644 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1645
Jiri Olsad97ae042018-08-30 08:32:36 +02001646 for (idx = 0; idx < config->run_count; idx++) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02001647 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001648 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001649
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001650 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001651 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001652
1653 for (h = 0; h < n; h++)
1654 fprintf(output, "#");
1655
1656 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001657 }
1658
1659 fprintf(output, "\n%*s# Final result:\n", indent, "");
1660}
1661
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001662static double timeval2double(struct timeval *t)
1663{
1664 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1665}
1666
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001667static void print_footer(struct perf_stat_config *config)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001668{
Jiri Olsa26893a62018-08-30 08:32:40 +02001669 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001670 FILE *output = config->output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001671 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001672
Jiri Olsaaea0dca2018-08-30 08:32:41 +02001673 if (!config->null_run)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001674 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001675
Jiri Olsad97ae042018-08-30 08:32:36 +02001676 if (config->run_count == 1) {
Jiri Olsabc22de92018-04-23 11:08:20 +02001677 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001678
Jiri Olsa8897a892018-08-30 08:32:44 +02001679 if (config->ru_display) {
1680 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1681 double ru_stime = timeval2double(&config->ru_data.ru_stime);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001682
1683 fprintf(output, "\n\n");
1684 fprintf(output, " %17.9f seconds user\n", ru_utime);
1685 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1686 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001687 } else {
Jiri Olsa26893a62018-08-30 08:32:40 +02001688 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsabc22de92018-04-23 11:08:20 +02001689 /*
1690 * Display at most 2 more significant
1691 * digits than the stddev inaccuracy.
1692 */
1693 int precision = get_precision(sd) + 2;
1694
Jiri Olsae55c14a2018-04-23 11:08:21 +02001695 if (walltime_run_table)
Jiri Olsad97ae042018-08-30 08:32:36 +02001696 print_table(config, output, precision, avg);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001697
Jiri Olsabc22de92018-04-23 11:08:20 +02001698 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1699 precision, avg, precision, sd);
1700
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001701 print_noise_pct(config, sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001702 }
1703 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001704
Jiri Olsa31084122018-08-30 08:32:42 +02001705 if (config->print_free_counters_hint &&
Andi Kleen918c7b062017-05-22 18:00:16 -07001706 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1707 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001708 fprintf(output,
1709"Some events weren't counted. Try disabling the NMI watchdog:\n"
1710" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1711" perf stat ...\n"
1712" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001713
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +02001714 if (config->print_mixed_hw_group_error)
Kan Liang30060ea2018-04-24 11:20:11 -07001715 fprintf(output,
1716 "The events in group usually have to be from "
1717 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001718}
1719
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001720static void
1721perf_evlist__print_counters(struct perf_evlist *evlist,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001722 struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001723 struct target *_target,
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001724 struct timespec *ts,
1725 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001726{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001727 bool metric_only = config->metric_only;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001728 int interval = config->interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001729 struct perf_evsel *counter;
1730 char buf[64], *prefix = NULL;
1731
1732 if (interval)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001733 print_interval(config, evlist, prefix = buf, ts);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001734 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001735 print_header(config, _target, argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001736
Andi Kleen54b50912016-03-03 15:57:36 -08001737 if (metric_only) {
1738 static int num_print_iv;
1739
Andi Kleen41c8ca22016-05-24 12:52:38 -07001740 if (num_print_iv == 0 && !interval)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001741 print_metric_headers(config, evlist, prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001742 if (num_print_iv++ == 25)
1743 num_print_iv = 0;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001744 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1745 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001746 }
1747
Jiri Olsab64df7f2018-08-30 08:32:26 +02001748 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001749 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001750 case AGGR_SOCKET:
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001751 print_aggr(config, evlist, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001752 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001753 case AGGR_THREAD:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001754 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001755 if (is_duration_time(counter))
1756 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001757 print_aggr_thread(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001758 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001759 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001760 case AGGR_GLOBAL:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001761 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001762 if (is_duration_time(counter))
1763 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001764 print_counter_aggr(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001765 }
Andi Kleen54b50912016-03-03 15:57:36 -08001766 if (metric_only)
Jiri Olsab64df7f2018-08-30 08:32:26 +02001767 fputc('\n', config->output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001768 break;
1769 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001770 if (metric_only)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001771 print_no_aggr_metric(config, evlist, prefix);
Andi Kleen206cab62016-03-03 15:57:37 -08001772 else {
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001773 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001774 if (is_duration_time(counter))
1775 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001776 print_counter(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001777 }
Andi Kleen206cab62016-03-03 15:57:37 -08001778 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001779 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001780 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001781 default:
1782 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001783 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001784
Jiri Olsafa7070a2018-08-30 08:32:29 +02001785 if (!interval && !config->csv_output)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001786 print_footer(config);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001787
Jiri Olsab64df7f2018-08-30 08:32:26 +02001788 fflush(config->output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001789}
1790
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001791static void print_counters(struct timespec *ts, int argc, const char **argv)
1792{
Jiri Olsa01748202018-08-30 08:32:25 +02001793 /* Do not print anything if we record to the pipe. */
1794 if (STAT_RECORD && perf_stat.data.is_pipe)
1795 return;
1796
Jiri Olsac512e0e2018-08-30 08:32:33 +02001797 perf_evlist__print_counters(evsel_list, &stat_config, &target,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001798 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001799}
1800
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001801static volatile int signr = -1;
1802
Ingo Molnar52425192009-05-26 09:17:18 +02001803static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001804{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001805 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001806 done = 1;
1807
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001808 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001809 /*
1810 * render child_pid harmless
1811 * won't send SIGTERM to a random
1812 * process in case of race condition
1813 * and fast PID recycling
1814 */
1815 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001816}
1817
1818static void sig_atexit(void)
1819{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001820 sigset_t set, oset;
1821
1822 /*
1823 * avoid race condition with SIGCHLD handler
1824 * in skip_signal() which is modifying child_pid
1825 * goal is to avoid send SIGTERM to a random
1826 * process
1827 */
1828 sigemptyset(&set);
1829 sigaddset(&set, SIGCHLD);
1830 sigprocmask(SIG_BLOCK, &set, &oset);
1831
Chris Wilson933da832009-10-04 01:35:01 +01001832 if (child_pid != -1)
1833 kill(child_pid, SIGTERM);
1834
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001835 sigprocmask(SIG_SETMASK, &oset, NULL);
1836
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001837 if (signr == -1)
1838 return;
1839
1840 signal(signr, SIG_DFL);
1841 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001842}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001843
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001844static int stat__set_big_num(const struct option *opt __maybe_unused,
1845 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001846{
1847 big_num_opt = unset ? 0 : 1;
1848 return 0;
1849}
1850
Andi Kleen44b1e602016-05-30 12:49:42 -03001851static int enable_metric_only(const struct option *opt __maybe_unused,
1852 const char *s __maybe_unused, int unset)
1853{
1854 force_metric_only = true;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001855 stat_config.metric_only = !unset;
Andi Kleen44b1e602016-05-30 12:49:42 -03001856 return 0;
1857}
1858
Andi Kleenb18f3e32017-08-31 12:40:31 -07001859static int parse_metric_groups(const struct option *opt,
1860 const char *str,
1861 int unset __maybe_unused)
1862{
1863 return metricgroup__parse_groups(opt, str, &metric_events);
1864}
1865
Jiri Olsae0547312015-11-05 15:40:45 +01001866static const struct option stat_options[] = {
1867 OPT_BOOLEAN('T', "transaction", &transaction_run,
1868 "hardware transaction statistics"),
1869 OPT_CALLBACK('e', "event", &evsel_list, "event",
1870 "event selector. use 'perf list' to list available events",
1871 parse_events_option),
1872 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1873 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001874 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001875 "child tasks do not inherit counters"),
1876 OPT_STRING('p', "pid", &target.pid, "pid",
1877 "stat events on existing process id"),
1878 OPT_STRING('t', "tid", &target.tid, "tid",
1879 "stat events on existing thread id"),
1880 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1881 "system-wide collection from all CPUs"),
1882 OPT_BOOLEAN('g', "group", &group,
1883 "put the counters into a counter group"),
1884 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1885 OPT_INCR('v', "verbose", &verbose,
1886 "be more verbose (show counter open errors, etc)"),
Jiri Olsad97ae042018-08-30 08:32:36 +02001887 OPT_INTEGER('r', "repeat", &stat_config.run_count,
Jiri Olsae0547312015-11-05 15:40:45 +01001888 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001889 OPT_BOOLEAN(0, "table", &walltime_run_table,
1890 "display details about each run (only with -r option)"),
Jiri Olsaaea0dca2018-08-30 08:32:41 +02001891 OPT_BOOLEAN('n', "null", &stat_config.null_run,
Jiri Olsae0547312015-11-05 15:40:45 +01001892 "null run - dont start any counters"),
1893 OPT_INCR('d', "detailed", &detailed_run,
1894 "detailed run - start a lot of events"),
1895 OPT_BOOLEAN('S', "sync", &sync_run,
1896 "call sync() before starting a run"),
1897 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1898 "print large numbers with thousands\' separators",
1899 stat__set_big_num),
1900 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1901 "list of cpus to monitor in system-wide"),
1902 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1903 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001904 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsafa7070a2018-08-30 08:32:29 +02001905 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
Jiri Olsae0547312015-11-05 15:40:45 +01001906 "print counts with custom separator"),
1907 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1908 "monitor event in cgroup name only", parse_cgroups),
1909 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1910 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1911 OPT_INTEGER(0, "log-fd", &output_fd,
1912 "log output to fd, instead of stderr"),
1913 OPT_STRING(0, "pre", &pre_cmd, "command",
1914 "command to run prior to the measured command"),
1915 OPT_STRING(0, "post", &post_cmd, "command",
1916 "command to run after to the measured command"),
1917 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001918 "print counts at regular interval in ms "
1919 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001920 OPT_INTEGER(0, "interval-count", &stat_config.times,
1921 "print counts for fixed number of times"),
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001922 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
Jiri Olsa9660e082018-06-07 00:15:06 +02001923 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001924 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1925 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001926 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1927 "aggregate counts per processor socket", AGGR_SOCKET),
1928 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1929 "aggregate counts per physical processor core", AGGR_CORE),
1930 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1931 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001932 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001933 "ms to wait before starting measurement after program start"),
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001934 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
Andi Kleen44b1e602016-05-30 12:49:42 -03001935 "Only print computed metrics. No raw values", enable_metric_only),
1936 OPT_BOOLEAN(0, "topdown", &topdown_run,
1937 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001938 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1939 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001940 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1941 "monitor specified metrics or metric groups (separated by ,)",
1942 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001943 OPT_END()
1944};
1945
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001946static int perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
1947 struct cpu_map *map, int cpu)
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001948{
1949 return cpu_map__get_socket(map, cpu, NULL);
1950}
1951
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001952static int perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
1953 struct cpu_map *map, int cpu)
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001954{
1955 return cpu_map__get_core(map, cpu, NULL);
1956}
1957
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001958static int cpu_map__get_max(struct cpu_map *map)
1959{
1960 int i, max = -1;
1961
1962 for (i = 0; i < map->nr; i++) {
1963 if (map->map[i] > max)
1964 max = map->map[i];
1965 }
1966
1967 return max;
1968}
1969
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001970static int perf_stat__get_aggr(struct perf_stat_config *config,
1971 aggr_get_id_t get_id, struct cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001972{
1973 int cpu;
1974
1975 if (idx >= map->nr)
1976 return -1;
1977
1978 cpu = map->map[idx];
1979
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001980 if (config->cpus_aggr_map->map[cpu] == -1)
1981 config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001982
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001983 return config->cpus_aggr_map->map[cpu];
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001984}
1985
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001986static int perf_stat__get_socket_cached(struct perf_stat_config *config,
1987 struct cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001988{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001989 return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001990}
1991
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001992static int perf_stat__get_core_cached(struct perf_stat_config *config,
1993 struct cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001994{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001995 return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001996}
1997
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001998static int perf_stat_init_aggr_mode(void)
1999{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002000 int nr;
2001
Jiri Olsa421a50f2015-07-21 14:31:22 +02002002 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002003 case AGGR_SOCKET:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002004 if (cpu_map__build_socket_map(evsel_list->cpus, &stat_config.aggr_map)) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002005 perror("cannot build socket map");
2006 return -1;
2007 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002008 stat_config.aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002009 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002010 case AGGR_CORE:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002011 if (cpu_map__build_core_map(evsel_list->cpus, &stat_config.aggr_map)) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01002012 perror("cannot build core map");
2013 return -1;
2014 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002015 stat_config.aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002016 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002017 case AGGR_NONE:
2018 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002019 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002020 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002021 default:
2022 break;
2023 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002024
2025 /*
2026 * The evsel_list->cpus is the base we operate on,
2027 * taking the highest cpu number to be the size of
2028 * the aggregation translate cpumap.
2029 */
2030 nr = cpu_map__get_max(evsel_list->cpus);
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002031 stat_config.cpus_aggr_map = cpu_map__empty_new(nr + 1);
2032 return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002033}
2034
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002035static void perf_stat__exit_aggr_mode(void)
2036{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002037 cpu_map__put(stat_config.aggr_map);
2038 cpu_map__put(stat_config.cpus_aggr_map);
2039 stat_config.aggr_map = NULL;
2040 stat_config.cpus_aggr_map = NULL;
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002041}
2042
Jiri Olsa68d702f2015-11-05 15:40:58 +01002043static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2044{
2045 int cpu;
2046
2047 if (idx > map->nr)
2048 return -1;
2049
2050 cpu = map->map[idx];
2051
Jan Stancekda8a58b2017-02-17 12:10:26 +01002052 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002053 return -1;
2054
2055 return cpu;
2056}
2057
2058static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2059{
2060 struct perf_env *env = data;
2061 int cpu = perf_env__get_cpu(env, map, idx);
2062
2063 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2064}
2065
2066static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2067{
2068 struct perf_env *env = data;
2069 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2070
2071 if (cpu != -1) {
2072 int socket_id = env->cpu[cpu].socket_id;
2073
2074 /*
2075 * Encode socket in upper 16 bits
2076 * core_id is relative to socket, and
2077 * we need a global id. So we combine
2078 * socket + core id.
2079 */
2080 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2081 }
2082
2083 return core;
2084}
2085
2086static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2087 struct cpu_map **sockp)
2088{
2089 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2090}
2091
2092static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2093 struct cpu_map **corep)
2094{
2095 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2096}
2097
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002098static int perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
2099 struct cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002100{
2101 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2102}
2103
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002104static int perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
2105 struct cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002106{
2107 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2108}
2109
2110static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2111{
2112 struct perf_env *env = &st->session->header.env;
2113
2114 switch (stat_config.aggr_mode) {
2115 case AGGR_SOCKET:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002116 if (perf_env__build_socket_map(env, evsel_list->cpus, &stat_config.aggr_map)) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01002117 perror("cannot build socket map");
2118 return -1;
2119 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002120 stat_config.aggr_get_id = perf_stat__get_socket_file;
Jiri Olsa68d702f2015-11-05 15:40:58 +01002121 break;
2122 case AGGR_CORE:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002123 if (perf_env__build_core_map(env, evsel_list->cpus, &stat_config.aggr_map)) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01002124 perror("cannot build core map");
2125 return -1;
2126 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002127 stat_config.aggr_get_id = perf_stat__get_core_file;
Jiri Olsa68d702f2015-11-05 15:40:58 +01002128 break;
2129 case AGGR_NONE:
2130 case AGGR_GLOBAL:
2131 case AGGR_THREAD:
2132 case AGGR_UNSET:
2133 default:
2134 break;
2135 }
2136
2137 return 0;
2138}
2139
Andi Kleen44b1e602016-05-30 12:49:42 -03002140static int topdown_filter_events(const char **attr, char **str, bool use_group)
2141{
2142 int off = 0;
2143 int i;
2144 int len = 0;
2145 char *s;
2146
2147 for (i = 0; attr[i]; i++) {
2148 if (pmu_have_event("cpu", attr[i])) {
2149 len += strlen(attr[i]) + 1;
2150 attr[i - off] = attr[i];
2151 } else
2152 off++;
2153 }
2154 attr[i - off] = NULL;
2155
2156 *str = malloc(len + 1 + 2);
2157 if (!*str)
2158 return -1;
2159 s = *str;
2160 if (i - off == 0) {
2161 *s = 0;
2162 return 0;
2163 }
2164 if (use_group)
2165 *s++ = '{';
2166 for (i = 0; attr[i]; i++) {
2167 strcpy(s, attr[i]);
2168 s += strlen(s);
2169 *s++ = ',';
2170 }
2171 if (use_group) {
2172 s[-1] = '}';
2173 *s = 0;
2174 } else
2175 s[-1] = 0;
2176 return 0;
2177}
2178
2179__weak bool arch_topdown_check_group(bool *warn)
2180{
2181 *warn = false;
2182 return false;
2183}
2184
2185__weak void arch_topdown_group_warn(void)
2186{
2187}
2188
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002189/*
2190 * Add default attributes, if there were no attributes specified or
2191 * if -d/--detailed, -d -d or -d -d -d is used:
2192 */
2193static int add_default_attributes(void)
2194{
Andi Kleen44b1e602016-05-30 12:49:42 -03002195 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002196 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002197
2198 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2199 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2200 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2201 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2202
2203 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002204};
2205 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002206 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002207};
2208 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002209 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002210};
2211 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002212 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2213 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2214 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2215
2216};
2217
2218/*
2219 * Detailed stats (-d), covering the L1 and last level data caches:
2220 */
2221 struct perf_event_attr detailed_attrs[] = {
2222
2223 { .type = PERF_TYPE_HW_CACHE,
2224 .config =
2225 PERF_COUNT_HW_CACHE_L1D << 0 |
2226 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2227 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2228
2229 { .type = PERF_TYPE_HW_CACHE,
2230 .config =
2231 PERF_COUNT_HW_CACHE_L1D << 0 |
2232 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2233 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2234
2235 { .type = PERF_TYPE_HW_CACHE,
2236 .config =
2237 PERF_COUNT_HW_CACHE_LL << 0 |
2238 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2239 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2240
2241 { .type = PERF_TYPE_HW_CACHE,
2242 .config =
2243 PERF_COUNT_HW_CACHE_LL << 0 |
2244 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2245 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2246};
2247
2248/*
2249 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2250 */
2251 struct perf_event_attr very_detailed_attrs[] = {
2252
2253 { .type = PERF_TYPE_HW_CACHE,
2254 .config =
2255 PERF_COUNT_HW_CACHE_L1I << 0 |
2256 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2257 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2258
2259 { .type = PERF_TYPE_HW_CACHE,
2260 .config =
2261 PERF_COUNT_HW_CACHE_L1I << 0 |
2262 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2263 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2264
2265 { .type = PERF_TYPE_HW_CACHE,
2266 .config =
2267 PERF_COUNT_HW_CACHE_DTLB << 0 |
2268 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2269 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2270
2271 { .type = PERF_TYPE_HW_CACHE,
2272 .config =
2273 PERF_COUNT_HW_CACHE_DTLB << 0 |
2274 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2275 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2276
2277 { .type = PERF_TYPE_HW_CACHE,
2278 .config =
2279 PERF_COUNT_HW_CACHE_ITLB << 0 |
2280 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2281 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2282
2283 { .type = PERF_TYPE_HW_CACHE,
2284 .config =
2285 PERF_COUNT_HW_CACHE_ITLB << 0 |
2286 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2287 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2288
2289};
2290
2291/*
2292 * Very, very detailed stats (-d -d -d), adding prefetch events:
2293 */
2294 struct perf_event_attr very_very_detailed_attrs[] = {
2295
2296 { .type = PERF_TYPE_HW_CACHE,
2297 .config =
2298 PERF_COUNT_HW_CACHE_L1D << 0 |
2299 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2300 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2301
2302 { .type = PERF_TYPE_HW_CACHE,
2303 .config =
2304 PERF_COUNT_HW_CACHE_L1D << 0 |
2305 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2306 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2307};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002308 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002309
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002310 /* Set attrs if no event is selected and !null_run: */
Jiri Olsaaea0dca2018-08-30 08:32:41 +02002311 if (stat_config.null_run)
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002312 return 0;
2313
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002314 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002315 /* Handle -T as -M transaction. Once platform specific metrics
2316 * support has been added to the json files, all archictures
2317 * will use this approach. To determine transaction support
2318 * on an architecture test for such a metric name.
2319 */
2320 if (metricgroup__has_metric("transaction")) {
2321 struct option opt = { .value = &evsel_list };
2322
2323 return metricgroup__parse_groups(&opt, "transaction",
2324 &metric_events);
2325 }
2326
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002327 if (pmu_have_event("cpu", "cycles-ct") &&
2328 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002329 err = parse_events(evsel_list, transaction_attrs,
2330 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002331 else
Thomas Richterfca323402018-03-08 15:57:35 +01002332 err = parse_events(evsel_list,
2333 transaction_limited_attrs,
2334 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002335 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002336 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002337 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002338 return -1;
2339 }
2340 return 0;
2341 }
2342
Kan Liangdaefd0b2017-05-26 12:05:38 -07002343 if (smi_cost) {
2344 int smi;
2345
2346 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2347 fprintf(stderr, "freeze_on_smi is not supported.\n");
2348 return -1;
2349 }
2350
2351 if (!smi) {
2352 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2353 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2354 return -1;
2355 }
2356 smi_reset = true;
2357 }
2358
2359 if (pmu_have_event("msr", "aperf") &&
2360 pmu_have_event("msr", "smi")) {
2361 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002362 stat_config.metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002363 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002364 } else {
2365 fprintf(stderr, "To measure SMI cost, it needs "
2366 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002367 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002368 return -1;
2369 }
2370 if (err) {
2371 fprintf(stderr, "Cannot set up SMI cost events\n");
2372 return -1;
2373 }
2374 return 0;
2375 }
2376
Andi Kleen44b1e602016-05-30 12:49:42 -03002377 if (topdown_run) {
2378 char *str = NULL;
2379 bool warn = false;
2380
2381 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2382 stat_config.aggr_mode != AGGR_CORE) {
2383 pr_err("top down event configuration requires --per-core mode\n");
2384 return -1;
2385 }
2386 stat_config.aggr_mode = AGGR_CORE;
2387 if (nr_cgroups || !target__has_cpu(&target)) {
2388 pr_err("top down event configuration requires system-wide mode (-a)\n");
2389 return -1;
2390 }
2391
2392 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002393 stat_config.metric_only = true;
Andi Kleen44b1e602016-05-30 12:49:42 -03002394 if (topdown_filter_events(topdown_attrs, &str,
2395 arch_topdown_check_group(&warn)) < 0) {
2396 pr_err("Out of memory\n");
2397 return -1;
2398 }
2399 if (topdown_attrs[0] && str) {
2400 if (warn)
2401 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002402 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002403 if (err) {
2404 fprintf(stderr,
2405 "Cannot set up top down events %s: %d\n",
2406 str, err);
2407 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002408 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002409 return -1;
2410 }
2411 } else {
2412 fprintf(stderr, "System does not support topdown\n");
2413 return -1;
2414 }
2415 free(str);
2416 }
2417
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002418 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002419 if (target__has_cpu(&target))
2420 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2421
Andi Kleen9dec4472016-02-26 16:27:56 -08002422 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2423 return -1;
2424 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2425 if (perf_evlist__add_default_attrs(evsel_list,
2426 frontend_attrs) < 0)
2427 return -1;
2428 }
2429 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2430 if (perf_evlist__add_default_attrs(evsel_list,
2431 backend_attrs) < 0)
2432 return -1;
2433 }
2434 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002435 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002436 }
2437
2438 /* Detailed events get appended to the event list: */
2439
2440 if (detailed_run < 1)
2441 return 0;
2442
2443 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002444 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002445 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002446
2447 if (detailed_run < 2)
2448 return 0;
2449
2450 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002451 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002452 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002453
2454 if (detailed_run < 3)
2455 return 0;
2456
2457 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002458 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002459}
2460
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002461static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002462 "perf stat record [<options>]",
2463 NULL,
2464};
2465
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002466static void init_features(struct perf_session *session)
2467{
2468 int feat;
2469
2470 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2471 perf_header__set_feat(&session->header, feat);
2472
2473 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2474 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2475 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2476 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2477}
2478
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002479static int __cmd_record(int argc, const char **argv)
2480{
2481 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002482 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002483
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002484 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002485 PARSE_OPT_STOP_AT_NON_OPTION);
2486
2487 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002488 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002489
Jiri Olsad97ae042018-08-30 08:32:36 +02002490 if (stat_config.run_count != 1 || forever) {
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002491 pr_err("Cannot use -r option with perf stat record.\n");
2492 return -1;
2493 }
2494
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002495 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002496 if (session == NULL) {
2497 pr_err("Perf session creation failed.\n");
2498 return -1;
2499 }
2500
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002501 init_features(session);
2502
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002503 session->evlist = evsel_list;
2504 perf_stat.session = session;
2505 perf_stat.record = true;
2506 return argc;
2507}
2508
Jiri Olsaa56f9392015-11-05 15:40:59 +01002509static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2510 union perf_event *event,
2511 struct perf_session *session)
2512{
Andi Kleene3b03b62016-05-05 16:04:03 -07002513 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002514 struct perf_evsel *counter;
2515 struct timespec tsh, *ts = NULL;
2516 const char **argv = session->header.env.cmdline_argv;
2517 int argc = session->header.env.nr_cmdline;
2518
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002519 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002520 perf_stat_process_counter(&stat_config, counter);
2521
Andi Kleene3b03b62016-05-05 16:04:03 -07002522 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2523 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002524
Andi Kleene3b03b62016-05-05 16:04:03 -07002525 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002526 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2527 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002528 ts = &tsh;
2529 }
2530
2531 print_counters(ts, argc, argv);
2532 return 0;
2533}
2534
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002535static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002536int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002537 union perf_event *event,
2538 struct perf_session *session __maybe_unused)
2539{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002540 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2541
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002542 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002543
Jiri Olsa89af4e02015-11-05 15:41:02 +01002544 if (cpu_map__empty(st->cpus)) {
2545 if (st->aggr_mode != AGGR_UNSET)
2546 pr_warning("warning: processing task data, aggregation mode not set\n");
2547 return 0;
2548 }
2549
2550 if (st->aggr_mode != AGGR_UNSET)
2551 stat_config.aggr_mode = st->aggr_mode;
2552
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002553 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002554 perf_stat_init_aggr_mode();
2555 else
2556 perf_stat_init_aggr_mode_file(st);
2557
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002558 return 0;
2559}
2560
Jiri Olsa1975d362015-11-05 15:40:56 +01002561static int set_maps(struct perf_stat *st)
2562{
2563 if (!st->cpus || !st->threads)
2564 return 0;
2565
2566 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2567 return -EINVAL;
2568
2569 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2570
2571 if (perf_evlist__alloc_stats(evsel_list, true))
2572 return -ENOMEM;
2573
2574 st->maps_allocated = true;
2575 return 0;
2576}
2577
2578static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002579int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002580 union perf_event *event,
2581 struct perf_session *session __maybe_unused)
2582{
2583 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2584
2585 if (st->threads) {
2586 pr_warning("Extra thread map event, ignoring.\n");
2587 return 0;
2588 }
2589
2590 st->threads = thread_map__new_event(&event->thread_map);
2591 if (!st->threads)
2592 return -ENOMEM;
2593
2594 return set_maps(st);
2595}
2596
2597static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002598int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002599 union perf_event *event,
2600 struct perf_session *session __maybe_unused)
2601{
2602 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2603 struct cpu_map *cpus;
2604
2605 if (st->cpus) {
2606 pr_warning("Extra cpu map event, ignoring.\n");
2607 return 0;
2608 }
2609
2610 cpus = cpu_map__new_data(&event->cpu_map.data);
2611 if (!cpus)
2612 return -ENOMEM;
2613
2614 st->cpus = cpus;
2615 return set_maps(st);
2616}
2617
Jin Yao56739442017-12-05 22:03:07 +08002618static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2619{
2620 int i;
2621
2622 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2623 if (!config->stats)
2624 return -1;
2625
2626 config->stats_num = nthreads;
2627
2628 for (i = 0; i < nthreads; i++)
2629 runtime_stat__init(&config->stats[i]);
2630
2631 return 0;
2632}
2633
2634static void runtime_stat_delete(struct perf_stat_config *config)
2635{
2636 int i;
2637
2638 if (!config->stats)
2639 return;
2640
2641 for (i = 0; i < config->stats_num; i++)
2642 runtime_stat__exit(&config->stats[i]);
2643
2644 free(config->stats);
2645}
2646
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002647static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002648 "perf stat report [<options>]",
2649 NULL,
2650};
2651
2652static struct perf_stat perf_stat = {
2653 .tool = {
2654 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002655 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002656 .thread_map = process_thread_map_event,
2657 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002658 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002659 .stat = perf_event__process_stat_event,
2660 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002661 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002662 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002663};
2664
2665static int __cmd_report(int argc, const char **argv)
2666{
2667 struct perf_session *session;
2668 const struct option options[] = {
2669 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002670 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2671 "aggregate counts per processor socket", AGGR_SOCKET),
2672 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2673 "aggregate counts per physical processor core", AGGR_CORE),
2674 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2675 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002676 OPT_END()
2677 };
2678 struct stat st;
2679 int ret;
2680
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002681 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002682
2683 if (!input_name || !strlen(input_name)) {
2684 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2685 input_name = "-";
2686 else
2687 input_name = "perf.data";
2688 }
2689
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002690 perf_stat.data.file.path = input_name;
2691 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002692
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002693 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002694 if (session == NULL)
2695 return -1;
2696
2697 perf_stat.session = session;
2698 stat_config.output = stderr;
2699 evsel_list = session->evlist;
2700
2701 ret = perf_session__process_events(session);
2702 if (ret)
2703 return ret;
2704
2705 perf_session__delete(session);
2706 return 0;
2707}
2708
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002709static void setup_system_wide(int forks)
2710{
2711 /*
2712 * Make system wide (-a) the default target if
2713 * no target was specified and one of following
2714 * conditions is met:
2715 *
2716 * - there's no workload specified
2717 * - there is workload specified but all requested
2718 * events are system wide events
2719 */
2720 if (!target__none(&target))
2721 return;
2722
2723 if (!forks)
2724 target.system_wide = true;
2725 else {
2726 struct perf_evsel *counter;
2727
2728 evlist__for_each_entry(evsel_list, counter) {
2729 if (!counter->system_wide)
2730 return;
2731 }
2732
2733 if (evsel_list->nr_entries)
2734 target.system_wide = true;
2735 }
2736}
2737
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002738int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002739{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002740 const char * const stat_usage[] = {
2741 "perf stat [<options>] [<command>]",
2742 NULL
2743 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002744 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002745 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002746 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002747 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002748 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002749
Stephane Eranian5af52b52010-05-18 15:00:01 +02002750 setlocale(LC_ALL, "");
2751
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002752 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002753 if (evsel_list == NULL)
2754 return -ENOMEM;
2755
Wang Nan1669e502016-02-19 11:43:58 +00002756 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002757 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2758 (const char **) stat_usage,
2759 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002760 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002761 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002762
Jiri Olsafa7070a2018-08-30 08:32:29 +02002763 if (stat_config.csv_sep) {
2764 stat_config.csv_output = true;
2765 if (!strcmp(stat_config.csv_sep, "\\t"))
2766 stat_config.csv_sep = "\t";
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002767 } else
Jiri Olsafa7070a2018-08-30 08:32:29 +02002768 stat_config.csv_sep = DEFAULT_SEPARATOR;
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002769
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002770 if (argc && !strncmp(argv[0], "rec", 3)) {
2771 argc = __cmd_record(argc, argv);
2772 if (argc < 0)
2773 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002774 } else if (argc && !strncmp(argv[0], "rep", 3))
2775 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002776
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002777 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002778 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002779
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002780 /*
2781 * For record command the -o is already taken care of.
2782 */
2783 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002784 output = NULL;
2785
Jim Cromie56f3bae2011-09-07 17:14:00 -06002786 if (output_name && output_fd) {
2787 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002788 parse_options_usage(stat_usage, stat_options, "o", 1);
2789 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002790 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002791 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002792
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002793 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
Andi Kleen54b50912016-03-03 15:57:36 -08002794 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2795 goto out;
2796 }
2797
Jiri Olsad97ae042018-08-30 08:32:36 +02002798 if (stat_config.metric_only && stat_config.run_count > 1) {
Andi Kleen54b50912016-03-03 15:57:36 -08002799 fprintf(stderr, "--metric-only is not supported with -r\n");
2800 goto out;
2801 }
2802
Jiri Olsad97ae042018-08-30 08:32:36 +02002803 if (walltime_run_table && stat_config.run_count <= 1) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02002804 fprintf(stderr, "--table is only supported with -r\n");
2805 parse_options_usage(stat_usage, stat_options, "r", 1);
2806 parse_options_usage(NULL, stat_options, "table", 0);
2807 goto out;
2808 }
2809
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002810 if (output_fd < 0) {
2811 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002812 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002813 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002814 }
2815
Stephane Eranian4aa90152011-08-15 22:22:33 +02002816 if (!output) {
2817 struct timespec tm;
2818 mode = append_file ? "a" : "w";
2819
2820 output = fopen(output_name, mode);
2821 if (!output) {
2822 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002823 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002824 }
2825 clock_gettime(CLOCK_REALTIME, &tm);
2826 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002827 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002828 mode = append_file ? "a" : "w";
2829 output = fdopen(output_fd, mode);
2830 if (!output) {
2831 perror("Failed opening logfd");
2832 return -errno;
2833 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002834 }
2835
Jiri Olsa58215222015-07-21 14:31:24 +02002836 stat_config.output = output;
2837
Stephane Eraniand7470b62010-12-01 18:49:05 +02002838 /*
2839 * let the spreadsheet do the pretty-printing
2840 */
Jiri Olsafa7070a2018-08-30 08:32:29 +02002841 if (stat_config.csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002842 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002843 if (big_num_opt == 1) {
2844 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002845 parse_options_usage(stat_usage, stat_options, "B", 1);
2846 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002847 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002848 } else /* Nope, so disable big number formatting */
2849 big_num = false;
2850 } else if (big_num_opt == 0) /* User passed --no-big-num */
2851 big_num = false;
2852
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002853 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002854
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002855 /*
2856 * Display user/system times only for single
2857 * run and when there's specified tracee.
2858 */
Jiri Olsad97ae042018-08-30 08:32:36 +02002859 if ((stat_config.run_count == 1) && target__none(&target))
Jiri Olsa8897a892018-08-30 08:32:44 +02002860 stat_config.ru_display = true;
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002861
Jiri Olsad97ae042018-08-30 08:32:36 +02002862 if (stat_config.run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002863 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002864 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002865 goto out;
Jiri Olsad97ae042018-08-30 08:32:36 +02002866 } else if (stat_config.run_count == 0) {
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002867 forever = true;
Jiri Olsad97ae042018-08-30 08:32:36 +02002868 stat_config.run_count = 1;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002869 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002870
Jiri Olsae55c14a2018-04-23 11:08:21 +02002871 if (walltime_run_table) {
Jiri Olsad97ae042018-08-30 08:32:36 +02002872 walltime_run = zalloc(stat_config.run_count * sizeof(walltime_run[0]));
Jiri Olsae55c14a2018-04-23 11:08:21 +02002873 if (!walltime_run) {
2874 pr_err("failed to setup -r option");
2875 goto out;
2876 }
2877 }
2878
Jin Yao1d9f8d12017-12-05 22:03:10 +08002879 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2880 !target__has_task(&target)) {
2881 if (!target.system_wide || target.cpu_list) {
2882 fprintf(stderr, "The --per-thread option is only "
2883 "available when monitoring via -p -t -a "
2884 "options or only --per-thread.\n");
2885 parse_options_usage(NULL, stat_options, "p", 1);
2886 parse_options_usage(NULL, stat_options, "t", 1);
2887 goto out;
2888 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002889 }
2890
2891 /*
2892 * no_aggr, cgroup are for system-wide only
2893 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2894 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002895 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2896 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002897 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002898 fprintf(stderr, "both cgroup and no-aggregation "
2899 "modes only available in system-wide mode\n");
2900
Jiri Olsae0547312015-11-05 15:40:45 +01002901 parse_options_usage(stat_usage, stat_options, "G", 1);
2902 parse_options_usage(NULL, stat_options, "A", 1);
2903 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002904 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002905 }
2906
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002907 if (add_default_attributes())
2908 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002909
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002910 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002911
Jin Yao1d9f8d12017-12-05 22:03:10 +08002912 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2913 target.per_thread = true;
2914
Namhyung Kim77a6f012012-05-07 14:09:04 +09002915 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002916 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002917 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002918 parse_options_usage(stat_usage, stat_options, "p", 1);
2919 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002920 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002921 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002922 parse_options_usage(stat_usage, stat_options, "C", 1);
2923 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002924 }
2925 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002926 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002927
2928 /*
2929 * Initialize thread_map with comm names,
2930 * so we could print it out on output.
2931 */
Jin Yao56739442017-12-05 22:03:07 +08002932 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002933 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002934 if (target.system_wide) {
2935 if (runtime_stat_new(&stat_config,
2936 thread_map__nr(evsel_list->threads))) {
2937 goto out;
2938 }
2939 }
2940 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002941
yuzhoujiandb06a262018-01-29 10:25:22 +01002942 if (stat_config.times && interval)
2943 interval_count = true;
2944 else if (stat_config.times && !interval) {
2945 pr_err("interval-count option should be used together with "
2946 "interval-print.\n");
2947 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2948 parse_options_usage(stat_usage, stat_options, "I", 1);
2949 goto out;
2950 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002951
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002952 if (timeout && timeout < 100) {
2953 if (timeout < 10) {
2954 pr_err("timeout must be >= 10ms.\n");
2955 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2956 goto out;
2957 } else
2958 pr_warning("timeout < 100ms. "
2959 "The overhead percentage could be high in some cases. "
2960 "Please proceed with caution.\n");
2961 }
2962 if (timeout && interval) {
2963 pr_err("timeout option is not supported with interval-print.\n");
2964 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2965 parse_options_usage(stat_usage, stat_options, "I", 1);
2966 goto out;
2967 }
2968
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002969 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002970 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002971
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002972 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002973 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002974
Ingo Molnar58d7e992009-05-15 11:03:23 +02002975 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002976 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2977 * while avoiding that older tools show confusing messages.
2978 *
2979 * However for pipe sessions we need to keep it zero,
2980 * because script's perf_evsel__check_attr is triggered
2981 * by attr->sample_type != 0, and we can't run it on
2982 * stat sessions.
2983 */
2984 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2985
2986 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002987 * We dont want to block the signals - that would cause
2988 * child tasks to inherit that and Ctrl-C would not work.
2989 * What we want is for Ctrl-C to work in the exec()-ed
2990 * task, but being ignored by perf stat itself:
2991 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002992 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002993 if (!forever)
2994 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002995 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002996 signal(SIGALRM, skip_signal);
2997 signal(SIGABRT, skip_signal);
2998
Ingo Molnar42202dd2009-06-13 14:57:28 +02002999 status = 0;
Jiri Olsad97ae042018-08-30 08:32:36 +02003000 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
3001 if (stat_config.run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003002 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3003 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003004
Jiri Olsae55c14a2018-04-23 11:08:21 +02003005 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003006 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003007 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003008 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003009 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003010 }
3011
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003012 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003013 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003014
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003015 if (STAT_RECORD) {
3016 /*
3017 * We synthesize the kernel mmap record just so that older tools
3018 * don't emit warnings about not being able to resolve symbols
3019 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3020 * a saner message about no samples being in the perf.data file.
3021 *
3022 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003023 * in header.c at the moment 'perf stat record' gets introduced, which
3024 * is not really needed once we start adding the stat specific PERF_RECORD_
3025 * records, but the need to suppress the kptr_restrict messages in older
3026 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003027 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003028 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003029 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3030 process_synthesized_event,
3031 &perf_stat.session->machines.host);
3032 if (err) {
3033 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3034 "older tools may produce warnings about this file\n.");
3035 }
3036
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003037 if (!interval) {
3038 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3039 pr_err("failed to write stat round event\n");
3040 }
3041
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003042 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003043 perf_stat.session->header.data_size += perf_stat.bytes_written;
3044 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3045 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003046
3047 perf_session__delete(perf_stat.session);
3048 }
3049
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003050 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003051 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003052out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003053 free(walltime_run);
3054
Kan Liangdaefd0b2017-05-26 12:05:38 -07003055 if (smi_cost && smi_reset)
3056 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3057
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003058 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003059
3060 runtime_stat_delete(&stat_config);
3061
Ingo Molnar42202dd2009-06-13 14:57:28 +02003062 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003063}