xref: /freebsd/sys/dev/hwpmc/hwpmc_logging.c (revision d0ba1baed3f6e4936a0c1b89c25f6c59168ef6de)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2007 Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * Copyright (c) 2018 Matthew Macy
7  * All rights reserved.
8  *
9  * Portions of this software were developed by A. Joseph Koshy under
10  * sponsorship from the FreeBSD Foundation and Google, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34 
35 /*
36  * Logging code for hwpmc(4)
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/capsicum.h>
44 #include <sys/file.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/lock.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/pmc.h>
51 #include <sys/pmckern.h>
52 #include <sys/pmclog.h>
53 #include <sys/proc.h>
54 #include <sys/sched.h>
55 #include <sys/signalvar.h>
56 #include <sys/smp.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 #include <sys/uio.h>
61 #include <sys/unistd.h>
62 #include <sys/vnode.h>
63 
64 #ifdef NUMA
65 #define NDOMAINS vm_ndomains
66 #define curdomain PCPU_GET(domain)
67 #else
68 #define NDOMAINS 1
69 #define curdomain 0
70 #define malloc_domain(size, type, domain, flags) malloc((size), (type), (flags))
71 #define free_domain(addr, type) free(addr, type)
72 #endif
73 
74 /*
75  * Sysctl tunables
76  */
77 
78 SYSCTL_DECL(_kern_hwpmc);
79 
80 /*
81  * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
82  */
83 
84 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
85 #if (__FreeBSD_version < 1100000)
86 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
87 #endif
88 SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RDTUN,
89     &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
90 
91 /*
92  * kern.hwpmc.nbuffer -- number of global log buffers
93  */
94 
95 static int pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
96 #if (__FreeBSD_version < 1100000)
97 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers_pcpu);
98 #endif
99 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers_pcpu, CTLFLAG_RDTUN,
100     &pmc_nlogbuffers_pcpu, 0, "number of log buffers per cpu");
101 
102 /*
103  * Global log buffer list and associated spin lock.
104  */
105 
106 static struct mtx pmc_kthread_mtx;	/* sleep lock */
107 
108 #define	PMCLOG_INIT_BUFFER_DESCRIPTOR(D, buf, domain) do {						\
109 		(D)->plb_fence = ((char *) (buf)) +	1024*pmclog_buffer_size;			\
110 		(D)->plb_base  = (D)->plb_ptr = ((char *) (buf));				\
111 		(D)->plb_domain = domain; \
112 	} while (0)
113 
114 #define	PMCLOG_RESET_BUFFER_DESCRIPTOR(D) do {			\
115 		(D)->plb_ptr  = (D)->plb_base; \
116 	} while (0)
117 
118 /*
119  * Log file record constructors.
120  */
121 #define	_PMCLOG_TO_HEADER(T,L)						\
122 	((PMCLOG_HEADER_MAGIC << 24) |					\
123 	 (PMCLOG_TYPE_ ## T << 16)   |					\
124 	 ((L) & 0xFFFF))
125 
126 /* reserve LEN bytes of space and initialize the entry header */
127 #define	_PMCLOG_RESERVE_SAFE(PO,TYPE,LEN,ACTION) do {			\
128 		uint32_t *_le;						\
129 		int _len = roundup((LEN), sizeof(uint32_t));	\
130 		if ((_le = pmclog_reserve((PO), _len)) == NULL) {	\
131 			ACTION;						\
132 		}							\
133 		*_le = _PMCLOG_TO_HEADER(TYPE,_len);			\
134 		_le += 3	/* skip over timestamp */
135 
136 /* reserve LEN bytes of space and initialize the entry header */
137 #define	_PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do {			\
138 		uint32_t *_le;						\
139 		int _len = roundup((LEN), sizeof(uint32_t));		\
140 		spinlock_enter();									\
141 		if ((_le = pmclog_reserve((PO), _len)) == NULL) {	\
142 			spinlock_exit();								\
143 			ACTION;											\
144 		}												\
145 		*_le = _PMCLOG_TO_HEADER(TYPE,_len);			\
146 		_le += 3	/* skip over timestamp */
147 
148 
149 #define	PMCLOG_RESERVE_SAFE(P,T,L)		_PMCLOG_RESERVE_SAFE(P,T,L,return)
150 #define	PMCLOG_RESERVE(P,T,L)		_PMCLOG_RESERVE(P,T,L,return)
151 #define	PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L,		\
152 	error=ENOMEM;goto error)
153 
154 #define	PMCLOG_EMIT32(V)	do { *_le++ = (V); } while (0)
155 #define	PMCLOG_EMIT64(V)	do { 					\
156 		*_le++ = (uint32_t) ((V) & 0xFFFFFFFF);			\
157 		*_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF);		\
158 	} while (0)
159 
160 
161 /* Emit a string.  Caution: does NOT update _le, so needs to be last */
162 #define	PMCLOG_EMITSTRING(S,L)	do { bcopy((S), _le, (L)); } while (0)
163 #define	PMCLOG_EMITNULLSTRING(L) do { bzero(_le, (L)); } while (0)
164 
165 #define	PMCLOG_DESPATCH_SAFE(PO)						\
166 	    pmclog_release((PO));						\
167 	} while (0)
168 
169 #define	PMCLOG_DESPATCH(PO)							\
170 	    pmclog_release((PO));						\
171 		spinlock_exit();							\
172 	} while (0)
173 
174 #define	PMCLOG_DESPATCH_SYNC(PO)						\
175 	    pmclog_schedule_io((PO));						\
176 		spinlock_exit();								\
177 		} while (0)
178 
179 
180 /*
181  * Assertions about the log file format.
182  */
183 
184 CTASSERT(sizeof(struct pmclog_callchain) == 6*4 +
185     PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t));
186 CTASSERT(sizeof(struct pmclog_closelog) == 3*4);
187 CTASSERT(sizeof(struct pmclog_dropnotify) == 3*4);
188 CTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX +
189     4*4 + sizeof(uintfptr_t));
190 CTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
191     4*4 + sizeof(uintfptr_t));
192 CTASSERT(sizeof(struct pmclog_map_out) == 4*4 + 2*sizeof(uintfptr_t));
193 CTASSERT(sizeof(struct pmclog_pcsample) == 6*4 + sizeof(uintfptr_t));
194 CTASSERT(sizeof(struct pmclog_pmcallocate) == 6*4);
195 CTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX);
196 CTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4);
197 CTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4);
198 CTASSERT(sizeof(struct pmclog_proccsw) == 5*4 + 8);
199 CTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
200     sizeof(uintfptr_t));
201 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
202     sizeof(uintfptr_t));
203 CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8);
204 CTASSERT(sizeof(struct pmclog_procfork) == 5*4);
205 CTASSERT(sizeof(struct pmclog_sysexit) == 4*4);
206 CTASSERT(sizeof(struct pmclog_userdata) == 4*4);
207 
208 /*
209  * Log buffer structure
210  */
211 
212 struct pmclog_buffer {
213 	TAILQ_ENTRY(pmclog_buffer) plb_next;
214 	char 		*plb_base;
215 	char		*plb_ptr;
216 	char 		*plb_fence;
217 	uint16_t	 plb_domain;
218 } __aligned(CACHE_LINE_SIZE);
219 
220 /*
221  * Prototypes
222  */
223 
224 static int pmclog_get_buffer(struct pmc_owner *po);
225 static void pmclog_loop(void *arg);
226 static void pmclog_release(struct pmc_owner *po);
227 static uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
228 static void pmclog_schedule_io(struct pmc_owner *po);
229 static void pmclog_schedule_all(struct pmc_owner *po);
230 static void pmclog_stop_kthread(struct pmc_owner *po);
231 
232 /*
233  * Helper functions
234  */
235 
236 static inline void
237 pmc_plb_rele_unlocked(struct pmclog_buffer *plb)
238 {
239 	TAILQ_INSERT_HEAD(&pmc_dom_hdrs[plb->plb_domain]->pdbh_head, plb, plb_next);
240 }
241 
242 static inline void
243 pmc_plb_rele(struct pmclog_buffer *plb)
244 {
245 	mtx_lock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx);
246 	pmc_plb_rele_unlocked(plb);
247 	mtx_unlock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx);
248 }
249 
250 
251 /*
252  * Get a log buffer
253  */
254 
255 static int
256 pmclog_get_buffer(struct pmc_owner *po)
257 {
258 	struct pmclog_buffer *plb;
259 	int domain;
260 
261 	KASSERT(po->po_curbuf[curcpu] == NULL,
262 	    ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
263 
264 	domain = curdomain;
265 	MPASS(pmc_dom_hdrs[domain]);
266 	mtx_lock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx);
267 	if ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL)
268 		TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next);
269 	mtx_unlock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx);
270 
271 	PMCDBG2(LOG,GTB,1, "po=%p plb=%p", po, plb);
272 
273 #ifdef	HWPMC_DEBUG
274 	if (plb)
275 		KASSERT(plb->plb_ptr == plb->plb_base &&
276 		    plb->plb_base < plb->plb_fence,
277 		    ("[pmclog,%d] po=%p buffer invariants: ptr=%p "
278 		    "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
279 		    plb->plb_base, plb->plb_fence));
280 #endif
281 
282 	po->po_curbuf[curcpu] = plb;
283 
284 	/* update stats */
285 	counter_u64_add(pmc_stats.pm_buffer_requests, 1);
286 	if (plb == NULL)
287 		counter_u64_add(pmc_stats.pm_buffer_requests_failed, 1);
288 
289 	return (plb ? 0 : ENOMEM);
290 }
291 
292 struct pmclog_proc_init_args {
293 	struct proc *kthr;
294 	struct pmc_owner *po;
295 	bool exit;
296 	bool acted;
297 };
298 
299 int
300 pmclog_proc_create(struct thread *td, void **handlep)
301 {
302 	struct pmclog_proc_init_args *ia;
303 	int error;
304 
305 	ia = malloc(sizeof(*ia), M_TEMP, M_WAITOK | M_ZERO);
306 	error = kproc_create(pmclog_loop, ia, &ia->kthr,
307 	    RFHIGHPID, 0, "hwpmc: proc(%d)", td->td_proc->p_pid);
308 	if (error == 0)
309 		*handlep = ia;
310 	return (error);
311 }
312 
313 void
314 pmclog_proc_ignite(void *handle, struct pmc_owner *po)
315 {
316 	struct pmclog_proc_init_args *ia;
317 
318 	ia = handle;
319 	mtx_lock(&pmc_kthread_mtx);
320 	MPASS(!ia->acted);
321 	MPASS(ia->po == NULL);
322 	MPASS(!ia->exit);
323 	MPASS(ia->kthr != NULL);
324 	if (po == NULL) {
325 		ia->exit = true;
326 	} else {
327 		ia->po = po;
328 		KASSERT(po->po_kthread == NULL,
329 		    ("[pmclog,%d] po=%p kthread (%p) already present",
330 		    __LINE__, po, po->po_kthread));
331 		po->po_kthread = ia->kthr;
332 	}
333 	wakeup(ia);
334 	while (!ia->acted)
335 		msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogw", 0);
336 	mtx_unlock(&pmc_kthread_mtx);
337 	free(ia, M_TEMP);
338 }
339 
340 /*
341  * Log handler loop.
342  *
343  * This function is executed by each pmc owner's helper thread.
344  */
345 
346 static void
347 pmclog_loop(void *arg)
348 {
349 	struct pmclog_proc_init_args *ia;
350 	struct pmc_owner *po;
351 	struct pmclog_buffer *lb;
352 	struct proc *p;
353 	struct ucred *ownercred;
354 	struct ucred *mycred;
355 	struct thread *td;
356 	sigset_t unb;
357 	struct uio auio;
358 	struct iovec aiov;
359 	size_t nbytes;
360 	int error;
361 
362 	td = curthread;
363 
364 	SIGEMPTYSET(unb);
365 	SIGADDSET(unb, SIGHUP);
366 	(void)kern_sigprocmask(td, SIG_UNBLOCK, &unb, NULL, 0);
367 
368 	ia = arg;
369 	MPASS(ia->kthr == curproc);
370 	MPASS(!ia->acted);
371 	mtx_lock(&pmc_kthread_mtx);
372 	while (ia->po == NULL && !ia->exit)
373 		msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogi", 0);
374 	if (ia->exit) {
375 		ia->acted = true;
376 		wakeup(ia);
377 		mtx_unlock(&pmc_kthread_mtx);
378 		kproc_exit(0);
379 	}
380 	MPASS(ia->po != NULL);
381 	po = ia->po;
382 	ia->acted = true;
383 	wakeup(ia);
384 	mtx_unlock(&pmc_kthread_mtx);
385 	ia = NULL;
386 
387 	p = po->po_owner;
388 	mycred = td->td_ucred;
389 
390 	PROC_LOCK(p);
391 	ownercred = crhold(p->p_ucred);
392 	PROC_UNLOCK(p);
393 
394 	PMCDBG2(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
395 	KASSERT(po->po_kthread == curthread->td_proc,
396 	    ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
397 		po, po->po_kthread, curthread->td_proc));
398 
399 	lb = NULL;
400 
401 
402 	/*
403 	 * Loop waiting for I/O requests to be added to the owner
404 	 * struct's queue.  The loop is exited when the log file
405 	 * is deconfigured.
406 	 */
407 
408 	mtx_lock(&pmc_kthread_mtx);
409 
410 	for (;;) {
411 
412 		/* check if we've been asked to exit */
413 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
414 			break;
415 
416 		if (lb == NULL) { /* look for a fresh buffer to write */
417 			mtx_lock_spin(&po->po_mtx);
418 			if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
419 				mtx_unlock_spin(&po->po_mtx);
420 
421 				/* No more buffers and shutdown required. */
422 				if (po->po_flags & PMC_PO_SHUTDOWN)
423 					break;
424 
425 				(void) msleep(po, &pmc_kthread_mtx, PWAIT,
426 				    "pmcloop", 0);
427 				continue;
428 			}
429 
430 			TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
431 			mtx_unlock_spin(&po->po_mtx);
432 		}
433 
434 		mtx_unlock(&pmc_kthread_mtx);
435 
436 		/* process the request */
437 		PMCDBG3(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
438 		    lb->plb_base, lb->plb_ptr);
439 		/* change our thread's credentials before issuing the I/O */
440 
441 		aiov.iov_base = lb->plb_base;
442 		aiov.iov_len  = nbytes = lb->plb_ptr - lb->plb_base;
443 
444 		auio.uio_iov    = &aiov;
445 		auio.uio_iovcnt = 1;
446 		auio.uio_offset = -1;
447 		auio.uio_resid  = nbytes;
448 		auio.uio_rw     = UIO_WRITE;
449 		auio.uio_segflg = UIO_SYSSPACE;
450 		auio.uio_td     = td;
451 
452 		/* switch thread credentials -- see kern_ktrace.c */
453 		td->td_ucred = ownercred;
454 		error = fo_write(po->po_file, &auio, ownercred, 0, td);
455 		td->td_ucred = mycred;
456 
457 		if (error) {
458 			/* XXX some errors are recoverable */
459 			/* send a SIGIO to the owner and exit */
460 			PROC_LOCK(p);
461 			kern_psignal(p, SIGIO);
462 			PROC_UNLOCK(p);
463 
464 			mtx_lock(&pmc_kthread_mtx);
465 
466 			po->po_error = error; /* save for flush log */
467 
468 			PMCDBG2(LOG,WRI,2, "po=%p error=%d", po, error);
469 
470 			break;
471 		}
472 
473 		mtx_lock(&pmc_kthread_mtx);
474 
475 		/* put the used buffer back into the global pool */
476 		PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
477 
478 		pmc_plb_rele(lb);
479 		lb = NULL;
480 	}
481 
482 	wakeup_one(po->po_kthread);
483 	po->po_kthread = NULL;
484 
485 	mtx_unlock(&pmc_kthread_mtx);
486 
487 	/* return the current I/O buffer to the global pool */
488 	if (lb) {
489 		PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
490 
491 		pmc_plb_rele(lb);
492 	}
493 
494 	/*
495 	 * Exit this thread, signalling the waiter
496 	 */
497 
498 	crfree(ownercred);
499 
500 	kproc_exit(0);
501 }
502 
503 /*
504  * Release and log entry and schedule an I/O if needed.
505  */
506 
507 static void
508 pmclog_release(struct pmc_owner *po)
509 {
510 	struct pmclog_buffer *plb;
511 
512 	plb = po->po_curbuf[curcpu];
513 	KASSERT(plb->plb_ptr >= plb->plb_base,
514 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
515 		po, plb->plb_ptr, plb->plb_base));
516 	KASSERT(plb->plb_ptr <= plb->plb_fence,
517 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
518 		po, plb->plb_ptr, plb->plb_fence));
519 
520 	/* schedule an I/O if we've filled a buffer */
521 	if (plb->plb_ptr >= plb->plb_fence)
522 		pmclog_schedule_io(po);
523 
524 	PMCDBG1(LOG,REL,1, "po=%p", po);
525 }
526 
527 
528 /*
529  * Attempt to reserve 'length' bytes of space in an owner's log
530  * buffer.  The function returns a pointer to 'length' bytes of space
531  * if there was enough space or returns NULL if no space was
532  * available.  Non-null returns do so with the po mutex locked.  The
533  * caller must invoke pmclog_release() on the pmc owner structure
534  * when done.
535  */
536 
537 static uint32_t *
538 pmclog_reserve(struct pmc_owner *po, int length)
539 {
540 	uintptr_t newptr, oldptr;
541 	uint32_t *lh;
542 	struct timespec ts;
543 	struct pmclog_buffer *plb, **pplb;
544 
545 	PMCDBG2(LOG,ALL,1, "po=%p len=%d", po, length);
546 
547 	KASSERT(length % sizeof(uint32_t) == 0,
548 	    ("[pmclog,%d] length not a multiple of word size", __LINE__));
549 
550 	/* No more data when shutdown in progress. */
551 	if (po->po_flags & PMC_PO_SHUTDOWN)
552 		return (NULL);
553 
554 	pplb = &po->po_curbuf[curcpu];
555 	if (*pplb == NULL && pmclog_get_buffer(po) != 0)
556 		goto fail;
557 
558 	KASSERT(*pplb != NULL,
559 	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
560 
561 	plb = *pplb;
562 	KASSERT(plb->plb_ptr >= plb->plb_base &&
563 	    plb->plb_ptr <= plb->plb_fence,
564 	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
565 		__LINE__, po, plb->plb_ptr, plb->plb_base,
566 		plb->plb_fence));
567 
568 	oldptr = (uintptr_t) plb->plb_ptr;
569 	newptr = oldptr + length;
570 
571 	KASSERT(oldptr != (uintptr_t) NULL,
572 	    ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
573 
574 	/*
575 	 * If we have space in the current buffer, return a pointer to
576 	 * available space with the PO structure locked.
577 	 */
578 	if (newptr <= (uintptr_t) plb->plb_fence) {
579 		plb->plb_ptr = (char *) newptr;
580 		goto done;
581 	}
582 
583 	/*
584 	 * Otherwise, schedule the current buffer for output and get a
585 	 * fresh buffer.
586 	 */
587 	pmclog_schedule_io(po);
588 
589 	if (pmclog_get_buffer(po) != 0)
590 		goto fail;
591 
592 	plb = *pplb;
593 	KASSERT(plb != NULL,
594 	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
595 
596 	KASSERT(plb->plb_ptr != NULL,
597 	    ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
598 
599 	KASSERT(plb->plb_ptr == plb->plb_base &&
600 	    plb->plb_ptr <= plb->plb_fence,
601 	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
602 		__LINE__, po, plb->plb_ptr, plb->plb_base,
603 		plb->plb_fence));
604 
605 	oldptr = (uintptr_t) plb->plb_ptr;
606 
607  done:
608 	lh = (uint32_t *) oldptr;
609 	lh++;				/* skip header */
610 	getnanotime(&ts);		/* fill in the timestamp */
611 	*lh++ = ts.tv_sec & 0xFFFFFFFF;
612 	*lh++ = ts.tv_nsec & 0xFFFFFFF;
613 	return ((uint32_t *) oldptr);
614  fail:
615 	return (NULL);
616 }
617 
618 /*
619  * Schedule an I/O.
620  *
621  * Transfer the current buffer to the helper kthread.
622  */
623 
624 static void
625 pmclog_schedule_io(struct pmc_owner *po)
626 {
627 	struct pmclog_buffer *plb;
628 
629 	plb = po->po_curbuf[curcpu];
630 	po->po_curbuf[curcpu] = NULL;
631 	KASSERT(plb != NULL,
632 	    ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
633 	KASSERT(plb->plb_ptr >= plb->plb_base,
634 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
635 		po, plb->plb_ptr, plb->plb_base));
636 	KASSERT(plb->plb_ptr <= plb->plb_fence,
637 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
638 		po, plb->plb_ptr, plb->plb_fence));
639 
640 	PMCDBG1(LOG,SIO, 1, "po=%p", po);
641 
642 	/*
643 	 * Add the current buffer to the tail of the buffer list and
644 	 * wakeup the helper.
645 	 */
646 	mtx_lock_spin(&po->po_mtx);
647 	TAILQ_INSERT_TAIL(&po->po_logbuffers, plb, plb_next);
648 	mtx_unlock_spin(&po->po_mtx);
649 	wakeup_one(po);
650 }
651 
652 /*
653  * Stop the helper kthread.
654  */
655 
656 static void
657 pmclog_stop_kthread(struct pmc_owner *po)
658 {
659 
660 	mtx_lock(&pmc_kthread_mtx);
661 	po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
662 	if (po->po_kthread != NULL) {
663 		PROC_LOCK(po->po_kthread);
664 		kern_psignal(po->po_kthread, SIGHUP);
665 		PROC_UNLOCK(po->po_kthread);
666 	}
667 	wakeup_one(po);
668 	while (po->po_kthread)
669 		msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
670 	mtx_unlock(&pmc_kthread_mtx);
671 }
672 
673 /*
674  * Public functions
675  */
676 
677 /*
678  * Configure a log file for pmc owner 'po'.
679  *
680  * Parameter 'logfd' is a file handle referencing an open file in the
681  * owner process.  This file needs to have been opened for writing.
682  */
683 
684 int
685 pmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
686 {
687 	struct proc *p;
688 	int error;
689 
690 	sx_assert(&pmc_sx, SA_XLOCKED);
691 	PMCDBG2(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
692 
693 	p = po->po_owner;
694 
695 	/* return EBUSY if a log file was already present */
696 	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
697 		return (EBUSY);
698 
699 	KASSERT(po->po_file == NULL,
700 	    ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
701 		po->po_file));
702 
703 	/* get a reference to the file state */
704 	error = fget_write(curthread, logfd, &cap_write_rights, &po->po_file);
705 	if (error)
706 		goto error;
707 
708 	/* mark process as owning a log file */
709 	po->po_flags |= PMC_PO_OWNS_LOGFILE;
710 
711 	/* mark process as using HWPMCs */
712 	PROC_LOCK(p);
713 	p->p_flag |= P_HWPMC;
714 	PROC_UNLOCK(p);
715 
716 	/* create a log initialization entry */
717 	PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
718 	    sizeof(struct pmclog_initialize));
719 	PMCLOG_EMIT32(PMC_VERSION);
720 	PMCLOG_EMIT32(md->pmd_cputype);
721 	PMCLOG_DESPATCH_SYNC(po);
722 
723 	return (0);
724 
725  error:
726 	KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
727 	    "stopped", __LINE__, po));
728 
729 	if (po->po_file)
730 		(void) fdrop(po->po_file, curthread);
731 	po->po_file  = NULL;	/* clear file and error state */
732 	po->po_error = 0;
733 	po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
734 
735 	return (error);
736 }
737 
738 
739 /*
740  * De-configure a log file.  This will throw away any buffers queued
741  * for this owner process.
742  */
743 
744 int
745 pmclog_deconfigure_log(struct pmc_owner *po)
746 {
747 	int error;
748 	struct pmclog_buffer *lb;
749 
750 	PMCDBG1(LOG,CFG,1, "de-config po=%p", po);
751 
752 	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
753 		return (EINVAL);
754 
755 	KASSERT(po->po_sscount == 0,
756 	    ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
757 	KASSERT(po->po_file != NULL,
758 	    ("[pmclog,%d] po=%p no log file", __LINE__, po));
759 
760 	/* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
761 	pmclog_stop_kthread(po);
762 
763 	KASSERT(po->po_kthread == NULL,
764 	    ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
765 
766 	/* return all queued log buffers to the global pool */
767 	while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
768 		TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
769 		PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
770 		pmc_plb_rele(lb);
771 	}
772 	for (int i = 0; i < mp_ncpus; i++) {
773 		thread_lock(curthread);
774 		sched_bind(curthread, i);
775 		thread_unlock(curthread);
776 		/* return the 'current' buffer to the global pool */
777 		if ((lb = po->po_curbuf[curcpu]) != NULL) {
778 			PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
779 			pmc_plb_rele(lb);
780 		}
781 	}
782 	thread_lock(curthread);
783 	sched_unbind(curthread);
784 	thread_unlock(curthread);
785 
786 	/* drop a reference to the fd */
787 	if (po->po_file != NULL) {
788 		error = fdrop(po->po_file, curthread);
789 		po->po_file = NULL;
790 	} else
791 		error = 0;
792 	po->po_error = 0;
793 
794 	return (error);
795 }
796 
797 /*
798  * Flush a process' log buffer.
799  */
800 
801 int
802 pmclog_flush(struct pmc_owner *po)
803 {
804 	int error;
805 
806 	PMCDBG1(LOG,FLS,1, "po=%p", po);
807 
808 	/*
809 	 * If there is a pending error recorded by the logger thread,
810 	 * return that.
811 	 */
812 	if (po->po_error)
813 		return (po->po_error);
814 
815 	error = 0;
816 
817 	/*
818 	 * Check that we do have an active log file.
819 	 */
820 	mtx_lock(&pmc_kthread_mtx);
821 	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
822 		error = EINVAL;
823 		goto error;
824 	}
825 
826 	pmclog_schedule_all(po);
827  error:
828 	mtx_unlock(&pmc_kthread_mtx);
829 
830 	return (error);
831 }
832 
833 static void
834 pmclog_schedule_one_cond(void *arg)
835 {
836 	struct pmc_owner *po = arg;
837 	struct pmclog_buffer *plb;
838 
839 	spinlock_enter();
840 	/* tell hardclock not to run again */
841 	if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
842 		PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
843 	plb = po->po_curbuf[curcpu];
844 	if (plb && plb->plb_ptr != plb->plb_base)
845 		pmclog_schedule_io(po);
846 	spinlock_exit();
847 }
848 
849 static void
850 pmclog_schedule_all(struct pmc_owner *po)
851 {
852 	/*
853 	 * Schedule the current buffer if any and not empty.
854 	 */
855 	for (int i = 0; i < mp_ncpus; i++) {
856 		thread_lock(curthread);
857 		sched_bind(curthread, i);
858 		thread_unlock(curthread);
859 		pmclog_schedule_one_cond(po);
860 	}
861 	thread_lock(curthread);
862 	sched_unbind(curthread);
863 	thread_unlock(curthread);
864 }
865 
866 int
867 pmclog_close(struct pmc_owner *po)
868 {
869 
870 	PMCDBG1(LOG,CLO,1, "po=%p", po);
871 
872 	pmclog_process_closelog(po);
873 
874 	mtx_lock(&pmc_kthread_mtx);
875 
876 	/*
877 	 * Schedule the current buffer.
878 	 */
879 	pmclog_schedule_all(po);
880 	wakeup_one(po);
881 
882 	/*
883 	 * Initiate shutdown: no new data queued,
884 	 * thread will close file on last block.
885 	 */
886 	po->po_flags |= PMC_PO_SHUTDOWN;
887 	mtx_unlock(&pmc_kthread_mtx);
888 
889 	return (0);
890 }
891 
892 void
893 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
894 {
895 	int n, recordlen;
896 	uint32_t flags;
897 	struct pmc_owner *po;
898 
899 	PMCDBG3(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
900 	    ps->ps_nsamples);
901 
902 	recordlen = offsetof(struct pmclog_callchain, pl_pc) +
903 	    ps->ps_nsamples * sizeof(uintfptr_t);
904 	po = pm->pm_owner;
905 	flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
906 	PMCLOG_RESERVE_SAFE(po, CALLCHAIN, recordlen);
907 	PMCLOG_EMIT32(ps->ps_pid);
908 	PMCLOG_EMIT32(pm->pm_id);
909 	PMCLOG_EMIT32(flags);
910 	for (n = 0; n < ps->ps_nsamples; n++)
911 		PMCLOG_EMITADDR(ps->ps_pc[n]);
912 	PMCLOG_DESPATCH_SAFE(po);
913 }
914 
915 void
916 pmclog_process_closelog(struct pmc_owner *po)
917 {
918 	PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
919 	PMCLOG_DESPATCH_SYNC(po);
920 }
921 
922 void
923 pmclog_process_dropnotify(struct pmc_owner *po)
924 {
925 	PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
926 	PMCLOG_DESPATCH(po);
927 }
928 
929 void
930 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
931     const char *path)
932 {
933 	int pathlen, recordlen;
934 
935 	KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
936 
937 	pathlen = strlen(path) + 1;	/* #bytes for path name */
938 	recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
939 	    pathlen;
940 
941 	PMCLOG_RESERVE(po, MAP_IN, recordlen);
942 	PMCLOG_EMIT32(pid);
943 	PMCLOG_EMITADDR(start);
944 	PMCLOG_EMITSTRING(path,pathlen);
945 	PMCLOG_DESPATCH(po);
946 }
947 
948 void
949 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
950     uintfptr_t end)
951 {
952 	KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
953 
954 	PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
955 	PMCLOG_EMIT32(pid);
956 	PMCLOG_EMITADDR(start);
957 	PMCLOG_EMITADDR(end);
958 	PMCLOG_DESPATCH(po);
959 }
960 
961 void
962 pmclog_process_pmcallocate(struct pmc *pm)
963 {
964 	struct pmc_owner *po;
965 	struct pmc_soft *ps;
966 
967 	po = pm->pm_owner;
968 
969 	PMCDBG1(LOG,ALL,1, "pm=%p", pm);
970 
971 	if (PMC_TO_CLASS(pm) == PMC_CLASS_SOFT) {
972 		PMCLOG_RESERVE(po, PMCALLOCATEDYN,
973 		    sizeof(struct pmclog_pmcallocatedyn));
974 		PMCLOG_EMIT32(pm->pm_id);
975 		PMCLOG_EMIT32(pm->pm_event);
976 		PMCLOG_EMIT32(pm->pm_flags);
977 		ps = pmc_soft_ev_acquire(pm->pm_event);
978 		if (ps != NULL)
979 			PMCLOG_EMITSTRING(ps->ps_ev.pm_ev_name,PMC_NAME_MAX);
980 		else
981 			PMCLOG_EMITNULLSTRING(PMC_NAME_MAX);
982 		pmc_soft_ev_release(ps);
983 		PMCLOG_DESPATCH_SYNC(po);
984 	} else {
985 		PMCLOG_RESERVE(po, PMCALLOCATE,
986 		    sizeof(struct pmclog_pmcallocate));
987 		PMCLOG_EMIT32(pm->pm_id);
988 		PMCLOG_EMIT32(pm->pm_event);
989 		PMCLOG_EMIT32(pm->pm_flags);
990 		PMCLOG_DESPATCH_SYNC(po);
991 	}
992 }
993 
994 void
995 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
996 {
997 	int pathlen, recordlen;
998 	struct pmc_owner *po;
999 
1000 	PMCDBG2(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
1001 
1002 	po = pm->pm_owner;
1003 
1004 	pathlen = strlen(path) + 1;	/* #bytes for the string */
1005 	recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
1006 
1007 	PMCLOG_RESERVE(po, PMCATTACH, recordlen);
1008 	PMCLOG_EMIT32(pm->pm_id);
1009 	PMCLOG_EMIT32(pid);
1010 	PMCLOG_EMITSTRING(path, pathlen);
1011 	PMCLOG_DESPATCH_SYNC(po);
1012 }
1013 
1014 void
1015 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
1016 {
1017 	struct pmc_owner *po;
1018 
1019 	PMCDBG2(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
1020 
1021 	po = pm->pm_owner;
1022 
1023 	PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
1024 	PMCLOG_EMIT32(pm->pm_id);
1025 	PMCLOG_EMIT32(pid);
1026 	PMCLOG_DESPATCH_SYNC(po);
1027 }
1028 
1029 /*
1030  * Log a context switch event to the log file.
1031  */
1032 
1033 void
1034 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
1035 {
1036 	struct pmc_owner *po;
1037 
1038 	KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
1039 	    ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
1040 
1041 	PMCDBG3(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
1042 	    v);
1043 
1044 	po = pm->pm_owner;
1045 
1046 	PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
1047 	PMCLOG_EMIT32(pm->pm_id);
1048 	PMCLOG_EMIT64(v);
1049 	PMCLOG_EMIT32(pp->pp_proc->p_pid);
1050 	PMCLOG_DESPATCH(po);
1051 }
1052 
1053 void
1054 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
1055     uintfptr_t startaddr, char *path)
1056 {
1057 	int pathlen, recordlen;
1058 
1059 	PMCDBG3(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
1060 
1061 	pathlen   = strlen(path) + 1;	/* #bytes for the path */
1062 	recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
1063 
1064 	PMCLOG_RESERVE(po, PROCEXEC, recordlen);
1065 	PMCLOG_EMIT32(pid);
1066 	PMCLOG_EMITADDR(startaddr);
1067 	PMCLOG_EMIT32(pmid);
1068 	PMCLOG_EMITSTRING(path,pathlen);
1069 	PMCLOG_DESPATCH(po);
1070 }
1071 
1072 /*
1073  * Log a process exit event (and accumulated pmc value) to the log file.
1074  */
1075 
1076 void
1077 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
1078 {
1079 	int ri;
1080 	struct pmc_owner *po;
1081 
1082 	ri = PMC_TO_ROWINDEX(pm);
1083 	PMCDBG3(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
1084 	    pp->pp_pmcs[ri].pp_pmcval);
1085 
1086 	po = pm->pm_owner;
1087 
1088 	PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
1089 	PMCLOG_EMIT32(pm->pm_id);
1090 	PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
1091 	PMCLOG_EMIT32(pp->pp_proc->p_pid);
1092 	PMCLOG_DESPATCH(po);
1093 }
1094 
1095 /*
1096  * Log a fork event.
1097  */
1098 
1099 void
1100 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
1101 {
1102 	PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
1103 	PMCLOG_EMIT32(oldpid);
1104 	PMCLOG_EMIT32(newpid);
1105 	PMCLOG_DESPATCH(po);
1106 }
1107 
1108 /*
1109  * Log a process exit event of the form suitable for system-wide PMCs.
1110  */
1111 
1112 void
1113 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
1114 {
1115 	PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
1116 	PMCLOG_EMIT32(pid);
1117 	PMCLOG_DESPATCH(po);
1118 }
1119 
1120 /*
1121  * Write a user log entry.
1122  */
1123 
1124 int
1125 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
1126 {
1127 	int error;
1128 
1129 	PMCDBG2(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
1130 
1131 	error = 0;
1132 
1133 	PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
1134 	    sizeof(struct pmclog_userdata));
1135 	PMCLOG_EMIT32(wl->pm_userdata);
1136 	PMCLOG_DESPATCH(po);
1137 
1138  error:
1139 	return (error);
1140 }
1141 
1142 /*
1143  * Initialization.
1144  *
1145  * Create a pool of log buffers and initialize mutexes.
1146  */
1147 
1148 void
1149 pmclog_initialize()
1150 {
1151 	int domain;
1152 	struct pmclog_buffer *plb;
1153 
1154 	if (pmclog_buffer_size <= 0 || pmclog_buffer_size > 16*1024) {
1155 		(void) printf("hwpmc: tunable logbuffersize=%d must be "
1156 					  "greater than zero and less than or equal to 16MB.\n",
1157 					  pmclog_buffer_size);
1158 		pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1159 	}
1160 
1161 	if (pmc_nlogbuffers_pcpu <= 0) {
1162 		(void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
1163 					  "than zero.\n", pmc_nlogbuffers_pcpu);
1164 		pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
1165 	}
1166 	if (pmc_nlogbuffers_pcpu*pmclog_buffer_size > 32*1024) {
1167 		(void) printf("hwpmc: memory allocated pcpu must be less than 32MB (is %dK).\n",
1168 					  pmc_nlogbuffers_pcpu*pmclog_buffer_size);
1169 		pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
1170 		pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1171 	}
1172 	for (domain = 0; domain < NDOMAINS; domain++) {
1173 		int ncpus = pmc_dom_hdrs[domain]->pdbh_ncpus;
1174 		int total = ncpus*pmc_nlogbuffers_pcpu;
1175 
1176 		plb = malloc_domain(sizeof(struct pmclog_buffer)*total, M_PMC, domain, M_WAITOK|M_ZERO);
1177 		pmc_dom_hdrs[domain]->pdbh_plbs = plb;
1178 		for (int i = 0; i < total; i++, plb++) {
1179 			void *buf;
1180 
1181 			buf = malloc_domain(1024 * pmclog_buffer_size, M_PMC, domain,
1182 								M_WAITOK|M_ZERO);
1183 			PMCLOG_INIT_BUFFER_DESCRIPTOR(plb, buf, domain);
1184 			pmc_plb_rele_unlocked(plb);
1185 		}
1186 	}
1187 	mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
1188 }
1189 
1190 /*
1191  * Shutdown logging.
1192  *
1193  * Destroy mutexes and release memory back the to free pool.
1194  */
1195 
1196 void
1197 pmclog_shutdown()
1198 {
1199 	struct pmclog_buffer *plb;
1200 	int domain;
1201 
1202 	mtx_destroy(&pmc_kthread_mtx);
1203 
1204 	for (domain = 0; domain < NDOMAINS; domain++) {
1205 		while ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL) {
1206 			TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next);
1207 			free(plb->plb_base, M_PMC);
1208 		}
1209 		free(pmc_dom_hdrs[domain]->pdbh_plbs, M_PMC);
1210 	}
1211 }
1212