perfmon: top down level 1 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 <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_METRIC_RETIRING = 0,
31   TOPDOWN_E_METRIC_BAD_SPEC,
32   TOPDOWN_E_METRIC_FE_BOUND,
33   TOPDOWN_E_METRIC_BE_BOUND,
34 } topdown_lvl1_counters_t;
35
36 enum
37 {
38   TOPDOWN_SLOTS = 0,
39   TOPDOWN_METRICS,
40 } topdown_lvl1_metrics_t;
41
42 static_always_inline f32
43 topdown_lvl1_parse_row (perfmon_node_stats_t *ns, topdown_lvl1_counters_t e)
44 {
45   f64 slots_t0 =
46     ns->t[0].value[TOPDOWN_SLOTS] *
47     ((f64) GET_METRIC (ns->t[0].value[TOPDOWN_METRICS], e) / 0xff);
48   f64 slots_t1 =
49     ns->t[1].value[TOPDOWN_SLOTS] *
50     ((f64) GET_METRIC (ns->t[1].value[TOPDOWN_METRICS], e) / 0xff);
51   u64 slots_delta =
52     ns->t[1].value[TOPDOWN_SLOTS] - ns->t[0].value[TOPDOWN_SLOTS];
53
54   slots_t1 = slots_t1 - slots_t0;
55
56   return (slots_t1 / slots_delta) * 100;
57 }
58
59 static u8 *
60 format_topdown_lvl1 (u8 *s, va_list *args)
61 {
62   perfmon_node_stats_t *st = va_arg (*args, perfmon_node_stats_t *);
63   u64 row = va_arg (*args, int);
64
65   switch (row)
66     {
67     case 0:
68       s = format (s, "%f",
69                   topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_BAD_SPEC) +
70                     topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_RETIRING));
71       break;
72     case 1:
73       s = format (s, "%f",
74                   topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_BE_BOUND) +
75                     topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_FE_BOUND));
76       break;
77     case 2:
78       s = format (s, "%f",
79                   topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_RETIRING));
80       break;
81     case 3:
82       s = format (s, "%f",
83                   topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_BAD_SPEC));
84       break;
85     case 4:
86       s = format (s, "%f",
87                   topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_FE_BOUND));
88       break;
89     case 5:
90       s = format (s, "%f",
91                   topdown_lvl1_parse_row (st, TOPDOWN_E_METRIC_BE_BOUND));
92       break;
93     }
94   return s;
95 }
96
97 PERFMON_REGISTER_BUNDLE (topdown_lvl1) = {
98   .name = "topdown-level1",
99   .description = "Top-down Microarchitecture Analysis Level 1",
100   .source = "intel-core",
101   .type = PERFMON_BUNDLE_TYPE_NODE,
102   .offset_type = PERFMON_OFFSET_TYPE_METRICS,
103   .events[0] = INTEL_CORE_E_TOPDOWN_SLOTS,
104   .events[1] = INTEL_CORE_E_TOPDOWN_L1_METRICS,
105   .metrics[0] = RDPMC_FIXED_SLOTS | FIXED_COUNTER_SLOTS,
106   .metrics[1] = RDPMC_L1_METRICS | METRIC_COUNTER_TOPDOWN_L1,
107   .n_events = 2,
108   .cpu_supports = clib_cpu_supports_avx512_bitalg,
109   .format_fn = format_topdown_lvl1,
110   .column_headers = PERFMON_STRINGS ("% NS", "% ST", "% NS.RT", "% NS.BS",
111                                      "% ST.FE", "% ST.BE"),
112   .footer = "Not Stalled (NS),STalled (ST),\n"
113             " Retiring (RT), Bad Speculation (BS),\n"
114             " FrontEnd bound (FE), BackEnd bound (BE)",
115 };