]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpmcstat/libpmcstat_pmu_util.c
Revert r334242 "pmc(3)/hwpmc(4): update supported Intel processors to rely fully...
[FreeBSD/FreeBSD.git] / lib / libpmcstat / libpmcstat_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 *
67 pmu_alias_get(const char *name)
68 {
69         struct pmu_alias *pa;
70
71         for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
72                 if (strcasecmp(name, pa->pa_alias) == 0)
73                         return (pa->pa_name);
74         return (name);
75 }
76
77 struct pmu_event_desc {
78         uint32_t ped_umask;
79         uint32_t ped_event;
80         uint64_t ped_period;
81 };
82
83 static const struct pmu_events_map *
84 pmu_events_map_get(void)
85 {
86         size_t s;
87         char buf[64];
88         const struct pmu_events_map *pme;
89
90         if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
91                                          (void *)NULL, 0) == -1)
92                 return (NULL);
93         if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
94                                          (void *)NULL, 0) == -1)
95                 return (NULL);
96         for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
97                 if (strcmp(buf, pme->cpuid) == 0)
98                         return (pme);
99         return (NULL);
100 }
101
102 static const struct pmu_event *
103 pmu_event_get(const char *event_name)
104 {
105         const struct pmu_events_map *pme;
106         const struct pmu_event *pe;
107
108         if ((pme = pmu_events_map_get()) == NULL)
109                 return (NULL);
110         for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
111                 if (pe->name == NULL)
112                         continue;
113                 if (strcasecmp(pe->name, event_name) == 0)
114                         return (pe);
115         }
116         return (NULL);
117 }
118
119 static int
120 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
121 {
122         char *event;
123         char *kvp, *key, *value;
124
125         if ((event = strdup(eventin)) == NULL)
126                 return (ENOMEM);
127         bzero(ped, sizeof(*ped));
128         while ((kvp = strsep(&event, ",")) != NULL) {
129                 key = strsep(&kvp, "=");
130                 if (key == NULL)
131                         abort();
132                 value = kvp;
133                 if (strcmp(key, "umask") == 0)
134                         ped->ped_umask = strtol(value, NULL, 16);
135                 if (strcmp(key, "event") == 0)
136                         ped->ped_event = strtol(value, NULL, 16);
137                 if (strcmp(key, "period") == 0)
138                         ped->ped_period = strtol(value, NULL, 10);
139         }
140         free(event);
141         return (0);
142 }
143
144 uint64_t
145 pmcstat_pmu_sample_rate_get(const char *event_name)
146 {
147         const struct pmu_event *pe;
148         struct pmu_event_desc ped;
149
150         event_name = pmu_alias_get(event_name);
151         if ((pe = pmu_event_get(event_name)) == NULL)
152                 return (DEFAULT_SAMPLE_COUNT);
153         if (pe->alias && (pe = pmu_event_get(pe->alias)) == NULL)
154                 return (DEFAULT_SAMPLE_COUNT);
155         if (pe->event == NULL)
156                 return (DEFAULT_SAMPLE_COUNT);
157         if (pmu_parse_event(&ped, pe->event))
158                 return (DEFAULT_SAMPLE_COUNT);
159         return (ped.ped_period);
160 }
161
162 #else
163 uint64_t pmcstat_pmu_sample_rate_get(const char *event_name __unused) { return (DEFAULT_SAMPLE_COUNT); }
164 #endif