]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpmc/libpmc_pmu_util.c
hwpmc: ABI fixes
[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__) || defined(__i386__)
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         {"cycles", "tsc-tsc"},
64         {"instructions", "inst-retired.any_p"},
65         {"branch-mispredicts", "br_misp_retired.all_branches" },
66         {"branches", "br_inst_retired.all_branches" },
67         {"interrupts", "hw_interrupts.received"},
68         {"ic-misses", "frontend_retired.l1i_miss"},
69         {NULL, NULL},
70 };
71
72 /*
73  *  The Intel fixed mode counters are:
74  *      "inst_retired.any",
75  *      "cpu_clk_unhalted.thread",
76  *      "cpu_clk_unhalted.thread_any",
77  *      "cpu_clk_unhalted.ref_tsc",
78  *
79  */
80
81 static const char *
82 pmu_alias_get(const char *name)
83 {
84         struct pmu_alias *pa;
85
86         for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
87                 if (strcasecmp(name, pa->pa_alias) == 0)
88                         return (pa->pa_name);
89         return (name);
90 }
91
92 struct pmu_event_desc {
93         uint64_t ped_period;
94         uint64_t ped_offcore_rsp;
95         uint32_t ped_event;
96         uint32_t ped_frontend;
97         uint32_t ped_ldlat;
98         uint32_t ped_config1;
99         int16_t ped_umask;
100         uint8_t ped_cmask;
101         uint8_t ped_any;
102         uint8_t ped_inv;
103         uint8_t ped_edge;
104         uint8_t ped_fc_mask;
105         uint8_t ped_ch_mask;
106 };
107
108 static const struct pmu_events_map *
109 pmu_events_map_get(void)
110 {
111         size_t s;
112         char buf[64];
113         const struct pmu_events_map *pme;
114
115         if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
116             (void *)NULL, 0) == -1)
117                 return (NULL);
118         if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
119             (void *)NULL, 0) == -1)
120                 return (NULL);
121         for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
122                 if (strcmp(buf, pme->cpuid) == 0)
123                         return (pme);
124         return (NULL);
125 }
126
127 static const struct pmu_event *
128 pmu_event_get(const char *event_name, int *idx)
129 {
130         const struct pmu_events_map *pme;
131         const struct pmu_event *pe;
132         int i;
133
134         if ((pme = pmu_events_map_get()) == NULL)
135                 return (NULL);
136         for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
137                 if (pe->name == NULL)
138                         continue;
139                 if (strcasecmp(pe->name, event_name) == 0) {
140                         if (idx)
141                                 *idx = i;
142                         return (pe);
143                 }
144         }
145         return (NULL);
146 }
147
148 const char *
149 pmc_pmu_event_get_by_idx(int idx)
150 {
151         const struct pmu_events_map *pme;
152         const struct pmu_event *pe;
153         int i;
154
155         if ((pme = pmu_events_map_get()) == NULL)
156                 return (NULL);
157         for (i = 0, pe = pme->table; (pe->name || pe->desc || pe->event) && i < idx; pe++, i++);
158         return (pe->name);
159 }
160
161 static int
162 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
163 {
164         char *event;
165         char *kvp, *key, *value, *r;
166         char *debug;
167
168         if ((event = strdup(eventin)) == NULL)
169                 return (ENOMEM);
170         r = event;
171         bzero(ped, sizeof(*ped));
172         ped->ped_umask = -1;
173         while ((kvp = strsep(&event, ",")) != NULL) {
174                 key = strsep(&kvp, "=");
175                 if (key == NULL)
176                         abort();
177                 value = kvp;
178                 if (strcmp(key, "umask") == 0)
179                         ped->ped_umask = strtol(value, NULL, 16);
180                 else if (strcmp(key, "event") == 0)
181                         ped->ped_event = strtol(value, NULL, 16);
182                 else if (strcmp(key, "period") == 0)
183                         ped->ped_period = strtol(value, NULL, 10);
184                 else if (strcmp(key, "offcore_rsp") == 0)
185                         ped->ped_offcore_rsp = strtol(value, NULL, 16);
186                 else if (strcmp(key, "any") == 0)
187                         ped->ped_any = strtol(value, NULL, 10);
188                 else if (strcmp(key, "cmask") == 0)
189                         ped->ped_cmask = strtol(value, NULL, 10);
190                 else if (strcmp(key, "inv") == 0)
191                         ped->ped_inv = strtol(value, NULL, 10);
192                 else if (strcmp(key, "edge") == 0)
193                         ped->ped_edge = strtol(value, NULL, 10);
194                 else if (strcmp(key, "frontend") == 0)
195                         ped->ped_frontend = strtol(value, NULL, 16);
196                 else if (strcmp(key, "ldlat") == 0)
197                         ped->ped_ldlat = strtol(value, NULL, 16);
198                 else if (strcmp(key, "fc_mask") == 0)
199                         ped->ped_fc_mask = strtol(value, NULL, 16);
200                 else if (strcmp(key, "ch_mask") == 0)
201                         ped->ped_ch_mask = strtol(value, NULL, 16);
202                 else if (strcmp(key, "config1") == 0)
203                         ped->ped_config1 = strtol(value, NULL, 16);
204                 else {
205                         debug = getenv("PMUDEBUG");
206                         if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
207                                 printf("unrecognized kvpair: %s:%s\n", key, value);
208                 }
209         }
210         free(r);
211         return (0);
212 }
213
214 uint64_t
215 pmc_pmu_sample_rate_get(const char *event_name)
216 {
217         const struct pmu_event *pe;
218         struct pmu_event_desc ped;
219
220         event_name = pmu_alias_get(event_name);
221         if ((pe = pmu_event_get(event_name, NULL)) == NULL)
222                 return (DEFAULT_SAMPLE_COUNT);
223         if (pe->alias && (pe = pmu_event_get(pe->alias, NULL)) == NULL)
224                 return (DEFAULT_SAMPLE_COUNT);
225         if (pe->event == NULL)
226                 return (DEFAULT_SAMPLE_COUNT);
227         if (pmu_parse_event(&ped, pe->event))
228                 return (DEFAULT_SAMPLE_COUNT);
229         return (ped.ped_period);
230 }
231
232 int
233 pmc_pmu_enabled(void)
234 {
235
236         return (pmu_events_map_get() != NULL);
237 }
238
239 void
240 pmc_pmu_print_counters(const char *event_name)
241 {
242         const struct pmu_events_map *pme;
243         const struct pmu_event *pe;
244         struct pmu_event_desc ped;
245         char *debug;
246         int do_debug;
247
248         debug = getenv("PMUDEBUG");
249         do_debug = 0;
250
251         if (debug != NULL && strcmp(debug, "true") == 0)
252                 do_debug = 1;
253         if ((pme = pmu_events_map_get()) == NULL)
254                 return;
255         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
256                 if (pe->name == NULL)
257                         continue;
258                 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
259                         continue;
260                 printf("\t%s\n", pe->name);
261                 if (do_debug)
262                         pmu_parse_event(&ped, pe->event);
263         }
264 }
265
266 void
267 pmc_pmu_print_counter_desc(const char *ev)
268 {
269         const struct pmu_events_map *pme;
270         const struct pmu_event *pe;
271
272         if ((pme = pmu_events_map_get()) == NULL)
273                 return;
274         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
275                 if (pe->name == NULL)
276                         continue;
277                 if (strcasestr(pe->name, ev) != NULL &&
278                     pe->desc != NULL)
279                         printf("%s:\t%s\n", pe->name, pe->desc);
280         }
281 }
282
283 void
284 pmc_pmu_print_counter_desc_long(const char *ev)
285 {
286         const struct pmu_events_map *pme;
287         const struct pmu_event *pe;
288
289         if ((pme = pmu_events_map_get()) == NULL)
290                 return;
291         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
292                 if (pe->name == NULL)
293                         continue;
294                 if (strcasestr(pe->name, ev) != NULL) {
295                         if (pe->long_desc != NULL)
296                                 printf("%s:\n%s\n", pe->name, pe->long_desc);
297                         else if (pe->desc != NULL)
298                                 printf("%s:\t%s\n", pe->name, pe->desc);
299                 }
300         }
301 }
302
303 void
304 pmc_pmu_print_counter_full(const char *ev)
305 {
306         const struct pmu_events_map *pme;
307         const struct pmu_event *pe;
308
309         if ((pme = pmu_events_map_get()) == NULL)
310                 return;
311         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
312                 if (pe->name == NULL)
313                         continue;
314                 if (strcasestr(pe->name, ev) == NULL)
315                         continue;
316                 printf("name: %s\n", pe->name);
317                 if (pe->long_desc != NULL)
318                         printf("desc: %s\n", pe->long_desc);
319                 else if (pe->desc != NULL)
320                         printf("desc: %s\n", pe->desc);
321                 if (pe->event != NULL)
322                         printf("event: %s\n", pe->event);
323                 if (pe->topic != NULL)
324                         printf("topic: %s\n", pe->topic);
325                 if (pe->pmu != NULL)
326                         printf("pmu: %s\n", pe->pmu);
327                 if (pe->unit != NULL)
328                         printf("unit: %s\n", pe->unit);
329                 if (pe->perpkg != NULL)
330                         printf("perpkg: %s\n", pe->perpkg);
331                 if (pe->metric_expr != NULL)
332                         printf("metric_expr: %s\n", pe->metric_expr);
333                 if (pe->metric_name != NULL)
334                         printf("metric_name: %s\n", pe->metric_name);
335                 if (pe->metric_group != NULL)
336                         printf("metric_group: %s\n", pe->metric_group);
337         }
338 }
339
340 int
341 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
342 {
343         const struct pmu_event *pe;
344         struct pmu_event_desc ped;
345         struct pmc_md_iap_op_pmcallocate *iap;
346         int idx, isfixed;
347
348         iap = &pm->pm_md.pm_iap;
349         isfixed = 0;
350         bzero(iap, sizeof(*iap));
351         event_name = pmu_alias_get(event_name);
352         pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
353         if ((pe = pmu_event_get(event_name, &idx)) == NULL)
354                 return (ENOENT);
355         if (pe->alias && (pe = pmu_event_get(pe->alias, &idx)) == NULL)
356                 return (ENOENT);
357         if (pe->event == NULL)
358                 return (ENOENT);
359         if (pmu_parse_event(&ped, pe->event))
360                 return (ENOENT);
361
362
363         if (strcasestr(event_name, "UNC_") == event_name ||
364                 strcasestr(event_name, "uncore") != NULL) {
365                 pm->pm_class = PMC_CLASS_UCP;
366                 pm->pm_caps |= PMC_CAP_QUALIFIER;
367         } else if ((ped.ped_umask == -1) ||
368                            (ped.ped_event == 0x0 && ped.ped_umask == 0x3)) {
369                 pm->pm_class = PMC_CLASS_IAF;
370         } else {
371                 pm->pm_class = PMC_CLASS_IAP;
372                 pm->pm_caps |= PMC_CAP_QUALIFIER;
373         }
374         pm->pm_ev = idx;
375         iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
376         if (ped.ped_umask > 0)
377                 iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
378         iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
379         iap->pm_iap_rsp = ped.ped_offcore_rsp;
380
381         iap->pm_iap_config |= (IAP_USR | IAP_OS);
382         if (ped.ped_edge)
383                 iap->pm_iap_config |= IAP_EDGE;
384         if (ped.ped_any)
385                 iap->pm_iap_config |= IAP_ANY;
386         if (ped.ped_inv)
387                 iap->pm_iap_config |= IAP_EDGE;
388         if (pm->pm_caps & PMC_CAP_INTERRUPT)
389                 iap->pm_iap_config |= IAP_INT;
390         return (0);
391 }
392
393 /*
394  * Ultimately rely on AMD calling theirs the same
395  */
396 static const char *stat_mode_cntrs[] = {
397         "cpu_clk_unhalted.thread_any",
398         "inst_retired.any",
399         "br_inst_retired.all_branches",
400         "br_misp_retired.all_branches",
401         "longest_lat_cache.reference",
402         "longest_lat_cache.miss",
403 };
404
405 int
406 pmc_pmu_stat_mode(const char ***cntrs)
407 {
408         if (pmc_pmu_enabled()) {
409                 *cntrs = stat_mode_cntrs;
410                 return (0);
411         }
412         return (EOPNOTSUPP);
413 }
414
415 #else
416
417 uint64_t
418 pmc_pmu_sample_rate_get(const char *event_name __unused)
419 {
420         return (DEFAULT_SAMPLE_COUNT);
421 }
422
423 void
424 pmc_pmu_print_counters(const char *event_name __unused)
425 {
426 }
427
428 void
429 pmc_pmu_print_counter_desc(const char *e __unused)
430 {
431 }
432
433 void
434 pmc_pmu_print_counter_desc_long(const char *e __unused)
435 {
436 }
437
438 void
439 pmc_pmu_print_counter_full(const char *e __unused)
440 {
441
442 }
443
444 int
445 pmc_pmu_enabled(void)
446 {
447         return (0);
448 }
449
450 int
451 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
452 {
453         return (EOPNOTSUPP);
454 }
455
456 const char *
457 pmc_pmu_event_get_by_idx(int idx __unused)
458 {
459         return (NULL);
460 }
461 int
462 pmc_pmu_stat_mode(const char ***a __unused)
463 {
464         return (EOPNOTSUPP);
465 }
466
467 #endif