Thomas Gleixner | 9100704 | 2019-05-29 07:12:25 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2 | /* |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 3 | * 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 Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 9 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 10 | $ perf stat ./hackbench 10 |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 11 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 12 | Time: 0.118 |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 13 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 14 | Performance counter stats for './hackbench 10': |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 15 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 16 | 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 Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 29 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 30 | * |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 31 | * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 32 | * |
| 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 Rajput | 6e750a8f | 2009-06-27 03:02:07 +0530 | [diff] [blame] | 40 | * Jaswinder Singh Rajput <jaswinder@kernel.org> |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 41 | */ |
| 42 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 43 | #include "builtin.h" |
Arnaldo Carvalho de Melo | c1a604d | 2019-08-29 15:20:59 -0300 | [diff] [blame] | 44 | #include "perf.h" |
Arnaldo Carvalho de Melo | f14d570 | 2014-10-17 12:17:40 -0300 | [diff] [blame] | 45 | #include "util/cgroup.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 46 | #include <subcmd/parse-options.h> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 47 | #include "util/parse-events.h" |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 48 | #include "util/pmu.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 49 | #include "util/event.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 50 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 51 | #include "util/evsel.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 52 | #include "util/debug.h" |
Ingo Molnar | a5d243d | 2011-04-27 05:39:24 +0200 | [diff] [blame] | 53 | #include "util/color.h" |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 54 | #include "util/stat.h" |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 55 | #include "util/header.h" |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 56 | #include "util/cpumap.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 57 | #include "util/thread_map.h" |
Jiri Olsa | d809560 | 2015-08-07 12:51:03 +0200 | [diff] [blame] | 58 | #include "util/counts.h" |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 59 | #include "util/group.h" |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 60 | #include "util/session.h" |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 61 | #include "util/tool.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 62 | #include "util/string2.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 63 | #include "util/metricgroup.h" |
Arnaldo Carvalho de Melo | ea49e01 | 2019-09-18 11:36:13 -0300 | [diff] [blame] | 64 | #include "util/synthetic-events.h" |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 65 | #include "util/target.h" |
Arnaldo Carvalho de Melo | f371102 | 2019-08-29 15:16:27 -0300 | [diff] [blame] | 66 | #include "util/time-utils.h" |
Jiri Olsa | 9660e08 | 2018-06-07 00:15:06 +0200 | [diff] [blame] | 67 | #include "util/top.h" |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 68 | #include "util/affinity.h" |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 69 | #include "util/pfm.h" |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 70 | #include "asm/bug.h" |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 71 | |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 72 | #include <linux/time64.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 73 | #include <linux/zalloc.h> |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 74 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 75 | #include <errno.h> |
Arnaldo Carvalho de Melo | 9607ad3 | 2017-04-19 15:49:18 -0300 | [diff] [blame] | 76 | #include <signal.h> |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 77 | #include <stdlib.h> |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 78 | #include <sys/prctl.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 79 | #include <inttypes.h> |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 80 | #include <locale.h> |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 81 | #include <math.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 82 | #include <sys/types.h> |
| 83 | #include <sys/stat.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 84 | #include <sys/wait.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 85 | #include <unistd.h> |
Jiri Olsa | 0ce2da1 | 2018-06-05 14:13:13 +0200 | [diff] [blame] | 86 | #include <sys/time.h> |
| 87 | #include <sys/resource.h> |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 88 | #include <linux/err.h> |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 89 | |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 90 | #include <linux/ctype.h> |
Jiri Olsa | 453fa03 | 2019-07-21 13:24:43 +0200 | [diff] [blame] | 91 | #include <perf/evlist.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 92 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 93 | #define DEFAULT_SEPARATOR " " |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 94 | #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi" |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 95 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 96 | static void print_counters(struct timespec *ts, int argc, const char **argv); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 97 | |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 98 | /* Default events used for perf stat -T */ |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 99 | static const char *transaction_attrs = { |
| 100 | "task-clock," |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 101 | "{" |
| 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 Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 112 | static const char * transaction_limited_attrs = { |
| 113 | "task-clock," |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 114 | "{" |
| 115 | "instructions," |
| 116 | "cycles," |
| 117 | "cpu/cycles-t/," |
| 118 | "cpu/tx-start/" |
| 119 | "}" |
| 120 | }; |
| 121 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 122 | static 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 Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 131 | static const char *smi_cost_attrs = { |
| 132 | "{" |
| 133 | "msr/aperf/," |
| 134 | "msr/smi/," |
| 135 | "cycles" |
| 136 | "}" |
| 137 | }; |
| 138 | |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 139 | static struct evlist *evsel_list; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 140 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 141 | static struct target target = { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 142 | .uid = UINT_MAX, |
| 143 | }; |
Jaswinder Singh Rajput | 3d63259 | 2009-06-24 18:19:34 +0530 | [diff] [blame] | 144 | |
Jiri Olsa | c1a1f5d | 2018-06-07 00:15:09 +0200 | [diff] [blame] | 145 | #define METRIC_ONLY_LEN 20 |
| 146 | |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 147 | static volatile pid_t child_pid = -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 148 | static int detailed_run = 0; |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 149 | static bool transaction_run; |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 150 | static bool topdown_run = false; |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 151 | static bool smi_cost = false; |
| 152 | static bool smi_reset = false; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 153 | static int big_num_opt = -1; |
Lin Ming | 43bece7 | 2011-08-17 18:42:07 +0800 | [diff] [blame] | 154 | static bool group = false; |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 155 | static const char *pre_cmd = NULL; |
| 156 | static const char *post_cmd = NULL; |
| 157 | static bool sync_run = false; |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 158 | static bool forever = false; |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 159 | static bool force_metric_only = false; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 160 | static struct timespec ref_time; |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 161 | static bool append_file; |
yuzhoujian | db06a26 | 2018-01-29 10:25:22 +0100 | [diff] [blame] | 162 | static bool interval_count; |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 163 | static const char *output_name; |
| 164 | static int output_fd; |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 165 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 166 | struct perf_stat { |
| 167 | bool record; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 168 | struct perf_data data; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 169 | struct perf_session *session; |
| 170 | u64 bytes_written; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 171 | struct perf_tool tool; |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 172 | bool maps_allocated; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 173 | struct perf_cpu_map *cpus; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 174 | struct perf_thread_map *threads; |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 175 | enum aggr_mode aggr_mode; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static struct perf_stat perf_stat; |
| 179 | #define STAT_RECORD perf_stat.record |
| 180 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 181 | static volatile int done = 0; |
| 182 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 183 | static struct perf_stat_config stat_config = { |
Jiri Olsa | 26893a6 | 2018-08-30 08:32:40 +0200 | [diff] [blame] | 184 | .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 Olsa | 34ff086 | 2018-08-30 08:32:47 +0200 | [diff] [blame] | 190 | .big_num = true, |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 191 | .ctl_fd = -1, |
| 192 | .ctl_fd_ack = -1 |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 193 | }; |
| 194 | |
Jiri Olsa | a9a17902 | 2020-06-02 12:17:36 +0200 | [diff] [blame] | 195 | static 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 | |
| 214 | static 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 Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 248 | static 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 Melo | 310ebb9 | 2016-08-08 14:57:04 -0300 | [diff] [blame] | 253 | r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 254 | r->tv_sec--; |
| 255 | } else { |
| 256 | r->tv_nsec = a->tv_nsec - b->tv_nsec ; |
| 257 | } |
| 258 | } |
| 259 | |
Jiri Olsa | 254ecbc7 | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 260 | static void perf_stat__reset_stats(void) |
| 261 | { |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 262 | int i; |
| 263 | |
Jiri Olsa | 254ecbc7 | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 264 | perf_evlist__reset_stats(evsel_list); |
Jiri Olsa | f87027b | 2015-06-03 16:25:59 +0200 | [diff] [blame] | 265 | perf_stat__reset_shadow_stats(); |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 266 | |
| 267 | for (i = 0; i < stat_config.stats_num; i++) |
| 268 | perf_stat__reset_shadow_per_stat(&stat_config.stats[i]); |
Jiri Olsa | 1eda3b2 | 2015-06-03 16:25:55 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 271 | static int process_synthesized_event(struct perf_tool *tool __maybe_unused, |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 272 | union perf_event *event, |
| 273 | struct perf_sample *sample __maybe_unused, |
| 274 | struct machine *machine __maybe_unused) |
| 275 | { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 276 | if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) { |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 277 | 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 Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 285 | static int write_stat_round_event(u64 tm, u64 type) |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 286 | { |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 287 | return perf_event__synthesize_stat_round(NULL, tm, type, |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 288 | 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 Olsa | 8cd36f3 | 2019-09-02 22:04:12 +0200 | [diff] [blame] | 295 | #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 296 | |
Arnaldo Carvalho de Melo | ddc6999 | 2020-05-04 13:46:34 -0300 | [diff] [blame] | 297 | static int evsel__write_stat_event(struct evsel *counter, u32 cpu, u32 thread, |
| 298 | struct perf_counts_values *count) |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 299 | { |
| 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 Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 306 | static int read_single_counter(struct evsel *counter, int cpu, |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 307 | 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 Melo | ea08969 | 2020-04-30 11:00:53 -0300 | [diff] [blame] | 317 | return evsel__read_counter(counter, cpu, thread); |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 320 | /* |
| 321 | * Read out the results of a single counter: |
| 322 | * do not aggregate counts across CPUs in system-wide mode |
| 323 | */ |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 324 | static int read_counter_cpu(struct evsel *counter, struct timespec *rs, int cpu) |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 325 | { |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 326 | int nthreads = perf_thread_map__nr(evsel_list->core.threads); |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 327 | int thread; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 328 | |
Suzuki K. Poulose | 3b4331d | 2015-02-13 18:40:58 +0000 | [diff] [blame] | 329 | if (!counter->supported) |
| 330 | return -ENOENT; |
| 331 | |
Jiri Olsa | 648b5af | 2019-08-06 11:35:19 +0200 | [diff] [blame] | 332 | if (counter->core.system_wide) |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 333 | nthreads = 1; |
| 334 | |
| 335 | for (thread = 0; thread < nthreads; thread++) { |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 336 | struct perf_counts_values *count; |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 337 | |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 338 | count = perf_counts(counter->counts, cpu, thread); |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 339 | |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 340 | /* |
| 341 | * The leader's group read loads data into its group members |
Arnaldo Carvalho de Melo | ea08969 | 2020-04-30 11:00:53 -0300 | [diff] [blame] | 342 | * (via evsel__read_counter()) and sets their count->loaded. |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 343 | */ |
| 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 Melo | ddc6999 | 2020-05-04 13:46:34 -0300 | [diff] [blame] | 355 | if (evsel__write_stat_event(counter, cpu, thread, count)) { |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 356 | pr_err("failed to write stat event\n"); |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 357 | return -1; |
Stephane Eranian | db49a71 | 2017-04-12 11:23:01 -0700 | [diff] [blame] | 358 | } |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 359 | } |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 360 | |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 361 | if (verbose > 1) { |
| 362 | fprintf(stat_config.output, |
| 363 | "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 364 | evsel__name(counter), |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 365 | cpu, |
| 366 | count->val, count->ena, count->run); |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 367 | } |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 368 | } |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 369 | |
| 370 | return 0; |
Ingo Molnar | 2996f5d | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Jin Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 373 | static int read_affinity_counters(struct timespec *rs) |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 374 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 375 | struct evsel *counter; |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 376 | struct affinity affinity; |
| 377 | int i, ncpus, cpu; |
| 378 | |
| 379 | if (affinity__setup(&affinity) < 0) |
Jin Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 380 | return -1; |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 381 | |
| 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 Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static void read_counters(struct timespec *rs) |
| 404 | { |
| 405 | struct evsel *counter; |
| 406 | |
Jin Yao | ee6a961 | 2020-09-03 09:01:13 +0800 | [diff] [blame] | 407 | if (!stat_config.stop_read_counter && (read_affinity_counters(rs) < 0)) |
Jin Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 408 | return; |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 409 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 410 | evlist__for_each_entry(evsel_list, counter) { |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 411 | if (counter->err) |
Andi Kleen | 245bad8 | 2015-09-01 15:52:46 -0700 | [diff] [blame] | 412 | pr_debug("failed to read counter %s\n", counter->name); |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 413 | if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter)) |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 414 | pr_warning("failed to process counter %s\n", counter->name); |
Andi Kleen | 4b49ab7 | 2019-11-20 16:15:20 -0800 | [diff] [blame] | 415 | counter->err = 0; |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Jin Yao | 72f02a9 | 2020-05-20 12:27:33 +0800 | [diff] [blame] | 419 | static 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 | |
| 435 | static 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 | |
| 448 | static 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 Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 459 | static void process_interval(void) |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 460 | { |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 461 | struct timespec ts, rs; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 462 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 463 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 464 | diff_timespec(&rs, &ts, &ref_time); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 465 | |
Jin Yao | 197ba86 | 2020-04-20 22:54:17 +0800 | [diff] [blame] | 466 | perf_stat__reset_shadow_per_stat(&rt_stat); |
Jin Yao | 72f02a9 | 2020-05-20 12:27:33 +0800 | [diff] [blame] | 467 | runtime_stat_reset(&stat_config); |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 468 | read_counters(&rs); |
| 469 | |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 470 | if (STAT_RECORD) { |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 471 | if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL)) |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 472 | pr_err("failed to write stat round event\n"); |
| 473 | } |
| 474 | |
Andi Kleen | b90f133 | 2017-08-31 12:40:36 -0700 | [diff] [blame] | 475 | init_stats(&walltime_nsecs_stats); |
Jiri Olsa | ea9eb1f | 2020-05-18 15:14:45 +0200 | [diff] [blame] | 476 | update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000ULL); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 477 | print_counters(&rs, 0, NULL); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Alexey Budankov | dece3a4 | 2020-07-17 10:02:08 +0300 | [diff] [blame] | 480 | static 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 Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 490 | static void enable_counters(void) |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 491 | { |
Alexey Budankov | 2162b9c | 2020-07-17 10:04:33 +0300 | [diff] [blame] | 492 | 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 Olsa | 728c0ee | 2018-08-30 08:32:11 +0200 | [diff] [blame] | 499 | usleep(stat_config.initial_delay * USEC_PER_MSEC); |
Alexey Budankov | 2162b9c | 2020-07-17 10:04:33 +0300 | [diff] [blame] | 500 | } |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 501 | |
| 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 Budankov | 2162b9c | 2020-07-17 10:04:33 +0300 | [diff] [blame] | 507 | if (!target__none(&target) || stat_config.initial_delay) { |
Jiri Olsa | 1c87f16 | 2019-07-21 13:24:08 +0200 | [diff] [blame] | 508 | evlist__enable(evsel_list); |
Alexey Budankov | 2162b9c | 2020-07-17 10:04:33 +0300 | [diff] [blame] | 509 | if (stat_config.initial_delay > 0) |
| 510 | pr_info(EVLIST_ENABLED_MSG); |
| 511 | } |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 514 | static 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 Olsa | e74676d | 2019-07-21 13:24:09 +0200 | [diff] [blame] | 522 | evlist__disable(evsel_list); |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 525 | static volatile int workload_exec_errno; |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 526 | |
| 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 Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 532 | static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, |
| 533 | void *ucontext __maybe_unused) |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 534 | { |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 535 | workload_exec_errno = info->si_value.sival_int; |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 536 | } |
| 537 | |
Arnaldo Carvalho de Melo | ddc6999 | 2020-05-04 13:46:34 -0300 | [diff] [blame] | 538 | static bool evsel__should_store_id(struct evsel *counter) |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 539 | { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 540 | return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID; |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Jiri Olsa | cbb5df7e | 2018-10-22 11:30:15 +0200 | [diff] [blame] | 543 | static bool is_target_alive(struct target *_target, |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 544 | struct perf_thread_map *threads) |
Jiri Olsa | cbb5df7e | 2018-10-22 11:30:15 +0200 | [diff] [blame] | 545 | { |
| 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 Budankov | bee328c | 2020-07-17 10:05:06 +0300 | [diff] [blame] | 565 | static 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 | |
| 589 | static 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 | |
| 606 | static int dispatch_events(bool forks, int timeout, int interval, int *times) |
Alexey Budankov | 987b823 | 2020-07-17 10:04:02 +0300 | [diff] [blame] | 607 | { |
| 608 | int child_exited = 0, status = 0; |
Alexey Budankov | bee328c | 2020-07-17 10:05:06 +0300 | [diff] [blame] | 609 | 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 Budankov | 987b823 | 2020-07-17 10:04:02 +0300 | [diff] [blame] | 620 | |
| 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 Budankov | bee328c | 2020-07-17 10:05:06 +0300 | [diff] [blame] | 630 | 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 Budankov | 987b823 | 2020-07-17 10:04:02 +0300 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return status; |
| 643 | } |
| 644 | |
Andi Kleen | e0e6a6c | 2019-11-20 16:15:18 -0800 | [diff] [blame] | 645 | enum counter_recovery { |
| 646 | COUNTER_SKIP, |
| 647 | COUNTER_RETRY, |
| 648 | COUNTER_FATAL, |
| 649 | }; |
| 650 | |
| 651 | static 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 Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 663 | evsel__name(counter)); |
Andi Kleen | e0e6a6c | 2019-11-20 16:15:18 -0800 | [diff] [blame] | 664 | counter->supported = false; |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 665 | /* |
| 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 Kleen | e0e6a6c | 2019-11-20 16:15:18 -0800 | [diff] [blame] | 670 | |
| 671 | if ((counter->leader != counter) || |
| 672 | !(counter->leader->core.nr_members > 1)) |
| 673 | return COUNTER_SKIP; |
Arnaldo Carvalho de Melo | ae43089 | 2020-04-30 11:46:15 -0300 | [diff] [blame] | 674 | } else if (evsel__fallback(counter, errno, msg, sizeof(msg))) { |
Andi Kleen | e0e6a6c | 2019-11-20 16:15:18 -0800 | [diff] [blame] | 675 | 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 Melo | 2bb72db | 2020-05-04 13:43:03 -0300 | [diff] [blame] | 692 | evsel__open_strerror(counter, &target, errno, msg, sizeof(msg)); |
Andi Kleen | e0e6a6c | 2019-11-20 16:15:18 -0800 | [diff] [blame] | 693 | ui__error("%s\n", msg); |
| 694 | |
| 695 | if (child_pid != -1) |
| 696 | kill(child_pid, SIGTERM); |
| 697 | return COUNTER_FATAL; |
| 698 | } |
| 699 | |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 700 | static int __run_perf_stat(int argc, const char **argv, int run_idx) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 701 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 702 | int interval = stat_config.interval; |
yuzhoujian | db06a26 | 2018-01-29 10:25:22 +0100 | [diff] [blame] | 703 | int times = stat_config.times; |
yuzhoujian | f1f8ad5 | 2018-01-29 10:25:23 +0100 | [diff] [blame] | 704 | int timeout = stat_config.timeout; |
Arnaldo Carvalho de Melo | d6195a6 | 2017-02-13 16:45:24 -0300 | [diff] [blame] | 705 | char msg[BUFSIZ]; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 706 | unsigned long long t0, t1; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 707 | struct evsel *counter; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 708 | size_t l; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 709 | int status = 0; |
Zhang, Yanmin | 6be2850 | 2010-03-18 11:36:03 -0300 | [diff] [blame] | 710 | const bool forks = (argc > 0); |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 711 | bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false; |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 712 | struct affinity affinity; |
| 713 | int i, cpu; |
| 714 | bool second_pass = false; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 715 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 716 | if (forks) { |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 717 | if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 718 | workload_exec_failed_signal) < 0) { |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 719 | perror("failed to prepare workload"); |
| 720 | return -1; |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 721 | } |
Namhyung Kim | d20a47e | 2013-09-30 18:01:11 +0900 | [diff] [blame] | 722 | child_pid = evsel_list->workload.pid; |
Paul Mackerras | 051ae7f | 2009-06-29 21:13:21 +1000 | [diff] [blame] | 723 | } |
| 724 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 725 | if (group) |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 726 | perf_evlist__set_leader(evsel_list); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 727 | |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 728 | if (affinity__setup(&affinity) < 0) |
| 729 | return -1; |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 730 | |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 731 | evlist__for_each_cpu (evsel_list, i, cpu) { |
| 732 | affinity__set(&affinity, cpu); |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 733 | |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 734 | evlist__for_each_entry(evsel_list, counter) { |
| 735 | if (evsel__cpu_iter_skip(counter, cpu)) |
Andi Kleen | e0e6a6c | 2019-11-20 16:15:18 -0800 | [diff] [blame] | 736 | continue; |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 737 | if (counter->reset_group || counter->errored) |
| 738 | continue; |
| 739 | try_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; |
| 799 | try_again_reset: |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 800 | pr_debug2("reopening weak %s\n", evsel__name(counter)); |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 801 | 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 Yao | ab6c79b | 2018-01-16 23:43:08 +0800 | [diff] [blame] | 816 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 817 | } |
Andi Kleen | 4804e01 | 2019-11-20 16:15:19 -0800 | [diff] [blame] | 818 | } |
| 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 Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 826 | |
| 827 | l = strlen(counter->unit); |
Jiri Olsa | df4f7b4 | 2018-08-30 08:32:32 +0200 | [diff] [blame] | 828 | if (l > stat_config.unit_width) |
| 829 | stat_config.unit_width = l; |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 830 | |
Arnaldo Carvalho de Melo | ddc6999 | 2020-05-04 13:46:34 -0300 | [diff] [blame] | 831 | if (evsel__should_store_id(counter) && |
Arnaldo Carvalho de Melo | 3439775 | 2020-05-04 13:45:19 -0300 | [diff] [blame] | 832 | evsel__store_ids(counter, evsel_list)) |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 833 | return -1; |
Arnaldo Carvalho de Melo | 084ab9f | 2010-03-22 13:10:28 -0300 | [diff] [blame] | 834 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 835 | |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 836 | if (perf_evlist__apply_filters(evsel_list, &counter)) { |
Arnaldo Carvalho de Melo | 62d94b0 | 2017-06-27 11:22:31 -0300 | [diff] [blame] | 837 | pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 838 | counter->filter, evsel__name(counter), errno, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 839 | str_error_r(errno, msg, sizeof(msg))); |
Frederic Weisbecker | cfd748a | 2011-03-14 16:40:30 +0100 | [diff] [blame] | 840 | return -1; |
| 841 | } |
| 842 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 843 | if (STAT_RECORD) { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 844 | int err, fd = perf_data__fd(&perf_stat.data); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 845 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 846 | if (is_pipe) { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 847 | err = perf_header__write_pipe(perf_data__fd(&perf_stat.data)); |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 848 | } else { |
| 849 | err = perf_session__write_header(perf_stat.session, evsel_list, |
| 850 | fd, false); |
| 851 | } |
| 852 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 853 | if (err < 0) |
| 854 | return err; |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 855 | |
Arnaldo Carvalho de Melo | b251892 | 2019-09-10 17:17:33 +0100 | [diff] [blame] | 856 | err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list, |
| 857 | process_synthesized_event, is_pipe); |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 858 | if (err < 0) |
| 859 | return err; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 862 | /* |
| 863 | * Enable counters and exec the command: |
| 864 | */ |
| 865 | t0 = rdclock(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 866 | clock_gettime(CLOCK_MONOTONIC, &ref_time); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 867 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 868 | if (forks) { |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 869 | perf_evlist__start_workload(evsel_list); |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 870 | enable_counters(); |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 871 | |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 872 | if (interval || timeout || evlist__ctlfd_initialized(evsel_list)) |
Alexey Budankov | bee328c | 2020-07-17 10:05:06 +0300 | [diff] [blame] | 873 | status = dispatch_events(forks, timeout, interval, ×); |
Arnaldo Carvalho de Melo | cfbd41b | 2020-04-15 12:31:26 -0300 | [diff] [blame] | 874 | if (child_pid != -1) { |
| 875 | if (timeout) |
| 876 | kill(child_pid, SIGTERM); |
Jin Yao | 8a99255 | 2019-01-03 15:40:45 +0800 | [diff] [blame] | 877 | wait4(child_pid, &status, 0, &stat_config.ru_data); |
Arnaldo Carvalho de Melo | cfbd41b | 2020-04-15 12:31:26 -0300 | [diff] [blame] | 878 | } |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 879 | |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 880 | if (workload_exec_errno) { |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 881 | const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 882 | pr_err("Workload failed: %s\n", emsg); |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 883 | return -1; |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 884 | } |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 885 | |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 886 | if (WIFSIGNALED(status)) |
| 887 | psignal(WTERMSIG(status), argv[0]); |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 888 | } else { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 889 | enable_counters(); |
Alexey Budankov | bee328c | 2020-07-17 10:05:06 +0300 | [diff] [blame] | 890 | status = dispatch_events(forks, timeout, interval, ×); |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 891 | } |
Ingo Molnar | 44db76c | 2009-06-03 19:36:07 +0200 | [diff] [blame] | 892 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 893 | disable_counters(); |
| 894 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 895 | t1 = rdclock(); |
| 896 | |
Jiri Olsa | 54ac0b1 | 2018-08-30 08:32:50 +0200 | [diff] [blame] | 897 | if (stat_config.walltime_run_table) |
| 898 | stat_config.walltime_run[run_idx] = t1 - t0; |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 899 | |
Jin Yao | ee6a961 | 2020-09-03 09:01:13 +0800 | [diff] [blame] | 900 | if (interval && stat_config.summary) { |
Jin Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 901 | stat_config.interval = 0; |
Jin Yao | ee6a961 | 2020-09-03 09:01:13 +0800 | [diff] [blame] | 902 | stat_config.stop_read_counter = true; |
Jin Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 903 | 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 Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 915 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 916 | /* |
| 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 Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 922 | read_counters(&(struct timespec) { .tv_nsec = t1-t0 }); |
Jiri Olsa | 08ef3af | 2019-07-15 16:21:21 +0200 | [diff] [blame] | 923 | |
| 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 Olsa | 750b4ed | 2019-07-21 13:24:07 +0200 | [diff] [blame] | 929 | evlist__close(evsel_list); |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 930 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 931 | return WEXITSTATUS(status); |
| 932 | } |
| 933 | |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 934 | static int run_perf_stat(int argc, const char **argv, int run_idx) |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 935 | { |
| 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 Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 947 | ret = __run_perf_stat(argc, argv, run_idx); |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 948 | 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 Olsa | a5a9eac | 2018-08-30 08:32:24 +0200 | [diff] [blame] | 960 | static void print_counters(struct timespec *ts, int argc, const char **argv) |
| 961 | { |
Jiri Olsa | 0174820 | 2018-08-30 08:32:25 +0200 | [diff] [blame] | 962 | /* Do not print anything if we record to the pipe. */ |
| 963 | if (STAT_RECORD && perf_stat.data.is_pipe) |
| 964 | return; |
| 965 | |
Jiri Olsa | c512e0e | 2018-08-30 08:32:33 +0200 | [diff] [blame] | 966 | perf_evlist__print_counters(evsel_list, &stat_config, &target, |
Jiri Olsa | b64df7f | 2018-08-30 08:32:26 +0200 | [diff] [blame] | 967 | ts, argc, argv); |
Jiri Olsa | a5a9eac | 2018-08-30 08:32:24 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 970 | static volatile int signr = -1; |
| 971 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 972 | static void skip_signal(int signo) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 973 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 974 | if ((child_pid == -1) || stat_config.interval) |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 975 | done = 1; |
| 976 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 977 | signr = signo; |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 978 | /* |
| 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 Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | static void sig_atexit(void) |
| 988 | { |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 989 | 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 Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 1001 | if (child_pid != -1) |
| 1002 | kill(child_pid, SIGTERM); |
| 1003 | |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1004 | sigprocmask(SIG_SETMASK, &oset, NULL); |
| 1005 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1006 | if (signr == -1) |
| 1007 | return; |
| 1008 | |
| 1009 | signal(signr, SIG_DFL); |
| 1010 | kill(getpid(), signr); |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1011 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1012 | |
Paul A. Clarke | d778a77 | 2020-05-20 11:23:35 -0500 | [diff] [blame] | 1013 | void perf_stat__set_big_num(int set) |
| 1014 | { |
| 1015 | stat_config.big_num = (set != 0); |
| 1016 | } |
| 1017 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1018 | static int stat__set_big_num(const struct option *opt __maybe_unused, |
| 1019 | const char *s __maybe_unused, int unset) |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1020 | { |
| 1021 | big_num_opt = unset ? 0 : 1; |
Paul A. Clarke | d778a77 | 2020-05-20 11:23:35 -0500 | [diff] [blame] | 1022 | perf_stat__set_big_num(!unset); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1026 | static 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 Olsa | 0ce5aa0 | 2018-08-30 08:32:31 +0200 | [diff] [blame] | 1030 | stat_config.metric_only = !unset; |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1031 | return 0; |
| 1032 | } |
| 1033 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1034 | static int parse_metric_groups(const struct option *opt, |
| 1035 | const char *str, |
| 1036 | int unset __maybe_unused) |
| 1037 | { |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1038 | return metricgroup__parse_groups(opt, str, |
| 1039 | stat_config.metric_no_group, |
| 1040 | stat_config.metric_no_merge, |
| 1041 | &stat_config.metric_events); |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 1044 | static int parse_control_option(const struct option *opt, |
| 1045 | const char *str, |
| 1046 | int unset __maybe_unused) |
| 1047 | { |
Adrian Hunter | 9864a66 | 2020-09-01 12:37:53 +0300 | [diff] [blame] | 1048 | struct perf_stat_config *config = opt->value; |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 1049 | |
Adrian Hunter | a8fcbd2 | 2020-09-02 13:57:07 +0300 | [diff] [blame^] | 1050 | return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close); |
| 1051 | } |
| 1052 | |
| 1053 | static 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 Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 1061 | } |
| 1062 | |
Michael Petlan | 51433ea | 2018-12-10 11:00:04 -0500 | [diff] [blame] | 1063 | static struct option stat_options[] = { |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1064 | 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 Olsa | 5698f26 | 2018-08-30 08:32:12 +0200 | [diff] [blame] | 1071 | OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit, |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1072 | "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 Kleen | 75998bb | 2019-03-14 15:50:01 -0700 | [diff] [blame] | 1081 | OPT_BOOLEAN(0, "scale", &stat_config.scale, |
| 1082 | "Use --no-scale to disable counter scaling for multiplexing"), |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1083 | OPT_INCR('v', "verbose", &verbose, |
| 1084 | "be more verbose (show counter open errors, etc)"), |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 1085 | OPT_INTEGER('r', "repeat", &stat_config.run_count, |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1086 | "repeat command and print average + stddev (max: 100, forever: 0)"), |
Jiri Olsa | 54ac0b1 | 2018-08-30 08:32:50 +0200 | [diff] [blame] | 1087 | OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table, |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 1088 | "display details about each run (only with -r option)"), |
Jiri Olsa | aea0dca | 2018-08-30 08:32:41 +0200 | [diff] [blame] | 1089 | OPT_BOOLEAN('n', "null", &stat_config.null_run, |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1090 | "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 Olsa | fdee335 | 2018-08-30 08:32:48 +0200 | [diff] [blame] | 1102 | OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"), |
Jiri Olsa | fa7070a | 2018-08-30 08:32:29 +0200 | [diff] [blame] | 1103 | OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator", |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1104 | "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 Budankov | 9dc9a95 | 2018-04-03 21:18:33 +0300 | [diff] [blame] | 1116 | "print counts at regular interval in ms " |
| 1117 | "(overhead is possible for values <= 100ms)"), |
yuzhoujian | db06a26 | 2018-01-29 10:25:22 +0100 | [diff] [blame] | 1118 | OPT_INTEGER(0, "interval-count", &stat_config.times, |
| 1119 | "print counts for fixed number of times"), |
Jiri Olsa | 132c6ba | 2018-08-30 08:32:30 +0200 | [diff] [blame] | 1120 | OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear, |
Jiri Olsa | 9660e08 | 2018-06-07 00:15:06 +0200 | [diff] [blame] | 1121 | "clear screen in between new interval"), |
yuzhoujian | f1f8ad5 | 2018-01-29 10:25:23 +0100 | [diff] [blame] | 1122 | OPT_UINTEGER(0, "timeout", &stat_config.timeout, |
| 1123 | "stop workload and print counts after a timeout period in ms (>= 10ms)"), |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1124 | OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, |
| 1125 | "aggregate counts per processor socket", AGGR_SOCKET), |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1126 | OPT_SET_UINT(0, "per-die", &stat_config.aggr_mode, |
| 1127 | "aggregate counts per processor die", AGGR_DIE), |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1128 | 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 Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1132 | OPT_SET_UINT(0, "per-node", &stat_config.aggr_mode, |
| 1133 | "aggregate counts per numa node", AGGR_NODE), |
Alexey Budankov | 2162b9c | 2020-07-17 10:04:33 +0300 | [diff] [blame] | 1134 | OPT_INTEGER('D', "delay", &stat_config.initial_delay, |
| 1135 | "ms to wait before starting measurement after program start (-1: start with events disabled)"), |
Jiri Olsa | 0ce5aa0 | 2018-08-30 08:32:31 +0200 | [diff] [blame] | 1136 | OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL, |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1137 | "Only print computed metrics. No raw values", enable_metric_only), |
Ian Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1138 | 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 Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1142 | OPT_BOOLEAN(0, "topdown", &topdown_run, |
| 1143 | "measure topdown level 1 statistics"), |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 1144 | OPT_BOOLEAN(0, "smi-cost", &smi_cost, |
| 1145 | "measure SMI cost"), |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1146 | OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list", |
| 1147 | "monitor specified metrics or metric groups (separated by ,)", |
| 1148 | parse_metric_groups), |
Jin Yao | dd07102 | 2019-10-11 13:05:45 +0800 | [diff] [blame] | 1149 | 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 Yao | 1af62ce | 2020-02-14 16:04:52 +0800 | [diff] [blame] | 1155 | 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 Yao | ee6a961 | 2020-09-03 09:01:13 +0800 | [diff] [blame] | 1159 | OPT_BOOLEAN(0, "summary", &stat_config.summary, |
| 1160 | "print summary for interval mode"), |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 1161 | #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 Hunter | a8fcbd2 | 2020-09-02 13:57:07 +0300 | [diff] [blame^] | 1166 | OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]", |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 1167 | "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n" |
Adrian Hunter | a8fcbd2 | 2020-09-02 13:57:07 +0300 | [diff] [blame^] | 1168 | "\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 Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 1170 | parse_control_option), |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1171 | OPT_END() |
| 1172 | }; |
| 1173 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1174 | static int perf_stat__get_socket(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1175 | struct perf_cpu_map *map, int cpu) |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 1176 | { |
| 1177 | return cpu_map__get_socket(map, cpu, NULL); |
| 1178 | } |
| 1179 | |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1180 | static int perf_stat__get_die(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1181 | struct perf_cpu_map *map, int cpu) |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1182 | { |
| 1183 | return cpu_map__get_die(map, cpu, NULL); |
| 1184 | } |
| 1185 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1186 | static int perf_stat__get_core(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1187 | struct perf_cpu_map *map, int cpu) |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 1188 | { |
| 1189 | return cpu_map__get_core(map, cpu, NULL); |
| 1190 | } |
| 1191 | |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1192 | static 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 Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1198 | static int perf_stat__get_aggr(struct perf_stat_config *config, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1199 | aggr_get_id_t get_id, struct perf_cpu_map *map, int idx) |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1200 | { |
| 1201 | int cpu; |
| 1202 | |
| 1203 | if (idx >= map->nr) |
| 1204 | return -1; |
| 1205 | |
| 1206 | cpu = map->map[idx]; |
| 1207 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1208 | if (config->cpus_aggr_map->map[cpu] == -1) |
| 1209 | config->cpus_aggr_map->map[cpu] = get_id(config, map, idx); |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1210 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1211 | return config->cpus_aggr_map->map[cpu]; |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1214 | static int perf_stat__get_socket_cached(struct perf_stat_config *config, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1215 | struct perf_cpu_map *map, int idx) |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1216 | { |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1217 | return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx); |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1220 | static int perf_stat__get_die_cached(struct perf_stat_config *config, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1221 | struct perf_cpu_map *map, int idx) |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1222 | { |
| 1223 | return perf_stat__get_aggr(config, perf_stat__get_die, map, idx); |
| 1224 | } |
| 1225 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1226 | static int perf_stat__get_core_cached(struct perf_stat_config *config, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1227 | struct perf_cpu_map *map, int idx) |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1228 | { |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1229 | return perf_stat__get_aggr(config, perf_stat__get_core, map, idx); |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1232 | static 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 Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1238 | static bool term_percore_set(void) |
| 1239 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1240 | struct evsel *counter; |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1241 | |
| 1242 | evlist__for_each_entry(evsel_list, counter) { |
| 1243 | if (counter->percore) |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1250 | static int perf_stat_init_aggr_mode(void) |
| 1251 | { |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1252 | int nr; |
| 1253 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1254 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1255 | case AGGR_SOCKET: |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1256 | if (cpu_map__build_socket_map(evsel_list->core.cpus, &stat_config.aggr_map)) { |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1257 | perror("cannot build socket map"); |
| 1258 | return -1; |
| 1259 | } |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1260 | stat_config.aggr_get_id = perf_stat__get_socket_cached; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1261 | break; |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1262 | case AGGR_DIE: |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1263 | if (cpu_map__build_die_map(evsel_list->core.cpus, &stat_config.aggr_map)) { |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1264 | perror("cannot build die map"); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | stat_config.aggr_get_id = perf_stat__get_die_cached; |
| 1268 | break; |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1269 | case AGGR_CORE: |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1270 | if (cpu_map__build_core_map(evsel_list->core.cpus, &stat_config.aggr_map)) { |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1271 | perror("cannot build core map"); |
| 1272 | return -1; |
| 1273 | } |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1274 | stat_config.aggr_get_id = perf_stat__get_core_cached; |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1275 | break; |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1276 | 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 Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1283 | case AGGR_NONE: |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1284 | if (term_percore_set()) { |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1285 | if (cpu_map__build_core_map(evsel_list->core.cpus, |
Jin Yao | 4fc4d8d | 2019-04-12 21:59:49 +0800 | [diff] [blame] | 1286 | &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 Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1293 | case AGGR_GLOBAL: |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1294 | case AGGR_THREAD: |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1295 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1296 | default: |
| 1297 | break; |
| 1298 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1299 | |
| 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 Olsa | 4256d43 | 2019-09-02 14:12:53 +0200 | [diff] [blame] | 1305 | nr = perf_cpu_map__max(evsel_list->core.cpus); |
Jiri Olsa | 315c0a1 | 2019-08-22 13:11:39 +0200 | [diff] [blame] | 1306 | stat_config.cpus_aggr_map = perf_cpu_map__empty_new(nr + 1); |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1307 | return stat_config.cpus_aggr_map ? 0 : -ENOMEM; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1308 | } |
| 1309 | |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 1310 | static void perf_stat__exit_aggr_mode(void) |
| 1311 | { |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 1312 | perf_cpu_map__put(stat_config.aggr_map); |
| 1313 | perf_cpu_map__put(stat_config.cpus_aggr_map); |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1314 | stat_config.aggr_map = NULL; |
| 1315 | stat_config.cpus_aggr_map = NULL; |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 1316 | } |
| 1317 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1318 | static inline int perf_env__get_cpu(struct perf_env *env, struct perf_cpu_map *map, int idx) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1319 | { |
| 1320 | int cpu; |
| 1321 | |
| 1322 | if (idx > map->nr) |
| 1323 | return -1; |
| 1324 | |
| 1325 | cpu = map->map[idx]; |
| 1326 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 1327 | if (cpu >= env->nr_cpus_avail) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1328 | return -1; |
| 1329 | |
| 1330 | return cpu; |
| 1331 | } |
| 1332 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1333 | static int perf_env__get_socket(struct perf_cpu_map *map, int idx, void *data) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1334 | { |
| 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 Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1341 | static int perf_env__get_die(struct perf_cpu_map *map, int idx, void *data) |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1342 | { |
| 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 Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1365 | static int perf_env__get_core(struct perf_cpu_map *map, int idx, void *data) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1366 | { |
| 1367 | struct perf_env *env = data; |
| 1368 | int core = -1, cpu = perf_env__get_cpu(env, map, idx); |
| 1369 | |
| 1370 | if (cpu != -1) { |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1371 | /* |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1372 | * 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 Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1375 | * we need a global id. So we combine |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1376 | * socket + die id + core id |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1377 | */ |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1378 | 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 Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | return core; |
| 1393 | } |
| 1394 | |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1395 | static 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 Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1402 | static int perf_env__build_socket_map(struct perf_env *env, struct perf_cpu_map *cpus, |
| 1403 | struct perf_cpu_map **sockp) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1404 | { |
| 1405 | return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env); |
| 1406 | } |
| 1407 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1408 | static int perf_env__build_die_map(struct perf_env *env, struct perf_cpu_map *cpus, |
| 1409 | struct perf_cpu_map **diep) |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1410 | { |
| 1411 | return cpu_map__build_map(cpus, diep, perf_env__get_die, env); |
| 1412 | } |
| 1413 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1414 | static int perf_env__build_core_map(struct perf_env *env, struct perf_cpu_map *cpus, |
| 1415 | struct perf_cpu_map **corep) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1416 | { |
| 1417 | return cpu_map__build_map(cpus, corep, perf_env__get_core, env); |
| 1418 | } |
| 1419 | |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1420 | static 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 Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1426 | static int perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1427 | struct perf_cpu_map *map, int idx) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1428 | { |
| 1429 | return perf_env__get_socket(map, idx, &perf_stat.session->header.env); |
| 1430 | } |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1431 | static int perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1432 | struct perf_cpu_map *map, int idx) |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1433 | { |
| 1434 | return perf_env__get_die(map, idx, &perf_stat.session->header.env); |
| 1435 | } |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1436 | |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1437 | static int perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1438 | struct perf_cpu_map *map, int idx) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1439 | { |
| 1440 | return perf_env__get_core(map, idx, &perf_stat.session->header.env); |
| 1441 | } |
| 1442 | |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1443 | static 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 Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1449 | static 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 Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1455 | if (perf_env__build_socket_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) { |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1456 | perror("cannot build socket map"); |
| 1457 | return -1; |
| 1458 | } |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1459 | stat_config.aggr_get_id = perf_stat__get_socket_file; |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1460 | break; |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1461 | case AGGR_DIE: |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1462 | if (perf_env__build_die_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) { |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1463 | perror("cannot build die map"); |
| 1464 | return -1; |
| 1465 | } |
| 1466 | stat_config.aggr_get_id = perf_stat__get_die_file; |
| 1467 | break; |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1468 | case AGGR_CORE: |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 1469 | if (perf_env__build_core_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) { |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1470 | perror("cannot build core map"); |
| 1471 | return -1; |
| 1472 | } |
Jiri Olsa | 6f6b659 | 2018-08-30 08:32:45 +0200 | [diff] [blame] | 1473 | stat_config.aggr_get_id = perf_stat__get_core_file; |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1474 | break; |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 1475 | 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 Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1482 | 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 Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1493 | static 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 Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1542 | /* |
| 1543 | * Add default attributes, if there were no attributes specified or |
| 1544 | * if -d/--detailed, -d -d or -d -d -d is used: |
| 1545 | */ |
| 1546 | static int add_default_attributes(void) |
| 1547 | { |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1548 | int err; |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1549 | struct perf_event_attr default_attrs0[] = { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1550 | |
| 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 Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1557 | }; |
| 1558 | struct perf_event_attr frontend_attrs[] = { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1559 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1560 | }; |
| 1561 | struct perf_event_attr backend_attrs[] = { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1562 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1563 | }; |
| 1564 | struct perf_event_attr default_attrs1[] = { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1565 | { .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 Olsa | a5cfa62 | 2018-06-07 00:15:10 +0200 | [diff] [blame] | 1661 | struct parse_events_error errinfo; |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1662 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1663 | /* Set attrs if no event is selected and !null_run: */ |
Jiri Olsa | aea0dca | 2018-08-30 08:32:41 +0200 | [diff] [blame] | 1664 | if (stat_config.null_run) |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1665 | return 0; |
| 1666 | |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 1667 | bzero(&errinfo, sizeof(errinfo)); |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1668 | if (transaction_run) { |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 1669 | /* 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 Rogers | 05530a7 | 2020-05-20 11:20:10 -0700 | [diff] [blame] | 1678 | stat_config.metric_no_group, |
| 1679 | stat_config.metric_no_merge, |
Jiri Olsa | d0192fd | 2018-08-30 08:32:51 +0200 | [diff] [blame] | 1680 | &stat_config.metric_events); |
Thomas Richter | 742d92f | 2018-06-26 09:17:01 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1683 | if (pmu_have_event("cpu", "cycles-ct") && |
| 1684 | pmu_have_event("cpu", "el-start")) |
Thomas Richter | fca32340 | 2018-03-08 15:57:35 +0100 | [diff] [blame] | 1685 | err = parse_events(evsel_list, transaction_attrs, |
| 1686 | &errinfo); |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1687 | else |
Thomas Richter | fca32340 | 2018-03-08 15:57:35 +0100 | [diff] [blame] | 1688 | err = parse_events(evsel_list, |
| 1689 | transaction_limited_attrs, |
| 1690 | &errinfo); |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 1691 | if (err) { |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1692 | fprintf(stderr, "Cannot set up transaction events\n"); |
Jiri Olsa | a5cfa62 | 2018-06-07 00:15:10 +0200 | [diff] [blame] | 1693 | parse_events_print_error(&errinfo, transaction_attrs); |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 1694 | return -1; |
| 1695 | } |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 1699 | 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 Olsa | 0ce5aa0 | 2018-08-30 08:32:31 +0200 | [diff] [blame] | 1718 | stat_config.metric_only = true; |
Jiri Olsa | a5cfa62 | 2018-06-07 00:15:10 +0200 | [diff] [blame] | 1719 | err = parse_events(evsel_list, smi_cost_attrs, &errinfo); |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 1720 | } else { |
| 1721 | fprintf(stderr, "To measure SMI cost, it needs " |
| 1722 | "msr/aperf/, msr/smi/ and cpu/cycles/ support\n"); |
Jiri Olsa | a5cfa62 | 2018-06-07 00:15:10 +0200 | [diff] [blame] | 1723 | parse_events_print_error(&errinfo, smi_cost_attrs); |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 1724 | return -1; |
| 1725 | } |
| 1726 | if (err) { |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 1727 | parse_events_print_error(&errinfo, smi_cost_attrs); |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 1728 | fprintf(stderr, "Cannot set up SMI cost events\n"); |
| 1729 | return -1; |
| 1730 | } |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1734 | 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 Olsa | 0ce5aa0 | 2018-08-30 08:32:31 +0200 | [diff] [blame] | 1750 | stat_config.metric_only = true; |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1751 | 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 Olsa | a5cfa62 | 2018-06-07 00:15:10 +0200 | [diff] [blame] | 1759 | err = parse_events(evsel_list, str, &errinfo); |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1760 | if (err) { |
| 1761 | fprintf(stderr, |
| 1762 | "Cannot set up top down events %s: %d\n", |
| 1763 | str, err); |
Jiri Olsa | a5cfa62 | 2018-06-07 00:15:10 +0200 | [diff] [blame] | 1764 | parse_events_print_error(&errinfo, str); |
Leo Yan | c74b050 | 2019-07-02 18:34:11 +0800 | [diff] [blame] | 1765 | free(str); |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1766 | return -1; |
| 1767 | } |
| 1768 | } else { |
| 1769 | fprintf(stderr, "System does not support topdown\n"); |
| 1770 | return -1; |
| 1771 | } |
| 1772 | free(str); |
| 1773 | } |
| 1774 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 1775 | if (!evsel_list->core.nr_entries) { |
Namhyung Kim | a1f3d56 | 2016-05-13 15:01:03 +0900 | [diff] [blame] | 1776 | if (target__has_cpu(&target)) |
| 1777 | default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK; |
| 1778 | |
Arnaldo Carvalho de Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1779 | if (evlist__add_default_attrs(evsel_list, default_attrs0) < 0) |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1780 | return -1; |
| 1781 | if (pmu_have_event("cpu", "stalled-cycles-frontend")) { |
Arnaldo Carvalho de Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1782 | if (evlist__add_default_attrs(evsel_list, frontend_attrs) < 0) |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1783 | return -1; |
| 1784 | } |
| 1785 | if (pmu_have_event("cpu", "stalled-cycles-backend")) { |
Arnaldo Carvalho de Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1786 | if (evlist__add_default_attrs(evsel_list, backend_attrs) < 0) |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 1787 | return -1; |
| 1788 | } |
Arnaldo Carvalho de Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1789 | if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 1790 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1791 | } |
| 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 Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1799 | if (evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 1800 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1801 | |
| 1802 | if (detailed_run < 2) |
| 1803 | return 0; |
| 1804 | |
| 1805 | /* Append very detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1806 | if (evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 1807 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1808 | |
| 1809 | if (detailed_run < 3) |
| 1810 | return 0; |
| 1811 | |
| 1812 | /* Append very, very detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | e251abe | 2020-06-17 09:16:20 -0300 | [diff] [blame] | 1813 | return evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 1816 | static const char * const stat_record_usage[] = { |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1817 | "perf stat record [<options>]", |
| 1818 | NULL, |
| 1819 | }; |
| 1820 | |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 1821 | static 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 Olsa | 8002a63 | 2019-04-09 12:01:56 +0200 | [diff] [blame] | 1828 | perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 1829 | 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 Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1835 | static int __cmd_record(int argc, const char **argv) |
| 1836 | { |
| 1837 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 1838 | struct perf_data *data = &perf_stat.data; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1839 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 1840 | argc = parse_options(argc, argv, stat_options, stat_record_usage, |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1841 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1842 | |
| 1843 | if (output_name) |
Jiri Olsa | 2d4f279 | 2019-02-21 10:41:30 +0100 | [diff] [blame] | 1844 | data->path = output_name; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1845 | |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 1846 | if (stat_config.run_count != 1 || forever) { |
Jiri Olsa | e9d6db8e8 | 2015-11-05 15:40:53 +0100 | [diff] [blame] | 1847 | pr_err("Cannot use -r option with perf stat record.\n"); |
| 1848 | return -1; |
| 1849 | } |
| 1850 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 1851 | session = perf_session__new(data, false, NULL); |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 1852 | if (IS_ERR(session)) { |
| 1853 | pr_err("Perf session creation failed\n"); |
| 1854 | return PTR_ERR(session); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 1857 | init_features(session); |
| 1858 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 1859 | session->evlist = evsel_list; |
| 1860 | perf_stat.session = session; |
| 1861 | perf_stat.record = true; |
| 1862 | return argc; |
| 1863 | } |
| 1864 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1865 | static int process_stat_round_event(struct perf_session *session, |
| 1866 | union perf_event *event) |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 1867 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1868 | struct perf_record_stat_round *stat_round = &event->stat_round; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1869 | struct evsel *counter; |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 1870 | 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 Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1874 | evlist__for_each_entry(evsel_list, counter) |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 1875 | perf_stat_process_counter(&stat_config, counter); |
| 1876 | |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 1877 | if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL) |
| 1878 | update_stats(&walltime_nsecs_stats, stat_round->time); |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 1879 | |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 1880 | if (stat_config.interval && stat_round->time) { |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 1881 | tsh.tv_sec = stat_round->time / NSEC_PER_SEC; |
| 1882 | tsh.tv_nsec = stat_round->time % NSEC_PER_SEC; |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 1883 | ts = &tsh; |
| 1884 | } |
| 1885 | |
| 1886 | print_counters(ts, argc, argv); |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
Jiri Olsa | 62ba18ba | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 1890 | static |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1891 | int process_stat_config_event(struct perf_session *session, |
| 1892 | union perf_event *event) |
Jiri Olsa | 62ba18ba | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 1893 | { |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1894 | struct perf_tool *tool = session->tool; |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1895 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
| 1896 | |
Jiri Olsa | 62ba18ba | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 1897 | perf_event__read_stat_config(&stat_config, &event->stat_config); |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1898 | |
Jiri Olsa | 315c0a1 | 2019-08-22 13:11:39 +0200 | [diff] [blame] | 1899 | if (perf_cpu_map__empty(st->cpus)) { |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 1900 | 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 Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 1908 | if (perf_stat.data.is_pipe) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1909 | perf_stat_init_aggr_mode(); |
| 1910 | else |
| 1911 | perf_stat_init_aggr_mode_file(st); |
| 1912 | |
Jiri Olsa | 62ba18ba | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 1913 | return 0; |
| 1914 | } |
| 1915 | |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1916 | static 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 Olsa | 453fa03 | 2019-07-21 13:24:43 +0200 | [diff] [blame] | 1924 | perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads); |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1925 | |
| 1926 | if (perf_evlist__alloc_stats(evsel_list, true)) |
| 1927 | return -ENOMEM; |
| 1928 | |
| 1929 | st->maps_allocated = true; |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | static |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1934 | int process_thread_map_event(struct perf_session *session, |
| 1935 | union perf_event *event) |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1936 | { |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1937 | struct perf_tool *tool = session->tool; |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1938 | 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 | |
| 1952 | static |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1953 | int process_cpu_map_event(struct perf_session *session, |
| 1954 | union perf_event *event) |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1955 | { |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1956 | struct perf_tool *tool = session->tool; |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1957 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1958 | struct perf_cpu_map *cpus; |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1959 | |
| 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 Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 1973 | static const char * const stat_report_usage[] = { |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1974 | "perf stat report [<options>]", |
| 1975 | NULL, |
| 1976 | }; |
| 1977 | |
| 1978 | static struct perf_stat perf_stat = { |
| 1979 | .tool = { |
| 1980 | .attr = perf_event__process_attr, |
Jiri Olsa | fa6ea78 | 2015-11-05 15:41:00 +0100 | [diff] [blame] | 1981 | .event_update = perf_event__process_event_update, |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 1982 | .thread_map = process_thread_map_event, |
| 1983 | .cpu_map = process_cpu_map_event, |
Jiri Olsa | 62ba18ba | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 1984 | .stat_config = process_stat_config_event, |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 1985 | .stat = perf_event__process_stat_event, |
| 1986 | .stat_round = process_stat_round_event, |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1987 | }, |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 1988 | .aggr_mode = AGGR_UNSET, |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1989 | }; |
| 1990 | |
| 1991 | static 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 Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 1996 | OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode, |
| 1997 | "aggregate counts per processor socket", AGGR_SOCKET), |
Kan Liang | db5742b | 2019-06-04 15:50:42 -0700 | [diff] [blame] | 1998 | OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode, |
| 1999 | "aggregate counts per processor die", AGGR_DIE), |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 2000 | OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode, |
| 2001 | "aggregate counts per physical processor core", AGGR_CORE), |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 2002 | OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode, |
| 2003 | "aggregate counts per numa node", AGGR_NODE), |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 2004 | OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode, |
| 2005 | "disable CPU count aggregation", AGGR_NONE), |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2006 | OPT_END() |
| 2007 | }; |
| 2008 | struct stat st; |
| 2009 | int ret; |
| 2010 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 2011 | argc = parse_options(argc, argv, options, stat_report_usage, 0); |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2012 | |
| 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 Olsa | 2d4f279 | 2019-02-21 10:41:30 +0100 | [diff] [blame] | 2020 | perf_stat.data.path = input_name; |
| 2021 | perf_stat.data.mode = PERF_DATA_MODE_READ; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2022 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2023 | session = perf_session__new(&perf_stat.data, false, &perf_stat.tool); |
Mamatha Inamdar | 6ef81c5 | 2019-08-22 12:50:49 +0530 | [diff] [blame] | 2024 | if (IS_ERR(session)) |
| 2025 | return PTR_ERR(session); |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2026 | |
| 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 Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2039 | static 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 Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2056 | struct evsel *counter; |
Jiri Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2057 | |
| 2058 | evlist__for_each_entry(evsel_list, counter) { |
Jiri Olsa | 648b5af | 2019-08-06 11:35:19 +0200 | [diff] [blame] | 2059 | if (!counter->core.system_wide) |
Jiri Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2060 | return; |
| 2061 | } |
| 2062 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 2063 | if (evsel_list->core.nr_entries) |
Jiri Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2064 | target.system_wide = true; |
| 2065 | } |
| 2066 | } |
| 2067 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 2068 | int cmd_stat(int argc, const char **argv) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 2069 | { |
Arnaldo Carvalho de Melo | b070a54 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2070 | const char * const stat_usage[] = { |
| 2071 | "perf stat [<options>] [<command>]", |
| 2072 | NULL |
| 2073 | }; |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2074 | int status = -EINVAL, run_idx; |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2075 | const char *mode; |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 2076 | FILE *output = stderr; |
yuzhoujian | f1f8ad5 | 2018-01-29 10:25:23 +0100 | [diff] [blame] | 2077 | unsigned int interval, timeout; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2078 | const char * const stat_subcommands[] = { "record", "report" }; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2079 | |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 2080 | setlocale(LC_ALL, ""); |
| 2081 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 2082 | evsel_list = evlist__new(); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 2083 | if (evsel_list == NULL) |
| 2084 | return -ENOMEM; |
| 2085 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 2086 | parse_events__shrink_config_terms(); |
Michael Petlan | 51433ea | 2018-12-10 11:00:04 -0500 | [diff] [blame] | 2087 | |
| 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 Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2093 | argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands, |
| 2094 | (const char **) stat_usage, |
| 2095 | PARSE_OPT_STOP_AT_NON_OPTION); |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame] | 2096 | perf_stat__collect_metric_expr(evsel_list); |
Andi Kleen | fb4605b | 2016-03-01 10:57:52 -0800 | [diff] [blame] | 2097 | perf_stat__init_shadow_stats(); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2098 | |
Jiri Olsa | fa7070a | 2018-08-30 08:32:29 +0200 | [diff] [blame] | 2099 | 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 Olsa | 6edb78a | 2015-11-05 15:41:01 +0100 | [diff] [blame] | 2103 | } else |
Jiri Olsa | fa7070a | 2018-08-30 08:32:29 +0200 | [diff] [blame] | 2104 | stat_config.csv_sep = DEFAULT_SEPARATOR; |
Jiri Olsa | 6edb78a | 2015-11-05 15:41:01 +0100 | [diff] [blame] | 2105 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2106 | if (argc && !strncmp(argv[0], "rec", 3)) { |
| 2107 | argc = __cmd_record(argc, argv); |
| 2108 | if (argc < 0) |
| 2109 | return -1; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2110 | } else if (argc && !strncmp(argv[0], "rep", 3)) |
| 2111 | return __cmd_report(argc, argv); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2112 | |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 2113 | interval = stat_config.interval; |
yuzhoujian | f1f8ad5 | 2018-01-29 10:25:23 +0100 | [diff] [blame] | 2114 | timeout = stat_config.timeout; |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 2115 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2116 | /* |
| 2117 | * For record command the -o is already taken care of. |
| 2118 | */ |
| 2119 | if (!STAT_RECORD && output_name && strcmp(output_name, "-")) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2120 | output = NULL; |
| 2121 | |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 2122 | if (output_name && output_fd) { |
| 2123 | fprintf(stderr, "cannot use both --output and --log-fd\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2124 | parse_options_usage(stat_usage, stat_options, "o", 1); |
| 2125 | parse_options_usage(NULL, stat_options, "log-fd", 0); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2126 | goto out; |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 2127 | } |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2128 | |
Jiri Olsa | 0ce5aa0 | 2018-08-30 08:32:31 +0200 | [diff] [blame] | 2129 | if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) { |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 2130 | fprintf(stderr, "--metric-only is not supported with --per-thread\n"); |
| 2131 | goto out; |
| 2132 | } |
| 2133 | |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 2134 | if (stat_config.metric_only && stat_config.run_count > 1) { |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 2135 | fprintf(stderr, "--metric-only is not supported with -r\n"); |
| 2136 | goto out; |
| 2137 | } |
| 2138 | |
Jiri Olsa | 54ac0b1 | 2018-08-30 08:32:50 +0200 | [diff] [blame] | 2139 | if (stat_config.walltime_run_table && stat_config.run_count <= 1) { |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 2140 | 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 Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2146 | if (output_fd < 0) { |
| 2147 | fprintf(stderr, "argument to --log-fd must be a > 0\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2148 | parse_options_usage(stat_usage, stat_options, "log-fd", 0); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2149 | goto out; |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2152 | 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 Ahern | fceda7f | 2012-08-26 12:24:44 -0600 | [diff] [blame] | 2159 | return -1; |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2160 | } |
| 2161 | clock_gettime(CLOCK_REALTIME, &tm); |
| 2162 | fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2163 | } else if (output_fd > 0) { |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 2164 | mode = append_file ? "a" : "w"; |
| 2165 | output = fdopen(output_fd, mode); |
| 2166 | if (!output) { |
| 2167 | perror("Failed opening logfd"); |
| 2168 | return -errno; |
| 2169 | } |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 2172 | stat_config.output = output; |
| 2173 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2174 | /* |
| 2175 | * let the spreadsheet do the pretty-printing |
| 2176 | */ |
Jiri Olsa | fa7070a | 2018-08-30 08:32:29 +0200 | [diff] [blame] | 2177 | if (stat_config.csv_output) { |
Jim Cromie | 61a9f32 | 2011-09-07 17:14:04 -0600 | [diff] [blame] | 2178 | /* User explicitly passed -B? */ |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2179 | if (big_num_opt == 1) { |
| 2180 | fprintf(stderr, "-B option not supported with -x\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2181 | parse_options_usage(stat_usage, stat_options, "B", 1); |
| 2182 | parse_options_usage(NULL, stat_options, "x", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2183 | goto out; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2184 | } else /* Nope, so disable big number formatting */ |
Jiri Olsa | 34ff086 | 2018-08-30 08:32:47 +0200 | [diff] [blame] | 2185 | stat_config.big_num = false; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2186 | } else if (big_num_opt == 0) /* User passed --no-big-num */ |
Jiri Olsa | 34ff086 | 2018-08-30 08:32:47 +0200 | [diff] [blame] | 2187 | stat_config.big_num = false; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2188 | |
Jiri Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2189 | setup_system_wide(argc); |
David Ahern | ac3063b | 2013-09-30 07:37:37 -0600 | [diff] [blame] | 2190 | |
Jiri Olsa | 0ce2da1 | 2018-06-05 14:13:13 +0200 | [diff] [blame] | 2191 | /* |
| 2192 | * Display user/system times only for single |
| 2193 | * run and when there's specified tracee. |
| 2194 | */ |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 2195 | if ((stat_config.run_count == 1) && target__none(&target)) |
Jiri Olsa | 8897a89 | 2018-08-30 08:32:44 +0200 | [diff] [blame] | 2196 | stat_config.ru_display = true; |
Jiri Olsa | 0ce2da1 | 2018-06-05 14:13:13 +0200 | [diff] [blame] | 2197 | |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 2198 | if (stat_config.run_count < 0) { |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2199 | pr_err("Run count must be a positive number\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2200 | parse_options_usage(stat_usage, stat_options, "r", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2201 | goto out; |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 2202 | } else if (stat_config.run_count == 0) { |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2203 | forever = true; |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 2204 | stat_config.run_count = 1; |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2205 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2206 | |
Jiri Olsa | 54ac0b1 | 2018-08-30 08:32:50 +0200 | [diff] [blame] | 2207 | 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 Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 2210 | pr_err("failed to setup -r option"); |
| 2211 | goto out; |
| 2212 | } |
| 2213 | } |
| 2214 | |
Jin Yao | 1d9f8d1 | 2017-12-05 22:03:10 +0800 | [diff] [blame] | 2215 | 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 Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2225 | } |
| 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 Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 2231 | if (((stat_config.aggr_mode != AGGR_GLOBAL && |
| 2232 | stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) && |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2233 | !target__has_cpu(&target)) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2234 | fprintf(stderr, "both cgroup and no-aggregation " |
| 2235 | "modes only available in system-wide mode\n"); |
| 2236 | |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2237 | 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 Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2240 | goto out; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2243 | if (add_default_attributes()) |
| 2244 | goto out; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2245 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2246 | target__validate(&target); |
Arnaldo Carvalho de Melo | 5c98d466 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 2247 | |
Jin Yao | 1d9f8d1 | 2017-12-05 22:03:10 +0800 | [diff] [blame] | 2248 | if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide)) |
| 2249 | target.per_thread = true; |
| 2250 | |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 2251 | if (perf_evlist__create_maps(evsel_list, &target) < 0) { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2252 | if (target__has_task(&target)) { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 2253 | pr_err("Problems finding threads of monitor\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2254 | parse_options_usage(stat_usage, stat_options, "p", 1); |
| 2255 | parse_options_usage(NULL, stat_options, "t", 1); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2256 | } else if (target__has_cpu(&target)) { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 2257 | perror("failed to parse CPUs map"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2258 | parse_options_usage(stat_usage, stat_options, "C", 1); |
| 2259 | parse_options_usage(NULL, stat_options, "a", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2260 | } |
| 2261 | goto out; |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 2262 | } |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2263 | |
Jiri Olsa | a9a17902 | 2020-06-02 12:17:36 +0200 | [diff] [blame] | 2264 | evlist__check_cpu_maps(evsel_list); |
| 2265 | |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2266 | /* |
| 2267 | * Initialize thread_map with comm names, |
| 2268 | * so we could print it out on output. |
| 2269 | */ |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 2270 | if (stat_config.aggr_mode == AGGR_THREAD) { |
Jiri Olsa | 03617c2 | 2019-07-21 13:24:42 +0200 | [diff] [blame] | 2271 | thread_map__read_comms(evsel_list->core.threads); |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 2272 | if (target.system_wide) { |
| 2273 | if (runtime_stat_new(&stat_config, |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 2274 | perf_thread_map__nr(evsel_list->core.threads))) { |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 2275 | goto out; |
| 2276 | } |
| 2277 | } |
| 2278 | } |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2279 | |
Jiri Olsa | 86895b4 | 2019-08-28 10:17:43 +0200 | [diff] [blame] | 2280 | if (stat_config.aggr_mode == AGGR_NODE) |
| 2281 | cpu__setup_cpunode_map(); |
| 2282 | |
yuzhoujian | db06a26 | 2018-01-29 10:25:22 +0100 | [diff] [blame] | 2283 | 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 Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 2292 | |
yuzhoujian | f1f8ad5 | 2018-01-29 10:25:23 +0100 | [diff] [blame] | 2293 | 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 Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 2310 | if (perf_evlist__alloc_stats(evsel_list, interval)) |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 2311 | goto out; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 2312 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 2313 | if (perf_stat_init_aggr_mode()) |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 2314 | goto out; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 2315 | |
Ingo Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 2316 | /* |
Jiri Olsa | 7d9ad16 | 2018-08-30 08:32:14 +0200 | [diff] [blame] | 2317 | * 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 Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 2328 | * 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 Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 2333 | atexit(sig_atexit); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2334 | if (!forever) |
| 2335 | signal(SIGINT, skip_signal); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 2336 | signal(SIGCHLD, skip_signal); |
Ingo Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 2337 | signal(SIGALRM, skip_signal); |
| 2338 | signal(SIGABRT, skip_signal); |
| 2339 | |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 2340 | if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack)) |
| 2341 | goto out; |
| 2342 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2343 | status = 0; |
Jiri Olsa | d97ae04 | 2018-08-30 08:32:36 +0200 | [diff] [blame] | 2344 | for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) { |
| 2345 | if (stat_config.run_count != 1 && verbose > 0) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2346 | fprintf(output, "[ perf stat: executing run #%d ... ]\n", |
| 2347 | run_idx + 1); |
Ingo Molnar | f9cef0a | 2011-04-28 18:17:11 +0200 | [diff] [blame] | 2348 | |
Srikar Dronamraju | b63fd11 | 2019-09-04 15:17:37 +0530 | [diff] [blame] | 2349 | if (run_idx != 0) |
| 2350 | perf_evlist__reset_prev_raw_counts(evsel_list); |
| 2351 | |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 2352 | status = run_perf_stat(argc, argv, run_idx); |
Srikar Dronamraju | 443f2d5 | 2019-09-04 15:17:38 +0530 | [diff] [blame] | 2353 | if (forever && status != -1 && !interval) { |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 2354 | print_counters(NULL, argc, argv); |
Jiri Olsa | 254ecbc7 | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 2355 | perf_stat__reset_stats(); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2356 | } |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2357 | } |
| 2358 | |
Jin Yao | c7e5b32 | 2020-05-20 12:27:37 +0800 | [diff] [blame] | 2359 | if (!forever && status != -1 && (!interval || stat_config.summary)) |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 2360 | print_counters(NULL, argc, argv); |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 2361 | |
Alexey Budankov | 27e9769 | 2020-07-17 10:05:41 +0300 | [diff] [blame] | 2362 | evlist__finalize_ctlfd(evsel_list); |
| 2363 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2364 | 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 Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 2372 | * 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 Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2376 | */ |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2377 | int fd = perf_data__fd(&perf_stat.data); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2378 | 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 Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 2386 | 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 Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2391 | if (!perf_stat.data.is_pipe) { |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 2392 | perf_stat.session->header.data_size += perf_stat.bytes_written; |
| 2393 | perf_session__write_header(perf_stat.session, evsel_list, fd, true); |
| 2394 | } |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2395 | |
Jiri Olsa | 750b4ed | 2019-07-21 13:24:07 +0200 | [diff] [blame] | 2396 | evlist__close(evsel_list); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2397 | perf_session__delete(perf_stat.session); |
| 2398 | } |
| 2399 | |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 2400 | perf_stat__exit_aggr_mode(); |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 2401 | perf_evlist__free_stats(evsel_list); |
Arnaldo Carvalho de Melo | 0015e2e | 2011-02-01 16:18:10 -0200 | [diff] [blame] | 2402 | out: |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 2403 | zfree(&stat_config.walltime_run); |
Jiri Olsa | e55c14a | 2018-04-23 11:08:21 +0200 | [diff] [blame] | 2404 | |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 2405 | if (smi_cost && smi_reset) |
| 2406 | sysfs__write_int(FREEZE_ON_SMI_PATH, 0); |
| 2407 | |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 2408 | evlist__delete(evsel_list); |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 2409 | |
Jiri Olsa | 9afe565 | 2020-06-02 23:47:38 +0200 | [diff] [blame] | 2410 | metricgroup__rblist_exit(&stat_config.metric_events); |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 2411 | runtime_stat_delete(&stat_config); |
Adrian Hunter | a8fcbd2 | 2020-09-02 13:57:07 +0300 | [diff] [blame^] | 2412 | close_control_option(&stat_config); |
Jin Yao | 5673944 | 2017-12-05 22:03:07 +0800 | [diff] [blame] | 2413 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2414 | return status; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2415 | } |