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