]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpmcstat/libpmcstat_pmu_util.c
libpmcstat: compile in events based on json description
[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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  *
30  */
31
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/sysctl.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <string.h>
39 #include <pmc.h>
40 #include <pmclog.h>
41 #include <libpmcstat.h>
42 #include "pmu-events/pmu-events.h"
43
44 #if defined(__amd64__)
45 struct pmu_event_desc {
46         uint32_t ped_umask;
47         uint32_t ped_event;
48         uint64_t ped_period;
49 };
50
51 static const struct pmu_events_map *
52 pmu_events_map_get(void)
53 {
54         size_t s;
55         char buf[64];
56         const struct pmu_events_map *pme;
57
58         if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
59                                          (void *)NULL, 0) == -1)
60                 return (NULL);
61         if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
62                                          (void *)NULL, 0) == -1)
63                 return (NULL);
64         for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
65                 if (strcmp(buf, pme->cpuid) == 0)
66                         return (pme);
67         return (NULL);
68 }
69
70 static const struct pmu_event *
71 pmu_event_get(const char *event_name)
72 {
73         const struct pmu_events_map *pme;
74         const struct pmu_event *pe;
75
76         if ((pme = pmu_events_map_get()) == NULL)
77                 return (NULL);
78         for (pe = pme->table; pe->name != NULL; pe++)
79                 if (strcmp(pe->name, event_name) == 0)
80                         return (pe);
81         return (NULL);
82 }
83
84 static int
85 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
86 {
87         char *event;
88         char *kvp, *key, *value;
89
90         if ((event = strdup(eventin)) == NULL)
91                 return (ENOMEM);
92         bzero(ped, sizeof(*ped));
93         while ((kvp = strsep(&event, ",")) != NULL) {
94                 key = strsep(&kvp, "=");
95                 if (key == NULL)
96                         abort();
97                 value = 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);
104         }
105         free(event);
106         return (0);
107 }
108
109 uint64_t
110 pmcstat_pmu_sample_rate_get(const char *event_name)
111 {
112         const struct pmu_event *pe;
113         struct pmu_event_desc ped;
114
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);
124 }
125
126 #else
127 uint64_t pmcstat_pmu_sample_rate_get(void) { return (DEFAULT_SAMPLE_COUNT); }
128 #endif