blob: 53b2415ba3f3e729fb569806c6af61f7d485155f [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"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080010
Jiri Olsae55c14a2018-04-23 11:08:21 +020011struct stats {
Xiao Guangrong0007ece2012-09-17 16:31:14 +080012 double n, mean, M2;
David Ahernffe4f3c2013-08-02 14:05:40 -060013 u64 max, min;
Xiao Guangrong0007ece2012-09-17 16:31:14 +080014};
15
Jiri Olsae2f56da2015-06-04 15:50:55 +020016enum perf_stat_evsel_id {
17 PERF_STAT_EVSEL_ID__NONE = 0,
Jiri Olsa4c358d52015-06-03 16:25:52 +020018 PERF_STAT_EVSEL_ID__CYCLES_IN_TX,
19 PERF_STAT_EVSEL_ID__TRANSACTION_START,
20 PERF_STAT_EVSEL_ID__ELISION_START,
21 PERF_STAT_EVSEL_ID__CYCLES_IN_TX_CP,
Andi Kleen239bd472016-05-24 12:52:37 -070022 PERF_STAT_EVSEL_ID__TOPDOWN_TOTAL_SLOTS,
23 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_ISSUED,
24 PERF_STAT_EVSEL_ID__TOPDOWN_SLOTS_RETIRED,
25 PERF_STAT_EVSEL_ID__TOPDOWN_FETCH_BUBBLES,
26 PERF_STAT_EVSEL_ID__TOPDOWN_RECOVERY_BUBBLES,
Kan Liangdaefd0b2017-05-26 12:05:38 -070027 PERF_STAT_EVSEL_ID__SMI_NUM,
28 PERF_STAT_EVSEL_ID__APERF,
Jiri Olsae2f56da2015-06-04 15:50:55 +020029 PERF_STAT_EVSEL_ID__MAX,
30};
31
Jiri Olsa581cc8a2015-10-16 12:41:03 +020032struct perf_stat_evsel {
Jiri Olsaf7794d52017-07-26 14:02:05 +020033 struct stats res_stats[3];
34 enum perf_stat_evsel_id id;
35 u64 *group_data;
Jiri Olsae2f56da2015-06-04 15:50:55 +020036};
37
Jiri Olsaf87027b2015-06-03 16:25:59 +020038enum aggr_mode {
39 AGGR_NONE,
40 AGGR_GLOBAL,
41 AGGR_SOCKET,
42 AGGR_CORE,
Jiri Olsa32b8af82015-06-26 11:29:27 +020043 AGGR_THREAD,
Jiri Olsa208df992015-10-16 12:41:04 +020044 AGGR_UNSET,
Jiri Olsaf87027b2015-06-03 16:25:59 +020045};
46
Jin Yaoe5fcc2a2017-12-05 22:03:01 +080047enum {
48 CTX_BIT_USER = 1 << 0,
49 CTX_BIT_KERNEL = 1 << 1,
50 CTX_BIT_HV = 1 << 2,
51 CTX_BIT_HOST = 1 << 3,
52 CTX_BIT_IDLE = 1 << 4,
53 CTX_BIT_MAX = 1 << 5,
54};
55
56#define NUM_CTX CTX_BIT_MAX
57
58enum stat_type {
59 STAT_NONE = 0,
60 STAT_NSECS,
61 STAT_CYCLES,
62 STAT_STALLED_CYCLES_FRONT,
63 STAT_STALLED_CYCLES_BACK,
64 STAT_BRANCHES,
65 STAT_CACHEREFS,
66 STAT_L1_DCACHE,
67 STAT_L1_ICACHE,
68 STAT_LL_CACHE,
69 STAT_ITLB_CACHE,
70 STAT_DTLB_CACHE,
71 STAT_CYCLES_IN_TX,
72 STAT_TRANSACTION,
73 STAT_ELISION,
74 STAT_TOPDOWN_TOTAL_SLOTS,
75 STAT_TOPDOWN_SLOTS_ISSUED,
76 STAT_TOPDOWN_SLOTS_RETIRED,
77 STAT_TOPDOWN_FETCH_BUBBLES,
78 STAT_TOPDOWN_RECOVERY_BUBBLES,
79 STAT_SMI_NUM,
80 STAT_APERF,
81 STAT_MAX
82};
83
84struct runtime_stat {
85 struct rblist value_list;
86};
87
Jiri Olsa421a50f2015-07-21 14:31:22 +020088struct perf_stat_config {
Jiri Olsa728c0ee2018-08-30 08:32:11 +020089 enum aggr_mode aggr_mode;
90 bool scale;
Jiri Olsa5698f262018-08-30 08:32:12 +020091 bool no_inherit;
Jiri Olsa728c0ee2018-08-30 08:32:11 +020092 FILE *output;
93 unsigned int interval;
94 unsigned int timeout;
95 unsigned int initial_delay;
96 int times;
97 struct runtime_stat *stats;
98 int stats_num;
Jiri Olsa421a50f2015-07-21 14:31:22 +020099};
100
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800101void update_stats(struct stats *stats, u64 val);
102double avg_stats(struct stats *stats);
103double stddev_stats(struct stats *stats);
104double rel_stddev_stats(double stddev, double avg);
105
David Ahernffe4f3c2013-08-02 14:05:40 -0600106static inline void init_stats(struct stats *stats)
107{
108 stats->n = 0.0;
109 stats->mean = 0.0;
110 stats->M2 = 0.0;
111 stats->min = (u64) -1;
112 stats->max = 0;
113}
Jiri Olsae2f56da2015-06-04 15:50:55 +0200114
115struct perf_evsel;
Jiri Olsa24e34f62015-06-26 11:29:16 +0200116struct perf_evlist;
117
Jin Yao29734552017-12-05 22:03:11 +0800118struct perf_aggr_thread_value {
119 struct perf_evsel *counter;
120 int id;
121 double uval;
122 u64 val;
123 u64 run;
124 u64 ena;
125};
126
Jiri Olsae2f56da2015-06-04 15:50:55 +0200127bool __perf_evsel_stat__is(struct perf_evsel *evsel,
128 enum perf_stat_evsel_id id);
129
130#define perf_stat_evsel__is(evsel, id) \
131 __perf_evsel_stat__is(evsel, PERF_STAT_EVSEL_ID__ ## id)
132
Jin Yao8efb2df2017-12-05 22:03:03 +0800133extern struct runtime_stat rt_stat;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200134extern struct stats walltime_nsecs_stats;
135
Andi Kleen140aead2016-01-30 09:06:49 -0800136typedef void (*print_metric_t)(void *ctx, const char *color, const char *unit,
137 const char *fmt, double val);
138typedef void (*new_line_t )(void *ctx);
139
Jin Yao8efb2df2017-12-05 22:03:03 +0800140void runtime_stat__init(struct runtime_stat *st);
141void runtime_stat__exit(struct runtime_stat *st);
Andi Kleenfb4605b2016-03-01 10:57:52 -0800142void perf_stat__init_shadow_stats(void);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200143void perf_stat__reset_shadow_stats(void);
Jin Yao6a1e2c52017-12-05 22:03:06 +0800144void perf_stat__reset_shadow_per_stat(struct runtime_stat *st);
Jiri Olsa54830dd2017-01-23 22:42:56 +0100145void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
Jin Yao1fcd0392017-12-05 22:03:04 +0800146 int cpu, struct runtime_stat *st);
Andi Kleen140aead2016-01-30 09:06:49 -0800147struct perf_stat_output_ctx {
148 void *ctx;
149 print_metric_t print_metric;
150 new_line_t new_line;
Andi Kleen37932c12017-03-20 13:17:08 -0700151 bool force_header;
Andi Kleen140aead2016-01-30 09:06:49 -0800152};
153
154void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
155 double avg, int cpu,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700156 struct perf_stat_output_ctx *out,
Jin Yaoe0128b32017-12-05 22:03:05 +0800157 struct rblist *metric_events,
158 struct runtime_stat *st);
Andi Kleen37932c12017-03-20 13:17:08 -0700159void perf_stat__collect_metric_expr(struct perf_evlist *);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200160
Jiri Olsa24e34f62015-06-26 11:29:16 +0200161int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw);
162void perf_evlist__free_stats(struct perf_evlist *evlist);
163void perf_evlist__reset_stats(struct perf_evlist *evlist);
Jiri Olsaf80010e2015-07-21 14:31:27 +0200164
165int perf_stat_process_counter(struct perf_stat_config *config,
166 struct perf_evsel *counter);
Jiri Olsa0ea0e352015-10-25 15:51:32 +0100167struct perf_tool;
168union perf_event;
169struct perf_session;
170int perf_event__process_stat_event(struct perf_tool *tool,
171 union perf_event *event,
172 struct perf_session *session);
Jiri Olsae08a4562015-10-25 15:51:35 +0100173
174size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp);
175size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp);
176size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp);
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800177#endif