]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpmc/libpmc_pmu_util.c
libpmc: remove fixed counter diagnostic
[FreeBSD/FreeBSD.git] / lib / libpmc / libpmc_pmu_util.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018, Matthew Macy
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  *
29  */
30
31 #include <sys/types.h>
32 #include <sys/errno.h>
33 #include <sys/sysctl.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <pmc.h>
39 #include <pmclog.h>
40 #include <libpmcstat.h>
41 #include "pmu-events/pmu-events.h"
42
43 #if defined(__amd64__)
44 struct pmu_alias {
45         const char *pa_alias;
46         const char *pa_name;
47 };
48 static struct pmu_alias pmu_alias_table[] = {
49     { "UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
50     { "UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
51         { "LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
52         { "LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
53         { "LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
54         { "LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
55         { "LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
56         { "LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
57         { "RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
58         { "RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
59         { "BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
60         { "BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
61         { "BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
62         { "BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
63         { NULL, NULL },
64 };
65
66 static const char *fixed_mode_cntrs[] = {
67         "inst_retired.any",
68         "cpu_clk_unhalted.thread",
69         "cpu_clk_unhalted.thread_any",
70         "cpu_clk_unhalted.ref_tsc",
71         NULL
72 };
73
74 static const char *
75 pmu_alias_get(const char *name)
76 {
77         struct pmu_alias *pa;
78
79         for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
80                 if (strcasecmp(name, pa->pa_alias) == 0)
81                         return (pa->pa_name);
82         return (name);
83 }
84
85 struct pmu_event_desc {
86         uint64_t ped_period;
87         uint64_t ped_offcore_rsp;
88         uint32_t ped_event;
89         uint32_t ped_frontend;
90         uint32_t ped_ldlat;
91         uint32_t ped_config1;
92         uint8_t ped_umask;
93         uint8_t ped_cmask;
94         uint8_t ped_any;
95         uint8_t ped_inv;
96         uint8_t ped_edge;
97         uint8_t ped_fc_mask;
98         uint8_t ped_ch_mask;
99 };
100
101 static const struct pmu_events_map *
102 pmu_events_map_get(void)
103 {
104         size_t s;
105         char buf[64];
106         const struct pmu_events_map *pme;
107
108         if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
109                                          (void *)NULL, 0) == -1)
110                 return (NULL);
111         if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
112                                          (void *)NULL, 0) == -1)
113                 return (NULL);
114         for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
115                 if (strcmp(buf, pme->cpuid) == 0)
116                         return (pme);
117         return (NULL);
118 }
119
120 static const struct pmu_event *
121 pmu_event_get(const char *event_name, int *idx)
122 {
123         const struct pmu_events_map *pme;
124         const struct pmu_event *pe;
125         int i;
126
127         if ((pme = pmu_events_map_get()) == NULL)
128                 return (NULL);
129         for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
130                 if (pe->name == NULL)
131                         continue;
132                 if (strcasecmp(pe->name, event_name) == 0) {
133                         if (idx)
134                                 *idx = i;
135                         return (pe);
136                 }
137         }
138         return (NULL);
139 }
140
141 const char *
142 pmc_pmu_event_get_by_idx(int idx)
143 {
144         const struct pmu_events_map *pme;
145         const struct pmu_event *pe;
146         int i;
147
148         if ((pme = pmu_events_map_get()) == NULL)
149                 return (NULL);
150         for (i = 0, pe = pme->table; (pe->name || pe->desc || pe->event) && i < idx; pe++, i++)
151                 ;
152         return (pe->name);
153 }
154
155 static int
156 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
157 {
158         char *event;
159         char *kvp, *key, *value;
160         char *debug;
161
162         if ((event = strdup(eventin)) == NULL)
163                 return (ENOMEM);
164         bzero(ped, sizeof(*ped));
165         while ((kvp = strsep(&event, ",")) != NULL) {
166                 key = strsep(&kvp, "=");
167                 if (key == NULL)
168                         abort();
169                 value = kvp;
170                 if (strcmp(key, "umask") == 0)
171                         ped->ped_umask = strtol(value, NULL, 16);
172                 else if (strcmp(key, "event") == 0)
173                         ped->ped_event = strtol(value, NULL, 16);
174                 else if (strcmp(key, "period") == 0)
175                         ped->ped_period = strtol(value, NULL, 10);
176                 else if (strcmp(key, "offcore_rsp") == 0)
177                         ped->ped_offcore_rsp = strtol(value, NULL, 16);
178                 else if (strcmp(key, "any") == 0)
179                         ped->ped_any = strtol(value, NULL, 10);
180                 else if (strcmp(key, "cmask") == 0)
181                         ped->ped_cmask = strtol(value, NULL, 10);
182                 else if (strcmp(key, "inv") == 0)
183                         ped->ped_inv = strtol(value, NULL, 10);
184                 else if (strcmp(key, "edge") == 0)
185                         ped->ped_edge = strtol(value, NULL, 10);
186                 else if (strcmp(key, "frontend") == 0)
187                         ped->ped_frontend = strtol(value, NULL, 16);
188                 else if (strcmp(key, "ldlat") == 0)
189                         ped->ped_ldlat = strtol(value, NULL, 16);
190                 else if (strcmp(key, "fc_mask") == 0)
191                         ped->ped_fc_mask = strtol(value, NULL, 16);
192                 else if (strcmp(key, "ch_mask") == 0)
193                         ped->ped_ch_mask = strtol(value, NULL, 16);
194                 else if (strcmp(key, "config1") == 0)
195                         ped->ped_config1 = strtol(value, NULL, 16);
196                 else {
197                         debug = getenv("PMUDEBUG");
198                         if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
199                                 printf("unrecognized kvpair: %s:%s\n", key, value);
200                 }
201         }
202         free(event);
203         return (0);
204 }
205
206 uint64_t
207 pmc_pmu_sample_rate_get(const char *event_name)
208 {
209         const struct pmu_event *pe;
210         struct pmu_event_desc ped;
211
212         event_name = pmu_alias_get(event_name);
213         if ((pe = pmu_event_get(event_name, NULL)) == NULL)
214                 return (DEFAULT_SAMPLE_COUNT);
215         if (pe->alias && (pe = pmu_event_get(pe->alias, NULL)) == NULL)
216                 return (DEFAULT_SAMPLE_COUNT);
217         if (pe->event == NULL)
218                 return (DEFAULT_SAMPLE_COUNT);
219         if (pmu_parse_event(&ped, pe->event))
220                 return (DEFAULT_SAMPLE_COUNT);
221         return (ped.ped_period);
222 }
223
224 int
225 pmc_pmu_enabled(void)
226 {
227
228         return (pmu_events_map_get() != NULL);
229 }
230
231 void
232 pmc_pmu_print_counters(void)
233 {
234         const struct pmu_events_map *pme;
235         const struct pmu_event *pe;
236         struct pmu_event_desc ped;
237         char *debug;
238         int do_debug;
239         
240         debug = getenv("PMUDEBUG");
241         do_debug = 0;
242
243         if (debug != NULL && strcmp(debug, "true") == 0)
244                 do_debug = 1;
245         if ((pme = pmu_events_map_get()) == NULL)
246                 return;
247         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
248                 if (pe->name == NULL)
249                         continue;
250                 printf("\t%s\n", pe->name);
251                 if (do_debug)
252                         pmu_parse_event(&ped, pe->event);
253         }
254 }
255
256 void
257 pmc_pmu_print_counter_desc(const char *ev)
258 {
259         const struct pmu_events_map *pme;
260         const struct pmu_event *pe;
261
262         if ((pme = pmu_events_map_get()) == NULL)
263                 return;
264         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
265                 if (pe->name == NULL)
266                         continue;
267                 if (strcasestr(pe->name, ev) != NULL &&
268                         pe->desc != NULL)
269                                 printf("%s:\t%s\n", pe->name, pe->desc);
270         }
271 }
272
273 void
274 pmc_pmu_print_counter_desc_long(const char *ev)
275 {
276         const struct pmu_events_map *pme;
277         const struct pmu_event *pe;
278
279         if ((pme = pmu_events_map_get()) == NULL)
280                 return;
281         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
282                 if (pe->name == NULL)
283                         continue;
284                 if (strcasestr(pe->name, ev) != NULL) {
285                         if (pe->long_desc != NULL)
286                                 printf("%s:\n%s\n", pe->name, pe->long_desc);
287                         else if (pe->desc != NULL)
288                                 printf("%s:\t%s\n", pe->name, pe->desc);
289                 }
290         }
291 }
292
293 int
294 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
295 {
296         const struct pmu_event *pe;
297         struct pmu_event_desc ped;
298         struct pmc_md_iap_op_pmcallocate *iap;
299         struct pmc_md_iaf_op_pmcallocate *iaf;
300         int idx, isfixed;
301
302         iap = &pm->pm_md.pm_iap;
303         iaf = &pm->pm_md.pm_iaf;
304         isfixed = 0;
305         bzero(iap, sizeof(*iap));
306         event_name = pmu_alias_get(event_name);
307         pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
308         if ((pe = pmu_event_get(event_name, &idx)) == NULL)
309                 return (ENOENT);
310         if (pe->alias && (pe = pmu_event_get(pe->alias, &idx)) == NULL)
311                 return (ENOENT);
312         if (pe->event == NULL)
313                 return (ENOENT);
314         if (pmu_parse_event(&ped, pe->event))
315                 return (ENOENT);
316
317         for (idx = 0; fixed_mode_cntrs[idx] != NULL; idx++)
318                 if (strcmp(fixed_mode_cntrs[idx], event_name) == 0)
319                         isfixed = 1;
320
321         if (isfixed) {
322                 if (strcasestr(pe->desc, "retired") != NULL)
323                         pm->pm_ev = PMC_EV_IAF_INSTR_RETIRED_ANY;
324                 else if (strcasestr(pe->desc, "core") != NULL ||
325                                  strcasestr(pe->desc, "unhalted"))
326                         pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_CORE;
327                 else if (strcasestr(pe->desc, "ref") != NULL)
328                         pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_REF;
329                 iaf->pm_iaf_flags |= (IAF_USR | IAF_OS);
330                 if (ped.ped_any)
331                         iaf->pm_iaf_flags |= IAF_ANY;
332                 if (pm->pm_caps & PMC_CAP_INTERRUPT)
333                         iaf->pm_iaf_flags |= IAF_PMI;
334                 pm->pm_class = PMC_CLASS_IAF;
335                 return (0);
336         }
337         pm->pm_caps |= PMC_CAP_QUALIFIER;
338         pm->pm_class = PMC_CLASS_IAP;
339         pm->pm_ev = idx;
340         iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
341         iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
342         iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
343         iap->pm_iap_rsp = ped.ped_offcore_rsp;
344
345         iap->pm_iap_config |= (IAP_USR | IAP_OS);
346         if (ped.ped_edge)
347                 iap->pm_iap_config |= IAP_EDGE;
348         if (ped.ped_any)
349                 iap->pm_iap_config |= IAP_ANY;
350         if (ped.ped_inv)
351                 iap->pm_iap_config |= IAP_EDGE;
352         if (pm->pm_caps & PMC_CAP_INTERRUPT)
353                 iap->pm_iap_config |= IAP_INT;
354         return (0);
355 }
356
357 /*
358  * Ultimately rely on AMD calling theirs the same
359  */
360 static const char *stat_mode_cntrs[] = {
361         "cpu_clk_unhalted.thread_any",
362         "inst_retired.any",
363         "br_inst_retired.all_branches",
364         "br_misp_retired.all_branches",
365         "longest_lat_cache.reference",
366         "longest_lat_cache.miss",
367 };
368
369 int
370 pmc_pmu_stat_mode(const char ***cntrs)
371 {
372         if (pmc_pmu_enabled()) {
373                 *cntrs = stat_mode_cntrs;
374                 return (0);
375         }
376         return (EOPNOTSUPP);
377 }
378
379 #else
380 uint64_t pmc_pmu_sample_rate_get(const char *event_name __unused) { return (DEFAULT_SAMPLE_COUNT); }
381 void pmc_pmu_print_counters(void) {}
382 void pmc_pmu_print_counter_desc(const char *e __unused) {}
383 void pmc_pmu_print_counter_desc_long(const char *e __unused) {}
384 int pmc_pmu_enabled(void) { return (0); }
385 int pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) { return (EOPNOTSUPP); }
386 const char *pmc_pmu_event_get_by_idx(int idx __unused) { return (NULL); }
387 int pmc_pmu_stat_mode(const char ***a __unused) { return (EOPNOTSUPP); }
388
389 #endif