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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vppinfra/math.h>
18 #include <perfmon/perfmon.h>
19 #include <perfmon/intel/core.h>
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 */
26 #define FIXED_COUNTER_SLOTS 3
27 #define METRIC_COUNTER_TOPDOWN_L1_L2 0
31 TOPDOWN_E_RETIRING = 0,
48 TOPDOWN_E_RDPMC_SLOTS = 0,
49 TOPDOWN_E_RDPMC_METRICS,
52 typedef f64 (topdown_lvl1_parse_fn_t) (void *, topdown_e_t);
54 /* Parse thread level states from perfmon_reading */
55 static_always_inline f64
56 topdown_lvl1_perf_reading (void *ps, topdown_e_t e)
58 perfmon_reading_t *ss = (perfmon_reading_t *) ps;
60 /* slots are at value[0], everthing else follows at +1 */
61 return ((f64) ss->value[e + 1] / ss->value[0]) * 100;
64 static_always_inline f64
65 topdown_lvl1_rdpmc_metric (void *ps, topdown_e_t e)
67 perfmon_node_stats_t *ss = (perfmon_node_stats_t *) ps;
69 ss->t[0].value[TOPDOWN_E_RDPMC_SLOTS] *
70 ((f64) GET_METRIC (ss->t[0].value[TOPDOWN_E_RDPMC_METRICS], e) / 0xff);
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];
77 slots_t1 = slots_t1 - slots_t0;
79 return (slots_t1 / slots_delta) * 100;
82 /* Convert the TopDown enum to the perf reading index */
83 #define TO_LVL2_PERF_IDX(e) \
85 u8 to_idx[TOPDOWN_E_MAX] = { 0, 0, 0, 0, 5, 5, 6, 6, 7, 7, 8, 8 }; \
89 /* Parse thread level stats from perfmon_reading */
90 static_always_inline f64
91 topdown_lvl2_perf_reading (void *ps, topdown_e_t e)
93 perfmon_reading_t *ss = (perfmon_reading_t *) ps;
94 u64 value = ss->value[TO_LVL2_PERF_IDX (e)];
96 /* If it is an L1 metric, call L1 format */
97 if (TOPDOWN_E_BE_BOUND >= e)
99 return topdown_lvl1_perf_reading (ps, e);
102 /* all the odd metrics, are inferred from even and L1 metrics */
105 topdown_e_t e1 = TO_LVL2_PERF_IDX (e) - 4;
106 value = ss->value[e1] - value;
109 return (f64) value / ss->value[0] * 100;
112 /* Convert the TopDown enum to the rdpmc metric byte position */
113 #define TO_LVL2_METRIC_BYTE(e) \
115 u8 to_metric[TOPDOWN_E_MAX] = { 0, 0, 0, 0, 4, 4, 5, 5, 6, 6, 7, 7 }; \
119 /* Convert the TopDown L2 enum to the reference TopDown L1 enum */
120 #define TO_LVL1_REF(e) \
122 u8 to_lvl1[TOPDOWN_E_MAX] = { -1, \
126 TOPDOWN_E_RETIRING, \
127 TOPDOWN_E_RETIRING, \
128 TOPDOWN_E_BAD_SPEC, \
129 TOPDOWN_E_BAD_SPEC, \
130 TOPDOWN_E_FE_BOUND, \
131 TOPDOWN_E_FE_BOUND, \
132 TOPDOWN_E_BE_BOUND, \
133 TOPDOWN_E_BE_BOUND }; \
137 static_always_inline f64
138 topdown_lvl2_rdpmc_metric (void *ps, topdown_e_t e)
142 /* If it is an L1 metric, call L1 format */
143 if (TOPDOWN_E_BE_BOUND >= e)
145 return topdown_lvl1_rdpmc_metric (ps, e);
148 /* all the odd metrics, are inferred from even and L1 metrics */
151 /* get the L1 reference metric */
152 l1_value = topdown_lvl1_rdpmc_metric (ps, TO_LVL1_REF (e));
155 /* calculate the l2 metric */
157 fabs (l1_value - topdown_lvl1_rdpmc_metric (ps, TO_LVL2_METRIC_BYTE (e)));
162 format_topdown_lvl2 (u8 *s, va_list *args)
164 void *ps = va_arg (*args, void *);
165 u64 idx = va_arg (*args, int);
166 perfmon_bundle_type_t type = va_arg (*args, perfmon_bundle_type_t);
169 topdown_lvl1_parse_fn_t *parse_fn,
170 *parse_fns[PERFMON_BUNDLE_TYPE_MAX] = { 0, topdown_lvl2_rdpmc_metric,
171 topdown_lvl2_perf_reading, 0 };
173 parse_fn = parse_fns[type];
176 sv = parse_fn (ps, (topdown_e_t) idx);
177 s = format (s, "%f", sv);
182 static perfmon_cpu_supports_t topdown_lvl2_cpu_supports[] = {
183 /* Intel SPR supports papi/thread or rdpmc/node */
184 { clib_cpu_supports_avx512_fp16, PERFMON_BUNDLE_TYPE_NODE_OR_THREAD }
187 PERFMON_REGISTER_BUNDLE (topdown_lvl2_metric) = {
189 .description = "Top-down Microarchitecture Analysis Level 1 & 2",
190 .source = "intel-core",
191 .events[0] = INTEL_CORE_E_TOPDOWN_SLOTS,
192 .events[1] = INTEL_CORE_E_TOPDOWN_L1_RETIRING_METRIC,
193 .events[2] = INTEL_CORE_E_TOPDOWN_L1_BAD_SPEC_METRIC,
194 .events[3] = INTEL_CORE_E_TOPDOWN_L1_FE_BOUND_METRIC,
195 .events[4] = INTEL_CORE_E_TOPDOWN_L1_BE_BOUND_METRIC,
196 .events[5] = INTEL_CORE_E_TOPDOWN_L2_HEAVYOPS_METRIC,
197 .events[6] = INTEL_CORE_E_TOPDOWN_L2_BMISPRED_METRIC,
198 .events[7] = INTEL_CORE_E_TOPDOWN_L2_FETCHLAT_METRIC,
199 .events[8] = INTEL_CORE_E_TOPDOWN_L2_MEMBOUND_METRIC,
201 .preserve_samples = 0x1FF,
202 .cpu_supports = topdown_lvl2_cpu_supports,
203 .n_cpu_supports = ARRAY_LEN (topdown_lvl2_cpu_supports),
204 .format_fn = format_topdown_lvl2,
205 .column_headers = PERFMON_STRINGS ("% RT", "% BS", "% FE", "% BE", "% RT.HO",
206 "% RT.LO", "% BS.BM", "% BS.MC",
207 "% FE.FL", "% FE.FB", "% BE.MB",
209 .footer = "Retiring (RT), Bad Speculation (BS),\n"
210 " FrontEnd bound (1FE), BackEnd bound (BE),\n"
211 " Light Operations (LO), Heavy Operations (HO),\n"
212 " Branch Misprediction (BM), Machine Clears (MC),\n"
213 " Fetch Latency (FL), Fetch Bandwidth (FB),\n"
214 " Memory Bound (MB), Core Bound (CB)",