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>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <sys/pmclog.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
60 SYSCTL_DECL(_kern_hwpmc);
63 * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
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");
72 * kern.hwpmc.nbuffer -- number of global log buffers
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");
81 * Global log buffer list and associated spin lock.
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 */
89 #define PMCLOG_INIT_BUFFER_DESCRIPTOR(D) do { \
90 const int __roundup = roundup(sizeof(*D), \
92 (D)->plb_fence = ((char *) (D)) + \
93 1024*pmclog_buffer_size; \
94 (D)->plb_base = (D)->plb_ptr = ((char *) (D)) + \
100 * Log file record constructors.
102 #define _PMCLOG_TO_HEADER(T,L) \
103 ((PMCLOG_HEADER_MAGIC << 24) | \
104 (PMCLOG_TYPE_ ## T << 16) | \
107 /* reserve LEN bytes of space and initialize the entry header */
108 #define _PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do { \
110 int _len = roundup((LEN), sizeof(uint32_t)); \
111 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
114 *_le = _PMCLOG_TO_HEADER(TYPE,_len); \
115 _le += 3 /* skip over timestamp */
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)
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); \
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)
131 #define PMCLOG_DESPATCH(PO) \
132 pmclog_release((PO)); \
137 * Assertions about the log file format.
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 +
157 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
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);
165 * Log buffer structure
168 struct pmclog_buffer {
169 TAILQ_ENTRY(pmclog_buffer) plb_next;
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);
195 pmclog_get_buffer(struct pmc_owner *po)
197 struct pmclog_buffer *plb;
199 mtx_assert(&po->po_mtx, MA_OWNED);
201 KASSERT(po->po_curbuf == NULL,
202 ("[pmc,%d] po=%p current buffer still valid", __LINE__, po));
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);
209 PMCDBG(LOG,GTB,1, "po=%p plb=%p", po, plb);
213 KASSERT(plb->plb_ptr == plb->plb_base &&
214 plb->plb_base < plb->plb_fence,
215 ("[pmc,%d] po=%p buffer invariants: ptr=%p "
216 "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
217 plb->plb_base, plb->plb_fence));
223 atomic_add_int(&pmc_stats.pm_buffer_requests, 1);
225 atomic_add_int(&pmc_stats.pm_buffer_requests_failed, 1);
227 return plb ? 0 : ENOMEM;
233 * This function is executed by each pmc owner's helper thread.
237 pmclog_loop(void *arg)
240 struct pmc_owner *po;
241 struct pmclog_buffer *lb;
242 struct ucred *ownercred;
243 struct ucred *mycred;
249 po = (struct pmc_owner *) arg;
251 mycred = td->td_ucred;
253 PROC_LOCK(po->po_owner);
254 ownercred = crhold(po->po_owner->p_ucred);
255 PROC_UNLOCK(po->po_owner);
257 PMCDBG(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
258 KASSERT(po->po_kthread == curthread->td_proc,
259 ("[pmc,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
260 po, po->po_kthread, curthread->td_proc));
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
271 mtx_lock(&pmc_kthread_mtx);
275 /* check if we've been asked to exit */
276 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
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);
284 /* wakeup any processes waiting for a FLUSH */
285 if (po->po_flags & PMC_PO_IN_FLUSH) {
286 po->po_flags &= ~PMC_PO_IN_FLUSH;
287 wakeup_one(po->po_kthread);
290 (void) msleep(po, &pmc_kthread_mtx, PWAIT,
295 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
296 mtx_unlock_spin(&po->po_mtx);
299 mtx_unlock(&pmc_kthread_mtx);
301 /* process the request */
302 PMCDBG(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
303 lb->plb_base, lb->plb_ptr);
304 /* change our thread's credentials before issuing the I/O */
306 aiov.iov_base = lb->plb_base;
307 aiov.iov_len = nbytes = lb->plb_ptr - lb->plb_base;
309 auio.uio_iov = &aiov;
311 auio.uio_offset = -1;
312 auio.uio_resid = nbytes;
313 auio.uio_rw = UIO_WRITE;
314 auio.uio_segflg = UIO_SYSSPACE;
317 /* switch thread credentials -- see kern_ktrace.c */
318 td->td_ucred = ownercred;
319 error = fo_write(po->po_file, &auio, ownercred, 0, td);
320 td->td_ucred = mycred;
322 mtx_lock(&pmc_kthread_mtx);
325 /* XXX some errors are recoverable */
326 /* XXX also check for SIGPIPE if a socket */
328 /* send a SIGIO to the owner and exit */
329 PROC_LOCK(po->po_owner);
330 psignal(po->po_owner, SIGIO);
331 PROC_UNLOCK(po->po_owner);
333 po->po_error = error; /* save for flush log */
335 PMCDBG(LOG,WRI,2, "po=%p error=%d", po, error);
340 /* put the used buffer back into the global pool */
341 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
343 mtx_lock_spin(&pmc_bufferlist_mtx);
344 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
345 mtx_unlock_spin(&pmc_bufferlist_mtx);
350 po->po_kthread = NULL;
352 mtx_unlock(&pmc_kthread_mtx);
354 /* return the current I/O buffer to the global pool */
356 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
358 mtx_lock_spin(&pmc_bufferlist_mtx);
359 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
360 mtx_unlock_spin(&pmc_bufferlist_mtx);
364 * Exit this thread, signalling the waiter
373 * Release and log entry and schedule an I/O if needed.
377 pmclog_release(struct pmc_owner *po)
379 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
380 ("[pmc,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
381 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
382 KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
383 ("[pmc,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
384 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
386 /* schedule an I/O if we've filled a buffer */
387 if (po->po_curbuf->plb_ptr >= po->po_curbuf->plb_fence)
388 pmclog_schedule_io(po);
390 mtx_unlock_spin(&po->po_mtx);
392 PMCDBG(LOG,REL,1, "po=%p", po);
397 * Attempt to reserve 'length' bytes of space in an owner's log
398 * buffer. The function returns a pointer to 'length' bytes of space
399 * if there was enough space or returns NULL if no space was
400 * available. Non-null returns do so with the po mutex locked. The
401 * caller must invoke pmclog_release() on the pmc owner structure
406 pmclog_reserve(struct pmc_owner *po, int length)
408 uintptr_t newptr, oldptr;
412 PMCDBG(LOG,ALL,1, "po=%p len=%d", po, length);
414 KASSERT(length % sizeof(uint32_t) == 0,
415 ("[pmclog,%d] length not a multiple of word size", __LINE__));
417 mtx_lock_spin(&po->po_mtx);
419 if (po->po_curbuf == NULL)
420 if (pmclog_get_buffer(po) != 0) {
421 mtx_unlock_spin(&po->po_mtx);
425 KASSERT(po->po_curbuf != NULL,
426 ("[pmc,%d] po=%p no current buffer", __LINE__, po));
428 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base &&
429 po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
430 ("[pmc,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
431 __LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
432 po->po_curbuf->plb_fence));
434 oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
435 newptr = oldptr + length;
437 KASSERT(oldptr != (uintptr_t) NULL,
438 ("[pmc,%d] po=%p Null log buffer pointer", __LINE__, po));
441 * If we have space in the current buffer, return a pointer to
442 * available space with the PO structure locked.
444 if (newptr <= (uintptr_t) po->po_curbuf->plb_fence) {
445 po->po_curbuf->plb_ptr = (char *) newptr;
450 * Otherwise, schedule the current buffer for output and get a
453 pmclog_schedule_io(po);
455 if (pmclog_get_buffer(po) != 0) {
456 mtx_unlock_spin(&po->po_mtx);
460 KASSERT(po->po_curbuf != NULL,
461 ("[pmc,%d] po=%p no current buffer", __LINE__, po));
463 KASSERT(po->po_curbuf->plb_ptr != NULL,
464 ("[pmc,%d] null return from pmc_get_log_buffer", __LINE__));
466 KASSERT(po->po_curbuf->plb_ptr == po->po_curbuf->plb_base &&
467 po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
468 ("[pmc,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
469 __LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
470 po->po_curbuf->plb_fence));
472 oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
475 lh = (uint32_t *) oldptr;
476 lh++; /* skip header */
477 getnanotime(&ts); /* fill in the timestamp */
478 *lh++ = ts.tv_sec & 0xFFFFFFFF;
479 *lh++ = ts.tv_nsec & 0xFFFFFFF;
480 return (uint32_t *) oldptr;
486 * Transfer the current buffer to the helper kthread.
490 pmclog_schedule_io(struct pmc_owner *po)
492 KASSERT(po->po_curbuf != NULL,
493 ("[pmc,%d] schedule_io with null buffer po=%p", __LINE__, po));
495 KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
496 ("[pmc,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
497 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
498 KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
499 ("[pmc,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
500 po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
502 PMCDBG(LOG,SIO, 1, "po=%p", po);
504 mtx_assert(&po->po_mtx, MA_OWNED);
507 * Add the current buffer to the tail of the buffer list and
510 TAILQ_INSERT_TAIL(&po->po_logbuffers, po->po_curbuf, plb_next);
511 po->po_curbuf = NULL;
516 * Stop the helper kthread.
520 pmclog_stop_kthread(struct pmc_owner *po)
523 * Unset flag, wakeup the helper thread,
524 * wait for it to exit
527 mtx_assert(&pmc_kthread_mtx, MA_OWNED);
528 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
531 msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
539 * Configure a log file for pmc owner 'po'.
541 * Parameter 'logfd' is a file handle referencing an open file in the
542 * owner process. This file needs to have been opened for writing.
546 pmclog_configure_log(struct pmc_owner *po, int logfd)
551 PMCDBG(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
555 /* return EBUSY if a log file was already present */
556 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
559 KASSERT(po->po_kthread == NULL,
560 ("[pmc,%d] po=%p kthread (%p) already present", __LINE__, po,
562 KASSERT(po->po_file == NULL,
563 ("[pmc,%d] po=%p file (%p) already present", __LINE__, po,
566 /* get a reference to the file state */
567 error = fget_write(curthread, logfd, &po->po_file);
571 /* mark process as owning a log file */
572 po->po_flags |= PMC_PO_OWNS_LOGFILE;
573 error = kproc_create(pmclog_loop, po, &po->po_kthread,
574 RFHIGHPID, 0, "hwpmc: proc(%d)", p->p_pid);
578 /* mark process as using HWPMCs */
580 p->p_flag |= P_HWPMC;
583 /* create a log initialization entry */
584 PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
585 sizeof(struct pmclog_initialize));
586 PMCLOG_EMIT32(PMC_VERSION);
587 PMCLOG_EMIT32(md->pmd_cputype);
593 /* shutdown the thread */
594 mtx_lock(&pmc_kthread_mtx);
596 pmclog_stop_kthread(po);
597 mtx_unlock(&pmc_kthread_mtx);
599 KASSERT(po->po_kthread == NULL, ("[pmc,%d] po=%p kthread not stopped",
603 (void) fdrop(po->po_file, curthread);
604 po->po_file = NULL; /* clear file and error state */
612 * De-configure a log file. This will throw away any buffers queued
613 * for this owner process.
617 pmclog_deconfigure_log(struct pmc_owner *po)
620 struct pmclog_buffer *lb;
622 PMCDBG(LOG,CFG,1, "de-config po=%p", po);
624 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
627 KASSERT(po->po_sscount == 0,
628 ("[pmc,%d] po=%p still owning SS PMCs", __LINE__, po));
629 KASSERT(po->po_file != NULL,
630 ("[pmc,%d] po=%p no log file", __LINE__, po));
632 /* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
633 mtx_lock(&pmc_kthread_mtx);
635 pmclog_stop_kthread(po);
636 mtx_unlock(&pmc_kthread_mtx);
638 KASSERT(po->po_kthread == NULL,
639 ("[pmc,%d] po=%p kthread not stopped", __LINE__, po));
641 /* return all queued log buffers to the global pool */
642 while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
643 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
644 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
645 mtx_lock_spin(&pmc_bufferlist_mtx);
646 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
647 mtx_unlock_spin(&pmc_bufferlist_mtx);
650 /* return the 'current' buffer to the global pool */
651 if ((lb = po->po_curbuf) != NULL) {
652 PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
653 mtx_lock_spin(&pmc_bufferlist_mtx);
654 TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
655 mtx_unlock_spin(&pmc_bufferlist_mtx);
658 /* drop a reference to the fd */
659 error = fdrop(po->po_file, curthread);
667 * Flush a process' log buffer.
671 pmclog_flush(struct pmc_owner *po)
673 int error, has_pending_buffers;
675 PMCDBG(LOG,FLS,1, "po=%p", po);
678 * If there is a pending error recorded by the logger thread,
687 * Check that we do have an active log file.
689 mtx_lock(&pmc_kthread_mtx);
690 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
696 * Schedule the current buffer if any.
698 mtx_lock_spin(&po->po_mtx);
700 pmclog_schedule_io(po);
701 has_pending_buffers = !TAILQ_EMPTY(&po->po_logbuffers);
702 mtx_unlock_spin(&po->po_mtx);
704 if (has_pending_buffers) {
705 po->po_flags |= PMC_PO_IN_FLUSH; /* ask for a wakeup */
706 error = msleep(po->po_kthread, &pmc_kthread_mtx, PWAIT,
711 mtx_unlock(&pmc_kthread_mtx);
718 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
722 struct pmc_owner *po;
724 PMCDBG(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
727 recordlen = offsetof(struct pmclog_callchain, pl_pc) +
728 ps->ps_nsamples * sizeof(uintfptr_t);
730 flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
731 PMCLOG_RESERVE(po, CALLCHAIN, recordlen);
732 PMCLOG_EMIT32(ps->ps_pid);
733 PMCLOG_EMIT32(pm->pm_id);
734 PMCLOG_EMIT32(flags);
735 for (n = 0; n < ps->ps_nsamples; n++)
736 PMCLOG_EMITADDR(ps->ps_pc[n]);
741 pmclog_process_closelog(struct pmc_owner *po)
743 PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
748 pmclog_process_dropnotify(struct pmc_owner *po)
750 PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
755 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
758 int pathlen, recordlen;
760 KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
762 pathlen = strlen(path) + 1; /* #bytes for path name */
763 recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
766 PMCLOG_RESERVE(po, MAP_IN, recordlen);
768 PMCLOG_EMITADDR(start);
769 PMCLOG_EMITSTRING(path,pathlen);
774 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
777 KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
779 PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
781 PMCLOG_EMITADDR(start);
782 PMCLOG_EMITADDR(end);
787 pmclog_process_pmcallocate(struct pmc *pm)
789 struct pmc_owner *po;
793 PMCDBG(LOG,ALL,1, "pm=%p", pm);
795 PMCLOG_RESERVE(po, PMCALLOCATE, sizeof(struct pmclog_pmcallocate));
796 PMCLOG_EMIT32(pm->pm_id);
797 PMCLOG_EMIT32(pm->pm_event);
798 PMCLOG_EMIT32(pm->pm_flags);
803 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
805 int pathlen, recordlen;
806 struct pmc_owner *po;
808 PMCDBG(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
812 pathlen = strlen(path) + 1; /* #bytes for the string */
813 recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
815 PMCLOG_RESERVE(po, PMCATTACH, recordlen);
816 PMCLOG_EMIT32(pm->pm_id);
818 PMCLOG_EMITSTRING(path, pathlen);
823 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
825 struct pmc_owner *po;
827 PMCDBG(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
831 PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
832 PMCLOG_EMIT32(pm->pm_id);
838 * Log a context switch event to the log file.
842 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
844 struct pmc_owner *po;
846 KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
847 ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
849 PMCDBG(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
854 PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
855 PMCLOG_EMIT32(pm->pm_id);
857 PMCLOG_EMIT32(pp->pp_proc->p_pid);
862 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
863 uintfptr_t startaddr, char *path)
865 int pathlen, recordlen;
867 PMCDBG(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
869 pathlen = strlen(path) + 1; /* #bytes for the path */
870 recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
872 PMCLOG_RESERVE(po, PROCEXEC, recordlen);
874 PMCLOG_EMITADDR(startaddr);
876 PMCLOG_EMITSTRING(path,pathlen);
881 * Log a process exit event (and accumulated pmc value) to the log file.
885 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
888 struct pmc_owner *po;
890 ri = PMC_TO_ROWINDEX(pm);
891 PMCDBG(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
892 pp->pp_pmcs[ri].pp_pmcval);
896 PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
897 PMCLOG_EMIT32(pm->pm_id);
898 PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
899 PMCLOG_EMIT32(pp->pp_proc->p_pid);
908 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
910 PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
911 PMCLOG_EMIT32(oldpid);
912 PMCLOG_EMIT32(newpid);
917 * Log a process exit event of the form suitable for system-wide PMCs.
921 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
923 PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
929 * Write a user log entry.
933 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
937 PMCDBG(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
941 PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
942 sizeof(struct pmclog_userdata));
943 PMCLOG_EMIT32(wl->pm_userdata);
953 * Create a pool of log buffers and initialize mutexes.
960 struct pmclog_buffer *plb;
962 if (pmclog_buffer_size <= 0) {
963 (void) printf("hwpmc: tunable logbuffersize=%d must be greater "
964 "than zero.\n", pmclog_buffer_size);
965 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
968 if (pmc_nlogbuffers <= 0) {
969 (void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
970 "than zero.\n", pmc_nlogbuffers);
971 pmc_nlogbuffers = PMC_NLOGBUFFERS;
974 /* create global pool of log buffers */
975 for (n = 0; n < pmc_nlogbuffers; n++) {
976 MALLOC(plb, struct pmclog_buffer *, 1024 * pmclog_buffer_size,
977 M_PMC, M_ZERO|M_WAITOK);
978 PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
979 TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
981 mtx_init(&pmc_bufferlist_mtx, "pmc-buffer-list", "pmc-leaf",
983 mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
989 * Destroy mutexes and release memory back the to free pool.
995 struct pmclog_buffer *plb;
997 mtx_destroy(&pmc_kthread_mtx);
998 mtx_destroy(&pmc_bufferlist_mtx);
1000 while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
1001 TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);