2 * Copyright (c) 2003-2008 Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_hwpmc_hooks.h"
36 #include <sys/types.h>
37 #include <sys/ctype.h>
38 #include <sys/param.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
42 #include <sys/mutex.h>
44 #include <sys/pmckern.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
50 FEATURE(hwpmc_hooks, "Kernel support for HW PMC");
51 #define PMC_KERNEL_VERSION PMC_VERSION
53 #define PMC_KERNEL_VERSION 0
56 MALLOC_DECLARE(M_PMCHOOKS);
57 MALLOC_DEFINE(M_PMCHOOKS, "pmchooks", "Memory space for PMC hooks");
59 const int pmc_kernel_version = PMC_KERNEL_VERSION;
62 int (*pmc_hook)(struct thread *td, int function, void *arg) = NULL;
64 /* Interrupt handler */
65 int (*pmc_intr)(int cpu, struct trapframe *tf) = NULL;
67 /* Bitmask of CPUs requiring servicing at hardclock time */
68 volatile cpuset_t pmc_cpumask;
71 * A global count of SS mode PMCs. When non-zero, this means that
72 * we have processes that are sampling the system as a whole.
74 volatile int pmc_ss_count;
77 * Since PMC(4) may not be loaded in the current kernel, the
78 * convention followed is that a non-NULL value of 'pmc_hook' implies
79 * the presence of this kernel module.
81 * This requires us to protect 'pmc_hook' with a
82 * shared (sx) lock -- thus making the process of calling into PMC(4)
83 * somewhat more expensive than a simple 'if' check and indirect call.
88 * PMC Soft per cpu trapframe.
90 struct trapframe pmc_tf[MAXCPU];
93 * PMC Soft use a global table to store registered events.
96 SYSCTL_NODE(_kern, OID_AUTO, hwpmc, CTLFLAG_RW, 0, "HWPMC parameters");
98 static int pmc_softevents = 16;
99 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "softevents", &pmc_softevents);
100 SYSCTL_INT(_kern_hwpmc, OID_AUTO, softevents, CTLFLAG_TUN|CTLFLAG_RD,
101 &pmc_softevents, 0, "maximum number of soft events");
103 struct mtx pmc_softs_mtx;
105 struct pmc_soft **pmc_softs;
107 MTX_SYSINIT(pmc_soft_mtx, &pmc_softs_mtx, "pmc-softs", MTX_SPIN);
112 sx_init_flags(&pmc_sx, "pmc-sx", SX_NOWITNESS);
115 SYSINIT(pmcsx, SI_SUB_LOCK, SI_ORDER_MIDDLE, pmc_init_sx, NULL);
122 * A note on the CPU numbering scheme used by the hwpmc(4) driver.
124 * CPUs are denoted using numbers in the range 0..[pmc_cpu_max()-1].
125 * CPUs could be numbered "sparsely" in this range; the predicate
126 * `pmc_cpu_is_present()' is used to test whether a given CPU is
127 * physically present.
129 * Further, a CPU that is physically present may be administratively
130 * disabled or otherwise unavailable for use by hwpmc(4). The
131 * `pmc_cpu_is_active()' predicate tests for CPU usability. An
132 * "active" CPU participates in thread scheduling and can field
133 * interrupts raised by PMC hardware.
135 * On systems with hyperthreaded CPUs, multiple logical CPUs may share
136 * PMC hardware resources. For such processors one logical CPU is
137 * denoted as the primary owner of the in-CPU PMC resources. The
138 * pmc_cpu_is_primary() predicate is used to distinguish this primary
139 * CPU from the others.
143 pmc_cpu_is_active(int cpu)
146 return (pmc_cpu_is_present(cpu) &&
147 !CPU_ISSET(cpu, &hlt_cpus_mask));
155 pmc_cpu_is_disabled(int cpu)
157 return (!pmc_cpu_is_active(cpu));
161 pmc_cpu_is_present(int cpu)
164 return (!CPU_ABSENT(cpu));
171 pmc_cpu_is_primary(int cpu)
174 return (!CPU_ISSET(cpu, &logical_cpus_mask));
182 * Return the maximum CPU number supported by the system. The return
183 * value is used for scaling internal data structures and for runtime
199 * Return the count of CPUs in the `active' state in the system.
202 pmc_cpu_max_active(void)
206 * When support for CPU hot-plugging is added to the kernel,
207 * this function would change to return the current number
219 * Cleanup event name:
220 * - remove duplicate '_'
224 pmc_soft_namecleanup(char *name)
230 for ( ; *p == '_' ; p++)
233 if (*p == '_' && (*(p + 1) == '_' || *(p + 1) == '\0'))
242 pmc_soft_ev_register(struct pmc_soft *ps)
244 static int warned = 0;
248 ps->ps_ev.pm_ev_code = 0; /* invalid */
249 pmc_soft_namecleanup(ps->ps_ev.pm_ev_name);
251 mtx_lock_spin(&pmc_softs_mtx);
253 if (pmc_softs_count >= pmc_softevents) {
255 * XXX Reusing events can enter a race condition where
256 * new allocated event will be used as an old one.
258 for (n = 0; n < pmc_softevents; n++)
259 if (pmc_softs[n] == NULL)
261 if (n == pmc_softevents) {
262 mtx_unlock_spin(&pmc_softs_mtx);
264 printf("hwpmc: too many soft events, "
265 "increase kern.hwpmc.softevents tunable\n");
271 ps->ps_ev.pm_ev_code = PMC_EV_SOFT_FIRST + n;
274 ps->ps_ev.pm_ev_code = PMC_EV_SOFT_FIRST + pmc_softs_count;
275 pmc_softs[pmc_softs_count++] = ps;
278 mtx_unlock_spin(&pmc_softs_mtx);
282 pmc_soft_ev_deregister(struct pmc_soft *ps)
285 KASSERT(ps != NULL, ("pmc_soft_deregister: called with NULL"));
287 mtx_lock_spin(&pmc_softs_mtx);
289 if (ps->ps_ev.pm_ev_code != 0 &&
290 (ps->ps_ev.pm_ev_code - PMC_EV_SOFT_FIRST) < pmc_softevents) {
291 KASSERT(ps->ps_ev.pm_ev_code >= PMC_EV_SOFT_FIRST &&
292 ps->ps_ev.pm_ev_code <= PMC_EV_SOFT_LAST,
293 ("pmc_soft_deregister: invalid event value"));
294 pmc_softs[ps->ps_ev.pm_ev_code - PMC_EV_SOFT_FIRST] = NULL;
297 mtx_unlock_spin(&pmc_softs_mtx);
301 pmc_soft_ev_acquire(enum pmc_event ev)
305 if (ev == 0 || (ev - PMC_EV_SOFT_FIRST) >= pmc_softevents)
308 KASSERT(ev >= PMC_EV_SOFT_FIRST &&
309 ev <= PMC_EV_SOFT_LAST,
310 ("event out of range"));
312 mtx_lock_spin(&pmc_softs_mtx);
314 ps = pmc_softs[ev - PMC_EV_SOFT_FIRST];
316 mtx_unlock_spin(&pmc_softs_mtx);
322 pmc_soft_ev_release(struct pmc_soft *ps)
325 mtx_unlock_spin(&pmc_softs_mtx);
332 init_hwpmc(void *dummy __unused)
334 if (pmc_softevents <= 0 ||
335 pmc_softevents > PMC_EV_DYN_COUNT) {
336 (void) printf("hwpmc: tunable \"softevents\"=%d out of "
337 "range.\n", pmc_softevents);
338 pmc_softevents = PMC_EV_DYN_COUNT;
340 pmc_softs = malloc(pmc_softevents * sizeof(struct pmc_soft *), M_PMCHOOKS, M_NOWAIT|M_ZERO);
341 KASSERT(pmc_softs != NULL, ("cannot allocate soft events table"));
344 SYSINIT(hwpmc, SI_SUB_KDTRACE, SI_ORDER_FIRST, init_hwpmc, NULL);