2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/sysctl.h>
41 #include <libpmcstat.h>
42 #include "pmu-events/pmu-events.h"
44 #if defined(__amd64__)
45 struct pmu_event_desc {
51 static const struct pmu_events_map *
52 pmu_events_map_get(void)
56 const struct pmu_events_map *pme;
58 if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
59 (void *)NULL, 0) == -1)
61 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
62 (void *)NULL, 0) == -1)
64 for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
65 if (strcmp(buf, pme->cpuid) == 0)
70 static const struct pmu_event *
71 pmu_event_get(const char *event_name)
73 const struct pmu_events_map *pme;
74 const struct pmu_event *pe;
76 if ((pme = pmu_events_map_get()) == NULL)
78 for (pe = pme->table; pe->name != NULL; pe++)
79 if (strcmp(pe->name, event_name) == 0)
85 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
88 char *kvp, *key, *value;
90 if ((event = strdup(eventin)) == NULL)
92 bzero(ped, sizeof(*ped));
93 while ((kvp = strsep(&event, ",")) != NULL) {
94 key = strsep(&kvp, "=");
98 if (strcmp(key, "umask") == 0)
99 ped->ped_umask = strtol(value, NULL, 16);
100 if (strcmp(key, "event") == 0)
101 ped->ped_event = strtol(value, NULL, 16);
102 if (strcmp(key, "period") == 0)
103 ped->ped_umask = strtol(value, NULL, 10);
110 pmcstat_pmu_sample_rate_get(const char *event_name)
112 const struct pmu_event *pe;
113 struct pmu_event_desc ped;
115 if ((pe = pmu_event_get(event_name)) == NULL)
116 return (DEFAULT_SAMPLE_COUNT);
117 if (pe->alias && (pe = pmu_event_get(pe->alias)) == NULL)
118 return (DEFAULT_SAMPLE_COUNT);
119 if (pe->event == NULL)
120 return (DEFAULT_SAMPLE_COUNT);
121 if (pmu_parse_event(&ped, pe->event))
122 return (DEFAULT_SAMPLE_COUNT);
123 return (ped.ped_period);
127 uint64_t pmcstat_pmu_sample_rate_get(void) { return (DEFAULT_SAMPLE_COUNT); }