perfmon: adding support for papi TMAM
[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 <perfmon/perfmon.h>
18 #include <perfmon/intel/core.h>
19
20 #define GET_METRIC(m, i)  (((m) >> (i * 8)) & 0xff)
21 #define GET_RATIO(m, i)   (((m) >> (i * 32)) & 0xffffffff)
22 #define RDPMC_FIXED_SLOTS (1 << 30) /* fixed slots */
23 #define RDPMC_L1_METRICS  (1 << 29) /* l1 metric counters */
24
25 #define FIXED_COUNTER_SLOTS       3
26 #define METRIC_COUNTER_TOPDOWN_L1 0
27
28 typedef enum
29 {
30   TOPDOWN_E_RETIRING = 0,
31   TOPDOWN_E_BAD_SPEC,
32   TOPDOWN_E_FE_BOUND,
33   TOPDOWN_E_BE_BOUND,
34 } topdown_lvl1_t;
35
36 enum
37 {
38   TOPDOWN_E_RDPMC_SLOTS = 0,
39   TOPDOWN_E_RDPMC_METRICS,
40 };
41
42 typedef f64 (topdown_lvl1_parse_fn_t) (void *, topdown_lvl1_t);
43
44 /* Parse thread level states from perfmon_reading */
45 static_always_inline f64
46 topdown_lvl1_perf_reading (void *ps, topdown_lvl1_t e)
47 {
48   perfmon_reading_t *ss = (perfmon_reading_t *) ps;
49
50   /* slots are at value[0], everthing else follows at +1 */
51   return ((f64) ss->value[e + 1] / ss->value[0]) * 100;
52 }
53
54 static_always_inline f64
55 topdown_lvl1_rdpmc_metric (void *ps, topdown_lvl1_t e)
56 {
57   perfmon_node_stats_t *ss = (perfmon_node_stats_t *) ps;
58   f64 slots_t0 =
59     ss->t[0].value[TOPDOWN_E_RDPMC_SLOTS] *
60     ((f64) GET_METRIC (ss->t[0].value[TOPDOWN_E_RDPMC_METRICS], e) / 0xff);
61   f64 slots_t1 =
62     ss->t[1].value[TOPDOWN_E_RDPMC_SLOTS] *
63     ((f64) GET_METRIC (ss->t[1].value[TOPDOWN_E_RDPMC_METRICS], e) / 0xff);
64   u64 slots_delta = ss->t[1].value[TOPDOWN_E_RDPMC_SLOTS] -
65                     ss->t[0].value[TOPDOWN_E_RDPMC_SLOTS];
66
67   slots_t1 = slots_t1 - slots_t0;
68
69   return (slots_t1 / slots_delta) * 100;
70 }
71
72 static u8 *
73 format_topdown_lvl1 (u8 *s, va_list *args)
74 {
75   void *ps = va_arg (*args, void *);
76   u64 idx = va_arg (*args, int);
77   perfmon_bundle_type_t type = va_arg (*args, perfmon_bundle_type_t);
78   f64 sv = 0;
79
80   topdown_lvl1_parse_fn_t *parse_fn,
81     *parse_fns[PERFMON_BUNDLE_TYPE_MAX] = { 0, topdown_lvl1_rdpmc_metric,
82                                             topdown_lvl1_perf_reading, 0 };
83   parse_fn = parse_fns[type];
84   ASSERT (parse_fn);
85
86   switch (idx)
87     {
88     case 0:
89       sv =
90         parse_fn (ps, TOPDOWN_E_BAD_SPEC) + parse_fn (ps, TOPDOWN_E_RETIRING);
91       break;
92     case 1:
93       sv =
94         parse_fn (ps, TOPDOWN_E_BE_BOUND) + parse_fn (ps, TOPDOWN_E_FE_BOUND);
95       break;
96     default:
97       sv = parse_fn (ps, (topdown_lvl1_t) idx - 2);
98       break;
99     }
100
101   s = format (s, "%f", sv);
102
103   return s;
104 }
105
106 static perfmon_cpu_supports_t topdown_lvl1_cpu_supports[] = {
107   /* Intel SNR supports papi/thread only */
108   { clib_cpu_supports_movdiri, PERFMON_BUNDLE_TYPE_THREAD },
109   /* Intel ICX supports papi/thread or rdpmc/node */
110   { clib_cpu_supports_avx512_bitalg, PERFMON_BUNDLE_TYPE_NODE }
111 };
112
113 PERFMON_REGISTER_BUNDLE (topdown_lvl1_metric) = {
114   .name = "topdown-level1",
115   .description = "Top-down Microarchitecture Analysis Level 1",
116   .source = "intel-core",
117   .offset_type = PERFMON_OFFSET_TYPE_METRICS,
118   .events[0] = INTEL_CORE_E_TOPDOWN_SLOTS,
119   .events[1] = INTEL_CORE_E_TOPDOWN_L1_RETIRING_METRIC,
120   .events[2] = INTEL_CORE_E_TOPDOWN_L1_BAD_SPEC_METRIC,
121   .events[3] = INTEL_CORE_E_TOPDOWN_L1_FE_BOUND_METRIC,
122   .events[4] = INTEL_CORE_E_TOPDOWN_L1_BE_BOUND_METRIC,
123   .n_events = 5,
124   .metrics[0] = RDPMC_FIXED_SLOTS | FIXED_COUNTER_SLOTS,
125   .metrics[1] = RDPMC_L1_METRICS | METRIC_COUNTER_TOPDOWN_L1,
126   .n_metrics = 2,
127   .cpu_supports = topdown_lvl1_cpu_supports,
128   .n_cpu_supports = ARRAY_LEN (topdown_lvl1_cpu_supports),
129   .format_fn = format_topdown_lvl1,
130   .column_headers = PERFMON_STRINGS ("% NS", "% ST", "% NS.RT", "% NS.BS",
131                                      "% ST.FE", "% ST.BE"),
132   .footer = "Not Stalled (NS),STalled (ST),\n"
133             " Retiring (RT), Bad Speculation (BS),\n"
134             " FrontEnd bound (FE), BackEnd bound (BE)",
135 };