^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * builtin-stat.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Builtin stat command: Give a precise performance counters summary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * overview about any workload, CPU or specific PID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Sample output:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) $ perf stat ./hackbench 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) Time: 0.118
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) Performance counter stats for './hackbench 10':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) 1708.761321 task-clock # 11.037 CPUs utilized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) 41,190 context-switches # 0.024 M/sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) 6,735 CPU-migrations # 0.004 M/sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) 17,318 page-faults # 0.010 M/sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) 5,205,202,243 cycles # 3.046 GHz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 2,603,501,247 instructions # 0.50 insns per cycle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # 1.48 stalled cycles per insn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) 484,357,498 branches # 283.455 M/sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 6,388,934 branch-misses # 1.32% of all branches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 0.154822978 seconds time elapsed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Improvements and fixes by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Arjan van de Ven <arjan@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Yanmin Zhang <yanmin.zhang@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Wu Fengguang <fengguang.wu@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Mike Galbraith <efault@gmx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * Paul Mackerras <paulus@samba.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Jaswinder Singh Rajput <jaswinder@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "perf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include "util/cgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "util/parse-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "util/pmu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "util/event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include "util/evlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include "util/evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include "util/color.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include "util/stat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include "util/header.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include "util/cpumap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include "util/thread_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include "util/counts.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include "util/topdown.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include "util/session.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include "util/tool.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include "util/string2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include "util/metricgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include "util/synthetic-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include "util/target.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include "util/time-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include "util/top.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include "util/affinity.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #include "util/pfm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #include "asm/bug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #include <api/fs/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #include <locale.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #include <math.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #include <perf/evlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define DEFAULT_SEPARATOR " "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void print_counters(struct timespec *ts, int argc, const char **argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Default events used for perf stat -T */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static const char *transaction_attrs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) "task-clock,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) "{"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) "instructions,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "cycles,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) "cpu/cycles-t/,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) "cpu/tx-start/,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) "cpu/el-start/,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "cpu/cycles-ct/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) "}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* More limited version when the CPU does not have all events. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static const char * transaction_limited_attrs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "task-clock,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "{"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "instructions,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "cycles,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "cpu/cycles-t/,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "cpu/tx-start/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static const char * topdown_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "topdown-total-slots",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) "topdown-slots-retired",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) "topdown-recovery-bubbles",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "topdown-fetch-bubbles",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) "topdown-slots-issued",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static const char *topdown_metric_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) "slots",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) "topdown-retiring",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) "topdown-bad-spec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) "topdown-fe-bound",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) "topdown-be-bound",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static const char *smi_cost_attrs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) "{"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) "msr/aperf/,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) "msr/smi/,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) "cycles"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) "}"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static struct evlist *evsel_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static struct target target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .uid = UINT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define METRIC_ONLY_LEN 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static volatile pid_t child_pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int detailed_run = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static bool transaction_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static bool topdown_run = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static bool smi_cost = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static bool smi_reset = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int big_num_opt = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static bool group = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static const char *pre_cmd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static const char *post_cmd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static bool sync_run = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static bool forever = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static bool force_metric_only = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static struct timespec ref_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static bool append_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static bool interval_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static const char *output_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int output_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct perf_stat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) bool record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct perf_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct perf_session *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) u64 bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct perf_tool tool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) bool maps_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct perf_cpu_map *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct perf_thread_map *threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) enum aggr_mode aggr_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static struct perf_stat perf_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #define STAT_RECORD perf_stat.record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static volatile int done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static struct perf_stat_config stat_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .aggr_mode = AGGR_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .scale = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .unit_width = 4, /* strlen("unit") */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .run_count = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .metric_only_len = METRIC_ONLY_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .walltime_nsecs_stats = &walltime_nsecs_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .big_num = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .ctl_fd = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .ctl_fd_ack = -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static bool cpus_map_matched(struct evsel *a, struct evsel *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!a->core.cpus && !b->core.cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!a->core.cpus || !b->core.cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (a->core.cpus->nr != b->core.cpus->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) for (int i = 0; i < a->core.cpus->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (a->core.cpus->map[i] != b->core.cpus->map[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static void evlist__check_cpu_maps(struct evlist *evlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct evsel *evsel, *pos, *leader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) char buf[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) evlist__for_each_entry(evlist, evsel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) leader = evsel->leader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* Check that leader matches cpus with each member. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (leader == evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (cpus_map_matched(leader, evsel))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* If there's mismatch disable the group and warn user. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) WARN_ONCE(1, "WARNING: grouped events cpus do not match, disabling group:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) evsel__group_desc(leader, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) pr_warning(" %s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) cpu_map__snprint(leader->core.cpus, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pr_warning(" %s: %s\n", leader->name, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) pr_warning(" %s: %s\n", evsel->name, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) for_each_group_evsel(pos, leader) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) pos->leader = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) pos->core.nr_members = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) evsel->leader->core.nr_members = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static inline void diff_timespec(struct timespec *r, struct timespec *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct timespec *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) r->tv_sec = a->tv_sec - b->tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (a->tv_nsec < b->tv_nsec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) r->tv_sec--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) r->tv_nsec = a->tv_nsec - b->tv_nsec ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void perf_stat__reset_stats(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) perf_evlist__reset_stats(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) perf_stat__reset_shadow_stats();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) for (i = 0; i < stat_config.stats_num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct perf_sample *sample __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct machine *machine __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pr_err("failed to write perf data, error: %m\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) perf_stat.bytes_written += event->header.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int write_stat_round_event(u64 tm, u64 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return perf_event__synthesize_stat_round(NULL, tm, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) process_synthesized_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) #define WRITE_STAT_ROUND_EVENT(time, interval) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static int evsel__write_stat_event(struct evsel *counter, u32 cpu, u32 thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct perf_counts_values *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct perf_sample_id *sid = SID(counter, cpu, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) process_synthesized_event, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int read_single_counter(struct evsel *counter, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int thread, struct timespec *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (counter->tool_event == PERF_TOOL_DURATION_TIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) u64 val = rs->tv_nsec + rs->tv_sec*1000000000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct perf_counts_values *count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) perf_counts(counter->counts, cpu, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) count->ena = count->run = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) count->val = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return evsel__read_counter(counter, cpu, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * Read out the results of a single counter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * do not aggregate counts across CPUs in system-wide mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static int read_counter_cpu(struct evsel *counter, struct timespec *rs, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int nthreads = perf_thread_map__nr(evsel_list->core.threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (!counter->supported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (counter->core.system_wide)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) nthreads = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) for (thread = 0; thread < nthreads; thread++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct perf_counts_values *count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) count = perf_counts(counter->counts, cpu, thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * The leader's group read loads data into its group members
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * (via evsel__read_counter()) and sets their count->loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!perf_counts__is_loaded(counter->counts, cpu, thread) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) read_single_counter(counter, cpu, thread, rs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) counter->counts->scaled = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) perf_counts(counter->counts, cpu, thread)->ena = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) perf_counts(counter->counts, cpu, thread)->run = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) perf_counts__set_loaded(counter->counts, cpu, thread, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (STAT_RECORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (evsel__write_stat_event(counter, cpu, thread, count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) pr_err("failed to write stat event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (verbose > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) fprintf(stat_config.output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) evsel__name(counter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) count->val, count->ena, count->run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static int read_affinity_counters(struct timespec *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct evsel *counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct affinity affinity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int i, ncpus, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (affinity__setup(&affinity) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ncpus = perf_cpu_map__nr(evsel_list->core.all_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (!target__has_cpu(&target) || target__has_per_thread(&target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ncpus = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) evlist__for_each_cpu(evsel_list, i, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (i >= ncpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) affinity__set(&affinity, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (evsel__cpu_iter_skip(counter, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (!counter->err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) counter->err = read_counter_cpu(counter, rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) counter->cpu_iter - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) affinity__cleanup(&affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static void read_counters(struct timespec *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct evsel *counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!stat_config.stop_read_counter && (read_affinity_counters(rs) < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (counter->err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) pr_debug("failed to read counter %s\n", counter->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) pr_warning("failed to process counter %s\n", counter->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) counter->err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) config->stats = calloc(nthreads, sizeof(struct runtime_stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!config->stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) config->stats_num = nthreads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) for (i = 0; i < nthreads; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) runtime_stat__init(&config->stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static void runtime_stat_delete(struct perf_stat_config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!config->stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) for (i = 0; i < config->stats_num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) runtime_stat__exit(&config->stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) zfree(&config->stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static void runtime_stat_reset(struct perf_stat_config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (!config->stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) for (i = 0; i < config->stats_num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) perf_stat__reset_shadow_per_stat(&config->stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static void process_interval(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct timespec ts, rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) clock_gettime(CLOCK_MONOTONIC, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) diff_timespec(&rs, &ts, &ref_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) perf_stat__reset_shadow_per_stat(&rt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) runtime_stat_reset(&stat_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) read_counters(&rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (STAT_RECORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_err("failed to write stat round event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) init_stats(&walltime_nsecs_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000ULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) print_counters(&rs, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static bool handle_interval(unsigned int interval, int *times)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) process_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (interval_count && !(--(*times)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) static void enable_counters(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (stat_config.initial_delay < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) pr_info(EVLIST_DISABLED_MSG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (stat_config.initial_delay > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) pr_info(EVLIST_DISABLED_MSG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) usleep(stat_config.initial_delay * USEC_PER_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * We need to enable counters only if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * - we don't have tracee (attaching to task or cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * - we have initial delay configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (!target__none(&target) || stat_config.initial_delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) evlist__enable(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (stat_config.initial_delay > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) pr_info(EVLIST_ENABLED_MSG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static void disable_counters(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * If we don't have tracee (attaching to task or cpu), counters may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * still be running. To get accurate group ratios, we must stop groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * from counting before reading their constituent counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!target__none(&target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) evlist__disable(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static volatile int workload_exec_errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * perf_evlist__prepare_workload will send a SIGUSR1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * if the fork fails, since we asked by setting its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * want_signal to true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) void *ucontext __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) workload_exec_errno = info->si_value.sival_int;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static bool evsel__should_store_id(struct evsel *counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static bool is_target_alive(struct target *_target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct perf_thread_map *threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (!target__has_task(_target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) for (i = 0; i < threads->nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) threads->map[i].pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (!stat(path, &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static void process_evlist(struct evlist *evlist, unsigned int interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (evlist__ctlfd_process(evlist, &cmd) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) case EVLIST_CTL_CMD_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) pr_info(EVLIST_ENABLED_MSG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) process_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) case EVLIST_CTL_CMD_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) process_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) pr_info(EVLIST_DISABLED_MSG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) case EVLIST_CTL_CMD_SNAPSHOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) case EVLIST_CTL_CMD_ACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) case EVLIST_CTL_CMD_UNSUPPORTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static void compute_tts(struct timespec *time_start, struct timespec *time_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int *time_to_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) int tts = *time_to_sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct timespec time_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) diff_timespec(&time_diff, time_stop, time_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) tts -= time_diff.tv_sec * MSEC_PER_SEC +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) time_diff.tv_nsec / NSEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (tts < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) tts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) *time_to_sleep = tts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) static int dispatch_events(bool forks, int timeout, int interval, int *times)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int child_exited = 0, status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) int time_to_sleep, sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) struct timespec time_start, time_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) sleep_time = interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) else if (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) sleep_time = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) sleep_time = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) time_to_sleep = sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) while (!done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (forks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) child_exited = waitpid(child_pid, &status, WNOHANG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (child_exited)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) clock_gettime(CLOCK_MONOTONIC, &time_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (timeout || handle_interval(interval, times))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) time_to_sleep = sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) } else { /* fd revent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) process_evlist(evsel_list, interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) clock_gettime(CLOCK_MONOTONIC, &time_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) compute_tts(&time_start, &time_stop, &time_to_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) enum counter_recovery {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) COUNTER_SKIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) COUNTER_RETRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) COUNTER_FATAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static enum counter_recovery stat_handle_error(struct evsel *counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) char msg[BUFSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * PPC returns ENXIO for HW counters until 2.6.37
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * (behavior changed with commit b0a873e).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (errno == EINVAL || errno == ENOSYS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) errno == ENOENT || errno == EOPNOTSUPP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) errno == ENXIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (verbose > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) ui__warning("%s event is not supported by the kernel.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) evsel__name(counter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) counter->supported = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * errored is a sticky flag that means one of the counter's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * cpu event had a problem and needs to be reexamined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) counter->errored = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if ((counter->leader != counter) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) !(counter->leader->core.nr_members > 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return COUNTER_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) } else if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (verbose > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) ui__warning("%s\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return COUNTER_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) } else if (target__has_per_thread(&target) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) evsel_list->core.threads &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) evsel_list->core.threads->err_thread != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * For global --per-thread case, skip current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * error thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (!thread_map__remove(evsel_list->core.threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) evsel_list->core.threads->err_thread)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) evsel_list->core.threads->err_thread = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return COUNTER_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) evsel__open_strerror(counter, &target, errno, msg, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) ui__error("%s\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (child_pid != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) kill(child_pid, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return COUNTER_FATAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static int __run_perf_stat(int argc, const char **argv, int run_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) int interval = stat_config.interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) int times = stat_config.times;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) int timeout = stat_config.timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) char msg[BUFSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) unsigned long long t0, t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct evsel *counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) size_t l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) const bool forks = (argc > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct affinity affinity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int i, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) bool second_pass = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (forks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) workload_exec_failed_signal) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) perror("failed to prepare workload");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) child_pid = evsel_list->workload.pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) perf_evlist__set_leader(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (affinity__setup(&affinity) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) evlist__for_each_cpu (evsel_list, i, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) affinity__set(&affinity, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (evsel__cpu_iter_skip(counter, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (counter->reset_group || counter->errored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (create_perf_stat_counter(counter, &stat_config, &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) counter->cpu_iter - 1) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * Weak group failed. We cannot just undo this here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * because earlier CPUs might be in group mode, and the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * doesn't support mixing group and non group reads. Defer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * it to later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * Don't close here because we're in the wrong affinity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if ((errno == EINVAL || errno == EBADF) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) counter->leader != counter &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) counter->weak_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) perf_evlist__reset_weak_group(evsel_list, counter, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) assert(counter->reset_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) second_pass = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) switch (stat_handle_error(counter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) case COUNTER_FATAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) case COUNTER_RETRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) case COUNTER_SKIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) counter->supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (second_pass) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * Now redo all the weak group after closing them,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * and also close errored counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) evlist__for_each_cpu(evsel_list, i, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) affinity__set(&affinity, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) /* First close errored or weak retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (!counter->reset_group && !counter->errored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (evsel__cpu_iter_skip_no_inc(counter, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) perf_evsel__close_cpu(&counter->core, counter->cpu_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) /* Now reopen weak */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (!counter->reset_group && !counter->errored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (evsel__cpu_iter_skip(counter, cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (!counter->reset_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) try_again_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) pr_debug2("reopening weak %s\n", evsel__name(counter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (create_perf_stat_counter(counter, &stat_config, &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) counter->cpu_iter - 1) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) switch (stat_handle_error(counter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) case COUNTER_FATAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) case COUNTER_RETRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) goto try_again_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) case COUNTER_SKIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) counter->supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) affinity__cleanup(&affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (!counter->supported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) perf_evsel__free_fd(&counter->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) l = strlen(counter->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (l > stat_config.unit_width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) stat_config.unit_width = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) if (evsel__should_store_id(counter) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) evsel__store_ids(counter, evsel_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (perf_evlist__apply_filters(evsel_list, &counter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) counter->filter, evsel__name(counter), errno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) str_error_r(errno, msg, sizeof(msg)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (STAT_RECORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) int err, fd = perf_data__fd(&perf_stat.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (is_pipe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) err = perf_session__write_header(perf_stat.session, evsel_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) fd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) process_synthesized_event, is_pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * Enable counters and exec the command:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) t0 = rdclock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) clock_gettime(CLOCK_MONOTONIC, &ref_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (forks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) perf_evlist__start_workload(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) enable_counters();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (interval || timeout || evlist__ctlfd_initialized(evsel_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) status = dispatch_events(forks, timeout, interval, ×);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (child_pid != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) kill(child_pid, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) wait4(child_pid, &status, 0, &stat_config.ru_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (workload_exec_errno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) pr_err("Workload failed: %s\n", emsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) if (WIFSIGNALED(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) psignal(WTERMSIG(status), argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) enable_counters();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) status = dispatch_events(forks, timeout, interval, ×);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) disable_counters();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) t1 = rdclock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (stat_config.walltime_run_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) stat_config.walltime_run[run_idx] = t1 - t0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (interval && stat_config.summary) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) stat_config.interval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) stat_config.stop_read_counter = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) init_stats(&walltime_nsecs_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) update_stats(&walltime_nsecs_stats, t1 - t0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (stat_config.aggr_mode == AGGR_GLOBAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) perf_evlist__save_aggr_prev_raw_counts(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) perf_evlist__copy_prev_raw_counts(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) perf_evlist__reset_prev_raw_counts(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) runtime_stat_reset(&stat_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) perf_stat__reset_shadow_per_stat(&rt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) update_stats(&walltime_nsecs_stats, t1 - t0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * Closing a group leader splits the group, and as we only disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * group leaders, results in remaining events becoming enabled. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * avoid arbitrary skew, we must read all counters before closing any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * group leaders.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) read_counters(&(struct timespec) { .tv_nsec = t1-t0 });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * We need to keep evsel_list alive, because it's processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) * later the evsel_list will be closed after.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (!STAT_RECORD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) evlist__close(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) return WEXITSTATUS(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) static int run_perf_stat(int argc, const char **argv, int run_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (pre_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) ret = system(pre_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (sync_run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) ret = __run_perf_stat(argc, argv, run_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if (post_cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) ret = system(post_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) static void print_counters(struct timespec *ts, int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) /* Do not print anything if we record to the pipe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (STAT_RECORD && perf_stat.data.is_pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) perf_evlist__print_counters(evsel_list, &stat_config, &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) ts, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) static volatile int signr = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) static void skip_signal(int signo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if ((child_pid == -1) || stat_config.interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) signr = signo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * render child_pid harmless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * won't send SIGTERM to a random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) * process in case of race condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * and fast PID recycling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) child_pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) static void sig_atexit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) sigset_t set, oset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * avoid race condition with SIGCHLD handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * in skip_signal() which is modifying child_pid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * goal is to avoid send SIGTERM to a random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) * process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) sigemptyset(&set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) sigaddset(&set, SIGCHLD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) sigprocmask(SIG_BLOCK, &set, &oset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (child_pid != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) kill(child_pid, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) sigprocmask(SIG_SETMASK, &oset, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (signr == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) signal(signr, SIG_DFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) kill(getpid(), signr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) void perf_stat__set_big_num(int set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) stat_config.big_num = (set != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) static int stat__set_big_num(const struct option *opt __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) const char *s __maybe_unused, int unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) big_num_opt = unset ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) perf_stat__set_big_num(!unset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) static int enable_metric_only(const struct option *opt __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) const char *s __maybe_unused, int unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) force_metric_only = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) stat_config.metric_only = !unset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static int parse_metric_groups(const struct option *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) const char *str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) int unset __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) return metricgroup__parse_groups(opt, str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) stat_config.metric_no_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) stat_config.metric_no_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) &stat_config.metric_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) static int parse_control_option(const struct option *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) const char *str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) int unset __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) struct perf_stat_config *config = opt->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) static int parse_stat_cgroups(const struct option *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) const char *str, int unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (stat_config.cgroup_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) return parse_cgroups(opt, str, unset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) static struct option stat_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) OPT_BOOLEAN('T', "transaction", &transaction_run,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) "hardware transaction statistics"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) OPT_CALLBACK('e', "event", &evsel_list, "event",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) "event selector. use 'perf list' to list available events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) parse_events_option),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) OPT_CALLBACK(0, "filter", &evsel_list, "filter",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) "event filter", parse_filter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) "child tasks do not inherit counters"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) OPT_STRING('p', "pid", &target.pid, "pid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) "stat events on existing process id"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) OPT_STRING('t', "tid", &target.tid, "tid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) "stat events on existing thread id"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) "system-wide collection from all CPUs"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) OPT_BOOLEAN('g', "group", &group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) "put the counters into a counter group"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) OPT_BOOLEAN(0, "scale", &stat_config.scale,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) "Use --no-scale to disable counter scaling for multiplexing"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) OPT_INCR('v', "verbose", &verbose,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) "be more verbose (show counter open errors, etc)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) OPT_INTEGER('r', "repeat", &stat_config.run_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) "repeat command and print average + stddev (max: 100, forever: 0)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) "display details about each run (only with -r option)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) OPT_BOOLEAN('n', "null", &stat_config.null_run,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) "null run - dont start any counters"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) OPT_INCR('d', "detailed", &detailed_run,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) "detailed run - start a lot of events"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) OPT_BOOLEAN('S', "sync", &sync_run,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) "call sync() before starting a run"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) "print large numbers with thousands\' separators",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) stat__set_big_num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) "list of cpus to monitor in system-wide"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) "disable CPU count aggregation", AGGR_NONE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) "print counts with custom separator"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) "monitor event in cgroup name only", parse_stat_cgroups),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) OPT_STRING(0, "for-each-cgroup", &stat_config.cgroup_list, "name",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) "expand events for each cgroup"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) OPT_STRING('o', "output", &output_name, "file", "output file name"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) OPT_INTEGER(0, "log-fd", &output_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) "log output to fd, instead of stderr"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) OPT_STRING(0, "pre", &pre_cmd, "command",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) "command to run prior to the measured command"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) OPT_STRING(0, "post", &post_cmd, "command",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) "command to run after to the measured command"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) OPT_UINTEGER('I', "interval-print", &stat_config.interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) "print counts at regular interval in ms "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) "(overhead is possible for values <= 100ms)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) OPT_INTEGER(0, "interval-count", &stat_config.times,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) "print counts for fixed number of times"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) "clear screen in between new interval"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) OPT_UINTEGER(0, "timeout", &stat_config.timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) "stop workload and print counts after a timeout period in ms (>= 10ms)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) "aggregate counts per processor socket", AGGR_SOCKET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) OPT_SET_UINT(0, "per-die", &stat_config.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) "aggregate counts per processor die", AGGR_DIE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) "aggregate counts per physical processor core", AGGR_CORE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) "aggregate counts per thread", AGGR_THREAD),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) OPT_SET_UINT(0, "per-node", &stat_config.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) "aggregate counts per numa node", AGGR_NODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) OPT_INTEGER('D', "delay", &stat_config.initial_delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) "ms to wait before starting measurement after program start (-1: start with events disabled)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) "Only print computed metrics. No raw values", enable_metric_only),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) OPT_BOOLEAN(0, "metric-no-group", &stat_config.metric_no_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) "don't group metric events, impacts multiplexing"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) "don't try to share events between metrics in a group"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) OPT_BOOLEAN(0, "topdown", &topdown_run,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) "measure topdown level 1 statistics"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) OPT_BOOLEAN(0, "smi-cost", &smi_cost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) "measure SMI cost"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) "monitor specified metrics or metric groups (separated by ,)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) parse_metric_groups),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) "Configure all used events to run in kernel space.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) PARSE_OPT_EXCLUSIVE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) OPT_BOOLEAN_FLAG(0, "all-user", &stat_config.all_user,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) "Configure all used events to run in user space.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) PARSE_OPT_EXCLUSIVE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) OPT_BOOLEAN(0, "percore-show-thread", &stat_config.percore_show_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) "Use with 'percore' event qualifier to show the event "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) "counts of one hardware thread by sum up total hardware "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) "threads of same physical core"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) OPT_BOOLEAN(0, "summary", &stat_config.summary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) "print summary for interval mode"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) #ifdef HAVE_LIBPFM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) "libpfm4 event selector. use 'perf list' to list available events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) parse_libpfm_events_option),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) parse_control_option),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static int perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) struct perf_cpu_map *map, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return cpu_map__get_socket(map, cpu, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static int perf_stat__get_die(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) struct perf_cpu_map *map, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) return cpu_map__get_die(map, cpu, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) static int perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) struct perf_cpu_map *map, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) return cpu_map__get_core(map, cpu, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) static int perf_stat__get_node(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) struct perf_cpu_map *map, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) return cpu_map__get_node(map, cpu, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) static int perf_stat__get_aggr(struct perf_stat_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) aggr_get_id_t get_id, struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (idx >= map->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) cpu = map->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) if (config->cpus_aggr_map->map[cpu] == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) return config->cpus_aggr_map->map[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) static int perf_stat__get_socket_cached(struct perf_stat_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) static int perf_stat__get_die_cached(struct perf_stat_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) return perf_stat__get_aggr(config, perf_stat__get_die, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) static int perf_stat__get_core_cached(struct perf_stat_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) static int perf_stat__get_node_cached(struct perf_stat_config *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) return perf_stat__get_aggr(config, perf_stat__get_node, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static bool term_percore_set(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) struct evsel *counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (counter->percore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) static int perf_stat_init_aggr_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) switch (stat_config.aggr_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) case AGGR_SOCKET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) if (cpu_map__build_socket_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) perror("cannot build socket map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) stat_config.aggr_get_id = perf_stat__get_socket_cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) case AGGR_DIE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) if (cpu_map__build_die_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) perror("cannot build die map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) stat_config.aggr_get_id = perf_stat__get_die_cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) case AGGR_CORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (cpu_map__build_core_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) perror("cannot build core map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) stat_config.aggr_get_id = perf_stat__get_core_cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) case AGGR_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) if (cpu_map__build_node_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) perror("cannot build core map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) stat_config.aggr_get_id = perf_stat__get_node_cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) case AGGR_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) if (term_percore_set()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (cpu_map__build_core_map(evsel_list->core.cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) perror("cannot build core map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) stat_config.aggr_get_id = perf_stat__get_core_cached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) case AGGR_GLOBAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) case AGGR_THREAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) case AGGR_UNSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) * The evsel_list->cpus is the base we operate on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) * taking the highest cpu number to be the size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) * the aggregation translate cpumap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) nr = perf_cpu_map__max(evsel_list->core.cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) stat_config.cpus_aggr_map = perf_cpu_map__empty_new(nr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static void perf_stat__exit_aggr_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) perf_cpu_map__put(stat_config.aggr_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) perf_cpu_map__put(stat_config.cpus_aggr_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) stat_config.aggr_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) stat_config.cpus_aggr_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) static inline int perf_env__get_cpu(struct perf_env *env, struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) if (idx > map->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) cpu = map->map[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (cpu >= env->nr_cpus_avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) return cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) static int perf_env__get_socket(struct perf_cpu_map *map, int idx, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) struct perf_env *env = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) int cpu = perf_env__get_cpu(env, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) static int perf_env__get_die(struct perf_cpu_map *map, int idx, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) struct perf_env *env = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) int die_id = -1, cpu = perf_env__get_cpu(env, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (cpu != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) * Encode socket in bit range 15:8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) * die_id is relative to socket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) * we need a global id. So we combine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) * socket + die id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) if (WARN_ONCE(env->cpu[cpu].socket_id >> 8, "The socket id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) if (WARN_ONCE(env->cpu[cpu].die_id >> 8, "The die id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) die_id = (env->cpu[cpu].socket_id << 8) | (env->cpu[cpu].die_id & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) return die_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) static int perf_env__get_core(struct perf_cpu_map *map, int idx, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) struct perf_env *env = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) int core = -1, cpu = perf_env__get_cpu(env, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (cpu != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) * Encode socket in bit range 31:24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) * encode die id in bit range 23:16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) * core_id is relative to socket and die,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) * we need a global id. So we combine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) * socket + die id + core id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) if (WARN_ONCE(env->cpu[cpu].socket_id >> 8, "The socket id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) if (WARN_ONCE(env->cpu[cpu].die_id >> 8, "The die id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) if (WARN_ONCE(env->cpu[cpu].core_id >> 16, "The core id number is too big.\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) core = (env->cpu[cpu].socket_id << 24) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) (env->cpu[cpu].die_id << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) (env->cpu[cpu].core_id & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) return core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) static int perf_env__get_node(struct perf_cpu_map *map, int idx, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) int cpu = perf_env__get_cpu(data, map, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) return perf_env__numa_node(data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) static int perf_env__build_socket_map(struct perf_env *env, struct perf_cpu_map *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) struct perf_cpu_map **sockp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) static int perf_env__build_die_map(struct perf_env *env, struct perf_cpu_map *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) struct perf_cpu_map **diep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) return cpu_map__build_map(cpus, diep, perf_env__get_die, env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) static int perf_env__build_core_map(struct perf_env *env, struct perf_cpu_map *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) struct perf_cpu_map **corep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) static int perf_env__build_node_map(struct perf_env *env, struct perf_cpu_map *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) struct perf_cpu_map **nodep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) return cpu_map__build_map(cpus, nodep, perf_env__get_node, env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) static int perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) static int perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return perf_env__get_die(map, idx, &perf_stat.session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) static int perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) return perf_env__get_core(map, idx, &perf_stat.session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) static int perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) struct perf_cpu_map *map, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) return perf_env__get_node(map, idx, &perf_stat.session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) struct perf_env *env = &st->session->header.env;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) switch (stat_config.aggr_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) case AGGR_SOCKET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) if (perf_env__build_socket_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) perror("cannot build socket map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) stat_config.aggr_get_id = perf_stat__get_socket_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) case AGGR_DIE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (perf_env__build_die_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) perror("cannot build die map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) stat_config.aggr_get_id = perf_stat__get_die_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) case AGGR_CORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) if (perf_env__build_core_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) perror("cannot build core map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) stat_config.aggr_get_id = perf_stat__get_core_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) case AGGR_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) if (perf_env__build_node_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) perror("cannot build core map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) stat_config.aggr_get_id = perf_stat__get_node_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) case AGGR_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) case AGGR_GLOBAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) case AGGR_THREAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) case AGGR_UNSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) * Add default attributes, if there were no attributes specified or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) * if -d/--detailed, -d -d or -d -d -d is used:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) static int add_default_attributes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) struct perf_event_attr default_attrs0[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) struct perf_event_attr frontend_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) struct perf_event_attr backend_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) struct perf_event_attr default_attrs1[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) * Detailed stats (-d), covering the L1 and last level data caches:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) struct perf_event_attr detailed_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) PERF_COUNT_HW_CACHE_L1D << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) PERF_COUNT_HW_CACHE_L1D << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) PERF_COUNT_HW_CACHE_LL << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) PERF_COUNT_HW_CACHE_LL << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) struct perf_event_attr very_detailed_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) PERF_COUNT_HW_CACHE_L1I << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) PERF_COUNT_HW_CACHE_L1I << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) PERF_COUNT_HW_CACHE_DTLB << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) PERF_COUNT_HW_CACHE_DTLB << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) PERF_COUNT_HW_CACHE_ITLB << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) PERF_COUNT_HW_CACHE_ITLB << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) (PERF_COUNT_HW_CACHE_OP_READ << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * Very, very detailed stats (-d -d -d), adding prefetch events:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) struct perf_event_attr very_very_detailed_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) PERF_COUNT_HW_CACHE_L1D << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) { .type = PERF_TYPE_HW_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) .config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) PERF_COUNT_HW_CACHE_L1D << 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) struct parse_events_error errinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) /* Set attrs if no event is selected and !null_run: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) if (stat_config.null_run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) bzero(&errinfo, sizeof(errinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) if (transaction_run) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) /* Handle -T as -M transaction. Once platform specific metrics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) * support has been added to the json files, all archictures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) * will use this approach. To determine transaction support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) * on an architecture test for such a metric name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) if (metricgroup__has_metric("transaction")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) struct option opt = { .value = &evsel_list };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) return metricgroup__parse_groups(&opt, "transaction",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) stat_config.metric_no_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) stat_config.metric_no_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) &stat_config.metric_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) if (pmu_have_event("cpu", "cycles-ct") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) pmu_have_event("cpu", "el-start"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) err = parse_events(evsel_list, transaction_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) &errinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) err = parse_events(evsel_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) transaction_limited_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) &errinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) fprintf(stderr, "Cannot set up transaction events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) parse_events_print_error(&errinfo, transaction_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) if (smi_cost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) int smi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) fprintf(stderr, "freeze_on_smi is not supported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) if (!smi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) fprintf(stderr, "Failed to set freeze_on_smi.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) smi_reset = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (pmu_have_event("msr", "aperf") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) pmu_have_event("msr", "smi")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) if (!force_metric_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) stat_config.metric_only = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) fprintf(stderr, "To measure SMI cost, it needs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) parse_events_print_error(&errinfo, smi_cost_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) parse_events_print_error(&errinfo, smi_cost_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) fprintf(stderr, "Cannot set up SMI cost events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) if (topdown_run) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) char *str = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) bool warn = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) if (!force_metric_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) stat_config.metric_only = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) if (topdown_filter_events(topdown_metric_attrs, &str, 1) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) pr_err("Out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) if (topdown_metric_attrs[0] && str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (!stat_config.interval && !stat_config.metric_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) fprintf(stat_config.output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) "Topdown accuracy may decrease when measuring long periods.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) "Please print the result regularly, e.g. -I1000\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) goto setup_metrics;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) zfree(&str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) if (stat_config.aggr_mode != AGGR_GLOBAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) stat_config.aggr_mode != AGGR_CORE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) pr_err("top down event configuration requires --per-core mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) stat_config.aggr_mode = AGGR_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) if (nr_cgroups || !target__has_cpu(&target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) pr_err("top down event configuration requires system-wide mode (-a)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) if (topdown_filter_events(topdown_attrs, &str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) arch_topdown_check_group(&warn)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) pr_err("Out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (topdown_attrs[0] && str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) arch_topdown_group_warn();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) setup_metrics:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) err = parse_events(evsel_list, str, &errinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) "Cannot set up top down events %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) str, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) parse_events_print_error(&errinfo, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) fprintf(stderr, "System does not support topdown\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) free(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) if (!evsel_list->core.nr_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) if (target__has_cpu(&target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) if (evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) if (evlist__add_default_attrs(evsel_list, frontend_attrs) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) if (pmu_have_event("cpu", "stalled-cycles-backend")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) if (evlist__add_default_attrs(evsel_list, backend_attrs) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) /* Detailed events get appended to the event list: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) if (detailed_run < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) /* Append detailed run extra attributes: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) if (evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) if (detailed_run < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) /* Append very detailed run extra attributes: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) if (evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (detailed_run < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) /* Append very, very detailed run extra attributes: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) return evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) static const char * const stat_record_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) "perf stat record [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) static void init_features(struct perf_session *session)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) int feat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) perf_header__set_feat(&session->header, feat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) static int __cmd_record(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) struct perf_session *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) struct perf_data *data = &perf_stat.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) argc = parse_options(argc, argv, stat_options, stat_record_usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) PARSE_OPT_STOP_AT_NON_OPTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) if (output_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) data->path = output_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) if (stat_config.run_count != 1 || forever) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) pr_err("Cannot use -r option with perf stat record.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) session = perf_session__new(data, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) if (IS_ERR(session)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) pr_err("Perf session creation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) return PTR_ERR(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) init_features(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) session->evlist = evsel_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) perf_stat.session = session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) perf_stat.record = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) return argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) static int process_stat_round_event(struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) struct perf_record_stat_round *stat_round = &event->stat_round;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) struct evsel *counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) struct timespec tsh, *ts = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) const char **argv = session->header.env.cmdline_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) int argc = session->header.env.nr_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) evlist__for_each_entry(evsel_list, counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) perf_stat_process_counter(&stat_config, counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) update_stats(&walltime_nsecs_stats, stat_round->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) if (stat_config.interval && stat_round->time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) ts = &tsh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) print_counters(ts, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) int process_stat_config_event(struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) struct perf_tool *tool = session->tool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) struct perf_stat *st = container_of(tool, struct perf_stat, tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) perf_event__read_stat_config(&stat_config, &event->stat_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (perf_cpu_map__empty(st->cpus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (st->aggr_mode != AGGR_UNSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) pr_warning("warning: processing task data, aggregation mode not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (st->aggr_mode != AGGR_UNSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) stat_config.aggr_mode = st->aggr_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) if (perf_stat.data.is_pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) perf_stat_init_aggr_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) perf_stat_init_aggr_mode_file(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) static int set_maps(struct perf_stat *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) if (!st->cpus || !st->threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) if (perf_evlist__alloc_stats(evsel_list, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) st->maps_allocated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) int process_thread_map_event(struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) struct perf_tool *tool = session->tool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) struct perf_stat *st = container_of(tool, struct perf_stat, tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) if (st->threads) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) pr_warning("Extra thread map event, ignoring.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) st->threads = thread_map__new_event(&event->thread_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) if (!st->threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) return set_maps(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) int process_cpu_map_event(struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) struct perf_tool *tool = session->tool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) struct perf_stat *st = container_of(tool, struct perf_stat, tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) struct perf_cpu_map *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) if (st->cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) pr_warning("Extra cpu map event, ignoring.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) cpus = cpu_map__new_data(&event->cpu_map.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) if (!cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) st->cpus = cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) return set_maps(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) static const char * const stat_report_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) "perf stat report [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) static struct perf_stat perf_stat = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) .tool = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) .attr = perf_event__process_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) .event_update = perf_event__process_event_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) .thread_map = process_thread_map_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) .cpu_map = process_cpu_map_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) .stat_config = process_stat_config_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) .stat = perf_event__process_stat_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) .stat_round = process_stat_round_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) .aggr_mode = AGGR_UNSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) static int __cmd_report(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) struct perf_session *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) const struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) OPT_STRING('i', "input", &input_name, "file", "input file name"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) "aggregate counts per processor socket", AGGR_SOCKET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) "aggregate counts per processor die", AGGR_DIE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) "aggregate counts per physical processor core", AGGR_CORE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) "aggregate counts per numa node", AGGR_NODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) "disable CPU count aggregation", AGGR_NONE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) argc = parse_options(argc, argv, options, stat_report_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) if (!input_name || !strlen(input_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) input_name = "-";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) input_name = "perf.data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) perf_stat.data.path = input_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) perf_stat.data.mode = PERF_DATA_MODE_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) if (IS_ERR(session))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) return PTR_ERR(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) perf_stat.session = session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) stat_config.output = stderr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) evsel_list = session->evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) ret = perf_session__process_events(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) perf_session__delete(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) static void setup_system_wide(int forks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) * Make system wide (-a) the default target if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) * no target was specified and one of following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) * conditions is met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) * - there's no workload specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) * - there is workload specified but all requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) * events are system wide events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) if (!target__none(&target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) if (!forks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) target.system_wide = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) struct evsel *counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) evlist__for_each_entry(evsel_list, counter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) if (!counter->core.system_wide &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) strcmp(counter->name, "duration_time")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) if (evsel_list->core.nr_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) target.system_wide = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) int cmd_stat(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) const char * const stat_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) "perf stat [<options>] [<command>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) int status = -EINVAL, run_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) const char *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) FILE *output = stderr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) unsigned int interval, timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) const char * const stat_subcommands[] = { "record", "report" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) setlocale(LC_ALL, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) evsel_list = evlist__new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) if (evsel_list == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) parse_events__shrink_config_terms();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) /* String-parsing callback-based options would segfault when negated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) set_option_flag(stat_options, 'e', "event", PARSE_OPT_NONEG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) set_option_flag(stat_options, 'M', "metrics", PARSE_OPT_NONEG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) set_option_flag(stat_options, 'G', "cgroup", PARSE_OPT_NONEG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) (const char **) stat_usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) PARSE_OPT_STOP_AT_NON_OPTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) perf_stat__collect_metric_expr(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) perf_stat__init_shadow_stats();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) if (stat_config.csv_sep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) stat_config.csv_output = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) if (!strcmp(stat_config.csv_sep, "\\t"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) stat_config.csv_sep = "\t";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) stat_config.csv_sep = DEFAULT_SEPARATOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) if (argc && !strncmp(argv[0], "rec", 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) argc = __cmd_record(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) if (argc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) } else if (argc && !strncmp(argv[0], "rep", 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) return __cmd_report(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) interval = stat_config.interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) timeout = stat_config.timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) * For record command the -o is already taken care of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) output = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) if (output_name && output_fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) fprintf(stderr, "cannot use both --output and --log-fd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) parse_options_usage(stat_usage, stat_options, "o", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) parse_options_usage(NULL, stat_options, "log-fd", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) fprintf(stderr, "--metric-only is not supported with --per-thread\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) if (stat_config.metric_only && stat_config.run_count > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) fprintf(stderr, "--metric-only is not supported with -r\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) fprintf(stderr, "--table is only supported with -r\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) parse_options_usage(stat_usage, stat_options, "r", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) parse_options_usage(NULL, stat_options, "table", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) if (output_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) fprintf(stderr, "argument to --log-fd must be a > 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) parse_options_usage(stat_usage, stat_options, "log-fd", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) if (!output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) struct timespec tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) mode = append_file ? "a" : "w";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) output = fopen(output_name, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) if (!output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) perror("failed to create output file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) clock_gettime(CLOCK_REALTIME, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) } else if (output_fd > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) mode = append_file ? "a" : "w";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) output = fdopen(output_fd, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) if (!output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) perror("Failed opening logfd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) stat_config.output = output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) * let the spreadsheet do the pretty-printing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) if (stat_config.csv_output) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) /* User explicitly passed -B? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) if (big_num_opt == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) fprintf(stderr, "-B option not supported with -x\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) parse_options_usage(stat_usage, stat_options, "B", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) parse_options_usage(NULL, stat_options, "x", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) } else /* Nope, so disable big number formatting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) stat_config.big_num = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) } else if (big_num_opt == 0) /* User passed --no-big-num */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) stat_config.big_num = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) setup_system_wide(argc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) * Display user/system times only for single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) * run and when there's specified tracee.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) if ((stat_config.run_count == 1) && target__none(&target))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) stat_config.ru_display = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) if (stat_config.run_count < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) pr_err("Run count must be a positive number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) parse_options_usage(stat_usage, stat_options, "r", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) } else if (stat_config.run_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) forever = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) stat_config.run_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) if (stat_config.walltime_run_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) if (!stat_config.walltime_run) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) pr_err("failed to setup -r option");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) if ((stat_config.aggr_mode == AGGR_THREAD) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) !target__has_task(&target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) if (!target.system_wide || target.cpu_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) fprintf(stderr, "The --per-thread option is only "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) "available when monitoring via -p -t -a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) "options or only --per-thread.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) parse_options_usage(NULL, stat_options, "p", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) parse_options_usage(NULL, stat_options, "t", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) * no_aggr, cgroup are for system-wide only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) * --per-thread is aggregated per thread, we dont mix it with cpu mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) if (((stat_config.aggr_mode != AGGR_GLOBAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) !target__has_cpu(&target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) fprintf(stderr, "both cgroup and no-aggregation "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) "modes only available in system-wide mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) parse_options_usage(stat_usage, stat_options, "G", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) parse_options_usage(NULL, stat_options, "A", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) parse_options_usage(NULL, stat_options, "a", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) if (add_default_attributes())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) if (stat_config.cgroup_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) if (nr_cgroups > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) parse_options_usage(stat_usage, stat_options, "G", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) parse_options_usage(NULL, stat_options, "for-each-cgroup", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) &stat_config.metric_events, true) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) target__validate(&target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) target.per_thread = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) if (perf_evlist__create_maps(evsel_list, &target) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) if (target__has_task(&target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) pr_err("Problems finding threads of monitor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) parse_options_usage(stat_usage, stat_options, "p", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) parse_options_usage(NULL, stat_options, "t", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) } else if (target__has_cpu(&target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) perror("failed to parse CPUs map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) parse_options_usage(stat_usage, stat_options, "C", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) parse_options_usage(NULL, stat_options, "a", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) evlist__check_cpu_maps(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) * Initialize thread_map with comm names,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) * so we could print it out on output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) if (stat_config.aggr_mode == AGGR_THREAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) thread_map__read_comms(evsel_list->core.threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) if (target.system_wide) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) if (runtime_stat_new(&stat_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) perf_thread_map__nr(evsel_list->core.threads))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) if (stat_config.aggr_mode == AGGR_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) cpu__setup_cpunode_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) if (stat_config.times && interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) interval_count = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) else if (stat_config.times && !interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) pr_err("interval-count option should be used together with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) "interval-print.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) parse_options_usage(stat_usage, stat_options, "interval-count", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) parse_options_usage(stat_usage, stat_options, "I", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) if (timeout && timeout < 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) if (timeout < 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) pr_err("timeout must be >= 10ms.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) parse_options_usage(stat_usage, stat_options, "timeout", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) pr_warning("timeout < 100ms. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) "The overhead percentage could be high in some cases. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) "Please proceed with caution.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) if (timeout && interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) pr_err("timeout option is not supported with interval-print.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) parse_options_usage(stat_usage, stat_options, "timeout", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) parse_options_usage(stat_usage, stat_options, "I", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) if (perf_evlist__alloc_stats(evsel_list, interval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) if (perf_stat_init_aggr_mode())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) * while avoiding that older tools show confusing messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) * However for pipe sessions we need to keep it zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) * because script's perf_evsel__check_attr is triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) * by attr->sample_type != 0, and we can't run it on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) * stat sessions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) * We dont want to block the signals - that would cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) * child tasks to inherit that and Ctrl-C would not work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) * What we want is for Ctrl-C to work in the exec()-ed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) * task, but being ignored by perf stat itself:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) atexit(sig_atexit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) if (!forever)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) signal(SIGINT, skip_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) signal(SIGCHLD, skip_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) signal(SIGALRM, skip_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) signal(SIGABRT, skip_signal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) if (stat_config.run_count != 1 && verbose > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) fprintf(output, "[ perf stat: executing run #%d ... ]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) run_idx + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) if (run_idx != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) perf_evlist__reset_prev_raw_counts(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) status = run_perf_stat(argc, argv, run_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) if (forever && status != -1 && !interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) print_counters(NULL, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) perf_stat__reset_stats();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) if (!forever && status != -1 && (!interval || stat_config.summary))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) print_counters(NULL, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) evlist__finalize_ctlfd(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) if (STAT_RECORD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) * We synthesize the kernel mmap record just so that older tools
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) * don't emit warnings about not being able to resolve symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) * due to /proc/sys/kernel/kptr_restrict settings and instear provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) * a saner message about no samples being in the perf.data file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) * This also serves to suppress a warning about f_header.data.size == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) * in header.c at the moment 'perf stat record' gets introduced, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) * is not really needed once we start adding the stat specific PERF_RECORD_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) * records, but the need to suppress the kptr_restrict messages in older
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) * tools remain -acme
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) int fd = perf_data__fd(&perf_stat.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) process_synthesized_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) &perf_stat.session->machines.host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) "older tools may produce warnings about this file\n.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) if (!interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) pr_err("failed to write stat round event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) if (!perf_stat.data.is_pipe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) perf_stat.session->header.data_size += perf_stat.bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) perf_session__write_header(perf_stat.session, evsel_list, fd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) evlist__close(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) perf_session__delete(perf_stat.session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) perf_stat__exit_aggr_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) perf_evlist__free_stats(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) zfree(&stat_config.walltime_run);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) if (smi_cost && smi_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) evlist__delete(evsel_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) metricgroup__rblist_exit(&stat_config.metric_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) runtime_stat_delete(&stat_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) }