blob: 14fe3e548229fcc2f8b29bc637791db5f9aa953a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Xiao Guangrong0007ece2012-09-17 16:31:14 +08002#ifndef __PERF_STATS_H
3#define __PERF_STATS_H
4
Borislav Petkovd944c4e2014-04-25 21:31:02 +02005#include <linux/types.h>
Jiri Olsaf87027b2015-06-03 16:25:59 +02006#include <stdio.h>
Jiri Olsa8897a892018-08-30 08:32:44 +02007#include <sys/types.h>
Jiri Olsa8897a892018-08-30 08:32:44 +02008#include <sys/resource.h>
Jin Yaoe5fcc2a2017-12-05 22:03:01 +08009#include "rblist.h"
Jiri Olsa0a4e64d2018-08-30 08:32:23 +020010#include "event.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080011
Arnaldo Carvalho de Melo38b7b672019-08-22 14:35:55 -030012struct timespec;
13
Jiri Olsae55c14a2018-04-23 11:08:21 +020014struct stats {
Xiao Guangrong0007ece2012-09-17 16:31:14 +080015 double n, mean, M2;
David Ahernffe4f3c2013-08-02 14:05:40 -060016 u64 max, min;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080017};
18
Jiri Olsae2f56da2015-06-04 15:50:55 +020019enum perf_stat_evsel_id {
20 PERF_STAT_EVSEL_ID__NONE = 0,
Jiri Olsa4c358d52015-06-03 16:25:52 +020021 PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
22 PERF_STAT_EVSEL_ID__TRANSACTION_START,
23 PERF_STAT_EVSEL_ID__ELISION_START,
24 PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
Andi Kleen239bd472016-05-24 12:52:37 -070025 PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
26 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
27 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
28 PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
29 PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
Kan Liangdaefd0b2017-05-26 12:05:38 -070030 PERF_STAT_EVSEL_ID__SMI_NUM,
31 PERF_STAT_EVSEL_ID__APERF,
Jiri Olsae2f56da2015-06-04 15:50:55 +020032 PERF_STAT_EVSEL_ID__MAX,
33};
34
Jiri Olsa581cc8a2015-10-16 12:41:03 +020035struct perf_stat_evsel {
Jiri Olsaf7794d52017-07-26 14:02:05 +020036 struct stats res_stats[3];
37 enum perf_stat_evsel_id id;
38 u64 *group_data;
Jiri Olsae2f56da2015-06-04 15:50:55 +020039};
40
Jiri Olsaf87027b2015-06-03 16:25:59 +020041enum aggr_mode {
42 AGGR_NONE,
43 AGGR_GLOBAL,
44 AGGR_SOCKET,
Kan Liangdb5742b2019-06-04 15:50:42 -070045 AGGR_DIE,
Jiri Olsaf87027b2015-06-03 16:25:59 +020046 AGGR_CORE,
Jiri Olsa32b8af82015-06-26 11:29:27 +020047 AGGR_THREAD,
Jiri Olsa208df992015-10-16 12:41:04 +020048 AGGR_UNSET,
Jiri Olsaf87027b2015-06-03 16:25:59 +020049};
50
Jin Yaoe5fcc2a2017-12-05 22:03:01 +080051enum {
52 CTX_BIT_USER = 1 << 0,
53 CTX_BIT_KERNEL = 1 << 1,
54 CTX_BIT_HV = 1 << 2,
55 CTX_BIT_HOST = 1 << 3,
56 CTX_BIT_IDLE = 1 << 4,
57 CTX_BIT_MAX = 1 << 5,
58};
59
60#define NUM_CTX CTX_BIT_MAX
61
62enum stat_type {
63 STAT_NONE = 0,
64 STAT_NSECS,
65 STAT_CYCLES,
66 STAT_STALLED_CYCLES_FRONT,
67 STAT_STALLED_CYCLES_BACK,
68 STAT_BRANCHES,
69 STAT_CACHEREFS,
70 STAT_L1_DCACHE,
71 STAT_L1_ICACHE,
72 STAT_LL_CACHE,
73 STAT_ITLB_CACHE,
74 STAT_DTLB_CACHE,
75 STAT_CYCLES_IN_TX,
76 STAT_TRANSACTION,
77 STAT_ELISION,
78 STAT_TOPDOWN_TOTAL_SLOTS,
79 STAT_TOPDOWN_SLOTS_ISSUED,
80 STAT_TOPDOWN_SLOTS_RETIRED,
81 STAT_TOPDOWN_FETCH_BUBBLES,
82 STAT_TOPDOWN_RECOVERY_BUBBLES,
83 STAT_SMI_NUM,
84 STAT_APERF,
85 STAT_MAX
86};
87
88struct runtime_stat {
89 struct rblist value_list;
90};
91
Jiri Olsa6f6b6592018-08-30 08:32:45 +020092typedef int (*aggr_get_id_t)(struct perf_stat_config *config,
Jiri Olsaf8548392019-07-21 13:23:49 +020093 struct perf_cpu_map *m, int cpu);
Jiri Olsa6f6b6592018-08-30 08:32:45 +020094
Jiri Olsa421a50f2015-07-21 14:31:22 +020095struct perf_stat_config {
Jiri Olsa728c0ee2018-08-30 08:32:11 +020096 enum aggr_mode aggr_mode;
97 bool scale;
Jiri Olsa5698f262018-08-30 08:32:12 +020098 bool no_inherit;
Jiri Olsa7d9ad162018-08-30 08:32:14 +020099 bool identifier;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200100 bool csv_output;
Jiri Olsa132c6ba2018-08-30 08:32:30 +0200101 bool interval_clear;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +0200102 bool metric_only;
Jiri Olsaaea0dca2018-08-30 08:32:41 +0200103 bool null_run;
Jiri Olsa8897a892018-08-30 08:32:44 +0200104 bool ru_display;
Jiri Olsa34ff0862018-08-30 08:32:47 +0200105 bool big_num;
Jiri Olsafdee3352018-08-30 08:32:48 +0200106 bool no_merge;
Jiri Olsa54ac0b12018-08-30 08:32:50 +0200107 bool walltime_run_table;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200108 FILE *output;
109 unsigned int interval;
110 unsigned int timeout;
111 unsigned int initial_delay;
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200112 unsigned int unit_width;
Jiri Olsaee1760e2018-08-30 08:32:37 +0200113 unsigned int metric_only_len;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200114 int times;
Jiri Olsad97ae042018-08-30 08:32:36 +0200115 int run_count;
Jiri Olsa31084122018-08-30 08:32:42 +0200116 int print_free_counters_hint;
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +0200117 int print_mixed_hw_group_error;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200118 struct runtime_stat *stats;
119 int stats_num;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200120 const char *csv_sep;
Jiri Olsa26893a62018-08-30 08:32:40 +0200121 struct stats *walltime_nsecs_stats;
Jiri Olsa8897a892018-08-30 08:32:44 +0200122 struct rusage ru_data;
Jiri Olsaf8548392019-07-21 13:23:49 +0200123 struct perf_cpu_map *aggr_map;
Jiri Olsa6f6b6592018-08-30 08:32:45 +0200124 aggr_get_id_t aggr_get_id;
Jiri Olsaf8548392019-07-21 13:23:49 +0200125 struct perf_cpu_map *cpus_aggr_map;
Jiri Olsa54ac0b12018-08-30 08:32:50 +0200126 u64 *walltime_run;
Jiri Olsad0192fd2018-08-30 08:32:51 +0200127 struct rblist metric_events;
Jiri Olsa421a50f2015-07-21 14:31:22 +0200128};
129
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800130void update_stats(struct stats *stats, u64 val);
131double avg_stats(struct stats *stats);
132double stddev_stats(struct stats *stats);
133double rel_stddev_stats(double stddev, double avg);
134
David Ahernffe4f3c2013-08-02 14:05:40 -0600135static inline void init_stats(struct stats *stats)
136{
137 stats->n = 0.0;
138 stats->mean = 0.0;
139 stats->M2 = 0.0;
140 stats->min = (u64) -1;
141 stats->max = 0;
142}
Jiri Olsae2f56da2015-06-04 15:50:55 +0200143
Jiri Olsa32dcd022019-07-21 13:23:51 +0200144struct evsel;
Jiri Olsa63503db2019-07-21 13:23:52 +0200145struct evlist;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200146
Jin Yao29734552017-12-05 22:03:11 +0800147struct perf_aggr_thread_value {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200148 struct evsel *counter;
Jin Yao29734552017-12-05 22:03:11 +0800149 int id;
150 double uval;
151 u64 val;
152 u64 run;
153 u64 ena;
154};
155
Jiri Olsa32dcd022019-07-21 13:23:51 +0200156bool __perf_evsel_stat__is(struct evsel *evsel,
Jiri Olsae2f56da2015-06-04 15:50:55 +0200157 enum perf_stat_evsel_id id);
158
159#define perf_stat_evsel__is(evsel, id) \
160 __perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
161
Jin Yao8efb2df2017-12-05 22:03:03 +0800162extern struct runtime_stat rt_stat;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200163extern struct stats walltime_nsecs_stats;
164
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200165typedef void (*print_metric_t)(struct perf_stat_config *config,
166 void *ctx, const char *color, const char *unit,
Andi Kleen140aead2016-01-30 09:06:49 -0800167 const char *fmt, double val);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200168typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
Andi Kleen140aead2016-01-30 09:06:49 -0800169
Jin Yao8efb2df2017-12-05 22:03:03 +0800170void runtime_stat__init(struct runtime_stat *st);
171void runtime_stat__exit(struct runtime_stat *st);
Andi Kleenfb4605b2016-03-01 10:57:52 -0800172void perf_stat__init_shadow_stats(void);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200173void perf_stat__reset_shadow_stats(void);
Jin Yao6a1e2c52017-12-05 22:03:06 +0800174void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
Jiri Olsa32dcd022019-07-21 13:23:51 +0200175void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
Jin Yao1fcd0392017-12-05 22:03:04 +0800176 int cpu, struct runtime_stat *st);
Andi Kleen140aead2016-01-30 09:06:49 -0800177struct perf_stat_output_ctx {
178 void *ctx;
179 print_metric_t print_metric;
180 new_line_t new_line;
Andi Kleen37932c12017-03-20 13:17:08 -0700181 bool force_header;
Andi Kleen140aead2016-01-30 09:06:49 -0800182};
183
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200184void perf_stat__print_shadow_stats(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200185 struct evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800186 double avg, int cpu,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700187 struct perf_stat_output_ctx *out,
Jin Yaoe0128b32017-12-05 22:03:05 +0800188 struct rblist *metric_events,
189 struct runtime_stat *st);
Jiri Olsa63503db2019-07-21 13:23:52 +0200190void perf_stat__collect_metric_expr(struct evlist *);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200191
Jiri Olsa63503db2019-07-21 13:23:52 +0200192int perf_evlist__alloc_stats(struct evlist *evlist, bool alloc_raw);
193void perf_evlist__free_stats(struct evlist *evlist);
194void perf_evlist__reset_stats(struct evlist *evlist);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200195
196int perf_stat_process_counter(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200197 struct evsel *counter);
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100198struct perf_tool;
199union perf_event;
200struct perf_session;
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -0300201struct target;
202
Jiri Olsa89f16882018-09-13 14:54:03 +0200203int perf_event__process_stat_event(struct perf_session *session,
204 union perf_event *event);
Jiri Olsae08a4562015-10-25 15:51:35 +0100205
206size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
207size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
208size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
Jiri Olsad09cefd2018-08-30 08:32:17 +0200209
Jiri Olsa32dcd022019-07-21 13:23:51 +0200210int create_perf_stat_counter(struct evsel *evsel,
Jiri Olsad09cefd2018-08-30 08:32:17 +0200211 struct perf_stat_config *config,
212 struct target *target);
Jiri Olsa0a4e64d2018-08-30 08:32:23 +0200213int perf_stat_synthesize_config(struct perf_stat_config *config,
214 struct perf_tool *tool,
Jiri Olsa63503db2019-07-21 13:23:52 +0200215 struct evlist *evlist,
Jiri Olsa0a4e64d2018-08-30 08:32:23 +0200216 perf_event__handler_t process,
217 bool attrs);
Jiri Olsa088519f2018-08-30 08:32:52 +0200218void
Jiri Olsa63503db2019-07-21 13:23:52 +0200219perf_evlist__print_counters(struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200220 struct perf_stat_config *config,
221 struct target *_target,
222 struct timespec *ts,
223 int argc, const char **argv);
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800224#endif