2 * Copyright (c) 2005-2007 Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Logging code for hwpmc(4)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/capability.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
48 #include <sys/pmckern.h>
49 #include <sys/pmclog.h>
51 #include <sys/signalvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
62 SYSCTL_DECL(_kern_hwpmc);
65 * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
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");
74 * kern.hwpmc.nbuffer -- number of global log buffers
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");
83 * Global log buffer list and associated spin lock.
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 */
91 #define PMCLOG_INIT_BUFFER_DESCRIPTOR(D) do { \
92 const int __roundup = roundup(sizeof(*D), \
94 (D)->plb_fence = ((char *) (D)) + \
95 1024*pmclog_buffer_size; \
96 (D)->plb_base = (D)->plb_ptr = ((char *) (D)) + \
102 * Log file record constructors.
104 #define _PMCLOG_TO_HEADER(T,L) \
105 ((PMCLOG_HEADER_MAGIC << 24) | \
106 (PMCLOG_TYPE_ ## T << 16) | \
109 /* reserve LEN bytes of space and initialize the entry header */
110 #define _PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do { \
112 int _len = roundup((LEN), sizeof(uint32_t)); \
113 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
116 *_le = _PMCLOG_TO_HEADER(TYPE,_len); \
117 _le += 3 /* skip over timestamp */
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)
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); \
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)
133 #define PMCLOG_DESPATCH(PO) \
134 pmclog_release((PO)); \
139 * Assertions about the log file format.
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 +
159 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
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);
167 * Log buffer structure
170 struct pmclog_buffer {
171 TAILQ_ENTRY(pmclog_buffer) plb_next;
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);
197 pmclog_get_buffer(struct pmc_owner *po)
199 struct pmclog_buffer *plb;
201 mtx_assert(&po->po_mtx, MA_OWNED);
203 KASSERT(po->po_curbuf == NULL,
204 ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
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);
211 PMCDBG(LOG,GTB,1, "po=%p plb=%p", po, 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));
225 atomic_add_int(&pmc_stats.pm_buffer_requests, 1);
227 atomic_add_int(&pmc_stats.pm_buffer_requests_failed, 1);
229 return (plb ? 0 : ENOMEM);
235 * This function is executed by each pmc owner's helper thread.
239 pmclog_loop(void *arg)
242 struct pmc_owner *po;
243 struct pmclog_buffer *lb;
245 struct ucred *ownercred;
246 struct ucred *mycred;
252 po = (struct pmc_owner *) arg;
255 mycred = td->td_ucred;
258 ownercred = crhold(p->p_ucred);
261 PMCDBG(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
262 KASSERT(po->po_kthread == curthread->td_proc,
263 ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
264 po, po->po_kthread, curthread->td_proc));
270 * Loop waiting for I/O requests to be added to the owner
271 * struct's queue. The loop is exited when the log file
275 mtx_lock(&pmc_kthread_mtx);
279 /* check if we've been asked to exit */
280 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
283 if (lb == NULL) { /* look for a fresh buffer to write */
284 mtx_lock_spin(&po->po_mtx);
285 if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
286 mtx_unlock_spin(&po->po_mtx);
288 if (po->po_flags & PMC_PO_SHUTDOWN) {
289 mtx_unlock(&pmc_kthread_mtx);
291 * Close the file to get PMCLOG_EOF
292 * error in pmclog(3).
294 fo_close(po->po_file, curthread);
295 mtx_lock(&pmc_kthread_mtx);
298 (void) msleep(po, &pmc_kthread_mtx, PWAIT,
303 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
304 mtx_unlock_spin(&po->po_mtx);
307 mtx_unlock(&pmc_kthread_mtx);
309 /* process the request */
310 PMCDBG(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
311 lb->plb_base, lb->plb_ptr);
312 /* change our thread's credentials before issuing the I/O */
314 aiov.iov_base = lb->plb_base;
315 aiov.iov_len = nbytes = lb->plb_ptr - lb->plb_base;
317 auio.uio_iov = &aiov;
319 auio.uio_offset = -1;
320 auio.uio_resid = nbytes;
321 auio.uio_rw = UIO_WRITE;
322 auio.uio_segflg = UIO_SYSSPACE;
325 /* switch thread credentials -- see kern_ktrace.c */
326 td->td_ucred = ownercred;
327 error = fo_write(po->po_file, &auio, ownercred, 0, td);
328 td->td_ucred = mycred;
331 /* XXX some errors are recoverable */
332 /* send a SIGIO to the owner and exit */
334 kern_psignal(p, SIGIO);
337 mtx_lock(&pmc_kthread_mtx);
339 po->po_error = error; /* save for flush log */
341 PMCDBG(LOG,WRI,2, "po=%p error=%d", po, error);
346 mtx_lock(&pmc_kthread_mtx);
348 /* put the used buffer back into the global pool */
349 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
351 mtx_lock_spin(&pmc_bufferlist_mtx);
352 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
353 mtx_unlock_spin(&pmc_bufferlist_mtx);
358 po->po_kthread = NULL;
360 mtx_unlock(&pmc_kthread_mtx);
362 /* return the current I/O buffer to the global pool */
364 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
366 mtx_lock_spin(&pmc_bufferlist_mtx);
367 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
368 mtx_unlock_spin(&pmc_bufferlist_mtx);
372 * Exit this thread, signalling the waiter
381 * Release and log entry and schedule an I/O if needed.
385 pmclog_release(struct pmc_owner *po)
387 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
388 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
389 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
390 KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
391 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
392 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
394 /* schedule an I/O if we've filled a buffer */
395 if (po->po_curbuf->plb_ptr >= po->po_curbuf->plb_fence)
396 pmclog_schedule_io(po);
398 mtx_unlock_spin(&po->po_mtx);
400 PMCDBG(LOG,REL,1, "po=%p", po);
405 * Attempt to reserve 'length' bytes of space in an owner's log
406 * buffer. The function returns a pointer to 'length' bytes of space
407 * if there was enough space or returns NULL if no space was
408 * available. Non-null returns do so with the po mutex locked. The
409 * caller must invoke pmclog_release() on the pmc owner structure
414 pmclog_reserve(struct pmc_owner *po, int length)
416 uintptr_t newptr, oldptr;
420 PMCDBG(LOG,ALL,1, "po=%p len=%d", po, length);
422 KASSERT(length % sizeof(uint32_t) == 0,
423 ("[pmclog,%d] length not a multiple of word size", __LINE__));
425 mtx_lock_spin(&po->po_mtx);
427 /* No more data when shutdown in progress. */
428 if (po->po_flags & PMC_PO_SHUTDOWN) {
429 mtx_unlock_spin(&po->po_mtx);
433 if (po->po_curbuf == NULL)
434 if (pmclog_get_buffer(po) != 0) {
435 mtx_unlock_spin(&po->po_mtx);
439 KASSERT(po->po_curbuf != NULL,
440 ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
442 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base &&
443 po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
444 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
445 __LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
446 po->po_curbuf->plb_fence));
448 oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
449 newptr = oldptr + length;
451 KASSERT(oldptr != (uintptr_t) NULL,
452 ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
455 * If we have space in the current buffer, return a pointer to
456 * available space with the PO structure locked.
458 if (newptr <= (uintptr_t) po->po_curbuf->plb_fence) {
459 po->po_curbuf->plb_ptr = (char *) newptr;
464 * Otherwise, schedule the current buffer for output and get a
467 pmclog_schedule_io(po);
469 if (pmclog_get_buffer(po) != 0) {
470 mtx_unlock_spin(&po->po_mtx);
474 KASSERT(po->po_curbuf != NULL,
475 ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
477 KASSERT(po->po_curbuf->plb_ptr != NULL,
478 ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
480 KASSERT(po->po_curbuf->plb_ptr == po->po_curbuf->plb_base &&
481 po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
482 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
483 __LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
484 po->po_curbuf->plb_fence));
486 oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
489 lh = (uint32_t *) oldptr;
490 lh++; /* skip header */
491 getnanotime(&ts); /* fill in the timestamp */
492 *lh++ = ts.tv_sec & 0xFFFFFFFF;
493 *lh++ = ts.tv_nsec & 0xFFFFFFF;
494 return ((uint32_t *) oldptr);
500 * Transfer the current buffer to the helper kthread.
504 pmclog_schedule_io(struct pmc_owner *po)
506 KASSERT(po->po_curbuf != NULL,
507 ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
509 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
510 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
511 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
512 KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
513 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
514 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
516 PMCDBG(LOG,SIO, 1, "po=%p", po);
518 mtx_assert(&po->po_mtx, MA_OWNED);
521 * Add the current buffer to the tail of the buffer list and
524 TAILQ_INSERT_TAIL(&po->po_logbuffers, po->po_curbuf, plb_next);
525 po->po_curbuf = NULL;
530 * Stop the helper kthread.
534 pmclog_stop_kthread(struct pmc_owner *po)
537 * Close the file to force the thread out of fo_write,
538 * unset flag, wakeup the helper thread,
539 * wait for it to exit
542 if (po->po_file != NULL)
543 fo_close(po->po_file, curthread);
545 mtx_lock(&pmc_kthread_mtx);
546 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
549 msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
550 mtx_unlock(&pmc_kthread_mtx);
558 * Configure a log file for pmc owner 'po'.
560 * Parameter 'logfd' is a file handle referencing an open file in the
561 * owner process. This file needs to have been opened for writing.
565 pmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
571 * As long as it is possible to get a LOR between pmc_sx lock and
572 * proctree/allproc sx locks used for adding a new process, assure
573 * the former is not held here.
575 sx_assert(&pmc_sx, SA_UNLOCKED);
576 PMCDBG(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
580 /* return EBUSY if a log file was already present */
581 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
584 KASSERT(po->po_kthread == NULL,
585 ("[pmclog,%d] po=%p kthread (%p) already present", __LINE__, po,
587 KASSERT(po->po_file == NULL,
588 ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
591 /* get a reference to the file state */
592 error = fget_write(curthread, logfd, CAP_WRITE, &po->po_file);
596 /* mark process as owning a log file */
597 po->po_flags |= PMC_PO_OWNS_LOGFILE;
598 error = kproc_create(pmclog_loop, po, &po->po_kthread,
599 RFHIGHPID, 0, "hwpmc: proc(%d)", p->p_pid);
603 /* mark process as using HWPMCs */
605 p->p_flag |= P_HWPMC;
608 /* create a log initialization entry */
609 PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
610 sizeof(struct pmclog_initialize));
611 PMCLOG_EMIT32(PMC_VERSION);
612 PMCLOG_EMIT32(md->pmd_cputype);
618 /* shutdown the thread */
620 pmclog_stop_kthread(po);
622 KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
623 "stopped", __LINE__, po));
626 (void) fdrop(po->po_file, curthread);
627 po->po_file = NULL; /* clear file and error state */
635 * De-configure a log file. This will throw away any buffers queued
636 * for this owner process.
640 pmclog_deconfigure_log(struct pmc_owner *po)
643 struct pmclog_buffer *lb;
645 PMCDBG(LOG,CFG,1, "de-config po=%p", po);
647 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
650 KASSERT(po->po_sscount == 0,
651 ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
652 KASSERT(po->po_file != NULL,
653 ("[pmclog,%d] po=%p no log file", __LINE__, po));
655 /* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
657 pmclog_stop_kthread(po);
659 KASSERT(po->po_kthread == NULL,
660 ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
662 /* return all queued log buffers to the global pool */
663 while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
664 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
665 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
666 mtx_lock_spin(&pmc_bufferlist_mtx);
667 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
668 mtx_unlock_spin(&pmc_bufferlist_mtx);
671 /* return the 'current' buffer to the global pool */
672 if ((lb = po->po_curbuf) != NULL) {
673 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
674 mtx_lock_spin(&pmc_bufferlist_mtx);
675 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
676 mtx_unlock_spin(&pmc_bufferlist_mtx);
679 /* drop a reference to the fd */
680 error = fdrop(po->po_file, curthread);
688 * Flush a process' log buffer.
692 pmclog_flush(struct pmc_owner *po)
695 struct pmclog_buffer *lb;
697 PMCDBG(LOG,FLS,1, "po=%p", po);
700 * If there is a pending error recorded by the logger thread,
704 return (po->po_error);
709 * Check that we do have an active log file.
711 mtx_lock(&pmc_kthread_mtx);
712 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
718 * Schedule the current buffer if any and not empty.
720 mtx_lock_spin(&po->po_mtx);
722 if (lb && lb->plb_ptr != lb->plb_base) {
723 pmclog_schedule_io(po);
726 mtx_unlock_spin(&po->po_mtx);
729 mtx_unlock(&pmc_kthread_mtx);
735 pmclog_close(struct pmc_owner *po)
738 PMCDBG(LOG,CLO,1, "po=%p", po);
740 mtx_lock(&pmc_kthread_mtx);
743 * Schedule the current buffer.
745 mtx_lock_spin(&po->po_mtx);
747 pmclog_schedule_io(po);
750 mtx_unlock_spin(&po->po_mtx);
753 * Initiate shutdown: no new data queued,
754 * thread will close file on last block.
756 po->po_flags |= PMC_PO_SHUTDOWN;
758 mtx_unlock(&pmc_kthread_mtx);
764 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
768 struct pmc_owner *po;
770 PMCDBG(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
773 recordlen = offsetof(struct pmclog_callchain, pl_pc) +
774 ps->ps_nsamples * sizeof(uintfptr_t);
776 flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
777 PMCLOG_RESERVE(po, CALLCHAIN, recordlen);
778 PMCLOG_EMIT32(ps->ps_pid);
779 PMCLOG_EMIT32(pm->pm_id);
780 PMCLOG_EMIT32(flags);
781 for (n = 0; n < ps->ps_nsamples; n++)
782 PMCLOG_EMITADDR(ps->ps_pc[n]);
787 pmclog_process_closelog(struct pmc_owner *po)
789 PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
794 pmclog_process_dropnotify(struct pmc_owner *po)
796 PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
801 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
804 int pathlen, recordlen;
806 KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
808 pathlen = strlen(path) + 1; /* #bytes for path name */
809 recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
812 PMCLOG_RESERVE(po, MAP_IN, recordlen);
814 PMCLOG_EMITADDR(start);
815 PMCLOG_EMITSTRING(path,pathlen);
820 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
823 KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
825 PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
827 PMCLOG_EMITADDR(start);
828 PMCLOG_EMITADDR(end);
833 pmclog_process_pmcallocate(struct pmc *pm)
835 struct pmc_owner *po;
839 PMCDBG(LOG,ALL,1, "pm=%p", pm);
841 PMCLOG_RESERVE(po, PMCALLOCATE, sizeof(struct pmclog_pmcallocate));
842 PMCLOG_EMIT32(pm->pm_id);
843 PMCLOG_EMIT32(pm->pm_event);
844 PMCLOG_EMIT32(pm->pm_flags);
849 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
851 int pathlen, recordlen;
852 struct pmc_owner *po;
854 PMCDBG(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
858 pathlen = strlen(path) + 1; /* #bytes for the string */
859 recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
861 PMCLOG_RESERVE(po, PMCATTACH, recordlen);
862 PMCLOG_EMIT32(pm->pm_id);
864 PMCLOG_EMITSTRING(path, pathlen);
869 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
871 struct pmc_owner *po;
873 PMCDBG(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
877 PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
878 PMCLOG_EMIT32(pm->pm_id);
884 * Log a context switch event to the log file.
888 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
890 struct pmc_owner *po;
892 KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
893 ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
895 PMCDBG(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
900 PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
901 PMCLOG_EMIT32(pm->pm_id);
903 PMCLOG_EMIT32(pp->pp_proc->p_pid);
908 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
909 uintfptr_t startaddr, char *path)
911 int pathlen, recordlen;
913 PMCDBG(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
915 pathlen = strlen(path) + 1; /* #bytes for the path */
916 recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
918 PMCLOG_RESERVE(po, PROCEXEC, recordlen);
920 PMCLOG_EMITADDR(startaddr);
922 PMCLOG_EMITSTRING(path,pathlen);
927 * Log a process exit event (and accumulated pmc value) to the log file.
931 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
934 struct pmc_owner *po;
936 ri = PMC_TO_ROWINDEX(pm);
937 PMCDBG(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
938 pp->pp_pmcs[ri].pp_pmcval);
942 PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
943 PMCLOG_EMIT32(pm->pm_id);
944 PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
945 PMCLOG_EMIT32(pp->pp_proc->p_pid);
954 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
956 PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
957 PMCLOG_EMIT32(oldpid);
958 PMCLOG_EMIT32(newpid);
963 * Log a process exit event of the form suitable for system-wide PMCs.
967 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
969 PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
975 * Write a user log entry.
979 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
983 PMCDBG(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
987 PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
988 sizeof(struct pmclog_userdata));
989 PMCLOG_EMIT32(wl->pm_userdata);
999 * Create a pool of log buffers and initialize mutexes.
1006 struct pmclog_buffer *plb;
1008 if (pmclog_buffer_size <= 0) {
1009 (void) printf("hwpmc: tunable logbuffersize=%d must be "
1010 "greater than zero.\n", pmclog_buffer_size);
1011 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1014 if (pmc_nlogbuffers <= 0) {
1015 (void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
1016 "than zero.\n", pmc_nlogbuffers);
1017 pmc_nlogbuffers = PMC_NLOGBUFFERS;
1020 /* create global pool of log buffers */
1021 for (n = 0; n < pmc_nlogbuffers; n++) {
1022 plb = malloc(1024 * pmclog_buffer_size, M_PMC,
1024 PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
1025 TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
1027 mtx_init(&pmc_bufferlist_mtx, "pmc-buffer-list", "pmc-leaf",
1029 mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
1035 * Destroy mutexes and release memory back the to free pool.
1041 struct pmclog_buffer *plb;
1043 mtx_destroy(&pmc_kthread_mtx);
1044 mtx_destroy(&pmc_bufferlist_mtx);
1046 while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
1047 TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);