2 * Copyright (c) 2005 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Logging code for hwpmc(4)
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <sys/pmclog.h>
45 #include <sys/signalvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
56 SYSCTL_DECL(_kern_hwpmc);
59 * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
62 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
63 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
64 SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_TUN|CTLFLAG_RD,
65 &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
69 * kern.hwpmc.nbuffer -- number of global log buffers
72 static int pmc_nlogbuffers = PMC_NLOGBUFFERS;
73 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers);
74 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers, CTLFLAG_TUN|CTLFLAG_RD,
75 &pmc_nlogbuffers, 0, "number of global log buffers");
78 * Global log buffer list and associated spin lock.
81 TAILQ_HEAD(, pmclog_buffer) pmc_bufferlist =
82 TAILQ_HEAD_INITIALIZER(pmc_bufferlist);
83 static struct mtx pmc_bufferlist_mtx; /* spin lock */
84 static struct mtx pmc_kthread_mtx; /* sleep lock */
86 #define PMCLOG_INIT_BUFFER_DESCRIPTOR(D) do { \
87 const int __roundup = roundup(sizeof(*D), \
89 (D)->plb_fence = ((char *) (D)) + \
90 1024*pmclog_buffer_size; \
91 (D)->plb_base = (D)->plb_ptr = ((char *) (D)) + \
97 * Log file record constructors.
100 #define _PMCLOG_TO_HEADER(T,L) \
101 ((PMCLOG_HEADER_MAGIC << 24) | \
102 (PMCLOG_TYPE_ ## T << 16) | \
105 /* reserve LEN bytes of space and initialize the entry header */
106 #define _PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do { \
108 int _len = roundup((LEN), sizeof(uint32_t)); \
109 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
112 *_le = _PMCLOG_TO_HEADER(TYPE,_len); \
113 _le += 3 /* skip over timestamp */
115 #define PMCLOG_RESERVE(P,T,L) _PMCLOG_RESERVE(P,T,L,return)
116 #define PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L, \
117 error=ENOMEM;goto error)
119 #define PMCLOG_EMIT32(V) do { *_le++ = (V); } while (0)
120 #define PMCLOG_EMIT64(V) do { \
121 *_le++ = (uint32_t) ((V) & 0xFFFFFFFF); \
122 *_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF); \
126 /* Emit a string. Caution: does NOT update _le, so needs to be last */
127 #define PMCLOG_EMITSTRING(S,L) do { bcopy((S), _le, (L)); } while (0)
129 #define PMCLOG_DESPATCH(PO) \
130 pmclog_release((PO)); \
135 * Assertions about the log file format.
138 CTASSERT(sizeof(struct pmclog_closelog) == 3*4);
139 CTASSERT(sizeof(struct pmclog_dropnotify) == 3*4);
140 CTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX +
141 4*4 + sizeof(uintfptr_t));
142 CTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
143 4*4 + sizeof(uintfptr_t));
144 CTASSERT(sizeof(struct pmclog_map_out) == 4*4 + 2*sizeof(uintfptr_t));
145 CTASSERT(sizeof(struct pmclog_pcsample) == 6*4 + sizeof(uintfptr_t));
146 CTASSERT(sizeof(struct pmclog_pmcallocate) == 6*4);
147 CTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX);
148 CTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4);
149 CTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4);
150 CTASSERT(sizeof(struct pmclog_proccsw) == 5*4 + 8);
151 CTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
153 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
155 CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8);
156 CTASSERT(sizeof(struct pmclog_procfork) == 5*4);
157 CTASSERT(sizeof(struct pmclog_sysexit) == 4*4);
158 CTASSERT(sizeof(struct pmclog_userdata) == 4*4);
161 * Log buffer structure
164 struct pmclog_buffer {
165 TAILQ_ENTRY(pmclog_buffer) plb_next;
175 static int pmclog_get_buffer(struct pmc_owner *po);
176 static void pmclog_loop(void *arg);
177 static void pmclog_release(struct pmc_owner *po);
178 static uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
179 static void pmclog_schedule_io(struct pmc_owner *po);
180 static void pmclog_stop_kthread(struct pmc_owner *po);
191 pmclog_get_buffer(struct pmc_owner *po)
193 struct pmclog_buffer *plb;
195 mtx_assert(&po->po_mtx, MA_OWNED);
197 KASSERT(po->po_curbuf == NULL,
198 ("[pmc,%d] po=%p current buffer still valid", __LINE__, po));
200 mtx_lock_spin(&pmc_bufferlist_mtx);
201 if ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL)
202 TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
203 mtx_unlock_spin(&pmc_bufferlist_mtx);
205 PMCDBG(LOG,GTB,1, "po=%p plb=%p", po, plb);
209 KASSERT(plb->plb_ptr == plb->plb_base &&
210 plb->plb_base < plb->plb_fence,
211 ("[pmc,%d] po=%p buffer invariants: ptr=%p "
212 "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
213 plb->plb_base, plb->plb_fence));
219 atomic_add_int(&pmc_stats.pm_buffer_requests, 1);
221 atomic_add_int(&pmc_stats.pm_buffer_requests_failed, 1);
223 return plb ? 0 : ENOMEM;
229 * This function is executed by each pmc owner's helper thread.
233 pmclog_loop(void *arg)
236 struct pmc_owner *po;
237 struct pmclog_buffer *lb;
238 struct ucred *ownercred;
239 struct ucred *mycred;
245 po = (struct pmc_owner *) arg;
247 mycred = td->td_ucred;
249 PROC_LOCK(po->po_owner);
250 ownercred = crhold(po->po_owner->p_ucred);
251 PROC_UNLOCK(po->po_owner);
253 PMCDBG(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
254 KASSERT(po->po_kthread == curthread->td_proc,
255 ("[pmc,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
256 po, po->po_kthread, curthread->td_proc));
262 * Loop waiting for I/O requests to be added to the owner
263 * struct's queue. The loop is exited when the log file
267 mtx_lock(&pmc_kthread_mtx);
271 /* check if we've been asked to exit */
272 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
275 if (lb == NULL) { /* look for a fresh buffer to write */
276 mtx_lock_spin(&po->po_mtx);
277 if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
278 mtx_unlock_spin(&po->po_mtx);
280 /* wakeup any processes waiting for a FLUSH */
281 if (po->po_flags & PMC_PO_IN_FLUSH) {
282 po->po_flags &= ~PMC_PO_IN_FLUSH;
283 wakeup_one(po->po_kthread);
286 (void) msleep(po, &pmc_kthread_mtx, PWAIT,
291 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
292 mtx_unlock_spin(&po->po_mtx);
295 mtx_unlock(&pmc_kthread_mtx);
297 /* process the request */
298 PMCDBG(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
299 lb->plb_base, lb->plb_ptr);
300 /* change our thread's credentials before issuing the I/O */
302 aiov.iov_base = lb->plb_base;
303 aiov.iov_len = nbytes = lb->plb_ptr - lb->plb_base;
305 auio.uio_iov = &aiov;
307 auio.uio_offset = -1;
308 auio.uio_resid = nbytes;
309 auio.uio_rw = UIO_WRITE;
310 auio.uio_segflg = UIO_SYSSPACE;
313 /* switch thread credentials -- see kern_ktrace.c */
314 td->td_ucred = ownercred;
315 error = fo_write(po->po_file, &auio, ownercred, 0, td);
316 td->td_ucred = mycred;
318 mtx_lock(&pmc_kthread_mtx);
321 /* XXX some errors are recoverable */
322 /* XXX also check for SIGPIPE if a socket */
324 /* send a SIGIO to the owner and exit */
325 PROC_LOCK(po->po_owner);
326 psignal(po->po_owner, SIGIO);
327 PROC_UNLOCK(po->po_owner);
329 po->po_error = error; /* save for flush log */
331 PMCDBG(LOG,WRI,2, "po=%p error=%d", po, error);
336 /* put the used buffer back into the global pool */
337 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
339 mtx_lock_spin(&pmc_bufferlist_mtx);
340 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
341 mtx_unlock_spin(&pmc_bufferlist_mtx);
346 po->po_kthread = NULL;
348 mtx_unlock(&pmc_kthread_mtx);
350 /* return the current I/O buffer to the global pool */
352 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
354 mtx_lock_spin(&pmc_bufferlist_mtx);
355 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
356 mtx_unlock_spin(&pmc_bufferlist_mtx);
360 * Exit this thread, signalling the waiter
369 * Release and log entry and schedule an I/O if needed.
373 pmclog_release(struct pmc_owner *po)
375 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
376 ("[pmc,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
377 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
378 KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
379 ("[pmc,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
380 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
382 /* schedule an I/O if we've filled a buffer */
383 if (po->po_curbuf->plb_ptr >= po->po_curbuf->plb_fence)
384 pmclog_schedule_io(po);
386 mtx_unlock_spin(&po->po_mtx);
388 PMCDBG(LOG,REL,1, "po=%p", po);
393 * Attempt to reserve 'length' bytes of space in an owner's log
394 * buffer. The function returns a pointer to 'length' bytes of space
395 * if there was enough space or returns NULL if no space was
396 * available. Non-null returns do so with the po mutex locked. The
397 * caller must invoke pmclog_release() on the pmc owner structure
402 pmclog_reserve(struct pmc_owner *po, int length)
404 uintptr_t newptr, oldptr;
408 PMCDBG(LOG,ALL,1, "po=%p len=%d", po, length);
410 KASSERT(length % sizeof(uint32_t) == 0,
411 ("[pmclog,%d] length not a multiple of word size", __LINE__));
413 mtx_lock_spin(&po->po_mtx);
415 if (po->po_curbuf == NULL)
416 if (pmclog_get_buffer(po) != 0) {
417 mtx_unlock_spin(&po->po_mtx);
421 KASSERT(po->po_curbuf != NULL,
422 ("[pmc,%d] po=%p no current buffer", __LINE__, po));
424 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base &&
425 po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
426 ("[pmc,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
427 __LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
428 po->po_curbuf->plb_fence));
430 oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
431 newptr = oldptr + length;
433 KASSERT(oldptr != (uintptr_t) NULL,
434 ("[pmc,%d] po=%p Null log buffer pointer", __LINE__, po));
437 * If we have space in the current buffer, return a pointer to
438 * available space with the PO structure locked.
440 if (newptr <= (uintptr_t) po->po_curbuf->plb_fence) {
441 po->po_curbuf->plb_ptr = (char *) newptr;
446 * Otherwise, schedule the current buffer for output and get a
449 pmclog_schedule_io(po);
451 if (pmclog_get_buffer(po) != 0) {
452 mtx_unlock_spin(&po->po_mtx);
456 KASSERT(po->po_curbuf != NULL,
457 ("[pmc,%d] po=%p no current buffer", __LINE__, po));
459 KASSERT(po->po_curbuf->plb_ptr != NULL,
460 ("[pmc,%d] null return from pmc_get_log_buffer", __LINE__));
462 KASSERT(po->po_curbuf->plb_ptr == po->po_curbuf->plb_base &&
463 po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
464 ("[pmc,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
465 __LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
466 po->po_curbuf->plb_fence));
468 oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
471 lh = (uint32_t *) oldptr;
472 lh++; /* skip header */
473 getnanotime(&ts); /* fill in the timestamp */
474 *lh++ = ts.tv_sec & 0xFFFFFFFF;
475 *lh++ = ts.tv_nsec & 0xFFFFFFF;
476 return (uint32_t *) oldptr;
482 * Transfer the current buffer to the helper kthread.
486 pmclog_schedule_io(struct pmc_owner *po)
488 KASSERT(po->po_curbuf != NULL,
489 ("[pmc,%d] schedule_io with null buffer po=%p", __LINE__, po));
491 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
492 ("[pmc,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
493 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
494 KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
495 ("[pmc,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
496 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
498 PMCDBG(LOG,SIO, 1, "po=%p", po);
500 mtx_assert(&po->po_mtx, MA_OWNED);
503 * Add the current buffer to the tail of the buffer list and
506 TAILQ_INSERT_TAIL(&po->po_logbuffers, po->po_curbuf, plb_next);
507 po->po_curbuf = NULL;
512 * Stop the helper kthread.
516 pmclog_stop_kthread(struct pmc_owner *po)
519 * Unset flag, wakeup the helper thread,
520 * wait for it to exit
523 mtx_assert(&pmc_kthread_mtx, MA_OWNED);
524 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
527 msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
535 * Configure a log file for pmc owner 'po'.
537 * Parameter 'logfd' is a file handle referencing an open file in the
538 * owner process. This file needs to have been opened for writing.
542 pmclog_configure_log(struct pmc_owner *po, int logfd)
547 PMCDBG(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
551 /* return EBUSY if a log file was already present */
552 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
555 KASSERT(po->po_kthread == NULL,
556 ("[pmc,%d] po=%p kthread (%p) already present", __LINE__, po,
558 KASSERT(po->po_file == NULL,
559 ("[pmc,%d] po=%p file (%p) already present", __LINE__, po,
562 /* get a reference to the file state */
563 error = fget_write(curthread, logfd, &po->po_file);
567 /* mark process as owning a log file */
568 po->po_flags |= PMC_PO_OWNS_LOGFILE;
569 error = kthread_create(pmclog_loop, po, &po->po_kthread,
570 RFHIGHPID, 0, "hwpmc: proc(%d)", p->p_pid);
574 /* mark process as using HWPMCs */
576 p->p_flag |= P_HWPMC;
579 /* create a log initialization entry */
580 PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
581 sizeof(struct pmclog_initialize));
582 PMCLOG_EMIT32(PMC_VERSION);
583 PMCLOG_EMIT32(md->pmd_cputype);
589 /* shutdown the thread */
590 mtx_lock(&pmc_kthread_mtx);
592 pmclog_stop_kthread(po);
593 mtx_unlock(&pmc_kthread_mtx);
595 KASSERT(po->po_kthread == NULL, ("[pmc,%d] po=%p kthread not stopped",
599 (void) fdrop(po->po_file, curthread);
600 po->po_file = NULL; /* clear file and error state */
608 * De-configure a log file. This will throw away any buffers queued
609 * for this owner process.
613 pmclog_deconfigure_log(struct pmc_owner *po)
616 struct pmclog_buffer *lb;
618 PMCDBG(LOG,CFG,1, "de-config po=%p", po);
620 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
623 KASSERT(po->po_sscount == 0,
624 ("[pmc,%d] po=%p still owning SS PMCs", __LINE__, po));
625 KASSERT(po->po_file != NULL,
626 ("[pmc,%d] po=%p no log file", __LINE__, po));
628 /* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
629 mtx_lock(&pmc_kthread_mtx);
631 pmclog_stop_kthread(po);
632 mtx_unlock(&pmc_kthread_mtx);
634 KASSERT(po->po_kthread == NULL,
635 ("[pmc,%d] po=%p kthread not stopped", __LINE__, po));
637 /* return all queued log buffers to the global pool */
638 while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
639 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
640 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
641 mtx_lock_spin(&pmc_bufferlist_mtx);
642 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
643 mtx_unlock_spin(&pmc_bufferlist_mtx);
646 /* return the 'current' buffer to the global pool */
647 if ((lb = po->po_curbuf) != NULL) {
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);
654 /* drop a reference to the fd */
655 error = fdrop(po->po_file, curthread);
663 * Flush a process' log buffer.
667 pmclog_flush(struct pmc_owner *po)
669 int error, has_pending_buffers;
671 PMCDBG(LOG,FLS,1, "po=%p", po);
674 * If there is a pending error recorded by the logger thread,
683 * Check that we do have an active log file.
685 mtx_lock(&pmc_kthread_mtx);
686 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
692 * Schedule the current buffer if any.
694 mtx_lock_spin(&po->po_mtx);
696 pmclog_schedule_io(po);
697 has_pending_buffers = !TAILQ_EMPTY(&po->po_logbuffers);
698 mtx_unlock_spin(&po->po_mtx);
700 if (has_pending_buffers) {
701 po->po_flags |= PMC_PO_IN_FLUSH; /* ask for a wakeup */
702 error = msleep(po->po_kthread, &pmc_kthread_mtx, PWAIT,
707 mtx_unlock(&pmc_kthread_mtx);
714 * Send a 'close log' event to the log file.
718 pmclog_process_closelog(struct pmc_owner *po)
720 PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
725 pmclog_process_dropnotify(struct pmc_owner *po)
727 PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
732 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
735 int pathlen, recordlen;
737 KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
739 pathlen = strlen(path) + 1; /* #bytes for path name */
740 recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
743 PMCLOG_RESERVE(po, MAP_IN, recordlen);
745 PMCLOG_EMITADDR(start);
746 PMCLOG_EMITSTRING(path,pathlen);
751 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
754 KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
756 PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
758 PMCLOG_EMITADDR(start);
759 PMCLOG_EMITADDR(end);
764 pmclog_process_pcsample(struct pmc *pm, struct pmc_sample *ps)
766 struct pmc_owner *po;
768 PMCDBG(LOG,SAM,1,"pm=%p pid=%d pc=%p", pm, ps->ps_pid,
773 PMCLOG_RESERVE(po, PCSAMPLE, sizeof(struct pmclog_pcsample));
774 PMCLOG_EMIT32(ps->ps_pid);
775 PMCLOG_EMITADDR(ps->ps_pc);
776 PMCLOG_EMIT32(pm->pm_id);
777 PMCLOG_EMIT32(ps->ps_usermode);
782 pmclog_process_pmcallocate(struct pmc *pm)
784 struct pmc_owner *po;
788 PMCDBG(LOG,ALL,1, "pm=%p", pm);
790 PMCLOG_RESERVE(po, PMCALLOCATE, sizeof(struct pmclog_pmcallocate));
791 PMCLOG_EMIT32(pm->pm_id);
792 PMCLOG_EMIT32(pm->pm_event);
793 PMCLOG_EMIT32(pm->pm_flags);
798 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
800 int pathlen, recordlen;
801 struct pmc_owner *po;
803 PMCDBG(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
807 pathlen = strlen(path) + 1; /* #bytes for the string */
808 recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
810 PMCLOG_RESERVE(po, PMCATTACH, recordlen);
811 PMCLOG_EMIT32(pm->pm_id);
813 PMCLOG_EMITSTRING(path, pathlen);
818 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
820 struct pmc_owner *po;
822 PMCDBG(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
826 PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
827 PMCLOG_EMIT32(pm->pm_id);
833 * Log a context switch event to the log file.
837 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
839 struct pmc_owner *po;
841 KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
842 ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
844 PMCDBG(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
849 PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
850 PMCLOG_EMIT32(pm->pm_id);
852 PMCLOG_EMIT32(pp->pp_proc->p_pid);
857 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
858 uintfptr_t startaddr, char *path)
860 int pathlen, recordlen;
862 PMCDBG(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
864 pathlen = strlen(path) + 1; /* #bytes for the path */
865 recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
867 PMCLOG_RESERVE(po, PROCEXEC, recordlen);
869 PMCLOG_EMITADDR(startaddr);
871 PMCLOG_EMITSTRING(path,pathlen);
876 * Log a process exit event (and accumulated pmc value) to the log file.
880 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
883 struct pmc_owner *po;
885 ri = PMC_TO_ROWINDEX(pm);
886 PMCDBG(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
887 pp->pp_pmcs[ri].pp_pmcval);
891 PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
892 PMCLOG_EMIT32(pm->pm_id);
893 PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
894 PMCLOG_EMIT32(pp->pp_proc->p_pid);
903 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
905 PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
906 PMCLOG_EMIT32(oldpid);
907 PMCLOG_EMIT32(newpid);
912 * Log a process exit event of the form suitable for system-wide PMCs.
916 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
918 PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
924 * Write a user log entry.
928 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
932 PMCDBG(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
936 PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
937 sizeof(struct pmclog_userdata));
938 PMCLOG_EMIT32(wl->pm_userdata);
948 * Create a pool of log buffers and initialize mutexes.
955 struct pmclog_buffer *plb;
957 if (pmclog_buffer_size <= 0) {
958 (void) printf("hwpmc: tunable logbuffersize=%d must be greater "
959 "than zero.\n", pmclog_buffer_size);
960 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
963 if (pmc_nlogbuffers <= 0) {
964 (void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
965 "than zero.\n", pmc_nlogbuffers);
966 pmc_nlogbuffers = PMC_NLOGBUFFERS;
969 /* create global pool of log buffers */
970 for (n = 0; n < pmc_nlogbuffers; n++) {
971 MALLOC(plb, struct pmclog_buffer *, 1024 * pmclog_buffer_size,
972 M_PMC, M_ZERO|M_WAITOK);
973 PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
974 TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
976 mtx_init(&pmc_bufferlist_mtx, "pmc-buffer-list", "pmc-leaf",
978 mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
984 * Destroy mutexes and release memory back the to free pool.
990 struct pmclog_buffer *plb;
992 mtx_destroy(&pmc_kthread_mtx);
993 mtx_destroy(&pmc_bufferlist_mtx);
995 while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
996 TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);