xref: /freebsd/sys/dev/hwpmc/hwpmc_logging.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (c) 2005-2007 Joseph Koshy
3  * Copyright (c) 2007 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by A. Joseph Koshy under
7  * sponsorship from the FreeBSD Foundation and Google, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  * Logging code for hwpmc(4)
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/file.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/lock.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/pmc.h>
47 #include <sys/pmclog.h>
48 #include <sys/proc.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 
56 /*
57  * Sysctl tunables
58  */
59 
60 SYSCTL_DECL(_kern_hwpmc);
61 
62 /*
63  * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
64  */
65 
66 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
67 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
68 SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_TUN|CTLFLAG_RD,
69     &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
70 
71 /*
72  * kern.hwpmc.nbuffer -- number of global log buffers
73  */
74 
75 static int pmc_nlogbuffers = PMC_NLOGBUFFERS;
76 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers);
77 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers, CTLFLAG_TUN|CTLFLAG_RD,
78     &pmc_nlogbuffers, 0, "number of global log buffers");
79 
80 /*
81  * Global log buffer list and associated spin lock.
82  */
83 
84 TAILQ_HEAD(, pmclog_buffer) pmc_bufferlist =
85 	TAILQ_HEAD_INITIALIZER(pmc_bufferlist);
86 static struct mtx pmc_bufferlist_mtx;	/* spin lock */
87 static struct mtx pmc_kthread_mtx;	/* sleep lock */
88 
89 #define	PMCLOG_INIT_BUFFER_DESCRIPTOR(D) do {				\
90 		const int __roundup = roundup(sizeof(*D),		\
91 			sizeof(uint32_t));				\
92 		(D)->plb_fence = ((char *) (D)) +			\
93 			 1024*pmclog_buffer_size;			\
94 		(D)->plb_base  = (D)->plb_ptr = ((char *) (D)) +	\
95 			__roundup;					\
96 	} while (0)
97 
98 
99 /*
100  * Log file record constructors.
101  */
102 #define	_PMCLOG_TO_HEADER(T,L)						\
103 	((PMCLOG_HEADER_MAGIC << 24) |					\
104 	 (PMCLOG_TYPE_ ## T << 16)   |					\
105 	 ((L) & 0xFFFF))
106 
107 /* reserve LEN bytes of space and initialize the entry header */
108 #define	_PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do {			\
109 		uint32_t *_le;						\
110 		int _len = roundup((LEN), sizeof(uint32_t));		\
111 		if ((_le = pmclog_reserve((PO), _len)) == NULL) {	\
112 			ACTION;						\
113 		}							\
114 		*_le = _PMCLOG_TO_HEADER(TYPE,_len);			\
115 		_le += 3	/* skip over timestamp */
116 
117 #define	PMCLOG_RESERVE(P,T,L)		_PMCLOG_RESERVE(P,T,L,return)
118 #define	PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L,		\
119 	error=ENOMEM;goto error)
120 
121 #define	PMCLOG_EMIT32(V)	do { *_le++ = (V); } while (0)
122 #define	PMCLOG_EMIT64(V)	do { 					\
123 		*_le++ = (uint32_t) ((V) & 0xFFFFFFFF);			\
124 		*_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF);		\
125 	} while (0)
126 
127 
128 /* Emit a string.  Caution: does NOT update _le, so needs to be last */
129 #define	PMCLOG_EMITSTRING(S,L)	do { bcopy((S), _le, (L)); } while (0)
130 
131 #define	PMCLOG_DESPATCH(PO)						\
132 		pmclog_release((PO));					\
133 	} while (0)
134 
135 
136 /*
137  * Assertions about the log file format.
138  */
139 
140 CTASSERT(sizeof(struct pmclog_callchain) == 6*4 +
141     PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t));
142 CTASSERT(sizeof(struct pmclog_closelog) == 3*4);
143 CTASSERT(sizeof(struct pmclog_dropnotify) == 3*4);
144 CTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX +
145     4*4 + sizeof(uintfptr_t));
146 CTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
147     4*4 + sizeof(uintfptr_t));
148 CTASSERT(sizeof(struct pmclog_map_out) == 4*4 + 2*sizeof(uintfptr_t));
149 CTASSERT(sizeof(struct pmclog_pcsample) == 6*4 + sizeof(uintfptr_t));
150 CTASSERT(sizeof(struct pmclog_pmcallocate) == 6*4);
151 CTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX);
152 CTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4);
153 CTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4);
154 CTASSERT(sizeof(struct pmclog_proccsw) == 5*4 + 8);
155 CTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
156     sizeof(uintfptr_t));
157 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
158     sizeof(uintfptr_t));
159 CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8);
160 CTASSERT(sizeof(struct pmclog_procfork) == 5*4);
161 CTASSERT(sizeof(struct pmclog_sysexit) == 4*4);
162 CTASSERT(sizeof(struct pmclog_userdata) == 4*4);
163 
164 /*
165  * Log buffer structure
166  */
167 
168 struct pmclog_buffer {
169 	TAILQ_ENTRY(pmclog_buffer) plb_next;
170 	char 		*plb_base;
171 	char		*plb_ptr;
172 	char 		*plb_fence;
173 };
174 
175 /*
176  * Prototypes
177  */
178 
179 static int pmclog_get_buffer(struct pmc_owner *po);
180 static void pmclog_loop(void *arg);
181 static void pmclog_release(struct pmc_owner *po);
182 static uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
183 static void pmclog_schedule_io(struct pmc_owner *po);
184 static void pmclog_stop_kthread(struct pmc_owner *po);
185 
186 /*
187  * Helper functions
188  */
189 
190 /*
191  * Get a log buffer
192  */
193 
194 static int
195 pmclog_get_buffer(struct pmc_owner *po)
196 {
197 	struct pmclog_buffer *plb;
198 
199 	mtx_assert(&po->po_mtx, MA_OWNED);
200 
201 	KASSERT(po->po_curbuf == NULL,
202 	    ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
203 
204 	mtx_lock_spin(&pmc_bufferlist_mtx);
205 	if ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL)
206 		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
207 	mtx_unlock_spin(&pmc_bufferlist_mtx);
208 
209 	PMCDBG(LOG,GTB,1, "po=%p plb=%p", po, plb);
210 
211 #ifdef	DEBUG
212 	if (plb)
213 		KASSERT(plb->plb_ptr == plb->plb_base &&
214 		    plb->plb_base < plb->plb_fence,
215 		    ("[pmclog,%d] po=%p buffer invariants: ptr=%p "
216 		    "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
217 		    plb->plb_base, plb->plb_fence));
218 #endif
219 
220 	po->po_curbuf = plb;
221 
222 	/* update stats */
223 	atomic_add_int(&pmc_stats.pm_buffer_requests, 1);
224 	if (plb == NULL)
225 		atomic_add_int(&pmc_stats.pm_buffer_requests_failed, 1);
226 
227 	return (plb ? 0 : ENOMEM);
228 }
229 
230 /*
231  * Log handler loop.
232  *
233  * This function is executed by each pmc owner's helper thread.
234  */
235 
236 static void
237 pmclog_loop(void *arg)
238 {
239 	int error;
240 	struct pmc_owner *po;
241 	struct pmclog_buffer *lb;
242 	struct ucred *ownercred;
243 	struct ucred *mycred;
244 	struct thread *td;
245 	struct uio auio;
246 	struct iovec aiov;
247 	size_t nbytes;
248 
249 	po = (struct pmc_owner *) arg;
250 	td = curthread;
251 	mycred = td->td_ucred;
252 
253 	PROC_LOCK(po->po_owner);
254 	ownercred = crhold(po->po_owner->p_ucred);
255 	PROC_UNLOCK(po->po_owner);
256 
257 	PMCDBG(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
258 	KASSERT(po->po_kthread == curthread->td_proc,
259 	    ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
260 		po, po->po_kthread, curthread->td_proc));
261 
262 	lb = NULL;
263 
264 
265 	/*
266 	 * Loop waiting for I/O requests to be added to the owner
267 	 * struct's queue.  The loop is exited when the log file
268 	 * is deconfigured.
269 	 */
270 
271 	mtx_lock(&pmc_kthread_mtx);
272 
273 	for (;;) {
274 
275 		/* check if we've been asked to exit */
276 		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
277 			break;
278 
279 		if (lb == NULL) { /* look for a fresh buffer to write */
280 			mtx_lock_spin(&po->po_mtx);
281 			if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
282 				mtx_unlock_spin(&po->po_mtx);
283 
284 				/*
285 				 * Wakeup the thread waiting for the
286 				 * PMC_OP_FLUSHLOG request to
287 				 * complete.
288 				 */
289 				if (po->po_flags & PMC_PO_IN_FLUSH) {
290 					po->po_flags &= ~PMC_PO_IN_FLUSH;
291 					wakeup_one(po->po_kthread);
292 				}
293 
294 				(void) msleep(po, &pmc_kthread_mtx, PWAIT,
295 				    "pmcloop", 0);
296 				continue;
297 			}
298 
299 			TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
300 			mtx_unlock_spin(&po->po_mtx);
301 		}
302 
303 		mtx_unlock(&pmc_kthread_mtx);
304 
305 		/* process the request */
306 		PMCDBG(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
307 		    lb->plb_base, lb->plb_ptr);
308 		/* change our thread's credentials before issuing the I/O */
309 
310 		aiov.iov_base = lb->plb_base;
311 		aiov.iov_len  = nbytes = lb->plb_ptr - lb->plb_base;
312 
313 		auio.uio_iov    = &aiov;
314 		auio.uio_iovcnt = 1;
315 		auio.uio_offset = -1;
316 		auio.uio_resid  = nbytes;
317 		auio.uio_rw     = UIO_WRITE;
318 		auio.uio_segflg = UIO_SYSSPACE;
319 		auio.uio_td     = td;
320 
321 		/* switch thread credentials -- see kern_ktrace.c */
322 		td->td_ucred = ownercred;
323 		error = fo_write(po->po_file, &auio, ownercred, 0, td);
324 		td->td_ucred = mycred;
325 
326 		mtx_lock(&pmc_kthread_mtx);
327 
328 		if (error) {
329 			/* XXX some errors are recoverable */
330 			/* XXX also check for SIGPIPE if a socket */
331 
332 			/* send a SIGIO to the owner and exit */
333 			PROC_LOCK(po->po_owner);
334 			psignal(po->po_owner, SIGIO);
335 			PROC_UNLOCK(po->po_owner);
336 
337 			po->po_error = error; /* save for flush log */
338 
339 			PMCDBG(LOG,WRI,2, "po=%p error=%d", po, error);
340 
341 			break;
342 		}
343 
344 		/* put the used buffer back into the global pool */
345 		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
346 
347 		mtx_lock_spin(&pmc_bufferlist_mtx);
348 		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
349 		mtx_unlock_spin(&pmc_bufferlist_mtx);
350 
351 		lb = NULL;
352 	}
353 
354 	po->po_kthread = NULL;
355 
356 	mtx_unlock(&pmc_kthread_mtx);
357 
358 	/* return the current I/O buffer to the global pool */
359 	if (lb) {
360 		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
361 
362 		mtx_lock_spin(&pmc_bufferlist_mtx);
363 		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
364 		mtx_unlock_spin(&pmc_bufferlist_mtx);
365 	}
366 
367 	/*
368 	 * Exit this thread, signalling the waiter
369 	 */
370 
371 	crfree(ownercred);
372 
373 	kproc_exit(0);
374 }
375 
376 /*
377  * Release and log entry and schedule an I/O if needed.
378  */
379 
380 static void
381 pmclog_release(struct pmc_owner *po)
382 {
383 	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
384 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
385 		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
386 	KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
387 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
388 		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
389 
390 	/* schedule an I/O if we've filled a buffer */
391 	if (po->po_curbuf->plb_ptr >= po->po_curbuf->plb_fence)
392 		pmclog_schedule_io(po);
393 
394 	mtx_unlock_spin(&po->po_mtx);
395 
396 	PMCDBG(LOG,REL,1, "po=%p", po);
397 }
398 
399 
400 /*
401  * Attempt to reserve 'length' bytes of space in an owner's log
402  * buffer.  The function returns a pointer to 'length' bytes of space
403  * if there was enough space or returns NULL if no space was
404  * available.  Non-null returns do so with the po mutex locked.  The
405  * caller must invoke pmclog_release() on the pmc owner structure
406  * when done.
407  */
408 
409 static uint32_t *
410 pmclog_reserve(struct pmc_owner *po, int length)
411 {
412 	uintptr_t newptr, oldptr;
413 	uint32_t *lh;
414 	struct timespec ts;
415 
416 	PMCDBG(LOG,ALL,1, "po=%p len=%d", po, length);
417 
418 	KASSERT(length % sizeof(uint32_t) == 0,
419 	    ("[pmclog,%d] length not a multiple of word size", __LINE__));
420 
421 	mtx_lock_spin(&po->po_mtx);
422 
423 	if (po->po_curbuf == NULL)
424 		if (pmclog_get_buffer(po) != 0) {
425 			mtx_unlock_spin(&po->po_mtx);
426 			return (NULL);
427 		}
428 
429 	KASSERT(po->po_curbuf != NULL,
430 	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
431 
432 	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base &&
433 	    po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
434 	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
435 		__LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
436 		po->po_curbuf->plb_fence));
437 
438 	oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
439 	newptr = oldptr + length;
440 
441 	KASSERT(oldptr != (uintptr_t) NULL,
442 	    ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
443 
444 	/*
445 	 * If we have space in the current buffer, return a pointer to
446 	 * available space with the PO structure locked.
447 	 */
448 	if (newptr <= (uintptr_t) po->po_curbuf->plb_fence) {
449 		po->po_curbuf->plb_ptr = (char *) newptr;
450 		goto done;
451 	}
452 
453 	/*
454 	 * Otherwise, schedule the current buffer for output and get a
455 	 * fresh buffer.
456 	 */
457 	pmclog_schedule_io(po);
458 
459 	if (pmclog_get_buffer(po) != 0) {
460 		mtx_unlock_spin(&po->po_mtx);
461 		return (NULL);
462 	}
463 
464 	KASSERT(po->po_curbuf != NULL,
465 	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
466 
467 	KASSERT(po->po_curbuf->plb_ptr != NULL,
468 	    ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
469 
470 	KASSERT(po->po_curbuf->plb_ptr == po->po_curbuf->plb_base &&
471 	    po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
472 	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
473 		__LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
474 		po->po_curbuf->plb_fence));
475 
476 	oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
477 
478  done:
479 	lh = (uint32_t *) oldptr;
480 	lh++;				/* skip header */
481 	getnanotime(&ts);		/* fill in the timestamp */
482 	*lh++ = ts.tv_sec & 0xFFFFFFFF;
483 	*lh++ = ts.tv_nsec & 0xFFFFFFF;
484 	return ((uint32_t *) oldptr);
485 }
486 
487 /*
488  * Schedule an I/O.
489  *
490  * Transfer the current buffer to the helper kthread.
491  */
492 
493 static void
494 pmclog_schedule_io(struct pmc_owner *po)
495 {
496 	KASSERT(po->po_curbuf != NULL,
497 	    ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
498 
499 	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
500 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
501 		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
502 	KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
503 	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
504 		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
505 
506 	PMCDBG(LOG,SIO, 1, "po=%p", po);
507 
508 	mtx_assert(&po->po_mtx, MA_OWNED);
509 
510 	/*
511 	 * Add the current buffer to the tail of the buffer list and
512 	 * wakeup the helper.
513 	 */
514 	TAILQ_INSERT_TAIL(&po->po_logbuffers, po->po_curbuf, plb_next);
515 	po->po_curbuf = NULL;
516 	wakeup_one(po);
517 }
518 
519 /*
520  * Stop the helper kthread.
521  */
522 
523 static void
524 pmclog_stop_kthread(struct pmc_owner *po)
525 {
526 	/*
527 	 * Unset flag, wakeup the helper thread,
528 	 * wait for it to exit
529 	 */
530 
531 	mtx_assert(&pmc_kthread_mtx, MA_OWNED);
532 	po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
533 	wakeup_one(po);
534 	if (po->po_kthread)
535 		msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
536 }
537 
538 /*
539  * Public functions
540  */
541 
542 /*
543  * Configure a log file for pmc owner 'po'.
544  *
545  * Parameter 'logfd' is a file handle referencing an open file in the
546  * owner process.  This file needs to have been opened for writing.
547  */
548 
549 int
550 pmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
551 {
552 	int error;
553 	struct proc *p;
554 
555 	PMCDBG(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
556 
557 	p = po->po_owner;
558 
559 	/* return EBUSY if a log file was already present */
560 	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
561 		return (EBUSY);
562 
563 	KASSERT(po->po_kthread == NULL,
564 	    ("[pmclog,%d] po=%p kthread (%p) already present", __LINE__, po,
565 		po->po_kthread));
566 	KASSERT(po->po_file == NULL,
567 	    ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
568 		po->po_file));
569 
570 	/* get a reference to the file state */
571 	error = fget_write(curthread, logfd, &po->po_file);
572 	if (error)
573 		goto error;
574 
575 	/* mark process as owning a log file */
576 	po->po_flags |= PMC_PO_OWNS_LOGFILE;
577 	error = kproc_create(pmclog_loop, po, &po->po_kthread,
578 	    RFHIGHPID, 0, "hwpmc: proc(%d)", p->p_pid);
579 	if (error)
580 		goto error;
581 
582 	/* mark process as using HWPMCs */
583 	PROC_LOCK(p);
584 	p->p_flag |= P_HWPMC;
585 	PROC_UNLOCK(p);
586 
587 	/* create a log initialization entry */
588 	PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
589 	    sizeof(struct pmclog_initialize));
590 	PMCLOG_EMIT32(PMC_VERSION);
591 	PMCLOG_EMIT32(md->pmd_cputype);
592 	PMCLOG_DESPATCH(po);
593 
594 	return (0);
595 
596  error:
597 	/* shutdown the thread */
598 	mtx_lock(&pmc_kthread_mtx);
599 	if (po->po_kthread)
600 		pmclog_stop_kthread(po);
601 	mtx_unlock(&pmc_kthread_mtx);
602 
603 	KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
604 	    "stopped", __LINE__, po));
605 
606 	if (po->po_file)
607 		(void) fdrop(po->po_file, curthread);
608 	po->po_file  = NULL;	/* clear file and error state */
609 	po->po_error = 0;
610 
611 	return (error);
612 }
613 
614 
615 /*
616  * De-configure a log file.  This will throw away any buffers queued
617  * for this owner process.
618  */
619 
620 int
621 pmclog_deconfigure_log(struct pmc_owner *po)
622 {
623 	int error;
624 	struct pmclog_buffer *lb;
625 
626 	PMCDBG(LOG,CFG,1, "de-config po=%p", po);
627 
628 	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
629 		return (EINVAL);
630 
631 	KASSERT(po->po_sscount == 0,
632 	    ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
633 	KASSERT(po->po_file != NULL,
634 	    ("[pmclog,%d] po=%p no log file", __LINE__, po));
635 
636 	/* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
637 	mtx_lock(&pmc_kthread_mtx);
638 	if (po->po_kthread)
639 		pmclog_stop_kthread(po);
640 	mtx_unlock(&pmc_kthread_mtx);
641 
642 	KASSERT(po->po_kthread == NULL,
643 	    ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
644 
645 	/* return all queued log buffers to the global pool */
646 	while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
647 		TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
648 		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
649 		mtx_lock_spin(&pmc_bufferlist_mtx);
650 		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
651 		mtx_unlock_spin(&pmc_bufferlist_mtx);
652 	}
653 
654 	/* return the 'current' buffer to the global pool */
655 	if ((lb = po->po_curbuf) != NULL) {
656 		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
657 		mtx_lock_spin(&pmc_bufferlist_mtx);
658 		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
659 		mtx_unlock_spin(&pmc_bufferlist_mtx);
660 	}
661 
662 	/* drop a reference to the fd */
663 	error = fdrop(po->po_file, curthread);
664 	po->po_file  = NULL;
665 	po->po_error = 0;
666 
667 	return (error);
668 }
669 
670 /*
671  * Flush a process' log buffer.
672  */
673 
674 int
675 pmclog_flush(struct pmc_owner *po)
676 {
677 	int error, has_pending_buffers;
678 
679 	PMCDBG(LOG,FLS,1, "po=%p", po);
680 
681 	/*
682 	 * If there is a pending error recorded by the logger thread,
683 	 * return that.
684 	 */
685 	if (po->po_error)
686 		return (po->po_error);
687 
688 	error = 0;
689 
690 	/*
691 	 * Check that we do have an active log file.
692 	 */
693 	mtx_lock(&pmc_kthread_mtx);
694 	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
695 		error = EINVAL;
696 		goto error;
697 	}
698 
699 	/*
700 	 * Schedule the current buffer if any.
701 	 */
702 	mtx_lock_spin(&po->po_mtx);
703 	if (po->po_curbuf)
704 		pmclog_schedule_io(po);
705 	has_pending_buffers = !TAILQ_EMPTY(&po->po_logbuffers);
706 	mtx_unlock_spin(&po->po_mtx);
707 
708 	if (has_pending_buffers) {
709 		po->po_flags |= PMC_PO_IN_FLUSH; /* ask for a wakeup */
710 		error = msleep(po->po_kthread, &pmc_kthread_mtx, PWAIT,
711 		    "pmcflush", 0);
712 		if (error == 0)
713 			error = po->po_error;
714 	}
715 
716  error:
717 	mtx_unlock(&pmc_kthread_mtx);
718 
719 	return (error);
720 }
721 
722 
723 void
724 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
725 {
726 	int n, recordlen;
727 	uint32_t flags;
728 	struct pmc_owner *po;
729 
730 	PMCDBG(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
731 	    ps->ps_nsamples);
732 
733 	recordlen = offsetof(struct pmclog_callchain, pl_pc) +
734 	    ps->ps_nsamples * sizeof(uintfptr_t);
735 	po = pm->pm_owner;
736 	flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
737 	PMCLOG_RESERVE(po, CALLCHAIN, recordlen);
738 	PMCLOG_EMIT32(ps->ps_pid);
739 	PMCLOG_EMIT32(pm->pm_id);
740 	PMCLOG_EMIT32(flags);
741 	for (n = 0; n < ps->ps_nsamples; n++)
742 		PMCLOG_EMITADDR(ps->ps_pc[n]);
743 	PMCLOG_DESPATCH(po);
744 }
745 
746 void
747 pmclog_process_closelog(struct pmc_owner *po)
748 {
749 	PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
750 	PMCLOG_DESPATCH(po);
751 }
752 
753 void
754 pmclog_process_dropnotify(struct pmc_owner *po)
755 {
756 	PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
757 	PMCLOG_DESPATCH(po);
758 }
759 
760 void
761 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
762     const char *path)
763 {
764 	int pathlen, recordlen;
765 
766 	KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
767 
768 	pathlen = strlen(path) + 1;	/* #bytes for path name */
769 	recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
770 	    pathlen;
771 
772 	PMCLOG_RESERVE(po, MAP_IN, recordlen);
773 	PMCLOG_EMIT32(pid);
774 	PMCLOG_EMITADDR(start);
775 	PMCLOG_EMITSTRING(path,pathlen);
776 	PMCLOG_DESPATCH(po);
777 }
778 
779 void
780 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
781     uintfptr_t end)
782 {
783 	KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
784 
785 	PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
786 	PMCLOG_EMIT32(pid);
787 	PMCLOG_EMITADDR(start);
788 	PMCLOG_EMITADDR(end);
789 	PMCLOG_DESPATCH(po);
790 }
791 
792 void
793 pmclog_process_pmcallocate(struct pmc *pm)
794 {
795 	struct pmc_owner *po;
796 
797 	po = pm->pm_owner;
798 
799 	PMCDBG(LOG,ALL,1, "pm=%p", pm);
800 
801 	PMCLOG_RESERVE(po, PMCALLOCATE, sizeof(struct pmclog_pmcallocate));
802 	PMCLOG_EMIT32(pm->pm_id);
803 	PMCLOG_EMIT32(pm->pm_event);
804 	PMCLOG_EMIT32(pm->pm_flags);
805 	PMCLOG_DESPATCH(po);
806 }
807 
808 void
809 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
810 {
811 	int pathlen, recordlen;
812 	struct pmc_owner *po;
813 
814 	PMCDBG(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
815 
816 	po = pm->pm_owner;
817 
818 	pathlen = strlen(path) + 1;	/* #bytes for the string */
819 	recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
820 
821 	PMCLOG_RESERVE(po, PMCATTACH, recordlen);
822 	PMCLOG_EMIT32(pm->pm_id);
823 	PMCLOG_EMIT32(pid);
824 	PMCLOG_EMITSTRING(path, pathlen);
825 	PMCLOG_DESPATCH(po);
826 }
827 
828 void
829 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
830 {
831 	struct pmc_owner *po;
832 
833 	PMCDBG(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
834 
835 	po = pm->pm_owner;
836 
837 	PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
838 	PMCLOG_EMIT32(pm->pm_id);
839 	PMCLOG_EMIT32(pid);
840 	PMCLOG_DESPATCH(po);
841 }
842 
843 /*
844  * Log a context switch event to the log file.
845  */
846 
847 void
848 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
849 {
850 	struct pmc_owner *po;
851 
852 	KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
853 	    ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
854 
855 	PMCDBG(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
856 	    v);
857 
858 	po = pm->pm_owner;
859 
860 	PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
861 	PMCLOG_EMIT32(pm->pm_id);
862 	PMCLOG_EMIT64(v);
863 	PMCLOG_EMIT32(pp->pp_proc->p_pid);
864 	PMCLOG_DESPATCH(po);
865 }
866 
867 void
868 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
869     uintfptr_t startaddr, char *path)
870 {
871 	int pathlen, recordlen;
872 
873 	PMCDBG(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
874 
875 	pathlen   = strlen(path) + 1;	/* #bytes for the path */
876 	recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
877 
878 	PMCLOG_RESERVE(po, PROCEXEC, recordlen);
879 	PMCLOG_EMIT32(pid);
880 	PMCLOG_EMITADDR(startaddr);
881 	PMCLOG_EMIT32(pmid);
882 	PMCLOG_EMITSTRING(path,pathlen);
883 	PMCLOG_DESPATCH(po);
884 }
885 
886 /*
887  * Log a process exit event (and accumulated pmc value) to the log file.
888  */
889 
890 void
891 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
892 {
893 	int ri;
894 	struct pmc_owner *po;
895 
896 	ri = PMC_TO_ROWINDEX(pm);
897 	PMCDBG(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
898 	    pp->pp_pmcs[ri].pp_pmcval);
899 
900 	po = pm->pm_owner;
901 
902 	PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
903 	PMCLOG_EMIT32(pm->pm_id);
904 	PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
905 	PMCLOG_EMIT32(pp->pp_proc->p_pid);
906 	PMCLOG_DESPATCH(po);
907 }
908 
909 /*
910  * Log a fork event.
911  */
912 
913 void
914 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
915 {
916 	PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
917 	PMCLOG_EMIT32(oldpid);
918 	PMCLOG_EMIT32(newpid);
919 	PMCLOG_DESPATCH(po);
920 }
921 
922 /*
923  * Log a process exit event of the form suitable for system-wide PMCs.
924  */
925 
926 void
927 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
928 {
929 	PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
930 	PMCLOG_EMIT32(pid);
931 	PMCLOG_DESPATCH(po);
932 }
933 
934 /*
935  * Write a user log entry.
936  */
937 
938 int
939 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
940 {
941 	int error;
942 
943 	PMCDBG(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
944 
945 	error = 0;
946 
947 	PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
948 	    sizeof(struct pmclog_userdata));
949 	PMCLOG_EMIT32(wl->pm_userdata);
950 	PMCLOG_DESPATCH(po);
951 
952  error:
953 	return (error);
954 }
955 
956 /*
957  * Initialization.
958  *
959  * Create a pool of log buffers and initialize mutexes.
960  */
961 
962 void
963 pmclog_initialize()
964 {
965 	int n;
966 	struct pmclog_buffer *plb;
967 
968 	if (pmclog_buffer_size <= 0) {
969 		(void) printf("hwpmc: tunable logbuffersize=%d must be "
970 		    "greater than zero.\n", pmclog_buffer_size);
971 		pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
972 	}
973 
974 	if (pmc_nlogbuffers <= 0) {
975 		(void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
976 		    "than zero.\n", pmc_nlogbuffers);
977 		pmc_nlogbuffers = PMC_NLOGBUFFERS;
978 	}
979 
980 	/* create global pool of log buffers */
981 	for (n = 0; n < pmc_nlogbuffers; n++) {
982 		plb = malloc(1024 * pmclog_buffer_size, M_PMC,
983 		    M_WAITOK|M_ZERO);
984 		PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
985 		TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
986 	}
987 	mtx_init(&pmc_bufferlist_mtx, "pmc-buffer-list", "pmc-leaf",
988 	    MTX_SPIN);
989 	mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
990 }
991 
992 /*
993  * Shutdown logging.
994  *
995  * Destroy mutexes and release memory back the to free pool.
996  */
997 
998 void
999 pmclog_shutdown()
1000 {
1001 	struct pmclog_buffer *plb;
1002 
1003 	mtx_destroy(&pmc_kthread_mtx);
1004 	mtx_destroy(&pmc_bufferlist_mtx);
1005 
1006 	while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
1007 		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
1008 		free(plb, M_PMC);
1009 	}
1010 }
1011