2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2008 Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
6 * Copyright (c) 2018 Matthew Macy
9 * Portions of this software were developed by A. Joseph Koshy under
10 * sponsorship from the FreeBSD Foundation and Google, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/domainset.h>
41 #include <sys/eventhandler.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
52 #include <sys/pmckern.h>
53 #include <sys/pmclog.h>
56 #include <sys/queue.h>
57 #include <sys/resourcevar.h>
58 #include <sys/rwlock.h>
59 #include <sys/sched.h>
60 #include <sys/signalvar.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/syslog.h>
66 #include <sys/taskqueue.h>
67 #include <sys/vnode.h>
69 #include <sys/linker.h> /* needs to be after <sys/malloc.h> */
71 #include <machine/atomic.h>
72 #include <machine/md_var.h>
75 #include <vm/vm_extern.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
80 #include "hwpmc_soft.h"
82 #define PMC_EPOCH_ENTER() struct epoch_tracker pmc_et; epoch_enter_preempt(global_epoch_preempt, &pmc_et)
83 #define PMC_EPOCH_EXIT() epoch_exit_preempt(global_epoch_preempt, &pmc_et)
90 PMC_FLAG_NONE = 0x00, /* do nothing */
91 PMC_FLAG_REMOVE = 0x01, /* atomically remove entry from hash */
92 PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
93 PMC_FLAG_NOWAIT = 0x04, /* do not wait for mallocs */
97 * The offset in sysent where the syscall is allocated.
100 static int pmc_syscall_num = NO_SYSCALL;
101 struct pmc_cpu **pmc_pcpu; /* per-cpu state */
102 pmc_value_t *pmc_pcpu_saved; /* saved PMC values: CSW handling */
104 #define PMC_PCPU_SAVED(C,R) pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
106 struct mtx_pool *pmc_mtxpool;
107 static int *pmc_pmcdisp; /* PMC row dispositions */
109 #define PMC_ROW_DISP_IS_FREE(R) (pmc_pmcdisp[(R)] == 0)
110 #define PMC_ROW_DISP_IS_THREAD(R) (pmc_pmcdisp[(R)] > 0)
111 #define PMC_ROW_DISP_IS_STANDALONE(R) (pmc_pmcdisp[(R)] < 0)
113 #define PMC_MARK_ROW_FREE(R) do { \
114 pmc_pmcdisp[(R)] = 0; \
117 #define PMC_MARK_ROW_STANDALONE(R) do { \
118 KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
120 atomic_add_int(&pmc_pmcdisp[(R)], -1); \
121 KASSERT(pmc_pmcdisp[(R)] >= (-pmc_cpu_max_active()), \
122 ("[pmc,%d] row disposition error", __LINE__)); \
125 #define PMC_UNMARK_ROW_STANDALONE(R) do { \
126 atomic_add_int(&pmc_pmcdisp[(R)], 1); \
127 KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
131 #define PMC_MARK_ROW_THREAD(R) do { \
132 KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
134 atomic_add_int(&pmc_pmcdisp[(R)], 1); \
137 #define PMC_UNMARK_ROW_THREAD(R) do { \
138 atomic_add_int(&pmc_pmcdisp[(R)], -1); \
139 KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
144 /* various event handlers */
145 static eventhandler_tag pmc_exit_tag, pmc_fork_tag, pmc_kld_load_tag,
148 /* Module statistics */
149 struct pmc_driverstats pmc_stats;
152 /* Machine/processor dependent operations */
153 static struct pmc_mdep *md;
156 * Hash tables mapping owner processes and target threads to PMCs.
159 struct mtx pmc_processhash_mtx; /* spin mutex */
160 static u_long pmc_processhashmask;
161 static LIST_HEAD(pmc_processhash, pmc_process) *pmc_processhash;
164 * Hash table of PMC owner descriptors. This table is protected by
165 * the shared PMC "sx" lock.
168 static u_long pmc_ownerhashmask;
169 static LIST_HEAD(pmc_ownerhash, pmc_owner) *pmc_ownerhash;
172 * List of PMC owners with system-wide sampling PMCs.
175 static CK_LIST_HEAD(, pmc_owner) pmc_ss_owners;
178 * List of free thread entries. This is protected by the spin
181 static struct mtx pmc_threadfreelist_mtx; /* spin mutex */
182 static LIST_HEAD(, pmc_thread) pmc_threadfreelist;
183 static int pmc_threadfreelist_entries=0;
184 #define THREADENTRY_SIZE \
185 (sizeof(struct pmc_thread) + (md->pmd_npmc * sizeof(struct pmc_threadpmcstate)))
188 * Task to free thread descriptors
190 static struct task free_task;
193 * A map of row indices to classdep structures.
195 static struct pmc_classdep **pmc_rowindex_to_classdep;
202 static int pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
203 static int pmc_debugflags_parse(char *newstr, char *fence);
206 static int load(struct module *module, int cmd, void *arg);
207 static int pmc_add_sample(ring_type_t ring, struct pmc *pm, struct trapframe *tf);
208 static void pmc_add_thread_descriptors_from_proc(struct proc *p,
209 struct pmc_process *pp);
210 static int pmc_attach_process(struct proc *p, struct pmc *pm);
211 static struct pmc *pmc_allocate_pmc_descriptor(void);
212 static struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
213 static int pmc_attach_one_process(struct proc *p, struct pmc *pm);
214 static int pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
216 static int pmc_can_attach(struct pmc *pm, struct proc *p);
217 static void pmc_capture_user_callchain(int cpu, int soft, struct trapframe *tf);
218 static void pmc_cleanup(void);
219 static int pmc_detach_process(struct proc *p, struct pmc *pm);
220 static int pmc_detach_one_process(struct proc *p, struct pmc *pm,
222 static void pmc_destroy_owner_descriptor(struct pmc_owner *po);
223 static void pmc_destroy_pmc_descriptor(struct pmc *pm);
224 static void pmc_destroy_process_descriptor(struct pmc_process *pp);
225 static struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
226 static int pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
227 static struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
229 static struct pmc_process *pmc_find_process_descriptor(struct proc *p,
231 static struct pmc_thread *pmc_find_thread_descriptor(struct pmc_process *pp,
232 struct thread *td, uint32_t mode);
233 static void pmc_force_context_switch(void);
234 static void pmc_link_target_process(struct pmc *pm,
235 struct pmc_process *pp);
236 static void pmc_log_all_process_mappings(struct pmc_owner *po);
237 static void pmc_log_kernel_mappings(struct pmc *pm);
238 static void pmc_log_process_mappings(struct pmc_owner *po, struct proc *p);
239 static void pmc_maybe_remove_owner(struct pmc_owner *po);
240 static void pmc_process_csw_in(struct thread *td);
241 static void pmc_process_csw_out(struct thread *td);
242 static void pmc_process_exit(void *arg, struct proc *p);
243 static void pmc_process_fork(void *arg, struct proc *p1,
244 struct proc *p2, int n);
245 static void pmc_process_samples(int cpu, ring_type_t soft);
246 static void pmc_release_pmc_descriptor(struct pmc *pmc);
247 static void pmc_process_thread_add(struct thread *td);
248 static void pmc_process_thread_delete(struct thread *td);
249 static void pmc_process_thread_userret(struct thread *td);
250 static void pmc_remove_owner(struct pmc_owner *po);
251 static void pmc_remove_process_descriptor(struct pmc_process *pp);
252 static void pmc_restore_cpu_binding(struct pmc_binding *pb);
253 static void pmc_save_cpu_binding(struct pmc_binding *pb);
254 static void pmc_select_cpu(int cpu);
255 static int pmc_start(struct pmc *pm);
256 static int pmc_stop(struct pmc *pm);
257 static int pmc_syscall_handler(struct thread *td, void *syscall_args);
258 static struct pmc_thread *pmc_thread_descriptor_pool_alloc(void);
259 static void pmc_thread_descriptor_pool_drain(void);
260 static void pmc_thread_descriptor_pool_free(struct pmc_thread *pt);
261 static void pmc_unlink_target_process(struct pmc *pmc,
262 struct pmc_process *pp);
263 static int generic_switch_in(struct pmc_cpu *pc, struct pmc_process *pp);
264 static int generic_switch_out(struct pmc_cpu *pc, struct pmc_process *pp);
265 static struct pmc_mdep *pmc_generic_cpu_initialize(void);
266 static void pmc_generic_cpu_finalize(struct pmc_mdep *md);
267 static void pmc_post_callchain_callback(void);
268 static void pmc_process_threadcreate(struct thread *td);
269 static void pmc_process_threadexit(struct thread *td);
270 static void pmc_process_proccreate(struct proc *p);
271 static void pmc_process_allproc(struct pmc *pm);
274 * Kernel tunables and sysctl(8) interface.
277 SYSCTL_DECL(_kern_hwpmc);
278 SYSCTL_NODE(_kern_hwpmc, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
283 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_ignored, CTLFLAG_RW,
284 &pmc_stats.pm_intr_ignored, "# of interrupts ignored");
285 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_processed, CTLFLAG_RW,
286 &pmc_stats.pm_intr_processed, "# of interrupts processed");
287 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_bufferfull, CTLFLAG_RW,
288 &pmc_stats.pm_intr_bufferfull, "# of interrupts where buffer was full");
289 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, syscalls, CTLFLAG_RW,
290 &pmc_stats.pm_syscalls, "# of syscalls");
291 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, syscall_errors, CTLFLAG_RW,
292 &pmc_stats.pm_syscall_errors, "# of syscall_errors");
293 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, buffer_requests, CTLFLAG_RW,
294 &pmc_stats.pm_buffer_requests, "# of buffer requests");
295 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, buffer_requests_failed, CTLFLAG_RW,
296 &pmc_stats.pm_buffer_requests_failed, "# of buffer requests which failed");
297 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, log_sweeps, CTLFLAG_RW,
298 &pmc_stats.pm_log_sweeps, "# of ?");
299 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, merges, CTLFLAG_RW,
300 &pmc_stats.pm_merges, "# of times kernel stack was found for user trace");
301 SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, overwrites, CTLFLAG_RW,
302 &pmc_stats.pm_overwrites, "# of times a sample was overwritten before being logged");
304 static int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
305 SYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_RDTUN,
306 &pmc_callchaindepth, 0, "depth of call chain records");
309 SYSCTL_STRING(_kern_hwpmc, OID_AUTO, cpuid, CTLFLAG_RD,
310 pmc_cpuid, 0, "cpu version string");
312 struct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
313 char pmc_debugstr[PMC_DEBUG_STRSIZE];
314 TUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
315 sizeof(pmc_debugstr));
316 SYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
317 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_NEEDGIANT,
318 0, 0, pmc_debugflags_sysctl_handler, "A",
324 * kern.hwpmc.hashrows -- determines the number of rows in the
325 * of the hash table used to look up threads
328 static int pmc_hashsize = PMC_HASH_SIZE;
329 SYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_RDTUN,
330 &pmc_hashsize, 0, "rows in hash tables");
333 * kern.hwpmc.nsamples --- number of PC samples/callchain stacks per CPU
336 static int pmc_nsamples = PMC_NSAMPLES;
337 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_RDTUN,
338 &pmc_nsamples, 0, "number of PC samples per CPU");
340 static uint64_t pmc_sample_mask = PMC_NSAMPLES-1;
343 * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
346 static int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
347 SYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_RDTUN,
348 &pmc_mtxpool_size, 0, "size of spin mutex pool");
352 * kern.hwpmc.threadfreelist_entries -- number of free entries
355 SYSCTL_INT(_kern_hwpmc, OID_AUTO, threadfreelist_entries, CTLFLAG_RD,
356 &pmc_threadfreelist_entries, 0, "number of avalable thread entries");
360 * kern.hwpmc.threadfreelist_max -- maximum number of free entries
363 static int pmc_threadfreelist_max = PMC_THREADLIST_MAX;
364 SYSCTL_INT(_kern_hwpmc, OID_AUTO, threadfreelist_max, CTLFLAG_RW,
365 &pmc_threadfreelist_max, 0,
366 "maximum number of available thread entries before freeing some");
370 * security.bsd.unprivileged_syspmcs -- allow non-root processes to
371 * allocate system-wide PMCs.
373 * Allowing unprivileged processes to allocate system PMCs is convenient
374 * if system-wide measurements need to be taken concurrently with other
375 * per-process measurements. This feature is turned off by default.
378 static int pmc_unprivileged_syspmcs = 0;
379 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RWTUN,
380 &pmc_unprivileged_syspmcs, 0,
381 "allow unprivileged process to allocate system PMCs");
384 * Hash function. Discard the lower 2 bits of the pointer since
385 * these are always zero for our uses. The hash multiplier is
386 * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
390 #define _PMC_HM 11400714819323198486u
392 #define _PMC_HM 2654435769u
394 #error Must know the size of 'long' to compile
397 #define PMC_HASH_PTR(P,M) ((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
403 /* The `sysent' for the new syscall */
404 static struct sysent pmc_sysent = {
406 .sy_call = pmc_syscall_handler,
409 static struct syscall_module_data pmc_syscall_mod = {
412 .offset = &pmc_syscall_num,
413 .new_sysent = &pmc_sysent,
414 .old_sysent = { .sy_narg = 0, .sy_call = NULL },
415 .flags = SY_THR_STATIC_KLD,
418 static moduledata_t pmc_mod = {
419 .name = PMC_MODULE_NAME,
420 .evhand = syscall_module_handler,
421 .priv = &pmc_syscall_mod,
424 #ifdef EARLY_AP_STARTUP
425 DECLARE_MODULE(pmc, pmc_mod, SI_SUB_SYSCALLS, SI_ORDER_ANY);
427 DECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
429 MODULE_VERSION(pmc, PMC_VERSION);
432 enum pmc_dbgparse_state {
433 PMCDS_WS, /* in whitespace */
434 PMCDS_MAJOR, /* seen a major keyword */
439 pmc_debugflags_parse(char *newstr, char *fence)
442 struct pmc_debugflags *tmpflags;
443 int error, found, *newbits, tmp;
446 tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO);
451 for (; p < fence && (c = *p); p++) {
453 /* skip white space */
454 if (c == ' ' || c == '\t')
457 /* look for a keyword followed by "=" */
458 for (q = p; p < fence && (c = *p) && c != '='; p++)
468 /* lookup flag group name */
469 #define DBG_SET_FLAG_MAJ(S,F) \
470 if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0) \
471 newbits = &tmpflags->pdb_ ## F;
473 DBG_SET_FLAG_MAJ("cpu", CPU);
474 DBG_SET_FLAG_MAJ("csw", CSW);
475 DBG_SET_FLAG_MAJ("logging", LOG);
476 DBG_SET_FLAG_MAJ("module", MOD);
477 DBG_SET_FLAG_MAJ("md", MDP);
478 DBG_SET_FLAG_MAJ("owner", OWN);
479 DBG_SET_FLAG_MAJ("pmc", PMC);
480 DBG_SET_FLAG_MAJ("process", PRC);
481 DBG_SET_FLAG_MAJ("sampling", SAM);
483 if (newbits == NULL) {
488 p++; /* skip the '=' */
490 /* Now parse the individual flags */
493 for (q = p; p < fence && (c = *p); p++)
494 if (c == ' ' || c == '\t' || c == ',')
497 /* p == fence or c == ws or c == "," or c == 0 */
499 if ((kwlen = p - q) == 0) {
505 #define DBG_SET_FLAG_MIN(S,F) \
506 if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0) \
507 tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
509 /* a '*' denotes all possible flags in the group */
510 if (kwlen == 1 && *q == '*')
512 /* look for individual flag names */
513 DBG_SET_FLAG_MIN("allocaterow", ALR);
514 DBG_SET_FLAG_MIN("allocate", ALL);
515 DBG_SET_FLAG_MIN("attach", ATT);
516 DBG_SET_FLAG_MIN("bind", BND);
517 DBG_SET_FLAG_MIN("config", CFG);
518 DBG_SET_FLAG_MIN("exec", EXC);
519 DBG_SET_FLAG_MIN("exit", EXT);
520 DBG_SET_FLAG_MIN("find", FND);
521 DBG_SET_FLAG_MIN("flush", FLS);
522 DBG_SET_FLAG_MIN("fork", FRK);
523 DBG_SET_FLAG_MIN("getbuf", GTB);
524 DBG_SET_FLAG_MIN("hook", PMH);
525 DBG_SET_FLAG_MIN("init", INI);
526 DBG_SET_FLAG_MIN("intr", INT);
527 DBG_SET_FLAG_MIN("linktarget", TLK);
528 DBG_SET_FLAG_MIN("mayberemove", OMR);
529 DBG_SET_FLAG_MIN("ops", OPS);
530 DBG_SET_FLAG_MIN("read", REA);
531 DBG_SET_FLAG_MIN("register", REG);
532 DBG_SET_FLAG_MIN("release", REL);
533 DBG_SET_FLAG_MIN("remove", ORM);
534 DBG_SET_FLAG_MIN("sample", SAM);
535 DBG_SET_FLAG_MIN("scheduleio", SIO);
536 DBG_SET_FLAG_MIN("select", SEL);
537 DBG_SET_FLAG_MIN("signal", SIG);
538 DBG_SET_FLAG_MIN("swi", SWI);
539 DBG_SET_FLAG_MIN("swo", SWO);
540 DBG_SET_FLAG_MIN("start", STA);
541 DBG_SET_FLAG_MIN("stop", STO);
542 DBG_SET_FLAG_MIN("syscall", PMS);
543 DBG_SET_FLAG_MIN("unlinktarget", TUL);
544 DBG_SET_FLAG_MIN("write", WRI);
546 /* unrecognized flag name */
551 if (c == 0 || c == ' ' || c == '\t') { /* end of flag group */
560 /* save the new flag set */
561 bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
564 free(tmpflags, M_PMC);
569 pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
571 char *fence, *newstr;
575 (void) arg1; (void) arg2; /* unused parameters */
577 n = sizeof(pmc_debugstr);
578 newstr = malloc(n, M_PMC, M_WAITOK|M_ZERO);
579 (void) strlcpy(newstr, pmc_debugstr, n);
581 error = sysctl_handle_string(oidp, newstr, n, req);
583 /* if there is a new string, parse and copy it */
584 if (error == 0 && req->newptr != NULL) {
585 fence = newstr + (n < req->newlen ? n : req->newlen + 1);
586 if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
587 (void) strlcpy(pmc_debugstr, newstr,
588 sizeof(pmc_debugstr));
598 * Map a row index to a classdep structure and return the adjusted row
599 * index for the PMC class index.
601 static struct pmc_classdep *
602 pmc_ri_to_classdep(struct pmc_mdep *md, int ri, int *adjri)
604 struct pmc_classdep *pcd;
608 KASSERT(ri >= 0 && ri < md->pmd_npmc,
609 ("[pmc,%d] illegal row-index %d", __LINE__, ri));
611 pcd = pmc_rowindex_to_classdep[ri];
614 ("[pmc,%d] ri %d null pcd", __LINE__, ri));
616 *adjri = ri - pcd->pcd_ri;
618 KASSERT(*adjri >= 0 && *adjri < pcd->pcd_num,
619 ("[pmc,%d] adjusted row-index %d", __LINE__, *adjri));
625 * Concurrency Control
627 * The driver manages the following data structures:
629 * - target process descriptors, one per target process
630 * - owner process descriptors (and attached lists), one per owner process
631 * - lookup hash tables for owner and target processes
632 * - PMC descriptors (and attached lists)
633 * - per-cpu hardware state
634 * - the 'hook' variable through which the kernel calls into
636 * - the machine hardware state (managed by the MD layer)
638 * These data structures are accessed from:
640 * - thread context-switch code
641 * - interrupt handlers (possibly on multiple cpus)
642 * - kernel threads on multiple cpus running on behalf of user
643 * processes doing system calls
644 * - this driver's private kernel threads
646 * = Locks and Locking strategy =
648 * The driver uses four locking strategies for its operation:
650 * - The global SX lock "pmc_sx" is used to protect internal
653 * Calls into the module by syscall() start with this lock being
654 * held in exclusive mode. Depending on the requested operation,
655 * the lock may be downgraded to 'shared' mode to allow more
656 * concurrent readers into the module. Calls into the module from
657 * other parts of the kernel acquire the lock in shared mode.
659 * This SX lock is held in exclusive mode for any operations that
660 * modify the linkages between the driver's internal data structures.
662 * The 'pmc_hook' function pointer is also protected by this lock.
663 * It is only examined with the sx lock held in exclusive mode. The
664 * kernel module is allowed to be unloaded only with the sx lock held
665 * in exclusive mode. In normal syscall handling, after acquiring the
666 * pmc_sx lock we first check that 'pmc_hook' is non-null before
667 * proceeding. This prevents races between the thread unloading the module
668 * and other threads seeking to use the module.
670 * - Lookups of target process structures and owner process structures
671 * cannot use the global "pmc_sx" SX lock because these lookups need
672 * to happen during context switches and in other critical sections
673 * where sleeping is not allowed. We protect these lookup tables
674 * with their own private spin-mutexes, "pmc_processhash_mtx" and
675 * "pmc_ownerhash_mtx".
677 * - Interrupt handlers work in a lock free manner. At interrupt
678 * time, handlers look at the PMC pointer (phw->phw_pmc) configured
679 * when the PMC was started. If this pointer is NULL, the interrupt
680 * is ignored after updating driver statistics. We ensure that this
681 * pointer is set (using an atomic operation if necessary) before the
682 * PMC hardware is started. Conversely, this pointer is unset atomically
683 * only after the PMC hardware is stopped.
685 * We ensure that everything needed for the operation of an
686 * interrupt handler is available without it needing to acquire any
687 * locks. We also ensure that a PMC's software state is destroyed only
688 * after the PMC is taken off hardware (on all CPUs).
690 * - Context-switch handling with process-private PMCs needs more
693 * A given process may be the target of multiple PMCs. For example,
694 * PMCATTACH and PMCDETACH may be requested by a process on one CPU
695 * while the target process is running on another. A PMC could also
696 * be getting released because its owner is exiting. We tackle
697 * these situations in the following manner:
699 * - each target process structure 'pmc_process' has an array
700 * of 'struct pmc *' pointers, one for each hardware PMC.
702 * - At context switch IN time, each "target" PMC in RUNNING state
703 * gets started on hardware and a pointer to each PMC is copied into
704 * the per-cpu phw array. The 'runcount' for the PMC is
707 * - At context switch OUT time, all process-virtual PMCs are stopped
708 * on hardware. The saved value is added to the PMCs value field
709 * only if the PMC is in a non-deleted state (the PMCs state could
710 * have changed during the current time slice).
712 * Note that since in-between a switch IN on a processor and a switch
713 * OUT, the PMC could have been released on another CPU. Therefore
714 * context switch OUT always looks at the hardware state to turn
715 * OFF PMCs and will update a PMC's saved value only if reachable
716 * from the target process record.
718 * - OP PMCRELEASE could be called on a PMC at any time (the PMC could
719 * be attached to many processes at the time of the call and could
720 * be active on multiple CPUs).
722 * We prevent further scheduling of the PMC by marking it as in
723 * state 'DELETED'. If the runcount of the PMC is non-zero then
724 * this PMC is currently running on a CPU somewhere. The thread
725 * doing the PMCRELEASE operation waits by repeatedly doing a
726 * pause() till the runcount comes to zero.
728 * The contents of a PMC descriptor (struct pmc) are protected using
729 * a spin-mutex. In order to save space, we use a mutex pool.
731 * In terms of lock types used by witness(4), we use:
732 * - Type "pmc-sx", used by the global SX lock.
733 * - Type "pmc-sleep", for sleep mutexes used by logger threads.
734 * - Type "pmc-per-proc", for protecting PMC owner descriptors.
735 * - Type "pmc-leaf", used for all other spin mutexes.
739 * save the cpu binding of the current kthread
743 pmc_save_cpu_binding(struct pmc_binding *pb)
745 PMCDBG0(CPU,BND,2, "save-cpu");
746 thread_lock(curthread);
747 pb->pb_bound = sched_is_bound(curthread);
748 pb->pb_cpu = curthread->td_oncpu;
749 thread_unlock(curthread);
750 PMCDBG1(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
754 * restore the cpu binding of the current thread
758 pmc_restore_cpu_binding(struct pmc_binding *pb)
760 PMCDBG2(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
761 curthread->td_oncpu, pb->pb_cpu);
762 thread_lock(curthread);
764 sched_bind(curthread, pb->pb_cpu);
766 sched_unbind(curthread);
767 thread_unlock(curthread);
768 PMCDBG0(CPU,BND,2, "restore-cpu done");
772 * move execution over the specified cpu and bind it there.
776 pmc_select_cpu(int cpu)
778 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
779 ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
781 /* Never move to an inactive CPU. */
782 KASSERT(pmc_cpu_is_active(cpu), ("[pmc,%d] selecting inactive "
783 "CPU %d", __LINE__, cpu));
785 PMCDBG1(CPU,SEL,2, "select-cpu cpu=%d", cpu);
786 thread_lock(curthread);
787 sched_bind(curthread, cpu);
788 thread_unlock(curthread);
790 KASSERT(curthread->td_oncpu == cpu,
791 ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
792 cpu, curthread->td_oncpu));
794 PMCDBG1(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
798 * Force a context switch.
800 * We do this by pause'ing for 1 tick -- invoking mi_switch() is not
801 * guaranteed to force a context switch.
805 pmc_force_context_switch(void)
814 #if defined(__i386__) || defined(__amd64__)
815 if (__predict_true(amd_feature & AMDID_RDTSCP))
820 return get_cyclecount();
825 * Get the file name for an executable. This is a simple wrapper
826 * around vn_fullpath(9).
830 pmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
833 *fullpath = "unknown";
835 vn_fullpath(curthread, v, fullpath, freepath);
839 * remove an process owning PMCs
843 pmc_remove_owner(struct pmc_owner *po)
845 struct pmc *pm, *tmp;
847 sx_assert(&pmc_sx, SX_XLOCKED);
849 PMCDBG1(OWN,ORM,1, "remove-owner po=%p", po);
851 /* Remove descriptor from the owner hash table */
852 LIST_REMOVE(po, po_next);
854 /* release all owned PMC descriptors */
855 LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
856 PMCDBG1(OWN,ORM,2, "pmc=%p", pm);
857 KASSERT(pm->pm_owner == po,
858 ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
860 pmc_release_pmc_descriptor(pm); /* will unlink from the list */
861 pmc_destroy_pmc_descriptor(pm);
864 KASSERT(po->po_sscount == 0,
865 ("[pmc,%d] SS count not zero", __LINE__));
866 KASSERT(LIST_EMPTY(&po->po_pmcs),
867 ("[pmc,%d] PMC list not empty", __LINE__));
869 /* de-configure the log file if present */
870 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
871 pmclog_deconfigure_log(po);
875 * remove an owner process record if all conditions are met.
879 pmc_maybe_remove_owner(struct pmc_owner *po)
882 PMCDBG1(OWN,OMR,1, "maybe-remove-owner po=%p", po);
885 * Remove owner record if
886 * - this process does not own any PMCs
887 * - this process has not allocated a system-wide sampling buffer
890 if (LIST_EMPTY(&po->po_pmcs) &&
891 ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
892 pmc_remove_owner(po);
893 pmc_destroy_owner_descriptor(po);
898 * Add an association between a target process and a PMC.
902 pmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
905 struct pmc_target *pt;
907 struct pmc_thread *pt_td;
910 sx_assert(&pmc_sx, SX_XLOCKED);
912 KASSERT(pm != NULL && pp != NULL,
913 ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
914 KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
915 ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
916 __LINE__, pm, pp->pp_proc->p_pid));
917 KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= ((int) md->pmd_npmc - 1),
918 ("[pmc,%d] Illegal reference count %d for process record %p",
919 __LINE__, pp->pp_refcnt, (void *) pp));
921 ri = PMC_TO_ROWINDEX(pm);
923 PMCDBG3(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
927 LIST_FOREACH(pt, &pm->pm_targets, pt_next)
928 if (pt->pt_process == pp)
929 KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
933 pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO);
936 LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
938 atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
941 if (pm->pm_owner->po_owner == pp->pp_proc)
942 pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
945 * Initialize the per-process values at this row index.
947 pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
948 pm->pm_sc.pm_reloadcount : 0;
953 /* Confirm that the per-thread values at this row index are cleared. */
954 if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
955 mtx_lock_spin(pp->pp_tdslock);
956 LIST_FOREACH(pt_td, &pp->pp_tds, pt_next) {
957 KASSERT(pt_td->pt_pmcs[ri].pt_pmcval == (pmc_value_t) 0,
958 ("[pmc,%d] pt_pmcval not cleared for pid=%d at "
959 "ri=%d", __LINE__, pp->pp_proc->p_pid, ri));
961 mtx_unlock_spin(pp->pp_tdslock);
967 * Removes the association between a target process and a PMC.
971 pmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
975 struct pmc_target *ptgt;
976 struct pmc_thread *pt;
978 sx_assert(&pmc_sx, SX_XLOCKED);
980 KASSERT(pm != NULL && pp != NULL,
981 ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
983 KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt <= (int) md->pmd_npmc,
984 ("[pmc,%d] Illegal ref count %d on process record %p",
985 __LINE__, pp->pp_refcnt, (void *) pp));
987 ri = PMC_TO_ROWINDEX(pm);
989 PMCDBG3(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
992 KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
993 ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
994 ri, pm, pp->pp_pmcs[ri].pp_pmc));
996 pp->pp_pmcs[ri].pp_pmc = NULL;
997 pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
999 /* Clear the per-thread values at this row index. */
1000 if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1001 mtx_lock_spin(pp->pp_tdslock);
1002 LIST_FOREACH(pt, &pp->pp_tds, pt_next)
1003 pt->pt_pmcs[ri].pt_pmcval = (pmc_value_t) 0;
1004 mtx_unlock_spin(pp->pp_tdslock);
1007 /* Remove owner-specific flags */
1008 if (pm->pm_owner->po_owner == pp->pp_proc) {
1009 pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
1010 pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
1015 /* Remove the target process from the PMC structure */
1016 LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
1017 if (ptgt->pt_process == pp)
1020 KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
1021 "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
1023 LIST_REMOVE(ptgt, pt_next);
1026 /* if the PMC now lacks targets, send the owner a SIGIO */
1027 if (LIST_EMPTY(&pm->pm_targets)) {
1028 p = pm->pm_owner->po_owner;
1030 kern_psignal(p, SIGIO);
1033 PMCDBG2(PRC,SIG,2, "signalling proc=%p signal=%d", p,
1039 * Check if PMC 'pm' may be attached to target process 't'.
1043 pmc_can_attach(struct pmc *pm, struct proc *t)
1045 struct proc *o; /* pmc owner */
1046 struct ucred *oc, *tc; /* owner, target credentials */
1047 int decline_attach, i;
1050 * A PMC's owner can always attach that PMC to itself.
1053 if ((o = pm->pm_owner->po_owner) == t)
1067 * The effective uid of the PMC owner should match at least one
1068 * of the {effective,real,saved} uids of the target process.
1071 decline_attach = oc->cr_uid != tc->cr_uid &&
1072 oc->cr_uid != tc->cr_svuid &&
1073 oc->cr_uid != tc->cr_ruid;
1076 * Every one of the target's group ids, must be in the owner's
1079 for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
1080 decline_attach = !groupmember(tc->cr_groups[i], oc);
1082 /* check the read and saved gids too */
1083 if (decline_attach == 0)
1084 decline_attach = !groupmember(tc->cr_rgid, oc) ||
1085 !groupmember(tc->cr_svgid, oc);
1090 return !decline_attach;
1094 * Attach a process to a PMC.
1098 pmc_attach_one_process(struct proc *p, struct pmc *pm)
1101 char *fullpath, *freepath;
1102 struct pmc_process *pp;
1104 sx_assert(&pmc_sx, SX_XLOCKED);
1106 PMCDBG5(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
1107 PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1110 * Locate the process descriptor corresponding to process 'p',
1111 * allocating space as needed.
1113 * Verify that rowindex 'pm_rowindex' is free in the process
1116 * If not, allocate space for a descriptor and link the
1117 * process descriptor and PMC.
1119 ri = PMC_TO_ROWINDEX(pm);
1121 /* mark process as using HWPMCs */
1123 p->p_flag |= P_HWPMC;
1126 if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL) {
1131 if (pp->pp_pmcs[ri].pp_pmc == pm) {/* already present at slot [ri] */
1136 if (pp->pp_pmcs[ri].pp_pmc != NULL) {
1141 pmc_link_target_process(pm, pp);
1143 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
1144 (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
1145 pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
1147 pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
1149 /* issue an attach event to a configured log file */
1150 if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
1151 if (p->p_flag & P_KPROC) {
1152 fullpath = kernelname;
1155 pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1156 pmclog_process_pmcattach(pm, p->p_pid, fullpath);
1158 free(freepath, M_TEMP);
1159 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1160 pmc_log_process_mappings(pm->pm_owner, p);
1166 p->p_flag &= ~P_HWPMC;
1172 * Attach a process and optionally its children
1176 pmc_attach_process(struct proc *p, struct pmc *pm)
1181 sx_assert(&pmc_sx, SX_XLOCKED);
1183 PMCDBG5(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
1184 PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1188 * If this PMC successfully allowed a GETMSR operation
1189 * in the past, disallow further ATTACHes.
1192 if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
1195 if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1196 return pmc_attach_one_process(p, pm);
1199 * Traverse all child processes, attaching them to
1203 sx_slock(&proctree_lock);
1208 if ((error = pmc_attach_one_process(p, pm)) != 0)
1210 if (!LIST_EMPTY(&p->p_children))
1211 p = LIST_FIRST(&p->p_children);
1215 if (LIST_NEXT(p, p_sibling)) {
1216 p = LIST_NEXT(p, p_sibling);
1224 (void) pmc_detach_process(top, pm);
1227 sx_sunlock(&proctree_lock);
1232 * Detach a process from a PMC. If there are no other PMCs tracking
1233 * this process, remove the process structure from its hash table. If
1234 * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1238 pmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1241 struct pmc_process *pp;
1243 sx_assert(&pmc_sx, SX_XLOCKED);
1246 ("[pmc,%d] null pm pointer", __LINE__));
1248 ri = PMC_TO_ROWINDEX(pm);
1250 PMCDBG6(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1251 pm, ri, p, p->p_pid, p->p_comm, flags);
1253 if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1256 if (pp->pp_pmcs[ri].pp_pmc != pm)
1259 pmc_unlink_target_process(pm, pp);
1261 /* Issue a detach entry if a log file is configured */
1262 if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1263 pmclog_process_pmcdetach(pm, p->p_pid);
1266 * If there are no PMCs targeting this process, we remove its
1267 * descriptor from the target hash table and unset the P_HWPMC
1268 * flag in the struct proc.
1270 KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
1271 ("[pmc,%d] Illegal refcnt %d for process struct %p",
1272 __LINE__, pp->pp_refcnt, pp));
1274 if (pp->pp_refcnt != 0) /* still a target of some PMC */
1277 pmc_remove_process_descriptor(pp);
1279 if (flags & PMC_FLAG_REMOVE)
1280 pmc_destroy_process_descriptor(pp);
1283 p->p_flag &= ~P_HWPMC;
1290 * Detach a process and optionally its descendants from a PMC.
1294 pmc_detach_process(struct proc *p, struct pmc *pm)
1298 sx_assert(&pmc_sx, SX_XLOCKED);
1300 PMCDBG5(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1301 PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1303 if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1304 return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1307 * Traverse all children, detaching them from this PMC. We
1308 * ignore errors since we could be detaching a PMC from a
1309 * partially attached proc tree.
1312 sx_slock(&proctree_lock);
1317 (void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1319 if (!LIST_EMPTY(&p->p_children))
1320 p = LIST_FIRST(&p->p_children);
1324 if (LIST_NEXT(p, p_sibling)) {
1325 p = LIST_NEXT(p, p_sibling);
1333 sx_sunlock(&proctree_lock);
1335 if (LIST_EMPTY(&pm->pm_targets))
1336 pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1343 * Thread context switch IN
1347 pmc_process_csw_in(struct thread *td)
1350 unsigned int adjri, ri;
1355 pmc_value_t newvalue;
1356 struct pmc_process *pp;
1357 struct pmc_thread *pt;
1358 struct pmc_classdep *pcd;
1362 if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1365 KASSERT(pp->pp_proc == td->td_proc,
1366 ("[pmc,%d] not my thread state", __LINE__));
1368 critical_enter(); /* no preemption from this point */
1370 cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1372 PMCDBG5(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1373 p->p_pid, p->p_comm, pp);
1375 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1376 ("[pmc,%d] weird CPU id %d", __LINE__, cpu));
1380 for (ri = 0; ri < md->pmd_npmc; ri++) {
1382 if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1385 KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1386 ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1387 __LINE__, PMC_TO_MODE(pm)));
1389 KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1390 ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1391 __LINE__, PMC_TO_ROWINDEX(pm), ri));
1394 * Only PMCs that are marked as 'RUNNING' need
1395 * be placed on hardware.
1398 if (pm->pm_state != PMC_STATE_RUNNING)
1401 KASSERT(counter_u64_fetch(pm->pm_runcount) >= 0,
1402 ("[pmc,%d] pm=%p runcount %ld", __LINE__, (void *) pm,
1403 (unsigned long)counter_u64_fetch(pm->pm_runcount)));
1405 /* increment PMC runcount */
1406 counter_u64_add(pm->pm_runcount, 1);
1408 /* configure the HWPMC we are going to use. */
1409 pcd = pmc_ri_to_classdep(md, ri, &adjri);
1410 pcd->pcd_config_pmc(cpu, adjri, pm);
1412 phw = pc->pc_hwpmcs[ri];
1414 KASSERT(phw != NULL,
1415 ("[pmc,%d] null hw pointer", __LINE__));
1417 KASSERT(phw->phw_pmc == pm,
1418 ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1422 * Write out saved value and start the PMC.
1424 * Sampling PMCs use a per-thread value, while
1425 * counting mode PMCs use a per-pmc value that is
1426 * inherited across descendants.
1428 if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1430 pt = pmc_find_thread_descriptor(pp, td,
1434 ("[pmc,%d] No thread found for td=%p", __LINE__,
1437 mtx_pool_lock_spin(pmc_mtxpool, pm);
1440 * If we have a thread descriptor, use the per-thread
1441 * counter in the descriptor. If not, we will use
1442 * a per-process counter.
1444 * TODO: Remove the per-process "safety net" once
1445 * we have thoroughly tested that we don't hit the
1449 if (pt->pt_pmcs[ri].pt_pmcval > 0)
1450 newvalue = pt->pt_pmcs[ri].pt_pmcval;
1452 newvalue = pm->pm_sc.pm_reloadcount;
1455 * Use the saved value calculated after the most
1456 * recent time a thread using the shared counter
1457 * switched out. Reset the saved count in case
1458 * another thread from this process switches in
1459 * before any threads switch out.
1462 newvalue = pp->pp_pmcs[ri].pp_pmcval;
1463 pp->pp_pmcs[ri].pp_pmcval =
1464 pm->pm_sc.pm_reloadcount;
1466 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1467 KASSERT(newvalue > 0 && newvalue <=
1468 pm->pm_sc.pm_reloadcount,
1469 ("[pmc,%d] pmcval outside of expected range cpu=%d "
1470 "ri=%d pmcval=%jx pm_reloadcount=%jx", __LINE__,
1471 cpu, ri, newvalue, pm->pm_sc.pm_reloadcount));
1473 KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1474 ("[pmc,%d] illegal mode=%d", __LINE__,
1476 mtx_pool_lock_spin(pmc_mtxpool, pm);
1477 newvalue = PMC_PCPU_SAVED(cpu, ri) =
1478 pm->pm_gv.pm_savedvalue;
1479 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1482 PMCDBG3(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1484 pcd->pcd_write_pmc(cpu, adjri, newvalue);
1486 /* If a sampling mode PMC, reset stalled state. */
1487 if (PMC_TO_MODE(pm) == PMC_MODE_TS)
1488 pm->pm_pcpu_state[cpu].pps_stalled = 0;
1490 /* Indicate that we desire this to run. */
1491 pm->pm_pcpu_state[cpu].pps_cpustate = 1;
1493 /* Start the PMC. */
1494 pcd->pcd_start_pmc(cpu, adjri);
1498 * perform any other architecture/cpu dependent thread
1499 * switch-in actions.
1502 (void) (*md->pmd_switch_in)(pc, pp);
1509 * Thread context switch OUT.
1513 pmc_process_csw_out(struct thread *td)
1521 pmc_value_t newvalue;
1522 unsigned int adjri, ri;
1523 struct pmc_process *pp;
1524 struct pmc_thread *pt = NULL;
1525 struct pmc_classdep *pcd;
1529 * Locate our process descriptor; this may be NULL if
1530 * this process is exiting and we have already removed
1531 * the process from the target process table.
1533 * Note that due to kernel preemption, multiple
1534 * context switches may happen while the process is
1537 * Note also that if the target process cannot be
1538 * found we still need to deconfigure any PMCs that
1539 * are currently running on hardware.
1543 pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1551 cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1553 PMCDBG5(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1554 p->p_pid, p->p_comm, pp);
1556 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1557 ("[pmc,%d weird CPU id %d", __LINE__, cpu));
1562 * When a PMC gets unlinked from a target PMC, it will
1563 * be removed from the target's pp_pmc[] array.
1565 * However, on a MP system, the target could have been
1566 * executing on another CPU at the time of the unlink.
1567 * So, at context switch OUT time, we need to look at
1568 * the hardware to determine if a PMC is scheduled on
1572 for (ri = 0; ri < md->pmd_npmc; ri++) {
1574 pcd = pmc_ri_to_classdep(md, ri, &adjri);
1576 (void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
1578 if (pm == NULL) /* nothing at this row index */
1581 mode = PMC_TO_MODE(pm);
1582 if (!PMC_IS_VIRTUAL_MODE(mode))
1583 continue; /* not a process virtual PMC */
1585 KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1586 ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1587 __LINE__, PMC_TO_ROWINDEX(pm), ri));
1590 * Change desired state, and then stop if not stalled.
1591 * This two-step dance should avoid race conditions where
1592 * an interrupt re-enables the PMC after this code has
1593 * already checked the pm_stalled flag.
1595 pm->pm_pcpu_state[cpu].pps_cpustate = 0;
1596 if (pm->pm_pcpu_state[cpu].pps_stalled == 0)
1597 pcd->pcd_stop_pmc(cpu, adjri);
1599 KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
1600 ("[pmc,%d] pm=%p runcount %ld", __LINE__, (void *) pm,
1601 (unsigned long)counter_u64_fetch(pm->pm_runcount)));
1603 /* reduce this PMC's runcount */
1604 counter_u64_add(pm->pm_runcount, -1);
1607 * If this PMC is associated with this process,
1611 if (pm->pm_state != PMC_STATE_DELETED && pp != NULL &&
1612 pp->pp_pmcs[ri].pp_pmc != NULL) {
1613 KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1614 ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1615 pm, ri, pp->pp_pmcs[ri].pp_pmc));
1617 KASSERT(pp->pp_refcnt > 0,
1618 ("[pmc,%d] pp refcnt = %d", __LINE__,
1621 pcd->pcd_read_pmc(cpu, adjri, &newvalue);
1623 if (mode == PMC_MODE_TS) {
1624 PMCDBG3(CSW,SWO,1,"cpu=%d ri=%d val=%jd (samp)",
1628 pt = pmc_find_thread_descriptor(pp, td,
1632 ("[pmc,%d] No thread found for td=%p",
1635 mtx_pool_lock_spin(pmc_mtxpool, pm);
1638 * If we have a thread descriptor, save the
1639 * per-thread counter in the descriptor. If not,
1640 * we will update the per-process counter.
1642 * TODO: Remove the per-process "safety net"
1643 * once we have thoroughly tested that we
1644 * don't hit the above assert.
1647 pt->pt_pmcs[ri].pt_pmcval = newvalue;
1650 * For sampling process-virtual PMCs,
1651 * newvalue is the number of events to
1652 * be seen until the next sampling
1653 * interrupt. We can just add the events
1654 * left from this invocation to the
1655 * counter, then adjust in case we
1656 * overflow our range.
1658 * (Recall that we reload the counter
1659 * every time we use it.)
1661 pp->pp_pmcs[ri].pp_pmcval += newvalue;
1662 if (pp->pp_pmcs[ri].pp_pmcval >
1663 pm->pm_sc.pm_reloadcount)
1664 pp->pp_pmcs[ri].pp_pmcval -=
1665 pm->pm_sc.pm_reloadcount;
1667 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1669 tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1671 PMCDBG3(CSW,SWO,1,"cpu=%d ri=%d tmp=%jd (count)",
1675 * For counting process-virtual PMCs,
1676 * we expect the count to be
1677 * increasing monotonically, modulo a 64
1681 ("[pmc,%d] negative increment cpu=%d "
1682 "ri=%d newvalue=%jx saved=%jx "
1683 "incr=%jx", __LINE__, cpu, ri,
1684 newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1686 mtx_pool_lock_spin(pmc_mtxpool, pm);
1687 pm->pm_gv.pm_savedvalue += tmp;
1688 pp->pp_pmcs[ri].pp_pmcval += tmp;
1689 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1691 if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1692 pmclog_process_proccsw(pm, pp, tmp, td);
1696 /* mark hardware as free */
1697 pcd->pcd_config_pmc(cpu, adjri, NULL);
1701 * perform any other architecture/cpu dependent thread
1702 * switch out functions.
1705 (void) (*md->pmd_switch_out)(pc, pp);
1711 * A new thread for a process.
1714 pmc_process_thread_add(struct thread *td)
1716 struct pmc_process *pmc;
1718 pmc = pmc_find_process_descriptor(td->td_proc, PMC_FLAG_NONE);
1720 pmc_find_thread_descriptor(pmc, td, PMC_FLAG_ALLOCATE);
1724 * A thread delete for a process.
1727 pmc_process_thread_delete(struct thread *td)
1729 struct pmc_process *pmc;
1731 pmc = pmc_find_process_descriptor(td->td_proc, PMC_FLAG_NONE);
1733 pmc_thread_descriptor_pool_free(pmc_find_thread_descriptor(pmc,
1734 td, PMC_FLAG_REMOVE));
1738 * A userret() call for a thread.
1741 pmc_process_thread_userret(struct thread *td)
1744 pmc_capture_user_callchain(curcpu, PMC_UR, td->td_frame);
1749 * A mapping change for a process.
1753 pmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
1757 char *fullpath, *freepath;
1758 const struct pmc *pm;
1759 struct pmc_owner *po;
1760 const struct pmc_process *pp;
1762 freepath = fullpath = NULL;
1763 MPASS(!in_epoch(global_epoch_preempt));
1764 pmc_getfilename((struct vnode *) pkm->pm_file, &fullpath, &freepath);
1766 pid = td->td_proc->p_pid;
1769 /* Inform owners of all system-wide sampling PMCs. */
1770 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1771 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1772 pmclog_process_map_in(po, pid, pkm->pm_address, fullpath);
1774 if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1778 * Inform sampling PMC owners tracking this process.
1780 for (ri = 0; ri < md->pmd_npmc; ri++)
1781 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1782 PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1783 pmclog_process_map_in(pm->pm_owner,
1784 pid, pkm->pm_address, fullpath);
1788 free(freepath, M_TEMP);
1794 * Log an munmap request.
1798 pmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm)
1802 struct pmc_owner *po;
1803 const struct pmc *pm;
1804 const struct pmc_process *pp;
1806 pid = td->td_proc->p_pid;
1809 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1810 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1811 pmclog_process_map_out(po, pid, pkm->pm_address,
1812 pkm->pm_address + pkm->pm_size);
1815 if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1818 for (ri = 0; ri < md->pmd_npmc; ri++)
1819 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1820 PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1821 pmclog_process_map_out(pm->pm_owner, pid,
1822 pkm->pm_address, pkm->pm_address + pkm->pm_size);
1826 * Log mapping information about the kernel.
1830 pmc_log_kernel_mappings(struct pmc *pm)
1832 struct pmc_owner *po;
1833 struct pmckern_map_in *km, *kmbase;
1835 MPASS(in_epoch(global_epoch_preempt) || sx_xlocked(&pmc_sx));
1836 KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
1837 ("[pmc,%d] non-sampling PMC (%p) desires mapping information",
1838 __LINE__, (void *) pm));
1842 if (po->po_flags & PMC_PO_INITIAL_MAPPINGS_DONE)
1844 if (PMC_TO_MODE(pm) == PMC_MODE_SS)
1845 pmc_process_allproc(pm);
1847 * Log the current set of kernel modules.
1849 kmbase = linker_hwpmc_list_objects();
1850 for (km = kmbase; km->pm_file != NULL; km++) {
1851 PMCDBG2(LOG,REG,1,"%s %p", (char *) km->pm_file,
1852 (void *) km->pm_address);
1853 pmclog_process_map_in(po, (pid_t) -1, km->pm_address,
1856 free(kmbase, M_LINKER);
1858 po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE;
1862 * Log the mappings for a single process.
1866 pmc_log_process_mappings(struct pmc_owner *po, struct proc *p)
1871 vm_map_entry_t entry;
1872 vm_offset_t last_end;
1873 u_int last_timestamp;
1874 struct vnode *last_vp;
1875 vm_offset_t start_addr;
1876 vm_object_t obj, lobj, tobj;
1877 char *fullpath, *freepath;
1880 last_end = (vm_offset_t) 0;
1881 fullpath = freepath = NULL;
1883 if ((vm = vmspace_acquire_ref(p)) == NULL)
1887 vm_map_lock_read(map);
1889 VM_MAP_ENTRY_FOREACH(entry, map) {
1891 if (entry == NULL) {
1892 PMCDBG2(LOG,OPS,2, "hwpmc: vm_map entry unexpectedly "
1893 "NULL! pid=%d vm_map=%p\n", p->p_pid, map);
1898 * We only care about executable map entries.
1900 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
1901 !(entry->protection & VM_PROT_EXECUTE) ||
1902 (entry->object.vm_object == NULL)) {
1906 obj = entry->object.vm_object;
1907 VM_OBJECT_RLOCK(obj);
1910 * Walk the backing_object list to find the base
1911 * (non-shadowed) vm_object.
1913 for (lobj = tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
1915 VM_OBJECT_RLOCK(tobj);
1917 VM_OBJECT_RUNLOCK(lobj);
1922 * At this point lobj is the base vm_object and it is locked.
1925 PMCDBG3(LOG,OPS,2, "hwpmc: lobj unexpectedly NULL! pid=%d "
1926 "vm_map=%p vm_obj=%p\n", p->p_pid, map, obj);
1927 VM_OBJECT_RUNLOCK(obj);
1931 vp = vm_object_vnode(lobj);
1934 VM_OBJECT_RUNLOCK(lobj);
1935 VM_OBJECT_RUNLOCK(obj);
1940 * Skip contiguous regions that point to the same
1941 * vnode, so we don't emit redundant MAP-IN
1944 if (entry->start == last_end && vp == last_vp) {
1945 last_end = entry->end;
1947 VM_OBJECT_RUNLOCK(lobj);
1948 VM_OBJECT_RUNLOCK(obj);
1953 * We don't want to keep the proc's vm_map or this
1954 * vm_object locked while we walk the pathname, since
1955 * vn_fullpath() can sleep. However, if we drop the
1956 * lock, it's possible for concurrent activity to
1957 * modify the vm_map list. To protect against this,
1958 * we save the vm_map timestamp before we release the
1959 * lock, and check it after we reacquire the lock
1962 start_addr = entry->start;
1963 last_end = entry->end;
1964 last_timestamp = map->timestamp;
1965 vm_map_unlock_read(map);
1969 VM_OBJECT_RUNLOCK(lobj);
1971 VM_OBJECT_RUNLOCK(obj);
1974 pmc_getfilename(vp, &fullpath, &freepath);
1980 pmclog_process_map_in(po, p->p_pid, start_addr, fullpath);
1982 free(freepath, M_TEMP);
1984 vm_map_lock_read(map);
1987 * If our saved timestamp doesn't match, this means
1988 * that the vm_map was modified out from under us and
1989 * we can't trust our current "entry" pointer. Do a
1990 * new lookup for this entry. If there is no entry
1991 * for this address range, vm_map_lookup_entry() will
1992 * return the previous one, so we always want to go to
1993 * the next entry on the next loop iteration.
1995 * There is an edge condition here that can occur if
1996 * there is no entry at or before this address. In
1997 * this situation, vm_map_lookup_entry returns
1998 * &map->header, which would cause our loop to abort
1999 * without processing the rest of the map. However,
2000 * in practice this will never happen for process
2001 * vm_map. This is because the executable's text
2002 * segment is the first mapping in the proc's address
2003 * space, and this mapping is never removed until the
2004 * process exits, so there will always be a non-header
2005 * entry at or before the requested address for
2006 * vm_map_lookup_entry to return.
2008 if (map->timestamp != last_timestamp)
2009 vm_map_lookup_entry(map, last_end - 1, &entry);
2012 vm_map_unlock_read(map);
2018 * Log mappings for all processes in the system.
2022 pmc_log_all_process_mappings(struct pmc_owner *po)
2024 struct proc *p, *top;
2026 sx_assert(&pmc_sx, SX_XLOCKED);
2028 if ((p = pfind(1)) == NULL)
2029 panic("[pmc,%d] Cannot find init", __LINE__);
2033 sx_slock(&proctree_lock);
2038 pmc_log_process_mappings(po, p);
2039 if (!LIST_EMPTY(&p->p_children))
2040 p = LIST_FIRST(&p->p_children);
2044 if (LIST_NEXT(p, p_sibling)) {
2045 p = LIST_NEXT(p, p_sibling);
2052 sx_sunlock(&proctree_lock);
2056 * The 'hook' invoked from the kernel proper
2061 const char *pmc_hooknames[] = {
2062 /* these strings correspond to PMC_FN_* in <sys/pmckern.h> */
2085 pmc_hook_handler(struct thread *td, int function, void *arg)
2089 PMCDBG4(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
2090 pmc_hooknames[function], arg);
2099 case PMC_FN_PROCESS_EXEC:
2101 char *fullpath, *freepath;
2103 int is_using_hwpmcs;
2106 struct pmc_owner *po;
2107 struct pmc_process *pp;
2108 struct pmckern_procexec *pk;
2110 sx_assert(&pmc_sx, SX_XLOCKED);
2113 pmc_getfilename(p->p_textvp, &fullpath, &freepath);
2115 pk = (struct pmckern_procexec *) arg;
2118 /* Inform owners of SS mode PMCs of the exec event. */
2119 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
2120 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
2121 pmclog_process_procexec(po, PMC_ID_INVALID,
2122 p->p_pid, pk->pm_entryaddr, fullpath);
2126 is_using_hwpmcs = p->p_flag & P_HWPMC;
2129 if (!is_using_hwpmcs) {
2131 free(freepath, M_TEMP);
2136 * PMCs are not inherited across an exec(): remove any
2137 * PMCs that this process is the owner of.
2140 if ((po = pmc_find_owner_descriptor(p)) != NULL) {
2141 pmc_remove_owner(po);
2142 pmc_destroy_owner_descriptor(po);
2146 * If the process being exec'ed is not the target of any
2149 if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
2151 free(freepath, M_TEMP);
2156 * Log the exec event to all monitoring owners. Skip
2157 * owners who have already received the event because
2158 * they had system sampling PMCs active.
2160 for (ri = 0; ri < md->pmd_npmc; ri++)
2161 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
2163 if (po->po_sscount == 0 &&
2164 po->po_flags & PMC_PO_OWNS_LOGFILE)
2165 pmclog_process_procexec(po, pm->pm_id,
2166 p->p_pid, pk->pm_entryaddr,
2171 free(freepath, M_TEMP);
2174 PMCDBG4(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
2175 p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
2177 if (pk->pm_credentialschanged == 0) /* no change */
2181 * If the newly exec()'ed process has a different credential
2182 * than before, allow it to be the target of a PMC only if
2183 * the PMC's owner has sufficient privilege.
2186 for (ri = 0; ri < md->pmd_npmc; ri++)
2187 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
2188 if (pmc_can_attach(pm, td->td_proc) != 0)
2189 pmc_detach_one_process(td->td_proc,
2192 KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
2193 ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
2194 pp->pp_refcnt, pp));
2197 * If this process is no longer the target of any
2198 * PMCs, we can remove the process entry and free
2202 if (pp->pp_refcnt == 0) {
2203 pmc_remove_process_descriptor(pp);
2204 pmc_destroy_process_descriptor(pp);
2212 pmc_process_csw_in(td);
2215 case PMC_FN_CSW_OUT:
2216 pmc_process_csw_out(td);
2220 * Process accumulated PC samples.
2222 * This function is expected to be called by hardclock() for
2223 * each CPU that has accumulated PC samples.
2225 * This function is to be executed on the CPU whose samples
2226 * are being processed.
2228 case PMC_FN_DO_SAMPLES:
2231 * Clear the cpu specific bit in the CPU mask before
2232 * do the rest of the processing. If the NMI handler
2233 * gets invoked after the "atomic_clear_int()" call
2234 * below but before "pmc_process_samples()" gets
2235 * around to processing the interrupt, then we will
2236 * come back here at the next hardclock() tick (and
2237 * may find nothing to do if "pmc_process_samples()"
2238 * had already processed the interrupt). We don't
2239 * lose the interrupt sample.
2241 DPCPU_SET(pmc_sampled, 0);
2242 cpu = PCPU_GET(cpuid);
2243 pmc_process_samples(cpu, PMC_HR);
2244 pmc_process_samples(cpu, PMC_SR);
2245 pmc_process_samples(cpu, PMC_UR);
2249 pmc_process_mmap(td, (struct pmckern_map_in *) arg);
2253 MPASS(in_epoch(global_epoch_preempt) || sx_xlocked(&pmc_sx));
2254 pmc_process_munmap(td, (struct pmckern_map_out *) arg);
2257 case PMC_FN_PROC_CREATE_LOG:
2258 pmc_process_proccreate((struct proc *)arg);
2261 case PMC_FN_USER_CALLCHAIN:
2263 * Record a call chain.
2265 KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2268 pmc_capture_user_callchain(PCPU_GET(cpuid), PMC_HR,
2269 (struct trapframe *) arg);
2271 KASSERT(td->td_pinned == 1,
2272 ("[pmc,%d] invalid td_pinned value", __LINE__));
2273 sched_unpin(); /* Can migrate safely now. */
2275 td->td_pflags &= ~TDP_CALLCHAIN;
2278 case PMC_FN_USER_CALLCHAIN_SOFT:
2280 * Record a call chain.
2282 KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2285 cpu = PCPU_GET(cpuid);
2286 pmc_capture_user_callchain(cpu, PMC_SR,
2287 (struct trapframe *) arg);
2289 KASSERT(td->td_pinned == 1,
2290 ("[pmc,%d] invalid td_pinned value", __LINE__));
2292 sched_unpin(); /* Can migrate safely now. */
2294 td->td_pflags &= ~TDP_CALLCHAIN;
2297 case PMC_FN_SOFT_SAMPLING:
2299 * Call soft PMC sampling intr.
2301 pmc_soft_intr((struct pmckern_soft *) arg);
2304 case PMC_FN_THR_CREATE:
2305 pmc_process_thread_add(td);
2306 pmc_process_threadcreate(td);
2309 case PMC_FN_THR_CREATE_LOG:
2310 pmc_process_threadcreate(td);
2313 case PMC_FN_THR_EXIT:
2314 KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2316 pmc_process_thread_delete(td);
2317 pmc_process_threadexit(td);
2319 case PMC_FN_THR_EXIT_LOG:
2320 pmc_process_threadexit(td);
2322 case PMC_FN_THR_USERRET:
2323 KASSERT(td == curthread, ("[pmc,%d] td != curthread",
2325 pmc_process_thread_userret(td);
2330 KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
2340 * allocate a 'struct pmc_owner' descriptor in the owner hash table.
2343 static struct pmc_owner *
2344 pmc_allocate_owner_descriptor(struct proc *p)
2347 struct pmc_owner *po;
2348 struct pmc_ownerhash *poh;
2350 hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2351 poh = &pmc_ownerhash[hindex];
2353 /* allocate space for N pointers and one descriptor struct */
2354 po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO);
2356 LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
2358 TAILQ_INIT(&po->po_logbuffers);
2359 mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc-per-proc", MTX_SPIN);
2361 PMCDBG4(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
2362 p, p->p_pid, p->p_comm, po);
2368 pmc_destroy_owner_descriptor(struct pmc_owner *po)
2371 PMCDBG4(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
2372 po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
2374 mtx_destroy(&po->po_mtx);
2379 * Allocate a thread descriptor from the free pool.
2381 * NOTE: This *can* return NULL.
2383 static struct pmc_thread *
2384 pmc_thread_descriptor_pool_alloc(void)
2386 struct pmc_thread *pt;
2388 mtx_lock_spin(&pmc_threadfreelist_mtx);
2389 if ((pt = LIST_FIRST(&pmc_threadfreelist)) != NULL) {
2390 LIST_REMOVE(pt, pt_next);
2391 pmc_threadfreelist_entries--;
2393 mtx_unlock_spin(&pmc_threadfreelist_mtx);
2399 * Add a thread descriptor to the free pool. We use this instead of free()
2400 * to maintain a cache of free entries. Additionally, we can safely call
2401 * this function when we cannot call free(), such as in a critical section.
2405 pmc_thread_descriptor_pool_free(struct pmc_thread *pt)
2411 memset(pt, 0, THREADENTRY_SIZE);
2412 mtx_lock_spin(&pmc_threadfreelist_mtx);
2413 LIST_INSERT_HEAD(&pmc_threadfreelist, pt, pt_next);
2414 pmc_threadfreelist_entries++;
2415 if (pmc_threadfreelist_entries > pmc_threadfreelist_max)
2416 taskqueue_enqueue(taskqueue_fast, &free_task);
2417 mtx_unlock_spin(&pmc_threadfreelist_mtx);
2421 * An asynchronous task to manage the free list.
2424 pmc_thread_descriptor_pool_free_task(void *arg __unused, int pending __unused)
2426 struct pmc_thread *pt;
2427 LIST_HEAD(, pmc_thread) tmplist;
2430 LIST_INIT(&tmplist);
2432 /* Determine what changes, if any, we need to make. */
2433 mtx_lock_spin(&pmc_threadfreelist_mtx);
2434 delta = pmc_threadfreelist_entries - pmc_threadfreelist_max;
2435 while (delta > 0 && (pt = LIST_FIRST(&pmc_threadfreelist)) != NULL) {
2437 pmc_threadfreelist_entries--;
2438 LIST_REMOVE(pt, pt_next);
2439 LIST_INSERT_HEAD(&tmplist, pt, pt_next);
2441 mtx_unlock_spin(&pmc_threadfreelist_mtx);
2443 /* If there are entries to free, free them. */
2444 while (!LIST_EMPTY(&tmplist)) {
2445 pt = LIST_FIRST(&tmplist);
2446 LIST_REMOVE(pt, pt_next);
2452 * Drain the thread free pool, freeing all allocations.
2455 pmc_thread_descriptor_pool_drain()
2457 struct pmc_thread *pt, *next;
2459 LIST_FOREACH_SAFE(pt, &pmc_threadfreelist, pt_next, next) {
2460 LIST_REMOVE(pt, pt_next);
2466 * find the descriptor corresponding to thread 'td', adding or removing it
2467 * as specified by 'mode'.
2469 * Note that this supports additional mode flags in addition to those
2470 * supported by pmc_find_process_descriptor():
2471 * PMC_FLAG_NOWAIT: Causes the function to not wait for mallocs.
2472 * This makes it safe to call while holding certain other locks.
2475 static struct pmc_thread *
2476 pmc_find_thread_descriptor(struct pmc_process *pp, struct thread *td,
2479 struct pmc_thread *pt = NULL, *ptnew = NULL;
2482 KASSERT(td != NULL, ("[pmc,%d] called to add NULL td", __LINE__));
2485 * Pre-allocate memory in the PMC_FLAG_ALLOCATE case prior to
2486 * acquiring the lock.
2488 if (mode & PMC_FLAG_ALLOCATE) {
2489 if ((ptnew = pmc_thread_descriptor_pool_alloc()) == NULL) {
2490 wait_flag = M_WAITOK;
2491 if ((mode & PMC_FLAG_NOWAIT) || in_epoch(global_epoch_preempt))
2492 wait_flag = M_NOWAIT;
2494 ptnew = malloc(THREADENTRY_SIZE, M_PMC,
2499 mtx_lock_spin(pp->pp_tdslock);
2501 LIST_FOREACH(pt, &pp->pp_tds, pt_next)
2502 if (pt->pt_td == td)
2505 if ((mode & PMC_FLAG_REMOVE) && pt != NULL)
2506 LIST_REMOVE(pt, pt_next);
2508 if ((mode & PMC_FLAG_ALLOCATE) && pt == NULL && ptnew != NULL) {
2512 LIST_INSERT_HEAD(&pp->pp_tds, pt, pt_next);
2515 mtx_unlock_spin(pp->pp_tdslock);
2517 if (ptnew != NULL) {
2525 * Try to add thread descriptors for each thread in a process.
2529 pmc_add_thread_descriptors_from_proc(struct proc *p, struct pmc_process *pp)
2531 struct thread *curtd;
2532 struct pmc_thread **tdlist;
2533 int i, tdcnt, tdlistsz;
2535 KASSERT(!PROC_LOCKED(p), ("[pmc,%d] proc unexpectedly locked",
2539 tdlistsz = roundup2(tdcnt, 32);
2542 tdlist = malloc(sizeof(struct pmc_thread*) * tdlistsz, M_TEMP, M_WAITOK);
2545 FOREACH_THREAD_IN_PROC(p, curtd)
2547 if (tdcnt >= tdlistsz) {
2549 free(tdlist, M_TEMP);
2553 * Try to add each thread to the list without sleeping. If unable,
2554 * add to a queue to retry after dropping the process lock.
2557 FOREACH_THREAD_IN_PROC(p, curtd) {
2558 tdlist[tdcnt] = pmc_find_thread_descriptor(pp, curtd,
2559 PMC_FLAG_ALLOCATE|PMC_FLAG_NOWAIT);
2560 if (tdlist[tdcnt] == NULL) {
2562 for (i = 0; i <= tdcnt; i++)
2563 pmc_thread_descriptor_pool_free(tdlist[i]);
2564 free(tdlist, M_TEMP);
2570 free(tdlist, M_TEMP);
2574 * find the descriptor corresponding to process 'p', adding or removing it
2575 * as specified by 'mode'.
2578 static struct pmc_process *
2579 pmc_find_process_descriptor(struct proc *p, uint32_t mode)
2582 struct pmc_process *pp, *ppnew;
2583 struct pmc_processhash *pph;
2585 hindex = PMC_HASH_PTR(p, pmc_processhashmask);
2586 pph = &pmc_processhash[hindex];
2591 * Pre-allocate memory in the PMC_FLAG_ALLOCATE case since we
2592 * cannot call malloc(9) once we hold a spin lock.
2594 if (mode & PMC_FLAG_ALLOCATE)
2595 ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc *
2596 sizeof(struct pmc_targetstate), M_PMC, M_WAITOK|M_ZERO);
2598 mtx_lock_spin(&pmc_processhash_mtx);
2599 LIST_FOREACH(pp, pph, pp_next)
2600 if (pp->pp_proc == p)
2603 if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
2604 LIST_REMOVE(pp, pp_next);
2606 if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
2609 LIST_INIT(&ppnew->pp_tds);
2610 ppnew->pp_tdslock = mtx_pool_find(pmc_mtxpool, ppnew);
2611 LIST_INSERT_HEAD(pph, ppnew, pp_next);
2612 mtx_unlock_spin(&pmc_processhash_mtx);
2616 /* Add thread descriptors for this process' current threads. */
2617 pmc_add_thread_descriptors_from_proc(p, pp);
2620 mtx_unlock_spin(&pmc_processhash_mtx);
2629 * remove a process descriptor from the process hash table.
2633 pmc_remove_process_descriptor(struct pmc_process *pp)
2635 KASSERT(pp->pp_refcnt == 0,
2636 ("[pmc,%d] Removing process descriptor %p with count %d",
2637 __LINE__, pp, pp->pp_refcnt));
2639 mtx_lock_spin(&pmc_processhash_mtx);
2640 LIST_REMOVE(pp, pp_next);
2641 mtx_unlock_spin(&pmc_processhash_mtx);
2645 * destroy a process descriptor.
2649 pmc_destroy_process_descriptor(struct pmc_process *pp)
2651 struct pmc_thread *pmc_td;
2653 while ((pmc_td = LIST_FIRST(&pp->pp_tds)) != NULL) {
2654 LIST_REMOVE(pmc_td, pt_next);
2655 pmc_thread_descriptor_pool_free(pmc_td);
2662 * find an owner descriptor corresponding to proc 'p'
2665 static struct pmc_owner *
2666 pmc_find_owner_descriptor(struct proc *p)
2669 struct pmc_owner *po;
2670 struct pmc_ownerhash *poh;
2672 hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2673 poh = &pmc_ownerhash[hindex];
2676 LIST_FOREACH(po, poh, po_next)
2677 if (po->po_owner == p)
2680 PMCDBG5(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
2681 "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
2687 * pmc_allocate_pmc_descriptor
2689 * Allocate a pmc descriptor and initialize its
2694 pmc_allocate_pmc_descriptor(void)
2698 pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO);
2699 pmc->pm_runcount = counter_u64_alloc(M_WAITOK);
2700 pmc->pm_pcpu_state = malloc(sizeof(struct pmc_pcpu_state)*mp_ncpus, M_PMC, M_WAITOK|M_ZERO);
2701 PMCDBG1(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
2707 * Destroy a pmc descriptor.
2711 pmc_destroy_pmc_descriptor(struct pmc *pm)
2714 KASSERT(pm->pm_state == PMC_STATE_DELETED ||
2715 pm->pm_state == PMC_STATE_FREE,
2716 ("[pmc,%d] destroying non-deleted PMC", __LINE__));
2717 KASSERT(LIST_EMPTY(&pm->pm_targets),
2718 ("[pmc,%d] destroying pmc with targets", __LINE__));
2719 KASSERT(pm->pm_owner == NULL,
2720 ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
2721 KASSERT(counter_u64_fetch(pm->pm_runcount) == 0,
2722 ("[pmc,%d] pmc has non-zero run count %ld", __LINE__,
2723 (unsigned long)counter_u64_fetch(pm->pm_runcount)));
2725 counter_u64_free(pm->pm_runcount);
2726 free(pm->pm_pcpu_state, M_PMC);
2731 pmc_wait_for_pmc_idle(struct pmc *pm)
2734 volatile int maxloop;
2736 maxloop = 100 * pmc_cpu_max();
2739 * Loop (with a forced context switch) till the PMC's runcount
2740 * comes down to zero.
2742 pmclog_flush(pm->pm_owner, 1);
2743 while (counter_u64_fetch(pm->pm_runcount) > 0) {
2744 pmclog_flush(pm->pm_owner, 1);
2747 KASSERT(maxloop > 0,
2748 ("[pmc,%d] (ri%d, rc%ld) waiting too long for "
2749 "pmc to be free", __LINE__,
2750 PMC_TO_ROWINDEX(pm), (unsigned long)counter_u64_fetch(pm->pm_runcount)));
2752 pmc_force_context_switch();
2757 * This function does the following things:
2759 * - detaches the PMC from hardware
2760 * - unlinks all target threads that were attached to it
2761 * - removes the PMC from its owner's list
2762 * - destroys the PMC private mutex
2764 * Once this function completes, the given pmc pointer can be freed by
2765 * calling pmc_destroy_pmc_descriptor().
2769 pmc_release_pmc_descriptor(struct pmc *pm)
2773 u_int adjri, ri, cpu;
2774 struct pmc_owner *po;
2775 struct pmc_binding pb;
2776 struct pmc_process *pp;
2777 struct pmc_classdep *pcd;
2778 struct pmc_target *ptgt, *tmp;
2780 sx_assert(&pmc_sx, SX_XLOCKED);
2782 KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
2784 ri = PMC_TO_ROWINDEX(pm);
2785 pcd = pmc_ri_to_classdep(md, ri, &adjri);
2786 mode = PMC_TO_MODE(pm);
2788 PMCDBG3(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
2792 * First, we take the PMC off hardware.
2795 if (PMC_IS_SYSTEM_MODE(mode)) {
2798 * A system mode PMC runs on a specific CPU. Switch
2799 * to this CPU and turn hardware off.
2801 pmc_save_cpu_binding(&pb);
2803 cpu = PMC_TO_CPU(pm);
2805 pmc_select_cpu(cpu);
2807 /* switch off non-stalled CPUs */
2808 pm->pm_pcpu_state[cpu].pps_cpustate = 0;
2809 if (pm->pm_state == PMC_STATE_RUNNING &&
2810 pm->pm_pcpu_state[cpu].pps_stalled == 0) {
2812 phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
2814 KASSERT(phw->phw_pmc == pm,
2815 ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
2816 __LINE__, ri, phw->phw_pmc, pm));
2817 PMCDBG2(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
2820 pcd->pcd_stop_pmc(cpu, adjri);
2824 PMCDBG2(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
2827 pcd->pcd_config_pmc(cpu, adjri, NULL);
2830 /* adjust the global and process count of SS mode PMCs */
2831 if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
2834 if (po->po_sscount == 0) {
2835 atomic_subtract_rel_int(&pmc_ss_count, 1);
2836 CK_LIST_REMOVE(po, po_ssnext);
2837 epoch_wait_preempt(global_epoch_preempt);
2841 pm->pm_state = PMC_STATE_DELETED;
2843 pmc_restore_cpu_binding(&pb);
2846 * We could have references to this PMC structure in
2847 * the per-cpu sample queues. Wait for the queue to
2850 pmc_wait_for_pmc_idle(pm);
2852 } else if (PMC_IS_VIRTUAL_MODE(mode)) {
2855 * A virtual PMC could be running on multiple CPUs at
2858 * By marking its state as DELETED, we ensure that
2859 * this PMC is never further scheduled on hardware.
2861 * Then we wait till all CPUs are done with this PMC.
2863 pm->pm_state = PMC_STATE_DELETED;
2866 /* Wait for the PMCs runcount to come to zero. */
2867 pmc_wait_for_pmc_idle(pm);
2870 * At this point the PMC is off all CPUs and cannot be
2871 * freshly scheduled onto a CPU. It is now safe to
2872 * unlink all targets from this PMC. If a
2873 * process-record's refcount falls to zero, we remove
2874 * it from the hash table. The module-wide SX lock
2875 * protects us from races.
2877 LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
2878 pp = ptgt->pt_process;
2879 pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
2881 PMCDBG1(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
2884 * If the target process record shows that no
2885 * PMCs are attached to it, reclaim its space.
2888 if (pp->pp_refcnt == 0) {
2889 pmc_remove_process_descriptor(pp);
2890 pmc_destroy_process_descriptor(pp);
2894 cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
2899 * Release any MD resources
2901 (void) pcd->pcd_release_pmc(cpu, adjri, pm);
2904 * Update row disposition
2907 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
2908 PMC_UNMARK_ROW_STANDALONE(ri);
2910 PMC_UNMARK_ROW_THREAD(ri);
2912 /* unlink from the owner's list */
2914 LIST_REMOVE(pm, pm_next);
2915 pm->pm_owner = NULL;
2920 * Register an owner and a pmc.
2924 pmc_register_owner(struct proc *p, struct pmc *pmc)
2926 struct pmc_owner *po;
2928 sx_assert(&pmc_sx, SX_XLOCKED);
2930 if ((po = pmc_find_owner_descriptor(p)) == NULL)
2931 if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
2934 KASSERT(pmc->pm_owner == NULL,
2935 ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
2938 LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
2941 p->p_flag |= P_HWPMC;
2944 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
2945 pmclog_process_pmcallocate(pmc);
2947 PMCDBG2(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2954 * Return the current row disposition:
2956 * > 0 => PROCESS MODE
2957 * < 0 => SYSTEM MODE
2961 pmc_getrowdisp(int ri)
2963 return pmc_pmcdisp[ri];
2967 * Check if a PMC at row index 'ri' can be allocated to the current
2970 * Allocation can fail if:
2971 * - the current process is already being profiled by a PMC at index 'ri',
2972 * attached to it via OP_PMCATTACH.
2973 * - the current process has already allocated a PMC at index 'ri'
2978 pmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2982 struct pmc_owner *po;
2983 struct pmc_process *pp;
2985 PMCDBG5(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2986 "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2989 * We shouldn't have already allocated a process-mode PMC at
2992 * We shouldn't have allocated a system-wide PMC on the same
2995 if ((po = pmc_find_owner_descriptor(p)) != NULL)
2996 LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2997 if (PMC_TO_ROWINDEX(pm) == ri) {
2998 mode = PMC_TO_MODE(pm);
2999 if (PMC_IS_VIRTUAL_MODE(mode))
3001 if (PMC_IS_SYSTEM_MODE(mode) &&
3002 (int) PMC_TO_CPU(pm) == cpu)
3008 * We also shouldn't be the target of any PMC at this index
3009 * since otherwise a PMC_ATTACH to ourselves will fail.
3011 if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
3012 if (pp->pp_pmcs[ri].pp_pmc)
3015 PMCDBG4(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
3016 p, p->p_pid, p->p_comm, ri);
3022 * Check if a given PMC at row index 'ri' can be currently used in
3027 pmc_can_allocate_row(int ri, enum pmc_mode mode)
3031 sx_assert(&pmc_sx, SX_XLOCKED);
3033 PMCDBG2(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
3035 if (PMC_IS_SYSTEM_MODE(mode))
3036 disp = PMC_DISP_STANDALONE;
3038 disp = PMC_DISP_THREAD;
3041 * check disposition for PMC row 'ri':
3043 * Expected disposition Row-disposition Result
3045 * STANDALONE STANDALONE or FREE proceed
3046 * STANDALONE THREAD fail
3047 * THREAD THREAD or FREE proceed
3048 * THREAD STANDALONE fail
3051 if (!PMC_ROW_DISP_IS_FREE(ri) &&
3052 !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
3053 !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
3060 PMCDBG2(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
3067 * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
3071 pmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
3075 KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
3076 ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
3077 PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
3079 LIST_FOREACH(pm, &po->po_pmcs, pm_next)
3080 if (pm->pm_id == pmcid)
3087 pmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
3090 struct pmc *pm, *opm;
3091 struct pmc_owner *po;
3092 struct pmc_process *pp;
3094 PMCDBG1(PMC,FND,1, "find-pmc id=%d", pmcid);
3095 if (PMC_ID_TO_ROWINDEX(pmcid) >= md->pmd_npmc)
3098 if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL) {
3100 * In case of PMC_F_DESCENDANTS child processes we will not find
3101 * the current process in the owners hash list. Find the owner
3102 * process first and from there lookup the po.
3104 if ((pp = pmc_find_process_descriptor(curthread->td_proc,
3105 PMC_FLAG_NONE)) == NULL) {
3108 opm = pp->pp_pmcs[PMC_ID_TO_ROWINDEX(pmcid)].pp_pmc;
3111 if ((opm->pm_flags & (PMC_F_ATTACHED_TO_OWNER|
3112 PMC_F_DESCENDANTS)) != (PMC_F_ATTACHED_TO_OWNER|
3119 if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
3122 PMCDBG2(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
3133 pmc_start(struct pmc *pm)
3136 struct pmc_owner *po;
3137 struct pmc_binding pb;
3138 struct pmc_classdep *pcd;
3139 int adjri, error, cpu, ri;
3142 ("[pmc,%d] null pm", __LINE__));
3144 mode = PMC_TO_MODE(pm);
3145 ri = PMC_TO_ROWINDEX(pm);
3146 pcd = pmc_ri_to_classdep(md, ri, &adjri);
3150 PMCDBG3(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
3155 * Disallow PMCSTART if a logfile is required but has not been
3158 if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
3159 (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
3160 return (EDOOFUS); /* programming error */
3163 * If this is a sampling mode PMC, log mapping information for
3164 * the kernel modules that are currently loaded.
3166 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3167 pmc_log_kernel_mappings(pm);
3169 if (PMC_IS_VIRTUAL_MODE(mode)) {
3172 * If a PMCATTACH has never been done on this PMC,
3173 * attach it to its owner process.
3176 if (LIST_EMPTY(&pm->pm_targets))
3177 error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
3178 pmc_attach_process(po->po_owner, pm);
3181 * If the PMC is attached to its owner, then force a context
3182 * switch to ensure that the MD state gets set correctly.
3186 pm->pm_state = PMC_STATE_RUNNING;
3187 if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
3188 pmc_force_context_switch();
3196 * A system-wide PMC.
3198 * Add the owner to the global list if this is a system-wide
3202 if (mode == PMC_MODE_SS) {
3204 * Log mapping information for all existing processes in the
3205 * system. Subsequent mappings are logged as they happen;
3206 * see pmc_process_mmap().
3208 if (po->po_logprocmaps == 0) {
3209 pmc_log_all_process_mappings(po);
3210 po->po_logprocmaps = 1;
3213 if (po->po_sscount == 1) {
3214 atomic_add_rel_int(&pmc_ss_count, 1);
3215 CK_LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
3216 PMCDBG1(PMC,OPS,1, "po=%p in global list", po);
3221 * Move to the CPU associated with this
3222 * PMC, and start the hardware.
3225 pmc_save_cpu_binding(&pb);
3227 cpu = PMC_TO_CPU(pm);
3229 if (!pmc_cpu_is_active(cpu))
3232 pmc_select_cpu(cpu);
3235 * global PMCs are configured at allocation time
3236 * so write out the initial value and start the PMC.
3239 pm->pm_state = PMC_STATE_RUNNING;
3242 if ((error = pcd->pcd_write_pmc(cpu, adjri,
3243 PMC_IS_SAMPLING_MODE(mode) ?
3244 pm->pm_sc.pm_reloadcount :
3245 pm->pm_sc.pm_initial)) == 0) {
3246 /* If a sampling mode PMC, reset stalled state. */
3247 if (PMC_IS_SAMPLING_MODE(mode))
3248 pm->pm_pcpu_state[cpu].pps_stalled = 0;
3250 /* Indicate that we desire this to run. Start it. */
3251 pm->pm_pcpu_state[cpu].pps_cpustate = 1;
3252 error = pcd->pcd_start_pmc(cpu, adjri);
3256 pmc_restore_cpu_binding(&pb);
3266 pmc_stop(struct pmc *pm)
3268 struct pmc_owner *po;
3269 struct pmc_binding pb;
3270 struct pmc_classdep *pcd;
3271 int adjri, cpu, error, ri;
3273 KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
3275 PMCDBG3(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
3276 PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
3278 pm->pm_state = PMC_STATE_STOPPED;
3281 * If the PMC is a virtual mode one, changing the state to
3282 * non-RUNNING is enough to ensure that the PMC never gets
3285 * If this PMC is current running on a CPU, then it will
3286 * handled correctly at the time its target process is context
3290 if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
3294 * A system-mode PMC. Move to the CPU associated with
3295 * this PMC, and stop the hardware. We update the
3296 * 'initial count' so that a subsequent PMCSTART will
3297 * resume counting from the current hardware count.
3300 pmc_save_cpu_binding(&pb);
3302 cpu = PMC_TO_CPU(pm);
3304 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
3305 ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
3307 if (!pmc_cpu_is_active(cpu))
3310 pmc_select_cpu(cpu);
3312 ri = PMC_TO_ROWINDEX(pm);
3313 pcd = pmc_ri_to_classdep(md, ri, &adjri);
3315 pm->pm_pcpu_state[cpu].pps_cpustate = 0;
3317 if ((error = pcd->pcd_stop_pmc(cpu, adjri)) == 0)
3318 error = pcd->pcd_read_pmc(cpu, adjri, &pm->pm_sc.pm_initial);
3321 pmc_restore_cpu_binding(&pb);
3325 /* remove this owner from the global list of SS PMC owners */
3326 if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
3328 if (po->po_sscount == 0) {
3329 atomic_subtract_rel_int(&pmc_ss_count, 1);
3330 CK_LIST_REMOVE(po, po_ssnext);
3331 epoch_wait_preempt(global_epoch_preempt);
3332 PMCDBG1(PMC,OPS,2,"po=%p removed from global list", po);
3339 static struct pmc_classdep *
3340 pmc_class_to_classdep(enum pmc_class class)
3344 for (n = 0; n < md->pmd_nclass; n++)
3345 if (md->pmd_classdep[n].pcd_class == class)
3346 return (&md->pmd_classdep[n]);
3350 #if defined(HWPMC_DEBUG) && defined(KTR)
3351 static const char *pmc_op_to_name[] = {
3353 #define __PMC_OP(N, D) #N ,
3360 * The syscall interface
3363 #define PMC_GET_SX_XLOCK(...) do { \
3364 sx_xlock(&pmc_sx); \
3365 if (pmc_hook == NULL) { \
3366 sx_xunlock(&pmc_sx); \
3367 return __VA_ARGS__; \
3371 #define PMC_DOWNGRADE_SX() do { \
3372 sx_downgrade(&pmc_sx); \
3373 is_sx_downgraded = 1; \
3377 pmc_syscall_handler(struct thread *td, void *syscall_args)
3379 int error, is_sx_downgraded, op;
3380 struct pmc_syscall_args *c;
3381 void *pmclog_proc_handle;
3384 c = (struct pmc_syscall_args *)syscall_args;
3387 /* PMC isn't set up yet */
3388 if (pmc_hook == NULL)
3390 if (op == PMC_OP_CONFIGURELOG) {
3392 * We cannot create the logging process inside
3393 * pmclog_configure_log() because there is a LOR
3394 * between pmc_sx and process structure locks.
3395 * Instead, pre-create the process and ignite the loop
3396 * if everything is fine, otherwise direct the process
3399 error = pmclog_proc_create(td, &pmclog_proc_handle);
3404 PMC_GET_SX_XLOCK(ENOSYS);
3405 is_sx_downgraded = 0;
3406 PMCDBG3(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
3407 pmc_op_to_name[op], arg);
3410 counter_u64_add(pmc_stats.pm_syscalls, 1);
3416 * Configure a log file.
3418 * XXX This OP will be reworked.
3421 case PMC_OP_CONFIGURELOG:
3425 struct pmc_owner *po;
3426 struct pmc_op_configurelog cl;
3428 if ((error = copyin(arg, &cl, sizeof(cl))) != 0) {
3429 pmclog_proc_ignite(pmclog_proc_handle, NULL);
3433 /* mark this process as owning a log file */
3435 if ((po = pmc_find_owner_descriptor(p)) == NULL)
3436 if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
3437 pmclog_proc_ignite(pmclog_proc_handle, NULL);
3443 * If a valid fd was passed in, try to configure that,
3444 * otherwise if 'fd' was less than zero and there was
3445 * a log file configured, flush its buffers and
3448 if (cl.pm_logfd >= 0) {
3449 error = pmclog_configure_log(md, po, cl.pm_logfd);
3450 pmclog_proc_ignite(pmclog_proc_handle, error == 0 ?
3452 } else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
3453 pmclog_proc_ignite(pmclog_proc_handle, NULL);
3454 error = pmclog_close(po);
3456 LIST_FOREACH(pm, &po->po_pmcs, pm_next)
3457 if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
3458 pm->pm_state == PMC_STATE_RUNNING)
3460 error = pmclog_deconfigure_log(po);
3463 pmclog_proc_ignite(pmclog_proc_handle, NULL);
3473 case PMC_OP_FLUSHLOG:
3475 struct pmc_owner *po;
3477 sx_assert(&pmc_sx, SX_XLOCKED);
3479 if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3484 error = pmclog_flush(po, 0);
3492 case PMC_OP_CLOSELOG:
3494 struct pmc_owner *po;
3496 sx_assert(&pmc_sx, SX_XLOCKED);
3498 if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3503 error = pmclog_close(po);
3508 * Retrieve hardware configuration.
3511 case PMC_OP_GETCPUINFO: /* CPU information */
3513 struct pmc_op_getcpuinfo gci;
3514 struct pmc_classinfo *pci;
3515 struct pmc_classdep *pcd;
3518 memset(&gci, 0, sizeof(gci));
3519 gci.pm_cputype = md->pmd_cputype;
3520 gci.pm_ncpu = pmc_cpu_max();
3521 gci.pm_npmc = md->pmd_npmc;
3522 gci.pm_nclass = md->pmd_nclass;
3523 pci = gci.pm_classes;
3524 pcd = md->pmd_classdep;
3525 for (cl = 0; cl < md->pmd_nclass; cl++, pci++, pcd++) {
3526 pci->pm_caps = pcd->pcd_caps;
3527 pci->pm_class = pcd->pcd_class;
3528 pci->pm_width = pcd->pcd_width;
3529 pci->pm_num = pcd->pcd_num;
3531 error = copyout(&gci, arg, sizeof(gci));
3536 * Retrieve soft events list.
3538 case PMC_OP_GETDYNEVENTINFO:
3542 struct pmc_op_getdyneventinfo *gei;
3543 struct pmc_dyn_event_descr dev;
3544 struct pmc_soft *ps;
3547 sx_assert(&pmc_sx, SX_LOCKED);
3549 gei = (struct pmc_op_getdyneventinfo *) arg;
3551 if ((error = copyin(&gei->pm_class, &cl, sizeof(cl))) != 0)
3554 /* Only SOFT class is dynamic. */
3555 if (cl != PMC_CLASS_SOFT) {
3561 for (ev = PMC_EV_SOFT_FIRST; (int)ev <= PMC_EV_SOFT_LAST; ev++) {
3562 ps = pmc_soft_ev_acquire(ev);
3565 bcopy(&ps->ps_ev, &dev, sizeof(dev));
3566 pmc_soft_ev_release(ps);
3568 error = copyout(&dev,
3569 &gei->pm_events[nevent],
3570 sizeof(struct pmc_dyn_event_descr));
3578 error = copyout(&nevent, &gei->pm_nevent,
3584 * Get module statistics
3587 case PMC_OP_GETDRIVERSTATS:
3589 struct pmc_op_getdriverstats gms;
3590 #define CFETCH(a, b, field) a.field = counter_u64_fetch(b.field)
3591 CFETCH(gms, pmc_stats, pm_intr_ignored);
3592 CFETCH(gms, pmc_stats, pm_intr_processed);
3593 CFETCH(gms, pmc_stats, pm_intr_bufferfull);
3594 CFETCH(gms, pmc_stats, pm_syscalls);
3595 CFETCH(gms, pmc_stats, pm_syscall_errors);
3596 CFETCH(gms, pmc_stats, pm_buffer_requests);
3597 CFETCH(gms, pmc_stats, pm_buffer_requests_failed);
3598 CFETCH(gms, pmc_stats, pm_log_sweeps);
3600 error = copyout(&gms, arg, sizeof(gms));
3606 * Retrieve module version number
3609 case PMC_OP_GETMODULEVERSION:
3613 /* retrieve the client's idea of the ABI version */
3614 if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
3616 /* don't service clients newer than our driver */
3618 if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
3619 error = EPROGMISMATCH;
3622 error = copyout(&modv, arg, sizeof(int));
3628 * Retrieve the state of all the PMCs on a given
3632 case PMC_OP_GETPMCINFO:
3636 size_t pmcinfo_size;
3637 uint32_t cpu, n, npmc;
3638 struct pmc_owner *po;
3639 struct pmc_binding pb;
3640 struct pmc_classdep *pcd;
3641 struct pmc_info *p, *pmcinfo;
3642 struct pmc_op_getpmcinfo *gpi;
3646 gpi = (struct pmc_op_getpmcinfo *) arg;
3648 if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
3651 if (cpu >= pmc_cpu_max()) {
3656 if (!pmc_cpu_is_active(cpu)) {
3661 /* switch to CPU 'cpu' */
3662 pmc_save_cpu_binding(&pb);
3663 pmc_select_cpu(cpu);
3665 npmc = md->pmd_npmc;
3667 pmcinfo_size = npmc * sizeof(struct pmc_info);
3668 pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK | M_ZERO);
3672 for (n = 0; n < md->pmd_npmc; n++, p++) {
3674 pcd = pmc_ri_to_classdep(md, n, &ari);
3676 KASSERT(pcd != NULL,
3677 ("[pmc,%d] null pcd ri=%d", __LINE__, n));
3679 if ((error = pcd->pcd_describe(cpu, ari, p, &pm)) != 0)
3682 if (PMC_ROW_DISP_IS_STANDALONE(n))
3683 p->pm_rowdisp = PMC_DISP_STANDALONE;
3684 else if (PMC_ROW_DISP_IS_THREAD(n))
3685 p->pm_rowdisp = PMC_DISP_THREAD;
3687 p->pm_rowdisp = PMC_DISP_FREE;
3689 p->pm_ownerpid = -1;
3691 if (pm == NULL) /* no PMC associated */
3696 KASSERT(po->po_owner != NULL,
3697 ("[pmc,%d] pmc_owner had a null proc pointer",
3700 p->pm_ownerpid = po->po_owner->p_pid;
3701 p->pm_mode = PMC_TO_MODE(pm);
3702 p->pm_event = pm->pm_event;
3703 p->pm_flags = pm->pm_flags;
3705 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3707 pm->pm_sc.pm_reloadcount;
3710 pmc_restore_cpu_binding(&pb);
3712 /* now copy out the PMC info collected */
3714 error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
3716 free(pmcinfo, M_PMC);
3722 * Set the administrative state of a PMC. I.e. whether
3723 * the PMC is to be used or not.
3726 case PMC_OP_PMCADMIN:
3729 enum pmc_state request;
3732 struct pmc_op_pmcadmin pma;
3733 struct pmc_binding pb;
3735 sx_assert(&pmc_sx, SX_XLOCKED);
3737 KASSERT(td == curthread,
3738 ("[pmc,%d] td != curthread", __LINE__));
3740 error = priv_check(td, PRIV_PMC_MANAGE);
3744 if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
3749 if (cpu < 0 || cpu >= (int) pmc_cpu_max()) {
3754 if (!pmc_cpu_is_active(cpu)) {
3759 request = pma.pm_state;
3761 if (request != PMC_STATE_DISABLED &&
3762 request != PMC_STATE_FREE) {
3767 ri = pma.pm_pmc; /* pmc id == row index */
3768 if (ri < 0 || ri >= (int) md->pmd_npmc) {
3774 * We can't disable a PMC with a row-index allocated
3775 * for process virtual PMCs.
3778 if (PMC_ROW_DISP_IS_THREAD(ri) &&
3779 request == PMC_STATE_DISABLED) {
3785 * otherwise, this PMC on this CPU is either free or
3786 * in system-wide mode.
3789 pmc_save_cpu_binding(&pb);
3790 pmc_select_cpu(cpu);
3793 phw = pc->pc_hwpmcs[ri];
3796 * XXX do we need some kind of 'forced' disable?
3799 if (phw->phw_pmc == NULL) {
3800 if (request == PMC_STATE_DISABLED &&
3801 (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
3802 phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
3803 PMC_MARK_ROW_STANDALONE(ri);
3804 } else if (request == PMC_STATE_FREE &&
3805 (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
3806 phw->phw_state |= PMC_PHW_FLAG_IS_ENABLED;
3807 PMC_UNMARK_ROW_STANDALONE(ri);
3809 /* other cases are a no-op */
3813 pmc_restore_cpu_binding(&pb);
3822 case PMC_OP_PMCALLOCATE:
3830 struct pmc_binding pb;
3831 struct pmc_classdep *pcd;
3832 struct pmc_op_pmcallocate pa;
3834 if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
3841 if ((mode != PMC_MODE_SS && mode != PMC_MODE_SC &&
3842 mode != PMC_MODE_TS && mode != PMC_MODE_TC) ||
3843 (cpu != (u_int) PMC_CPU_ANY && cpu >= pmc_cpu_max())) {
3849 * Virtual PMCs should only ask for a default CPU.
3850 * System mode PMCs need to specify a non-default CPU.
3853 if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
3854 (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
3860 * Check that an inactive CPU is not being asked for.
3863 if (PMC_IS_SYSTEM_MODE(mode) && !pmc_cpu_is_active(cpu)) {
3869 * Refuse an allocation for a system-wide PMC if this
3870 * process has been jailed, or if this process lacks
3871 * super-user credentials and the sysctl tunable
3872 * 'security.bsd.unprivileged_syspmcs' is zero.
3875 if (PMC_IS_SYSTEM_MODE(mode)) {
3876 if (jailed(curthread->td_ucred)) {
3880 if (!pmc_unprivileged_syspmcs) {
3881 error = priv_check(curthread,
3889 * Look for valid values for 'pm_flags'
3892 if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
3893 PMC_F_LOG_PROCEXIT | PMC_F_CALLCHAIN |
3894 PMC_F_USERCALLCHAIN)) != 0) {
3899 /* PMC_F_USERCALLCHAIN is only valid with PMC_F_CALLCHAIN */
3900 if ((pa.pm_flags & (PMC_F_CALLCHAIN | PMC_F_USERCALLCHAIN)) ==
3901 PMC_F_USERCALLCHAIN) {
3906 /* PMC_F_USERCALLCHAIN is only valid for sampling mode */
3907 if (pa.pm_flags & PMC_F_USERCALLCHAIN &&
3908 mode != PMC_MODE_TS && mode != PMC_MODE_SS) {
3913 /* process logging options are not allowed for system PMCs */
3914 if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
3915 (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
3921 * All sampling mode PMCs need to be able to interrupt the
3924 if (PMC_IS_SAMPLING_MODE(mode))
3925 caps |= PMC_CAP_INTERRUPT;
3927 /* A valid class specifier should have been passed in. */
3928 pcd = pmc_class_to_classdep(pa.pm_class);
3934 /* The requested PMC capabilities should be feasible. */
3935 if ((pcd->pcd_caps & caps) != caps) {
3940 PMCDBG4(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
3941 pa.pm_ev, caps, mode, cpu);
3943 pmc = pmc_allocate_pmc_descriptor();
3944 pmc->pm_id = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
3946 pmc->pm_event = pa.pm_ev;
3947 pmc->pm_state = PMC_STATE_FREE;
3948 pmc->pm_caps = caps;
3949 pmc->pm_flags = pa.pm_flags;
3951 /* XXX set lower bound on sampling for process counters */
3952 if (PMC_IS_SAMPLING_MODE(mode)) {
3954 * Don't permit requested sample rate to be less than 1000
3956 if (pa.pm_count < 1000)
3958 "pmcallocate: passed sample rate %ju - setting to 1000\n",
3959 (uintmax_t)pa.pm_count);
3960 pmc->pm_sc.pm_reloadcount = MAX(1000, pa.pm_count);
3962 pmc->pm_sc.pm_initial = pa.pm_count;
3964 /* switch thread to CPU 'cpu' */
3965 pmc_save_cpu_binding(&pb);
3967 #define PMC_IS_SHAREABLE_PMC(cpu, n) \
3968 (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state & \
3969 PMC_PHW_FLAG_IS_SHAREABLE)
3970 #define PMC_IS_UNALLOCATED(cpu, n) \
3971 (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
3973 if (PMC_IS_SYSTEM_MODE(mode)) {
3974 pmc_select_cpu(cpu);
3975 for (n = pcd->pcd_ri; n < (int) md->pmd_npmc; n++) {
3976 pcd = pmc_ri_to_classdep(md, n, &adjri);
3977 if (pmc_can_allocate_row(n, mode) == 0 &&
3978 pmc_can_allocate_rowindex(
3979 curthread->td_proc, n, cpu) == 0 &&
3980 (PMC_IS_UNALLOCATED(cpu, n) ||
3981 PMC_IS_SHAREABLE_PMC(cpu, n)) &&
3982 pcd->pcd_allocate_pmc(cpu, adjri, pmc,
3987 /* Process virtual mode */
3988 for (n = pcd->pcd_ri; n < (int) md->pmd_npmc; n++) {
3989 pcd = pmc_ri_to_classdep(md, n, &adjri);
3990 if (pmc_can_allocate_row(n, mode) == 0 &&
3991 pmc_can_allocate_rowindex(
3992 curthread->td_proc, n,
3993 PMC_CPU_ANY) == 0 &&
3994 pcd->pcd_allocate_pmc(curthread->td_oncpu,
3995 adjri, pmc, &pa) == 0)
4000 #undef PMC_IS_UNALLOCATED
4001 #undef PMC_IS_SHAREABLE_PMC
4003 pmc_restore_cpu_binding(&pb);
4005 if (n == (int) md->pmd_npmc) {
4006 pmc_destroy_pmc_descriptor(pmc);
4012 /* Fill in the correct value in the ID field */
4013 pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
4015 PMCDBG5(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
4016 pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
4018 /* Process mode PMCs with logging enabled need log files */
4019 if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
4020 pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
4022 /* All system mode sampling PMCs require a log file */
4023 if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
4024 pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
4027 * Configure global pmc's immediately
4030 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
4032 pmc_save_cpu_binding(&pb);
4033 pmc_select_cpu(cpu);
4035 phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
4036 pcd = pmc_ri_to_classdep(md, n, &adjri);
4038 if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
4039 (error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) {
4040 (void) pcd->pcd_release_pmc(cpu, adjri, pmc);
4041 pmc_destroy_pmc_descriptor(pmc);
4043 pmc_restore_cpu_binding(&pb);
4048 pmc_restore_cpu_binding(&pb);
4051 pmc->pm_state = PMC_STATE_ALLOCATED;
4052 pmc->pm_class = pa.pm_class;
4055 * mark row disposition
4058 if (PMC_IS_SYSTEM_MODE(mode))
4059 PMC_MARK_ROW_STANDALONE(n);
4061 PMC_MARK_ROW_THREAD(n);
4064 * Register this PMC with the current thread as its owner.
4068 pmc_register_owner(curthread->td_proc, pmc)) != 0) {
4069 pmc_release_pmc_descriptor(pmc);
4070 pmc_destroy_pmc_descriptor(pmc);
4077 * Return the allocated index.
4080 pa.pm_pmcid = pmc->pm_id;
4082 error = copyout(&pa, arg, sizeof(pa));
4088 * Attach a PMC to a process.
4091 case PMC_OP_PMCATTACH:
4095 struct pmc_op_pmcattach a;
4097 sx_assert(&pmc_sx, SX_XLOCKED);
4099 if ((error = copyin(arg, &a, sizeof(a))) != 0)
4105 } else if (a.pm_pid == 0)
4106 a.pm_pid = td->td_proc->p_pid;
4108 if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
4111 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
4116 /* PMCs may be (re)attached only when allocated or stopped */
4117 if (pm->pm_state == PMC_STATE_RUNNING) {
4120 } else if (pm->pm_state != PMC_STATE_ALLOCATED &&
4121 pm->pm_state != PMC_STATE_STOPPED) {
4127 if ((p = pfind(a.pm_pid)) == NULL) {
4133 * Ignore processes that are working on exiting.
4135 if (p->p_flag & P_WEXIT) {
4137 PROC_UNLOCK(p); /* pfind() returns a locked process */
4142 * we are allowed to attach a PMC to a process if
4145 error = p_candebug(curthread, p);
4150 error = pmc_attach_process(p, pm);
4156 * Detach an attached PMC from a process.
4159 case PMC_OP_PMCDETACH:
4163 struct pmc_op_pmcattach a;
4165 if ((error = copyin(arg, &a, sizeof(a))) != 0)
4171 } else if (a.pm_pid == 0)
4172 a.pm_pid = td->td_proc->p_pid;
4174 if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
4177 if ((p = pfind(a.pm_pid)) == NULL) {
4183 * Treat processes that are in the process of exiting
4184 * as if they were not present.
4187 if (p->p_flag & P_WEXIT)
4190 PROC_UNLOCK(p); /* pfind() returns a locked process */
4193 error = pmc_detach_process(p, pm);
4199 * Retrieve the MSR number associated with the counter
4200 * 'pmc_id'. This allows processes to directly use RDPMC
4201 * instructions to read their PMCs, without the overhead of a
4205 case PMC_OP_PMCGETMSR:
4209 struct pmc_target *pt;
4210 struct pmc_op_getmsr gm;
4211 struct pmc_classdep *pcd;
4215 if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
4218 if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
4222 * The allocated PMC has to be a process virtual PMC,
4223 * i.e., of type MODE_T[CS]. Global PMCs can only be
4224 * read using the PMCREAD operation since they may be
4225 * allocated on a different CPU than the one we could
4226 * be running on at the time of the RDPMC instruction.
4228 * The GETMSR operation is not allowed for PMCs that
4229 * are inherited across processes.
4232 if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
4233 (pm->pm_flags & PMC_F_DESCENDANTS)) {
4239 * It only makes sense to use a RDPMC (or its
4240 * equivalent instruction on non-x86 architectures) on
4241 * a process that has allocated and attached a PMC to
4242 * itself. Conversely the PMC is only allowed to have
4243 * one process attached to it -- its owner.
4246 if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
4247 LIST_NEXT(pt, pt_next) != NULL ||
4248 pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
4253 ri = PMC_TO_ROWINDEX(pm);
4254 pcd = pmc_ri_to_classdep(md, ri, &adjri);
4256 /* PMC class has no 'GETMSR' support */
4257 if (pcd->pcd_get_msr == NULL) {
4262 if ((error = (*pcd->pcd_get_msr)(adjri, &gm.pm_msr)) < 0)
4265 if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
4269 * Mark our process as using MSRs. Update machine
4270 * state using a forced context switch.
4273 pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
4274 pmc_force_context_switch();
4280 * Release an allocated PMC
4283 case PMC_OP_PMCRELEASE:
4287 struct pmc_owner *po;
4288 struct pmc_op_simple sp;
4291 * Find PMC pointer for the named PMC.
4293 * Use pmc_release_pmc_descriptor() to switch off the
4294 * PMC, remove all its target threads, and remove the
4295 * PMC from its owner's list.
4297 * Remove the owner record if this is the last PMC
4303 if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
4306 pmcid = sp.pm_pmcid;
4308 if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
4312 pmc_release_pmc_descriptor(pm);
4313 pmc_maybe_remove_owner(po);
4314 pmc_destroy_pmc_descriptor(pm);
4320 * Read and/or write a PMC.
4328 pmc_value_t oldvalue;
4329 struct pmc_binding pb;
4330 struct pmc_op_pmcrw prw;
4331 struct pmc_classdep *pcd;
4332 struct pmc_op_pmcrw *pprw;
4336 if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
4340 PMCDBG2(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
4343 /* must have at least one flag set */
4344 if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
4349 /* locate pmc descriptor */
4350 if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
4353 /* Can't read a PMC that hasn't been started. */
4354 if (pm->pm_state != PMC_STATE_ALLOCATED &&
4355 pm->pm_state != PMC_STATE_STOPPED &&
4356 pm->pm_state != PMC_STATE_RUNNING) {
4361 /* writing a new value is allowed only for 'STOPPED' pmcs */
4362 if (pm->pm_state == PMC_STATE_RUNNING &&
4363 (prw.pm_flags & PMC_F_NEWVALUE)) {
4368 if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
4371 * If this PMC is attached to its owner (i.e.,
4372 * the process requesting this operation) and
4373 * is running, then attempt to get an
4374 * upto-date reading from hardware for a READ.
4375 * Writes are only allowed when the PMC is
4376 * stopped, so only update the saved value
4379 * If the PMC is not running, or is not
4380 * attached to its owner, read/write to the
4384 ri = PMC_TO_ROWINDEX(pm);
4385 pcd = pmc_ri_to_classdep(md, ri, &adjri);
4387 mtx_pool_lock_spin(pmc_mtxpool, pm);
4388 cpu = curthread->td_oncpu;
4390 if (prw.pm_flags & PMC_F_OLDVALUE) {
4391 if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
4392 (pm->pm_state == PMC_STATE_RUNNING))
4393 error = (*pcd->pcd_read_pmc)(cpu, adjri,
4396 oldvalue = pm->pm_gv.pm_savedvalue;
4398 if (prw.pm_flags & PMC_F_NEWVALUE)
4399 pm->pm_gv.pm_savedvalue = prw.pm_value;
4401 mtx_pool_unlock_spin(pmc_mtxpool, pm);
4403 } else { /* System mode PMCs */
4404 cpu = PMC_TO_CPU(pm);
4405 ri = PMC_TO_ROWINDEX(pm);
4406 pcd = pmc_ri_to_classdep(md, ri, &adjri);
4408 if (!pmc_cpu_is_active(cpu)) {
4413 /* move this thread to CPU 'cpu' */
4414 pmc_save_cpu_binding(&pb);
4415 pmc_select_cpu(cpu);
4418 /* save old value */
4419 if (prw.pm_flags & PMC_F_OLDVALUE)
4420 if ((error = (*pcd->pcd_read_pmc)(cpu, adjri,
4423 /* write out new value */
4424 if (prw.pm_flags & PMC_F_NEWVALUE)
4425 error = (*pcd->pcd_write_pmc)(cpu, adjri,
4429 pmc_restore_cpu_binding(&pb);
4434 pprw = (struct pmc_op_pmcrw *) arg;
4437 if (prw.pm_flags & PMC_F_NEWVALUE)
4438 PMCDBG3(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
4439 ri, prw.pm_value, oldvalue);
4440 else if (prw.pm_flags & PMC_F_OLDVALUE)
4441 PMCDBG2(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
4444 /* return old value if requested */
4445 if (prw.pm_flags & PMC_F_OLDVALUE)
4446 if ((error = copyout(&oldvalue, &pprw->pm_value,
4447 sizeof(prw.pm_value))))
4455 * Set the sampling rate for a sampling mode PMC and the
4456 * initial count for a counting mode PMC.
4459 case PMC_OP_PMCSETCOUNT:
4462 struct pmc_op_pmcsetcount sc;
4466 if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
4469 if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
4472 if (pm->pm_state == PMC_STATE_RUNNING) {
4477 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
4479 * Don't permit requested sample rate to be less than 1000
4481 if (sc.pm_count < 1000)
4483 "pmcsetcount: passed sample rate %ju - setting to 1000\n",
4484 (uintmax_t)sc.pm_count);
4485 pm->pm_sc.pm_reloadcount = MAX(1000, sc.pm_count);
4487 pm->pm_sc.pm_initial = sc.pm_count;
4496 case PMC_OP_PMCSTART:
4500 struct pmc_op_simple sp;
4502 sx_assert(&pmc_sx, SX_XLOCKED);
4504 if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
4507 pmcid = sp.pm_pmcid;
4509 if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
4512 KASSERT(pmcid == pm->pm_id,
4513 ("[pmc,%d] pmcid %x != id %x", __LINE__,
4516 if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
4518 else if (pm->pm_state != PMC_STATE_STOPPED &&
4519 pm->pm_state != PMC_STATE_ALLOCATED) {
4524 error = pmc_start(pm);
4533 case PMC_OP_PMCSTOP:
4537 struct pmc_op_simple sp;
4541 if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
4544 pmcid = sp.pm_pmcid;
4547 * Mark the PMC as inactive and invoke the MD stop
4548 * routines if needed.
4551 if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
4554 KASSERT(pmcid == pm->pm_id,
4555 ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
4558 if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
4560 else if (pm->pm_state != PMC_STATE_RUNNING) {
4565 error = pmc_stop(pm);
4571 * Write a user supplied value to the log file.
4574 case PMC_OP_WRITELOG:
4576 struct pmc_op_writelog wl;
4577 struct pmc_owner *po;
4581 if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
4584 if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
4589 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
4594 error = pmclog_process_userlog(po, &wl);
4604 if (is_sx_downgraded)
4605 sx_sunlock(&pmc_sx);
4607 sx_xunlock(&pmc_sx);
4610 counter_u64_add(pmc_stats.pm_syscall_errors, 1);
4621 * Mark the thread as needing callchain capture and post an AST. The
4622 * actual callchain capture will be done in a context where it is safe
4623 * to take page faults.
4627 pmc_post_callchain_callback(void)
4634 * If there is multiple PMCs for the same interrupt ignore new post
4636 if (td->td_pflags & TDP_CALLCHAIN)
4640 * Mark this thread as needing callchain capture.
4641 * `td->td_pflags' will be safe to touch because this thread
4642 * was in user space when it was interrupted.
4644 td->td_pflags |= TDP_CALLCHAIN;
4647 * Don't let this thread migrate between CPUs until callchain
4648 * capture completes.
4656 * Find a free slot in the per-cpu array of samples and capture the
4657 * current callchain there. If a sample was successfully added, a bit
4658 * is set in mask 'pmc_cpumask' denoting that the DO_SAMPLES hook
4659 * needs to be invoked from the clock handler.
4661 * This function is meant to be called from an NMI handler. It cannot
4662 * use any of the locking primitives supplied by the OS.
4666 pmc_add_sample(ring_type_t ring, struct pmc *pm, struct trapframe *tf)
4668 int error, cpu, callchaindepth, inuserspace;
4670 struct pmc_sample *ps;
4671 struct pmc_samplebuffer *psb;
4676 * Allocate space for a sample buffer.
4679 psb = pmc_pcpu[cpu]->pc_sb[ring];
4680 inuserspace = TRAPF_USERMODE(tf);
4681 ps = PMC_PROD_SAMPLE(psb);
4682 if (psb->ps_considx != psb->ps_prodidx &&
4683 ps->ps_nsamples) { /* in use, reader hasn't caught up */
4684 pm->pm_pcpu_state[cpu].pps_stalled = 1;
4685 counter_u64_add(pmc_stats.pm_intr_bufferfull, 1);
4686 PMCDBG6(SAM,INT,1,"(spc) cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d",
4687 cpu, pm, (void *) tf, inuserspace,
4688 (int) (psb->ps_prodidx & pmc_sample_mask),
4689 (int) (psb->ps_considx & pmc_sample_mask));
4695 /* Fill in entry. */
4696 PMCDBG6(SAM,INT,1,"cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", cpu, pm,
4697 (void *) tf, inuserspace,
4698 (int) (psb->ps_prodidx & pmc_sample_mask),
4699 (int) (psb->ps_considx & pmc_sample_mask));
4704 ps->ps_pid = td->td_proc->p_pid;
4705 ps->ps_tid = td->td_tid;
4706 ps->ps_tsc = pmc_rdtsc();
4707 ps->ps_ticks = ticks;
4709 ps->ps_flags = inuserspace ? PMC_CC_F_USERSPACE : 0;
4711 callchaindepth = (pm->pm_flags & PMC_F_CALLCHAIN) ?
4712 pmc_callchaindepth : 1;
4714 MPASS(ps->ps_pc != NULL);
4715 if (callchaindepth == 1)
4716 ps->ps_pc[0] = PMC_TRAPFRAME_TO_PC(tf);
4719 * Kernel stack traversals can be done immediately,
4720 * while we defer to an AST for user space traversals.
4724 pmc_save_kernel_callchain(ps->ps_pc,
4725 callchaindepth, tf);
4727 pmc_post_callchain_callback();
4728 callchaindepth = PMC_USER_CALLCHAIN_PENDING;
4732 ps->ps_nsamples = callchaindepth; /* mark entry as in use */
4733 if (ring == PMC_UR) {
4734 ps->ps_nsamples_actual = callchaindepth; /* mark entry as in use */
4735 ps->ps_nsamples = PMC_USER_CALLCHAIN_PENDING;
4737 ps->ps_nsamples = callchaindepth; /* mark entry as in use */
4739 KASSERT(counter_u64_fetch(pm->pm_runcount) >= 0,
4740 ("[pmc,%d] pm=%p runcount %ld", __LINE__, (void *) pm,
4741 (unsigned long)counter_u64_fetch(pm->pm_runcount)));
4743 counter_u64_add(pm->pm_runcount, 1); /* hold onto PMC */
4744 /* increment write pointer */
4747 /* mark CPU as needing processing */
4748 if (callchaindepth != PMC_USER_CALLCHAIN_PENDING)
4749 DPCPU_SET(pmc_sampled, 1);
4755 * Interrupt processing.
4757 * This function is meant to be called from an NMI handler. It cannot
4758 * use any of the locking primitives supplied by the OS.
4762 pmc_process_interrupt(int ring, struct pmc *pm, struct trapframe *tf)
4767 if ((pm->pm_flags & PMC_F_USERCALLCHAIN) &&
4768 (td->td_proc->p_flag & P_KPROC) == 0 &&
4769 !TRAPF_USERMODE(tf)) {
4770 atomic_add_int(&td->td_pmcpend, 1);
4771 return (pmc_add_sample(PMC_UR, pm, tf));
4773 return (pmc_add_sample(ring, pm, tf));
4777 * Capture a user call chain. This function will be called from ast()
4778 * before control returns to userland and before the process gets
4783 pmc_capture_user_callchain(int cpu, int ring, struct trapframe *tf)
4787 struct pmc_sample *ps;
4788 struct pmc_samplebuffer *psb;
4789 uint64_t considx, prodidx;
4790 int nsamples, nrecords, pass, iter;
4794 int start_ticks = ticks;
4796 psb = pmc_pcpu[cpu]->pc_sb[ring];
4799 KASSERT(td->td_pflags & TDP_CALLCHAIN,
4800 ("[pmc,%d] Retrieving callchain for thread that doesn't want it",
4811 nrecords = atomic_readandclear_32(&td->td_pmcpend);
4813 for (iter = 0, considx = psb->ps_considx, prodidx = psb->ps_prodidx;
4814 considx < prodidx && iter < pmc_nsamples; considx++, iter++) {
4815 ps = PMC_CONS_SAMPLE_OFF(psb, considx);
4818 * Iterate through all deferred callchain requests.
4819 * Walk from the current read pointer to the current
4824 if (ps->ps_nsamples == PMC_SAMPLE_FREE) {
4829 if ((ps->ps_pmc == NULL) ||
4830 (ps->ps_pmc->pm_state != PMC_STATE_RUNNING))
4833 if (ps->ps_td != td ||
4834 ps->ps_nsamples != PMC_USER_CALLCHAIN_PENDING ||
4835 ps->ps_pmc->pm_state != PMC_STATE_RUNNING)
4838 KASSERT(ps->ps_cpu == cpu,
4839 ("[pmc,%d] cpu mismatch ps_cpu=%d pcpu=%d", __LINE__,
4840 ps->ps_cpu, PCPU_GET(cpuid)));
4844 KASSERT(pm->pm_flags & PMC_F_CALLCHAIN,
4845 ("[pmc,%d] Retrieving callchain for PMC that doesn't "
4846 "want it", __LINE__));
4848 KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
4849 ("[pmc,%d] runcount %ld", __LINE__, (unsigned long)counter_u64_fetch(pm->pm_runcount)));
4851 if (ring == PMC_UR) {
4852 nsamples = ps->ps_nsamples_actual;
4853 counter_u64_add(pmc_stats.pm_merges, 1);
4858 * Retrieve the callchain and mark the sample buffer
4859 * as 'processable' by the timer tick sweep code.
4866 if (__predict_true(nsamples < pmc_callchaindepth - 1))
4867 nsamples += pmc_save_user_callchain(ps->ps_pc + nsamples,
4868 pmc_callchaindepth - nsamples - 1, tf);
4871 * We have to prevent hardclock from potentially overwriting
4872 * this sample between when we read the value and when we set
4877 * Verify that the sample hasn't been dropped in the meantime
4879 if (ps->ps_nsamples == PMC_USER_CALLCHAIN_PENDING) {
4880 ps->ps_nsamples = nsamples;
4882 * If we couldn't get a sample, simply drop the reference
4885 counter_u64_add(pm->pm_runcount, -1);
4888 if (nrecords-- == 1)
4891 if (__predict_false(ring == PMC_UR && td->td_pmcpend)) {
4896 /* only collect samples for this part once */
4901 if ((ticks - start_ticks) > hz)
4902 log(LOG_ERR, "%s took %d ticks\n", __func__, (ticks - start_ticks));
4905 /* mark CPU as needing processing */
4906 DPCPU_SET(pmc_sampled, 1);
4910 * Process saved PC samples.
4914 pmc_process_samples(int cpu, ring_type_t ring)
4919 struct pmc_owner *po;
4920 struct pmc_sample *ps;
4921 struct pmc_classdep *pcd;
4922 struct pmc_samplebuffer *psb;
4925 KASSERT(PCPU_GET(cpuid) == cpu,
4926 ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
4927 PCPU_GET(cpuid), cpu));
4929 psb = pmc_pcpu[cpu]->pc_sb[ring];
4930 delta = psb->ps_prodidx - psb->ps_considx;
4931 MPASS(delta <= pmc_nsamples);
4932 MPASS(psb->ps_considx <= psb->ps_prodidx);
4933 for (n = 0; psb->ps_considx < psb->ps_prodidx; psb->ps_considx++, n++) {
4934 ps = PMC_CONS_SAMPLE(psb);
4936 if (__predict_false(ps->ps_nsamples == PMC_SAMPLE_FREE))
4939 /* skip non-running samples */
4940 if (pm->pm_state != PMC_STATE_RUNNING)
4943 KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
4944 ("[pmc,%d] pm=%p runcount %ld", __LINE__, (void *) pm,
4945 (unsigned long)counter_u64_fetch(pm->pm_runcount)));
4949 KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
4950 ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
4951 pm, PMC_TO_MODE(pm)));
4954 /* If there is a pending AST wait for completion */
4955 if (ps->ps_nsamples == PMC_USER_CALLCHAIN_PENDING) {
4956 /* if we've been waiting more than 1 tick to
4957 * collect a callchain for this record then
4958 * drop it and move on.
4960 if (ticks - ps->ps_ticks > 1) {
4962 * track how often we hit this as it will
4963 * preferentially lose user samples
4964 * for long running system calls
4966 counter_u64_add(pmc_stats.pm_overwrites, 1);
4969 /* Need a rescan at a later time. */
4970 DPCPU_SET(pmc_sampled, 1);
4974 PMCDBG6(SAM,OPS,1,"cpu=%d pm=%p n=%d fl=%x wr=%d rd=%d", cpu,
4975 pm, ps->ps_nsamples, ps->ps_flags,
4976 (int) (psb->ps_prodidx & pmc_sample_mask),
4977 (int) (psb->ps_considx & pmc_sample_mask));
4980 * If this is a process-mode PMC that is attached to
4981 * its owner, and if the PC is in user mode, update
4982 * profiling statistics like timer-based profiling
4985 * Otherwise, this is either a sampling-mode PMC that
4986 * is attached to a different process than its owner,
4987 * or a system-wide sampling PMC. Dispatch a log
4988 * entry to the PMC's owner process.
4990 if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
4991 if (ps->ps_flags & PMC_CC_F_USERSPACE) {
4992 td = FIRST_THREAD_IN_PROC(po->po_owner);
4993 addupc_intr(td, ps->ps_pc[0], 1);
4996 pmclog_process_callchain(pm, ps);
4999 ps->ps_nsamples = 0; /* mark entry as free */
5000 KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5001 ("[pmc,%d] pm=%p runcount %ld", __LINE__, (void *) pm,
5002 (unsigned long)counter_u64_fetch(pm->pm_runcount)));
5004 counter_u64_add(pm->pm_runcount, -1);
5007 counter_u64_add(pmc_stats.pm_log_sweeps, 1);
5009 /* Do not re-enable stalled PMCs if we failed to process any samples */
5014 * Restart any stalled sampling PMCs on this CPU.
5016 * If the NMI handler sets the pm_stalled field of a PMC after
5017 * the check below, we'll end up processing the stalled PMC at
5018 * the next hardclock tick.
5020 for (n = 0; n < md->pmd_npmc; n++) {
5021 pcd = pmc_ri_to_classdep(md, n, &adjri);
5022 KASSERT(pcd != NULL,
5023 ("[pmc,%d] null pcd ri=%d", __LINE__, n));
5024 (void) (*pcd->pcd_get_config)(cpu,adjri,&pm);
5026 if (pm == NULL || /* !cfg'ed */
5027 pm->pm_state != PMC_STATE_RUNNING || /* !active */
5028 !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
5029 !pm->pm_pcpu_state[cpu].pps_cpustate || /* !desired */
5030 !pm->pm_pcpu_state[cpu].pps_stalled) /* !stalled */
5033 pm->pm_pcpu_state[cpu].pps_stalled = 0;
5034 (*pcd->pcd_start_pmc)(cpu, adjri);
5043 * Handle a process exit.
5045 * Remove this process from all hash tables. If this process
5046 * owned any PMCs, turn off those PMCs and deallocate them,
5047 * removing any associations with target processes.
5049 * This function will be called by the last 'thread' of a
5052 * XXX This eventhandler gets called early in the exit process.
5053 * Consider using a 'hook' invocation from thread_exit() or equivalent
5054 * spot. Another negative is that kse_exit doesn't seem to call
5060 pmc_process_exit(void *arg __unused, struct proc *p)
5065 int is_using_hwpmcs;
5066 struct pmc_owner *po;
5067 struct pmc_process *pp;
5068 struct pmc_classdep *pcd;
5069 pmc_value_t newvalue, tmp;
5072 is_using_hwpmcs = p->p_flag & P_HWPMC;
5076 * Log a sysexit event to all SS PMC owners.
5079 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5080 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5081 pmclog_process_sysexit(po, p->p_pid);
5084 if (!is_using_hwpmcs)
5088 PMCDBG3(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
5092 * Since this code is invoked by the last thread in an exiting
5093 * process, we would have context switched IN at some prior
5094 * point. However, with PREEMPTION, kernel mode context
5095 * switches may happen any time, so we want to disable a
5096 * context switch OUT till we get any PMCs targeting this
5097 * process off the hardware.
5099 * We also need to atomically remove this process'
5100 * entry from our target process hash table, using
5103 PMCDBG3(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
5106 critical_enter(); /* no preemption */
5108 cpu = curthread->td_oncpu;
5110 if ((pp = pmc_find_process_descriptor(p,
5111 PMC_FLAG_REMOVE)) != NULL) {
5114 "process-exit proc=%p pmc-process=%p", p, pp);
5117 * The exiting process could the target of
5118 * some PMCs which will be running on
5119 * currently executing CPU.
5121 * We need to turn these PMCs off like we
5122 * would do at context switch OUT time.
5124 for (ri = 0; ri < md->pmd_npmc; ri++) {
5127 * Pick up the pmc pointer from hardware
5128 * state similar to the CSW_OUT code.
5132 pcd = pmc_ri_to_classdep(md, ri, &adjri);
5134 (void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
5136 PMCDBG2(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
5139 !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
5142 PMCDBG4(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
5143 "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
5146 KASSERT(PMC_TO_ROWINDEX(pm) == ri,
5147 ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
5148 __LINE__, PMC_TO_ROWINDEX(pm), ri));
5150 KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
5151 ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
5152 __LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
5154 KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
5155 ("[pmc,%d] bad runcount ri %d rc %ld",
5156 __LINE__, ri, (unsigned long)counter_u64_fetch(pm->pm_runcount)));
5159 * Change desired state, and then stop if not
5160 * stalled. This two-step dance should avoid
5161 * race conditions where an interrupt re-enables
5162 * the PMC after this code has already checked
5163 * the pm_stalled flag.
5165 if (pm->pm_pcpu_state[cpu].pps_cpustate) {
5166 pm->pm_pcpu_state[cpu].pps_cpustate = 0;
5167 if (!pm->pm_pcpu_state[cpu].pps_stalled) {
5168 (void) pcd->pcd_stop_pmc(cpu, adjri);
5170 if (PMC_TO_MODE(pm) == PMC_MODE_TC) {
5171 pcd->pcd_read_pmc(cpu, adjri,
5174 PMC_PCPU_SAVED(cpu,ri);
5176 mtx_pool_lock_spin(pmc_mtxpool,
5178 pm->pm_gv.pm_savedvalue += tmp;
5179 pp->pp_pmcs[ri].pp_pmcval +=
5181 mtx_pool_unlock_spin(
5187 KASSERT((int64_t) counter_u64_fetch(pm->pm_runcount) > 0,
5188 ("[pmc,%d] runcount is %d", __LINE__, ri));
5190 counter_u64_add(pm->pm_runcount, -1);
5192 (void) pcd->pcd_config_pmc(cpu, adjri, NULL);
5196 * Inform the MD layer of this pseudo "context switch
5199 (void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
5201 critical_exit(); /* ok to be pre-empted now */
5204 * Unlink this process from the PMCs that are
5205 * targeting it. This will send a signal to
5206 * all PMC owner's whose PMCs are orphaned.
5208 * Log PMC value at exit time if requested.
5210 for (ri = 0; ri < md->pmd_npmc; ri++)
5211 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
5212 if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
5213 PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
5214 pmclog_process_procexit(pm, pp);
5215 pmc_unlink_target_process(pm, pp);
5220 critical_exit(); /* pp == NULL */
5224 * If the process owned PMCs, free them up and free up
5227 if ((po = pmc_find_owner_descriptor(p)) != NULL) {
5228 pmc_remove_owner(po);
5229 pmc_destroy_owner_descriptor(po);
5232 sx_xunlock(&pmc_sx);
5236 * Handle a process fork.
5238 * If the parent process 'p1' is under HWPMC monitoring, then copy
5239 * over any attached PMCs that have 'do_descendants' semantics.
5243 pmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
5246 int is_using_hwpmcs;
5248 uint32_t do_descendants;
5250 struct pmc_owner *po;
5251 struct pmc_process *ppnew, *ppold;
5253 (void) flags; /* unused parameter */
5256 is_using_hwpmcs = p1->p_flag & P_HWPMC;
5260 * If there are system-wide sampling PMCs active, we need to
5261 * log all fork events to their owner's logs.
5264 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5265 if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
5266 pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
5267 pmclog_process_proccreate(po, newproc, 1);
5271 if (!is_using_hwpmcs)
5275 PMCDBG4(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
5276 p1->p_pid, p1->p_comm, newproc);
5279 * If the parent process (curthread->td_proc) is a
5280 * target of any PMCs, look for PMCs that are to be
5281 * inherited, and link these into the new process
5284 if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
5285 PMC_FLAG_NONE)) == NULL)
5286 goto done; /* nothing to do */
5289 for (ri = 0; ri < md->pmd_npmc; ri++)
5290 if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
5291 do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
5292 if (do_descendants == 0) /* nothing to do */
5296 * Now mark the new process as being tracked by this driver.
5299 newproc->p_flag |= P_HWPMC;
5300 PROC_UNLOCK(newproc);
5302 /* allocate a descriptor for the new process */
5303 if ((ppnew = pmc_find_process_descriptor(newproc,
5304 PMC_FLAG_ALLOCATE)) == NULL)
5308 * Run through all PMCs that were targeting the old process
5309 * and which specified F_DESCENDANTS and attach them to the
5312 * Log the fork event to all owners of PMCs attached to this
5313 * process, if not already logged.
5315 for (ri = 0; ri < md->pmd_npmc; ri++)
5316 if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
5317 (pm->pm_flags & PMC_F_DESCENDANTS)) {
5318 pmc_link_target_process(pm, ppnew);
5320 if (po->po_sscount == 0 &&
5321 po->po_flags & PMC_PO_OWNS_LOGFILE)
5322 pmclog_process_procfork(po, p1->p_pid,
5327 sx_xunlock(&pmc_sx);
5331 pmc_process_threadcreate(struct thread *td)
5333 struct pmc_owner *po;
5336 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5337 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5338 pmclog_process_threadcreate(po, td, 1);
5343 pmc_process_threadexit(struct thread *td)
5345 struct pmc_owner *po;
5348 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5349 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5350 pmclog_process_threadexit(po, td);
5355 pmc_process_proccreate(struct proc *p)
5357 struct pmc_owner *po;
5360 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5361 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5362 pmclog_process_proccreate(po, p, 1 /* sync */);
5367 pmc_process_allproc(struct pmc *pm)
5369 struct pmc_owner *po;
5374 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
5376 sx_slock(&allproc_lock);
5377 FOREACH_PROC_IN_SYSTEM(p) {
5378 pmclog_process_proccreate(po, p, 0 /* sync */);
5380 FOREACH_THREAD_IN_PROC(p, td)
5381 pmclog_process_threadcreate(po, td, 0 /* sync */);
5384 sx_sunlock(&allproc_lock);
5385 pmclog_flush(po, 0);
5389 pmc_kld_load(void *arg __unused, linker_file_t lf)
5391 struct pmc_owner *po;
5394 * Notify owners of system sampling PMCs about KLD operations.
5397 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5398 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5399 pmclog_process_map_in(po, (pid_t) -1,
5400 (uintfptr_t) lf->address, lf->filename);
5404 * TODO: Notify owners of (all) process-sampling PMCs too.
5409 pmc_kld_unload(void *arg __unused, const char *filename __unused,
5410 caddr_t address, size_t size)
5412 struct pmc_owner *po;
5415 CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
5416 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
5417 pmclog_process_map_out(po, (pid_t) -1,
5418 (uintfptr_t) address, (uintfptr_t) address + size);
5422 * TODO: Notify owners of process-sampling PMCs.
5430 pmc_name_of_pmcclass(enum pmc_class class)
5435 #define __PMC_CLASS(S,V,D) \
5436 case PMC_CLASS_##S: \
5440 return ("<unknown>");
5445 * Base class initializer: allocate structure and set default classes.
5448 pmc_mdep_alloc(int nclasses)
5450 struct pmc_mdep *md;
5453 /* SOFT + md classes */
5455 md = malloc(sizeof(struct pmc_mdep) + n *
5456 sizeof(struct pmc_classdep), M_PMC, M_WAITOK|M_ZERO);
5459 /* Add base class. */
5460 pmc_soft_initialize(md);
5465 pmc_mdep_free(struct pmc_mdep *md)
5467 pmc_soft_finalize(md);
5472 generic_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
5474 (void) pc; (void) pp;
5480 generic_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
5482 (void) pc; (void) pp;
5487 static struct pmc_mdep *
5488 pmc_generic_cpu_initialize(void)
5490 struct pmc_mdep *md;
5492 md = pmc_mdep_alloc(0);
5494 md->pmd_cputype = PMC_CPU_GENERIC;
5496 md->pmd_pcpu_init = NULL;
5497 md->pmd_pcpu_fini = NULL;
5498 md->pmd_switch_in = generic_switch_in;
5499 md->pmd_switch_out = generic_switch_out;
5505 pmc_generic_cpu_finalize(struct pmc_mdep *md)
5512 pmc_initialize(void)
5514 int c, cpu, error, n, ri;
5515 unsigned int maxcpu, domain;
5517 struct pmc_binding pb;
5518 struct pmc_sample *ps;
5519 struct pmc_classdep *pcd;
5520 struct pmc_samplebuffer *sb;
5525 pmc_stats.pm_intr_ignored = counter_u64_alloc(M_WAITOK);
5526 pmc_stats.pm_intr_processed = counter_u64_alloc(M_WAITOK);
5527 pmc_stats.pm_intr_bufferfull = counter_u64_alloc(M_WAITOK);
5528 pmc_stats.pm_syscalls = counter_u64_alloc(M_WAITOK);
5529 pmc_stats.pm_syscall_errors = counter_u64_alloc(M_WAITOK);
5530 pmc_stats.pm_buffer_requests = counter_u64_alloc(M_WAITOK);
5531 pmc_stats.pm_buffer_requests_failed = counter_u64_alloc(M_WAITOK);
5532 pmc_stats.pm_log_sweeps = counter_u64_alloc(M_WAITOK);
5533 pmc_stats.pm_merges = counter_u64_alloc(M_WAITOK);
5534 pmc_stats.pm_overwrites = counter_u64_alloc(M_WAITOK);
5537 /* parse debug flags first */
5538 if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
5539 pmc_debugstr, sizeof(pmc_debugstr)))
5540 pmc_debugflags_parse(pmc_debugstr,
5541 pmc_debugstr+strlen(pmc_debugstr));
5544 PMCDBG1(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
5546 /* check kernel version */
5547 if (pmc_kernel_version != PMC_VERSION) {
5548 if (pmc_kernel_version == 0)
5549 printf("hwpmc: this kernel has not been compiled with "
5550 "'options HWPMC_HOOKS'.\n");
5552 printf("hwpmc: kernel version (0x%x) does not match "
5553 "module version (0x%x).\n", pmc_kernel_version,
5555 return EPROGMISMATCH;
5559 * check sysctl parameters
5562 if (pmc_hashsize <= 0) {
5563 (void) printf("hwpmc: tunable \"hashsize\"=%d must be "
5564 "greater than zero.\n", pmc_hashsize);
5565 pmc_hashsize = PMC_HASH_SIZE;
5568 if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
5569 (void) printf("hwpmc: tunable \"nsamples\"=%d out of "
5570 "range.\n", pmc_nsamples);
5571 pmc_nsamples = PMC_NSAMPLES;
5573 pmc_sample_mask = pmc_nsamples-1;
5575 if (pmc_callchaindepth <= 0 ||
5576 pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) {
5577 (void) printf("hwpmc: tunable \"callchaindepth\"=%d out of "
5578 "range - using %d.\n", pmc_callchaindepth,
5579 PMC_CALLCHAIN_DEPTH_MAX);
5580 pmc_callchaindepth = PMC_CALLCHAIN_DEPTH_MAX;
5583 md = pmc_md_initialize();
5585 /* Default to generic CPU. */
5586 md = pmc_generic_cpu_initialize();
5591 KASSERT(md->pmd_nclass >= 1 && md->pmd_npmc >= 1,
5592 ("[pmc,%d] no classes or pmcs", __LINE__));
5594 /* Compute the map from row-indices to classdep pointers. */
5595 pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) *
5596 md->pmd_npmc, M_PMC, M_WAITOK|M_ZERO);
5598 for (n = 0; n < md->pmd_npmc; n++)
5599 pmc_rowindex_to_classdep[n] = NULL;
5600 for (ri = c = 0; c < md->pmd_nclass; c++) {
5601 pcd = &md->pmd_classdep[c];
5602 for (n = 0; n < pcd->pcd_num; n++, ri++)
5603 pmc_rowindex_to_classdep[ri] = pcd;
5606 KASSERT(ri == md->pmd_npmc,
5607 ("[pmc,%d] npmc miscomputed: ri=%d, md->npmc=%d", __LINE__,
5610 maxcpu = pmc_cpu_max();
5612 /* allocate space for the per-cpu array */
5613 pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
5616 /* per-cpu 'saved values' for managing process-mode PMCs */
5617 pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
5620 /* Perform CPU-dependent initialization. */
5621 pmc_save_cpu_binding(&pb);
5623 for (cpu = 0; error == 0 && cpu < maxcpu; cpu++) {
5624 if (!pmc_cpu_is_active(cpu))
5626 pmc_select_cpu(cpu);
5627 pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) +
5628 md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC,
5630 if (md->pmd_pcpu_init)
5631 error = md->pmd_pcpu_init(md, cpu);
5632 for (n = 0; error == 0 && n < md->pmd_nclass; n++)
5633 error = md->pmd_classdep[n].pcd_pcpu_init(md, cpu);
5635 pmc_restore_cpu_binding(&pb);
5640 /* allocate space for the sample array */
5641 for (cpu = 0; cpu < maxcpu; cpu++) {
5642 if (!pmc_cpu_is_active(cpu))
5644 pc = pcpu_find(cpu);
5645 domain = pc->pc_domain;
5646 sb = malloc_domainset(sizeof(struct pmc_samplebuffer) +
5647 pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
5648 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5650 KASSERT(pmc_pcpu[cpu] != NULL,
5651 ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
5653 sb->ps_callchains = malloc_domainset(pmc_callchaindepth *
5654 pmc_nsamples * sizeof(uintptr_t), M_PMC,
5655 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5657 for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
5658 ps->ps_pc = sb->ps_callchains +
5659 (n * pmc_callchaindepth);
5661 pmc_pcpu[cpu]->pc_sb[PMC_HR] = sb;
5663 sb = malloc_domainset(sizeof(struct pmc_samplebuffer) +
5664 pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
5665 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5667 sb->ps_callchains = malloc_domainset(pmc_callchaindepth *
5668 pmc_nsamples * sizeof(uintptr_t), M_PMC,
5669 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5670 for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
5671 ps->ps_pc = sb->ps_callchains +
5672 (n * pmc_callchaindepth);
5674 pmc_pcpu[cpu]->pc_sb[PMC_SR] = sb;
5676 sb = malloc_domainset(sizeof(struct pmc_samplebuffer) +
5677 pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
5678 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5679 sb->ps_callchains = malloc_domainset(pmc_callchaindepth *
5680 pmc_nsamples * sizeof(uintptr_t), M_PMC,
5681 DOMAINSET_PREF(domain), M_WAITOK | M_ZERO);
5682 for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
5683 ps->ps_pc = sb->ps_callchains + n * pmc_callchaindepth;
5685 pmc_pcpu[cpu]->pc_sb[PMC_UR] = sb;
5688 /* allocate space for the row disposition array */
5689 pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
5690 M_PMC, M_WAITOK|M_ZERO);
5692 /* mark all PMCs as available */
5693 for (n = 0; n < (int) md->pmd_npmc; n++)
5694 PMC_MARK_ROW_FREE(n);
5696 /* allocate thread hash tables */
5697 pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
5698 &pmc_ownerhashmask);
5700 pmc_processhash = hashinit(pmc_hashsize, M_PMC,
5701 &pmc_processhashmask);
5702 mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc-leaf",
5705 CK_LIST_INIT(&pmc_ss_owners);
5708 /* allocate a pool of spin mutexes */
5709 pmc_mtxpool = mtx_pool_create("pmc-leaf", pmc_mtxpool_size,
5712 PMCDBG4(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
5713 "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
5714 pmc_processhash, pmc_processhashmask);
5716 /* Initialize a spin mutex for the thread free list. */
5717 mtx_init(&pmc_threadfreelist_mtx, "pmc-threadfreelist", "pmc-leaf",
5720 /* Initialize the task to prune the thread free list. */
5721 TASK_INIT(&free_task, 0, pmc_thread_descriptor_pool_free_task, NULL);
5723 /* register process {exit,fork,exec} handlers */
5724 pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
5725 pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
5726 pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
5727 pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
5729 /* register kld event handlers */
5730 pmc_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, pmc_kld_load,
5731 NULL, EVENTHANDLER_PRI_ANY);
5732 pmc_kld_unload_tag = EVENTHANDLER_REGISTER(kld_unload, pmc_kld_unload,
5733 NULL, EVENTHANDLER_PRI_ANY);
5735 /* initialize logging */
5736 pmclog_initialize();
5738 /* set hook functions */
5739 pmc_intr = md->pmd_intr;
5741 pmc_hook = pmc_hook_handler;
5744 printf(PMC_MODULE_NAME ":");
5745 for (n = 0; n < (int) md->pmd_nclass; n++) {
5746 pcd = &md->pmd_classdep[n];
5747 printf(" %s/%d/%d/0x%b",
5748 pmc_name_of_pmcclass(pcd->pcd_class),
5753 "\1INT\2USR\3SYS\4EDG\5THR"
5754 "\6REA\7WRI\10INV\11QUA\12PRC"
5763 /* prepare to be unloaded */
5768 unsigned int maxcpu;
5769 struct pmc_ownerhash *ph;
5770 struct pmc_owner *po, *tmp;
5771 struct pmc_binding pb;
5773 struct pmc_processhash *prh;
5776 PMCDBG0(MOD,INI,0, "cleanup");
5778 /* switch off sampling */
5780 DPCPU_ID_SET(cpu, pmc_sampled, 0);
5784 if (pmc_hook == NULL) { /* being unloaded already */
5785 sx_xunlock(&pmc_sx);
5789 pmc_hook = NULL; /* prevent new threads from entering module */
5791 /* deregister event handlers */
5792 EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
5793 EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
5794 EVENTHANDLER_DEREGISTER(kld_load, pmc_kld_load_tag);
5795 EVENTHANDLER_DEREGISTER(kld_unload, pmc_kld_unload_tag);
5797 /* send SIGBUS to all owner threads, free up allocations */
5799 for (ph = pmc_ownerhash;
5800 ph <= &pmc_ownerhash[pmc_ownerhashmask];
5802 LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
5803 pmc_remove_owner(po);
5805 /* send SIGBUS to owner processes */
5806 PMCDBG3(MOD,INI,2, "cleanup signal proc=%p "
5807 "(%d, %s)", po->po_owner,
5808 po->po_owner->p_pid,
5809 po->po_owner->p_comm);
5811 PROC_LOCK(po->po_owner);
5812 kern_psignal(po->po_owner, SIGBUS);
5813 PROC_UNLOCK(po->po_owner);
5815 pmc_destroy_owner_descriptor(po);
5819 /* reclaim allocated data structures */
5820 taskqueue_drain(taskqueue_fast, &free_task);
5821 mtx_destroy(&pmc_threadfreelist_mtx);
5822 pmc_thread_descriptor_pool_drain();
5825 mtx_pool_destroy(&pmc_mtxpool);
5827 mtx_destroy(&pmc_processhash_mtx);
5828 if (pmc_processhash) {
5830 struct pmc_process *pp;
5832 PMCDBG0(MOD,INI,3, "destroy process hash");
5833 for (prh = pmc_processhash;
5834 prh <= &pmc_processhash[pmc_processhashmask];
5836 LIST_FOREACH(pp, prh, pp_next)
5837 PMCDBG1(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
5840 hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
5841 pmc_processhash = NULL;
5844 if (pmc_ownerhash) {
5845 PMCDBG0(MOD,INI,3, "destroy owner hash");
5846 hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
5847 pmc_ownerhash = NULL;
5850 KASSERT(CK_LIST_EMPTY(&pmc_ss_owners),
5851 ("[pmc,%d] Global SS owner list not empty", __LINE__));
5852 KASSERT(pmc_ss_count == 0,
5853 ("[pmc,%d] Global SS count not empty", __LINE__));
5855 /* do processor and pmc-class dependent cleanup */
5856 maxcpu = pmc_cpu_max();
5858 PMCDBG0(MOD,INI,3, "md cleanup");
5860 pmc_save_cpu_binding(&pb);
5861 for (cpu = 0; cpu < maxcpu; cpu++) {
5862 PMCDBG2(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
5863 cpu, pmc_pcpu[cpu]);
5864 if (!pmc_cpu_is_active(cpu) || pmc_pcpu[cpu] == NULL)
5866 pmc_select_cpu(cpu);
5867 for (c = 0; c < md->pmd_nclass; c++)
5868 md->pmd_classdep[c].pcd_pcpu_fini(md, cpu);
5869 if (md->pmd_pcpu_fini)
5870 md->pmd_pcpu_fini(md, cpu);
5873 if (md->pmd_cputype == PMC_CPU_GENERIC)
5874 pmc_generic_cpu_finalize(md);
5876 pmc_md_finalize(md);
5880 pmc_restore_cpu_binding(&pb);
5883 /* Free per-cpu descriptors. */
5884 for (cpu = 0; cpu < maxcpu; cpu++) {
5885 if (!pmc_cpu_is_active(cpu))
5887 KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_HR] != NULL,
5888 ("[pmc,%d] Null hw cpu sample buffer cpu=%d", __LINE__,
5890 KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_SR] != NULL,
5891 ("[pmc,%d] Null sw cpu sample buffer cpu=%d", __LINE__,
5893 KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_UR] != NULL,
5894 ("[pmc,%d] Null userret cpu sample buffer cpu=%d", __LINE__,
5896 free_domain(pmc_pcpu[cpu]->pc_sb[PMC_HR]->ps_callchains, M_PMC);
5897 free_domain(pmc_pcpu[cpu]->pc_sb[PMC_HR], M_PMC);
5898 free_domain(pmc_pcpu[cpu]->pc_sb[PMC_SR]->ps_callchains, M_PMC);
5899 free_domain(pmc_pcpu[cpu]->pc_sb[PMC_SR], M_PMC);
5900 free_domain(pmc_pcpu[cpu]->pc_sb[PMC_UR]->ps_callchains, M_PMC);
5901 free_domain(pmc_pcpu[cpu]->pc_sb[PMC_UR], M_PMC);
5902 free_domain(pmc_pcpu[cpu], M_PMC);
5905 free(pmc_pcpu, M_PMC);
5908 free(pmc_pcpu_saved, M_PMC);
5909 pmc_pcpu_saved = NULL;
5912 free(pmc_pmcdisp, M_PMC);
5916 if (pmc_rowindex_to_classdep) {
5917 free(pmc_rowindex_to_classdep, M_PMC);
5918 pmc_rowindex_to_classdep = NULL;
5922 counter_u64_free(pmc_stats.pm_intr_ignored);
5923 counter_u64_free(pmc_stats.pm_intr_processed);
5924 counter_u64_free(pmc_stats.pm_intr_bufferfull);
5925 counter_u64_free(pmc_stats.pm_syscalls);
5926 counter_u64_free(pmc_stats.pm_syscall_errors);
5927 counter_u64_free(pmc_stats.pm_buffer_requests);
5928 counter_u64_free(pmc_stats.pm_buffer_requests_failed);
5929 counter_u64_free(pmc_stats.pm_log_sweeps);
5930 counter_u64_free(pmc_stats.pm_merges);
5931 counter_u64_free(pmc_stats.pm_overwrites);
5932 sx_xunlock(&pmc_sx); /* we are done */
5936 * The function called at load/unload.
5940 load (struct module *module __unused, int cmd, void *arg __unused)
5948 /* initialize the subsystem */
5949 error = pmc_initialize();
5952 PMCDBG2(MOD,INI,1, "syscall=%d maxcpu=%d",
5953 pmc_syscall_num, pmc_cpu_max());
5960 PMCDBG0(MOD,INI,1, "unloaded");
5964 error = EINVAL; /* XXX should panic(9) */