perfmon: refactor perf metric support
[vpp.git] / src / plugins / perfmon / intel / bundle / topdown_metrics.c
1 /*
2  * Copyright (c) 2021 Intel and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vppinfra/math.h>
18 #include <perfmon/perfmon.h>
19 #include <perfmon/intel/core.h>
20
21 #define GET_METRIC(m, i)  (((m) >> (i * 8)) & 0xff)
22 #define GET_RATIO(m, i)   (((m) >> (i * 32)) & 0xffffffff)
23 #define RDPMC_SLOTS       (1 << 30) /* fixed slots */
24 #define RDPMC_METRICS     (1 << 29) /* l1 & l2 metric counters */
25
26 #define FIXED_COUNTER_SLOTS       3
27 #define METRIC_COUNTER_TOPDOWN_L1_L2 0
28
29 typedef enum
30 {
31   TOPDOWN_E_RETIRING = 0,
32   TOPDOWN_E_BAD_SPEC,
33   TOPDOWN_E_FE_BOUND,
34   TOPDOWN_E_BE_BOUND,
35   TOPDOWN_E_HEAVYOPS,
36   TOPDOWN_E_LIGHTOPS,
37   TOPDOWN_E_BMISPRED,
38   TOPDOWN_E_MCHCLEAR,
39   TOPDOWN_E_FETCHLAT,
40   TOPDOWN_E_FETCH_BW,
41   TOPDOWN_E_MEMBOUND,
42   TOPDOWN_E_CORBOUND,
43   TOPDOWN_E_MAX,
44 } topdown_e_t;
45
46 enum
47 {
48   TOPDOWN_E_RDPMC_SLOTS = 0,
49   TOPDOWN_E_RDPMC_METRICS,
50 };
51
52 typedef f64 (topdown_lvl1_parse_fn_t) (void *, topdown_e_t);
53
54 /* Parse thread level states from perfmon_reading */
55 static_always_inline f64
56 topdown_lvl1_perf_reading (void *ps, topdown_e_t e)
57 {
58   perfmon_reading_t *ss = (perfmon_reading_t *) ps;
59
60   /* slots are at value[0], everthing else follows at +1 */
61   return ((f64) ss->value[e + 1] / ss->value[0]) * 100;
62 }
63
64 static_always_inline f64
65 topdown_lvl1_rdpmc_metric (void *ps, topdown_e_t e)
66 {
67   perfmon_node_stats_t *ss = (perfmon_node_stats_t *) ps;
68   f64 slots_t0 =
69     ss->t[0].value[TOPDOWN_E_RDPMC_SLOTS] *
70     ((f64) GET_METRIC (ss->t[0].value[TOPDOWN_E_RDPMC_METRICS], e) / 0xff);
71   f64 slots_t1 =
72     ss->t[1].value[TOPDOWN_E_RDPMC_SLOTS] *
73     ((f64) GET_METRIC (ss->t[1].value[TOPDOWN_E_RDPMC_METRICS], e) / 0xff);
74   u64 slots_delta = ss->t[1].value[TOPDOWN_E_RDPMC_SLOTS] -
75                     ss->t[0].value[TOPDOWN_E_RDPMC_SLOTS];
76
77   slots_t1 = slots_t1 - slots_t0;
78
79   return (slots_t1 / slots_delta) * 100;
80 }
81
82 static u8 *
83 format_topdown_lvl1 (u8 *s, va_list *args)
84 {
85   void *ps = va_arg (*args, void *);
86   u64 idx = va_arg (*args, int);
87   perfmon_bundle_type_t type = va_arg (*args, perfmon_bundle_type_t);
88   f64 sv = 0;
89
90   topdown_lvl1_parse_fn_t *parse_fn,
91     *parse_fns[PERFMON_BUNDLE_TYPE_MAX] = { 0, topdown_lvl1_rdpmc_metric,
92                                             topdown_lvl1_perf_reading, 0 };
93   parse_fn = parse_fns[type];
94   ASSERT (parse_fn);
95
96   switch (idx)
97     {
98     case 0:
99       sv =
100         parse_fn (ps, TOPDOWN_E_BAD_SPEC) + parse_fn (ps, TOPDOWN_E_RETIRING);
101       break;
102     case 1:
103       sv =
104         parse_fn (ps, TOPDOWN_E_BE_BOUND) + parse_fn (ps, TOPDOWN_E_FE_BOUND);
105       break;
106     default:
107       sv = parse_fn (ps, (topdown_e_t) idx - 2);
108       break;
109     }
110
111   s = format (s, "%f", sv);
112
113   return s;
114 }
115
116 static perfmon_cpu_supports_t topdown_lvl1_cpu_supports[] = {
117   /* Intel ICX supports papi/thread or rdpmc/node */
118   { clib_cpu_supports_avx512_bitalg, PERFMON_BUNDLE_TYPE_NODE_OR_THREAD }
119 };
120
121 PERFMON_REGISTER_BUNDLE (topdown_lvl1_metric) = {
122   .name = "topdown-level1",
123   .description = "Top-down Microarchitecture Analysis Level 1",
124   .source = "intel-core",
125   .events[0] = INTEL_CORE_E_TOPDOWN_SLOTS,
126   .events[1] = INTEL_CORE_E_TOPDOWN_L1_RETIRING_METRIC,
127   .events[2] = INTEL_CORE_E_TOPDOWN_L1_BAD_SPEC_METRIC,
128   .events[3] = INTEL_CORE_E_TOPDOWN_L1_FE_BOUND_METRIC,
129   .events[4] = INTEL_CORE_E_TOPDOWN_L1_BE_BOUND_METRIC,
130   .n_events = 5,
131   .preserve_samples = 0x1F,
132   .cpu_supports = topdown_lvl1_cpu_supports,
133   .n_cpu_supports = ARRAY_LEN (topdown_lvl1_cpu_supports),
134   .format_fn = format_topdown_lvl1,
135   .column_headers = PERFMON_STRINGS ("% NS", "% ST", "% NS.RT", "% NS.BS",
136                                      "% ST.FE", "% ST.BE"),
137   .footer = "Not Stalled (NS),STalled (ST),\n"
138             " Retiring (RT), Bad Speculation (BS),\n"
139             " FrontEnd bound (FE), BackEnd bound (BE)",
140 };
141
142 /* Convert the TopDown enum to the perf reading index */
143 #define TO_LVL2_PERF_IDX(e)                                                   \
144   ({                                                                          \
145     u8 to_idx[TOPDOWN_E_MAX] = { 0, 0, 0, 0, 5, 5, 6, 6, 7, 7, 8, 8 };        \
146     to_idx[e];                                                                \
147   })
148
149 /* Parse thread level stats from perfmon_reading */
150 static_always_inline f64
151 topdown_lvl2_perf_reading (void *ps, topdown_e_t e)
152 {
153   perfmon_reading_t *ss = (perfmon_reading_t *) ps;
154   u64 value = ss->value[TO_LVL2_PERF_IDX (e)];
155
156   /* If it is an L1 metric, call L1 format */
157   if (TOPDOWN_E_BE_BOUND >= e)
158     {
159       return topdown_lvl1_perf_reading (ps, e);
160     }
161
162   /* all the odd metrics, are inferred from even and L1 metrics */
163   if (e & 0x1)
164     {
165       topdown_e_t e1 = TO_LVL2_PERF_IDX (e) - 4;
166       value = ss->value[e1] - value;
167     }
168
169   return (f64) value / ss->value[0] * 100;
170 }
171
172 /* Convert the TopDown enum to the rdpmc metric byte position */
173 #define TO_LVL2_METRIC_BYTE(e)                                                \
174   ({                                                                          \
175     u8 to_metric[TOPDOWN_E_MAX] = { 0, 0, 0, 0, 4, 4, 5, 5, 6, 6, 7, 7 };     \
176     to_metric[e];                                                             \
177   })
178
179 /* Convert the TopDown L2 enum to the reference TopDown L1 enum */
180 #define TO_LVL1_REF(e)                                                        \
181   ({                                                                          \
182     u8 to_lvl1[TOPDOWN_E_MAX] = { -1,                                         \
183                                   -1,                                         \
184                                   -1,                                         \
185                                   -1,                                         \
186                                   TOPDOWN_E_RETIRING,                         \
187                                   TOPDOWN_E_RETIRING,                         \
188                                   TOPDOWN_E_BAD_SPEC,                         \
189                                   TOPDOWN_E_BAD_SPEC,                         \
190                                   TOPDOWN_E_FE_BOUND,                         \
191                                   TOPDOWN_E_FE_BOUND,                         \
192                                   TOPDOWN_E_BE_BOUND,                         \
193                                   TOPDOWN_E_BE_BOUND };                       \
194     to_lvl1[e];                                                               \
195   })
196
197 static_always_inline f64
198 topdown_lvl2_rdpmc_metric (void *ps, topdown_e_t e)
199 {
200   f64 r, l1_value = 0;
201
202   /* If it is an L1 metric, call L1 format */
203   if (TOPDOWN_E_BE_BOUND >= e)
204     {
205       return topdown_lvl1_rdpmc_metric (ps, e);
206     }
207
208   /* all the odd metrics, are inferred from even and L1 metrics */
209   if (e & 0x1)
210     {
211       /* get the L1 reference metric */
212       l1_value = topdown_lvl1_rdpmc_metric (ps, TO_LVL1_REF (e));
213     }
214
215   /* calculate the l2 metric */
216   r =
217     fabs (l1_value - topdown_lvl1_rdpmc_metric (ps, TO_LVL2_METRIC_BYTE (e)));
218   return r;
219 }
220
221 static u8 *
222 format_topdown_lvl2 (u8 *s, va_list *args)
223 {
224   void *ps = va_arg (*args, void *);
225   u64 idx = va_arg (*args, int);
226   perfmon_bundle_type_t type = va_arg (*args, perfmon_bundle_type_t);
227   f64 sv = 0;
228
229   topdown_lvl1_parse_fn_t *parse_fn,
230     *parse_fns[PERFMON_BUNDLE_TYPE_MAX] = { 0, topdown_lvl2_rdpmc_metric,
231                                             topdown_lvl2_perf_reading, 0 };
232
233   parse_fn = parse_fns[type];
234   ASSERT (parse_fn);
235
236   sv = parse_fn (ps, (topdown_e_t) idx);
237   s = format (s, "%f", sv);
238
239   return s;
240 }
241
242 static perfmon_cpu_supports_t topdown_lvl2_cpu_supports[] = {
243   /* Intel SPR supports papi/thread or rdpmc/node */
244   { clib_cpu_supports_avx512_fp16, PERFMON_BUNDLE_TYPE_NODE_OR_THREAD }
245 };
246
247 PERFMON_REGISTER_BUNDLE (topdown_lvl2_metric) = {
248   .name = "topdown-level2",
249   .description = "Top-down Microarchitecture Analysis Level 2",
250   .source = "intel-core",
251   .events[0] = INTEL_CORE_E_TOPDOWN_SLOTS,
252   .events[1] = INTEL_CORE_E_TOPDOWN_L1_RETIRING_METRIC,
253   .events[2] = INTEL_CORE_E_TOPDOWN_L1_BAD_SPEC_METRIC,
254   .events[3] = INTEL_CORE_E_TOPDOWN_L1_FE_BOUND_METRIC,
255   .events[4] = INTEL_CORE_E_TOPDOWN_L1_BE_BOUND_METRIC,
256   .events[5] = INTEL_CORE_E_TOPDOWN_L2_HEAVYOPS_METRIC,
257   .events[6] = INTEL_CORE_E_TOPDOWN_L2_BMISPRED_METRIC,
258   .events[7] = INTEL_CORE_E_TOPDOWN_L2_FETCHLAT_METRIC,
259   .events[8] = INTEL_CORE_E_TOPDOWN_L2_MEMBOUND_METRIC,
260   .n_events = 9,
261   .preserve_samples = 0x1FF,
262   .cpu_supports = topdown_lvl2_cpu_supports,
263   .n_cpu_supports = ARRAY_LEN (topdown_lvl2_cpu_supports),
264   .format_fn = format_topdown_lvl2,
265   .column_headers = PERFMON_STRINGS ("% RT", "% BS", "% FE", "% BE", "% RT.HO",
266                                      "% RT.LO", "% BS.BM", "% BS.MC",
267                                      "% FE.FL", "% FE.FB", "% BE.MB",
268                                      "% BE.CB"),
269   .footer = "Retiring (RT), Bad Speculation (BS),\n"
270             " FrontEnd bound (1FE), BackEnd bound (BE),\n"
271             " Light Operations (LO), Heavy Operations (HO),\n"
272             " Branch Misprediction (BM), Machine Clears (MC),\n"
273             " Fetch Latency (FL), Fetch Bandwidth (FB),\n"
274             " Memory Bound (MB), Core Bound (CB)",
275 };