2 * Copyright (c) 2003-2008 Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mount.h>
45 #include <sys/mutex.h>
47 #include <sys/pmckern.h>
48 #include <sys/pmclog.h>
51 #include <sys/queue.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
54 #include <sys/sched.h>
55 #include <sys/signalvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysent.h>
60 #include <sys/systm.h>
61 #include <sys/vnode.h>
63 #include <sys/linker.h> /* needs to be after <sys/malloc.h> */
65 #include <machine/atomic.h>
66 #include <machine/md_var.h>
69 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
74 #include "hwpmc_soft.h"
81 PMC_FLAG_NONE = 0x00, /* do nothing */
82 PMC_FLAG_REMOVE = 0x01, /* atomically remove entry from hash */
83 PMC_FLAG_ALLOCATE = 0x02, /* add entry to hash if not found */
87 * The offset in sysent where the syscall is allocated.
90 static int pmc_syscall_num = NO_SYSCALL;
91 struct pmc_cpu **pmc_pcpu; /* per-cpu state */
92 pmc_value_t *pmc_pcpu_saved; /* saved PMC values: CSW handling */
94 #define PMC_PCPU_SAVED(C,R) pmc_pcpu_saved[(R) + md->pmd_npmc*(C)]
96 struct mtx_pool *pmc_mtxpool;
97 static int *pmc_pmcdisp; /* PMC row dispositions */
99 #define PMC_ROW_DISP_IS_FREE(R) (pmc_pmcdisp[(R)] == 0)
100 #define PMC_ROW_DISP_IS_THREAD(R) (pmc_pmcdisp[(R)] > 0)
101 #define PMC_ROW_DISP_IS_STANDALONE(R) (pmc_pmcdisp[(R)] < 0)
103 #define PMC_MARK_ROW_FREE(R) do { \
104 pmc_pmcdisp[(R)] = 0; \
107 #define PMC_MARK_ROW_STANDALONE(R) do { \
108 KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
110 atomic_add_int(&pmc_pmcdisp[(R)], -1); \
111 KASSERT(pmc_pmcdisp[(R)] >= (-pmc_cpu_max_active()), \
112 ("[pmc,%d] row disposition error", __LINE__)); \
115 #define PMC_UNMARK_ROW_STANDALONE(R) do { \
116 atomic_add_int(&pmc_pmcdisp[(R)], 1); \
117 KASSERT(pmc_pmcdisp[(R)] <= 0, ("[pmc,%d] row disposition error", \
121 #define PMC_MARK_ROW_THREAD(R) do { \
122 KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
124 atomic_add_int(&pmc_pmcdisp[(R)], 1); \
127 #define PMC_UNMARK_ROW_THREAD(R) do { \
128 atomic_add_int(&pmc_pmcdisp[(R)], -1); \
129 KASSERT(pmc_pmcdisp[(R)] >= 0, ("[pmc,%d] row disposition error", \
134 /* various event handlers */
135 static eventhandler_tag pmc_exit_tag, pmc_fork_tag, pmc_kld_load_tag,
138 /* Module statistics */
139 struct pmc_op_getdriverstats pmc_stats;
141 /* Machine/processor dependent operations */
142 static struct pmc_mdep *md;
145 * Hash tables mapping owner processes and target threads to PMCs.
148 struct mtx pmc_processhash_mtx; /* spin mutex */
149 static u_long pmc_processhashmask;
150 static LIST_HEAD(pmc_processhash, pmc_process) *pmc_processhash;
153 * Hash table of PMC owner descriptors. This table is protected by
154 * the shared PMC "sx" lock.
157 static u_long pmc_ownerhashmask;
158 static LIST_HEAD(pmc_ownerhash, pmc_owner) *pmc_ownerhash;
161 * List of PMC owners with system-wide sampling PMCs.
164 static LIST_HEAD(, pmc_owner) pmc_ss_owners;
168 * A map of row indices to classdep structures.
170 static struct pmc_classdep **pmc_rowindex_to_classdep;
177 static int pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
178 static int pmc_debugflags_parse(char *newstr, char *fence);
181 static int load(struct module *module, int cmd, void *arg);
182 static int pmc_attach_process(struct proc *p, struct pmc *pm);
183 static struct pmc *pmc_allocate_pmc_descriptor(void);
184 static struct pmc_owner *pmc_allocate_owner_descriptor(struct proc *p);
185 static int pmc_attach_one_process(struct proc *p, struct pmc *pm);
186 static int pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
188 static int pmc_can_attach(struct pmc *pm, struct proc *p);
189 static void pmc_capture_user_callchain(int cpu, int soft, struct trapframe *tf);
190 static void pmc_cleanup(void);
191 static int pmc_detach_process(struct proc *p, struct pmc *pm);
192 static int pmc_detach_one_process(struct proc *p, struct pmc *pm,
194 static void pmc_destroy_owner_descriptor(struct pmc_owner *po);
195 static struct pmc_owner *pmc_find_owner_descriptor(struct proc *p);
196 static int pmc_find_pmc(pmc_id_t pmcid, struct pmc **pm);
197 static struct pmc *pmc_find_pmc_descriptor_in_process(struct pmc_owner *po,
199 static struct pmc_process *pmc_find_process_descriptor(struct proc *p,
201 static void pmc_force_context_switch(void);
202 static void pmc_link_target_process(struct pmc *pm,
203 struct pmc_process *pp);
204 static void pmc_log_all_process_mappings(struct pmc_owner *po);
205 static void pmc_log_kernel_mappings(struct pmc *pm);
206 static void pmc_log_process_mappings(struct pmc_owner *po, struct proc *p);
207 static void pmc_maybe_remove_owner(struct pmc_owner *po);
208 static void pmc_process_csw_in(struct thread *td);
209 static void pmc_process_csw_out(struct thread *td);
210 static void pmc_process_exit(void *arg, struct proc *p);
211 static void pmc_process_fork(void *arg, struct proc *p1,
212 struct proc *p2, int n);
213 static void pmc_process_samples(int cpu, int soft);
214 static void pmc_release_pmc_descriptor(struct pmc *pmc);
215 static void pmc_remove_owner(struct pmc_owner *po);
216 static void pmc_remove_process_descriptor(struct pmc_process *pp);
217 static void pmc_restore_cpu_binding(struct pmc_binding *pb);
218 static void pmc_save_cpu_binding(struct pmc_binding *pb);
219 static void pmc_select_cpu(int cpu);
220 static int pmc_start(struct pmc *pm);
221 static int pmc_stop(struct pmc *pm);
222 static int pmc_syscall_handler(struct thread *td, void *syscall_args);
223 static void pmc_unlink_target_process(struct pmc *pmc,
224 struct pmc_process *pp);
225 static int generic_switch_in(struct pmc_cpu *pc, struct pmc_process *pp);
226 static int generic_switch_out(struct pmc_cpu *pc, struct pmc_process *pp);
227 static struct pmc_mdep *pmc_generic_cpu_initialize(void);
228 static void pmc_generic_cpu_finalize(struct pmc_mdep *md);
231 * Kernel tunables and sysctl(8) interface.
234 SYSCTL_DECL(_kern_hwpmc);
236 static int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
237 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "callchaindepth", &pmc_callchaindepth);
238 SYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_TUN|CTLFLAG_RD,
239 &pmc_callchaindepth, 0, "depth of call chain records");
242 struct pmc_debugflags pmc_debugflags = PMC_DEBUG_DEFAULT_FLAGS;
243 char pmc_debugstr[PMC_DEBUG_STRSIZE];
244 TUNABLE_STR(PMC_SYSCTL_NAME_PREFIX "debugflags", pmc_debugstr,
245 sizeof(pmc_debugstr));
246 SYSCTL_PROC(_kern_hwpmc, OID_AUTO, debugflags,
247 CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_TUN,
248 0, 0, pmc_debugflags_sysctl_handler, "A", "debug flags");
252 * kern.hwpmc.hashrows -- determines the number of rows in the
253 * of the hash table used to look up threads
256 static int pmc_hashsize = PMC_HASH_SIZE;
257 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "hashsize", &pmc_hashsize);
258 SYSCTL_INT(_kern_hwpmc, OID_AUTO, hashsize, CTLFLAG_TUN|CTLFLAG_RD,
259 &pmc_hashsize, 0, "rows in hash tables");
262 * kern.hwpmc.nsamples --- number of PC samples/callchain stacks per CPU
265 static int pmc_nsamples = PMC_NSAMPLES;
266 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nsamples", &pmc_nsamples);
267 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nsamples, CTLFLAG_TUN|CTLFLAG_RD,
268 &pmc_nsamples, 0, "number of PC samples per CPU");
272 * kern.hwpmc.mtxpoolsize -- number of mutexes in the mutex pool.
275 static int pmc_mtxpool_size = PMC_MTXPOOL_SIZE;
276 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "mtxpoolsize", &pmc_mtxpool_size);
277 SYSCTL_INT(_kern_hwpmc, OID_AUTO, mtxpoolsize, CTLFLAG_TUN|CTLFLAG_RD,
278 &pmc_mtxpool_size, 0, "size of spin mutex pool");
282 * security.bsd.unprivileged_syspmcs -- allow non-root processes to
283 * allocate system-wide PMCs.
285 * Allowing unprivileged processes to allocate system PMCs is convenient
286 * if system-wide measurements need to be taken concurrently with other
287 * per-process measurements. This feature is turned off by default.
290 static int pmc_unprivileged_syspmcs = 0;
291 TUNABLE_INT("security.bsd.unprivileged_syspmcs", &pmc_unprivileged_syspmcs);
292 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_syspmcs, CTLFLAG_RW,
293 &pmc_unprivileged_syspmcs, 0,
294 "allow unprivileged process to allocate system PMCs");
297 * Hash function. Discard the lower 2 bits of the pointer since
298 * these are always zero for our uses. The hash multiplier is
299 * round((2^LONG_BIT) * ((sqrt(5)-1)/2)).
303 #define _PMC_HM 11400714819323198486u
305 #define _PMC_HM 2654435769u
307 #error Must know the size of 'long' to compile
310 #define PMC_HASH_PTR(P,M) ((((unsigned long) (P) >> 2) * _PMC_HM) & (M))
316 /* The `sysent' for the new syscall */
317 static struct sysent pmc_sysent = {
319 pmc_syscall_handler /* sy_call */
322 static struct syscall_module_data pmc_syscall_mod = {
330 static moduledata_t pmc_mod = {
332 syscall_module_handler,
336 DECLARE_MODULE(pmc, pmc_mod, SI_SUB_SMP, SI_ORDER_ANY);
337 MODULE_VERSION(pmc, PMC_VERSION);
340 enum pmc_dbgparse_state {
341 PMCDS_WS, /* in whitespace */
342 PMCDS_MAJOR, /* seen a major keyword */
347 pmc_debugflags_parse(char *newstr, char *fence)
350 struct pmc_debugflags *tmpflags;
351 int error, found, *newbits, tmp;
354 tmpflags = malloc(sizeof(*tmpflags), M_PMC, M_WAITOK|M_ZERO);
359 for (; p < fence && (c = *p); p++) {
361 /* skip white space */
362 if (c == ' ' || c == '\t')
365 /* look for a keyword followed by "=" */
366 for (q = p; p < fence && (c = *p) && c != '='; p++)
376 /* lookup flag group name */
377 #define DBG_SET_FLAG_MAJ(S,F) \
378 if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0) \
379 newbits = &tmpflags->pdb_ ## F;
381 DBG_SET_FLAG_MAJ("cpu", CPU);
382 DBG_SET_FLAG_MAJ("csw", CSW);
383 DBG_SET_FLAG_MAJ("logging", LOG);
384 DBG_SET_FLAG_MAJ("module", MOD);
385 DBG_SET_FLAG_MAJ("md", MDP);
386 DBG_SET_FLAG_MAJ("owner", OWN);
387 DBG_SET_FLAG_MAJ("pmc", PMC);
388 DBG_SET_FLAG_MAJ("process", PRC);
389 DBG_SET_FLAG_MAJ("sampling", SAM);
391 if (newbits == NULL) {
396 p++; /* skip the '=' */
398 /* Now parse the individual flags */
401 for (q = p; p < fence && (c = *p); p++)
402 if (c == ' ' || c == '\t' || c == ',')
405 /* p == fence or c == ws or c == "," or c == 0 */
407 if ((kwlen = p - q) == 0) {
413 #define DBG_SET_FLAG_MIN(S,F) \
414 if (kwlen == sizeof(S)-1 && strncmp(q, S, kwlen) == 0) \
415 tmp |= found = (1 << PMC_DEBUG_MIN_ ## F)
417 /* a '*' denotes all possible flags in the group */
418 if (kwlen == 1 && *q == '*')
420 /* look for individual flag names */
421 DBG_SET_FLAG_MIN("allocaterow", ALR);
422 DBG_SET_FLAG_MIN("allocate", ALL);
423 DBG_SET_FLAG_MIN("attach", ATT);
424 DBG_SET_FLAG_MIN("bind", BND);
425 DBG_SET_FLAG_MIN("config", CFG);
426 DBG_SET_FLAG_MIN("exec", EXC);
427 DBG_SET_FLAG_MIN("exit", EXT);
428 DBG_SET_FLAG_MIN("find", FND);
429 DBG_SET_FLAG_MIN("flush", FLS);
430 DBG_SET_FLAG_MIN("fork", FRK);
431 DBG_SET_FLAG_MIN("getbuf", GTB);
432 DBG_SET_FLAG_MIN("hook", PMH);
433 DBG_SET_FLAG_MIN("init", INI);
434 DBG_SET_FLAG_MIN("intr", INT);
435 DBG_SET_FLAG_MIN("linktarget", TLK);
436 DBG_SET_FLAG_MIN("mayberemove", OMR);
437 DBG_SET_FLAG_MIN("ops", OPS);
438 DBG_SET_FLAG_MIN("read", REA);
439 DBG_SET_FLAG_MIN("register", REG);
440 DBG_SET_FLAG_MIN("release", REL);
441 DBG_SET_FLAG_MIN("remove", ORM);
442 DBG_SET_FLAG_MIN("sample", SAM);
443 DBG_SET_FLAG_MIN("scheduleio", SIO);
444 DBG_SET_FLAG_MIN("select", SEL);
445 DBG_SET_FLAG_MIN("signal", SIG);
446 DBG_SET_FLAG_MIN("swi", SWI);
447 DBG_SET_FLAG_MIN("swo", SWO);
448 DBG_SET_FLAG_MIN("start", STA);
449 DBG_SET_FLAG_MIN("stop", STO);
450 DBG_SET_FLAG_MIN("syscall", PMS);
451 DBG_SET_FLAG_MIN("unlinktarget", TUL);
452 DBG_SET_FLAG_MIN("write", WRI);
454 /* unrecognized flag name */
459 if (c == 0 || c == ' ' || c == '\t') { /* end of flag group */
468 /* save the new flag set */
469 bcopy(tmpflags, &pmc_debugflags, sizeof(pmc_debugflags));
472 free(tmpflags, M_PMC);
477 pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS)
479 char *fence, *newstr;
483 (void) arg1; (void) arg2; /* unused parameters */
485 n = sizeof(pmc_debugstr);
486 newstr = malloc(n, M_PMC, M_WAITOK|M_ZERO);
487 (void) strlcpy(newstr, pmc_debugstr, n);
489 error = sysctl_handle_string(oidp, newstr, n, req);
491 /* if there is a new string, parse and copy it */
492 if (error == 0 && req->newptr != NULL) {
493 fence = newstr + (n < req->newlen ? n : req->newlen + 1);
494 if ((error = pmc_debugflags_parse(newstr, fence)) == 0)
495 (void) strlcpy(pmc_debugstr, newstr,
496 sizeof(pmc_debugstr));
506 * Map a row index to a classdep structure and return the adjusted row
507 * index for the PMC class index.
509 static struct pmc_classdep *
510 pmc_ri_to_classdep(struct pmc_mdep *md, int ri, int *adjri)
512 struct pmc_classdep *pcd;
516 KASSERT(ri >= 0 && ri < md->pmd_npmc,
517 ("[pmc,%d] illegal row-index %d", __LINE__, ri));
519 pcd = pmc_rowindex_to_classdep[ri];
522 ("[pmc,%d] ri %d null pcd", __LINE__, ri));
524 *adjri = ri - pcd->pcd_ri;
526 KASSERT(*adjri >= 0 && *adjri < pcd->pcd_num,
527 ("[pmc,%d] adjusted row-index %d", __LINE__, *adjri));
533 * Concurrency Control
535 * The driver manages the following data structures:
537 * - target process descriptors, one per target process
538 * - owner process descriptors (and attached lists), one per owner process
539 * - lookup hash tables for owner and target processes
540 * - PMC descriptors (and attached lists)
541 * - per-cpu hardware state
542 * - the 'hook' variable through which the kernel calls into
544 * - the machine hardware state (managed by the MD layer)
546 * These data structures are accessed from:
548 * - thread context-switch code
549 * - interrupt handlers (possibly on multiple cpus)
550 * - kernel threads on multiple cpus running on behalf of user
551 * processes doing system calls
552 * - this driver's private kernel threads
554 * = Locks and Locking strategy =
556 * The driver uses four locking strategies for its operation:
558 * - The global SX lock "pmc_sx" is used to protect internal
561 * Calls into the module by syscall() start with this lock being
562 * held in exclusive mode. Depending on the requested operation,
563 * the lock may be downgraded to 'shared' mode to allow more
564 * concurrent readers into the module. Calls into the module from
565 * other parts of the kernel acquire the lock in shared mode.
567 * This SX lock is held in exclusive mode for any operations that
568 * modify the linkages between the driver's internal data structures.
570 * The 'pmc_hook' function pointer is also protected by this lock.
571 * It is only examined with the sx lock held in exclusive mode. The
572 * kernel module is allowed to be unloaded only with the sx lock held
573 * in exclusive mode. In normal syscall handling, after acquiring the
574 * pmc_sx lock we first check that 'pmc_hook' is non-null before
575 * proceeding. This prevents races between the thread unloading the module
576 * and other threads seeking to use the module.
578 * - Lookups of target process structures and owner process structures
579 * cannot use the global "pmc_sx" SX lock because these lookups need
580 * to happen during context switches and in other critical sections
581 * where sleeping is not allowed. We protect these lookup tables
582 * with their own private spin-mutexes, "pmc_processhash_mtx" and
583 * "pmc_ownerhash_mtx".
585 * - Interrupt handlers work in a lock free manner. At interrupt
586 * time, handlers look at the PMC pointer (phw->phw_pmc) configured
587 * when the PMC was started. If this pointer is NULL, the interrupt
588 * is ignored after updating driver statistics. We ensure that this
589 * pointer is set (using an atomic operation if necessary) before the
590 * PMC hardware is started. Conversely, this pointer is unset atomically
591 * only after the PMC hardware is stopped.
593 * We ensure that everything needed for the operation of an
594 * interrupt handler is available without it needing to acquire any
595 * locks. We also ensure that a PMC's software state is destroyed only
596 * after the PMC is taken off hardware (on all CPUs).
598 * - Context-switch handling with process-private PMCs needs more
601 * A given process may be the target of multiple PMCs. For example,
602 * PMCATTACH and PMCDETACH may be requested by a process on one CPU
603 * while the target process is running on another. A PMC could also
604 * be getting released because its owner is exiting. We tackle
605 * these situations in the following manner:
607 * - each target process structure 'pmc_process' has an array
608 * of 'struct pmc *' pointers, one for each hardware PMC.
610 * - At context switch IN time, each "target" PMC in RUNNING state
611 * gets started on hardware and a pointer to each PMC is copied into
612 * the per-cpu phw array. The 'runcount' for the PMC is
615 * - At context switch OUT time, all process-virtual PMCs are stopped
616 * on hardware. The saved value is added to the PMCs value field
617 * only if the PMC is in a non-deleted state (the PMCs state could
618 * have changed during the current time slice).
620 * Note that since in-between a switch IN on a processor and a switch
621 * OUT, the PMC could have been released on another CPU. Therefore
622 * context switch OUT always looks at the hardware state to turn
623 * OFF PMCs and will update a PMC's saved value only if reachable
624 * from the target process record.
626 * - OP PMCRELEASE could be called on a PMC at any time (the PMC could
627 * be attached to many processes at the time of the call and could
628 * be active on multiple CPUs).
630 * We prevent further scheduling of the PMC by marking it as in
631 * state 'DELETED'. If the runcount of the PMC is non-zero then
632 * this PMC is currently running on a CPU somewhere. The thread
633 * doing the PMCRELEASE operation waits by repeatedly doing a
634 * pause() till the runcount comes to zero.
636 * The contents of a PMC descriptor (struct pmc) are protected using
637 * a spin-mutex. In order to save space, we use a mutex pool.
639 * In terms of lock types used by witness(4), we use:
640 * - Type "pmc-sx", used by the global SX lock.
641 * - Type "pmc-sleep", for sleep mutexes used by logger threads.
642 * - Type "pmc-per-proc", for protecting PMC owner descriptors.
643 * - Type "pmc-leaf", used for all other spin mutexes.
647 * save the cpu binding of the current kthread
651 pmc_save_cpu_binding(struct pmc_binding *pb)
653 PMCDBG(CPU,BND,2, "%s", "save-cpu");
654 thread_lock(curthread);
655 pb->pb_bound = sched_is_bound(curthread);
656 pb->pb_cpu = curthread->td_oncpu;
657 thread_unlock(curthread);
658 PMCDBG(CPU,BND,2, "save-cpu cpu=%d", pb->pb_cpu);
662 * restore the cpu binding of the current thread
666 pmc_restore_cpu_binding(struct pmc_binding *pb)
668 PMCDBG(CPU,BND,2, "restore-cpu curcpu=%d restore=%d",
669 curthread->td_oncpu, pb->pb_cpu);
670 thread_lock(curthread);
672 sched_bind(curthread, pb->pb_cpu);
674 sched_unbind(curthread);
675 thread_unlock(curthread);
676 PMCDBG(CPU,BND,2, "%s", "restore-cpu done");
680 * move execution over the specified cpu and bind it there.
684 pmc_select_cpu(int cpu)
686 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
687 ("[pmc,%d] bad cpu number %d", __LINE__, cpu));
689 /* Never move to an inactive CPU. */
690 KASSERT(pmc_cpu_is_active(cpu), ("[pmc,%d] selecting inactive "
691 "CPU %d", __LINE__, cpu));
693 PMCDBG(CPU,SEL,2, "select-cpu cpu=%d", cpu);
694 thread_lock(curthread);
695 sched_bind(curthread, cpu);
696 thread_unlock(curthread);
698 KASSERT(curthread->td_oncpu == cpu,
699 ("[pmc,%d] CPU not bound [cpu=%d, curr=%d]", __LINE__,
700 cpu, curthread->td_oncpu));
702 PMCDBG(CPU,SEL,2, "select-cpu cpu=%d ok", cpu);
706 * Force a context switch.
708 * We do this by pause'ing for 1 tick -- invoking mi_switch() is not
709 * guaranteed to force a context switch.
713 pmc_force_context_switch(void)
720 * Get the file name for an executable. This is a simple wrapper
721 * around vn_fullpath(9).
725 pmc_getfilename(struct vnode *v, char **fullpath, char **freepath)
728 *fullpath = "unknown";
730 vn_fullpath(curthread, v, fullpath, freepath);
734 * remove an process owning PMCs
738 pmc_remove_owner(struct pmc_owner *po)
740 struct pmc *pm, *tmp;
742 sx_assert(&pmc_sx, SX_XLOCKED);
744 PMCDBG(OWN,ORM,1, "remove-owner po=%p", po);
746 /* Remove descriptor from the owner hash table */
747 LIST_REMOVE(po, po_next);
749 /* release all owned PMC descriptors */
750 LIST_FOREACH_SAFE(pm, &po->po_pmcs, pm_next, tmp) {
751 PMCDBG(OWN,ORM,2, "pmc=%p", pm);
752 KASSERT(pm->pm_owner == po,
753 ("[pmc,%d] owner %p != po %p", __LINE__, pm->pm_owner, po));
755 pmc_release_pmc_descriptor(pm); /* will unlink from the list */
758 KASSERT(po->po_sscount == 0,
759 ("[pmc,%d] SS count not zero", __LINE__));
760 KASSERT(LIST_EMPTY(&po->po_pmcs),
761 ("[pmc,%d] PMC list not empty", __LINE__));
763 /* de-configure the log file if present */
764 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
765 pmclog_deconfigure_log(po);
769 * remove an owner process record if all conditions are met.
773 pmc_maybe_remove_owner(struct pmc_owner *po)
776 PMCDBG(OWN,OMR,1, "maybe-remove-owner po=%p", po);
779 * Remove owner record if
780 * - this process does not own any PMCs
781 * - this process has not allocated a system-wide sampling buffer
784 if (LIST_EMPTY(&po->po_pmcs) &&
785 ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)) {
786 pmc_remove_owner(po);
787 pmc_destroy_owner_descriptor(po);
792 * Add an association between a target process and a PMC.
796 pmc_link_target_process(struct pmc *pm, struct pmc_process *pp)
799 struct pmc_target *pt;
801 sx_assert(&pmc_sx, SX_XLOCKED);
803 KASSERT(pm != NULL && pp != NULL,
804 ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
805 KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
806 ("[pmc,%d] Attaching a non-process-virtual pmc=%p to pid=%d",
807 __LINE__, pm, pp->pp_proc->p_pid));
808 KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= ((int) md->pmd_npmc - 1),
809 ("[pmc,%d] Illegal reference count %d for process record %p",
810 __LINE__, pp->pp_refcnt, (void *) pp));
812 ri = PMC_TO_ROWINDEX(pm);
814 PMCDBG(PRC,TLK,1, "link-target pmc=%p ri=%d pmc-process=%p",
818 LIST_FOREACH(pt, &pm->pm_targets, pt_next)
819 if (pt->pt_process == pp)
820 KASSERT(0, ("[pmc,%d] pp %p already in pmc %p targets",
824 pt = malloc(sizeof(struct pmc_target), M_PMC, M_WAITOK|M_ZERO);
827 LIST_INSERT_HEAD(&pm->pm_targets, pt, pt_next);
829 atomic_store_rel_ptr((uintptr_t *)&pp->pp_pmcs[ri].pp_pmc,
832 if (pm->pm_owner->po_owner == pp->pp_proc)
833 pm->pm_flags |= PMC_F_ATTACHED_TO_OWNER;
836 * Initialize the per-process values at this row index.
838 pp->pp_pmcs[ri].pp_pmcval = PMC_TO_MODE(pm) == PMC_MODE_TS ?
839 pm->pm_sc.pm_reloadcount : 0;
846 * Removes the association between a target process and a PMC.
850 pmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
854 struct pmc_target *ptgt;
856 sx_assert(&pmc_sx, SX_XLOCKED);
858 KASSERT(pm != NULL && pp != NULL,
859 ("[pmc,%d] Null pm %p or pp %p", __LINE__, pm, pp));
861 KASSERT(pp->pp_refcnt >= 1 && pp->pp_refcnt <= (int) md->pmd_npmc,
862 ("[pmc,%d] Illegal ref count %d on process record %p",
863 __LINE__, pp->pp_refcnt, (void *) pp));
865 ri = PMC_TO_ROWINDEX(pm);
867 PMCDBG(PRC,TUL,1, "unlink-target pmc=%p ri=%d pmc-process=%p",
870 KASSERT(pp->pp_pmcs[ri].pp_pmc == pm,
871 ("[pmc,%d] PMC ri %d mismatch pmc %p pp->[ri] %p", __LINE__,
872 ri, pm, pp->pp_pmcs[ri].pp_pmc));
874 pp->pp_pmcs[ri].pp_pmc = NULL;
875 pp->pp_pmcs[ri].pp_pmcval = (pmc_value_t) 0;
877 /* Remove owner-specific flags */
878 if (pm->pm_owner->po_owner == pp->pp_proc) {
879 pp->pp_flags &= ~PMC_PP_ENABLE_MSR_ACCESS;
880 pm->pm_flags &= ~PMC_F_ATTACHED_TO_OWNER;
885 /* Remove the target process from the PMC structure */
886 LIST_FOREACH(ptgt, &pm->pm_targets, pt_next)
887 if (ptgt->pt_process == pp)
890 KASSERT(ptgt != NULL, ("[pmc,%d] process %p (pp: %p) not found "
891 "in pmc %p", __LINE__, pp->pp_proc, pp, pm));
893 LIST_REMOVE(ptgt, pt_next);
896 /* if the PMC now lacks targets, send the owner a SIGIO */
897 if (LIST_EMPTY(&pm->pm_targets)) {
898 p = pm->pm_owner->po_owner;
900 kern_psignal(p, SIGIO);
903 PMCDBG(PRC,SIG,2, "signalling proc=%p signal=%d", p,
909 * Check if PMC 'pm' may be attached to target process 't'.
913 pmc_can_attach(struct pmc *pm, struct proc *t)
915 struct proc *o; /* pmc owner */
916 struct ucred *oc, *tc; /* owner, target credentials */
917 int decline_attach, i;
920 * A PMC's owner can always attach that PMC to itself.
923 if ((o = pm->pm_owner->po_owner) == t)
937 * The effective uid of the PMC owner should match at least one
938 * of the {effective,real,saved} uids of the target process.
941 decline_attach = oc->cr_uid != tc->cr_uid &&
942 oc->cr_uid != tc->cr_svuid &&
943 oc->cr_uid != tc->cr_ruid;
946 * Every one of the target's group ids, must be in the owner's
949 for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
950 decline_attach = !groupmember(tc->cr_groups[i], oc);
952 /* check the read and saved gids too */
953 if (decline_attach == 0)
954 decline_attach = !groupmember(tc->cr_rgid, oc) ||
955 !groupmember(tc->cr_svgid, oc);
960 return !decline_attach;
964 * Attach a process to a PMC.
968 pmc_attach_one_process(struct proc *p, struct pmc *pm)
971 char *fullpath, *freepath;
972 struct pmc_process *pp;
974 sx_assert(&pmc_sx, SX_XLOCKED);
976 PMCDBG(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
977 PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
980 * Locate the process descriptor corresponding to process 'p',
981 * allocating space as needed.
983 * Verify that rowindex 'pm_rowindex' is free in the process
986 * If not, allocate space for a descriptor and link the
987 * process descriptor and PMC.
989 ri = PMC_TO_ROWINDEX(pm);
991 if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
994 if (pp->pp_pmcs[ri].pp_pmc == pm) /* already present at slot [ri] */
997 if (pp->pp_pmcs[ri].pp_pmc != NULL)
1000 pmc_link_target_process(pm, pp);
1002 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
1003 (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
1004 pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
1006 pm->pm_flags |= PMC_F_ATTACH_DONE; /* mark as attached */
1008 /* issue an attach event to a configured log file */
1009 if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
1010 pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1011 if (p->p_flag & P_KTHREAD) {
1012 fullpath = kernelname;
1015 pmclog_process_pmcattach(pm, p->p_pid, fullpath);
1017 free(freepath, M_TEMP);
1018 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1019 pmc_log_process_mappings(pm->pm_owner, p);
1021 /* mark process as using HWPMCs */
1023 p->p_flag |= P_HWPMC;
1030 * Attach a process and optionally its children
1034 pmc_attach_process(struct proc *p, struct pmc *pm)
1039 sx_assert(&pmc_sx, SX_XLOCKED);
1041 PMCDBG(PRC,ATT,1, "attach pm=%p ri=%d proc=%p (%d, %s)", pm,
1042 PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1046 * If this PMC successfully allowed a GETMSR operation
1047 * in the past, disallow further ATTACHes.
1050 if ((pm->pm_flags & PMC_PP_ENABLE_MSR_ACCESS) != 0)
1053 if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1054 return pmc_attach_one_process(p, pm);
1057 * Traverse all child processes, attaching them to
1061 sx_slock(&proctree_lock);
1066 if ((error = pmc_attach_one_process(p, pm)) != 0)
1068 if (!LIST_EMPTY(&p->p_children))
1069 p = LIST_FIRST(&p->p_children);
1073 if (LIST_NEXT(p, p_sibling)) {
1074 p = LIST_NEXT(p, p_sibling);
1082 (void) pmc_detach_process(top, pm);
1085 sx_sunlock(&proctree_lock);
1090 * Detach a process from a PMC. If there are no other PMCs tracking
1091 * this process, remove the process structure from its hash table. If
1092 * 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
1096 pmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
1099 struct pmc_process *pp;
1101 sx_assert(&pmc_sx, SX_XLOCKED);
1104 ("[pmc,%d] null pm pointer", __LINE__));
1106 ri = PMC_TO_ROWINDEX(pm);
1108 PMCDBG(PRC,ATT,2, "detach-one pm=%p ri=%d proc=%p (%d, %s) flags=0x%x",
1109 pm, ri, p, p->p_pid, p->p_comm, flags);
1111 if ((pp = pmc_find_process_descriptor(p, 0)) == NULL)
1114 if (pp->pp_pmcs[ri].pp_pmc != pm)
1117 pmc_unlink_target_process(pm, pp);
1119 /* Issue a detach entry if a log file is configured */
1120 if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
1121 pmclog_process_pmcdetach(pm, p->p_pid);
1124 * If there are no PMCs targetting this process, we remove its
1125 * descriptor from the target hash table and unset the P_HWPMC
1126 * flag in the struct proc.
1128 KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
1129 ("[pmc,%d] Illegal refcnt %d for process struct %p",
1130 __LINE__, pp->pp_refcnt, pp));
1132 if (pp->pp_refcnt != 0) /* still a target of some PMC */
1135 pmc_remove_process_descriptor(pp);
1137 if (flags & PMC_FLAG_REMOVE)
1141 p->p_flag &= ~P_HWPMC;
1148 * Detach a process and optionally its descendants from a PMC.
1152 pmc_detach_process(struct proc *p, struct pmc *pm)
1156 sx_assert(&pmc_sx, SX_XLOCKED);
1158 PMCDBG(PRC,ATT,1, "detach pm=%p ri=%d proc=%p (%d, %s)", pm,
1159 PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
1161 if ((pm->pm_flags & PMC_F_DESCENDANTS) == 0)
1162 return pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1165 * Traverse all children, detaching them from this PMC. We
1166 * ignore errors since we could be detaching a PMC from a
1167 * partially attached proc tree.
1170 sx_slock(&proctree_lock);
1175 (void) pmc_detach_one_process(p, pm, PMC_FLAG_REMOVE);
1177 if (!LIST_EMPTY(&p->p_children))
1178 p = LIST_FIRST(&p->p_children);
1182 if (LIST_NEXT(p, p_sibling)) {
1183 p = LIST_NEXT(p, p_sibling);
1191 sx_sunlock(&proctree_lock);
1193 if (LIST_EMPTY(&pm->pm_targets))
1194 pm->pm_flags &= ~PMC_F_ATTACH_DONE;
1201 * Thread context switch IN
1205 pmc_process_csw_in(struct thread *td)
1208 unsigned int adjri, ri;
1213 pmc_value_t newvalue;
1214 struct pmc_process *pp;
1215 struct pmc_classdep *pcd;
1219 if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE)) == NULL)
1222 KASSERT(pp->pp_proc == td->td_proc,
1223 ("[pmc,%d] not my thread state", __LINE__));
1225 critical_enter(); /* no preemption from this point */
1227 cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1229 PMCDBG(CSW,SWI,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1230 p->p_pid, p->p_comm, pp);
1232 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1233 ("[pmc,%d] wierd CPU id %d", __LINE__, cpu));
1237 for (ri = 0; ri < md->pmd_npmc; ri++) {
1239 if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL)
1242 KASSERT(PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)),
1243 ("[pmc,%d] Target PMC in non-virtual mode (%d)",
1244 __LINE__, PMC_TO_MODE(pm)));
1246 KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1247 ("[pmc,%d] Row index mismatch pmc %d != ri %d",
1248 __LINE__, PMC_TO_ROWINDEX(pm), ri));
1251 * Only PMCs that are marked as 'RUNNING' need
1252 * be placed on hardware.
1255 if (pm->pm_state != PMC_STATE_RUNNING)
1258 /* increment PMC runcount */
1259 atomic_add_rel_int(&pm->pm_runcount, 1);
1261 /* configure the HWPMC we are going to use. */
1262 pcd = pmc_ri_to_classdep(md, ri, &adjri);
1263 pcd->pcd_config_pmc(cpu, adjri, pm);
1265 phw = pc->pc_hwpmcs[ri];
1267 KASSERT(phw != NULL,
1268 ("[pmc,%d] null hw pointer", __LINE__));
1270 KASSERT(phw->phw_pmc == pm,
1271 ("[pmc,%d] hw->pmc %p != pmc %p", __LINE__,
1275 * Write out saved value and start the PMC.
1277 * Sampling PMCs use a per-process value, while
1278 * counting mode PMCs use a per-pmc value that is
1279 * inherited across descendants.
1281 if (PMC_TO_MODE(pm) == PMC_MODE_TS) {
1282 mtx_pool_lock_spin(pmc_mtxpool, pm);
1283 newvalue = PMC_PCPU_SAVED(cpu,ri) =
1284 pp->pp_pmcs[ri].pp_pmcval;
1285 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1287 KASSERT(PMC_TO_MODE(pm) == PMC_MODE_TC,
1288 ("[pmc,%d] illegal mode=%d", __LINE__,
1290 mtx_pool_lock_spin(pmc_mtxpool, pm);
1291 newvalue = PMC_PCPU_SAVED(cpu, ri) =
1292 pm->pm_gv.pm_savedvalue;
1293 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1296 PMCDBG(CSW,SWI,1,"cpu=%d ri=%d new=%jd", cpu, ri, newvalue);
1298 pcd->pcd_write_pmc(cpu, adjri, newvalue);
1299 pcd->pcd_start_pmc(cpu, adjri);
1303 * perform any other architecture/cpu dependent thread
1304 * switch-in actions.
1307 (void) (*md->pmd_switch_in)(pc, pp);
1314 * Thread context switch OUT.
1318 pmc_process_csw_out(struct thread *td)
1326 pmc_value_t newvalue;
1327 unsigned int adjri, ri;
1328 struct pmc_process *pp;
1329 struct pmc_classdep *pcd;
1333 * Locate our process descriptor; this may be NULL if
1334 * this process is exiting and we have already removed
1335 * the process from the target process table.
1337 * Note that due to kernel preemption, multiple
1338 * context switches may happen while the process is
1341 * Note also that if the target process cannot be
1342 * found we still need to deconfigure any PMCs that
1343 * are currently running on hardware.
1347 pp = pmc_find_process_descriptor(p, PMC_FLAG_NONE);
1355 cpu = PCPU_GET(cpuid); /* td->td_oncpu is invalid */
1357 PMCDBG(CSW,SWO,1, "cpu=%d proc=%p (%d, %s) pp=%p", cpu, p,
1358 p->p_pid, p->p_comm, pp);
1360 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
1361 ("[pmc,%d wierd CPU id %d", __LINE__, cpu));
1366 * When a PMC gets unlinked from a target PMC, it will
1367 * be removed from the target's pp_pmc[] array.
1369 * However, on a MP system, the target could have been
1370 * executing on another CPU at the time of the unlink.
1371 * So, at context switch OUT time, we need to look at
1372 * the hardware to determine if a PMC is scheduled on
1376 for (ri = 0; ri < md->pmd_npmc; ri++) {
1378 pcd = pmc_ri_to_classdep(md, ri, &adjri);
1380 (void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
1382 if (pm == NULL) /* nothing at this row index */
1385 mode = PMC_TO_MODE(pm);
1386 if (!PMC_IS_VIRTUAL_MODE(mode))
1387 continue; /* not a process virtual PMC */
1389 KASSERT(PMC_TO_ROWINDEX(pm) == ri,
1390 ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
1391 __LINE__, PMC_TO_ROWINDEX(pm), ri));
1393 /* Stop hardware if not already stopped */
1394 if (pm->pm_stalled == 0)
1395 pcd->pcd_stop_pmc(cpu, adjri);
1397 /* reduce this PMC's runcount */
1398 atomic_subtract_rel_int(&pm->pm_runcount, 1);
1401 * If this PMC is associated with this process,
1405 if (pp != NULL && pp->pp_pmcs[ri].pp_pmc != NULL) {
1407 KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
1408 ("[pmc,%d] pm %p != pp_pmcs[%d] %p", __LINE__,
1409 pm, ri, pp->pp_pmcs[ri].pp_pmc));
1411 KASSERT(pp->pp_refcnt > 0,
1412 ("[pmc,%d] pp refcnt = %d", __LINE__,
1415 pcd->pcd_read_pmc(cpu, adjri, &newvalue);
1417 tmp = newvalue - PMC_PCPU_SAVED(cpu,ri);
1419 PMCDBG(CSW,SWO,1,"cpu=%d ri=%d tmp=%jd", cpu, ri,
1422 if (mode == PMC_MODE_TS) {
1425 * For sampling process-virtual PMCs,
1426 * we expect the count to be
1427 * decreasing as the 'value'
1428 * programmed into the PMC is the
1429 * number of events to be seen till
1430 * the next sampling interrupt.
1433 tmp += pm->pm_sc.pm_reloadcount;
1434 mtx_pool_lock_spin(pmc_mtxpool, pm);
1435 pp->pp_pmcs[ri].pp_pmcval -= tmp;
1436 if ((int64_t) pp->pp_pmcs[ri].pp_pmcval < 0)
1437 pp->pp_pmcs[ri].pp_pmcval +=
1438 pm->pm_sc.pm_reloadcount;
1439 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1444 * For counting process-virtual PMCs,
1445 * we expect the count to be
1446 * increasing monotonically, modulo a 64
1449 KASSERT((int64_t) tmp >= 0,
1450 ("[pmc,%d] negative increment cpu=%d "
1451 "ri=%d newvalue=%jx saved=%jx "
1452 "incr=%jx", __LINE__, cpu, ri,
1453 newvalue, PMC_PCPU_SAVED(cpu,ri), tmp));
1455 mtx_pool_lock_spin(pmc_mtxpool, pm);
1456 pm->pm_gv.pm_savedvalue += tmp;
1457 pp->pp_pmcs[ri].pp_pmcval += tmp;
1458 mtx_pool_unlock_spin(pmc_mtxpool, pm);
1460 if (pm->pm_flags & PMC_F_LOG_PROCCSW)
1461 pmclog_process_proccsw(pm, pp, tmp);
1465 /* mark hardware as free */
1466 pcd->pcd_config_pmc(cpu, adjri, NULL);
1470 * perform any other architecture/cpu dependent thread
1471 * switch out functions.
1474 (void) (*md->pmd_switch_out)(pc, pp);
1480 * A mapping change for a process.
1484 pmc_process_mmap(struct thread *td, struct pmckern_map_in *pkm)
1488 char *fullpath, *freepath;
1489 const struct pmc *pm;
1490 struct pmc_owner *po;
1491 const struct pmc_process *pp;
1493 freepath = fullpath = NULL;
1494 pmc_getfilename((struct vnode *) pkm->pm_file, &fullpath, &freepath);
1496 pid = td->td_proc->p_pid;
1498 /* Inform owners of all system-wide sampling PMCs. */
1499 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1500 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1501 pmclog_process_map_in(po, pid, pkm->pm_address, fullpath);
1503 if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1507 * Inform sampling PMC owners tracking this process.
1509 for (ri = 0; ri < md->pmd_npmc; ri++)
1510 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1511 PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1512 pmclog_process_map_in(pm->pm_owner,
1513 pid, pkm->pm_address, fullpath);
1517 free(freepath, M_TEMP);
1522 * Log an munmap request.
1526 pmc_process_munmap(struct thread *td, struct pmckern_map_out *pkm)
1530 struct pmc_owner *po;
1531 const struct pmc *pm;
1532 const struct pmc_process *pp;
1534 pid = td->td_proc->p_pid;
1536 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1537 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1538 pmclog_process_map_out(po, pid, pkm->pm_address,
1539 pkm->pm_address + pkm->pm_size);
1541 if ((pp = pmc_find_process_descriptor(td->td_proc, 0)) == NULL)
1544 for (ri = 0; ri < md->pmd_npmc; ri++)
1545 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL &&
1546 PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1547 pmclog_process_map_out(pm->pm_owner, pid,
1548 pkm->pm_address, pkm->pm_address + pkm->pm_size);
1552 * Log mapping information about the kernel.
1556 pmc_log_kernel_mappings(struct pmc *pm)
1558 struct pmc_owner *po;
1559 struct pmckern_map_in *km, *kmbase;
1561 sx_assert(&pmc_sx, SX_LOCKED);
1562 KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
1563 ("[pmc,%d] non-sampling PMC (%p) desires mapping information",
1564 __LINE__, (void *) pm));
1568 if (po->po_flags & PMC_PO_INITIAL_MAPPINGS_DONE)
1572 * Log the current set of kernel modules.
1574 kmbase = linker_hwpmc_list_objects();
1575 for (km = kmbase; km->pm_file != NULL; km++) {
1576 PMCDBG(LOG,REG,1,"%s %p", (char *) km->pm_file,
1577 (void *) km->pm_address);
1578 pmclog_process_map_in(po, (pid_t) -1, km->pm_address,
1581 free(kmbase, M_LINKER);
1583 po->po_flags |= PMC_PO_INITIAL_MAPPINGS_DONE;
1587 * Log the mappings for a single process.
1591 pmc_log_process_mappings(struct pmc_owner *po, struct proc *p)
1596 vm_map_entry_t entry;
1597 vm_offset_t last_end;
1598 u_int last_timestamp;
1599 struct vnode *last_vp;
1600 vm_offset_t start_addr;
1601 vm_object_t obj, lobj, tobj;
1602 char *fullpath, *freepath;
1605 last_end = (vm_offset_t) 0;
1606 fullpath = freepath = NULL;
1608 if ((vm = vmspace_acquire_ref(p)) == NULL)
1612 vm_map_lock_read(map);
1614 for (entry = map->header.next; entry != &map->header; entry = entry->next) {
1616 if (entry == NULL) {
1617 PMCDBG(LOG,OPS,2, "hwpmc: vm_map entry unexpectedly "
1618 "NULL! pid=%d vm_map=%p\n", p->p_pid, map);
1623 * We only care about executable map entries.
1625 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
1626 !(entry->protection & VM_PROT_EXECUTE) ||
1627 (entry->object.vm_object == NULL)) {
1631 obj = entry->object.vm_object;
1632 VM_OBJECT_RLOCK(obj);
1635 * Walk the backing_object list to find the base
1636 * (non-shadowed) vm_object.
1638 for (lobj = tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
1640 VM_OBJECT_RLOCK(tobj);
1642 VM_OBJECT_RUNLOCK(lobj);
1647 * At this point lobj is the base vm_object and it is locked.
1650 PMCDBG(LOG,OPS,2, "hwpmc: lobj unexpectedly NULL! pid=%d "
1651 "vm_map=%p vm_obj=%p\n", p->p_pid, map, obj);
1652 VM_OBJECT_RUNLOCK(obj);
1656 if (lobj->type != OBJT_VNODE || lobj->handle == NULL) {
1658 VM_OBJECT_RUNLOCK(lobj);
1659 VM_OBJECT_RUNLOCK(obj);
1664 * Skip contiguous regions that point to the same
1665 * vnode, so we don't emit redundant MAP-IN
1668 if (entry->start == last_end && lobj->handle == last_vp) {
1669 last_end = entry->end;
1671 VM_OBJECT_RUNLOCK(lobj);
1672 VM_OBJECT_RUNLOCK(obj);
1677 * We don't want to keep the proc's vm_map or this
1678 * vm_object locked while we walk the pathname, since
1679 * vn_fullpath() can sleep. However, if we drop the
1680 * lock, it's possible for concurrent activity to
1681 * modify the vm_map list. To protect against this,
1682 * we save the vm_map timestamp before we release the
1683 * lock, and check it after we reacquire the lock
1686 start_addr = entry->start;
1687 last_end = entry->end;
1688 last_timestamp = map->timestamp;
1689 vm_map_unlock_read(map);
1694 VM_OBJECT_RUNLOCK(lobj);
1696 VM_OBJECT_RUNLOCK(obj);
1699 pmc_getfilename(vp, &fullpath, &freepath);
1705 pmclog_process_map_in(po, p->p_pid, start_addr, fullpath);
1707 free(freepath, M_TEMP);
1709 vm_map_lock_read(map);
1712 * If our saved timestamp doesn't match, this means
1713 * that the vm_map was modified out from under us and
1714 * we can't trust our current "entry" pointer. Do a
1715 * new lookup for this entry. If there is no entry
1716 * for this address range, vm_map_lookup_entry() will
1717 * return the previous one, so we always want to go to
1718 * entry->next on the next loop iteration.
1720 * There is an edge condition here that can occur if
1721 * there is no entry at or before this address. In
1722 * this situation, vm_map_lookup_entry returns
1723 * &map->header, which would cause our loop to abort
1724 * without processing the rest of the map. However,
1725 * in practice this will never happen for process
1726 * vm_map. This is because the executable's text
1727 * segment is the first mapping in the proc's address
1728 * space, and this mapping is never removed until the
1729 * process exits, so there will always be a non-header
1730 * entry at or before the requested address for
1731 * vm_map_lookup_entry to return.
1733 if (map->timestamp != last_timestamp)
1734 vm_map_lookup_entry(map, last_end - 1, &entry);
1737 vm_map_unlock_read(map);
1743 * Log mappings for all processes in the system.
1747 pmc_log_all_process_mappings(struct pmc_owner *po)
1749 struct proc *p, *top;
1751 sx_assert(&pmc_sx, SX_XLOCKED);
1753 if ((p = pfind(1)) == NULL)
1754 panic("[pmc,%d] Cannot find init", __LINE__);
1758 sx_slock(&proctree_lock);
1763 pmc_log_process_mappings(po, p);
1764 if (!LIST_EMPTY(&p->p_children))
1765 p = LIST_FIRST(&p->p_children);
1769 if (LIST_NEXT(p, p_sibling)) {
1770 p = LIST_NEXT(p, p_sibling);
1777 sx_sunlock(&proctree_lock);
1781 * The 'hook' invoked from the kernel proper
1786 const char *pmc_hooknames[] = {
1787 /* these strings correspond to PMC_FN_* in <sys/pmckern.h> */
1804 pmc_hook_handler(struct thread *td, int function, void *arg)
1807 PMCDBG(MOD,PMH,1, "hook td=%p func=%d \"%s\" arg=%p", td, function,
1808 pmc_hooknames[function], arg);
1817 case PMC_FN_PROCESS_EXEC:
1819 char *fullpath, *freepath;
1821 int is_using_hwpmcs;
1824 struct pmc_owner *po;
1825 struct pmc_process *pp;
1826 struct pmckern_procexec *pk;
1828 sx_assert(&pmc_sx, SX_XLOCKED);
1831 pmc_getfilename(p->p_textvp, &fullpath, &freepath);
1833 pk = (struct pmckern_procexec *) arg;
1835 /* Inform owners of SS mode PMCs of the exec event. */
1836 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
1837 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
1838 pmclog_process_procexec(po, PMC_ID_INVALID,
1839 p->p_pid, pk->pm_entryaddr, fullpath);
1842 is_using_hwpmcs = p->p_flag & P_HWPMC;
1845 if (!is_using_hwpmcs) {
1847 free(freepath, M_TEMP);
1852 * PMCs are not inherited across an exec(): remove any
1853 * PMCs that this process is the owner of.
1856 if ((po = pmc_find_owner_descriptor(p)) != NULL) {
1857 pmc_remove_owner(po);
1858 pmc_destroy_owner_descriptor(po);
1862 * If the process being exec'ed is not the target of any
1865 if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) {
1867 free(freepath, M_TEMP);
1872 * Log the exec event to all monitoring owners. Skip
1873 * owners who have already recieved the event because
1874 * they had system sampling PMCs active.
1876 for (ri = 0; ri < md->pmd_npmc; ri++)
1877 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
1879 if (po->po_sscount == 0 &&
1880 po->po_flags & PMC_PO_OWNS_LOGFILE)
1881 pmclog_process_procexec(po, pm->pm_id,
1882 p->p_pid, pk->pm_entryaddr,
1887 free(freepath, M_TEMP);
1890 PMCDBG(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d",
1891 p, p->p_pid, p->p_comm, pk->pm_credentialschanged);
1893 if (pk->pm_credentialschanged == 0) /* no change */
1897 * If the newly exec()'ed process has a different credential
1898 * than before, allow it to be the target of a PMC only if
1899 * the PMC's owner has sufficient priviledge.
1902 for (ri = 0; ri < md->pmd_npmc; ri++)
1903 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL)
1904 if (pmc_can_attach(pm, td->td_proc) != 0)
1905 pmc_detach_one_process(td->td_proc,
1908 KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= (int) md->pmd_npmc,
1909 ("[pmc,%d] Illegal ref count %d on pp %p", __LINE__,
1910 pp->pp_refcnt, pp));
1913 * If this process is no longer the target of any
1914 * PMCs, we can remove the process entry and free
1918 if (pp->pp_refcnt == 0) {
1919 pmc_remove_process_descriptor(pp);
1928 pmc_process_csw_in(td);
1931 case PMC_FN_CSW_OUT:
1932 pmc_process_csw_out(td);
1936 * Process accumulated PC samples.
1938 * This function is expected to be called by hardclock() for
1939 * each CPU that has accumulated PC samples.
1941 * This function is to be executed on the CPU whose samples
1942 * are being processed.
1944 case PMC_FN_DO_SAMPLES:
1947 * Clear the cpu specific bit in the CPU mask before
1948 * do the rest of the processing. If the NMI handler
1949 * gets invoked after the "atomic_clear_int()" call
1950 * below but before "pmc_process_samples()" gets
1951 * around to processing the interrupt, then we will
1952 * come back here at the next hardclock() tick (and
1953 * may find nothing to do if "pmc_process_samples()"
1954 * had already processed the interrupt). We don't
1955 * lose the interrupt sample.
1957 CPU_CLR_ATOMIC(PCPU_GET(cpuid), &pmc_cpumask);
1958 pmc_process_samples(PCPU_GET(cpuid), PMC_HR);
1959 pmc_process_samples(PCPU_GET(cpuid), PMC_SR);
1963 sx_assert(&pmc_sx, SX_LOCKED);
1964 pmc_process_mmap(td, (struct pmckern_map_in *) arg);
1968 sx_assert(&pmc_sx, SX_LOCKED);
1969 pmc_process_munmap(td, (struct pmckern_map_out *) arg);
1972 case PMC_FN_USER_CALLCHAIN:
1974 * Record a call chain.
1976 KASSERT(td == curthread, ("[pmc,%d] td != curthread",
1979 pmc_capture_user_callchain(PCPU_GET(cpuid), PMC_HR,
1980 (struct trapframe *) arg);
1981 td->td_pflags &= ~TDP_CALLCHAIN;
1984 case PMC_FN_USER_CALLCHAIN_SOFT:
1986 * Record a call chain.
1988 KASSERT(td == curthread, ("[pmc,%d] td != curthread",
1990 pmc_capture_user_callchain(PCPU_GET(cpuid), PMC_SR,
1991 (struct trapframe *) arg);
1992 td->td_pflags &= ~TDP_CALLCHAIN;
1995 case PMC_FN_SOFT_SAMPLING:
1997 * Call soft PMC sampling intr.
1999 pmc_soft_intr((struct pmckern_soft *) arg);
2004 KASSERT(0, ("[pmc,%d] unknown hook %d\n", __LINE__, function));
2014 * allocate a 'struct pmc_owner' descriptor in the owner hash table.
2017 static struct pmc_owner *
2018 pmc_allocate_owner_descriptor(struct proc *p)
2021 struct pmc_owner *po;
2022 struct pmc_ownerhash *poh;
2024 hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2025 poh = &pmc_ownerhash[hindex];
2027 /* allocate space for N pointers and one descriptor struct */
2028 po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK|M_ZERO);
2030 LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
2032 TAILQ_INIT(&po->po_logbuffers);
2033 mtx_init(&po->po_mtx, "pmc-owner-mtx", "pmc-per-proc", MTX_SPIN);
2035 PMCDBG(OWN,ALL,1, "allocate-owner proc=%p (%d, %s) pmc-owner=%p",
2036 p, p->p_pid, p->p_comm, po);
2042 pmc_destroy_owner_descriptor(struct pmc_owner *po)
2045 PMCDBG(OWN,REL,1, "destroy-owner po=%p proc=%p (%d, %s)",
2046 po, po->po_owner, po->po_owner->p_pid, po->po_owner->p_comm);
2048 mtx_destroy(&po->po_mtx);
2053 * find the descriptor corresponding to process 'p', adding or removing it
2054 * as specified by 'mode'.
2057 static struct pmc_process *
2058 pmc_find_process_descriptor(struct proc *p, uint32_t mode)
2061 struct pmc_process *pp, *ppnew;
2062 struct pmc_processhash *pph;
2064 hindex = PMC_HASH_PTR(p, pmc_processhashmask);
2065 pph = &pmc_processhash[hindex];
2070 * Pre-allocate memory in the FIND_ALLOCATE case since we
2071 * cannot call malloc(9) once we hold a spin lock.
2073 if (mode & PMC_FLAG_ALLOCATE)
2074 ppnew = malloc(sizeof(struct pmc_process) + md->pmd_npmc *
2075 sizeof(struct pmc_targetstate), M_PMC, M_WAITOK|M_ZERO);
2077 mtx_lock_spin(&pmc_processhash_mtx);
2078 LIST_FOREACH(pp, pph, pp_next)
2079 if (pp->pp_proc == p)
2082 if ((mode & PMC_FLAG_REMOVE) && pp != NULL)
2083 LIST_REMOVE(pp, pp_next);
2085 if ((mode & PMC_FLAG_ALLOCATE) && pp == NULL &&
2088 LIST_INSERT_HEAD(pph, ppnew, pp_next);
2092 mtx_unlock_spin(&pmc_processhash_mtx);
2094 if (pp != NULL && ppnew != NULL)
2101 * remove a process descriptor from the process hash table.
2105 pmc_remove_process_descriptor(struct pmc_process *pp)
2107 KASSERT(pp->pp_refcnt == 0,
2108 ("[pmc,%d] Removing process descriptor %p with count %d",
2109 __LINE__, pp, pp->pp_refcnt));
2111 mtx_lock_spin(&pmc_processhash_mtx);
2112 LIST_REMOVE(pp, pp_next);
2113 mtx_unlock_spin(&pmc_processhash_mtx);
2118 * find an owner descriptor corresponding to proc 'p'
2121 static struct pmc_owner *
2122 pmc_find_owner_descriptor(struct proc *p)
2125 struct pmc_owner *po;
2126 struct pmc_ownerhash *poh;
2128 hindex = PMC_HASH_PTR(p, pmc_ownerhashmask);
2129 poh = &pmc_ownerhash[hindex];
2132 LIST_FOREACH(po, poh, po_next)
2133 if (po->po_owner == p)
2136 PMCDBG(OWN,FND,1, "find-owner proc=%p (%d, %s) hindex=0x%x -> "
2137 "pmc-owner=%p", p, p->p_pid, p->p_comm, hindex, po);
2143 * pmc_allocate_pmc_descriptor
2145 * Allocate a pmc descriptor and initialize its
2150 pmc_allocate_pmc_descriptor(void)
2154 pmc = malloc(sizeof(struct pmc), M_PMC, M_WAITOK|M_ZERO);
2156 PMCDBG(PMC,ALL,1, "allocate-pmc -> pmc=%p", pmc);
2162 * Destroy a pmc descriptor.
2166 pmc_destroy_pmc_descriptor(struct pmc *pm)
2171 KASSERT(pm->pm_state == PMC_STATE_DELETED ||
2172 pm->pm_state == PMC_STATE_FREE,
2173 ("[pmc,%d] destroying non-deleted PMC", __LINE__));
2174 KASSERT(LIST_EMPTY(&pm->pm_targets),
2175 ("[pmc,%d] destroying pmc with targets", __LINE__));
2176 KASSERT(pm->pm_owner == NULL,
2177 ("[pmc,%d] destroying pmc attached to an owner", __LINE__));
2178 KASSERT(pm->pm_runcount == 0,
2179 ("[pmc,%d] pmc has non-zero run count %d", __LINE__,
2185 pmc_wait_for_pmc_idle(struct pmc *pm)
2188 volatile int maxloop;
2190 maxloop = 100 * pmc_cpu_max();
2193 * Loop (with a forced context switch) till the PMC's runcount
2194 * comes down to zero.
2196 while (atomic_load_acq_32(&pm->pm_runcount) > 0) {
2199 KASSERT(maxloop > 0,
2200 ("[pmc,%d] (ri%d, rc%d) waiting too long for "
2201 "pmc to be free", __LINE__,
2202 PMC_TO_ROWINDEX(pm), pm->pm_runcount));
2204 pmc_force_context_switch();
2209 * This function does the following things:
2211 * - detaches the PMC from hardware
2212 * - unlinks all target threads that were attached to it
2213 * - removes the PMC from its owner's list
2214 * - destroy's the PMC private mutex
2216 * Once this function completes, the given pmc pointer can be safely
2217 * FREE'd by the caller.
2221 pmc_release_pmc_descriptor(struct pmc *pm)
2225 u_int adjri, ri, cpu;
2226 struct pmc_owner *po;
2227 struct pmc_binding pb;
2228 struct pmc_process *pp;
2229 struct pmc_classdep *pcd;
2230 struct pmc_target *ptgt, *tmp;
2232 sx_assert(&pmc_sx, SX_XLOCKED);
2234 KASSERT(pm, ("[pmc,%d] null pmc", __LINE__));
2236 ri = PMC_TO_ROWINDEX(pm);
2237 pcd = pmc_ri_to_classdep(md, ri, &adjri);
2238 mode = PMC_TO_MODE(pm);
2240 PMCDBG(PMC,REL,1, "release-pmc pmc=%p ri=%d mode=%d", pm, ri,
2244 * First, we take the PMC off hardware.
2247 if (PMC_IS_SYSTEM_MODE(mode)) {
2250 * A system mode PMC runs on a specific CPU. Switch
2251 * to this CPU and turn hardware off.
2253 pmc_save_cpu_binding(&pb);
2255 cpu = PMC_TO_CPU(pm);
2257 pmc_select_cpu(cpu);
2259 /* switch off non-stalled CPUs */
2260 if (pm->pm_state == PMC_STATE_RUNNING &&
2261 pm->pm_stalled == 0) {
2263 phw = pmc_pcpu[cpu]->pc_hwpmcs[ri];
2265 KASSERT(phw->phw_pmc == pm,
2266 ("[pmc, %d] pmc ptr ri(%d) hw(%p) pm(%p)",
2267 __LINE__, ri, phw->phw_pmc, pm));
2268 PMCDBG(PMC,REL,2, "stopping cpu=%d ri=%d", cpu, ri);
2271 pcd->pcd_stop_pmc(cpu, adjri);
2275 PMCDBG(PMC,REL,2, "decfg cpu=%d ri=%d", cpu, ri);
2278 pcd->pcd_config_pmc(cpu, adjri, NULL);
2281 /* adjust the global and process count of SS mode PMCs */
2282 if (mode == PMC_MODE_SS && pm->pm_state == PMC_STATE_RUNNING) {
2285 if (po->po_sscount == 0) {
2286 atomic_subtract_rel_int(&pmc_ss_count, 1);
2287 LIST_REMOVE(po, po_ssnext);
2291 pm->pm_state = PMC_STATE_DELETED;
2293 pmc_restore_cpu_binding(&pb);
2296 * We could have references to this PMC structure in
2297 * the per-cpu sample queues. Wait for the queue to
2300 pmc_wait_for_pmc_idle(pm);
2302 } else if (PMC_IS_VIRTUAL_MODE(mode)) {
2305 * A virtual PMC could be running on multiple CPUs at
2308 * By marking its state as DELETED, we ensure that
2309 * this PMC is never further scheduled on hardware.
2311 * Then we wait till all CPUs are done with this PMC.
2313 pm->pm_state = PMC_STATE_DELETED;
2316 /* Wait for the PMCs runcount to come to zero. */
2317 pmc_wait_for_pmc_idle(pm);
2320 * At this point the PMC is off all CPUs and cannot be
2321 * freshly scheduled onto a CPU. It is now safe to
2322 * unlink all targets from this PMC. If a
2323 * process-record's refcount falls to zero, we remove
2324 * it from the hash table. The module-wide SX lock
2325 * protects us from races.
2327 LIST_FOREACH_SAFE(ptgt, &pm->pm_targets, pt_next, tmp) {
2328 pp = ptgt->pt_process;
2329 pmc_unlink_target_process(pm, pp); /* frees 'ptgt' */
2331 PMCDBG(PMC,REL,3, "pp->refcnt=%d", pp->pp_refcnt);
2334 * If the target process record shows that no
2335 * PMCs are attached to it, reclaim its space.
2338 if (pp->pp_refcnt == 0) {
2339 pmc_remove_process_descriptor(pp);
2344 cpu = curthread->td_oncpu; /* setup cpu for pmd_release() */
2349 * Release any MD resources
2351 (void) pcd->pcd_release_pmc(cpu, adjri, pm);
2354 * Update row disposition
2357 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm)))
2358 PMC_UNMARK_ROW_STANDALONE(ri);
2360 PMC_UNMARK_ROW_THREAD(ri);
2362 /* unlink from the owner's list */
2364 LIST_REMOVE(pm, pm_next);
2365 pm->pm_owner = NULL;
2368 pmc_destroy_pmc_descriptor(pm);
2372 * Register an owner and a pmc.
2376 pmc_register_owner(struct proc *p, struct pmc *pmc)
2378 struct pmc_owner *po;
2380 sx_assert(&pmc_sx, SX_XLOCKED);
2382 if ((po = pmc_find_owner_descriptor(p)) == NULL)
2383 if ((po = pmc_allocate_owner_descriptor(p)) == NULL)
2386 KASSERT(pmc->pm_owner == NULL,
2387 ("[pmc,%d] attempting to own an initialized PMC", __LINE__));
2390 LIST_INSERT_HEAD(&po->po_pmcs, pmc, pm_next);
2393 p->p_flag |= P_HWPMC;
2396 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
2397 pmclog_process_pmcallocate(pmc);
2399 PMCDBG(PMC,REG,1, "register-owner pmc-owner=%p pmc=%p",
2406 * Return the current row disposition:
2408 * > 0 => PROCESS MODE
2409 * < 0 => SYSTEM MODE
2413 pmc_getrowdisp(int ri)
2415 return pmc_pmcdisp[ri];
2419 * Check if a PMC at row index 'ri' can be allocated to the current
2422 * Allocation can fail if:
2423 * - the current process is already being profiled by a PMC at index 'ri',
2424 * attached to it via OP_PMCATTACH.
2425 * - the current process has already allocated a PMC at index 'ri'
2430 pmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
2434 struct pmc_owner *po;
2435 struct pmc_process *pp;
2437 PMCDBG(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
2438 "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
2441 * We shouldn't have already allocated a process-mode PMC at
2444 * We shouldn't have allocated a system-wide PMC on the same
2447 if ((po = pmc_find_owner_descriptor(p)) != NULL)
2448 LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
2449 if (PMC_TO_ROWINDEX(pm) == ri) {
2450 mode = PMC_TO_MODE(pm);
2451 if (PMC_IS_VIRTUAL_MODE(mode))
2453 if (PMC_IS_SYSTEM_MODE(mode) &&
2454 (int) PMC_TO_CPU(pm) == cpu)
2460 * We also shouldn't be the target of any PMC at this index
2461 * since otherwise a PMC_ATTACH to ourselves will fail.
2463 if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
2464 if (pp->pp_pmcs[ri].pp_pmc)
2467 PMCDBG(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
2468 p, p->p_pid, p->p_comm, ri);
2474 * Check if a given PMC at row index 'ri' can be currently used in
2479 pmc_can_allocate_row(int ri, enum pmc_mode mode)
2483 sx_assert(&pmc_sx, SX_XLOCKED);
2485 PMCDBG(PMC,ALR,1, "can-allocate-row ri=%d mode=%d", ri, mode);
2487 if (PMC_IS_SYSTEM_MODE(mode))
2488 disp = PMC_DISP_STANDALONE;
2490 disp = PMC_DISP_THREAD;
2493 * check disposition for PMC row 'ri':
2495 * Expected disposition Row-disposition Result
2497 * STANDALONE STANDALONE or FREE proceed
2498 * STANDALONE THREAD fail
2499 * THREAD THREAD or FREE proceed
2500 * THREAD STANDALONE fail
2503 if (!PMC_ROW_DISP_IS_FREE(ri) &&
2504 !(disp == PMC_DISP_THREAD && PMC_ROW_DISP_IS_THREAD(ri)) &&
2505 !(disp == PMC_DISP_STANDALONE && PMC_ROW_DISP_IS_STANDALONE(ri)))
2512 PMCDBG(PMC,ALR,2, "can-allocate-row ri=%d mode=%d ok", ri, mode);
2519 * Find a PMC descriptor with user handle 'pmcid' for thread 'td'.
2523 pmc_find_pmc_descriptor_in_process(struct pmc_owner *po, pmc_id_t pmcid)
2527 KASSERT(PMC_ID_TO_ROWINDEX(pmcid) < md->pmd_npmc,
2528 ("[pmc,%d] Illegal pmc index %d (max %d)", __LINE__,
2529 PMC_ID_TO_ROWINDEX(pmcid), md->pmd_npmc));
2531 LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2532 if (pm->pm_id == pmcid)
2539 pmc_find_pmc(pmc_id_t pmcid, struct pmc **pmc)
2543 struct pmc_owner *po;
2545 PMCDBG(PMC,FND,1, "find-pmc id=%d", pmcid);
2547 if ((po = pmc_find_owner_descriptor(curthread->td_proc)) == NULL)
2550 if ((pm = pmc_find_pmc_descriptor_in_process(po, pmcid)) == NULL)
2553 PMCDBG(PMC,FND,2, "find-pmc id=%d -> pmc=%p", pmcid, pm);
2564 pmc_start(struct pmc *pm)
2567 struct pmc_owner *po;
2568 struct pmc_binding pb;
2569 struct pmc_classdep *pcd;
2570 int adjri, error, cpu, ri;
2573 ("[pmc,%d] null pm", __LINE__));
2575 mode = PMC_TO_MODE(pm);
2576 ri = PMC_TO_ROWINDEX(pm);
2577 pcd = pmc_ri_to_classdep(md, ri, &adjri);
2581 PMCDBG(PMC,OPS,1, "start pmc=%p mode=%d ri=%d", pm, mode, ri);
2586 * Disallow PMCSTART if a logfile is required but has not been
2589 if ((pm->pm_flags & PMC_F_NEEDS_LOGFILE) &&
2590 (po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
2591 return (EDOOFUS); /* programming error */
2594 * If this is a sampling mode PMC, log mapping information for
2595 * the kernel modules that are currently loaded.
2597 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
2598 pmc_log_kernel_mappings(pm);
2600 if (PMC_IS_VIRTUAL_MODE(mode)) {
2603 * If a PMCATTACH has never been done on this PMC,
2604 * attach it to its owner process.
2607 if (LIST_EMPTY(&pm->pm_targets))
2608 error = (pm->pm_flags & PMC_F_ATTACH_DONE) ? ESRCH :
2609 pmc_attach_process(po->po_owner, pm);
2612 * If the PMC is attached to its owner, then force a context
2613 * switch to ensure that the MD state gets set correctly.
2617 pm->pm_state = PMC_STATE_RUNNING;
2618 if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER)
2619 pmc_force_context_switch();
2627 * A system-wide PMC.
2629 * Add the owner to the global list if this is a system-wide
2633 if (mode == PMC_MODE_SS) {
2634 if (po->po_sscount == 0) {
2635 LIST_INSERT_HEAD(&pmc_ss_owners, po, po_ssnext);
2636 atomic_add_rel_int(&pmc_ss_count, 1);
2637 PMCDBG(PMC,OPS,1, "po=%p in global list", po);
2642 * Log mapping information for all existing processes in the
2643 * system. Subsequent mappings are logged as they happen;
2644 * see pmc_process_mmap().
2646 if (po->po_logprocmaps == 0) {
2647 pmc_log_all_process_mappings(po);
2648 po->po_logprocmaps = 1;
2653 * Move to the CPU associated with this
2654 * PMC, and start the hardware.
2657 pmc_save_cpu_binding(&pb);
2659 cpu = PMC_TO_CPU(pm);
2661 if (!pmc_cpu_is_active(cpu))
2664 pmc_select_cpu(cpu);
2667 * global PMCs are configured at allocation time
2668 * so write out the initial value and start the PMC.
2671 pm->pm_state = PMC_STATE_RUNNING;
2674 if ((error = pcd->pcd_write_pmc(cpu, adjri,
2675 PMC_IS_SAMPLING_MODE(mode) ?
2676 pm->pm_sc.pm_reloadcount :
2677 pm->pm_sc.pm_initial)) == 0)
2678 error = pcd->pcd_start_pmc(cpu, adjri);
2681 pmc_restore_cpu_binding(&pb);
2691 pmc_stop(struct pmc *pm)
2693 struct pmc_owner *po;
2694 struct pmc_binding pb;
2695 struct pmc_classdep *pcd;
2696 int adjri, cpu, error, ri;
2698 KASSERT(pm != NULL, ("[pmc,%d] null pmc", __LINE__));
2700 PMCDBG(PMC,OPS,1, "stop pmc=%p mode=%d ri=%d", pm,
2701 PMC_TO_MODE(pm), PMC_TO_ROWINDEX(pm));
2703 pm->pm_state = PMC_STATE_STOPPED;
2706 * If the PMC is a virtual mode one, changing the state to
2707 * non-RUNNING is enough to ensure that the PMC never gets
2710 * If this PMC is current running on a CPU, then it will
2711 * handled correctly at the time its target process is context
2715 if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
2719 * A system-mode PMC. Move to the CPU associated with
2720 * this PMC, and stop the hardware. We update the
2721 * 'initial count' so that a subsequent PMCSTART will
2722 * resume counting from the current hardware count.
2725 pmc_save_cpu_binding(&pb);
2727 cpu = PMC_TO_CPU(pm);
2729 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
2730 ("[pmc,%d] illegal cpu=%d", __LINE__, cpu));
2732 if (!pmc_cpu_is_active(cpu))
2735 pmc_select_cpu(cpu);
2737 ri = PMC_TO_ROWINDEX(pm);
2738 pcd = pmc_ri_to_classdep(md, ri, &adjri);
2741 if ((error = pcd->pcd_stop_pmc(cpu, adjri)) == 0)
2742 error = pcd->pcd_read_pmc(cpu, adjri, &pm->pm_sc.pm_initial);
2745 pmc_restore_cpu_binding(&pb);
2749 /* remove this owner from the global list of SS PMC owners */
2750 if (PMC_TO_MODE(pm) == PMC_MODE_SS) {
2752 if (po->po_sscount == 0) {
2753 atomic_subtract_rel_int(&pmc_ss_count, 1);
2754 LIST_REMOVE(po, po_ssnext);
2755 PMCDBG(PMC,OPS,2,"po=%p removed from global list", po);
2764 static const char *pmc_op_to_name[] = {
2766 #define __PMC_OP(N, D) #N ,
2773 * The syscall interface
2776 #define PMC_GET_SX_XLOCK(...) do { \
2777 sx_xlock(&pmc_sx); \
2778 if (pmc_hook == NULL) { \
2779 sx_xunlock(&pmc_sx); \
2780 return __VA_ARGS__; \
2784 #define PMC_DOWNGRADE_SX() do { \
2785 sx_downgrade(&pmc_sx); \
2786 is_sx_downgraded = 1; \
2790 pmc_syscall_handler(struct thread *td, void *syscall_args)
2792 int error, is_sx_downgraded, is_sx_locked, op;
2793 struct pmc_syscall_args *c;
2796 PMC_GET_SX_XLOCK(ENOSYS);
2800 is_sx_downgraded = 0;
2803 c = (struct pmc_syscall_args *) syscall_args;
2808 PMCDBG(MOD,PMS,1, "syscall op=%d \"%s\" arg=%p", op,
2809 pmc_op_to_name[op], arg);
2812 atomic_add_int(&pmc_stats.pm_syscalls, 1);
2819 * Configure a log file.
2821 * XXX This OP will be reworked.
2824 case PMC_OP_CONFIGURELOG:
2828 struct pmc_owner *po;
2829 struct pmc_op_configurelog cl;
2831 sx_assert(&pmc_sx, SX_XLOCKED);
2833 if ((error = copyin(arg, &cl, sizeof(cl))) != 0)
2836 /* mark this process as owning a log file */
2838 if ((po = pmc_find_owner_descriptor(p)) == NULL)
2839 if ((po = pmc_allocate_owner_descriptor(p)) == NULL) {
2845 * If a valid fd was passed in, try to configure that,
2846 * otherwise if 'fd' was less than zero and there was
2847 * a log file configured, flush its buffers and
2850 if (cl.pm_logfd >= 0) {
2851 sx_xunlock(&pmc_sx);
2853 error = pmclog_configure_log(md, po, cl.pm_logfd);
2854 } else if (po->po_flags & PMC_PO_OWNS_LOGFILE) {
2855 pmclog_process_closelog(po);
2856 error = pmclog_close(po);
2858 LIST_FOREACH(pm, &po->po_pmcs, pm_next)
2859 if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
2860 pm->pm_state == PMC_STATE_RUNNING)
2862 error = pmclog_deconfigure_log(po);
2876 case PMC_OP_FLUSHLOG:
2878 struct pmc_owner *po;
2880 sx_assert(&pmc_sx, SX_XLOCKED);
2882 if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2887 error = pmclog_flush(po);
2895 case PMC_OP_CLOSELOG:
2897 struct pmc_owner *po;
2899 sx_assert(&pmc_sx, SX_XLOCKED);
2901 if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
2906 error = pmclog_close(po);
2911 * Retrieve hardware configuration.
2914 case PMC_OP_GETCPUINFO: /* CPU information */
2916 struct pmc_op_getcpuinfo gci;
2917 struct pmc_classinfo *pci;
2918 struct pmc_classdep *pcd;
2921 gci.pm_cputype = md->pmd_cputype;
2922 gci.pm_ncpu = pmc_cpu_max();
2923 gci.pm_npmc = md->pmd_npmc;
2924 gci.pm_nclass = md->pmd_nclass;
2925 pci = gci.pm_classes;
2926 pcd = md->pmd_classdep;
2927 for (cl = 0; cl < md->pmd_nclass; cl++, pci++, pcd++) {
2928 pci->pm_caps = pcd->pcd_caps;
2929 pci->pm_class = pcd->pcd_class;
2930 pci->pm_width = pcd->pcd_width;
2931 pci->pm_num = pcd->pcd_num;
2933 error = copyout(&gci, arg, sizeof(gci));
2938 * Retrieve soft events list.
2940 case PMC_OP_GETDYNEVENTINFO:
2944 struct pmc_op_getdyneventinfo *gei;
2945 struct pmc_dyn_event_descr dev;
2946 struct pmc_soft *ps;
2949 sx_assert(&pmc_sx, SX_LOCKED);
2951 gei = (struct pmc_op_getdyneventinfo *) arg;
2953 if ((error = copyin(&gei->pm_class, &cl, sizeof(cl))) != 0)
2956 /* Only SOFT class is dynamic. */
2957 if (cl != PMC_CLASS_SOFT) {
2963 for (ev = PMC_EV_SOFT_FIRST; (int)ev <= PMC_EV_SOFT_LAST; ev++) {
2964 ps = pmc_soft_ev_acquire(ev);
2967 bcopy(&ps->ps_ev, &dev, sizeof(dev));
2968 pmc_soft_ev_release(ps);
2970 error = copyout(&dev,
2971 &gei->pm_events[nevent],
2972 sizeof(struct pmc_dyn_event_descr));
2980 error = copyout(&nevent, &gei->pm_nevent,
2986 * Get module statistics
2989 case PMC_OP_GETDRIVERSTATS:
2991 struct pmc_op_getdriverstats gms;
2993 bcopy(&pmc_stats, &gms, sizeof(gms));
2994 error = copyout(&gms, arg, sizeof(gms));
3000 * Retrieve module version number
3003 case PMC_OP_GETMODULEVERSION:
3007 /* retrieve the client's idea of the ABI version */
3008 if ((error = copyin(arg, &cv, sizeof(uint32_t))) != 0)
3010 /* don't service clients newer than our driver */
3012 if ((cv & 0xFFFF0000) > (modv & 0xFFFF0000)) {
3013 error = EPROGMISMATCH;
3016 error = copyout(&modv, arg, sizeof(int));
3022 * Retrieve the state of all the PMCs on a given
3026 case PMC_OP_GETPMCINFO:
3030 size_t pmcinfo_size;
3031 uint32_t cpu, n, npmc;
3032 struct pmc_owner *po;
3033 struct pmc_binding pb;
3034 struct pmc_classdep *pcd;
3035 struct pmc_info *p, *pmcinfo;
3036 struct pmc_op_getpmcinfo *gpi;
3040 gpi = (struct pmc_op_getpmcinfo *) arg;
3042 if ((error = copyin(&gpi->pm_cpu, &cpu, sizeof(cpu))) != 0)
3045 if (cpu >= pmc_cpu_max()) {
3050 if (!pmc_cpu_is_active(cpu)) {
3055 /* switch to CPU 'cpu' */
3056 pmc_save_cpu_binding(&pb);
3057 pmc_select_cpu(cpu);
3059 npmc = md->pmd_npmc;
3061 pmcinfo_size = npmc * sizeof(struct pmc_info);
3062 pmcinfo = malloc(pmcinfo_size, M_PMC, M_WAITOK);
3066 for (n = 0; n < md->pmd_npmc; n++, p++) {
3068 pcd = pmc_ri_to_classdep(md, n, &ari);
3070 KASSERT(pcd != NULL,
3071 ("[pmc,%d] null pcd ri=%d", __LINE__, n));
3073 if ((error = pcd->pcd_describe(cpu, ari, p, &pm)) != 0)
3076 if (PMC_ROW_DISP_IS_STANDALONE(n))
3077 p->pm_rowdisp = PMC_DISP_STANDALONE;
3078 else if (PMC_ROW_DISP_IS_THREAD(n))
3079 p->pm_rowdisp = PMC_DISP_THREAD;
3081 p->pm_rowdisp = PMC_DISP_FREE;
3083 p->pm_ownerpid = -1;
3085 if (pm == NULL) /* no PMC associated */
3090 KASSERT(po->po_owner != NULL,
3091 ("[pmc,%d] pmc_owner had a null proc pointer",
3094 p->pm_ownerpid = po->po_owner->p_pid;
3095 p->pm_mode = PMC_TO_MODE(pm);
3096 p->pm_event = pm->pm_event;
3097 p->pm_flags = pm->pm_flags;
3099 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3101 pm->pm_sc.pm_reloadcount;
3104 pmc_restore_cpu_binding(&pb);
3106 /* now copy out the PMC info collected */
3108 error = copyout(pmcinfo, &gpi->pm_pmcs, pmcinfo_size);
3110 free(pmcinfo, M_PMC);
3116 * Set the administrative state of a PMC. I.e. whether
3117 * the PMC is to be used or not.
3120 case PMC_OP_PMCADMIN:
3123 enum pmc_state request;
3126 struct pmc_op_pmcadmin pma;
3127 struct pmc_binding pb;
3129 sx_assert(&pmc_sx, SX_XLOCKED);
3131 KASSERT(td == curthread,
3132 ("[pmc,%d] td != curthread", __LINE__));
3134 error = priv_check(td, PRIV_PMC_MANAGE);
3138 if ((error = copyin(arg, &pma, sizeof(pma))) != 0)
3143 if (cpu < 0 || cpu >= (int) pmc_cpu_max()) {
3148 if (!pmc_cpu_is_active(cpu)) {
3153 request = pma.pm_state;
3155 if (request != PMC_STATE_DISABLED &&
3156 request != PMC_STATE_FREE) {
3161 ri = pma.pm_pmc; /* pmc id == row index */
3162 if (ri < 0 || ri >= (int) md->pmd_npmc) {
3168 * We can't disable a PMC with a row-index allocated
3169 * for process virtual PMCs.
3172 if (PMC_ROW_DISP_IS_THREAD(ri) &&
3173 request == PMC_STATE_DISABLED) {
3179 * otherwise, this PMC on this CPU is either free or
3180 * in system-wide mode.
3183 pmc_save_cpu_binding(&pb);
3184 pmc_select_cpu(cpu);
3187 phw = pc->pc_hwpmcs[ri];
3190 * XXX do we need some kind of 'forced' disable?
3193 if (phw->phw_pmc == NULL) {
3194 if (request == PMC_STATE_DISABLED &&
3195 (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED)) {
3196 phw->phw_state &= ~PMC_PHW_FLAG_IS_ENABLED;
3197 PMC_MARK_ROW_STANDALONE(ri);
3198 } else if (request == PMC_STATE_FREE &&
3199 (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0) {
3200 phw->phw_state |= PMC_PHW_FLAG_IS_ENABLED;
3201 PMC_UNMARK_ROW_STANDALONE(ri);
3203 /* other cases are a no-op */
3207 pmc_restore_cpu_binding(&pb);
3216 case PMC_OP_PMCALLOCATE:
3224 struct pmc_binding pb;
3225 struct pmc_classdep *pcd;
3226 struct pmc_op_pmcallocate pa;
3228 if ((error = copyin(arg, &pa, sizeof(pa))) != 0)
3235 if ((mode != PMC_MODE_SS && mode != PMC_MODE_SC &&
3236 mode != PMC_MODE_TS && mode != PMC_MODE_TC) ||
3237 (cpu != (u_int) PMC_CPU_ANY && cpu >= pmc_cpu_max())) {
3243 * Virtual PMCs should only ask for a default CPU.
3244 * System mode PMCs need to specify a non-default CPU.
3247 if ((PMC_IS_VIRTUAL_MODE(mode) && cpu != (u_int) PMC_CPU_ANY) ||
3248 (PMC_IS_SYSTEM_MODE(mode) && cpu == (u_int) PMC_CPU_ANY)) {
3254 * Check that an inactive CPU is not being asked for.
3257 if (PMC_IS_SYSTEM_MODE(mode) && !pmc_cpu_is_active(cpu)) {
3263 * Refuse an allocation for a system-wide PMC if this
3264 * process has been jailed, or if this process lacks
3265 * super-user credentials and the sysctl tunable
3266 * 'security.bsd.unprivileged_syspmcs' is zero.
3269 if (PMC_IS_SYSTEM_MODE(mode)) {
3270 if (jailed(curthread->td_ucred)) {
3274 if (!pmc_unprivileged_syspmcs) {
3275 error = priv_check(curthread,
3283 * Look for valid values for 'pm_flags'
3286 if ((pa.pm_flags & ~(PMC_F_DESCENDANTS | PMC_F_LOG_PROCCSW |
3287 PMC_F_LOG_PROCEXIT | PMC_F_CALLCHAIN)) != 0) {
3292 /* process logging options are not allowed for system PMCs */
3293 if (PMC_IS_SYSTEM_MODE(mode) && (pa.pm_flags &
3294 (PMC_F_LOG_PROCCSW | PMC_F_LOG_PROCEXIT))) {
3300 * All sampling mode PMCs need to be able to interrupt the
3303 if (PMC_IS_SAMPLING_MODE(mode))
3304 caps |= PMC_CAP_INTERRUPT;
3306 /* A valid class specifier should have been passed in. */
3307 for (n = 0; n < md->pmd_nclass; n++)
3308 if (md->pmd_classdep[n].pcd_class == pa.pm_class)
3310 if (n == md->pmd_nclass) {
3315 /* The requested PMC capabilities should be feasible. */
3316 if ((md->pmd_classdep[n].pcd_caps & caps) != caps) {
3321 PMCDBG(PMC,ALL,2, "event=%d caps=0x%x mode=%d cpu=%d",
3322 pa.pm_ev, caps, mode, cpu);
3324 pmc = pmc_allocate_pmc_descriptor();
3325 pmc->pm_id = PMC_ID_MAKE_ID(cpu,pa.pm_mode,pa.pm_class,
3327 pmc->pm_event = pa.pm_ev;
3328 pmc->pm_state = PMC_STATE_FREE;
3329 pmc->pm_caps = caps;
3330 pmc->pm_flags = pa.pm_flags;
3332 /* switch thread to CPU 'cpu' */
3333 pmc_save_cpu_binding(&pb);
3335 #define PMC_IS_SHAREABLE_PMC(cpu, n) \
3336 (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_state & \
3337 PMC_PHW_FLAG_IS_SHAREABLE)
3338 #define PMC_IS_UNALLOCATED(cpu, n) \
3339 (pmc_pcpu[(cpu)]->pc_hwpmcs[(n)]->phw_pmc == NULL)
3341 if (PMC_IS_SYSTEM_MODE(mode)) {
3342 pmc_select_cpu(cpu);
3343 for (n = 0; n < (int) md->pmd_npmc; n++) {
3344 pcd = pmc_ri_to_classdep(md, n, &adjri);
3345 if (pmc_can_allocate_row(n, mode) == 0 &&
3346 pmc_can_allocate_rowindex(
3347 curthread->td_proc, n, cpu) == 0 &&
3348 (PMC_IS_UNALLOCATED(cpu, n) ||
3349 PMC_IS_SHAREABLE_PMC(cpu, n)) &&
3350 pcd->pcd_allocate_pmc(cpu, adjri, pmc,
3355 /* Process virtual mode */
3356 for (n = 0; n < (int) md->pmd_npmc; n++) {
3357 pcd = pmc_ri_to_classdep(md, n, &adjri);
3358 if (pmc_can_allocate_row(n, mode) == 0 &&
3359 pmc_can_allocate_rowindex(
3360 curthread->td_proc, n,
3361 PMC_CPU_ANY) == 0 &&
3362 pcd->pcd_allocate_pmc(curthread->td_oncpu,
3363 adjri, pmc, &pa) == 0)
3368 #undef PMC_IS_UNALLOCATED
3369 #undef PMC_IS_SHAREABLE_PMC
3371 pmc_restore_cpu_binding(&pb);
3373 if (n == (int) md->pmd_npmc) {
3374 pmc_destroy_pmc_descriptor(pmc);
3381 /* Fill in the correct value in the ID field */
3382 pmc->pm_id = PMC_ID_MAKE_ID(cpu,mode,pa.pm_class,n);
3384 PMCDBG(PMC,ALL,2, "ev=%d class=%d mode=%d n=%d -> pmcid=%x",
3385 pmc->pm_event, pa.pm_class, mode, n, pmc->pm_id);
3387 /* Process mode PMCs with logging enabled need log files */
3388 if (pmc->pm_flags & (PMC_F_LOG_PROCEXIT | PMC_F_LOG_PROCCSW))
3389 pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3391 /* All system mode sampling PMCs require a log file */
3392 if (PMC_IS_SAMPLING_MODE(mode) && PMC_IS_SYSTEM_MODE(mode))
3393 pmc->pm_flags |= PMC_F_NEEDS_LOGFILE;
3396 * Configure global pmc's immediately
3399 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pmc))) {
3401 pmc_save_cpu_binding(&pb);
3402 pmc_select_cpu(cpu);
3404 phw = pmc_pcpu[cpu]->pc_hwpmcs[n];
3405 pcd = pmc_ri_to_classdep(md, n, &adjri);
3407 if ((phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) == 0 ||
3408 (error = pcd->pcd_config_pmc(cpu, adjri, pmc)) != 0) {
3409 (void) pcd->pcd_release_pmc(cpu, adjri, pmc);
3410 pmc_destroy_pmc_descriptor(pmc);
3413 pmc_restore_cpu_binding(&pb);
3418 pmc_restore_cpu_binding(&pb);
3421 pmc->pm_state = PMC_STATE_ALLOCATED;
3424 * mark row disposition
3427 if (PMC_IS_SYSTEM_MODE(mode))
3428 PMC_MARK_ROW_STANDALONE(n);
3430 PMC_MARK_ROW_THREAD(n);
3433 * Register this PMC with the current thread as its owner.
3437 pmc_register_owner(curthread->td_proc, pmc)) != 0) {
3438 pmc_release_pmc_descriptor(pmc);
3445 * Return the allocated index.
3448 pa.pm_pmcid = pmc->pm_id;
3450 error = copyout(&pa, arg, sizeof(pa));
3456 * Attach a PMC to a process.
3459 case PMC_OP_PMCATTACH:
3463 struct pmc_op_pmcattach a;
3465 sx_assert(&pmc_sx, SX_XLOCKED);
3467 if ((error = copyin(arg, &a, sizeof(a))) != 0)
3473 } else if (a.pm_pid == 0)
3474 a.pm_pid = td->td_proc->p_pid;
3476 if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3479 if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) {
3484 /* PMCs may be (re)attached only when allocated or stopped */
3485 if (pm->pm_state == PMC_STATE_RUNNING) {
3488 } else if (pm->pm_state != PMC_STATE_ALLOCATED &&
3489 pm->pm_state != PMC_STATE_STOPPED) {
3495 if ((p = pfind(a.pm_pid)) == NULL) {
3501 * Ignore processes that are working on exiting.
3503 if (p->p_flag & P_WEXIT) {
3505 PROC_UNLOCK(p); /* pfind() returns a locked process */
3510 * we are allowed to attach a PMC to a process if
3513 error = p_candebug(curthread, p);
3518 error = pmc_attach_process(p, pm);
3524 * Detach an attached PMC from a process.
3527 case PMC_OP_PMCDETACH:
3531 struct pmc_op_pmcattach a;
3533 if ((error = copyin(arg, &a, sizeof(a))) != 0)
3539 } else if (a.pm_pid == 0)
3540 a.pm_pid = td->td_proc->p_pid;
3542 if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0)
3545 if ((p = pfind(a.pm_pid)) == NULL) {
3551 * Treat processes that are in the process of exiting
3552 * as if they were not present.
3555 if (p->p_flag & P_WEXIT)
3558 PROC_UNLOCK(p); /* pfind() returns a locked process */
3561 error = pmc_detach_process(p, pm);
3567 * Retrieve the MSR number associated with the counter
3568 * 'pmc_id'. This allows processes to directly use RDPMC
3569 * instructions to read their PMCs, without the overhead of a
3573 case PMC_OP_PMCGETMSR:
3577 struct pmc_target *pt;
3578 struct pmc_op_getmsr gm;
3579 struct pmc_classdep *pcd;
3583 if ((error = copyin(arg, &gm, sizeof(gm))) != 0)
3586 if ((error = pmc_find_pmc(gm.pm_pmcid, &pm)) != 0)
3590 * The allocated PMC has to be a process virtual PMC,
3591 * i.e., of type MODE_T[CS]. Global PMCs can only be
3592 * read using the PMCREAD operation since they may be
3593 * allocated on a different CPU than the one we could
3594 * be running on at the time of the RDPMC instruction.
3596 * The GETMSR operation is not allowed for PMCs that
3597 * are inherited across processes.
3600 if (!PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)) ||
3601 (pm->pm_flags & PMC_F_DESCENDANTS)) {
3607 * It only makes sense to use a RDPMC (or its
3608 * equivalent instruction on non-x86 architectures) on
3609 * a process that has allocated and attached a PMC to
3610 * itself. Conversely the PMC is only allowed to have
3611 * one process attached to it -- its owner.
3614 if ((pt = LIST_FIRST(&pm->pm_targets)) == NULL ||
3615 LIST_NEXT(pt, pt_next) != NULL ||
3616 pt->pt_process->pp_proc != pm->pm_owner->po_owner) {
3621 ri = PMC_TO_ROWINDEX(pm);
3622 pcd = pmc_ri_to_classdep(md, ri, &adjri);
3624 /* PMC class has no 'GETMSR' support */
3625 if (pcd->pcd_get_msr == NULL) {
3630 if ((error = (*pcd->pcd_get_msr)(adjri, &gm.pm_msr)) < 0)
3633 if ((error = copyout(&gm, arg, sizeof(gm))) < 0)
3637 * Mark our process as using MSRs. Update machine
3638 * state using a forced context switch.
3641 pt->pt_process->pp_flags |= PMC_PP_ENABLE_MSR_ACCESS;
3642 pmc_force_context_switch();
3648 * Release an allocated PMC
3651 case PMC_OP_PMCRELEASE:
3655 struct pmc_owner *po;
3656 struct pmc_op_simple sp;
3659 * Find PMC pointer for the named PMC.
3661 * Use pmc_release_pmc_descriptor() to switch off the
3662 * PMC, remove all its target threads, and remove the
3663 * PMC from its owner's list.
3665 * Remove the owner record if this is the last PMC
3671 if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3674 pmcid = sp.pm_pmcid;
3676 if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3680 pmc_release_pmc_descriptor(pm);
3681 pmc_maybe_remove_owner(po);
3689 * Read and/or write a PMC.
3697 pmc_value_t oldvalue;
3698 struct pmc_binding pb;
3699 struct pmc_op_pmcrw prw;
3700 struct pmc_classdep *pcd;
3701 struct pmc_op_pmcrw *pprw;
3705 if ((error = copyin(arg, &prw, sizeof(prw))) != 0)
3709 PMCDBG(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid,
3712 /* must have at least one flag set */
3713 if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) {
3718 /* locate pmc descriptor */
3719 if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0)
3722 /* Can't read a PMC that hasn't been started. */
3723 if (pm->pm_state != PMC_STATE_ALLOCATED &&
3724 pm->pm_state != PMC_STATE_STOPPED &&
3725 pm->pm_state != PMC_STATE_RUNNING) {
3730 /* writing a new value is allowed only for 'STOPPED' pmcs */
3731 if (pm->pm_state == PMC_STATE_RUNNING &&
3732 (prw.pm_flags & PMC_F_NEWVALUE)) {
3737 if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) {
3740 * If this PMC is attached to its owner (i.e.,
3741 * the process requesting this operation) and
3742 * is running, then attempt to get an
3743 * upto-date reading from hardware for a READ.
3744 * Writes are only allowed when the PMC is
3745 * stopped, so only update the saved value
3748 * If the PMC is not running, or is not
3749 * attached to its owner, read/write to the
3753 ri = PMC_TO_ROWINDEX(pm);
3754 pcd = pmc_ri_to_classdep(md, ri, &adjri);
3756 mtx_pool_lock_spin(pmc_mtxpool, pm);
3757 cpu = curthread->td_oncpu;
3759 if (prw.pm_flags & PMC_F_OLDVALUE) {
3760 if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) &&
3761 (pm->pm_state == PMC_STATE_RUNNING))
3762 error = (*pcd->pcd_read_pmc)(cpu, adjri,
3765 oldvalue = pm->pm_gv.pm_savedvalue;
3767 if (prw.pm_flags & PMC_F_NEWVALUE)
3768 pm->pm_gv.pm_savedvalue = prw.pm_value;
3770 mtx_pool_unlock_spin(pmc_mtxpool, pm);
3772 } else { /* System mode PMCs */
3773 cpu = PMC_TO_CPU(pm);
3774 ri = PMC_TO_ROWINDEX(pm);
3775 pcd = pmc_ri_to_classdep(md, ri, &adjri);
3777 if (!pmc_cpu_is_active(cpu)) {
3782 /* move this thread to CPU 'cpu' */
3783 pmc_save_cpu_binding(&pb);
3784 pmc_select_cpu(cpu);
3787 /* save old value */
3788 if (prw.pm_flags & PMC_F_OLDVALUE)
3789 if ((error = (*pcd->pcd_read_pmc)(cpu, adjri,
3792 /* write out new value */
3793 if (prw.pm_flags & PMC_F_NEWVALUE)
3794 error = (*pcd->pcd_write_pmc)(cpu, adjri,
3798 pmc_restore_cpu_binding(&pb);
3803 pprw = (struct pmc_op_pmcrw *) arg;
3806 if (prw.pm_flags & PMC_F_NEWVALUE)
3807 PMCDBG(PMC,OPS,2, "rw id=%d new %jx -> old %jx",
3808 ri, prw.pm_value, oldvalue);
3809 else if (prw.pm_flags & PMC_F_OLDVALUE)
3810 PMCDBG(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue);
3813 /* return old value if requested */
3814 if (prw.pm_flags & PMC_F_OLDVALUE)
3815 if ((error = copyout(&oldvalue, &pprw->pm_value,
3816 sizeof(prw.pm_value))))
3824 * Set the sampling rate for a sampling mode PMC and the
3825 * initial count for a counting mode PMC.
3828 case PMC_OP_PMCSETCOUNT:
3831 struct pmc_op_pmcsetcount sc;
3835 if ((error = copyin(arg, &sc, sizeof(sc))) != 0)
3838 if ((error = pmc_find_pmc(sc.pm_pmcid, &pm)) != 0)
3841 if (pm->pm_state == PMC_STATE_RUNNING) {
3846 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
3847 pm->pm_sc.pm_reloadcount = sc.pm_count;
3849 pm->pm_sc.pm_initial = sc.pm_count;
3858 case PMC_OP_PMCSTART:
3862 struct pmc_op_simple sp;
3864 sx_assert(&pmc_sx, SX_XLOCKED);
3866 if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3869 pmcid = sp.pm_pmcid;
3871 if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3874 KASSERT(pmcid == pm->pm_id,
3875 ("[pmc,%d] pmcid %x != id %x", __LINE__,
3878 if (pm->pm_state == PMC_STATE_RUNNING) /* already running */
3880 else if (pm->pm_state != PMC_STATE_STOPPED &&
3881 pm->pm_state != PMC_STATE_ALLOCATED) {
3886 error = pmc_start(pm);
3895 case PMC_OP_PMCSTOP:
3899 struct pmc_op_simple sp;
3903 if ((error = copyin(arg, &sp, sizeof(sp))) != 0)
3906 pmcid = sp.pm_pmcid;
3909 * Mark the PMC as inactive and invoke the MD stop
3910 * routines if needed.
3913 if ((error = pmc_find_pmc(pmcid, &pm)) != 0)
3916 KASSERT(pmcid == pm->pm_id,
3917 ("[pmc,%d] pmc id %x != pmcid %x", __LINE__,
3920 if (pm->pm_state == PMC_STATE_STOPPED) /* already stopped */
3922 else if (pm->pm_state != PMC_STATE_RUNNING) {
3927 error = pmc_stop(pm);
3933 * Write a user supplied value to the log file.
3936 case PMC_OP_WRITELOG:
3938 struct pmc_op_writelog wl;
3939 struct pmc_owner *po;
3943 if ((error = copyin(arg, &wl, sizeof(wl))) != 0)
3946 if ((po = pmc_find_owner_descriptor(td->td_proc)) == NULL) {
3951 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
3956 error = pmclog_process_userlog(po, &wl);
3966 if (is_sx_locked != 0) {
3967 if (is_sx_downgraded)
3968 sx_sunlock(&pmc_sx);
3970 sx_xunlock(&pmc_sx);
3974 atomic_add_int(&pmc_stats.pm_syscall_errors, 1);
3987 * Mark the thread as needing callchain capture and post an AST. The
3988 * actual callchain capture will be done in a context where it is safe
3989 * to take page faults.
3993 pmc_post_callchain_callback(void)
4000 * If there is multiple PMCs for the same interrupt ignore new post
4002 if (td->td_pflags & TDP_CALLCHAIN)
4006 * Mark this thread as needing callchain capture.
4007 * `td->td_pflags' will be safe to touch because this thread
4008 * was in user space when it was interrupted.
4010 td->td_pflags |= TDP_CALLCHAIN;
4013 * Don't let this thread migrate between CPUs until callchain
4014 * capture completes.
4022 * Interrupt processing.
4024 * Find a free slot in the per-cpu array of samples and capture the
4025 * current callchain there. If a sample was successfully added, a bit
4026 * is set in mask 'pmc_cpumask' denoting that the DO_SAMPLES hook
4027 * needs to be invoked from the clock handler.
4029 * This function is meant to be called from an NMI handler. It cannot
4030 * use any of the locking primitives supplied by the OS.
4034 pmc_process_interrupt(int cpu, int ring, struct pmc *pm, struct trapframe *tf,
4037 int error, callchaindepth;
4039 struct pmc_sample *ps;
4040 struct pmc_samplebuffer *psb;
4045 * Allocate space for a sample buffer.
4047 psb = pmc_pcpu[cpu]->pc_sb[ring];
4050 if (ps->ps_nsamples) { /* in use, reader hasn't caught up */
4052 atomic_add_int(&pmc_stats.pm_intr_bufferfull, 1);
4053 PMCDBG(SAM,INT,1,"(spc) cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d",
4054 cpu, pm, (void *) tf, inuserspace,
4055 (int) (psb->ps_write - psb->ps_samples),
4056 (int) (psb->ps_read - psb->ps_samples));
4062 /* Fill in entry. */
4063 PMCDBG(SAM,INT,1,"cpu=%d pm=%p tf=%p um=%d wr=%d rd=%d", cpu, pm,
4064 (void *) tf, inuserspace,
4065 (int) (psb->ps_write - psb->ps_samples),
4066 (int) (psb->ps_read - psb->ps_samples));
4068 KASSERT(pm->pm_runcount >= 0,
4069 ("[pmc,%d] pm=%p runcount %d", __LINE__, (void *) pm,
4072 atomic_add_rel_int(&pm->pm_runcount, 1); /* hold onto PMC */
4075 if ((td = curthread) && td->td_proc)
4076 ps->ps_pid = td->td_proc->p_pid;
4081 ps->ps_flags = inuserspace ? PMC_CC_F_USERSPACE : 0;
4083 callchaindepth = (pm->pm_flags & PMC_F_CALLCHAIN) ?
4084 pmc_callchaindepth : 1;
4086 if (callchaindepth == 1)
4087 ps->ps_pc[0] = PMC_TRAPFRAME_TO_PC(tf);
4090 * Kernel stack traversals can be done immediately,
4091 * while we defer to an AST for user space traversals.
4095 pmc_save_kernel_callchain(ps->ps_pc,
4096 callchaindepth, tf);
4098 pmc_post_callchain_callback();
4099 callchaindepth = PMC_SAMPLE_INUSE;
4103 ps->ps_nsamples = callchaindepth; /* mark entry as in use */
4105 /* increment write pointer, modulo ring buffer size */
4107 if (ps == psb->ps_fence)
4108 psb->ps_write = psb->ps_samples;
4113 /* mark CPU as needing processing */
4114 CPU_SET_ATOMIC(cpu, &pmc_cpumask);
4120 * Capture a user call chain. This function will be called from ast()
4121 * before control returns to userland and before the process gets
4126 pmc_capture_user_callchain(int cpu, int ring, struct trapframe *tf)
4131 struct pmc_sample *ps;
4132 struct pmc_samplebuffer *psb;
4137 psb = pmc_pcpu[cpu]->pc_sb[ring];
4140 KASSERT(td->td_pflags & TDP_CALLCHAIN,
4141 ("[pmc,%d] Retrieving callchain for thread that doesn't want it",
4149 * Iterate through all deferred callchain requests.
4152 ps = psb->ps_samples;
4153 for (i = 0; i < pmc_nsamples; i++, ps++) {
4155 if (ps->ps_nsamples != PMC_SAMPLE_INUSE)
4157 if (ps->ps_td != td)
4160 KASSERT(ps->ps_cpu == cpu,
4161 ("[pmc,%d] cpu mismatch ps_cpu=%d pcpu=%d", __LINE__,
4162 ps->ps_cpu, PCPU_GET(cpuid)));
4166 KASSERT(pm->pm_flags & PMC_F_CALLCHAIN,
4167 ("[pmc,%d] Retrieving callchain for PMC that doesn't "
4168 "want it", __LINE__));
4170 KASSERT(pm->pm_runcount > 0,
4171 ("[pmc,%d] runcount %d", __LINE__, pm->pm_runcount));
4174 * Retrieve the callchain and mark the sample buffer
4175 * as 'processable' by the timer tick sweep code.
4177 ps->ps_nsamples = pmc_save_user_callchain(ps->ps_pc,
4178 pmc_callchaindepth, tf);
4185 KASSERT(ncallchains > 0,
4186 ("[pmc,%d] cpu %d didn't find a sample to collect", __LINE__,
4189 KASSERT(td->td_pinned == 1,
4190 ("[pmc,%d] invalid td_pinned value", __LINE__));
4191 sched_unpin(); /* Can migrate safely now. */
4197 * Process saved PC samples.
4201 pmc_process_samples(int cpu, int ring)
4206 struct pmc_owner *po;
4207 struct pmc_sample *ps;
4208 struct pmc_classdep *pcd;
4209 struct pmc_samplebuffer *psb;
4211 KASSERT(PCPU_GET(cpuid) == cpu,
4212 ("[pmc,%d] not on the correct CPU pcpu=%d cpu=%d", __LINE__,
4213 PCPU_GET(cpuid), cpu));
4215 psb = pmc_pcpu[cpu]->pc_sb[ring];
4217 for (n = 0; n < pmc_nsamples; n++) { /* bound on #iterations */
4220 if (ps->ps_nsamples == PMC_SAMPLE_FREE)
4225 KASSERT(pm->pm_runcount > 0,
4226 ("[pmc,%d] pm=%p runcount %d", __LINE__, (void *) pm,
4231 KASSERT(PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)),
4232 ("[pmc,%d] pmc=%p non-sampling mode=%d", __LINE__,
4233 pm, PMC_TO_MODE(pm)));
4235 /* Ignore PMCs that have been switched off */
4236 if (pm->pm_state != PMC_STATE_RUNNING)
4239 /* If there is a pending AST wait for completion */
4240 if (ps->ps_nsamples == PMC_SAMPLE_INUSE) {
4241 /* Need a rescan at a later time. */
4242 CPU_SET_ATOMIC(cpu, &pmc_cpumask);
4246 PMCDBG(SAM,OPS,1,"cpu=%d pm=%p n=%d fl=%x wr=%d rd=%d", cpu,
4247 pm, ps->ps_nsamples, ps->ps_flags,
4248 (int) (psb->ps_write - psb->ps_samples),
4249 (int) (psb->ps_read - psb->ps_samples));
4252 * If this is a process-mode PMC that is attached to
4253 * its owner, and if the PC is in user mode, update
4254 * profiling statistics like timer-based profiling
4257 if (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) {
4258 if (ps->ps_flags & PMC_CC_F_USERSPACE) {
4259 td = FIRST_THREAD_IN_PROC(po->po_owner);
4260 addupc_intr(td, ps->ps_pc[0], 1);
4266 * Otherwise, this is either a sampling mode PMC that
4267 * is attached to a different process than its owner,
4268 * or a system-wide sampling PMC. Dispatch a log
4269 * entry to the PMC's owner process.
4271 pmclog_process_callchain(pm, ps);
4274 ps->ps_nsamples = 0; /* mark entry as free */
4275 atomic_subtract_rel_int(&pm->pm_runcount, 1);
4277 /* increment read pointer, modulo sample size */
4278 if (++ps == psb->ps_fence)
4279 psb->ps_read = psb->ps_samples;
4284 atomic_add_int(&pmc_stats.pm_log_sweeps, 1);
4286 /* Do not re-enable stalled PMCs if we failed to process any samples */
4291 * Restart any stalled sampling PMCs on this CPU.
4293 * If the NMI handler sets the pm_stalled field of a PMC after
4294 * the check below, we'll end up processing the stalled PMC at
4295 * the next hardclock tick.
4297 for (n = 0; n < md->pmd_npmc; n++) {
4298 pcd = pmc_ri_to_classdep(md, n, &adjri);
4299 KASSERT(pcd != NULL,
4300 ("[pmc,%d] null pcd ri=%d", __LINE__, n));
4301 (void) (*pcd->pcd_get_config)(cpu,adjri,&pm);
4303 if (pm == NULL || /* !cfg'ed */
4304 pm->pm_state != PMC_STATE_RUNNING || /* !active */
4305 !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) || /* !sampling */
4306 pm->pm_stalled == 0) /* !stalled */
4310 (*pcd->pcd_start_pmc)(cpu, adjri);
4319 * Handle a process exit.
4321 * Remove this process from all hash tables. If this process
4322 * owned any PMCs, turn off those PMCs and deallocate them,
4323 * removing any associations with target processes.
4325 * This function will be called by the last 'thread' of a
4328 * XXX This eventhandler gets called early in the exit process.
4329 * Consider using a 'hook' invocation from thread_exit() or equivalent
4330 * spot. Another negative is that kse_exit doesn't seem to call
4336 pmc_process_exit(void *arg __unused, struct proc *p)
4341 int is_using_hwpmcs;
4342 struct pmc_owner *po;
4343 struct pmc_process *pp;
4344 struct pmc_classdep *pcd;
4345 pmc_value_t newvalue, tmp;
4348 is_using_hwpmcs = p->p_flag & P_HWPMC;
4352 * Log a sysexit event to all SS PMC owners.
4354 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4355 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4356 pmclog_process_sysexit(po, p->p_pid);
4358 if (!is_using_hwpmcs)
4362 PMCDBG(PRC,EXT,1,"process-exit proc=%p (%d, %s)", p, p->p_pid,
4366 * Since this code is invoked by the last thread in an exiting
4367 * process, we would have context switched IN at some prior
4368 * point. However, with PREEMPTION, kernel mode context
4369 * switches may happen any time, so we want to disable a
4370 * context switch OUT till we get any PMCs targetting this
4371 * process off the hardware.
4373 * We also need to atomically remove this process'
4374 * entry from our target process hash table, using
4377 PMCDBG(PRC,EXT,1, "process-exit proc=%p (%d, %s)", p, p->p_pid,
4380 critical_enter(); /* no preemption */
4382 cpu = curthread->td_oncpu;
4384 if ((pp = pmc_find_process_descriptor(p,
4385 PMC_FLAG_REMOVE)) != NULL) {
4388 "process-exit proc=%p pmc-process=%p", p, pp);
4391 * The exiting process could the target of
4392 * some PMCs which will be running on
4393 * currently executing CPU.
4395 * We need to turn these PMCs off like we
4396 * would do at context switch OUT time.
4398 for (ri = 0; ri < md->pmd_npmc; ri++) {
4401 * Pick up the pmc pointer from hardware
4402 * state similar to the CSW_OUT code.
4406 pcd = pmc_ri_to_classdep(md, ri, &adjri);
4408 (void) (*pcd->pcd_get_config)(cpu, adjri, &pm);
4410 PMCDBG(PRC,EXT,2, "ri=%d pm=%p", ri, pm);
4413 !PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
4416 PMCDBG(PRC,EXT,2, "ppmcs[%d]=%p pm=%p "
4417 "state=%d", ri, pp->pp_pmcs[ri].pp_pmc,
4420 KASSERT(PMC_TO_ROWINDEX(pm) == ri,
4421 ("[pmc,%d] ri mismatch pmc(%d) ri(%d)",
4422 __LINE__, PMC_TO_ROWINDEX(pm), ri));
4424 KASSERT(pm == pp->pp_pmcs[ri].pp_pmc,
4425 ("[pmc,%d] pm %p != pp_pmcs[%d] %p",
4426 __LINE__, pm, ri, pp->pp_pmcs[ri].pp_pmc));
4428 (void) pcd->pcd_stop_pmc(cpu, adjri);
4430 KASSERT(pm->pm_runcount > 0,
4431 ("[pmc,%d] bad runcount ri %d rc %d",
4432 __LINE__, ri, pm->pm_runcount));
4434 /* Stop hardware only if it is actually running */
4435 if (pm->pm_state == PMC_STATE_RUNNING &&
4436 pm->pm_stalled == 0) {
4437 pcd->pcd_read_pmc(cpu, adjri, &newvalue);
4439 PMC_PCPU_SAVED(cpu,ri);
4441 mtx_pool_lock_spin(pmc_mtxpool, pm);
4442 pm->pm_gv.pm_savedvalue += tmp;
4443 pp->pp_pmcs[ri].pp_pmcval += tmp;
4444 mtx_pool_unlock_spin(pmc_mtxpool, pm);
4447 atomic_subtract_rel_int(&pm->pm_runcount,1);
4449 KASSERT((int) pm->pm_runcount >= 0,
4450 ("[pmc,%d] runcount is %d", __LINE__, ri));
4452 (void) pcd->pcd_config_pmc(cpu, adjri, NULL);
4456 * Inform the MD layer of this pseudo "context switch
4459 (void) md->pmd_switch_out(pmc_pcpu[cpu], pp);
4461 critical_exit(); /* ok to be pre-empted now */
4464 * Unlink this process from the PMCs that are
4465 * targetting it. This will send a signal to
4466 * all PMC owner's whose PMCs are orphaned.
4468 * Log PMC value at exit time if requested.
4470 for (ri = 0; ri < md->pmd_npmc; ri++)
4471 if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
4472 if (pm->pm_flags & PMC_F_NEEDS_LOGFILE &&
4473 PMC_IS_COUNTING_MODE(PMC_TO_MODE(pm)))
4474 pmclog_process_procexit(pm, pp);
4475 pmc_unlink_target_process(pm, pp);
4480 critical_exit(); /* pp == NULL */
4484 * If the process owned PMCs, free them up and free up
4487 if ((po = pmc_find_owner_descriptor(p)) != NULL) {
4488 pmc_remove_owner(po);
4489 pmc_destroy_owner_descriptor(po);
4492 sx_xunlock(&pmc_sx);
4496 * Handle a process fork.
4498 * If the parent process 'p1' is under HWPMC monitoring, then copy
4499 * over any attached PMCs that have 'do_descendants' semantics.
4503 pmc_process_fork(void *arg __unused, struct proc *p1, struct proc *newproc,
4506 int is_using_hwpmcs;
4508 uint32_t do_descendants;
4510 struct pmc_owner *po;
4511 struct pmc_process *ppnew, *ppold;
4513 (void) flags; /* unused parameter */
4516 is_using_hwpmcs = p1->p_flag & P_HWPMC;
4520 * If there are system-wide sampling PMCs active, we need to
4521 * log all fork events to their owner's logs.
4524 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4525 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4526 pmclog_process_procfork(po, p1->p_pid, newproc->p_pid);
4528 if (!is_using_hwpmcs)
4532 PMCDBG(PMC,FRK,1, "process-fork proc=%p (%d, %s) -> %p", p1,
4533 p1->p_pid, p1->p_comm, newproc);
4536 * If the parent process (curthread->td_proc) is a
4537 * target of any PMCs, look for PMCs that are to be
4538 * inherited, and link these into the new process
4541 if ((ppold = pmc_find_process_descriptor(curthread->td_proc,
4542 PMC_FLAG_NONE)) == NULL)
4543 goto done; /* nothing to do */
4546 for (ri = 0; ri < md->pmd_npmc; ri++)
4547 if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL)
4548 do_descendants |= pm->pm_flags & PMC_F_DESCENDANTS;
4549 if (do_descendants == 0) /* nothing to do */
4552 /* allocate a descriptor for the new process */
4553 if ((ppnew = pmc_find_process_descriptor(newproc,
4554 PMC_FLAG_ALLOCATE)) == NULL)
4558 * Run through all PMCs that were targeting the old process
4559 * and which specified F_DESCENDANTS and attach them to the
4562 * Log the fork event to all owners of PMCs attached to this
4563 * process, if not already logged.
4565 for (ri = 0; ri < md->pmd_npmc; ri++)
4566 if ((pm = ppold->pp_pmcs[ri].pp_pmc) != NULL &&
4567 (pm->pm_flags & PMC_F_DESCENDANTS)) {
4568 pmc_link_target_process(pm, ppnew);
4570 if (po->po_sscount == 0 &&
4571 po->po_flags & PMC_PO_OWNS_LOGFILE)
4572 pmclog_process_procfork(po, p1->p_pid,
4577 * Now mark the new process as being tracked by this driver.
4580 newproc->p_flag |= P_HWPMC;
4581 PROC_UNLOCK(newproc);
4584 sx_xunlock(&pmc_sx);
4588 pmc_kld_load(void *arg __unused, linker_file_t lf)
4590 struct pmc_owner *po;
4595 * Notify owners of system sampling PMCs about KLD operations.
4597 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4598 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4599 pmclog_process_map_in(po, (pid_t) -1,
4600 (uintfptr_t) lf->address, lf->filename);
4603 * TODO: Notify owners of (all) process-sampling PMCs too.
4606 sx_sunlock(&pmc_sx);
4610 pmc_kld_unload(void *arg __unused, const char *filename __unused,
4611 caddr_t address, size_t size)
4613 struct pmc_owner *po;
4617 LIST_FOREACH(po, &pmc_ss_owners, po_ssnext)
4618 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
4619 pmclog_process_map_out(po, (pid_t) -1,
4620 (uintfptr_t) address, (uintfptr_t) address + size);
4623 * TODO: Notify owners of process-sampling PMCs.
4626 sx_sunlock(&pmc_sx);
4633 static const char *pmc_name_of_pmcclass[] = {
4635 #define __PMC_CLASS(N) #N ,
4640 * Base class initializer: allocate structure and set default classes.
4643 pmc_mdep_alloc(int nclasses)
4645 struct pmc_mdep *md;
4648 /* SOFT + md classes */
4650 md = malloc(sizeof(struct pmc_mdep) + n *
4651 sizeof(struct pmc_classdep), M_PMC, M_WAITOK|M_ZERO);
4654 /* Add base class. */
4655 pmc_soft_initialize(md);
4660 pmc_mdep_free(struct pmc_mdep *md)
4662 pmc_soft_finalize(md);
4667 generic_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
4669 (void) pc; (void) pp;
4675 generic_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
4677 (void) pc; (void) pp;
4682 static struct pmc_mdep *
4683 pmc_generic_cpu_initialize(void)
4685 struct pmc_mdep *md;
4687 md = pmc_mdep_alloc(0);
4689 md->pmd_cputype = PMC_CPU_GENERIC;
4691 md->pmd_pcpu_init = NULL;
4692 md->pmd_pcpu_fini = NULL;
4693 md->pmd_switch_in = generic_switch_in;
4694 md->pmd_switch_out = generic_switch_out;
4700 pmc_generic_cpu_finalize(struct pmc_mdep *md)
4707 pmc_initialize(void)
4709 int c, cpu, error, n, ri;
4710 unsigned int maxcpu;
4711 struct pmc_binding pb;
4712 struct pmc_sample *ps;
4713 struct pmc_classdep *pcd;
4714 struct pmc_samplebuffer *sb;
4720 /* parse debug flags first */
4721 if (TUNABLE_STR_FETCH(PMC_SYSCTL_NAME_PREFIX "debugflags",
4722 pmc_debugstr, sizeof(pmc_debugstr)))
4723 pmc_debugflags_parse(pmc_debugstr,
4724 pmc_debugstr+strlen(pmc_debugstr));
4727 PMCDBG(MOD,INI,0, "PMC Initialize (version %x)", PMC_VERSION);
4729 /* check kernel version */
4730 if (pmc_kernel_version != PMC_VERSION) {
4731 if (pmc_kernel_version == 0)
4732 printf("hwpmc: this kernel has not been compiled with "
4733 "'options HWPMC_HOOKS'.\n");
4735 printf("hwpmc: kernel version (0x%x) does not match "
4736 "module version (0x%x).\n", pmc_kernel_version,
4738 return EPROGMISMATCH;
4742 * check sysctl parameters
4745 if (pmc_hashsize <= 0) {
4746 (void) printf("hwpmc: tunable \"hashsize\"=%d must be "
4747 "greater than zero.\n", pmc_hashsize);
4748 pmc_hashsize = PMC_HASH_SIZE;
4751 if (pmc_nsamples <= 0 || pmc_nsamples > 65535) {
4752 (void) printf("hwpmc: tunable \"nsamples\"=%d out of "
4753 "range.\n", pmc_nsamples);
4754 pmc_nsamples = PMC_NSAMPLES;
4757 if (pmc_callchaindepth <= 0 ||
4758 pmc_callchaindepth > PMC_CALLCHAIN_DEPTH_MAX) {
4759 (void) printf("hwpmc: tunable \"callchaindepth\"=%d out of "
4760 "range.\n", pmc_callchaindepth);
4761 pmc_callchaindepth = PMC_CALLCHAIN_DEPTH;
4764 md = pmc_md_initialize();
4766 /* Default to generic CPU. */
4767 md = pmc_generic_cpu_initialize();
4772 KASSERT(md->pmd_nclass >= 1 && md->pmd_npmc >= 1,
4773 ("[pmc,%d] no classes or pmcs", __LINE__));
4775 /* Compute the map from row-indices to classdep pointers. */
4776 pmc_rowindex_to_classdep = malloc(sizeof(struct pmc_classdep *) *
4777 md->pmd_npmc, M_PMC, M_WAITOK|M_ZERO);
4779 for (n = 0; n < md->pmd_npmc; n++)
4780 pmc_rowindex_to_classdep[n] = NULL;
4781 for (ri = c = 0; c < md->pmd_nclass; c++) {
4782 pcd = &md->pmd_classdep[c];
4783 for (n = 0; n < pcd->pcd_num; n++, ri++)
4784 pmc_rowindex_to_classdep[ri] = pcd;
4787 KASSERT(ri == md->pmd_npmc,
4788 ("[pmc,%d] npmc miscomputed: ri=%d, md->npmc=%d", __LINE__,
4791 maxcpu = pmc_cpu_max();
4793 /* allocate space for the per-cpu array */
4794 pmc_pcpu = malloc(maxcpu * sizeof(struct pmc_cpu *), M_PMC,
4797 /* per-cpu 'saved values' for managing process-mode PMCs */
4798 pmc_pcpu_saved = malloc(sizeof(pmc_value_t) * maxcpu * md->pmd_npmc,
4801 /* Perform CPU-dependent initialization. */
4802 pmc_save_cpu_binding(&pb);
4804 for (cpu = 0; error == 0 && cpu < maxcpu; cpu++) {
4805 if (!pmc_cpu_is_active(cpu))
4807 pmc_select_cpu(cpu);
4808 pmc_pcpu[cpu] = malloc(sizeof(struct pmc_cpu) +
4809 md->pmd_npmc * sizeof(struct pmc_hw *), M_PMC,
4811 if (md->pmd_pcpu_init)
4812 error = md->pmd_pcpu_init(md, cpu);
4813 for (n = 0; error == 0 && n < md->pmd_nclass; n++)
4814 error = md->pmd_classdep[n].pcd_pcpu_init(md, cpu);
4816 pmc_restore_cpu_binding(&pb);
4821 /* allocate space for the sample array */
4822 for (cpu = 0; cpu < maxcpu; cpu++) {
4823 if (!pmc_cpu_is_active(cpu))
4826 sb = malloc(sizeof(struct pmc_samplebuffer) +
4827 pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
4829 sb->ps_read = sb->ps_write = sb->ps_samples;
4830 sb->ps_fence = sb->ps_samples + pmc_nsamples;
4832 KASSERT(pmc_pcpu[cpu] != NULL,
4833 ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4835 sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples *
4836 sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO);
4838 for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
4839 ps->ps_pc = sb->ps_callchains +
4840 (n * pmc_callchaindepth);
4842 pmc_pcpu[cpu]->pc_sb[PMC_HR] = sb;
4844 sb = malloc(sizeof(struct pmc_samplebuffer) +
4845 pmc_nsamples * sizeof(struct pmc_sample), M_PMC,
4847 sb->ps_read = sb->ps_write = sb->ps_samples;
4848 sb->ps_fence = sb->ps_samples + pmc_nsamples;
4850 KASSERT(pmc_pcpu[cpu] != NULL,
4851 ("[pmc,%d] cpu=%d Null per-cpu data", __LINE__, cpu));
4853 sb->ps_callchains = malloc(pmc_callchaindepth * pmc_nsamples *
4854 sizeof(uintptr_t), M_PMC, M_WAITOK|M_ZERO);
4856 for (n = 0, ps = sb->ps_samples; n < pmc_nsamples; n++, ps++)
4857 ps->ps_pc = sb->ps_callchains +
4858 (n * pmc_callchaindepth);
4860 pmc_pcpu[cpu]->pc_sb[PMC_SR] = sb;
4863 /* allocate space for the row disposition array */
4864 pmc_pmcdisp = malloc(sizeof(enum pmc_mode) * md->pmd_npmc,
4865 M_PMC, M_WAITOK|M_ZERO);
4867 /* mark all PMCs as available */
4868 for (n = 0; n < (int) md->pmd_npmc; n++)
4869 PMC_MARK_ROW_FREE(n);
4871 /* allocate thread hash tables */
4872 pmc_ownerhash = hashinit(pmc_hashsize, M_PMC,
4873 &pmc_ownerhashmask);
4875 pmc_processhash = hashinit(pmc_hashsize, M_PMC,
4876 &pmc_processhashmask);
4877 mtx_init(&pmc_processhash_mtx, "pmc-process-hash", "pmc-leaf",
4880 LIST_INIT(&pmc_ss_owners);
4883 /* allocate a pool of spin mutexes */
4884 pmc_mtxpool = mtx_pool_create("pmc-leaf", pmc_mtxpool_size,
4887 PMCDBG(MOD,INI,1, "pmc_ownerhash=%p, mask=0x%lx "
4888 "targethash=%p mask=0x%lx", pmc_ownerhash, pmc_ownerhashmask,
4889 pmc_processhash, pmc_processhashmask);
4891 /* register process {exit,fork,exec} handlers */
4892 pmc_exit_tag = EVENTHANDLER_REGISTER(process_exit,
4893 pmc_process_exit, NULL, EVENTHANDLER_PRI_ANY);
4894 pmc_fork_tag = EVENTHANDLER_REGISTER(process_fork,
4895 pmc_process_fork, NULL, EVENTHANDLER_PRI_ANY);
4897 /* register kld event handlers */
4898 pmc_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, pmc_kld_load,
4899 NULL, EVENTHANDLER_PRI_ANY);
4900 pmc_kld_unload_tag = EVENTHANDLER_REGISTER(kld_unload, pmc_kld_unload,
4901 NULL, EVENTHANDLER_PRI_ANY);
4903 /* initialize logging */
4904 pmclog_initialize();
4906 /* set hook functions */
4907 pmc_intr = md->pmd_intr;
4908 pmc_hook = pmc_hook_handler;
4911 printf(PMC_MODULE_NAME ":");
4912 for (n = 0; n < (int) md->pmd_nclass; n++) {
4913 pcd = &md->pmd_classdep[n];
4914 printf(" %s/%d/%d/0x%b",
4915 pmc_name_of_pmcclass[pcd->pcd_class],
4920 "\1INT\2USR\3SYS\4EDG\5THR"
4921 "\6REA\7WRI\10INV\11QUA\12PRC"
4930 /* prepare to be unloaded */
4935 unsigned int maxcpu;
4936 struct pmc_ownerhash *ph;
4937 struct pmc_owner *po, *tmp;
4938 struct pmc_binding pb;
4940 struct pmc_processhash *prh;
4943 PMCDBG(MOD,INI,0, "%s", "cleanup");
4945 /* switch off sampling */
4946 CPU_ZERO(&pmc_cpumask);
4950 if (pmc_hook == NULL) { /* being unloaded already */
4951 sx_xunlock(&pmc_sx);
4955 pmc_hook = NULL; /* prevent new threads from entering module */
4957 /* deregister event handlers */
4958 EVENTHANDLER_DEREGISTER(process_fork, pmc_fork_tag);
4959 EVENTHANDLER_DEREGISTER(process_exit, pmc_exit_tag);
4960 EVENTHANDLER_DEREGISTER(kld_load, pmc_kld_load_tag);
4961 EVENTHANDLER_DEREGISTER(kld_unload, pmc_kld_unload_tag);
4963 /* send SIGBUS to all owner threads, free up allocations */
4965 for (ph = pmc_ownerhash;
4966 ph <= &pmc_ownerhash[pmc_ownerhashmask];
4968 LIST_FOREACH_SAFE(po, ph, po_next, tmp) {
4969 pmc_remove_owner(po);
4971 /* send SIGBUS to owner processes */
4972 PMCDBG(MOD,INI,2, "cleanup signal proc=%p "
4973 "(%d, %s)", po->po_owner,
4974 po->po_owner->p_pid,
4975 po->po_owner->p_comm);
4977 PROC_LOCK(po->po_owner);
4978 kern_psignal(po->po_owner, SIGBUS);
4979 PROC_UNLOCK(po->po_owner);
4981 pmc_destroy_owner_descriptor(po);
4985 /* reclaim allocated data structures */
4987 mtx_pool_destroy(&pmc_mtxpool);
4989 mtx_destroy(&pmc_processhash_mtx);
4990 if (pmc_processhash) {
4992 struct pmc_process *pp;
4994 PMCDBG(MOD,INI,3, "%s", "destroy process hash");
4995 for (prh = pmc_processhash;
4996 prh <= &pmc_processhash[pmc_processhashmask];
4998 LIST_FOREACH(pp, prh, pp_next)
4999 PMCDBG(MOD,INI,3, "pid=%d", pp->pp_proc->p_pid);
5002 hashdestroy(pmc_processhash, M_PMC, pmc_processhashmask);
5003 pmc_processhash = NULL;
5006 if (pmc_ownerhash) {
5007 PMCDBG(MOD,INI,3, "%s", "destroy owner hash");
5008 hashdestroy(pmc_ownerhash, M_PMC, pmc_ownerhashmask);
5009 pmc_ownerhash = NULL;
5012 KASSERT(LIST_EMPTY(&pmc_ss_owners),
5013 ("[pmc,%d] Global SS owner list not empty", __LINE__));
5014 KASSERT(pmc_ss_count == 0,
5015 ("[pmc,%d] Global SS count not empty", __LINE__));
5017 /* do processor and pmc-class dependent cleanup */
5018 maxcpu = pmc_cpu_max();
5020 PMCDBG(MOD,INI,3, "%s", "md cleanup");
5022 pmc_save_cpu_binding(&pb);
5023 for (cpu = 0; cpu < maxcpu; cpu++) {
5024 PMCDBG(MOD,INI,1,"pmc-cleanup cpu=%d pcs=%p",
5025 cpu, pmc_pcpu[cpu]);
5026 if (!pmc_cpu_is_active(cpu) || pmc_pcpu[cpu] == NULL)
5028 pmc_select_cpu(cpu);
5029 for (c = 0; c < md->pmd_nclass; c++)
5030 md->pmd_classdep[c].pcd_pcpu_fini(md, cpu);
5031 if (md->pmd_pcpu_fini)
5032 md->pmd_pcpu_fini(md, cpu);
5035 if (md->pmd_cputype == PMC_CPU_GENERIC)
5036 pmc_generic_cpu_finalize(md);
5038 pmc_md_finalize(md);
5042 pmc_restore_cpu_binding(&pb);
5045 /* Free per-cpu descriptors. */
5046 for (cpu = 0; cpu < maxcpu; cpu++) {
5047 if (!pmc_cpu_is_active(cpu))
5049 KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_HR] != NULL,
5050 ("[pmc,%d] Null hw cpu sample buffer cpu=%d", __LINE__,
5052 KASSERT(pmc_pcpu[cpu]->pc_sb[PMC_SR] != NULL,
5053 ("[pmc,%d] Null sw cpu sample buffer cpu=%d", __LINE__,
5055 free(pmc_pcpu[cpu]->pc_sb[PMC_HR]->ps_callchains, M_PMC);
5056 free(pmc_pcpu[cpu]->pc_sb[PMC_HR], M_PMC);
5057 free(pmc_pcpu[cpu]->pc_sb[PMC_SR]->ps_callchains, M_PMC);
5058 free(pmc_pcpu[cpu]->pc_sb[PMC_SR], M_PMC);
5059 free(pmc_pcpu[cpu], M_PMC);
5062 free(pmc_pcpu, M_PMC);
5065 free(pmc_pcpu_saved, M_PMC);
5066 pmc_pcpu_saved = NULL;
5069 free(pmc_pmcdisp, M_PMC);
5073 if (pmc_rowindex_to_classdep) {
5074 free(pmc_rowindex_to_classdep, M_PMC);
5075 pmc_rowindex_to_classdep = NULL;
5080 sx_xunlock(&pmc_sx); /* we are done */
5084 * The function called at load/unload.
5088 load (struct module *module __unused, int cmd, void *arg __unused)
5096 /* initialize the subsystem */
5097 error = pmc_initialize();
5100 PMCDBG(MOD,INI,1, "syscall=%d maxcpu=%d",
5101 pmc_syscall_num, pmc_cpu_max());
5108 PMCDBG(MOD,INI,1, "%s", "unloaded");
5112 error = EINVAL; /* XXX should panic(9) */
5120 MALLOC_DEFINE(M_PMC, "pmc", "Memory space for the PMC module");