2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003-2008 Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
8 * Portions of this software were developed by A. Joseph Koshy under
9 * sponsorship from the FreeBSD Foundation and Google, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 #include "opt_hwpmc_hooks.h"
36 #include <sys/param.h>
37 #include <sys/ctype.h>
38 #include <sys/domainset.h>
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
45 #include <sys/pmckern.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
55 FEATURE(hwpmc_hooks, "Kernel support for HW PMC");
56 #define PMC_KERNEL_VERSION PMC_VERSION
58 #define PMC_KERNEL_VERSION 0
61 MALLOC_DECLARE(M_PMCHOOKS);
62 MALLOC_DEFINE(M_PMCHOOKS, "pmchooks", "Memory space for PMC hooks");
65 MALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");
67 const int pmc_kernel_version = PMC_KERNEL_VERSION;
70 int __read_mostly (*pmc_hook)(struct thread *td, int function, void *arg) = NULL;
72 /* Interrupt handler */
73 int __read_mostly (*pmc_intr)(struct trapframe *tf) = NULL;
75 DPCPU_DEFINE(uint8_t, pmc_sampled);
78 * A global count of SS mode PMCs. When non-zero, this means that
79 * we have processes that are sampling the system as a whole.
81 volatile int pmc_ss_count;
84 * Since PMC(4) may not be loaded in the current kernel, the
85 * convention followed is that a non-NULL value of 'pmc_hook' implies
86 * the presence of this kernel module.
88 * This requires us to protect 'pmc_hook' with a
89 * shared (sx) lock -- thus making the process of calling into PMC(4)
90 * somewhat more expensive than a simple 'if' check and indirect call.
93 SX_SYSINIT(pmcsx, &pmc_sx, "pmc-sx");
96 * PMC Soft per cpu trapframe.
98 struct trapframe pmc_tf[MAXCPU];
101 * Per domain list of buffer headers
103 __read_mostly struct pmc_domain_buffer_header *pmc_dom_hdrs[MAXMEMDOM];
106 * PMC Soft use a global table to store registered events.
109 SYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
112 static int pmc_softevents = 16;
113 SYSCTL_INT(_kern_hwpmc, OID_AUTO, softevents, CTLFLAG_RDTUN,
114 &pmc_softevents, 0, "maximum number of soft events");
117 struct pmc_soft **pmc_softs;
119 struct mtx pmc_softs_mtx;
120 MTX_SYSINIT(pmc_soft_mtx, &pmc_softs_mtx, "pmc-softs", MTX_SPIN);
127 * A note on the CPU numbering scheme used by the hwpmc(4) driver.
129 * CPUs are denoted using numbers in the range 0..[pmc_cpu_max()-1].
130 * CPUs could be numbered "sparsely" in this range; the predicate
131 * `pmc_cpu_is_present()' is used to test whether a given CPU is
132 * physically present.
134 * Further, a CPU that is physically present may be administratively
135 * disabled or otherwise unavailable for use by hwpmc(4). The
136 * `pmc_cpu_is_active()' predicate tests for CPU usability. An
137 * "active" CPU participates in thread scheduling and can field
138 * interrupts raised by PMC hardware.
140 * On systems with hyperthreaded CPUs, multiple logical CPUs may share
141 * PMC hardware resources. For such processors one logical CPU is
142 * denoted as the primary owner of the in-CPU PMC resources. The
143 * pmc_cpu_is_primary() predicate is used to distinguish this primary
144 * CPU from the others.
148 pmc_cpu_is_active(int cpu)
151 return (pmc_cpu_is_present(cpu) &&
152 !CPU_ISSET(cpu, &hlt_cpus_mask));
160 pmc_cpu_is_disabled(int cpu)
162 return (!pmc_cpu_is_active(cpu));
166 pmc_cpu_is_present(int cpu)
169 return (!CPU_ABSENT(cpu));
176 pmc_cpu_is_primary(int cpu)
179 return (!CPU_ISSET(cpu, &logical_cpus_mask));
186 * Return the maximum CPU number supported by the system. The return
187 * value is used for scaling internal data structures and for runtime
203 * Return the count of CPUs in the `active' state in the system.
206 pmc_cpu_max_active(void)
210 * When support for CPU hot-plugging is added to the kernel,
211 * this function would change to return the current number
223 * Cleanup event name:
224 * - remove duplicate '_'
228 pmc_soft_namecleanup(char *name)
234 for ( ; *p == '_' ; p++)
237 if (*p == '_' && (*(p + 1) == '_' || *(p + 1) == '\0'))
246 pmc_soft_ev_register(struct pmc_soft *ps)
248 static int warned = 0;
252 ps->ps_ev.pm_ev_code = 0; /* invalid */
253 pmc_soft_namecleanup(ps->ps_ev.pm_ev_name);
255 mtx_lock_spin(&pmc_softs_mtx);
257 if (pmc_softs_count >= pmc_softevents) {
259 * XXX Reusing events can enter a race condition where
260 * new allocated event will be used as an old one.
262 for (n = 0; n < pmc_softevents; n++)
263 if (pmc_softs[n] == NULL)
265 if (n == pmc_softevents) {
266 mtx_unlock_spin(&pmc_softs_mtx);
268 printf("hwpmc: too many soft events, "
269 "increase kern.hwpmc.softevents tunable\n");
275 ps->ps_ev.pm_ev_code = PMC_EV_SOFT_FIRST + n;
278 ps->ps_ev.pm_ev_code = PMC_EV_SOFT_FIRST + pmc_softs_count;
279 pmc_softs[pmc_softs_count++] = ps;
282 mtx_unlock_spin(&pmc_softs_mtx);
286 pmc_soft_ev_deregister(struct pmc_soft *ps)
289 KASSERT(ps != NULL, ("pmc_soft_deregister: called with NULL"));
291 mtx_lock_spin(&pmc_softs_mtx);
293 if (ps->ps_ev.pm_ev_code != 0 &&
294 (ps->ps_ev.pm_ev_code - PMC_EV_SOFT_FIRST) < pmc_softevents) {
295 KASSERT((int)ps->ps_ev.pm_ev_code >= PMC_EV_SOFT_FIRST &&
296 (int)ps->ps_ev.pm_ev_code <= PMC_EV_SOFT_LAST,
297 ("pmc_soft_deregister: invalid event value"));
298 pmc_softs[ps->ps_ev.pm_ev_code - PMC_EV_SOFT_FIRST] = NULL;
301 mtx_unlock_spin(&pmc_softs_mtx);
305 pmc_soft_ev_acquire(enum pmc_event ev)
309 if (ev == 0 || (ev - PMC_EV_SOFT_FIRST) >= pmc_softevents)
312 KASSERT((int)ev >= PMC_EV_SOFT_FIRST &&
313 (int)ev <= PMC_EV_SOFT_LAST,
314 ("event out of range"));
316 mtx_lock_spin(&pmc_softs_mtx);
318 ps = pmc_softs[ev - PMC_EV_SOFT_FIRST];
320 mtx_unlock_spin(&pmc_softs_mtx);
326 pmc_soft_ev_release(struct pmc_soft *ps)
329 mtx_unlock_spin(&pmc_softs_mtx);
336 init_hwpmc(void *dummy __unused)
340 if (pmc_softevents <= 0 ||
341 pmc_softevents > PMC_EV_DYN_COUNT) {
342 (void) printf("hwpmc: tunable \"softevents\"=%d out of "
343 "range.\n", pmc_softevents);
344 pmc_softevents = PMC_EV_DYN_COUNT;
346 pmc_softs = malloc(pmc_softevents * sizeof(*pmc_softs), M_PMCHOOKS,
349 for (domain = 0; domain < vm_ndomains; domain++) {
350 pmc_dom_hdrs[domain] = malloc_domainset(
351 sizeof(struct pmc_domain_buffer_header), M_PMC,
352 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
353 mtx_init(&pmc_dom_hdrs[domain]->pdbh_mtx, "pmc_bufferlist_mtx", "pmc-leaf", MTX_SPIN);
354 TAILQ_INIT(&pmc_dom_hdrs[domain]->pdbh_head);
357 domain = pcpu_find(cpu)->pc_domain;
358 KASSERT(pmc_dom_hdrs[domain] != NULL, ("no mem allocated for domain: %d", domain));
359 pmc_dom_hdrs[domain]->pdbh_ncpus++;
364 SYSINIT(hwpmc, SI_SUB_KDTRACE, SI_ORDER_FIRST, init_hwpmc, NULL);