blob: dffcf211070657f6727df7ab0146b247986c7526 [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 Olsaa8e02322015-06-26 11:29:10 +02007#include "xyarray.h"
Jin Yaoe5fcc2a2017-12-05 22:03:01 +08008#include "rblist.h"
Jiri Olsa728c0ee2018-08-30 08:32:11 +02009#include "perf.h"
Jiri Olsa0a4e64d2018-08-30 08:32:23 +020010#include "event.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080011
Jiri Olsae55c14a2018-04-23 11:08:21 +020012struct stats {
Xiao Guangrong0007ece2012-09-17 16:31:14 +080013 double n, mean, M2;
David Ahernffe4f3c2013-08-02 14:05:40 -060014 u64 max, min;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080015};
16
Jiri Olsae2f56da2015-06-04 15:50:55 +020017enum perf_stat_evsel_id {
18 PERF_STAT_EVSEL_ID__NONE = 0,
Jiri Olsa4c358d52015-06-03 16:25:52 +020019 PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
20 PERF_STAT_EVSEL_ID__TRANSACTION_START,
21 PERF_STAT_EVSEL_ID__ELISION_START,
22 PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
Andi Kleen239bd472016-05-24 12:52:37 -070023 PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
24 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
25 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
26 PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
27 PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
Kan Liangdaefd0b2017-05-26 12:05:38 -070028 PERF_STAT_EVSEL_ID__SMI_NUM,
29 PERF_STAT_EVSEL_ID__APERF,
Jiri Olsae2f56da2015-06-04 15:50:55 +020030 PERF_STAT_EVSEL_ID__MAX,
31};
32
Jiri Olsa581cc8a2015-10-16 12:41:03 +020033struct perf_stat_evsel {
Jiri Olsaf7794d52017-07-26 14:02:05 +020034 struct stats res_stats[3];
35 enum perf_stat_evsel_id id;
36 u64 *group_data;
Jiri Olsae2f56da2015-06-04 15:50:55 +020037};
38
Jiri Olsaf87027b2015-06-03 16:25:59 +020039enum aggr_mode {
40 AGGR_NONE,
41 AGGR_GLOBAL,
42 AGGR_SOCKET,
43 AGGR_CORE,
Jiri Olsa32b8af82015-06-26 11:29:27 +020044 AGGR_THREAD,
Jiri Olsa208df992015-10-16 12:41:04 +020045 AGGR_UNSET,
Jiri Olsaf87027b2015-06-03 16:25:59 +020046};
47
Jin Yaoe5fcc2a2017-12-05 22:03:01 +080048enum {
49 CTX_BIT_USER = 1 << 0,
50 CTX_BIT_KERNEL = 1 << 1,
51 CTX_BIT_HV = 1 << 2,
52 CTX_BIT_HOST = 1 << 3,
53 CTX_BIT_IDLE = 1 << 4,
54 CTX_BIT_MAX = 1 << 5,
55};
56
57#define NUM_CTX CTX_BIT_MAX
58
59enum stat_type {
60 STAT_NONE = 0,
61 STAT_NSECS,
62 STAT_CYCLES,
63 STAT_STALLED_CYCLES_FRONT,
64 STAT_STALLED_CYCLES_BACK,
65 STAT_BRANCHES,
66 STAT_CACHEREFS,
67 STAT_L1_DCACHE,
68 STAT_L1_ICACHE,
69 STAT_LL_CACHE,
70 STAT_ITLB_CACHE,
71 STAT_DTLB_CACHE,
72 STAT_CYCLES_IN_TX,
73 STAT_TRANSACTION,
74 STAT_ELISION,
75 STAT_TOPDOWN_TOTAL_SLOTS,
76 STAT_TOPDOWN_SLOTS_ISSUED,
77 STAT_TOPDOWN_SLOTS_RETIRED,
78 STAT_TOPDOWN_FETCH_BUBBLES,
79 STAT_TOPDOWN_RECOVERY_BUBBLES,
80 STAT_SMI_NUM,
81 STAT_APERF,
82 STAT_MAX
83};
84
85struct runtime_stat {
86 struct rblist value_list;
87};
88
Jiri Olsa421a50f2015-07-21 14:31:22 +020089struct perf_stat_config {
Jiri Olsa728c0ee2018-08-30 08:32:11 +020090 enum aggr_mode aggr_mode;
91 bool scale;
Jiri Olsa5698f262018-08-30 08:32:12 +020092 bool no_inherit;
Jiri Olsa7d9ad162018-08-30 08:32:14 +020093 bool identifier;
Jiri Olsa728c0ee2018-08-30 08:32:11 +020094 FILE *output;
95 unsigned int interval;
96 unsigned int timeout;
97 unsigned int initial_delay;
98 int times;
99 struct runtime_stat *stats;
100 int stats_num;
Jiri Olsa421a50f2015-07-21 14:31:22 +0200101};
102
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800103void update_stats(struct stats *stats, u64 val);
104double avg_stats(struct stats *stats);
105double stddev_stats(struct stats *stats);
106double rel_stddev_stats(double stddev, double avg);
107
David Ahernffe4f3c2013-08-02 14:05:40 -0600108static inline void init_stats(struct stats *stats)
109{
110 stats->n = 0.0;
111 stats->mean = 0.0;
112 stats->M2 = 0.0;
113 stats->min = (u64) -1;
114 stats->max = 0;
115}
Jiri Olsae2f56da2015-06-04 15:50:55 +0200116
117struct perf_evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200118struct perf_evlist;
119
Jin Yao29734552017-12-05 22:03:11 +0800120struct perf_aggr_thread_value {
121 struct perf_evsel *counter;
122 int id;
123 double uval;
124 u64 val;
125 u64 run;
126 u64 ena;
127};
128
Jiri Olsae2f56da2015-06-04 15:50:55 +0200129bool __perf_evsel_stat__is(struct perf_evsel *evsel,
130 enum perf_stat_evsel_id id);
131
132#define perf_stat_evsel__is(evsel, id) \
133 __perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
134
Jin Yao8efb2df2017-12-05 22:03:03 +0800135extern struct runtime_stat rt_stat;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200136extern struct stats walltime_nsecs_stats;
137
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200138typedef void (*print_metric_t)(struct perf_stat_config *config,
139 void *ctx, const char *color, const char *unit,
Andi Kleen140aead2016-01-30 09:06:49 -0800140 const char *fmt, double val);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200141typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
Andi Kleen140aead2016-01-30 09:06:49 -0800142
Jin Yao8efb2df2017-12-05 22:03:03 +0800143void runtime_stat__init(struct runtime_stat *st);
144void runtime_stat__exit(struct runtime_stat *st);
Andi Kleenfb4605b2016-03-01 10:57:52 -0800145void perf_stat__init_shadow_stats(void);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200146void perf_stat__reset_shadow_stats(void);
Jin Yao6a1e2c52017-12-05 22:03:06 +0800147void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
Jiri Olsa54830dd2017-01-23 22:42:56 +0100148void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
Jin Yao1fcd0392017-12-05 22:03:04 +0800149 int cpu, struct runtime_stat *st);
Andi Kleen140aead2016-01-30 09:06:49 -0800150struct perf_stat_output_ctx {
151 void *ctx;
152 print_metric_t print_metric;
153 new_line_t new_line;
Andi Kleen37932c12017-03-20 13:17:08 -0700154 bool force_header;
Andi Kleen140aead2016-01-30 09:06:49 -0800155};
156
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200157void perf_stat__print_shadow_stats(struct perf_stat_config *config,
158 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800159 double avg, int cpu,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700160 struct perf_stat_output_ctx *out,
Jin Yaoe0128b32017-12-05 22:03:05 +0800161 struct rblist *metric_events,
162 struct runtime_stat *st);
Andi Kleen37932c12017-03-20 13:17:08 -0700163void perf_stat__collect_metric_expr(struct perf_evlist *);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200164
Jiri Olsa24e34f62015-06-26 11:29:16 +0200165int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw);
166void perf_evlist__free_stats(struct perf_evlist *evlist);
167void perf_evlist__reset_stats(struct perf_evlist *evlist);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200168
169int perf_stat_process_counter(struct perf_stat_config *config,
170 struct perf_evsel *counter);
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100171struct perf_tool;
172union perf_event;
173struct perf_session;
174int perf_event__process_stat_event(struct perf_tool *tool,
175 union perf_event *event,
176 struct perf_session *session);
Jiri Olsae08a4562015-10-25 15:51:35 +0100177
178size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
179size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
180size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
Jiri Olsad09cefd2018-08-30 08:32:17 +0200181
182int create_perf_stat_counter(struct perf_evsel *evsel,
183 struct perf_stat_config *config,
184 struct target *target);
Jiri Olsa0a4e64d2018-08-30 08:32:23 +0200185int perf_stat_synthesize_config(struct perf_stat_config *config,
186 struct perf_tool *tool,
187 struct perf_evlist *evlist,
188 perf_event__handler_t process,
189 bool attrs);
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800190#endif