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>
68 SYSCTL_DECL(_kern_hwpmc);
71 * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
74 static int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
75 #if (__FreeBSD_version < 1100000)
76 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
78 SYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RDTUN,
79 &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
82 * kern.hwpmc.nbuffer -- number of global log buffers
85 static int pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
86 #if (__FreeBSD_version < 1100000)
87 TUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers_pcpu);
89 SYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers_pcpu, CTLFLAG_RDTUN,
90 &pmc_nlogbuffers_pcpu, 0, "number of log buffers per cpu");
93 * Global log buffer list and associated spin lock.
96 static struct mtx pmc_kthread_mtx; /* sleep lock */
98 #define PMCLOG_INIT_BUFFER_DESCRIPTOR(D, buf, domain) do { \
99 (D)->plb_fence = ((char *) (buf)) + 1024*pmclog_buffer_size; \
100 (D)->plb_base = (D)->plb_ptr = ((char *) (buf)); \
101 (D)->plb_domain = domain; \
104 #define PMCLOG_RESET_BUFFER_DESCRIPTOR(D) do { \
105 (D)->plb_ptr = (D)->plb_base; \
109 * Log file record constructors.
111 #define _PMCLOG_TO_HEADER(T,L) \
112 ((PMCLOG_HEADER_MAGIC << 24) | \
113 (PMCLOG_TYPE_ ## T << 16) | \
116 /* reserve LEN bytes of space and initialize the entry header */
117 #define _PMCLOG_RESERVE_SAFE(PO,TYPE,LEN,ACTION) do { \
119 int _len = roundup((LEN), sizeof(uint32_t)); \
120 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
123 *_le = _PMCLOG_TO_HEADER(TYPE,_len); \
124 _le += 3 /* skip over timestamp */
126 /* reserve LEN bytes of space and initialize the entry header */
127 #define _PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do { \
129 int _len = roundup((LEN), sizeof(uint32_t)); \
131 if ((_le = pmclog_reserve((PO), _len)) == NULL) { \
135 *_le = _PMCLOG_TO_HEADER(TYPE,_len); \
136 _le += 3 /* skip over timestamp */
139 #define PMCLOG_RESERVE_SAFE(P,T,L) _PMCLOG_RESERVE_SAFE(P,T,L,return)
140 #define PMCLOG_RESERVE(P,T,L) _PMCLOG_RESERVE(P,T,L,return)
141 #define PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L, \
142 error=ENOMEM;goto error)
144 #define PMCLOG_EMIT32(V) do { *_le++ = (V); } while (0)
145 #define PMCLOG_EMIT64(V) do { \
146 *_le++ = (uint32_t) ((V) & 0xFFFFFFFF); \
147 *_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF); \
151 /* Emit a string. Caution: does NOT update _le, so needs to be last */
152 #define PMCLOG_EMITSTRING(S,L) do { bcopy((S), _le, (L)); } while (0)
153 #define PMCLOG_EMITNULLSTRING(L) do { bzero(_le, (L)); } while (0)
155 #define PMCLOG_DESPATCH_SAFE(PO) \
156 pmclog_release((PO)); \
159 #define PMCLOG_DESPATCH(PO) \
160 pmclog_release((PO)); \
164 #define PMCLOG_DESPATCH_SYNC(PO) \
165 pmclog_schedule_io((PO)); \
171 * Assertions about the log file format.
174 CTASSERT(sizeof(struct pmclog_callchain) == 6*4 +
175 PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t));
176 CTASSERT(sizeof(struct pmclog_closelog) == 3*4);
177 CTASSERT(sizeof(struct pmclog_dropnotify) == 3*4);
178 CTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX +
179 4*4 + sizeof(uintfptr_t));
180 CTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
181 4*4 + sizeof(uintfptr_t));
182 CTASSERT(sizeof(struct pmclog_map_out) == 4*4 + 2*sizeof(uintfptr_t));
183 CTASSERT(sizeof(struct pmclog_pcsample) == 6*4 + sizeof(uintfptr_t));
184 CTASSERT(sizeof(struct pmclog_pmcallocate) == 6*4);
185 CTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX);
186 CTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4);
187 CTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4);
188 CTASSERT(sizeof(struct pmclog_proccsw) == 5*4 + 8);
189 CTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
191 CTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
193 CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8);
194 CTASSERT(sizeof(struct pmclog_procfork) == 5*4);
195 CTASSERT(sizeof(struct pmclog_sysexit) == 4*4);
196 CTASSERT(sizeof(struct pmclog_userdata) == 4*4);
199 * Log buffer structure
202 struct pmclog_buffer {
203 TAILQ_ENTRY(pmclog_buffer) plb_next;
208 } __aligned(CACHE_LINE_SIZE);
210 struct pmc_domain_buffer_header {
212 TAILQ_HEAD(, pmclog_buffer) pdbh_head;
213 struct pmclog_buffer *pdbh_plbs;
215 } __aligned(CACHE_LINE_SIZE);
217 struct pmc_domain_buffer_header *pmc_dom_hdrs[MAXMEMDOM];
224 static int pmclog_get_buffer(struct pmc_owner *po);
225 static void pmclog_loop(void *arg);
226 static void pmclog_release(struct pmc_owner *po);
227 static uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
228 static void pmclog_schedule_io(struct pmc_owner *po);
229 static void pmclog_schedule_all(struct pmc_owner *po);
230 static void pmclog_stop_kthread(struct pmc_owner *po);
237 pmc_plb_rele_unlocked(struct pmclog_buffer *plb)
239 TAILQ_INSERT_HEAD(&pmc_dom_hdrs[plb->plb_domain]->pdbh_head, plb, plb_next);
243 pmc_plb_rele(struct pmclog_buffer *plb)
245 mtx_lock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx);
246 pmc_plb_rele_unlocked(plb);
247 mtx_unlock_spin(&pmc_dom_hdrs[plb->plb_domain]->pdbh_mtx);
256 pmclog_get_buffer(struct pmc_owner *po)
258 struct pmclog_buffer *plb;
261 KASSERT(po->po_curbuf[curcpu] == NULL,
262 ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
264 domain = PCPU_GET(domain);
265 mtx_lock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx);
266 if ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL)
267 TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next);
268 mtx_unlock_spin(&pmc_dom_hdrs[domain]->pdbh_mtx);
270 PMCDBG2(LOG,GTB,1, "po=%p plb=%p", po, plb);
274 KASSERT(plb->plb_ptr == plb->plb_base &&
275 plb->plb_base < plb->plb_fence,
276 ("[pmclog,%d] po=%p buffer invariants: ptr=%p "
277 "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
278 plb->plb_base, plb->plb_fence));
281 po->po_curbuf[curcpu] = plb;
284 counter_u64_add(pmc_stats.pm_buffer_requests, 1);
286 counter_u64_add(pmc_stats.pm_buffer_requests_failed, 1);
288 return (plb ? 0 : ENOMEM);
291 struct pmclog_proc_init_args {
293 struct pmc_owner *po;
299 pmclog_proc_create(struct thread *td, void **handlep)
301 struct pmclog_proc_init_args *ia;
304 ia = malloc(sizeof(*ia), M_TEMP, M_WAITOK | M_ZERO);
305 error = kproc_create(pmclog_loop, ia, &ia->kthr,
306 RFHIGHPID, 0, "hwpmc: proc(%d)", td->td_proc->p_pid);
313 pmclog_proc_ignite(void *handle, struct pmc_owner *po)
315 struct pmclog_proc_init_args *ia;
318 mtx_lock(&pmc_kthread_mtx);
320 MPASS(ia->po == NULL);
322 MPASS(ia->kthr != NULL);
327 KASSERT(po->po_kthread == NULL,
328 ("[pmclog,%d] po=%p kthread (%p) already present",
329 __LINE__, po, po->po_kthread));
330 po->po_kthread = ia->kthr;
334 msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogw", 0);
335 mtx_unlock(&pmc_kthread_mtx);
342 * This function is executed by each pmc owner's helper thread.
346 pmclog_loop(void *arg)
348 struct pmclog_proc_init_args *ia;
349 struct pmc_owner *po;
350 struct pmclog_buffer *lb;
352 struct ucred *ownercred;
353 struct ucred *mycred;
364 SIGADDSET(unb, SIGHUP);
365 (void)kern_sigprocmask(td, SIG_UNBLOCK, &unb, NULL, 0);
368 MPASS(ia->kthr == curproc);
370 mtx_lock(&pmc_kthread_mtx);
371 while (ia->po == NULL && !ia->exit)
372 msleep(ia, &pmc_kthread_mtx, PWAIT, "pmclogi", 0);
376 mtx_unlock(&pmc_kthread_mtx);
379 MPASS(ia->po != NULL);
383 mtx_unlock(&pmc_kthread_mtx);
387 mycred = td->td_ucred;
390 ownercred = crhold(p->p_ucred);
393 PMCDBG2(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
394 KASSERT(po->po_kthread == curthread->td_proc,
395 ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
396 po, po->po_kthread, curthread->td_proc));
402 * Loop waiting for I/O requests to be added to the owner
403 * struct's queue. The loop is exited when the log file
407 mtx_lock(&pmc_kthread_mtx);
411 /* check if we've been asked to exit */
412 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
415 if (lb == NULL) { /* look for a fresh buffer to write */
416 mtx_lock_spin(&po->po_mtx);
417 if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
418 mtx_unlock_spin(&po->po_mtx);
420 /* No more buffers and shutdown required. */
421 if (po->po_flags & PMC_PO_SHUTDOWN)
424 (void) msleep(po, &pmc_kthread_mtx, PWAIT,
429 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
430 mtx_unlock_spin(&po->po_mtx);
433 mtx_unlock(&pmc_kthread_mtx);
435 /* process the request */
436 PMCDBG3(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
437 lb->plb_base, lb->plb_ptr);
438 /* change our thread's credentials before issuing the I/O */
440 aiov.iov_base = lb->plb_base;
441 aiov.iov_len = nbytes = lb->plb_ptr - lb->plb_base;
443 auio.uio_iov = &aiov;
445 auio.uio_offset = -1;
446 auio.uio_resid = nbytes;
447 auio.uio_rw = UIO_WRITE;
448 auio.uio_segflg = UIO_SYSSPACE;
451 /* switch thread credentials -- see kern_ktrace.c */
452 td->td_ucred = ownercred;
453 error = fo_write(po->po_file, &auio, ownercred, 0, td);
454 td->td_ucred = mycred;
457 /* XXX some errors are recoverable */
458 /* send a SIGIO to the owner and exit */
460 kern_psignal(p, SIGIO);
463 mtx_lock(&pmc_kthread_mtx);
465 po->po_error = error; /* save for flush log */
467 PMCDBG2(LOG,WRI,2, "po=%p error=%d", po, error);
472 mtx_lock(&pmc_kthread_mtx);
474 /* put the used buffer back into the global pool */
475 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
481 wakeup_one(po->po_kthread);
482 po->po_kthread = NULL;
484 mtx_unlock(&pmc_kthread_mtx);
486 /* return the current I/O buffer to the global pool */
488 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
494 * Exit this thread, signalling the waiter
503 * Release and log entry and schedule an I/O if needed.
507 pmclog_release(struct pmc_owner *po)
509 struct pmclog_buffer *plb;
511 plb = po->po_curbuf[curcpu];
512 KASSERT(plb->plb_ptr >= plb->plb_base,
513 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
514 po, plb->plb_ptr, plb->plb_base));
515 KASSERT(plb->plb_ptr <= plb->plb_fence,
516 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
517 po, plb->plb_ptr, plb->plb_fence));
519 /* schedule an I/O if we've filled a buffer */
520 if (plb->plb_ptr >= plb->plb_fence)
521 pmclog_schedule_io(po);
523 PMCDBG1(LOG,REL,1, "po=%p", po);
528 * Attempt to reserve 'length' bytes of space in an owner's log
529 * buffer. The function returns a pointer to 'length' bytes of space
530 * if there was enough space or returns NULL if no space was
531 * available. Non-null returns do so with the po mutex locked. The
532 * caller must invoke pmclog_release() on the pmc owner structure
537 pmclog_reserve(struct pmc_owner *po, int length)
539 uintptr_t newptr, oldptr;
542 struct pmclog_buffer *plb, **pplb;
544 PMCDBG2(LOG,ALL,1, "po=%p len=%d", po, length);
546 KASSERT(length % sizeof(uint32_t) == 0,
547 ("[pmclog,%d] length not a multiple of word size", __LINE__));
549 /* No more data when shutdown in progress. */
550 if (po->po_flags & PMC_PO_SHUTDOWN)
553 pplb = &po->po_curbuf[curcpu];
554 if (*pplb == NULL && pmclog_get_buffer(po) != 0)
557 KASSERT(*pplb != NULL,
558 ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
561 KASSERT(plb->plb_ptr >= plb->plb_base &&
562 plb->plb_ptr <= plb->plb_fence,
563 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
564 __LINE__, po, plb->plb_ptr, plb->plb_base,
567 oldptr = (uintptr_t) plb->plb_ptr;
568 newptr = oldptr + length;
570 KASSERT(oldptr != (uintptr_t) NULL,
571 ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
574 * If we have space in the current buffer, return a pointer to
575 * available space with the PO structure locked.
577 if (newptr <= (uintptr_t) plb->plb_fence) {
578 plb->plb_ptr = (char *) newptr;
583 * Otherwise, schedule the current buffer for output and get a
586 pmclog_schedule_io(po);
588 if (pmclog_get_buffer(po) != 0)
593 ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
595 KASSERT(plb->plb_ptr != NULL,
596 ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
598 KASSERT(plb->plb_ptr == plb->plb_base &&
599 plb->plb_ptr <= plb->plb_fence,
600 ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
601 __LINE__, po, plb->plb_ptr, plb->plb_base,
604 oldptr = (uintptr_t) plb->plb_ptr;
607 lh = (uint32_t *) oldptr;
608 lh++; /* skip header */
609 getnanotime(&ts); /* fill in the timestamp */
610 *lh++ = ts.tv_sec & 0xFFFFFFFF;
611 *lh++ = ts.tv_nsec & 0xFFFFFFF;
612 return ((uint32_t *) oldptr);
620 * Transfer the current buffer to the helper kthread.
624 pmclog_schedule_io(struct pmc_owner *po)
626 struct pmclog_buffer *plb;
628 plb = po->po_curbuf[curcpu];
629 po->po_curbuf[curcpu] = NULL;
631 ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
632 KASSERT(plb->plb_ptr >= plb->plb_base,
633 ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
634 po, plb->plb_ptr, plb->plb_base));
635 KASSERT(plb->plb_ptr <= plb->plb_fence,
636 ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
637 po, plb->plb_ptr, plb->plb_fence));
639 PMCDBG1(LOG,SIO, 1, "po=%p", po);
642 * Add the current buffer to the tail of the buffer list and
645 mtx_lock_spin(&po->po_mtx);
646 TAILQ_INSERT_TAIL(&po->po_logbuffers, plb, plb_next);
647 mtx_unlock_spin(&po->po_mtx);
652 * Stop the helper kthread.
656 pmclog_stop_kthread(struct pmc_owner *po)
659 mtx_lock(&pmc_kthread_mtx);
660 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
661 if (po->po_kthread != NULL) {
662 PROC_LOCK(po->po_kthread);
663 kern_psignal(po->po_kthread, SIGHUP);
664 PROC_UNLOCK(po->po_kthread);
667 while (po->po_kthread)
668 msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
669 mtx_unlock(&pmc_kthread_mtx);
677 * Configure a log file for pmc owner 'po'.
679 * Parameter 'logfd' is a file handle referencing an open file in the
680 * owner process. This file needs to have been opened for writing.
684 pmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
689 sx_assert(&pmc_sx, SA_XLOCKED);
690 PMCDBG2(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
694 /* return EBUSY if a log file was already present */
695 if (po->po_flags & PMC_PO_OWNS_LOGFILE)
698 KASSERT(po->po_file == NULL,
699 ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
702 /* get a reference to the file state */
703 error = fget_write(curthread, logfd, &cap_write_rights, &po->po_file);
707 /* mark process as owning a log file */
708 po->po_flags |= PMC_PO_OWNS_LOGFILE;
710 /* mark process as using HWPMCs */
712 p->p_flag |= P_HWPMC;
715 /* create a log initialization entry */
716 PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
717 sizeof(struct pmclog_initialize));
718 PMCLOG_EMIT32(PMC_VERSION);
719 PMCLOG_EMIT32(md->pmd_cputype);
720 PMCLOG_DESPATCH_SYNC(po);
725 KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
726 "stopped", __LINE__, po));
729 (void) fdrop(po->po_file, curthread);
730 po->po_file = NULL; /* clear file and error state */
732 po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
739 * De-configure a log file. This will throw away any buffers queued
740 * for this owner process.
744 pmclog_deconfigure_log(struct pmc_owner *po)
747 struct pmclog_buffer *lb;
749 PMCDBG1(LOG,CFG,1, "de-config po=%p", po);
751 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
754 KASSERT(po->po_sscount == 0,
755 ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
756 KASSERT(po->po_file != NULL,
757 ("[pmclog,%d] po=%p no log file", __LINE__, po));
759 /* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
760 pmclog_stop_kthread(po);
762 KASSERT(po->po_kthread == NULL,
763 ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
765 /* return all queued log buffers to the global pool */
766 while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
767 TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
768 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
771 for (int i = 0; i < mp_ncpus; i++) {
772 thread_lock(curthread);
773 sched_bind(curthread, i);
774 thread_unlock(curthread);
775 /* return the 'current' buffer to the global pool */
776 if ((lb = po->po_curbuf[curcpu]) != NULL) {
777 PMCLOG_RESET_BUFFER_DESCRIPTOR(lb);
781 thread_lock(curthread);
782 sched_unbind(curthread);
783 thread_unlock(curthread);
785 /* drop a reference to the fd */
786 if (po->po_file != NULL) {
787 error = fdrop(po->po_file, curthread);
797 * Flush a process' log buffer.
801 pmclog_flush(struct pmc_owner *po)
805 PMCDBG1(LOG,FLS,1, "po=%p", po);
808 * If there is a pending error recorded by the logger thread,
812 return (po->po_error);
817 * Check that we do have an active log file.
819 mtx_lock(&pmc_kthread_mtx);
820 if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
825 pmclog_schedule_all(po);
827 mtx_unlock(&pmc_kthread_mtx);
833 pmclog_schedule_one_cond(void *arg)
835 struct pmc_owner *po = arg;
836 struct pmclog_buffer *plb;
839 /* tell hardclock not to run again */
840 if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
841 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
842 plb = po->po_curbuf[curcpu];
843 if (plb && plb->plb_ptr != plb->plb_base)
844 pmclog_schedule_io(po);
849 pmclog_schedule_all(struct pmc_owner *po)
852 * Schedule the current buffer if any and not empty.
854 for (int i = 0; i < mp_ncpus; i++) {
855 thread_lock(curthread);
856 sched_bind(curthread, i);
857 thread_unlock(curthread);
858 pmclog_schedule_one_cond(po);
860 thread_lock(curthread);
861 sched_unbind(curthread);
862 thread_unlock(curthread);
866 pmclog_close(struct pmc_owner *po)
869 PMCDBG1(LOG,CLO,1, "po=%p", po);
871 pmclog_process_closelog(po);
873 mtx_lock(&pmc_kthread_mtx);
876 * Schedule the current buffer.
878 pmclog_schedule_all(po);
882 * Initiate shutdown: no new data queued,
883 * thread will close file on last block.
885 po->po_flags |= PMC_PO_SHUTDOWN;
886 mtx_unlock(&pmc_kthread_mtx);
892 pmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
896 struct pmc_owner *po;
898 PMCDBG3(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
901 recordlen = offsetof(struct pmclog_callchain, pl_pc) +
902 ps->ps_nsamples * sizeof(uintfptr_t);
904 flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
905 PMCLOG_RESERVE_SAFE(po, CALLCHAIN, recordlen);
906 PMCLOG_EMIT32(ps->ps_pid);
907 PMCLOG_EMIT32(pm->pm_id);
908 PMCLOG_EMIT32(flags);
909 for (n = 0; n < ps->ps_nsamples; n++)
910 PMCLOG_EMITADDR(ps->ps_pc[n]);
911 PMCLOG_DESPATCH_SAFE(po);
915 pmclog_process_closelog(struct pmc_owner *po)
917 PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
918 PMCLOG_DESPATCH_SYNC(po);
922 pmclog_process_dropnotify(struct pmc_owner *po)
924 PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
929 pmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
932 int pathlen, recordlen;
934 KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
936 pathlen = strlen(path) + 1; /* #bytes for path name */
937 recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
940 PMCLOG_RESERVE(po, MAP_IN, recordlen);
942 PMCLOG_EMITADDR(start);
943 PMCLOG_EMITSTRING(path,pathlen);
948 pmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
951 KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
953 PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
955 PMCLOG_EMITADDR(start);
956 PMCLOG_EMITADDR(end);
961 pmclog_process_pmcallocate(struct pmc *pm)
963 struct pmc_owner *po;
968 PMCDBG1(LOG,ALL,1, "pm=%p", pm);
970 if (PMC_TO_CLASS(pm) == PMC_CLASS_SOFT) {
971 PMCLOG_RESERVE(po, PMCALLOCATEDYN,
972 sizeof(struct pmclog_pmcallocatedyn));
973 PMCLOG_EMIT32(pm->pm_id);
974 PMCLOG_EMIT32(pm->pm_event);
975 PMCLOG_EMIT32(pm->pm_flags);
976 ps = pmc_soft_ev_acquire(pm->pm_event);
978 PMCLOG_EMITSTRING(ps->ps_ev.pm_ev_name,PMC_NAME_MAX);
980 PMCLOG_EMITNULLSTRING(PMC_NAME_MAX);
981 pmc_soft_ev_release(ps);
982 PMCLOG_DESPATCH_SYNC(po);
984 PMCLOG_RESERVE(po, PMCALLOCATE,
985 sizeof(struct pmclog_pmcallocate));
986 PMCLOG_EMIT32(pm->pm_id);
987 PMCLOG_EMIT32(pm->pm_event);
988 PMCLOG_EMIT32(pm->pm_flags);
989 PMCLOG_DESPATCH_SYNC(po);
994 pmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
996 int pathlen, recordlen;
997 struct pmc_owner *po;
999 PMCDBG2(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
1003 pathlen = strlen(path) + 1; /* #bytes for the string */
1004 recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
1006 PMCLOG_RESERVE(po, PMCATTACH, recordlen);
1007 PMCLOG_EMIT32(pm->pm_id);
1009 PMCLOG_EMITSTRING(path, pathlen);
1010 PMCLOG_DESPATCH_SYNC(po);
1014 pmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
1016 struct pmc_owner *po;
1018 PMCDBG2(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
1022 PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
1023 PMCLOG_EMIT32(pm->pm_id);
1025 PMCLOG_DESPATCH_SYNC(po);
1029 * Log a context switch event to the log file.
1033 pmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
1035 struct pmc_owner *po;
1037 KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
1038 ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
1040 PMCDBG3(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
1045 PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
1046 PMCLOG_EMIT32(pm->pm_id);
1048 PMCLOG_EMIT32(pp->pp_proc->p_pid);
1049 PMCLOG_DESPATCH(po);
1053 pmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
1054 uintfptr_t startaddr, char *path)
1056 int pathlen, recordlen;
1058 PMCDBG3(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
1060 pathlen = strlen(path) + 1; /* #bytes for the path */
1061 recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
1063 PMCLOG_RESERVE(po, PROCEXEC, recordlen);
1065 PMCLOG_EMITADDR(startaddr);
1066 PMCLOG_EMIT32(pmid);
1067 PMCLOG_EMITSTRING(path,pathlen);
1068 PMCLOG_DESPATCH(po);
1072 * Log a process exit event (and accumulated pmc value) to the log file.
1076 pmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
1079 struct pmc_owner *po;
1081 ri = PMC_TO_ROWINDEX(pm);
1082 PMCDBG3(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
1083 pp->pp_pmcs[ri].pp_pmcval);
1087 PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
1088 PMCLOG_EMIT32(pm->pm_id);
1089 PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
1090 PMCLOG_EMIT32(pp->pp_proc->p_pid);
1091 PMCLOG_DESPATCH(po);
1099 pmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
1101 PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
1102 PMCLOG_EMIT32(oldpid);
1103 PMCLOG_EMIT32(newpid);
1104 PMCLOG_DESPATCH(po);
1108 * Log a process exit event of the form suitable for system-wide PMCs.
1112 pmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
1114 PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
1116 PMCLOG_DESPATCH(po);
1120 * Write a user log entry.
1124 pmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
1128 PMCDBG2(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
1132 PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
1133 sizeof(struct pmclog_userdata));
1134 PMCLOG_EMIT32(wl->pm_userdata);
1135 PMCLOG_DESPATCH(po);
1144 * Create a pool of log buffers and initialize mutexes.
1152 struct pmclog_buffer *plb;
1154 if (pmclog_buffer_size <= 0 || pmclog_buffer_size > 16*1024) {
1155 (void) printf("hwpmc: tunable logbuffersize=%d must be "
1156 "greater than zero and less than or equal to 16MB.\n",
1157 pmclog_buffer_size);
1158 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1161 if (pmc_nlogbuffers_pcpu <= 0) {
1162 (void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
1163 "than zero.\n", pmc_nlogbuffers_pcpu);
1164 pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
1167 if (pmc_nlogbuffers_pcpu*pmclog_buffer_size > 32*1024) {
1168 (void) printf("hwpmc: memory allocated pcpu must be less than 32MB (is %dK).\n",
1169 pmc_nlogbuffers_pcpu*pmclog_buffer_size);
1170 pmc_nlogbuffers_pcpu = PMC_NLOGBUFFERS_PCPU;
1171 pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1173 for (domain = 0; domain < vm_ndomains; domain++) {
1174 pmc_dom_hdrs[domain] = malloc_domain(sizeof(struct pmc_domain_buffer_header), M_PMC, domain,
1176 mtx_init(&pmc_dom_hdrs[domain]->pdbh_mtx, "pmc_bufferlist_mtx", "pmc-leaf", MTX_SPIN);
1177 TAILQ_INIT(&pmc_dom_hdrs[domain]->pdbh_head);
1180 if (CPU_ABSENT(cpu))
1182 pc = pcpu_find(cpu);
1183 domain = pc->pc_domain;
1184 pmc_dom_hdrs[domain]->pdbh_ncpus++;
1186 for (domain = 0; domain < vm_ndomains; domain++) {
1187 int ncpus = pmc_dom_hdrs[domain]->pdbh_ncpus;
1188 int total = ncpus*pmc_nlogbuffers_pcpu;
1190 plb = malloc_domain(sizeof(struct pmclog_buffer)*total, M_PMC, domain, M_WAITOK|M_ZERO);
1191 pmc_dom_hdrs[domain]->pdbh_plbs = plb;
1192 for (int i = 0; i < total; i++, plb++) {
1195 buf = malloc_domain(1024 * pmclog_buffer_size, M_PMC, domain,
1197 PMCLOG_INIT_BUFFER_DESCRIPTOR(plb, buf, domain);
1198 pmc_plb_rele_unlocked(plb);
1201 mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
1207 * Destroy mutexes and release memory back the to free pool.
1213 struct pmclog_buffer *plb;
1216 mtx_destroy(&pmc_kthread_mtx);
1218 for (domain = 0; domain < vm_ndomains; domain++) {
1219 mtx_destroy(&pmc_dom_hdrs[domain]->pdbh_mtx);
1220 while ((plb = TAILQ_FIRST(&pmc_dom_hdrs[domain]->pdbh_head)) != NULL) {
1221 TAILQ_REMOVE(&pmc_dom_hdrs[domain]->pdbh_head, plb, plb_next);
1222 free(plb->plb_base, M_PMC);
1224 free(pmc_dom_hdrs[domain]->pdbh_plbs, M_PMC);
1225 free(pmc_dom_hdrs[domain], M_PMC);