blob: 3a13a6dc5a629b9b3be233ba087d22cd7080adaa [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>
8#include <sys/time.h>
9#include <sys/resource.h>
10#include <sys/wait.h>
Jiri Olsaa8e02322015-06-26 11:29:10 +020011#include "xyarray.h"
Jin Yaoe5fcc2a2017-12-05 22:03:01 +080012#include "rblist.h"
Jiri Olsa728c0ee2018-08-30 08:32:11 +020013#include "perf.h"
Jiri Olsa0a4e64d2018-08-30 08:32:23 +020014#include "event.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080015
Jiri Olsae55c14a2018-04-23 11:08:21 +020016struct stats {
Xiao Guangrong0007ece2012-09-17 16:31:14 +080017 double n, mean, M2;
David Ahernffe4f3c2013-08-02 14:05:40 -060018 u64 max, min;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080019};
20
Jiri Olsae2f56da2015-06-04 15:50:55 +020021enum perf_stat_evsel_id {
22 PERF_STAT_EVSEL_ID__NONE = 0,
Jiri Olsa4c358d52015-06-03 16:25:52 +020023 PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
24 PERF_STAT_EVSEL_ID__TRANSACTION_START,
25 PERF_STAT_EVSEL_ID__ELISION_START,
26 PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
Andi Kleen239bd472016-05-24 12:52:37 -070027 PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
28 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
29 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
30 PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
31 PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
Kan Liangdaefd0b2017-05-26 12:05:38 -070032 PERF_STAT_EVSEL_ID__SMI_NUM,
33 PERF_STAT_EVSEL_ID__APERF,
Jiri Olsae2f56da2015-06-04 15:50:55 +020034 PERF_STAT_EVSEL_ID__MAX,
35};
36
Jiri Olsa581cc8a2015-10-16 12:41:03 +020037struct perf_stat_evsel {
Jiri Olsaf7794d52017-07-26 14:02:05 +020038 struct stats res_stats[3];
39 enum perf_stat_evsel_id id;
40 u64 *group_data;
Jiri Olsae2f56da2015-06-04 15:50:55 +020041};
42
Jiri Olsaf87027b2015-06-03 16:25:59 +020043enum aggr_mode {
44 AGGR_NONE,
45 AGGR_GLOBAL,
46 AGGR_SOCKET,
47 AGGR_CORE,
Jiri Olsa32b8af82015-06-26 11:29:27 +020048 AGGR_THREAD,
Jiri Olsa208df992015-10-16 12:41:04 +020049 AGGR_UNSET,
Jiri Olsaf87027b2015-06-03 16:25:59 +020050};
51
Jin Yaoe5fcc2a2017-12-05 22:03:01 +080052enum {
53 CTX_BIT_USER = 1 << 0,
54 CTX_BIT_KERNEL = 1 << 1,
55 CTX_BIT_HV = 1 << 2,
56 CTX_BIT_HOST = 1 << 3,
57 CTX_BIT_IDLE = 1 << 4,
58 CTX_BIT_MAX = 1 << 5,
59};
60
61#define NUM_CTX CTX_BIT_MAX
62
63enum stat_type {
64 STAT_NONE = 0,
65 STAT_NSECS,
66 STAT_CYCLES,
67 STAT_STALLED_CYCLES_FRONT,
68 STAT_STALLED_CYCLES_BACK,
69 STAT_BRANCHES,
70 STAT_CACHEREFS,
71 STAT_L1_DCACHE,
72 STAT_L1_ICACHE,
73 STAT_LL_CACHE,
74 STAT_ITLB_CACHE,
75 STAT_DTLB_CACHE,
76 STAT_CYCLES_IN_TX,
77 STAT_TRANSACTION,
78 STAT_ELISION,
79 STAT_TOPDOWN_TOTAL_SLOTS,
80 STAT_TOPDOWN_SLOTS_ISSUED,
81 STAT_TOPDOWN_SLOTS_RETIRED,
82 STAT_TOPDOWN_FETCH_BUBBLES,
83 STAT_TOPDOWN_RECOVERY_BUBBLES,
84 STAT_SMI_NUM,
85 STAT_APERF,
86 STAT_MAX
87};
88
89struct runtime_stat {
90 struct rblist value_list;
91};
92
Jiri Olsa6f6b6592018-08-30 08:32:45 +020093typedef int (*aggr_get_id_t)(struct perf_stat_config *config,
94 struct cpu_map *m, int cpu);
95
Jiri Olsa421a50f2015-07-21 14:31:22 +020096struct perf_stat_config {
Jiri Olsa728c0ee2018-08-30 08:32:11 +020097 enum aggr_mode aggr_mode;
98 bool scale;
Jiri Olsa5698f262018-08-30 08:32:12 +020099 bool no_inherit;
Jiri Olsa7d9ad162018-08-30 08:32:14 +0200100 bool identifier;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200101 bool csv_output;
Jiri Olsa132c6ba2018-08-30 08:32:30 +0200102 bool interval_clear;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +0200103 bool metric_only;
Jiri Olsaaea0dca2018-08-30 08:32:41 +0200104 bool null_run;
Jiri Olsa8897a892018-08-30 08:32:44 +0200105 bool ru_display;
Jiri Olsa34ff0862018-08-30 08:32:47 +0200106 bool big_num;
Jiri Olsafdee3352018-08-30 08:32:48 +0200107 bool no_merge;
Jiri Olsa54ac0b12018-08-30 08:32:50 +0200108 bool walltime_run_table;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200109 FILE *output;
110 unsigned int interval;
111 unsigned int timeout;
112 unsigned int initial_delay;
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200113 unsigned int unit_width;
Jiri Olsaee1760e2018-08-30 08:32:37 +0200114 unsigned int metric_only_len;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200115 int times;
Jiri Olsad97ae042018-08-30 08:32:36 +0200116 int run_count;
Jiri Olsa31084122018-08-30 08:32:42 +0200117 int print_free_counters_hint;
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +0200118 int print_mixed_hw_group_error;
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200119 struct runtime_stat *stats;
120 int stats_num;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200121 const char *csv_sep;
Jiri Olsa26893a62018-08-30 08:32:40 +0200122 struct stats *walltime_nsecs_stats;
Jiri Olsa8897a892018-08-30 08:32:44 +0200123 struct rusage ru_data;
Jiri Olsa6f6b6592018-08-30 08:32:45 +0200124 struct cpu_map *aggr_map;
125 aggr_get_id_t aggr_get_id;
126 struct cpu_map *cpus_aggr_map;
Jiri Olsa54ac0b12018-08-30 08:32:50 +0200127 u64 *walltime_run;
Jiri Olsad0192fd2018-08-30 08:32:51 +0200128 struct rblist metric_events;
Jiri Olsa421a50f2015-07-21 14:31:22 +0200129};
130
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800131void update_stats(struct stats *stats, u64 val);
132double avg_stats(struct stats *stats);
133double stddev_stats(struct stats *stats);
134double rel_stddev_stats(double stddev, double avg);
135
David Ahernffe4f3c2013-08-02 14:05:40 -0600136static inline void init_stats(struct stats *stats)
137{
138 stats->n = 0.0;
139 stats->mean = 0.0;
140 stats->M2 = 0.0;
141 stats->min = (u64) -1;
142 stats->max = 0;
143}
Jiri Olsae2f56da2015-06-04 15:50:55 +0200144
145struct perf_evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200146struct perf_evlist;
147
Jin Yao29734552017-12-05 22:03:11 +0800148struct perf_aggr_thread_value {
149 struct perf_evsel *counter;
150 int id;
151 double uval;
152 u64 val;
153 u64 run;
154 u64 ena;
155};
156
Jiri Olsae2f56da2015-06-04 15:50:55 +0200157bool __perf_evsel_stat__is(struct perf_evsel *evsel,
158 enum perf_stat_evsel_id id);
159
160#define perf_stat_evsel__is(evsel, id) \
161 __perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
162
Jin Yao8efb2df2017-12-05 22:03:03 +0800163extern struct runtime_stat rt_stat;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200164extern struct stats walltime_nsecs_stats;
165
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200166typedef void (*print_metric_t)(struct perf_stat_config *config,
167 void *ctx, const char *color, const char *unit,
Andi Kleen140aead2016-01-30 09:06:49 -0800168 const char *fmt, double val);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200169typedef void (*new_line_t)(struct perf_stat_config *config, void *ctx);
Andi Kleen140aead2016-01-30 09:06:49 -0800170
Jin Yao8efb2df2017-12-05 22:03:03 +0800171void runtime_stat__init(struct runtime_stat *st);
172void runtime_stat__exit(struct runtime_stat *st);
Andi Kleenfb4605b2016-03-01 10:57:52 -0800173void perf_stat__init_shadow_stats(void);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200174void perf_stat__reset_shadow_stats(void);
Jin Yao6a1e2c52017-12-05 22:03:06 +0800175void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
Jiri Olsa54830dd2017-01-23 22:42:56 +0100176void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
Jin Yao1fcd0392017-12-05 22:03:04 +0800177 int cpu, struct runtime_stat *st);
Andi Kleen140aead2016-01-30 09:06:49 -0800178struct perf_stat_output_ctx {
179 void *ctx;
180 print_metric_t print_metric;
181 new_line_t new_line;
Andi Kleen37932c12017-03-20 13:17:08 -0700182 bool force_header;
Andi Kleen140aead2016-01-30 09:06:49 -0800183};
184
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200185void perf_stat__print_shadow_stats(struct perf_stat_config *config,
186 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800187 double avg, int cpu,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700188 struct perf_stat_output_ctx *out,
Jin Yaoe0128b32017-12-05 22:03:05 +0800189 struct rblist *metric_events,
190 struct runtime_stat *st);
Andi Kleen37932c12017-03-20 13:17:08 -0700191void perf_stat__collect_metric_expr(struct perf_evlist *);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200192
Jiri Olsa24e34f62015-06-26 11:29:16 +0200193int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw);
194void perf_evlist__free_stats(struct perf_evlist *evlist);
195void perf_evlist__reset_stats(struct perf_evlist *evlist);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200196
197int perf_stat_process_counter(struct perf_stat_config *config,
198 struct perf_evsel *counter);
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100199struct perf_tool;
200union perf_event;
201struct perf_session;
202int perf_event__process_stat_event(struct perf_tool *tool,
203 union perf_event *event,
204 struct perf_session *session);
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
210int create_perf_stat_counter(struct perf_evsel *evsel,
211 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,
215 struct perf_evlist *evlist,
216 perf_event__handler_t process,
217 bool attrs);
Jiri Olsa088519f2018-08-30 08:32:52 +0200218void
219perf_evlist__print_counters(struct perf_evlist *evlist,
220 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