2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2007 Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
6 * Copyright (c) 2018 Matthew Macy
9 * Portions of this software were developed by A. Joseph Koshy under
10 * sponsorship from the FreeBSD Foundation and Google, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Logging code for hwpmc(4)
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/capsicum.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
51 #include <sys/pmckern.h>
52 #include <sys/pmclog.h>
54 #include <sys/sched.h>
55 #include <sys/signalvar.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
61 #include <sys/unistd.h>
62 #include <sys/vnode.h>
64 #if defined(__i386__) || defined(__amd64__)
65 #include <machine/clock.h>
69 #define NDOMAINS vm_ndomains
70 #define curdomain PCPU_GET(domain)
74 #define malloc_domain(size, type, domain, flags) malloc((size), (type), (flags))
75 #define free_domain(addr, type) free(addr, type)
82 SYSCTL_DECL(_kern_hwpmc);
85 * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
88 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
89 #if (__FreeBSD_version < 1100000)
90 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
92 SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RDTUN,
93 &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
96 * kern.hwpmc.nbuffer -- number of global log buffers
99 static int pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
100 #if (__FreeBSD_version < 1100000)
101 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers_pcpu);
103 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers_pcpu, CTLFLAG_RDTUN,
104 &pmc_nlogbuffers_pcpu, 0, "number of log buffers per cpu");
107 * Global log buffer list and associated spin lock.
110 static struct mtx pmc_kthread_mtx; /* sleep lock */
112 #define PMCLOG_INIT_BUFFER_DESCRIPTOR(D, buf, domain) do { \
113 (D)->plb_fence = ((char *) (buf)) + 1024*pmclog_buffer_size; \
114 (D)->plb_base = (D)->plb_ptr = ((char *) (buf)); \
115 (D)->plb_domain = domain; \
118 #define PMCLOG_RESET_BUFFER_DESCRIPTOR(D) do { \
119 (D)->plb_ptr = (D)->plb_base; \
123 * Log file record constructors.
125 #define _PMCLOG_TO_HEADER(T,L) \
126 ((PMCLOG_HEADER_MAGIC << 24) | \
127 (PMCLOG_TYPE_ ## T << 16) | \
130 /* reserve LEN bytes of space and initialize the entry header */
131 #define _PMCLOG_RESERVE_SAFE(PO,TYPE,LEN,ACTION, TSC) do { \
133 int _len = roundup((LEN), sizeof(uint32_t)); \
134 struct pmclog_header *ph; \
135 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
138 ph = (struct pmclog_header *)_le; \
139 ph->pl_header =_PMCLOG_TO_HEADER(TYPE,_len); \
140 ph->pl_tsc = (TSC); \
141 _le += sizeof(*ph)/4 /* skip over timestamp */
143 /* reserve LEN bytes of space and initialize the entry header */
144 #define _PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do { \
146 int _len = roundup((LEN), sizeof(uint32_t)); \
148 struct pmclog_header *ph; \
151 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
155 ph = (struct pmclog_header *)_le; \
156 ph->pl_header =_PMCLOG_TO_HEADER(TYPE,_len); \
158 _le += sizeof(*ph)/4 /* skip over timestamp */
162 #define PMCLOG_RESERVE_SAFE(P,T,L,TSC) _PMCLOG_RESERVE_SAFE(P,T,L,return,TSC)
163 #define PMCLOG_RESERVE(P,T,L) _PMCLOG_RESERVE(P,T,L,return)
164 #define PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L, \
165 error=ENOMEM;goto error)
167 #define PMCLOG_EMIT32(V) do { *_le++ = (V); } while (0)
168 #define PMCLOG_EMIT64(V) do { \
169 *_le++ = (uint32_t) ((V) & 0xFFFFFFFF); \
170 *_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF); \
174 /* Emit a string. Caution: does NOT update _le, so needs to be last */
175 #define PMCLOG_EMITSTRING(S,L) do { bcopy((S), _le, (L)); } while (0)
176 #define PMCLOG_EMITNULLSTRING(L) do { bzero(_le, (L)); } while (0)
178 #define PMCLOG_DESPATCH_SAFE(PO) \
179 pmclog_release((PO)); \
182 #define PMCLOG_DESPATCH_SCHED_LOCK(PO) \
183 pmclog_release_flags((PO), 0); \
186 #define PMCLOG_DESPATCH(PO) \
187 pmclog_release((PO)); \
191 #define PMCLOG_DESPATCH_SYNC(PO) \
192 pmclog_schedule_io((PO), 1); \
199 * Assertions about the log file format.
201 CTASSERT(sizeof(struct pmclog_callchain) == 7*4 + TSDELTA +
202 PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t));
203 CTASSERT(sizeof(struct pmclog_closelog) == 3*4 + TSDELTA);
204 CTASSERT(sizeof(struct pmclog_dropnotify) == 3*4 + TSDELTA);
205 CTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX + TSDELTA +
206 5*4 + sizeof(uintfptr_t));
207 CTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
208 5*4 + TSDELTA + sizeof(uintfptr_t));
209 CTASSERT(sizeof(struct pmclog_map_out) == 5*4 + 2*sizeof(uintfptr_t) + TSDELTA);
210 CTASSERT(sizeof(struct pmclog_pmcallocate) == 9*4 + TSDELTA);
211 CTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX + TSDELTA);
212 CTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4 + TSDELTA);
213 CTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4 + TSDELTA);
214 CTASSERT(sizeof(struct pmclog_proccsw) == 7*4 + 8 + TSDELTA);
215 CTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
216 sizeof(uintfptr_t) + TSDELTA);
217 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 + TSDELTA +
219 CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8 + TSDELTA);
220 CTASSERT(sizeof(struct pmclog_procfork) == 5*4 + TSDELTA);
221 CTASSERT(sizeof(struct pmclog_sysexit) == 6*4);
222 CTASSERT(sizeof(struct pmclog_userdata) == 6*4);
225 * Log buffer structure
228 struct pmclog_buffer {
229 TAILQ_ENTRY(pmclog_buffer) plb_next;
234 } __aligned(CACHE_LINE_SIZE);
240 static int pmclog_get_buffer(struct pmc_owner *po);
241 static void pmclog_loop(void *arg);
242 static void pmclog_release(struct pmc_owner *po);
243 static uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
244 static void pmclog_schedule_io(struct pmc_owner *po, int wakeup);
245 static void pmclog_schedule_all(struct pmc_owner *po, int force);
246 static void pmclog_stop_kthread(struct pmc_owner *po);
253 pmc_plb_rele_unlocked(struct pmclog_buffer *plb)
255 TAILQ_INSERT_HEAD(&pmc_dom_hdrs[plb->plb_domain]->pdbh_head, plb, plb_next);
259 pmc_plb_rele(struct pmclog_buffer *plb)
261 mtx_lock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx);
262 pmc_plb_rele_unlocked(plb);
263 mtx_unlock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx);
270 pmclog_get_buffer(struct pmc_owner *po)
272 struct pmclog_buffer *plb;
275 KASSERT(po->po_curbuf[curcpu] == NULL,
276 ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
279 MPASS(pmc_dom_hdrs[domain]);
280 mtx_lock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx);
281 if ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL)
282 TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next);
283 mtx_unlock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx);
285 PMCDBG2(LOG,GTB,1, "po=%p plb=%p", po, plb);
289 KASSERT(plb->plb_ptr == plb->plb_base &&
290 plb->plb_base < plb->plb_fence,
291 ("[pmclog,%d] po=%p buffer invariants: ptr=%p "
292 "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
293 plb->plb_base, plb->plb_fence));
296 po->po_curbuf[curcpu] = plb;
299 counter_u64_add(pmc_stats.pm_buffer_requests, 1);
301 counter_u64_add(pmc_stats.pm_buffer_requests_failed, 1);
303 return (plb ? 0 : ENOMEM);
306 struct pmclog_proc_init_args {
308 struct pmc_owner *po;
314 pmclog_proc_create(struct thread *td, void **handlep)
316 struct pmclog_proc_init_args *ia;
319 ia = malloc(sizeof(*ia), M_TEMP, M_WAITOK | M_ZERO);
320 error = kproc_create(pmclog_loop, ia, &ia->kthr,
321 RFHIGHPID, 0, "hwpmc: proc(%d)", td->td_proc->p_pid);
328 pmclog_proc_ignite(void *handle, struct pmc_owner *po)
330 struct pmclog_proc_init_args *ia;
333 mtx_lock(&pmc_kthread_mtx);
335 MPASS(ia->po == NULL);
337 MPASS(ia->kthr != NULL);
342 KASSERT(po->po_kthread == NULL,
343 ("[pmclog,%d] po=%p kthread (%p) already present",
344 __LINE__, po, po->po_kthread));
345 po->po_kthread = ia->kthr;
349 msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogw", 0);
350 mtx_unlock(&pmc_kthread_mtx);
357 * This function is executed by each pmc owner's helper thread.
360 pmclog_loop(void *arg)
362 struct pmclog_proc_init_args *ia;
363 struct pmc_owner *po;
364 struct pmclog_buffer *lb;
366 struct ucred *ownercred;
367 struct ucred *mycred;
378 SIGADDSET(unb, SIGHUP);
379 (void)kern_sigprocmask(td, SIG_UNBLOCK, &unb, NULL, 0);
382 MPASS(ia->kthr == curproc);
384 mtx_lock(&pmc_kthread_mtx);
385 while (ia->po == NULL && !ia->exit)
386 msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogi", 0);
390 mtx_unlock(&pmc_kthread_mtx);
393 MPASS(ia->po != NULL);
397 mtx_unlock(&pmc_kthread_mtx);
401 mycred = td->td_ucred;
404 ownercred = crhold(p->p_ucred);
407 PMCDBG2(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
408 KASSERT(po->po_kthread == curthread->td_proc,
409 ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
410 po, po->po_kthread, curthread->td_proc));
416 * Loop waiting for I/O requests to be added to the owner
417 * struct's queue. The loop is exited when the log file
421 mtx_lock(&pmc_kthread_mtx);
425 /* check if we've been asked to exit */
426 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
429 if (lb == NULL) { /* look for a fresh buffer to write */
430 mtx_lock_spin(&po->po_mtx);
431 if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
432 mtx_unlock_spin(&po->po_mtx);
434 /* No more buffers and shutdown required. */
435 if (po->po_flags & PMC_PO_SHUTDOWN)
438 (void) msleep(po, &pmc_kthread_mtx, PWAIT,
443 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
444 mtx_unlock_spin(&po->po_mtx);
447 mtx_unlock(&pmc_kthread_mtx);
449 /* process the request */
450 PMCDBG3(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
451 lb->plb_base, lb->plb_ptr);
452 /* change our thread's credentials before issuing the I/O */
454 aiov.iov_base = lb->plb_base;
455 aiov.iov_len = nbytes = lb->plb_ptr - lb->plb_base;
457 auio.uio_iov = &aiov;
459 auio.uio_offset = -1;
460 auio.uio_resid = nbytes;
461 auio.uio_rw = UIO_WRITE;
462 auio.uio_segflg = UIO_SYSSPACE;
465 /* switch thread credentials -- see kern_ktrace.c */
466 td->td_ucred = ownercred;
467 error = fo_write(po->po_file, &auio, ownercred, 0, td);
468 td->td_ucred = mycred;
471 /* XXX some errors are recoverable */
472 /* send a SIGIO to the owner and exit */
474 kern_psignal(p, SIGIO);
477 mtx_lock(&pmc_kthread_mtx);
479 po->po_error = error; /* save for flush log */
481 PMCDBG2(LOG,WRI,2, "po=%p error=%d", po, error);
486 mtx_lock(&pmc_kthread_mtx);
488 /* put the used buffer back into the global pool */
489 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
495 wakeup_one(po->po_kthread);
496 po->po_kthread = NULL;
498 mtx_unlock(&pmc_kthread_mtx);
500 /* return the current I/O buffer to the global pool */
502 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
508 * Exit this thread, signalling the waiter
517 * Release and log entry and schedule an I/O if needed.
521 pmclog_release_flags(struct pmc_owner *po, int wakeup)
523 struct pmclog_buffer *plb;
525 plb = po->po_curbuf[curcpu];
526 KASSERT(plb->plb_ptr >= plb->plb_base,
527 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
528 po, plb->plb_ptr, plb->plb_base));
529 KASSERT(plb->plb_ptr <= plb->plb_fence,
530 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
531 po, plb->plb_ptr, plb->plb_fence));
533 /* schedule an I/O if we've filled a buffer */
534 if (plb->plb_ptr >= plb->plb_fence)
535 pmclog_schedule_io(po, wakeup);
537 PMCDBG1(LOG,REL,1, "po=%p", po);
541 pmclog_release(struct pmc_owner *po)
544 pmclog_release_flags(po, 1);
549 * Attempt to reserve 'length' bytes of space in an owner's log
550 * buffer. The function returns a pointer to 'length' bytes of space
551 * if there was enough space or returns NULL if no space was
552 * available. Non-null returns do so with the po mutex locked. The
553 * caller must invoke pmclog_release() on the pmc owner structure
558 pmclog_reserve(struct pmc_owner *po, int length)
560 uintptr_t newptr, oldptr;
561 struct pmclog_buffer *plb, **pplb;
563 PMCDBG2(LOG,ALL,1, "po=%p len=%d", po, length);
565 KASSERT(length % sizeof(uint32_t) == 0,
566 ("[pmclog,%d] length not a multiple of word size", __LINE__));
568 /* No more data when shutdown in progress. */
569 if (po->po_flags & PMC_PO_SHUTDOWN)
572 pplb = &po->po_curbuf[curcpu];
573 if (*pplb == NULL && pmclog_get_buffer(po) != 0)
576 KASSERT(*pplb != NULL,
577 ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
580 KASSERT(plb->plb_ptr >= plb->plb_base &&
581 plb->plb_ptr <= plb->plb_fence,
582 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
583 __LINE__, po, plb->plb_ptr, plb->plb_base,
586 oldptr = (uintptr_t) plb->plb_ptr;
587 newptr = oldptr + length;
589 KASSERT(oldptr != (uintptr_t) NULL,
590 ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
593 * If we have space in the current buffer, return a pointer to
594 * available space with the PO structure locked.
596 if (newptr <= (uintptr_t) plb->plb_fence) {
597 plb->plb_ptr = (char *) newptr;
602 * Otherwise, schedule the current buffer for output and get a
605 pmclog_schedule_io(po, 0);
607 if (pmclog_get_buffer(po) != 0)
612 ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
614 KASSERT(plb->plb_ptr != NULL,
615 ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
617 KASSERT(plb->plb_ptr == plb->plb_base &&
618 plb->plb_ptr <= plb->plb_fence,
619 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
620 __LINE__, po, plb->plb_ptr, plb->plb_base,
623 oldptr = (uintptr_t) plb->plb_ptr;
626 return ((uint32_t *) oldptr);
634 * Transfer the current buffer to the helper kthread.
638 pmclog_schedule_io(struct pmc_owner *po, int wakeup)
640 struct pmclog_buffer *plb;
642 plb = po->po_curbuf[curcpu];
643 po->po_curbuf[curcpu] = NULL;
645 ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
646 KASSERT(plb->plb_ptr >= plb->plb_base,
647 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
648 po, plb->plb_ptr, plb->plb_base));
649 KASSERT(plb->plb_ptr <= plb->plb_fence,
650 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
651 po, plb->plb_ptr, plb->plb_fence));
653 PMCDBG1(LOG,SIO, 1, "po=%p", po);
656 * Add the current buffer to the tail of the buffer list and
659 mtx_lock_spin(&po->po_mtx);
660 TAILQ_INSERT_TAIL(&po->po_logbuffers, plb, plb_next);
661 mtx_unlock_spin(&po->po_mtx);
667 * Stop the helper kthread.
671 pmclog_stop_kthread(struct pmc_owner *po)
674 mtx_lock(&pmc_kthread_mtx);
675 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
676 if (po->po_kthread != NULL) {
677 PROC_LOCK(po->po_kthread);
678 kern_psignal(po->po_kthread, SIGHUP);
679 PROC_UNLOCK(po->po_kthread);
682 while (po->po_kthread)
683 msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
684 mtx_unlock(&pmc_kthread_mtx);
692 * Configure a log file for pmc owner 'po'.
694 * Parameter 'logfd' is a file handle referencing an open file in the
695 * owner process. This file needs to have been opened for writing.
699 pmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
706 sx_assert(&pmc_sx, SA_XLOCKED);
707 PMCDBG2(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
711 /* return EBUSY if a log file was already present */
712 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
715 KASSERT(po->po_file == NULL,
716 ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
719 /* get a reference to the file state */
720 error = fget_write(curthread, logfd, &cap_write_rights, &po->po_file);
724 /* mark process as owning a log file */
725 po->po_flags |= PMC_PO_OWNS_LOGFILE;
727 /* mark process as using HWPMCs */
729 p->p_flag |= P_HWPMC;
733 /* create a log initialization entry */
734 PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
735 sizeof(struct pmclog_initialize));
736 PMCLOG_EMIT32(PMC_VERSION);
737 PMCLOG_EMIT32(md->pmd_cputype);
738 #if defined(__i386__) || defined(__amd64__)
739 PMCLOG_EMIT64(tsc_freq);
741 /* other architectures will need to fill this in */
745 memcpy(_le, &ts, sizeof(ts));
747 PMCLOG_EMITSTRING(pmc_cpuid, PMC_CPUID_LEN);
748 PMCLOG_DESPATCH_SYNC(po);
753 KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
754 "stopped", __LINE__, po));
757 (void) fdrop(po->po_file, curthread);
758 po->po_file = NULL; /* clear file and error state */
760 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
767 * De-configure a log file. This will throw away any buffers queued
768 * for this owner process.
772 pmclog_deconfigure_log(struct pmc_owner *po)
775 struct pmclog_buffer *lb;
777 PMCDBG1(LOG,CFG,1, "de-config po=%p", po);
779 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
782 KASSERT(po->po_sscount == 0,
783 ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
784 KASSERT(po->po_file != NULL,
785 ("[pmclog,%d] po=%p no log file", __LINE__, po));
787 /* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
788 pmclog_stop_kthread(po);
790 KASSERT(po->po_kthread == NULL,
791 ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
793 /* return all queued log buffers to the global pool */
794 while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
795 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
796 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
799 for (int i = 0; i < mp_ncpus; i++) {
800 thread_lock(curthread);
801 sched_bind(curthread, i);
802 thread_unlock(curthread);
803 /* return the 'current' buffer to the global pool */
804 if ((lb = po->po_curbuf[curcpu]) != NULL) {
805 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
809 thread_lock(curthread);
810 sched_unbind(curthread);
811 thread_unlock(curthread);
813 /* drop a reference to the fd */
814 if (po->po_file != NULL) {
815 error = fdrop(po->po_file, curthread);
825 * Flush a process' log buffer.
829 pmclog_flush(struct pmc_owner *po, int force)
833 PMCDBG1(LOG,FLS,1, "po=%p", po);
836 * If there is a pending error recorded by the logger thread,
840 return (po->po_error);
845 * Check that we do have an active log file.
847 mtx_lock(&pmc_kthread_mtx);
848 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
853 pmclog_schedule_all(po, force);
855 mtx_unlock(&pmc_kthread_mtx);
861 pmclog_schedule_one_cond(struct pmc_owner *po, int force)
863 struct pmclog_buffer *plb;
868 /* tell hardclock not to run again */
869 if (PMC_CPU_HAS_SAMPLES(cpu))
870 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
872 pmc_flush_samples(cpu);
873 plb = po->po_curbuf[cpu];
874 if (plb && plb->plb_ptr != plb->plb_base)
875 pmclog_schedule_io(po, 1);
880 pmclog_schedule_all(struct pmc_owner *po, int force)
883 * Schedule the current buffer if any and not empty.
885 for (int i = 0; i < mp_ncpus; i++) {
886 thread_lock(curthread);
887 sched_bind(curthread, i);
888 thread_unlock(curthread);
889 pmclog_schedule_one_cond(po, force);
891 thread_lock(curthread);
892 sched_unbind(curthread);
893 thread_unlock(curthread);
897 pmclog_close(struct pmc_owner *po)
900 PMCDBG1(LOG,CLO,1, "po=%p", po);
902 pmclog_process_closelog(po);
904 mtx_lock(&pmc_kthread_mtx);
906 * Initiate shutdown: no new data queued,
907 * thread will close file on last block.
909 po->po_flags |= PMC_PO_SHUTDOWN;
910 /* give time for all to see */
914 * Schedule the current buffer.
916 pmclog_schedule_all(po, 0);
919 mtx_unlock(&pmc_kthread_mtx);
925 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
929 struct pmc_owner *po;
931 PMCDBG3(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
934 recordlen = offsetof(struct pmclog_callchain, pl_pc) +
935 ps->ps_nsamples * sizeof(uintfptr_t);
937 flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
938 PMCLOG_RESERVE_SAFE(po, CALLCHAIN, recordlen, ps->ps_tsc);
939 PMCLOG_EMIT32(ps->ps_pid);
940 PMCLOG_EMIT32(ps->ps_tid);
941 PMCLOG_EMIT32(pm->pm_id);
942 PMCLOG_EMIT32(flags);
943 for (n = 0; n < ps->ps_nsamples; n++)
944 PMCLOG_EMITADDR(ps->ps_pc[n]);
945 PMCLOG_DESPATCH_SAFE(po);
949 pmclog_process_closelog(struct pmc_owner *po)
951 PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
952 PMCLOG_DESPATCH_SYNC(po);
956 pmclog_process_dropnotify(struct pmc_owner *po)
958 PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
963 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
966 int pathlen, recordlen;
968 KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
970 pathlen = strlen(path) + 1; /* #bytes for path name */
971 recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
974 PMCLOG_RESERVE(po, MAP_IN, recordlen);
977 PMCLOG_EMITADDR(start);
978 PMCLOG_EMITSTRING(path,pathlen);
979 PMCLOG_DESPATCH_SYNC(po);
983 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
986 KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
988 PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
991 PMCLOG_EMITADDR(start);
992 PMCLOG_EMITADDR(end);
997 pmclog_process_pmcallocate(struct pmc *pm)
999 struct pmc_owner *po;
1000 struct pmc_soft *ps;
1004 PMCDBG1(LOG,ALL,1, "pm=%p", pm);
1006 if (PMC_TO_CLASS(pm) == PMC_CLASS_SOFT) {
1007 PMCLOG_RESERVE(po, PMCALLOCATEDYN,
1008 sizeof(struct pmclog_pmcallocatedyn));
1009 PMCLOG_EMIT32(pm->pm_id);
1010 PMCLOG_EMIT32(pm->pm_event);
1011 PMCLOG_EMIT32(pm->pm_flags);
1013 PMCLOG_EMIT64(pm->pm_sc.pm_reloadcount);
1014 ps = pmc_soft_ev_acquire(pm->pm_event);
1016 PMCLOG_EMITSTRING(ps->ps_ev.pm_ev_name,PMC_NAME_MAX);
1018 PMCLOG_EMITNULLSTRING(PMC_NAME_MAX);
1019 pmc_soft_ev_release(ps);
1020 PMCLOG_DESPATCH_SYNC(po);
1022 PMCLOG_RESERVE(po, PMCALLOCATE,
1023 sizeof(struct pmclog_pmcallocate));
1024 PMCLOG_EMIT32(pm->pm_id);
1025 PMCLOG_EMIT32(pm->pm_event);
1026 PMCLOG_EMIT32(pm->pm_flags);
1028 PMCLOG_EMIT64(pm->pm_sc.pm_reloadcount);
1029 PMCLOG_DESPATCH_SYNC(po);
1034 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
1036 int pathlen, recordlen;
1037 struct pmc_owner *po;
1039 PMCDBG2(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
1043 pathlen = strlen(path) + 1; /* #bytes for the string */
1044 recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
1046 PMCLOG_RESERVE(po, PMCATTACH, recordlen);
1047 PMCLOG_EMIT32(pm->pm_id);
1049 PMCLOG_EMITSTRING(path, pathlen);
1050 PMCLOG_DESPATCH_SYNC(po);
1054 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
1056 struct pmc_owner *po;
1058 PMCDBG2(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
1062 PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
1063 PMCLOG_EMIT32(pm->pm_id);
1065 PMCLOG_DESPATCH_SYNC(po);
1069 pmclog_process_proccreate(struct pmc_owner *po, struct proc *p, int sync)
1072 PMCLOG_RESERVE(po, PROC_CREATE, sizeof(struct pmclog_proccreate));
1073 PMCLOG_EMIT32(p->p_pid);
1074 PMCLOG_EMIT32(p->p_flag);
1075 PMCLOG_EMITSTRING(p->p_comm, MAXCOMLEN+1);
1076 PMCLOG_DESPATCH_SYNC(po);
1078 PMCLOG_RESERVE(po, PROC_CREATE, sizeof(struct pmclog_proccreate));
1079 PMCLOG_EMIT32(p->p_pid);
1080 PMCLOG_EMIT32(p->p_flag);
1081 PMCLOG_EMITSTRING(p->p_comm, MAXCOMLEN+1);
1082 PMCLOG_DESPATCH(po);
1087 * Log a context switch event to the log file.
1091 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v, struct thread *td)
1093 struct pmc_owner *po;
1095 KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
1096 ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
1098 PMCDBG3(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
1103 PMCLOG_RESERVE_SAFE(po, PROCCSW, sizeof(struct pmclog_proccsw), pmc_rdtsc());
1105 PMCLOG_EMIT32(pm->pm_id);
1106 PMCLOG_EMIT32(pp->pp_proc->p_pid);
1107 PMCLOG_EMIT32(td->td_tid);
1109 PMCLOG_DESPATCH_SCHED_LOCK(po);
1113 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
1114 uintfptr_t startaddr, char *path)
1116 int pathlen, recordlen;
1118 PMCDBG3(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
1120 pathlen = strlen(path) + 1; /* #bytes for the path */
1121 recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
1122 PMCLOG_RESERVE(po, PROCEXEC, recordlen);
1124 PMCLOG_EMIT32(pmid);
1125 PMCLOG_EMITADDR(startaddr);
1126 PMCLOG_EMITSTRING(path,pathlen);
1127 PMCLOG_DESPATCH_SYNC(po);
1131 * Log a process exit event (and accumulated pmc value) to the log file.
1135 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
1138 struct pmc_owner *po;
1140 ri = PMC_TO_ROWINDEX(pm);
1141 PMCDBG3(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
1142 pp->pp_pmcs[ri].pp_pmcval);
1146 PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
1147 PMCLOG_EMIT32(pm->pm_id);
1148 PMCLOG_EMIT32(pp->pp_proc->p_pid);
1149 PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
1150 PMCLOG_DESPATCH(po);
1158 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
1160 PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
1161 PMCLOG_EMIT32(oldpid);
1162 PMCLOG_EMIT32(newpid);
1163 PMCLOG_DESPATCH(po);
1167 * Log a process exit event of the form suitable for system-wide PMCs.
1171 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
1173 PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
1175 PMCLOG_DESPATCH(po);
1179 pmclog_process_threadcreate(struct pmc_owner *po, struct thread *td, int sync)
1185 PMCLOG_RESERVE(po, THR_CREATE, sizeof(struct pmclog_threadcreate));
1186 PMCLOG_EMIT32(td->td_tid);
1187 PMCLOG_EMIT32(p->p_pid);
1188 PMCLOG_EMIT32(p->p_flag);
1190 PMCLOG_EMITSTRING(td->td_name, MAXCOMLEN+1);
1191 PMCLOG_DESPATCH_SYNC(po);
1193 PMCLOG_RESERVE(po, THR_CREATE, sizeof(struct pmclog_threadcreate));
1194 PMCLOG_EMIT32(td->td_tid);
1195 PMCLOG_EMIT32(p->p_pid);
1196 PMCLOG_EMIT32(p->p_flag);
1198 PMCLOG_EMITSTRING(td->td_name, MAXCOMLEN+1);
1199 PMCLOG_DESPATCH(po);
1204 pmclog_process_threadexit(struct pmc_owner *po, struct thread *td)
1207 PMCLOG_RESERVE(po, THR_EXIT, sizeof(struct pmclog_threadexit));
1208 PMCLOG_EMIT32(td->td_tid);
1209 PMCLOG_DESPATCH(po);
1213 * Write a user log entry.
1217 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
1221 PMCDBG2(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
1225 PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
1226 sizeof(struct pmclog_userdata));
1227 PMCLOG_EMIT32(wl->pm_userdata);
1228 PMCLOG_DESPATCH(po);
1237 * Create a pool of log buffers and initialize mutexes.
1244 struct pmclog_buffer *plb;
1246 if (pmclog_buffer_size <= 0 || pmclog_buffer_size > 16*1024) {
1247 (void) printf("hwpmc: tunable logbuffersize=%d must be "
1248 "greater than zero and less than or equal to 16MB.\n",
1249 pmclog_buffer_size);
1250 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1253 if (pmc_nlogbuffers_pcpu <= 0) {
1254 (void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
1255 "than zero.\n", pmc_nlogbuffers_pcpu);
1256 pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
1258 if (pmc_nlogbuffers_pcpu*pmclog_buffer_size > 32*1024) {
1259 (void) printf("hwpmc: memory allocated pcpu must be less than 32MB (is %dK).\n",
1260 pmc_nlogbuffers_pcpu*pmclog_buffer_size);
1261 pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
1262 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1264 for (domain = 0; domain < NDOMAINS; domain++) {
1265 int ncpus = pmc_dom_hdrs[domain]->pdbh_ncpus;
1266 int total = ncpus*pmc_nlogbuffers_pcpu;
1268 plb = malloc_domain(sizeof(struct pmclog_buffer)*total, M_PMC, domain, M_WAITOK|M_ZERO);
1269 pmc_dom_hdrs[domain]->pdbh_plbs = plb;
1270 for (int i = 0; i < total; i++, plb++) {
1273 buf = malloc_domain(1024 * pmclog_buffer_size, M_PMC, domain,
1275 PMCLOG_INIT_BUFFER_DESCRIPTOR(plb, buf, domain);
1276 pmc_plb_rele_unlocked(plb);
1279 mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
1285 * Destroy mutexes and release memory back the to free pool.
1291 struct pmclog_buffer *plb;
1294 mtx_destroy(&pmc_kthread_mtx);
1296 for (domain = 0; domain < NDOMAINS; domain++) {
1297 while ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL) {
1298 TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next);
1299 free(plb->plb_base, M_PMC);
1301 free(pmc_dom_hdrs[domain]->pdbh_plbs, M_PMC);