perfmon: add Arm event bundles
[vpp.git] / src / plugins / perfmon / arm / bundle / branch_pred.c
1 /*
2  * Copyright (c) 2022 Arm 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/linux/sysfs.h>
18 #include <perfmon/perfmon.h>
19 #include <perfmon/arm/events.h>
20
21 /* as per .events[n] in PERFMON_REGISTER_BUNDLE */
22 enum
23 {
24   BR_RETIRED,
25   BR_MIS_PRED_RETIRED,
26   BR_PRED,
27   BR_MIS_PRED
28 };
29
30 static u8 *
31 format_arm_branch_pred (u8 *s, va_list *args)
32 {
33   perfmon_node_stats_t *ns = va_arg (*args, perfmon_node_stats_t *);
34   int row = va_arg (*args, int);
35
36   switch (row)
37     {
38     case 0:
39       s = format (s, "%.2f", (f64) ns->value[BR_RETIRED] / ns->n_calls);
40       break;
41
42     case 1:
43       s = format (s, "%.2f", (f64) ns->value[BR_RETIRED] / ns->n_packets);
44       break;
45
46     case 2:
47       s =
48         format (s, "%.2f", (f64) ns->value[BR_MIS_PRED_RETIRED] / ns->n_calls);
49       break;
50
51     case 3:
52       s = format (s, "%.2f",
53                   (f64) ns->value[BR_MIS_PRED_RETIRED] / ns->n_packets);
54       break;
55
56     case 4:
57       s =
58         format (s, "%.2f%%",
59                 (ns->value[BR_RETIRED] ? (f64) ns->value[BR_MIS_PRED_RETIRED] /
60                                            ns->value[BR_RETIRED] * 100 :
61                                                0));
62       break;
63
64     case 5:
65       s = format (s, "%.2f", (f64) ns->value[BR_PRED] / ns->n_calls);
66       break;
67
68     case 6:
69       s = format (s, "%.2f", (f64) ns->value[BR_PRED] / ns->n_packets);
70       break;
71
72     case 7:
73       s = format (s, "%.2f", (f64) ns->value[BR_MIS_PRED] / ns->n_calls);
74       break;
75
76     case 8:
77       s = format (s, "%.2f", (f64) ns->value[BR_MIS_PRED] / ns->n_packets);
78       break;
79
80     case 9:
81       s = format (s, "%.2f%%",
82                   (ns->value[BR_PRED] ?
83                            (f64) ns->value[BR_MIS_PRED] / ns->value[BR_PRED] * 100 :
84                            0));
85       break;
86
87     case 10:
88       s = format (s, "%llu", ns->n_packets);
89       break;
90     }
91   return s;
92 }
93
94 PERFMON_REGISTER_BUNDLE (arm_branch_pred) = {
95   .name = "branch-pred",
96   .description = "Branch (mis)predictions per call/packet",
97   .source = "arm",
98   .type = PERFMON_BUNDLE_TYPE_NODE,
99   .events[0] = ARMV8_PMUV3_BR_RETIRED,
100   .events[1] = ARMV8_PMUV3_BR_MIS_PRED_RETIRED,
101   .events[2] = ARMV8_PMUV3_BR_PRED,
102   .events[3] = ARMV8_PMUV3_BR_MIS_PRED,
103   .n_events = 4,
104   .n_columns = 11,
105   .format_fn = format_arm_branch_pred,
106   .column_headers = PERFMON_STRINGS ("[1.1]", "[1.2]", "[1.3]", "[1.4]", "\%",
107                                      "[2.1]", "[2.2]", "[2.3]", "[2.4]", "\%",
108                                      "pkts"),
109   /*
110    * set a bit for every event used in each column
111    * this allows us to disable columns at bundle registration if an
112    * event is not supported
113    */
114   .column_events = PERFMON_COLUMN_EVENTS (
115     SET_BIT (BR_RETIRED), SET_BIT (BR_RETIRED), SET_BIT (BR_MIS_PRED_RETIRED),
116     SET_BIT (BR_MIS_PRED_RETIRED),
117     SET_BIT (BR_RETIRED) | SET_BIT (BR_MIS_PRED_RETIRED), SET_BIT (BR_PRED),
118     SET_BIT (BR_PRED), SET_BIT (BR_MIS_PRED), SET_BIT (BR_MIS_PRED),
119     SET_BIT (BR_PRED) | SET_BIT (BR_MIS_PRED), 0),
120   .footer =
121     "An instruction that has been executed and retired is defined to\n"
122     "be architecturally executed. When a PE can perform speculative\n"
123     "execution, an instruction is not architecturally executed if the\n"
124     "PE discards the results of the speculative execution.\n\n"
125     "Per node statistics:\n"
126     "[1] Branch instruction architecturally executed\n"
127     "    [1.1] Branches/call\n"
128     "    [1.2] Branches/pkt\n"
129     "    [1.3] Mispredicted/call \n"
130     "    [1.4] Mispredicted/pkt\n"
131     "    [\%] Percentage of branches mispredicted\n"
132     "[2] Predictable branch speculatively executed\n"
133     "    [2.1] Branches/call\n"
134     "    [2.2] Branches/pkt\n"
135     "    [2.3] Mispredicted/call \n"
136     "    [2.4] Mispredicted/pkt\n"
137     "    [\%] Percentage of branches mispredicted\n\n"
138     "- See Armv8-A Architecture Reference Manual, D7.10 PMU events and"
139     " event numbers for full description.\n"
140 };