blob: 14688710195cb8e77cd98086032f02b48c3188da [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02003 * builtin-stat.c
4 *
5 * Builtin stat command: Give a precise performance counters summary
6 * overview about any workload, CPU or specific PID.
7 *
8 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02009
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020010 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020011
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020012 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020013
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020014 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020015
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020016 1708.761321 task-clock # 11.037 CPUs utilized
17 41,190 context-switches # 0.024 M/sec
18 6,735 CPU-migrations # 0.004 M/sec
19 17,318 page-faults # 0.010 M/sec
20 5,205,202,243 cycles # 3.046 GHz
21 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
22 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
23 2,603,501,247 instructions # 0.50 insns per cycle
24 # 1.48 stalled cycles per insn
25 484,357,498 branches # 283.455 M/sec
26 6,388,934 branch-misses # 1.32% of all branches
27
28 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020029
Ingo Molnar52425192009-05-26 09:17:18 +020030 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020031 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020032 *
33 * Improvements and fixes by:
34 *
35 * Arjan van de Ven <arjan@linux.intel.com>
36 * Yanmin Zhang <yanmin.zhang@intel.com>
37 * Wu Fengguang <fengguang.wu@intel.com>
38 * Mike Galbraith <efault@gmx.de>
39 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053040 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020041 */
42
Ingo Molnar16f762a2009-05-27 09:10:38 +020043#include "builtin.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030044#include "perf.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030045#include "util/cgroup.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060046#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020047#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070048#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020049#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020050#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020051#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020052#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020053#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080054#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080055#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110056#include "util/cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020057#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020058#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030059#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010060#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010061#include "util/tool.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030062#include "util/string2.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070063#include "util/metricgroup.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030064#include "util/synthetic-events.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030065#include "util/target.h"
Arnaldo Carvalho de Melof3711022019-08-29 15:16:27 -030066#include "util/time-utils.h"
Jiri Olsa9660e082018-06-07 00:15:06 +020067#include "util/top.h"
Andi Kleen4804e012019-11-20 16:15:19 -080068#include "util/affinity.h"
Stephane Eranian70943492020-05-05 11:29:43 -070069#include "util/pfm.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010070#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020071
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030072#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030073#include <linux/zalloc.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030074#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030075#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030076#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020077#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020078#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030079#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020080#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070081#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030082#include <sys/types.h>
83#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030084#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030085#include <unistd.h>
Jiri Olsa0ce2da12018-06-05 14:13:13 +020086#include <sys/time.h>
87#include <sys/resource.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053088#include <linux/err.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020089
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030090#include <linux/ctype.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020091#include <perf/evlist.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030092
Stephane Eraniand7470b62010-12-01 18:49:05 +020093#define DEFAULT_SEPARATOR " "
Kan Liangdaefd0b2017-05-26 12:05:38 -070094#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020095
Jiri Olsad4f63a42015-06-26 11:29:26 +020096static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010097
Andi Kleen4cabc3d2013-08-21 16:47:26 -070098/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020099static const char *transaction_attrs = {
100 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700101 "{"
102 "instructions,"
103 "cycles,"
104 "cpu/cycles-t/,"
105 "cpu/tx-start/,"
106 "cpu/el-start/,"
107 "cpu/cycles-ct/"
108 "}"
109};
110
111/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200112static const char * transaction_limited_attrs = {
113 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700114 "{"
115 "instructions,"
116 "cycles,"
117 "cpu/cycles-t/,"
118 "cpu/tx-start/"
119 "}"
120};
121
Andi Kleen44b1e602016-05-30 12:49:42 -0300122static const char * topdown_attrs[] = {
123 "topdown-total-slots",
124 "topdown-slots-retired",
125 "topdown-recovery-bubbles",
126 "topdown-fetch-bubbles",
127 "topdown-slots-issued",
128 NULL,
129};
130
Kan Liangdaefd0b2017-05-26 12:05:38 -0700131static const char *smi_cost_attrs = {
132 "{"
133 "msr/aperf/,"
134 "msr/smi/,"
135 "cycles"
136 "}"
137};
138
Jiri Olsa63503db2019-07-21 13:23:52 +0200139static struct evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200140
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300141static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900142 .uid = UINT_MAX,
143};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530144
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200145#define METRIC_ONLY_LEN 20
146
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200147static volatile pid_t child_pid = -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200148static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700149static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300150static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700151static bool smi_cost = false;
152static bool smi_reset = false;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200153static int big_num_opt = -1;
Lin Ming43bece72011-08-17 18:42:07 +0800154static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200155static const char *pre_cmd = NULL;
156static const char *post_cmd = NULL;
157static bool sync_run = false;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500158static bool forever = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300159static bool force_metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100160static struct timespec ref_time;
Jiri Olsae0547312015-11-05 15:40:45 +0100161static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100162static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100163static const char *output_name;
164static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200165
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100166struct perf_stat {
167 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100168 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100169 struct perf_session *session;
170 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100171 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100172 bool maps_allocated;
Jiri Olsaf8548392019-07-21 13:23:49 +0200173 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +0200174 struct perf_thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100175 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100176};
177
178static struct perf_stat perf_stat;
179#define STAT_RECORD perf_stat.record
180
Liming Wang60666c62009-12-31 16:05:50 +0800181static volatile int done = 0;
182
Jiri Olsa421a50f2015-07-21 14:31:22 +0200183static struct perf_stat_config stat_config = {
Jiri Olsa26893a62018-08-30 08:32:40 +0200184 .aggr_mode = AGGR_GLOBAL,
185 .scale = true,
186 .unit_width = 4, /* strlen("unit") */
187 .run_count = 1,
188 .metric_only_len = METRIC_ONLY_LEN,
189 .walltime_nsecs_stats = &walltime_nsecs_stats,
Jiri Olsa34ff0862018-08-30 08:32:47 +0200190 .big_num = true,
Alexey Budankov27e97692020-07-17 10:05:41 +0300191 .ctl_fd = -1,
192 .ctl_fd_ack = -1
Jiri Olsa421a50f2015-07-21 14:31:22 +0200193};
194
Jiri Olsaa9a179022020-06-02 12:17:36 +0200195static bool cpus_map_matched(struct evsel *a, struct evsel *b)
196{
197 if (!a->core.cpus && !b->core.cpus)
198 return true;
199
200 if (!a->core.cpus || !b->core.cpus)
201 return false;
202
203 if (a->core.cpus->nr != b->core.cpus->nr)
204 return false;
205
206 for (int i = 0; i < a->core.cpus->nr; i++) {
207 if (a->core.cpus->map[i] != b->core.cpus->map[i])
208 return false;
209 }
210
211 return true;
212}
213
214static void evlist__check_cpu_maps(struct evlist *evlist)
215{
216 struct evsel *evsel, *pos, *leader;
217 char buf[1024];
218
219 evlist__for_each_entry(evlist, evsel) {
220 leader = evsel->leader;
221
222 /* Check that leader matches cpus with each member. */
223 if (leader == evsel)
224 continue;
225 if (cpus_map_matched(leader, evsel))
226 continue;
227
228 /* If there's mismatch disable the group and warn user. */
229 WARN_ONCE(1, "WARNING: grouped events cpus do not match, disabling group:\n");
230 evsel__group_desc(leader, buf, sizeof(buf));
231 pr_warning(" %s\n", buf);
232
233 if (verbose) {
234 cpu_map__snprint(leader->core.cpus, buf, sizeof(buf));
235 pr_warning(" %s: %s\n", leader->name, buf);
236 cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf));
237 pr_warning(" %s: %s\n", evsel->name, buf);
238 }
239
240 for_each_group_evsel(pos, leader) {
241 pos->leader = pos;
242 pos->core.nr_members = 0;
243 }
244 evsel->leader->core.nr_members = 0;
245 }
246}
247
Stephane Eranian13370a92013-01-29 12:47:44 +0100248static inline void diff_timespec(struct timespec *r, struct timespec *a,
249 struct timespec *b)
250{
251 r->tv_sec = a->tv_sec - b->tv_sec;
252 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300253 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100254 r->tv_sec--;
255 } else {
256 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
257 }
258}
259
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200260static void perf_stat__reset_stats(void)
261{
Jin Yao56739442017-12-05 22:03:07 +0800262 int i;
263
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200264 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200265 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800266
267 for (i = 0; i < stat_config.stats_num; i++)
268 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200269}
270
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100271static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100272 union perf_event *event,
273 struct perf_sample *sample __maybe_unused,
274 struct machine *machine __maybe_unused)
275{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100276 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100277 pr_err("failed to write perf data, error: %m\n");
278 return -1;
279 }
280
281 perf_stat.bytes_written += event->header.size;
282 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100283}
284
Jiri Olsa1975d362015-11-05 15:40:56 +0100285static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100286{
Jiri Olsa1975d362015-11-05 15:40:56 +0100287 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100288 process_synthesized_event,
289 NULL);
290}
291
292#define WRITE_STAT_ROUND_EVENT(time, interval) \
293 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
294
Jiri Olsa8cd36f32019-09-02 22:04:12 +0200295#define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100296
Arnaldo Carvalho de Meloddc69992020-05-04 13:46:34 -0300297static int evsel__write_stat_event(struct evsel *counter, u32 cpu, u32 thread,
298 struct perf_counts_values *count)
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100299{
300 struct perf_sample_id *sid = SID(counter, cpu, thread);
301
302 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
303 process_synthesized_event, NULL);
304}
305
Jiri Olsa32dcd022019-07-21 13:23:51 +0200306static int read_single_counter(struct evsel *counter, int cpu,
Andi Kleenf0fbb112019-03-26 15:18:21 -0700307 int thread, struct timespec *rs)
308{
309 if (counter->tool_event == PERF_TOOL_DURATION_TIME) {
310 u64 val = rs->tv_nsec + rs->tv_sec*1000000000ULL;
311 struct perf_counts_values *count =
312 perf_counts(counter->counts, cpu, thread);
313 count->ena = count->run = val;
314 count->val = val;
315 return 0;
316 }
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -0300317 return evsel__read_counter(counter, cpu, thread);
Andi Kleenf0fbb112019-03-26 15:18:21 -0700318}
319
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200320/*
321 * Read out the results of a single counter:
322 * do not aggregate counts across CPUs in system-wide mode
323 */
Andi Kleen4b49ab72019-11-20 16:15:20 -0800324static int read_counter_cpu(struct evsel *counter, struct timespec *rs, int cpu)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200325{
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200326 int nthreads = perf_thread_map__nr(evsel_list->core.threads);
Andi Kleen4b49ab72019-11-20 16:15:20 -0800327 int thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200328
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000329 if (!counter->supported)
330 return -ENOENT;
331
Jiri Olsa648b5af2019-08-06 11:35:19 +0200332 if (counter->core.system_wide)
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100333 nthreads = 1;
334
335 for (thread = 0; thread < nthreads; thread++) {
Andi Kleen4b49ab72019-11-20 16:15:20 -0800336 struct perf_counts_values *count;
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200337
Andi Kleen4b49ab72019-11-20 16:15:20 -0800338 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200339
Andi Kleen4b49ab72019-11-20 16:15:20 -0800340 /*
341 * The leader's group read loads data into its group members
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -0300342 * (via evsel__read_counter()) and sets their count->loaded.
Andi Kleen4b49ab72019-11-20 16:15:20 -0800343 */
344 if (!perf_counts__is_loaded(counter->counts, cpu, thread) &&
345 read_single_counter(counter, cpu, thread, rs)) {
346 counter->counts->scaled = -1;
347 perf_counts(counter->counts, cpu, thread)->ena = 0;
348 perf_counts(counter->counts, cpu, thread)->run = 0;
349 return -1;
350 }
351
352 perf_counts__set_loaded(counter->counts, cpu, thread, false);
353
354 if (STAT_RECORD) {
Arnaldo Carvalho de Meloddc69992020-05-04 13:46:34 -0300355 if (evsel__write_stat_event(counter, cpu, thread, count)) {
Andi Kleen4b49ab72019-11-20 16:15:20 -0800356 pr_err("failed to write stat event\n");
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100357 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700358 }
Andi Kleen4b49ab72019-11-20 16:15:20 -0800359 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100360
Andi Kleen4b49ab72019-11-20 16:15:20 -0800361 if (verbose > 1) {
362 fprintf(stat_config.output,
363 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300364 evsel__name(counter),
Andi Kleen4b49ab72019-11-20 16:15:20 -0800365 cpu,
366 count->val, count->ena, count->run);
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100367 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200368 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200369
370 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200371}
372
Jin Yaoc7e5b322020-05-20 12:27:37 +0800373static int read_affinity_counters(struct timespec *rs)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200374{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200375 struct evsel *counter;
Andi Kleen4b49ab72019-11-20 16:15:20 -0800376 struct affinity affinity;
377 int i, ncpus, cpu;
378
379 if (affinity__setup(&affinity) < 0)
Jin Yaoc7e5b322020-05-20 12:27:37 +0800380 return -1;
Andi Kleen4b49ab72019-11-20 16:15:20 -0800381
382 ncpus = perf_cpu_map__nr(evsel_list->core.all_cpus);
383 if (!target__has_cpu(&target) || target__has_per_thread(&target))
384 ncpus = 1;
385 evlist__for_each_cpu(evsel_list, i, cpu) {
386 if (i >= ncpus)
387 break;
388 affinity__set(&affinity, cpu);
389
390 evlist__for_each_entry(evsel_list, counter) {
391 if (evsel__cpu_iter_skip(counter, cpu))
392 continue;
393 if (!counter->err) {
394 counter->err = read_counter_cpu(counter, rs,
395 counter->cpu_iter - 1);
396 }
397 }
398 }
399 affinity__cleanup(&affinity);
Jin Yaoc7e5b322020-05-20 12:27:37 +0800400 return 0;
401}
402
403static void read_counters(struct timespec *rs)
404{
405 struct evsel *counter;
406
Jin Yaoee6a9612020-09-03 09:01:13 +0800407 if (!stat_config.stop_read_counter && (read_affinity_counters(rs) < 0))
Jin Yaoc7e5b322020-05-20 12:27:37 +0800408 return;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200409
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300410 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen4b49ab72019-11-20 16:15:20 -0800411 if (counter->err)
Andi Kleen245bad82015-09-01 15:52:46 -0700412 pr_debug("failed to read counter %s\n", counter->name);
Andi Kleen4b49ab72019-11-20 16:15:20 -0800413 if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200414 pr_warning("failed to process counter %s\n", counter->name);
Andi Kleen4b49ab72019-11-20 16:15:20 -0800415 counter->err = 0;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200416 }
417}
418
Jin Yao72f02a92020-05-20 12:27:33 +0800419static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
420{
421 int i;
422
423 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
424 if (!config->stats)
425 return -1;
426
427 config->stats_num = nthreads;
428
429 for (i = 0; i < nthreads; i++)
430 runtime_stat__init(&config->stats[i]);
431
432 return 0;
433}
434
435static void runtime_stat_delete(struct perf_stat_config *config)
436{
437 int i;
438
439 if (!config->stats)
440 return;
441
442 for (i = 0; i < config->stats_num; i++)
443 runtime_stat__exit(&config->stats[i]);
444
445 zfree(&config->stats);
446}
447
448static void runtime_stat_reset(struct perf_stat_config *config)
449{
450 int i;
451
452 if (!config->stats)
453 return;
454
455 for (i = 0; i < config->stats_num; i++)
456 perf_stat__reset_shadow_per_stat(&config->stats[i]);
457}
458
Jiri Olsaba411a92015-06-26 11:29:24 +0200459static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100460{
Stephane Eranian13370a92013-01-29 12:47:44 +0100461 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100462
Stephane Eranian13370a92013-01-29 12:47:44 +0100463 clock_gettime(CLOCK_MONOTONIC, &ts);
464 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100465
Jin Yao197ba862020-04-20 22:54:17 +0800466 perf_stat__reset_shadow_per_stat(&rt_stat);
Jin Yao72f02a92020-05-20 12:27:33 +0800467 runtime_stat_reset(&stat_config);
Andi Kleenf0fbb112019-03-26 15:18:21 -0700468 read_counters(&rs);
469
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100470 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300471 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100472 pr_err("failed to write stat round event\n");
473 }
474
Andi Kleenb90f1332017-08-31 12:40:36 -0700475 init_stats(&walltime_nsecs_stats);
Jiri Olsaea9eb1f2020-05-18 15:14:45 +0200476 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000ULL);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200477 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100478}
479
Alexey Budankovdece3a42020-07-17 10:02:08 +0300480static bool handle_interval(unsigned int interval, int *times)
481{
482 if (interval) {
483 process_interval();
484 if (interval_count && !(--(*times)))
485 return true;
486 }
487 return false;
488}
489
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100490static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700491{
Alexey Budankov2162b9c2020-07-17 10:04:33 +0300492 if (stat_config.initial_delay < 0) {
493 pr_info(EVLIST_DISABLED_MSG);
494 return;
495 }
496
497 if (stat_config.initial_delay > 0) {
498 pr_info(EVLIST_DISABLED_MSG);
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200499 usleep(stat_config.initial_delay * USEC_PER_MSEC);
Alexey Budankov2162b9c2020-07-17 10:04:33 +0300500 }
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100501
502 /*
503 * We need to enable counters only if:
504 * - we don't have tracee (attaching to task or cpu)
505 * - we have initial delay configured
506 */
Alexey Budankov2162b9c2020-07-17 10:04:33 +0300507 if (!target__none(&target) || stat_config.initial_delay) {
Jiri Olsa1c87f162019-07-21 13:24:08 +0200508 evlist__enable(evsel_list);
Alexey Budankov2162b9c2020-07-17 10:04:33 +0300509 if (stat_config.initial_delay > 0)
510 pr_info(EVLIST_ENABLED_MSG);
511 }
Andi Kleen41191682013-08-02 17:41:11 -0700512}
513
Mark Rutland3df33ef2016-08-09 14:04:29 +0100514static void disable_counters(void)
515{
516 /*
517 * If we don't have tracee (attaching to task or cpu), counters may
518 * still be running. To get accurate group ratios, we must stop groups
519 * from counting before reading their constituent counters.
520 */
521 if (!target__none(&target))
Jiri Olsae74676d2019-07-21 13:24:09 +0200522 evlist__disable(evsel_list);
Mark Rutland3df33ef2016-08-09 14:04:29 +0100523}
524
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300525static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300526
527/*
528 * perf_evlist__prepare_workload will send a SIGUSR1
529 * if the fork fails, since we asked by setting its
530 * want_signal to true.
531 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300532static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
533 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300534{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300535 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300536}
537
Arnaldo Carvalho de Meloddc69992020-05-04 13:46:34 -0300538static bool evsel__should_store_id(struct evsel *counter)
Jiri Olsa82bf3112017-07-26 14:02:06 +0200539{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200540 return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200541}
542
Jiri Olsacbb5df7e2018-10-22 11:30:15 +0200543static bool is_target_alive(struct target *_target,
Jiri Olsa9749b902019-07-21 13:23:50 +0200544 struct perf_thread_map *threads)
Jiri Olsacbb5df7e2018-10-22 11:30:15 +0200545{
546 struct stat st;
547 int i;
548
549 if (!target__has_task(_target))
550 return true;
551
552 for (i = 0; i < threads->nr; i++) {
553 char path[PATH_MAX];
554
555 scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(),
556 threads->map[i].pid);
557
558 if (!stat(path, &st))
559 return true;
560 }
561
562 return false;
563}
564
Alexey Budankovbee328c2020-07-17 10:05:06 +0300565static void process_evlist(struct evlist *evlist, unsigned int interval)
566{
567 enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
568
569 if (evlist__ctlfd_process(evlist, &cmd) > 0) {
570 switch (cmd) {
571 case EVLIST_CTL_CMD_ENABLE:
572 pr_info(EVLIST_ENABLED_MSG);
573 if (interval)
574 process_interval();
575 break;
576 case EVLIST_CTL_CMD_DISABLE:
577 if (interval)
578 process_interval();
579 pr_info(EVLIST_DISABLED_MSG);
580 break;
581 case EVLIST_CTL_CMD_ACK:
582 case EVLIST_CTL_CMD_UNSUPPORTED:
583 default:
584 break;
585 }
586 }
587}
588
589static void compute_tts(struct timespec *time_start, struct timespec *time_stop,
590 int *time_to_sleep)
591{
592 int tts = *time_to_sleep;
593 struct timespec time_diff;
594
595 diff_timespec(&time_diff, time_stop, time_start);
596
597 tts -= time_diff.tv_sec * MSEC_PER_SEC +
598 time_diff.tv_nsec / NSEC_PER_MSEC;
599
600 if (tts < 0)
601 tts = 0;
602
603 *time_to_sleep = tts;
604}
605
606static int dispatch_events(bool forks, int timeout, int interval, int *times)
Alexey Budankov987b8232020-07-17 10:04:02 +0300607{
608 int child_exited = 0, status = 0;
Alexey Budankovbee328c2020-07-17 10:05:06 +0300609 int time_to_sleep, sleep_time;
610 struct timespec time_start, time_stop;
611
612 if (interval)
613 sleep_time = interval;
614 else if (timeout)
615 sleep_time = timeout;
616 else
617 sleep_time = 1000;
618
619 time_to_sleep = sleep_time;
Alexey Budankov987b8232020-07-17 10:04:02 +0300620
621 while (!done) {
622 if (forks)
623 child_exited = waitpid(child_pid, &status, WNOHANG);
624 else
625 child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0;
626
627 if (child_exited)
628 break;
629
Alexey Budankovbee328c2020-07-17 10:05:06 +0300630 clock_gettime(CLOCK_MONOTONIC, &time_start);
631 if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
632 if (timeout || handle_interval(interval, times))
633 break;
634 time_to_sleep = sleep_time;
635 } else { /* fd revent */
636 process_evlist(evsel_list, interval);
637 clock_gettime(CLOCK_MONOTONIC, &time_stop);
638 compute_tts(&time_start, &time_stop, &time_to_sleep);
639 }
Alexey Budankov987b8232020-07-17 10:04:02 +0300640 }
641
642 return status;
643}
644
Andi Kleene0e6a6c2019-11-20 16:15:18 -0800645enum counter_recovery {
646 COUNTER_SKIP,
647 COUNTER_RETRY,
648 COUNTER_FATAL,
649};
650
651static enum counter_recovery stat_handle_error(struct evsel *counter)
652{
653 char msg[BUFSIZ];
654 /*
655 * PPC returns ENXIO for HW counters until 2.6.37
656 * (behavior changed with commit b0a873e).
657 */
658 if (errno == EINVAL || errno == ENOSYS ||
659 errno == ENOENT || errno == EOPNOTSUPP ||
660 errno == ENXIO) {
661 if (verbose > 0)
662 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300663 evsel__name(counter));
Andi Kleene0e6a6c2019-11-20 16:15:18 -0800664 counter->supported = false;
Andi Kleen4804e012019-11-20 16:15:19 -0800665 /*
666 * errored is a sticky flag that means one of the counter's
667 * cpu event had a problem and needs to be reexamined.
668 */
669 counter->errored = true;
Andi Kleene0e6a6c2019-11-20 16:15:18 -0800670
671 if ((counter->leader != counter) ||
672 !(counter->leader->core.nr_members > 1))
673 return COUNTER_SKIP;
Arnaldo Carvalho de Meloae430892020-04-30 11:46:15 -0300674 } else if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
Andi Kleene0e6a6c2019-11-20 16:15:18 -0800675 if (verbose > 0)
676 ui__warning("%s\n", msg);
677 return COUNTER_RETRY;
678 } else if (target__has_per_thread(&target) &&
679 evsel_list->core.threads &&
680 evsel_list->core.threads->err_thread != -1) {
681 /*
682 * For global --per-thread case, skip current
683 * error thread.
684 */
685 if (!thread_map__remove(evsel_list->core.threads,
686 evsel_list->core.threads->err_thread)) {
687 evsel_list->core.threads->err_thread = -1;
688 return COUNTER_RETRY;
689 }
690 }
691
Arnaldo Carvalho de Melo2bb72db2020-05-04 13:43:03 -0300692 evsel__open_strerror(counter, &target, errno, msg, sizeof(msg));
Andi Kleene0e6a6c2019-11-20 16:15:18 -0800693 ui__error("%s\n", msg);
694
695 if (child_pid != -1)
696 kill(child_pid, SIGTERM);
697 return COUNTER_FATAL;
698}
699
Jiri Olsae55c14a2018-04-23 11:08:21 +0200700static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200701{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200702 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100703 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100704 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300705 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200706 unsigned long long t0, t1;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200707 struct evsel *counter;
Stephane Eranian410136f2013-11-12 17:58:49 +0100708 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200709 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300710 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100711 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Andi Kleen4804e012019-11-20 16:15:19 -0800712 struct affinity affinity;
713 int i, cpu;
714 bool second_pass = false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200715
Liming Wang60666c62009-12-31 16:05:50 +0800716 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100717 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300718 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900719 perror("failed to prepare workload");
720 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800721 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900722 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000723 }
724
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200725 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300726 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200727
Andi Kleen4804e012019-11-20 16:15:19 -0800728 if (affinity__setup(&affinity) < 0)
729 return -1;
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700730
Andi Kleen4804e012019-11-20 16:15:19 -0800731 evlist__for_each_cpu (evsel_list, i, cpu) {
732 affinity__set(&affinity, cpu);
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700733
Andi Kleen4804e012019-11-20 16:15:19 -0800734 evlist__for_each_entry(evsel_list, counter) {
735 if (evsel__cpu_iter_skip(counter, cpu))
Andi Kleene0e6a6c2019-11-20 16:15:18 -0800736 continue;
Andi Kleen4804e012019-11-20 16:15:19 -0800737 if (counter->reset_group || counter->errored)
738 continue;
739try_again:
740 if (create_perf_stat_counter(counter, &stat_config, &target,
741 counter->cpu_iter - 1) < 0) {
742
743 /*
744 * Weak group failed. We cannot just undo this here
745 * because earlier CPUs might be in group mode, and the kernel
746 * doesn't support mixing group and non group reads. Defer
747 * it to later.
748 * Don't close here because we're in the wrong affinity.
749 */
750 if ((errno == EINVAL || errno == EBADF) &&
751 counter->leader != counter &&
752 counter->weak_group) {
753 perf_evlist__reset_weak_group(evsel_list, counter, false);
754 assert(counter->reset_group);
755 second_pass = true;
756 continue;
757 }
758
759 switch (stat_handle_error(counter)) {
760 case COUNTER_FATAL:
761 return -1;
762 case COUNTER_RETRY:
763 goto try_again;
764 case COUNTER_SKIP:
765 continue;
766 default:
767 break;
768 }
769
770 }
771 counter->supported = true;
772 }
773 }
774
775 if (second_pass) {
776 /*
777 * Now redo all the weak group after closing them,
778 * and also close errored counters.
779 */
780
781 evlist__for_each_cpu(evsel_list, i, cpu) {
782 affinity__set(&affinity, cpu);
783 /* First close errored or weak retry */
784 evlist__for_each_entry(evsel_list, counter) {
785 if (!counter->reset_group && !counter->errored)
786 continue;
787 if (evsel__cpu_iter_skip_no_inc(counter, cpu))
788 continue;
789 perf_evsel__close_cpu(&counter->core, counter->cpu_iter);
790 }
791 /* Now reopen weak */
792 evlist__for_each_entry(evsel_list, counter) {
793 if (!counter->reset_group && !counter->errored)
794 continue;
795 if (evsel__cpu_iter_skip(counter, cpu))
796 continue;
797 if (!counter->reset_group)
798 continue;
799try_again_reset:
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300800 pr_debug2("reopening weak %s\n", evsel__name(counter));
Andi Kleen4804e012019-11-20 16:15:19 -0800801 if (create_perf_stat_counter(counter, &stat_config, &target,
802 counter->cpu_iter - 1) < 0) {
803
804 switch (stat_handle_error(counter)) {
805 case COUNTER_FATAL:
806 return -1;
807 case COUNTER_RETRY:
808 goto try_again_reset;
809 case COUNTER_SKIP:
810 continue;
811 default:
812 break;
813 }
814 }
815 counter->supported = true;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800816 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200817 }
Andi Kleen4804e012019-11-20 16:15:19 -0800818 }
819 affinity__cleanup(&affinity);
820
821 evlist__for_each_entry(evsel_list, counter) {
822 if (!counter->supported) {
823 perf_evsel__free_fd(&counter->core);
824 continue;
825 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100826
827 l = strlen(counter->unit);
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200828 if (l > stat_config.unit_width)
829 stat_config.unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100830
Arnaldo Carvalho de Meloddc69992020-05-04 13:46:34 -0300831 if (evsel__should_store_id(counter) &&
Arnaldo Carvalho de Melo34397752020-05-04 13:45:19 -0300832 evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100833 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300834 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200835
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300836 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300837 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300838 counter->filter, evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300839 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100840 return -1;
841 }
842
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100843 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100844 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100845
Jiri Olsa664c98d2015-11-05 15:40:50 +0100846 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100847 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100848 } else {
849 err = perf_session__write_header(perf_stat.session, evsel_list,
850 fd, false);
851 }
852
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100853 if (err < 0)
854 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100855
Arnaldo Carvalho de Melob2518922019-09-10 17:17:33 +0100856 err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
857 process_synthesized_event, is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100858 if (err < 0)
859 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100860 }
861
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200862 /*
863 * Enable counters and exec the command:
864 */
865 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100866 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200867
Liming Wang60666c62009-12-31 16:05:50 +0800868 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900869 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100870 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900871
Alexey Budankov27e97692020-07-17 10:05:41 +0300872 if (interval || timeout || evlist__ctlfd_initialized(evsel_list))
Alexey Budankovbee328c2020-07-17 10:05:06 +0300873 status = dispatch_events(forks, timeout, interval, &times);
Arnaldo Carvalho de Melocfbd41b2020-04-15 12:31:26 -0300874 if (child_pid != -1) {
875 if (timeout)
876 kill(child_pid, SIGTERM);
Jin Yao8a992552019-01-03 15:40:45 +0800877 wait4(child_pid, &status, 0, &stat_config.ru_data);
Arnaldo Carvalho de Melocfbd41b2020-04-15 12:31:26 -0300878 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300879
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300880 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300881 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300882 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300883 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300884 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300885
Andi Kleen33e49ea2011-09-15 14:31:40 -0700886 if (WIFSIGNALED(status))
887 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800888 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100889 enable_counters();
Alexey Budankovbee328c2020-07-17 10:05:06 +0300890 status = dispatch_events(forks, timeout, interval, &times);
Liming Wang60666c62009-12-31 16:05:50 +0800891 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200892
Mark Rutland3df33ef2016-08-09 14:04:29 +0100893 disable_counters();
894
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200895 t1 = rdclock();
896
Jiri Olsa54ac0b12018-08-30 08:32:50 +0200897 if (stat_config.walltime_run_table)
898 stat_config.walltime_run[run_idx] = t1 - t0;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200899
Jin Yaoee6a9612020-09-03 09:01:13 +0800900 if (interval && stat_config.summary) {
Jin Yaoc7e5b322020-05-20 12:27:37 +0800901 stat_config.interval = 0;
Jin Yaoee6a9612020-09-03 09:01:13 +0800902 stat_config.stop_read_counter = true;
Jin Yaoc7e5b322020-05-20 12:27:37 +0800903 init_stats(&walltime_nsecs_stats);
904 update_stats(&walltime_nsecs_stats, t1 - t0);
905
906 if (stat_config.aggr_mode == AGGR_GLOBAL)
907 perf_evlist__save_aggr_prev_raw_counts(evsel_list);
908
909 perf_evlist__copy_prev_raw_counts(evsel_list);
910 perf_evlist__reset_prev_raw_counts(evsel_list);
911 runtime_stat_reset(&stat_config);
912 perf_stat__reset_shadow_per_stat(&rt_stat);
913 } else
914 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200915
Mark Rutland3df33ef2016-08-09 14:04:29 +0100916 /*
917 * Closing a group leader splits the group, and as we only disable
918 * group leaders, results in remaining events becoming enabled. To
919 * avoid arbitrary skew, we must read all counters before closing any
920 * group leaders.
921 */
Andi Kleenf0fbb112019-03-26 15:18:21 -0700922 read_counters(&(struct timespec) { .tv_nsec = t1-t0 });
Jiri Olsa08ef3af2019-07-15 16:21:21 +0200923
924 /*
925 * We need to keep evsel_list alive, because it's processed
926 * later the evsel_list will be closed after.
927 */
928 if (!STAT_RECORD)
Jiri Olsa750b4ed2019-07-21 13:24:07 +0200929 evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200930
Ingo Molnar42202dd2009-06-13 14:57:28 +0200931 return WEXITSTATUS(status);
932}
933
Jiri Olsae55c14a2018-04-23 11:08:21 +0200934static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200935{
936 int ret;
937
938 if (pre_cmd) {
939 ret = system(pre_cmd);
940 if (ret)
941 return ret;
942 }
943
944 if (sync_run)
945 sync();
946
Jiri Olsae55c14a2018-04-23 11:08:21 +0200947 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200948 if (ret)
949 return ret;
950
951 if (post_cmd) {
952 ret = system(post_cmd);
953 if (ret)
954 return ret;
955 }
956
957 return ret;
958}
959
Jiri Olsaa5a9eac2018-08-30 08:32:24 +0200960static void print_counters(struct timespec *ts, int argc, const char **argv)
961{
Jiri Olsa01748202018-08-30 08:32:25 +0200962 /* Do not print anything if we record to the pipe. */
963 if (STAT_RECORD && perf_stat.data.is_pipe)
964 return;
965
Jiri Olsac512e0e2018-08-30 08:32:33 +0200966 perf_evlist__print_counters(evsel_list, &stat_config, &target,
Jiri Olsab64df7f2018-08-30 08:32:26 +0200967 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +0200968}
969
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200970static volatile int signr = -1;
971
Ingo Molnar52425192009-05-26 09:17:18 +0200972static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200973{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200974 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +0800975 done = 1;
976
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200977 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200978 /*
979 * render child_pid harmless
980 * won't send SIGTERM to a random
981 * process in case of race condition
982 * and fast PID recycling
983 */
984 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200985}
986
987static void sig_atexit(void)
988{
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200989 sigset_t set, oset;
990
991 /*
992 * avoid race condition with SIGCHLD handler
993 * in skip_signal() which is modifying child_pid
994 * goal is to avoid send SIGTERM to a random
995 * process
996 */
997 sigemptyset(&set);
998 sigaddset(&set, SIGCHLD);
999 sigprocmask(SIG_BLOCK, &set, &oset);
1000
Chris Wilson933da832009-10-04 01:35:01 +01001001 if (child_pid != -1)
1002 kill(child_pid, SIGTERM);
1003
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001004 sigprocmask(SIG_SETMASK, &oset, NULL);
1005
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001006 if (signr == -1)
1007 return;
1008
1009 signal(signr, SIG_DFL);
1010 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001011}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001012
Paul A. Clarked778a772020-05-20 11:23:35 -05001013void perf_stat__set_big_num(int set)
1014{
1015 stat_config.big_num = (set != 0);
1016}
1017
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001018static int stat__set_big_num(const struct option *opt __maybe_unused,
1019 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001020{
1021 big_num_opt = unset ? 0 : 1;
Paul A. Clarked778a772020-05-20 11:23:35 -05001022 perf_stat__set_big_num(!unset);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001023 return 0;
1024}
1025
Andi Kleen44b1e602016-05-30 12:49:42 -03001026static int enable_metric_only(const struct option *opt __maybe_unused,
1027 const char *s __maybe_unused, int unset)
1028{
1029 force_metric_only = true;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001030 stat_config.metric_only = !unset;
Andi Kleen44b1e602016-05-30 12:49:42 -03001031 return 0;
1032}
1033
Andi Kleenb18f3e32017-08-31 12:40:31 -07001034static int parse_metric_groups(const struct option *opt,
1035 const char *str,
1036 int unset __maybe_unused)
1037{
Ian Rogers05530a72020-05-20 11:20:10 -07001038 return metricgroup__parse_groups(opt, str,
1039 stat_config.metric_no_group,
1040 stat_config.metric_no_merge,
1041 &stat_config.metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -07001042}
1043
Alexey Budankov27e97692020-07-17 10:05:41 +03001044static int parse_control_option(const struct option *opt,
1045 const char *str,
1046 int unset __maybe_unused)
1047{
Adrian Hunter9864a662020-09-01 12:37:53 +03001048 struct perf_stat_config *config = opt->value;
Alexey Budankov27e97692020-07-17 10:05:41 +03001049
Adrian Huntera8fcbd22020-09-02 13:57:07 +03001050 return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
1051}
1052
1053static void close_control_option(struct perf_stat_config *config)
1054{
1055 if (config->ctl_fd_close) {
1056 config->ctl_fd_close = false;
1057 close(config->ctl_fd);
1058 if (config->ctl_fd_ack >= 0)
1059 close(config->ctl_fd_ack);
1060 }
Alexey Budankov27e97692020-07-17 10:05:41 +03001061}
1062
Michael Petlan51433ea2018-12-10 11:00:04 -05001063static struct option stat_options[] = {
Jiri Olsae0547312015-11-05 15:40:45 +01001064 OPT_BOOLEAN('T', "transaction", &transaction_run,
1065 "hardware transaction statistics"),
1066 OPT_CALLBACK('e', "event", &evsel_list, "event",
1067 "event selector. use 'perf list' to list available events",
1068 parse_events_option),
1069 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1070 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001071 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001072 "child tasks do not inherit counters"),
1073 OPT_STRING('p', "pid", &target.pid, "pid",
1074 "stat events on existing process id"),
1075 OPT_STRING('t', "tid", &target.tid, "tid",
1076 "stat events on existing thread id"),
1077 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1078 "system-wide collection from all CPUs"),
1079 OPT_BOOLEAN('g', "group", &group,
1080 "put the counters into a counter group"),
Andi Kleen75998bb2019-03-14 15:50:01 -07001081 OPT_BOOLEAN(0, "scale", &stat_config.scale,
1082 "Use --no-scale to disable counter scaling for multiplexing"),
Jiri Olsae0547312015-11-05 15:40:45 +01001083 OPT_INCR('v', "verbose", &verbose,
1084 "be more verbose (show counter open errors, etc)"),
Jiri Olsad97ae042018-08-30 08:32:36 +02001085 OPT_INTEGER('r', "repeat", &stat_config.run_count,
Jiri Olsae0547312015-11-05 15:40:45 +01001086 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsa54ac0b12018-08-30 08:32:50 +02001087 OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
Jiri Olsae55c14a2018-04-23 11:08:21 +02001088 "display details about each run (only with -r option)"),
Jiri Olsaaea0dca2018-08-30 08:32:41 +02001089 OPT_BOOLEAN('n', "null", &stat_config.null_run,
Jiri Olsae0547312015-11-05 15:40:45 +01001090 "null run - dont start any counters"),
1091 OPT_INCR('d', "detailed", &detailed_run,
1092 "detailed run - start a lot of events"),
1093 OPT_BOOLEAN('S', "sync", &sync_run,
1094 "call sync() before starting a run"),
1095 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1096 "print large numbers with thousands\' separators",
1097 stat__set_big_num),
1098 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1099 "list of cpus to monitor in system-wide"),
1100 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1101 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsafdee3352018-08-30 08:32:48 +02001102 OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"),
Jiri Olsafa7070a2018-08-30 08:32:29 +02001103 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
Jiri Olsae0547312015-11-05 15:40:45 +01001104 "print counts with custom separator"),
1105 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1106 "monitor event in cgroup name only", parse_cgroups),
1107 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1108 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1109 OPT_INTEGER(0, "log-fd", &output_fd,
1110 "log output to fd, instead of stderr"),
1111 OPT_STRING(0, "pre", &pre_cmd, "command",
1112 "command to run prior to the measured command"),
1113 OPT_STRING(0, "post", &post_cmd, "command",
1114 "command to run after to the measured command"),
1115 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001116 "print counts at regular interval in ms "
1117 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001118 OPT_INTEGER(0, "interval-count", &stat_config.times,
1119 "print counts for fixed number of times"),
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001120 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
Jiri Olsa9660e082018-06-07 00:15:06 +02001121 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001122 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1123 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001124 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1125 "aggregate counts per processor socket", AGGR_SOCKET),
Kan Liangdb5742b2019-06-04 15:50:42 -07001126 OPT_SET_UINT(0, "per-die", &stat_config.aggr_mode,
1127 "aggregate counts per processor die", AGGR_DIE),
Jiri Olsae0547312015-11-05 15:40:45 +01001128 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1129 "aggregate counts per physical processor core", AGGR_CORE),
1130 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1131 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa86895b42019-08-28 10:17:43 +02001132 OPT_SET_UINT(0, "per-node", &stat_config.aggr_mode,
1133 "aggregate counts per numa node", AGGR_NODE),
Alexey Budankov2162b9c2020-07-17 10:04:33 +03001134 OPT_INTEGER('D', "delay", &stat_config.initial_delay,
1135 "ms to wait before starting measurement after program start (-1: start with events disabled)"),
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001136 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
Andi Kleen44b1e602016-05-30 12:49:42 -03001137 "Only print computed metrics. No raw values", enable_metric_only),
Ian Rogers05530a72020-05-20 11:20:10 -07001138 OPT_BOOLEAN(0, "metric-no-group", &stat_config.metric_no_group,
1139 "don't group metric events, impacts multiplexing"),
1140 OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge,
1141 "don't try to share events between metrics in a group"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001142 OPT_BOOLEAN(0, "topdown", &topdown_run,
1143 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001144 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1145 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001146 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1147 "monitor specified metrics or metric groups (separated by ,)",
1148 parse_metric_groups),
Jin Yaodd071022019-10-11 13:05:45 +08001149 OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel,
1150 "Configure all used events to run in kernel space.",
1151 PARSE_OPT_EXCLUSIVE),
1152 OPT_BOOLEAN_FLAG(0, "all-user", &stat_config.all_user,
1153 "Configure all used events to run in user space.",
1154 PARSE_OPT_EXCLUSIVE),
Jin Yao1af62ce2020-02-14 16:04:52 +08001155 OPT_BOOLEAN(0, "percore-show-thread", &stat_config.percore_show_thread,
1156 "Use with 'percore' event qualifier to show the event "
1157 "counts of one hardware thread by sum up total hardware "
1158 "threads of same physical core"),
Jin Yaoee6a9612020-09-03 09:01:13 +08001159 OPT_BOOLEAN(0, "summary", &stat_config.summary,
1160 "print summary for interval mode"),
Stephane Eranian70943492020-05-05 11:29:43 -07001161#ifdef HAVE_LIBPFM
1162 OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
1163 "libpfm4 event selector. use 'perf list' to list available events",
1164 parse_libpfm_events_option),
1165#endif
Adrian Huntera8fcbd22020-09-02 13:57:07 +03001166 OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
Alexey Budankov27e97692020-07-17 10:05:41 +03001167 "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
Adrian Huntera8fcbd22020-09-02 13:57:07 +03001168 "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
1169 "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
Alexey Budankov27e97692020-07-17 10:05:41 +03001170 parse_control_option),
Jiri Olsae0547312015-11-05 15:40:45 +01001171 OPT_END()
1172};
1173
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001174static int perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
Jiri Olsaf8548392019-07-21 13:23:49 +02001175 struct perf_cpu_map *map, int cpu)
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001176{
1177 return cpu_map__get_socket(map, cpu, NULL);
1178}
1179
Kan Liangdb5742b2019-06-04 15:50:42 -07001180static int perf_stat__get_die(struct perf_stat_config *config __maybe_unused,
Jiri Olsaf8548392019-07-21 13:23:49 +02001181 struct perf_cpu_map *map, int cpu)
Kan Liangdb5742b2019-06-04 15:50:42 -07001182{
1183 return cpu_map__get_die(map, cpu, NULL);
1184}
1185
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001186static int perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
Jiri Olsaf8548392019-07-21 13:23:49 +02001187 struct perf_cpu_map *map, int cpu)
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001188{
1189 return cpu_map__get_core(map, cpu, NULL);
1190}
1191
Jiri Olsa86895b42019-08-28 10:17:43 +02001192static int perf_stat__get_node(struct perf_stat_config *config __maybe_unused,
1193 struct perf_cpu_map *map, int cpu)
1194{
1195 return cpu_map__get_node(map, cpu, NULL);
1196}
1197
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001198static int perf_stat__get_aggr(struct perf_stat_config *config,
Jiri Olsaf8548392019-07-21 13:23:49 +02001199 aggr_get_id_t get_id, struct perf_cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001200{
1201 int cpu;
1202
1203 if (idx >= map->nr)
1204 return -1;
1205
1206 cpu = map->map[idx];
1207
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001208 if (config->cpus_aggr_map->map[cpu] == -1)
1209 config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001210
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001211 return config->cpus_aggr_map->map[cpu];
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001212}
1213
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001214static int perf_stat__get_socket_cached(struct perf_stat_config *config,
Jiri Olsaf8548392019-07-21 13:23:49 +02001215 struct perf_cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001216{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001217 return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001218}
1219
Kan Liangdb5742b2019-06-04 15:50:42 -07001220static int perf_stat__get_die_cached(struct perf_stat_config *config,
Jiri Olsaf8548392019-07-21 13:23:49 +02001221 struct perf_cpu_map *map, int idx)
Kan Liangdb5742b2019-06-04 15:50:42 -07001222{
1223 return perf_stat__get_aggr(config, perf_stat__get_die, map, idx);
1224}
1225
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001226static int perf_stat__get_core_cached(struct perf_stat_config *config,
Jiri Olsaf8548392019-07-21 13:23:49 +02001227 struct perf_cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001228{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001229 return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001230}
1231
Jiri Olsa86895b42019-08-28 10:17:43 +02001232static int perf_stat__get_node_cached(struct perf_stat_config *config,
1233 struct perf_cpu_map *map, int idx)
1234{
1235 return perf_stat__get_aggr(config, perf_stat__get_node, map, idx);
1236}
1237
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001238static bool term_percore_set(void)
1239{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001240 struct evsel *counter;
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001241
1242 evlist__for_each_entry(evsel_list, counter) {
1243 if (counter->percore)
1244 return true;
1245 }
1246
1247 return false;
1248}
1249
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001250static int perf_stat_init_aggr_mode(void)
1251{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001252 int nr;
1253
Jiri Olsa421a50f2015-07-21 14:31:22 +02001254 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001255 case AGGR_SOCKET:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001256 if (cpu_map__build_socket_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001257 perror("cannot build socket map");
1258 return -1;
1259 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001260 stat_config.aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001261 break;
Kan Liangdb5742b2019-06-04 15:50:42 -07001262 case AGGR_DIE:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001263 if (cpu_map__build_die_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
Kan Liangdb5742b2019-06-04 15:50:42 -07001264 perror("cannot build die map");
1265 return -1;
1266 }
1267 stat_config.aggr_get_id = perf_stat__get_die_cached;
1268 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001269 case AGGR_CORE:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001270 if (cpu_map__build_core_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001271 perror("cannot build core map");
1272 return -1;
1273 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001274 stat_config.aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001275 break;
Jiri Olsa86895b42019-08-28 10:17:43 +02001276 case AGGR_NODE:
1277 if (cpu_map__build_node_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
1278 perror("cannot build core map");
1279 return -1;
1280 }
1281 stat_config.aggr_get_id = perf_stat__get_node_cached;
1282 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001283 case AGGR_NONE:
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001284 if (term_percore_set()) {
Jiri Olsaf72f9012019-07-21 13:24:41 +02001285 if (cpu_map__build_core_map(evsel_list->core.cpus,
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001286 &stat_config.aggr_map)) {
1287 perror("cannot build core map");
1288 return -1;
1289 }
1290 stat_config.aggr_get_id = perf_stat__get_core_cached;
1291 }
1292 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001293 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001294 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001295 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001296 default:
1297 break;
1298 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001299
1300 /*
1301 * The evsel_list->cpus is the base we operate on,
1302 * taking the highest cpu number to be the size of
1303 * the aggregation translate cpumap.
1304 */
Jiri Olsa4256d432019-09-02 14:12:53 +02001305 nr = perf_cpu_map__max(evsel_list->core.cpus);
Jiri Olsa315c0a12019-08-22 13:11:39 +02001306 stat_config.cpus_aggr_map = perf_cpu_map__empty_new(nr + 1);
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001307 return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001308}
1309
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001310static void perf_stat__exit_aggr_mode(void)
1311{
Jiri Olsa38f01d82019-07-21 13:24:17 +02001312 perf_cpu_map__put(stat_config.aggr_map);
1313 perf_cpu_map__put(stat_config.cpus_aggr_map);
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001314 stat_config.aggr_map = NULL;
1315 stat_config.cpus_aggr_map = NULL;
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001316}
1317
Jiri Olsaf8548392019-07-21 13:23:49 +02001318static inline int perf_env__get_cpu(struct perf_env *env, struct perf_cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001319{
1320 int cpu;
1321
1322 if (idx > map->nr)
1323 return -1;
1324
1325 cpu = map->map[idx];
1326
Jan Stancekda8a58b2017-02-17 12:10:26 +01001327 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001328 return -1;
1329
1330 return cpu;
1331}
1332
Jiri Olsaf8548392019-07-21 13:23:49 +02001333static int perf_env__get_socket(struct perf_cpu_map *map, int idx, void *data)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001334{
1335 struct perf_env *env = data;
1336 int cpu = perf_env__get_cpu(env, map, idx);
1337
1338 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1339}
1340
Jiri Olsaf8548392019-07-21 13:23:49 +02001341static int perf_env__get_die(struct perf_cpu_map *map, int idx, void *data)
Kan Liangdb5742b2019-06-04 15:50:42 -07001342{
1343 struct perf_env *env = data;
1344 int die_id = -1, cpu = perf_env__get_cpu(env, map, idx);
1345
1346 if (cpu != -1) {
1347 /*
1348 * Encode socket in bit range 15:8
1349 * die_id is relative to socket,
1350 * we need a global id. So we combine
1351 * socket + die id
1352 */
1353 if (WARN_ONCE(env->cpu[cpu].socket_id >> 8, "The socket id number is too big.\n"))
1354 return -1;
1355
1356 if (WARN_ONCE(env->cpu[cpu].die_id >> 8, "The die id number is too big.\n"))
1357 return -1;
1358
1359 die_id = (env->cpu[cpu].socket_id << 8) | (env->cpu[cpu].die_id & 0xff);
1360 }
1361
1362 return die_id;
1363}
1364
Jiri Olsaf8548392019-07-21 13:23:49 +02001365static int perf_env__get_core(struct perf_cpu_map *map, int idx, void *data)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001366{
1367 struct perf_env *env = data;
1368 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1369
1370 if (cpu != -1) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01001371 /*
Kan Liangdb5742b2019-06-04 15:50:42 -07001372 * Encode socket in bit range 31:24
1373 * encode die id in bit range 23:16
1374 * core_id is relative to socket and die,
Jiri Olsa68d702f2015-11-05 15:40:58 +01001375 * we need a global id. So we combine
Kan Liangdb5742b2019-06-04 15:50:42 -07001376 * socket + die id + core id
Jiri Olsa68d702f2015-11-05 15:40:58 +01001377 */
Kan Liangdb5742b2019-06-04 15:50:42 -07001378 if (WARN_ONCE(env->cpu[cpu].socket_id >> 8, "The socket id number is too big.\n"))
1379 return -1;
1380
1381 if (WARN_ONCE(env->cpu[cpu].die_id >> 8, "The die id number is too big.\n"))
1382 return -1;
1383
1384 if (WARN_ONCE(env->cpu[cpu].core_id >> 16, "The core id number is too big.\n"))
1385 return -1;
1386
1387 core = (env->cpu[cpu].socket_id << 24) |
1388 (env->cpu[cpu].die_id << 16) |
1389 (env->cpu[cpu].core_id & 0xffff);
Jiri Olsa68d702f2015-11-05 15:40:58 +01001390 }
1391
1392 return core;
1393}
1394
Jiri Olsa86895b42019-08-28 10:17:43 +02001395static int perf_env__get_node(struct perf_cpu_map *map, int idx, void *data)
1396{
1397 int cpu = perf_env__get_cpu(data, map, idx);
1398
1399 return perf_env__numa_node(data, cpu);
1400}
1401
Jiri Olsaf8548392019-07-21 13:23:49 +02001402static int perf_env__build_socket_map(struct perf_env *env, struct perf_cpu_map *cpus,
1403 struct perf_cpu_map **sockp)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001404{
1405 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1406}
1407
Jiri Olsaf8548392019-07-21 13:23:49 +02001408static int perf_env__build_die_map(struct perf_env *env, struct perf_cpu_map *cpus,
1409 struct perf_cpu_map **diep)
Kan Liangdb5742b2019-06-04 15:50:42 -07001410{
1411 return cpu_map__build_map(cpus, diep, perf_env__get_die, env);
1412}
1413
Jiri Olsaf8548392019-07-21 13:23:49 +02001414static int perf_env__build_core_map(struct perf_env *env, struct perf_cpu_map *cpus,
1415 struct perf_cpu_map **corep)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001416{
1417 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1418}
1419
Jiri Olsa86895b42019-08-28 10:17:43 +02001420static int perf_env__build_node_map(struct perf_env *env, struct perf_cpu_map *cpus,
1421 struct perf_cpu_map **nodep)
1422{
1423 return cpu_map__build_map(cpus, nodep, perf_env__get_node, env);
1424}
1425
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001426static int perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
Jiri Olsaf8548392019-07-21 13:23:49 +02001427 struct perf_cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001428{
1429 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1430}
Kan Liangdb5742b2019-06-04 15:50:42 -07001431static int perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused,
Jiri Olsaf8548392019-07-21 13:23:49 +02001432 struct perf_cpu_map *map, int idx)
Kan Liangdb5742b2019-06-04 15:50:42 -07001433{
1434 return perf_env__get_die(map, idx, &perf_stat.session->header.env);
1435}
Jiri Olsa68d702f2015-11-05 15:40:58 +01001436
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001437static int perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
Jiri Olsaf8548392019-07-21 13:23:49 +02001438 struct perf_cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001439{
1440 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1441}
1442
Jiri Olsa86895b42019-08-28 10:17:43 +02001443static int perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused,
1444 struct perf_cpu_map *map, int idx)
1445{
1446 return perf_env__get_node(map, idx, &perf_stat.session->header.env);
1447}
1448
Jiri Olsa68d702f2015-11-05 15:40:58 +01001449static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1450{
1451 struct perf_env *env = &st->session->header.env;
1452
1453 switch (stat_config.aggr_mode) {
1454 case AGGR_SOCKET:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001455 if (perf_env__build_socket_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01001456 perror("cannot build socket map");
1457 return -1;
1458 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001459 stat_config.aggr_get_id = perf_stat__get_socket_file;
Jiri Olsa68d702f2015-11-05 15:40:58 +01001460 break;
Kan Liangdb5742b2019-06-04 15:50:42 -07001461 case AGGR_DIE:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001462 if (perf_env__build_die_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
Kan Liangdb5742b2019-06-04 15:50:42 -07001463 perror("cannot build die map");
1464 return -1;
1465 }
1466 stat_config.aggr_get_id = perf_stat__get_die_file;
1467 break;
Jiri Olsa68d702f2015-11-05 15:40:58 +01001468 case AGGR_CORE:
Jiri Olsaf72f9012019-07-21 13:24:41 +02001469 if (perf_env__build_core_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01001470 perror("cannot build core map");
1471 return -1;
1472 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001473 stat_config.aggr_get_id = perf_stat__get_core_file;
Jiri Olsa68d702f2015-11-05 15:40:58 +01001474 break;
Jiri Olsa86895b42019-08-28 10:17:43 +02001475 case AGGR_NODE:
1476 if (perf_env__build_node_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
1477 perror("cannot build core map");
1478 return -1;
1479 }
1480 stat_config.aggr_get_id = perf_stat__get_node_file;
1481 break;
Jiri Olsa68d702f2015-11-05 15:40:58 +01001482 case AGGR_NONE:
1483 case AGGR_GLOBAL:
1484 case AGGR_THREAD:
1485 case AGGR_UNSET:
1486 default:
1487 break;
1488 }
1489
1490 return 0;
1491}
1492
Andi Kleen44b1e602016-05-30 12:49:42 -03001493static int topdown_filter_events(const char **attr, char **str, bool use_group)
1494{
1495 int off = 0;
1496 int i;
1497 int len = 0;
1498 char *s;
1499
1500 for (i = 0; attr[i]; i++) {
1501 if (pmu_have_event("cpu", attr[i])) {
1502 len += strlen(attr[i]) + 1;
1503 attr[i - off] = attr[i];
1504 } else
1505 off++;
1506 }
1507 attr[i - off] = NULL;
1508
1509 *str = malloc(len + 1 + 2);
1510 if (!*str)
1511 return -1;
1512 s = *str;
1513 if (i - off == 0) {
1514 *s = 0;
1515 return 0;
1516 }
1517 if (use_group)
1518 *s++ = '{';
1519 for (i = 0; attr[i]; i++) {
1520 strcpy(s, attr[i]);
1521 s += strlen(s);
1522 *s++ = ',';
1523 }
1524 if (use_group) {
1525 s[-1] = '}';
1526 *s = 0;
1527 } else
1528 s[-1] = 0;
1529 return 0;
1530}
1531
1532__weak bool arch_topdown_check_group(bool *warn)
1533{
1534 *warn = false;
1535 return false;
1536}
1537
1538__weak void arch_topdown_group_warn(void)
1539{
1540}
1541
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001542/*
1543 * Add default attributes, if there were no attributes specified or
1544 * if -d/--detailed, -d -d or -d -d -d is used:
1545 */
1546static int add_default_attributes(void)
1547{
Andi Kleen44b1e602016-05-30 12:49:42 -03001548 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08001549 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001550
1551 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1552 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1553 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1554 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1555
1556 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001557};
1558 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001559 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001560};
1561 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001562 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001563};
1564 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001565 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1566 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1567 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1568
1569};
1570
1571/*
1572 * Detailed stats (-d), covering the L1 and last level data caches:
1573 */
1574 struct perf_event_attr detailed_attrs[] = {
1575
1576 { .type = PERF_TYPE_HW_CACHE,
1577 .config =
1578 PERF_COUNT_HW_CACHE_L1D << 0 |
1579 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1580 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1581
1582 { .type = PERF_TYPE_HW_CACHE,
1583 .config =
1584 PERF_COUNT_HW_CACHE_L1D << 0 |
1585 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1586 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1587
1588 { .type = PERF_TYPE_HW_CACHE,
1589 .config =
1590 PERF_COUNT_HW_CACHE_LL << 0 |
1591 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1592 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1593
1594 { .type = PERF_TYPE_HW_CACHE,
1595 .config =
1596 PERF_COUNT_HW_CACHE_LL << 0 |
1597 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1598 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1599};
1600
1601/*
1602 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1603 */
1604 struct perf_event_attr very_detailed_attrs[] = {
1605
1606 { .type = PERF_TYPE_HW_CACHE,
1607 .config =
1608 PERF_COUNT_HW_CACHE_L1I << 0 |
1609 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1610 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1611
1612 { .type = PERF_TYPE_HW_CACHE,
1613 .config =
1614 PERF_COUNT_HW_CACHE_L1I << 0 |
1615 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1616 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1617
1618 { .type = PERF_TYPE_HW_CACHE,
1619 .config =
1620 PERF_COUNT_HW_CACHE_DTLB << 0 |
1621 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1622 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1623
1624 { .type = PERF_TYPE_HW_CACHE,
1625 .config =
1626 PERF_COUNT_HW_CACHE_DTLB << 0 |
1627 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1628 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1629
1630 { .type = PERF_TYPE_HW_CACHE,
1631 .config =
1632 PERF_COUNT_HW_CACHE_ITLB << 0 |
1633 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1634 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1635
1636 { .type = PERF_TYPE_HW_CACHE,
1637 .config =
1638 PERF_COUNT_HW_CACHE_ITLB << 0 |
1639 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1640 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1641
1642};
1643
1644/*
1645 * Very, very detailed stats (-d -d -d), adding prefetch events:
1646 */
1647 struct perf_event_attr very_very_detailed_attrs[] = {
1648
1649 { .type = PERF_TYPE_HW_CACHE,
1650 .config =
1651 PERF_COUNT_HW_CACHE_L1D << 0 |
1652 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1653 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1654
1655 { .type = PERF_TYPE_HW_CACHE,
1656 .config =
1657 PERF_COUNT_HW_CACHE_L1D << 0 |
1658 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1659 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1660};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02001661 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001662
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001663 /* Set attrs if no event is selected and !null_run: */
Jiri Olsaaea0dca2018-08-30 08:32:41 +02001664 if (stat_config.null_run)
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001665 return 0;
1666
Ian Rogersa910e462019-11-15 23:46:52 -08001667 bzero(&errinfo, sizeof(errinfo));
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001668 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02001669 /* Handle -T as -M transaction. Once platform specific metrics
1670 * support has been added to the json files, all archictures
1671 * will use this approach. To determine transaction support
1672 * on an architecture test for such a metric name.
1673 */
1674 if (metricgroup__has_metric("transaction")) {
1675 struct option opt = { .value = &evsel_list };
1676
1677 return metricgroup__parse_groups(&opt, "transaction",
Ian Rogers05530a72020-05-20 11:20:10 -07001678 stat_config.metric_no_group,
1679 stat_config.metric_no_merge,
Jiri Olsad0192fd2018-08-30 08:32:51 +02001680 &stat_config.metric_events);
Thomas Richter742d92f2018-06-26 09:17:01 +02001681 }
1682
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001683 if (pmu_have_event("cpu", "cycles-ct") &&
1684 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01001685 err = parse_events(evsel_list, transaction_attrs,
1686 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001687 else
Thomas Richterfca323402018-03-08 15:57:35 +01001688 err = parse_events(evsel_list,
1689 transaction_limited_attrs,
1690 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02001691 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001692 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02001693 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001694 return -1;
1695 }
1696 return 0;
1697 }
1698
Kan Liangdaefd0b2017-05-26 12:05:38 -07001699 if (smi_cost) {
1700 int smi;
1701
1702 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
1703 fprintf(stderr, "freeze_on_smi is not supported.\n");
1704 return -1;
1705 }
1706
1707 if (!smi) {
1708 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
1709 fprintf(stderr, "Failed to set freeze_on_smi.\n");
1710 return -1;
1711 }
1712 smi_reset = true;
1713 }
1714
1715 if (pmu_have_event("msr", "aperf") &&
1716 pmu_have_event("msr", "smi")) {
1717 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001718 stat_config.metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02001719 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07001720 } else {
1721 fprintf(stderr, "To measure SMI cost, it needs "
1722 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02001723 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07001724 return -1;
1725 }
1726 if (err) {
Ian Rogersa910e462019-11-15 23:46:52 -08001727 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07001728 fprintf(stderr, "Cannot set up SMI cost events\n");
1729 return -1;
1730 }
1731 return 0;
1732 }
1733
Andi Kleen44b1e602016-05-30 12:49:42 -03001734 if (topdown_run) {
1735 char *str = NULL;
1736 bool warn = false;
1737
1738 if (stat_config.aggr_mode != AGGR_GLOBAL &&
1739 stat_config.aggr_mode != AGGR_CORE) {
1740 pr_err("top down event configuration requires --per-core mode\n");
1741 return -1;
1742 }
1743 stat_config.aggr_mode = AGGR_CORE;
1744 if (nr_cgroups || !target__has_cpu(&target)) {
1745 pr_err("top down event configuration requires system-wide mode (-a)\n");
1746 return -1;
1747 }
1748
1749 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001750 stat_config.metric_only = true;
Andi Kleen44b1e602016-05-30 12:49:42 -03001751 if (topdown_filter_events(topdown_attrs, &str,
1752 arch_topdown_check_group(&warn)) < 0) {
1753 pr_err("Out of memory\n");
1754 return -1;
1755 }
1756 if (topdown_attrs[0] && str) {
1757 if (warn)
1758 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02001759 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03001760 if (err) {
1761 fprintf(stderr,
1762 "Cannot set up top down events %s: %d\n",
1763 str, err);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02001764 parse_events_print_error(&errinfo, str);
Leo Yanc74b0502019-07-02 18:34:11 +08001765 free(str);
Andi Kleen44b1e602016-05-30 12:49:42 -03001766 return -1;
1767 }
1768 } else {
1769 fprintf(stderr, "System does not support topdown\n");
1770 return -1;
1771 }
1772 free(str);
1773 }
1774
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001775 if (!evsel_list->core.nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09001776 if (target__has_cpu(&target))
1777 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
1778
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001779 if (evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
Andi Kleen9dec4472016-02-26 16:27:56 -08001780 return -1;
1781 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001782 if (evlist__add_default_attrs(evsel_list, frontend_attrs) < 0)
Andi Kleen9dec4472016-02-26 16:27:56 -08001783 return -1;
1784 }
1785 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001786 if (evlist__add_default_attrs(evsel_list, backend_attrs) < 0)
Andi Kleen9dec4472016-02-26 16:27:56 -08001787 return -1;
1788 }
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001789 if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001790 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001791 }
1792
1793 /* Detailed events get appended to the event list: */
1794
1795 if (detailed_run < 1)
1796 return 0;
1797
1798 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001799 if (evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001800 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001801
1802 if (detailed_run < 2)
1803 return 0;
1804
1805 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001806 if (evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001807 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001808
1809 if (detailed_run < 3)
1810 return 0;
1811
1812 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Meloe251abe2020-06-17 09:16:20 -03001813 return evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001814}
1815
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001816static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001817 "perf stat record [<options>]",
1818 NULL,
1819};
1820
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001821static void init_features(struct perf_session *session)
1822{
1823 int feat;
1824
1825 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1826 perf_header__set_feat(&session->header, feat);
1827
Jiri Olsa8002a632019-04-09 12:01:56 +02001828 perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001829 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1830 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1831 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1832 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1833}
1834
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001835static int __cmd_record(int argc, const char **argv)
1836{
1837 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001838 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001839
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001840 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001841 PARSE_OPT_STOP_AT_NON_OPTION);
1842
1843 if (output_name)
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001844 data->path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001845
Jiri Olsad97ae042018-08-30 08:32:36 +02001846 if (stat_config.run_count != 1 || forever) {
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01001847 pr_err("Cannot use -r option with perf stat record.\n");
1848 return -1;
1849 }
1850
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001851 session = perf_session__new(data, false, NULL);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05301852 if (IS_ERR(session)) {
1853 pr_err("Perf session creation failed\n");
1854 return PTR_ERR(session);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001855 }
1856
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001857 init_features(session);
1858
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001859 session->evlist = evsel_list;
1860 perf_stat.session = session;
1861 perf_stat.record = true;
1862 return argc;
1863}
1864
Jiri Olsa89f16882018-09-13 14:54:03 +02001865static int process_stat_round_event(struct perf_session *session,
1866 union perf_event *event)
Jiri Olsaa56f9392015-11-05 15:40:59 +01001867{
Jiri Olsa72932372019-08-28 15:57:16 +02001868 struct perf_record_stat_round *stat_round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001869 struct evsel *counter;
Jiri Olsaa56f9392015-11-05 15:40:59 +01001870 struct timespec tsh, *ts = NULL;
1871 const char **argv = session->header.env.cmdline_argv;
1872 int argc = session->header.env.nr_cmdline;
1873
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001874 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01001875 perf_stat_process_counter(&stat_config, counter);
1876
Andi Kleene3b03b62016-05-05 16:04:03 -07001877 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
1878 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01001879
Andi Kleene3b03b62016-05-05 16:04:03 -07001880 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03001881 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
1882 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01001883 ts = &tsh;
1884 }
1885
1886 print_counters(ts, argc, argv);
1887 return 0;
1888}
1889
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001890static
Jiri Olsa89f16882018-09-13 14:54:03 +02001891int process_stat_config_event(struct perf_session *session,
1892 union perf_event *event)
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001893{
Jiri Olsa89f16882018-09-13 14:54:03 +02001894 struct perf_tool *tool = session->tool;
Jiri Olsa68d702f2015-11-05 15:40:58 +01001895 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1896
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001897 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01001898
Jiri Olsa315c0a12019-08-22 13:11:39 +02001899 if (perf_cpu_map__empty(st->cpus)) {
Jiri Olsa89af4e02015-11-05 15:41:02 +01001900 if (st->aggr_mode != AGGR_UNSET)
1901 pr_warning("warning: processing task data, aggregation mode not set\n");
1902 return 0;
1903 }
1904
1905 if (st->aggr_mode != AGGR_UNSET)
1906 stat_config.aggr_mode = st->aggr_mode;
1907
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001908 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001909 perf_stat_init_aggr_mode();
1910 else
1911 perf_stat_init_aggr_mode_file(st);
1912
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001913 return 0;
1914}
1915
Jiri Olsa1975d362015-11-05 15:40:56 +01001916static int set_maps(struct perf_stat *st)
1917{
1918 if (!st->cpus || !st->threads)
1919 return 0;
1920
1921 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
1922 return -EINVAL;
1923
Jiri Olsa453fa032019-07-21 13:24:43 +02001924 perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads);
Jiri Olsa1975d362015-11-05 15:40:56 +01001925
1926 if (perf_evlist__alloc_stats(evsel_list, true))
1927 return -ENOMEM;
1928
1929 st->maps_allocated = true;
1930 return 0;
1931}
1932
1933static
Jiri Olsa89f16882018-09-13 14:54:03 +02001934int process_thread_map_event(struct perf_session *session,
1935 union perf_event *event)
Jiri Olsa1975d362015-11-05 15:40:56 +01001936{
Jiri Olsa89f16882018-09-13 14:54:03 +02001937 struct perf_tool *tool = session->tool;
Jiri Olsa1975d362015-11-05 15:40:56 +01001938 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1939
1940 if (st->threads) {
1941 pr_warning("Extra thread map event, ignoring.\n");
1942 return 0;
1943 }
1944
1945 st->threads = thread_map__new_event(&event->thread_map);
1946 if (!st->threads)
1947 return -ENOMEM;
1948
1949 return set_maps(st);
1950}
1951
1952static
Jiri Olsa89f16882018-09-13 14:54:03 +02001953int process_cpu_map_event(struct perf_session *session,
1954 union perf_event *event)
Jiri Olsa1975d362015-11-05 15:40:56 +01001955{
Jiri Olsa89f16882018-09-13 14:54:03 +02001956 struct perf_tool *tool = session->tool;
Jiri Olsa1975d362015-11-05 15:40:56 +01001957 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
Jiri Olsaf8548392019-07-21 13:23:49 +02001958 struct perf_cpu_map *cpus;
Jiri Olsa1975d362015-11-05 15:40:56 +01001959
1960 if (st->cpus) {
1961 pr_warning("Extra cpu map event, ignoring.\n");
1962 return 0;
1963 }
1964
1965 cpus = cpu_map__new_data(&event->cpu_map.data);
1966 if (!cpus)
1967 return -ENOMEM;
1968
1969 st->cpus = cpus;
1970 return set_maps(st);
1971}
1972
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001973static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001974 "perf stat report [<options>]",
1975 NULL,
1976};
1977
1978static struct perf_stat perf_stat = {
1979 .tool = {
1980 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01001981 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01001982 .thread_map = process_thread_map_event,
1983 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001984 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01001985 .stat = perf_event__process_stat_event,
1986 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001987 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01001988 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001989};
1990
1991static int __cmd_report(int argc, const char **argv)
1992{
1993 struct perf_session *session;
1994 const struct option options[] = {
1995 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01001996 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
1997 "aggregate counts per processor socket", AGGR_SOCKET),
Kan Liangdb5742b2019-06-04 15:50:42 -07001998 OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode,
1999 "aggregate counts per processor die", AGGR_DIE),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002000 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2001 "aggregate counts per physical processor core", AGGR_CORE),
Jiri Olsa86895b42019-08-28 10:17:43 +02002002 OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode,
2003 "aggregate counts per numa node", AGGR_NODE),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002004 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2005 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002006 OPT_END()
2007 };
2008 struct stat st;
2009 int ret;
2010
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002011 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002012
2013 if (!input_name || !strlen(input_name)) {
2014 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2015 input_name = "-";
2016 else
2017 input_name = "perf.data";
2018 }
2019
Jiri Olsa2d4f2792019-02-21 10:41:30 +01002020 perf_stat.data.path = input_name;
2021 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002022
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002023 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05302024 if (IS_ERR(session))
2025 return PTR_ERR(session);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002026
2027 perf_stat.session = session;
2028 stat_config.output = stderr;
2029 evsel_list = session->evlist;
2030
2031 ret = perf_session__process_events(session);
2032 if (ret)
2033 return ret;
2034
2035 perf_session__delete(session);
2036 return 0;
2037}
2038
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002039static void setup_system_wide(int forks)
2040{
2041 /*
2042 * Make system wide (-a) the default target if
2043 * no target was specified and one of following
2044 * conditions is met:
2045 *
2046 * - there's no workload specified
2047 * - there is workload specified but all requested
2048 * events are system wide events
2049 */
2050 if (!target__none(&target))
2051 return;
2052
2053 if (!forks)
2054 target.system_wide = true;
2055 else {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002056 struct evsel *counter;
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002057
2058 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa648b5af2019-08-06 11:35:19 +02002059 if (!counter->core.system_wide)
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002060 return;
2061 }
2062
Jiri Olsa6484d2f2019-07-21 13:24:28 +02002063 if (evsel_list->core.nr_entries)
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002064 target.system_wide = true;
2065 }
2066}
2067
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002068int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002069{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002070 const char * const stat_usage[] = {
2071 "perf stat [<options>] [<command>]",
2072 NULL
2073 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002074 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002075 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002076 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002077 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002078 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002079
Stephane Eranian5af52b52010-05-18 15:00:01 +02002080 setlocale(LC_ALL, "");
2081
Jiri Olsa0f98b112019-07-21 13:23:55 +02002082 evsel_list = evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002083 if (evsel_list == NULL)
2084 return -ENOMEM;
2085
Wang Nan1669e502016-02-19 11:43:58 +00002086 parse_events__shrink_config_terms();
Michael Petlan51433ea2018-12-10 11:00:04 -05002087
2088 /* String-parsing callback-based options would segfault when negated */
2089 set_option_flag(stat_options, 'e', "event", PARSE_OPT_NONEG);
2090 set_option_flag(stat_options, 'M', "metrics", PARSE_OPT_NONEG);
2091 set_option_flag(stat_options, 'G', "cgroup", PARSE_OPT_NONEG);
2092
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002093 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2094 (const char **) stat_usage,
2095 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002096 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002097 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002098
Jiri Olsafa7070a2018-08-30 08:32:29 +02002099 if (stat_config.csv_sep) {
2100 stat_config.csv_output = true;
2101 if (!strcmp(stat_config.csv_sep, "\\t"))
2102 stat_config.csv_sep = "\t";
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002103 } else
Jiri Olsafa7070a2018-08-30 08:32:29 +02002104 stat_config.csv_sep = DEFAULT_SEPARATOR;
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002105
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002106 if (argc && !strncmp(argv[0], "rec", 3)) {
2107 argc = __cmd_record(argc, argv);
2108 if (argc < 0)
2109 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002110 } else if (argc && !strncmp(argv[0], "rep", 3))
2111 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002112
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002113 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002114 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002115
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002116 /*
2117 * For record command the -o is already taken care of.
2118 */
2119 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002120 output = NULL;
2121
Jim Cromie56f3bae2011-09-07 17:14:00 -06002122 if (output_name && output_fd) {
2123 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002124 parse_options_usage(stat_usage, stat_options, "o", 1);
2125 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002126 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002127 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002128
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002129 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
Andi Kleen54b50912016-03-03 15:57:36 -08002130 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2131 goto out;
2132 }
2133
Jiri Olsad97ae042018-08-30 08:32:36 +02002134 if (stat_config.metric_only && stat_config.run_count > 1) {
Andi Kleen54b50912016-03-03 15:57:36 -08002135 fprintf(stderr, "--metric-only is not supported with -r\n");
2136 goto out;
2137 }
2138
Jiri Olsa54ac0b12018-08-30 08:32:50 +02002139 if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02002140 fprintf(stderr, "--table is only supported with -r\n");
2141 parse_options_usage(stat_usage, stat_options, "r", 1);
2142 parse_options_usage(NULL, stat_options, "table", 0);
2143 goto out;
2144 }
2145
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002146 if (output_fd < 0) {
2147 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002148 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002149 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002150 }
2151
Stephane Eranian4aa90152011-08-15 22:22:33 +02002152 if (!output) {
2153 struct timespec tm;
2154 mode = append_file ? "a" : "w";
2155
2156 output = fopen(output_name, mode);
2157 if (!output) {
2158 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002159 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002160 }
2161 clock_gettime(CLOCK_REALTIME, &tm);
2162 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002163 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002164 mode = append_file ? "a" : "w";
2165 output = fdopen(output_fd, mode);
2166 if (!output) {
2167 perror("Failed opening logfd");
2168 return -errno;
2169 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002170 }
2171
Jiri Olsa58215222015-07-21 14:31:24 +02002172 stat_config.output = output;
2173
Stephane Eraniand7470b62010-12-01 18:49:05 +02002174 /*
2175 * let the spreadsheet do the pretty-printing
2176 */
Jiri Olsafa7070a2018-08-30 08:32:29 +02002177 if (stat_config.csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002178 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002179 if (big_num_opt == 1) {
2180 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002181 parse_options_usage(stat_usage, stat_options, "B", 1);
2182 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002183 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002184 } else /* Nope, so disable big number formatting */
Jiri Olsa34ff0862018-08-30 08:32:47 +02002185 stat_config.big_num = false;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002186 } else if (big_num_opt == 0) /* User passed --no-big-num */
Jiri Olsa34ff0862018-08-30 08:32:47 +02002187 stat_config.big_num = false;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002188
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002189 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002190
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002191 /*
2192 * Display user/system times only for single
2193 * run and when there's specified tracee.
2194 */
Jiri Olsad97ae042018-08-30 08:32:36 +02002195 if ((stat_config.run_count == 1) && target__none(&target))
Jiri Olsa8897a892018-08-30 08:32:44 +02002196 stat_config.ru_display = true;
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002197
Jiri Olsad97ae042018-08-30 08:32:36 +02002198 if (stat_config.run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002199 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002200 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002201 goto out;
Jiri Olsad97ae042018-08-30 08:32:36 +02002202 } else if (stat_config.run_count == 0) {
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002203 forever = true;
Jiri Olsad97ae042018-08-30 08:32:36 +02002204 stat_config.run_count = 1;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002205 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002206
Jiri Olsa54ac0b12018-08-30 08:32:50 +02002207 if (stat_config.walltime_run_table) {
2208 stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
2209 if (!stat_config.walltime_run) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02002210 pr_err("failed to setup -r option");
2211 goto out;
2212 }
2213 }
2214
Jin Yao1d9f8d12017-12-05 22:03:10 +08002215 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2216 !target__has_task(&target)) {
2217 if (!target.system_wide || target.cpu_list) {
2218 fprintf(stderr, "The --per-thread option is only "
2219 "available when monitoring via -p -t -a "
2220 "options or only --per-thread.\n");
2221 parse_options_usage(NULL, stat_options, "p", 1);
2222 parse_options_usage(NULL, stat_options, "t", 1);
2223 goto out;
2224 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002225 }
2226
2227 /*
2228 * no_aggr, cgroup are for system-wide only
2229 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2230 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002231 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2232 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002233 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002234 fprintf(stderr, "both cgroup and no-aggregation "
2235 "modes only available in system-wide mode\n");
2236
Jiri Olsae0547312015-11-05 15:40:45 +01002237 parse_options_usage(stat_usage, stat_options, "G", 1);
2238 parse_options_usage(NULL, stat_options, "A", 1);
2239 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002240 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002241 }
2242
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002243 if (add_default_attributes())
2244 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002245
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002246 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002247
Jin Yao1d9f8d12017-12-05 22:03:10 +08002248 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2249 target.per_thread = true;
2250
Namhyung Kim77a6f012012-05-07 14:09:04 +09002251 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002252 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002253 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002254 parse_options_usage(stat_usage, stat_options, "p", 1);
2255 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002256 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002257 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002258 parse_options_usage(stat_usage, stat_options, "C", 1);
2259 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002260 }
2261 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002262 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002263
Jiri Olsaa9a179022020-06-02 12:17:36 +02002264 evlist__check_cpu_maps(evsel_list);
2265
Jiri Olsa32b8af82015-06-26 11:29:27 +02002266 /*
2267 * Initialize thread_map with comm names,
2268 * so we could print it out on output.
2269 */
Jin Yao56739442017-12-05 22:03:07 +08002270 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa03617c22019-07-21 13:24:42 +02002271 thread_map__read_comms(evsel_list->core.threads);
Jin Yao56739442017-12-05 22:03:07 +08002272 if (target.system_wide) {
2273 if (runtime_stat_new(&stat_config,
Jiri Olsaa2f354e2019-08-22 13:11:41 +02002274 perf_thread_map__nr(evsel_list->core.threads))) {
Jin Yao56739442017-12-05 22:03:07 +08002275 goto out;
2276 }
2277 }
2278 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002279
Jiri Olsa86895b42019-08-28 10:17:43 +02002280 if (stat_config.aggr_mode == AGGR_NODE)
2281 cpu__setup_cpunode_map();
2282
yuzhoujiandb06a262018-01-29 10:25:22 +01002283 if (stat_config.times && interval)
2284 interval_count = true;
2285 else if (stat_config.times && !interval) {
2286 pr_err("interval-count option should be used together with "
2287 "interval-print.\n");
2288 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2289 parse_options_usage(stat_usage, stat_options, "I", 1);
2290 goto out;
2291 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002292
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002293 if (timeout && timeout < 100) {
2294 if (timeout < 10) {
2295 pr_err("timeout must be >= 10ms.\n");
2296 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2297 goto out;
2298 } else
2299 pr_warning("timeout < 100ms. "
2300 "The overhead percentage could be high in some cases. "
2301 "Please proceed with caution.\n");
2302 }
2303 if (timeout && interval) {
2304 pr_err("timeout option is not supported with interval-print.\n");
2305 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2306 parse_options_usage(stat_usage, stat_options, "I", 1);
2307 goto out;
2308 }
2309
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002310 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002311 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002312
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002313 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002314 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002315
Ingo Molnar58d7e992009-05-15 11:03:23 +02002316 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002317 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2318 * while avoiding that older tools show confusing messages.
2319 *
2320 * However for pipe sessions we need to keep it zero,
2321 * because script's perf_evsel__check_attr is triggered
2322 * by attr->sample_type != 0, and we can't run it on
2323 * stat sessions.
2324 */
2325 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2326
2327 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002328 * We dont want to block the signals - that would cause
2329 * child tasks to inherit that and Ctrl-C would not work.
2330 * What we want is for Ctrl-C to work in the exec()-ed
2331 * task, but being ignored by perf stat itself:
2332 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002333 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002334 if (!forever)
2335 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002336 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002337 signal(SIGALRM, skip_signal);
2338 signal(SIGABRT, skip_signal);
2339
Alexey Budankov27e97692020-07-17 10:05:41 +03002340 if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack))
2341 goto out;
2342
Ingo Molnar42202dd2009-06-13 14:57:28 +02002343 status = 0;
Jiri Olsad97ae042018-08-30 08:32:36 +02002344 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
2345 if (stat_config.run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002346 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2347 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002348
Srikar Dronamrajub63fd112019-09-04 15:17:37 +05302349 if (run_idx != 0)
2350 perf_evlist__reset_prev_raw_counts(evsel_list);
2351
Jiri Olsae55c14a2018-04-23 11:08:21 +02002352 status = run_perf_stat(argc, argv, run_idx);
Srikar Dronamraju443f2d52019-09-04 15:17:38 +05302353 if (forever && status != -1 && !interval) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002354 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002355 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002356 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002357 }
2358
Jin Yaoc7e5b322020-05-20 12:27:37 +08002359 if (!forever && status != -1 && (!interval || stat_config.summary))
Jiri Olsad4f63a42015-06-26 11:29:26 +02002360 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002361
Alexey Budankov27e97692020-07-17 10:05:41 +03002362 evlist__finalize_ctlfd(evsel_list);
2363
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002364 if (STAT_RECORD) {
2365 /*
2366 * We synthesize the kernel mmap record just so that older tools
2367 * don't emit warnings about not being able to resolve symbols
2368 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2369 * a saner message about no samples being in the perf.data file.
2370 *
2371 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002372 * in header.c at the moment 'perf stat record' gets introduced, which
2373 * is not really needed once we start adding the stat specific PERF_RECORD_
2374 * records, but the need to suppress the kptr_restrict messages in older
2375 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002376 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002377 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002378 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2379 process_synthesized_event,
2380 &perf_stat.session->machines.host);
2381 if (err) {
2382 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2383 "older tools may produce warnings about this file\n.");
2384 }
2385
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002386 if (!interval) {
2387 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2388 pr_err("failed to write stat round event\n");
2389 }
2390
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002391 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01002392 perf_stat.session->header.data_size += perf_stat.bytes_written;
2393 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2394 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002395
Jiri Olsa750b4ed2019-07-21 13:24:07 +02002396 evlist__close(evsel_list);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002397 perf_session__delete(perf_stat.session);
2398 }
2399
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002400 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002401 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002402out:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03002403 zfree(&stat_config.walltime_run);
Jiri Olsae55c14a2018-04-23 11:08:21 +02002404
Kan Liangdaefd0b2017-05-26 12:05:38 -07002405 if (smi_cost && smi_reset)
2406 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2407
Jiri Olsac12995a2019-07-21 13:23:56 +02002408 evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08002409
Jiri Olsa9afe5652020-06-02 23:47:38 +02002410 metricgroup__rblist_exit(&stat_config.metric_events);
Jin Yao56739442017-12-05 22:03:07 +08002411 runtime_stat_delete(&stat_config);
Adrian Huntera8fcbd22020-09-02 13:57:07 +03002412 close_control_option(&stat_config);
Jin Yao56739442017-12-05 22:03:07 +08002413
Ingo Molnar42202dd2009-06-13 14:57:28 +02002414 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002415}