1147191Sjkoshy/*-
2174395Sjkoshy * Copyright (c) 2005-2007 Joseph Koshy
3174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation
4147191Sjkoshy * All rights reserved.
5147191Sjkoshy *
6174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under
7174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc.
8174395Sjkoshy *
9147191Sjkoshy * Redistribution and use in source and binary forms, with or without
10147191Sjkoshy * modification, are permitted provided that the following conditions
11147191Sjkoshy * are met:
12147191Sjkoshy * 1. Redistributions of source code must retain the above copyright
13147191Sjkoshy *    notice, this list of conditions and the following disclaimer.
14147191Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
15147191Sjkoshy *    notice, this list of conditions and the following disclaimer in the
16147191Sjkoshy *    documentation and/or other materials provided with the distribution.
17147191Sjkoshy *
18147191Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19147191Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20147191Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21147191Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22147191Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23147191Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24147191Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25147191Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26147191Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27147191Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28147191Sjkoshy * SUCH DAMAGE.
29147191Sjkoshy *
30147191Sjkoshy */
31147191Sjkoshy
32147191Sjkoshy/*
33147191Sjkoshy * Logging code for hwpmc(4)
34147191Sjkoshy */
35147191Sjkoshy
36147191Sjkoshy#include <sys/cdefs.h>
37147191Sjkoshy__FBSDID("$FreeBSD: releng/11.0/sys/dev/hwpmc/hwpmc_logging.c 292033 2015-12-09 22:46:40Z rrs $");
38147191Sjkoshy
39147191Sjkoshy#include <sys/param.h>
40277177Srrs#if (__FreeBSD_version >= 1100000)
41263233Srwatson#include <sys/capsicum.h>
42279894Srrs#else
43279894Srrs#include <sys/capability.h>
44277177Srrs#endif
45147191Sjkoshy#include <sys/file.h>
46147191Sjkoshy#include <sys/kernel.h>
47147191Sjkoshy#include <sys/kthread.h>
48147191Sjkoshy#include <sys/lock.h>
49147191Sjkoshy#include <sys/module.h>
50147191Sjkoshy#include <sys/mutex.h>
51147191Sjkoshy#include <sys/pmc.h>
52195005Sattilio#include <sys/pmckern.h>
53147191Sjkoshy#include <sys/pmclog.h>
54147191Sjkoshy#include <sys/proc.h>
55147191Sjkoshy#include <sys/signalvar.h>
56147191Sjkoshy#include <sys/sysctl.h>
57147191Sjkoshy#include <sys/systm.h>
58147191Sjkoshy#include <sys/uio.h>
59147191Sjkoshy#include <sys/unistd.h>
60147191Sjkoshy#include <sys/vnode.h>
61147191Sjkoshy
62147191Sjkoshy/*
63147191Sjkoshy * Sysctl tunables
64147191Sjkoshy */
65147191Sjkoshy
66147191SjkoshySYSCTL_DECL(_kern_hwpmc);
67147191Sjkoshy
68147191Sjkoshy/*
69147191Sjkoshy * kern.hwpmc.logbuffersize -- size of the per-cpu owner buffers.
70147191Sjkoshy */
71147191Sjkoshy
72147191Sjkoshystatic int pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
73292033Srrs#if (__FreeBSD_version < 1100000)
74292033SrrsTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "logbuffersize", &pmclog_buffer_size);
75292033Srrs#endif
76267992ShselaskySYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RDTUN,
77147191Sjkoshy    &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
78147191Sjkoshy
79147191Sjkoshy/*
80147191Sjkoshy * kern.hwpmc.nbuffer -- number of global log buffers
81147191Sjkoshy */
82147191Sjkoshy
83147191Sjkoshystatic int pmc_nlogbuffers = PMC_NLOGBUFFERS;
84292033Srrs#if (__FreeBSD_version < 1100000)
85292033SrrsTUNABLE_INT(PMC_SYSCTL_NAME_PREFIX "nbuffers", &pmc_nlogbuffers);
86292033Srrs#endif
87267992ShselaskySYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers, CTLFLAG_RDTUN,
88147191Sjkoshy    &pmc_nlogbuffers, 0, "number of global log buffers");
89147191Sjkoshy
90147191Sjkoshy/*
91147191Sjkoshy * Global log buffer list and associated spin lock.
92147191Sjkoshy */
93147191Sjkoshy
94147191SjkoshyTAILQ_HEAD(, pmclog_buffer) pmc_bufferlist =
95147191Sjkoshy	TAILQ_HEAD_INITIALIZER(pmc_bufferlist);
96147191Sjkoshystatic struct mtx pmc_bufferlist_mtx;	/* spin lock */
97147191Sjkoshystatic struct mtx pmc_kthread_mtx;	/* sleep lock */
98147191Sjkoshy
99147191Sjkoshy#define	PMCLOG_INIT_BUFFER_DESCRIPTOR(D) do {				\
100147191Sjkoshy		const int __roundup = roundup(sizeof(*D),		\
101147191Sjkoshy			sizeof(uint32_t));				\
102147191Sjkoshy		(D)->plb_fence = ((char *) (D)) +			\
103147191Sjkoshy			 1024*pmclog_buffer_size;			\
104147191Sjkoshy		(D)->plb_base  = (D)->plb_ptr = ((char *) (D)) +	\
105147191Sjkoshy			__roundup;					\
106147191Sjkoshy	} while (0)
107147191Sjkoshy
108147191Sjkoshy
109147191Sjkoshy/*
110147191Sjkoshy * Log file record constructors.
111147191Sjkoshy */
112147867Sjkoshy#define	_PMCLOG_TO_HEADER(T,L)						\
113147867Sjkoshy	((PMCLOG_HEADER_MAGIC << 24) |					\
114147867Sjkoshy	 (PMCLOG_TYPE_ ## T << 16)   |					\
115147867Sjkoshy	 ((L) & 0xFFFF))
116147867Sjkoshy
117147191Sjkoshy/* reserve LEN bytes of space and initialize the entry header */
118147191Sjkoshy#define	_PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do {			\
119147191Sjkoshy		uint32_t *_le;						\
120147191Sjkoshy		int _len = roundup((LEN), sizeof(uint32_t));		\
121147191Sjkoshy		if ((_le = pmclog_reserve((PO), _len)) == NULL) {	\
122147191Sjkoshy			ACTION;						\
123147191Sjkoshy		}							\
124147867Sjkoshy		*_le = _PMCLOG_TO_HEADER(TYPE,_len);			\
125147191Sjkoshy		_le += 3	/* skip over timestamp */
126147191Sjkoshy
127147191Sjkoshy#define	PMCLOG_RESERVE(P,T,L)		_PMCLOG_RESERVE(P,T,L,return)
128147191Sjkoshy#define	PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L,		\
129147191Sjkoshy	error=ENOMEM;goto error)
130147191Sjkoshy
131147191Sjkoshy#define	PMCLOG_EMIT32(V)	do { *_le++ = (V); } while (0)
132147191Sjkoshy#define	PMCLOG_EMIT64(V)	do { 					\
133147191Sjkoshy		*_le++ = (uint32_t) ((V) & 0xFFFFFFFF);			\
134147191Sjkoshy		*_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF);		\
135147191Sjkoshy	} while (0)
136147191Sjkoshy
137147191Sjkoshy
138147191Sjkoshy/* Emit a string.  Caution: does NOT update _le, so needs to be last */
139147191Sjkoshy#define	PMCLOG_EMITSTRING(S,L)	do { bcopy((S), _le, (L)); } while (0)
140233628Sfabient#define	PMCLOG_EMITNULLSTRING(L) do { bzero(_le, (L)); } while (0)
141147191Sjkoshy
142147191Sjkoshy#define	PMCLOG_DESPATCH(PO)						\
143147191Sjkoshy		pmclog_release((PO));					\
144147191Sjkoshy	} while (0)
145147191Sjkoshy
146147191Sjkoshy
147147191Sjkoshy/*
148147191Sjkoshy * Assertions about the log file format.
149147191Sjkoshy */
150147191Sjkoshy
151174395SjkoshyCTASSERT(sizeof(struct pmclog_callchain) == 6*4 +
152174395Sjkoshy    PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t));
153147191SjkoshyCTASSERT(sizeof(struct pmclog_closelog) == 3*4);
154147191SjkoshyCTASSERT(sizeof(struct pmclog_dropnotify) == 3*4);
155157144SjkoshyCTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX +
156157144Sjkoshy    4*4 + sizeof(uintfptr_t));
157157144SjkoshyCTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
158157144Sjkoshy    4*4 + sizeof(uintfptr_t));
159157144SjkoshyCTASSERT(sizeof(struct pmclog_map_out) == 4*4 + 2*sizeof(uintfptr_t));
160147708SjkoshyCTASSERT(sizeof(struct pmclog_pcsample) == 6*4 + sizeof(uintfptr_t));
161147191SjkoshyCTASSERT(sizeof(struct pmclog_pmcallocate) == 6*4);
162147191SjkoshyCTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX);
163147191SjkoshyCTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4);
164147191SjkoshyCTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4);
165147191SjkoshyCTASSERT(sizeof(struct pmclog_proccsw) == 5*4 + 8);
166147708SjkoshyCTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
167147708Sjkoshy    sizeof(uintfptr_t));
168147708SjkoshyCTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
169147708Sjkoshy    sizeof(uintfptr_t));
170147191SjkoshyCTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8);
171147191SjkoshyCTASSERT(sizeof(struct pmclog_procfork) == 5*4);
172147191SjkoshyCTASSERT(sizeof(struct pmclog_sysexit) == 4*4);
173147191SjkoshyCTASSERT(sizeof(struct pmclog_userdata) == 4*4);
174147191Sjkoshy
175147191Sjkoshy/*
176147191Sjkoshy * Log buffer structure
177147191Sjkoshy */
178147191Sjkoshy
179147191Sjkoshystruct pmclog_buffer {
180147191Sjkoshy	TAILQ_ENTRY(pmclog_buffer) plb_next;
181147191Sjkoshy	char 		*plb_base;
182147191Sjkoshy	char		*plb_ptr;
183147191Sjkoshy	char 		*plb_fence;
184147191Sjkoshy};
185147191Sjkoshy
186147191Sjkoshy/*
187147191Sjkoshy * Prototypes
188147191Sjkoshy */
189147191Sjkoshy
190147191Sjkoshystatic int pmclog_get_buffer(struct pmc_owner *po);
191147191Sjkoshystatic void pmclog_loop(void *arg);
192147191Sjkoshystatic void pmclog_release(struct pmc_owner *po);
193147191Sjkoshystatic uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
194147191Sjkoshystatic void pmclog_schedule_io(struct pmc_owner *po);
195147191Sjkoshystatic void pmclog_stop_kthread(struct pmc_owner *po);
196147191Sjkoshy
197147191Sjkoshy/*
198147191Sjkoshy * Helper functions
199147191Sjkoshy */
200147191Sjkoshy
201147191Sjkoshy/*
202147191Sjkoshy * Get a log buffer
203147191Sjkoshy */
204147191Sjkoshy
205147191Sjkoshystatic int
206147191Sjkoshypmclog_get_buffer(struct pmc_owner *po)
207147191Sjkoshy{
208147191Sjkoshy	struct pmclog_buffer *plb;
209147191Sjkoshy
210147191Sjkoshy	mtx_assert(&po->po_mtx, MA_OWNED);
211147191Sjkoshy
212147191Sjkoshy	KASSERT(po->po_curbuf == NULL,
213186127Sjkoshy	    ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
214147191Sjkoshy
215147191Sjkoshy	mtx_lock_spin(&pmc_bufferlist_mtx);
216147191Sjkoshy	if ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL)
217147191Sjkoshy		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
218147191Sjkoshy	mtx_unlock_spin(&pmc_bufferlist_mtx);
219147191Sjkoshy
220282658Sjhb	PMCDBG2(LOG,GTB,1, "po=%p plb=%p", po, plb);
221147191Sjkoshy
222282641Sjhb#ifdef	HWPMC_DEBUG
223147191Sjkoshy	if (plb)
224147191Sjkoshy		KASSERT(plb->plb_ptr == plb->plb_base &&
225147191Sjkoshy		    plb->plb_base < plb->plb_fence,
226186127Sjkoshy		    ("[pmclog,%d] po=%p buffer invariants: ptr=%p "
227147191Sjkoshy		    "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
228147191Sjkoshy		    plb->plb_base, plb->plb_fence));
229147191Sjkoshy#endif
230147191Sjkoshy
231147191Sjkoshy	po->po_curbuf = plb;
232147191Sjkoshy
233147191Sjkoshy	/* update stats */
234147191Sjkoshy	atomic_add_int(&pmc_stats.pm_buffer_requests, 1);
235147191Sjkoshy	if (plb == NULL)
236147191Sjkoshy		atomic_add_int(&pmc_stats.pm_buffer_requests_failed, 1);
237147191Sjkoshy
238186127Sjkoshy	return (plb ? 0 : ENOMEM);
239147191Sjkoshy}
240147191Sjkoshy
241147191Sjkoshy/*
242147191Sjkoshy * Log handler loop.
243147191Sjkoshy *
244147191Sjkoshy * This function is executed by each pmc owner's helper thread.
245147191Sjkoshy */
246147191Sjkoshy
247147191Sjkoshystatic void
248147191Sjkoshypmclog_loop(void *arg)
249147191Sjkoshy{
250226514Sfabient	int error;
251147191Sjkoshy	struct pmc_owner *po;
252147191Sjkoshy	struct pmclog_buffer *lb;
253199763Sfabient	struct proc *p;
254147191Sjkoshy	struct ucred *ownercred;
255147191Sjkoshy	struct ucred *mycred;
256147191Sjkoshy	struct thread *td;
257147191Sjkoshy	struct uio auio;
258147191Sjkoshy	struct iovec aiov;
259147191Sjkoshy	size_t nbytes;
260147191Sjkoshy
261147191Sjkoshy	po = (struct pmc_owner *) arg;
262199763Sfabient	p = po->po_owner;
263147191Sjkoshy	td = curthread;
264147191Sjkoshy	mycred = td->td_ucred;
265147191Sjkoshy
266199763Sfabient	PROC_LOCK(p);
267199763Sfabient	ownercred = crhold(p->p_ucred);
268199763Sfabient	PROC_UNLOCK(p);
269147191Sjkoshy
270282658Sjhb	PMCDBG2(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
271147191Sjkoshy	KASSERT(po->po_kthread == curthread->td_proc,
272186127Sjkoshy	    ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
273147191Sjkoshy		po, po->po_kthread, curthread->td_proc));
274147191Sjkoshy
275147191Sjkoshy	lb = NULL;
276147191Sjkoshy
277147191Sjkoshy
278147191Sjkoshy	/*
279147191Sjkoshy	 * Loop waiting for I/O requests to be added to the owner
280147191Sjkoshy	 * struct's queue.  The loop is exited when the log file
281147191Sjkoshy	 * is deconfigured.
282147191Sjkoshy	 */
283147191Sjkoshy
284147191Sjkoshy	mtx_lock(&pmc_kthread_mtx);
285147191Sjkoshy
286147191Sjkoshy	for (;;) {
287147191Sjkoshy
288147191Sjkoshy		/* check if we've been asked to exit */
289147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
290147191Sjkoshy			break;
291147191Sjkoshy
292147191Sjkoshy		if (lb == NULL) { /* look for a fresh buffer to write */
293147191Sjkoshy			mtx_lock_spin(&po->po_mtx);
294147191Sjkoshy			if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
295147191Sjkoshy				mtx_unlock_spin(&po->po_mtx);
296147191Sjkoshy
297233544Sfabient				/* No more buffers and shutdown required. */
298226514Sfabient				if (po->po_flags & PMC_PO_SHUTDOWN) {
299226514Sfabient					mtx_unlock(&pmc_kthread_mtx);
300226514Sfabient					/*
301226514Sfabient			 		 * Close the file to get PMCLOG_EOF
302226514Sfabient					 * error in pmclog(3).
303226514Sfabient					 */
304226514Sfabient					fo_close(po->po_file, curthread);
305226514Sfabient					mtx_lock(&pmc_kthread_mtx);
306233544Sfabient					break;
307226514Sfabient				}
308226514Sfabient
309185465Sjkoshy				(void) msleep(po, &pmc_kthread_mtx, PWAIT,
310185465Sjkoshy				    "pmcloop", 0);
311147191Sjkoshy				continue;
312147191Sjkoshy			}
313147191Sjkoshy
314147191Sjkoshy			TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
315147191Sjkoshy			mtx_unlock_spin(&po->po_mtx);
316147191Sjkoshy		}
317147191Sjkoshy
318147191Sjkoshy		mtx_unlock(&pmc_kthread_mtx);
319147191Sjkoshy
320147191Sjkoshy		/* process the request */
321282658Sjhb		PMCDBG3(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
322147191Sjkoshy		    lb->plb_base, lb->plb_ptr);
323147191Sjkoshy		/* change our thread's credentials before issuing the I/O */
324147191Sjkoshy
325147191Sjkoshy		aiov.iov_base = lb->plb_base;
326147191Sjkoshy		aiov.iov_len  = nbytes = lb->plb_ptr - lb->plb_base;
327147191Sjkoshy
328147191Sjkoshy		auio.uio_iov    = &aiov;
329147191Sjkoshy		auio.uio_iovcnt = 1;
330147191Sjkoshy		auio.uio_offset = -1;
331147191Sjkoshy		auio.uio_resid  = nbytes;
332147191Sjkoshy		auio.uio_rw     = UIO_WRITE;
333147191Sjkoshy		auio.uio_segflg = UIO_SYSSPACE;
334147191Sjkoshy		auio.uio_td     = td;
335147191Sjkoshy
336147191Sjkoshy		/* switch thread credentials -- see kern_ktrace.c */
337147191Sjkoshy		td->td_ucred = ownercred;
338147191Sjkoshy		error = fo_write(po->po_file, &auio, ownercred, 0, td);
339147191Sjkoshy		td->td_ucred = mycred;
340147191Sjkoshy
341147191Sjkoshy		if (error) {
342147191Sjkoshy			/* XXX some errors are recoverable */
343147191Sjkoshy			/* send a SIGIO to the owner and exit */
344199763Sfabient			PROC_LOCK(p);
345225617Skmacy			kern_psignal(p, SIGIO);
346199763Sfabient			PROC_UNLOCK(p);
347147191Sjkoshy
348199763Sfabient			mtx_lock(&pmc_kthread_mtx);
349199763Sfabient
350147191Sjkoshy			po->po_error = error; /* save for flush log */
351147191Sjkoshy
352282658Sjhb			PMCDBG2(LOG,WRI,2, "po=%p error=%d", po, error);
353147191Sjkoshy
354147191Sjkoshy			break;
355147191Sjkoshy		}
356147191Sjkoshy
357199763Sfabient		mtx_lock(&pmc_kthread_mtx);
358199763Sfabient
359147191Sjkoshy		/* put the used buffer back into the global pool */
360147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
361147191Sjkoshy
362147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
363147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
364147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
365147191Sjkoshy
366147191Sjkoshy		lb = NULL;
367147191Sjkoshy	}
368147191Sjkoshy
369233544Sfabient	wakeup_one(po->po_kthread);
370147191Sjkoshy	po->po_kthread = NULL;
371147191Sjkoshy
372147191Sjkoshy	mtx_unlock(&pmc_kthread_mtx);
373147191Sjkoshy
374147191Sjkoshy	/* return the current I/O buffer to the global pool */
375147191Sjkoshy	if (lb) {
376147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
377147191Sjkoshy
378147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
379147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
380147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
381147191Sjkoshy	}
382147191Sjkoshy
383147191Sjkoshy	/*
384147191Sjkoshy	 * Exit this thread, signalling the waiter
385147191Sjkoshy	 */
386147191Sjkoshy
387147191Sjkoshy	crfree(ownercred);
388147191Sjkoshy
389172836Sjulian	kproc_exit(0);
390147191Sjkoshy}
391147191Sjkoshy
392147191Sjkoshy/*
393147191Sjkoshy * Release and log entry and schedule an I/O if needed.
394147191Sjkoshy */
395147191Sjkoshy
396147191Sjkoshystatic void
397147191Sjkoshypmclog_release(struct pmc_owner *po)
398147191Sjkoshy{
399147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
400186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
401147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
402147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
403186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
404147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
405147191Sjkoshy
406147191Sjkoshy	/* schedule an I/O if we've filled a buffer */
407147191Sjkoshy	if (po->po_curbuf->plb_ptr >= po->po_curbuf->plb_fence)
408147191Sjkoshy		pmclog_schedule_io(po);
409147191Sjkoshy
410147191Sjkoshy	mtx_unlock_spin(&po->po_mtx);
411147191Sjkoshy
412282658Sjhb	PMCDBG1(LOG,REL,1, "po=%p", po);
413147191Sjkoshy}
414147191Sjkoshy
415147191Sjkoshy
416147191Sjkoshy/*
417147191Sjkoshy * Attempt to reserve 'length' bytes of space in an owner's log
418147191Sjkoshy * buffer.  The function returns a pointer to 'length' bytes of space
419147191Sjkoshy * if there was enough space or returns NULL if no space was
420147191Sjkoshy * available.  Non-null returns do so with the po mutex locked.  The
421147191Sjkoshy * caller must invoke pmclog_release() on the pmc owner structure
422147191Sjkoshy * when done.
423147191Sjkoshy */
424147191Sjkoshy
425147191Sjkoshystatic uint32_t *
426147191Sjkoshypmclog_reserve(struct pmc_owner *po, int length)
427147191Sjkoshy{
428147867Sjkoshy	uintptr_t newptr, oldptr;
429147191Sjkoshy	uint32_t *lh;
430147191Sjkoshy	struct timespec ts;
431147191Sjkoshy
432282658Sjhb	PMCDBG2(LOG,ALL,1, "po=%p len=%d", po, length);
433147191Sjkoshy
434147191Sjkoshy	KASSERT(length % sizeof(uint32_t) == 0,
435147191Sjkoshy	    ("[pmclog,%d] length not a multiple of word size", __LINE__));
436147191Sjkoshy
437147191Sjkoshy	mtx_lock_spin(&po->po_mtx);
438147191Sjkoshy
439204878Sfabient	/* No more data when shutdown in progress. */
440204878Sfabient	if (po->po_flags & PMC_PO_SHUTDOWN) {
441204878Sfabient		mtx_unlock_spin(&po->po_mtx);
442204878Sfabient		return (NULL);
443204878Sfabient	}
444204878Sfabient
445147191Sjkoshy	if (po->po_curbuf == NULL)
446147191Sjkoshy		if (pmclog_get_buffer(po) != 0) {
447147191Sjkoshy			mtx_unlock_spin(&po->po_mtx);
448186127Sjkoshy			return (NULL);
449147191Sjkoshy		}
450147191Sjkoshy
451147191Sjkoshy	KASSERT(po->po_curbuf != NULL,
452186127Sjkoshy	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
453147191Sjkoshy
454147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base &&
455147191Sjkoshy	    po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
456186127Sjkoshy	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
457147191Sjkoshy		__LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
458147191Sjkoshy		po->po_curbuf->plb_fence));
459147191Sjkoshy
460147867Sjkoshy	oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
461147191Sjkoshy	newptr = oldptr + length;
462147191Sjkoshy
463147867Sjkoshy	KASSERT(oldptr != (uintptr_t) NULL,
464186127Sjkoshy	    ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
465147191Sjkoshy
466147191Sjkoshy	/*
467147191Sjkoshy	 * If we have space in the current buffer, return a pointer to
468147191Sjkoshy	 * available space with the PO structure locked.
469147191Sjkoshy	 */
470147867Sjkoshy	if (newptr <= (uintptr_t) po->po_curbuf->plb_fence) {
471147867Sjkoshy		po->po_curbuf->plb_ptr = (char *) newptr;
472147191Sjkoshy		goto done;
473147191Sjkoshy	}
474147191Sjkoshy
475147867Sjkoshy	/*
476147867Sjkoshy	 * Otherwise, schedule the current buffer for output and get a
477147867Sjkoshy	 * fresh buffer.
478147867Sjkoshy	 */
479147191Sjkoshy	pmclog_schedule_io(po);
480147191Sjkoshy
481147191Sjkoshy	if (pmclog_get_buffer(po) != 0) {
482147191Sjkoshy		mtx_unlock_spin(&po->po_mtx);
483186127Sjkoshy		return (NULL);
484147191Sjkoshy	}
485147191Sjkoshy
486147191Sjkoshy	KASSERT(po->po_curbuf != NULL,
487186127Sjkoshy	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
488147191Sjkoshy
489147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr != NULL,
490186127Sjkoshy	    ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
491147191Sjkoshy
492147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr == po->po_curbuf->plb_base &&
493147191Sjkoshy	    po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
494186127Sjkoshy	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
495147191Sjkoshy		__LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
496147191Sjkoshy		po->po_curbuf->plb_fence));
497147191Sjkoshy
498147867Sjkoshy	oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
499147191Sjkoshy
500147191Sjkoshy done:
501147867Sjkoshy	lh = (uint32_t *) oldptr;
502147867Sjkoshy	lh++;				/* skip header */
503147867Sjkoshy	getnanotime(&ts);		/* fill in the timestamp */
504147191Sjkoshy	*lh++ = ts.tv_sec & 0xFFFFFFFF;
505147191Sjkoshy	*lh++ = ts.tv_nsec & 0xFFFFFFF;
506186127Sjkoshy	return ((uint32_t *) oldptr);
507147191Sjkoshy}
508147191Sjkoshy
509147191Sjkoshy/*
510147191Sjkoshy * Schedule an I/O.
511147191Sjkoshy *
512147191Sjkoshy * Transfer the current buffer to the helper kthread.
513147191Sjkoshy */
514147191Sjkoshy
515147191Sjkoshystatic void
516147191Sjkoshypmclog_schedule_io(struct pmc_owner *po)
517147191Sjkoshy{
518147191Sjkoshy	KASSERT(po->po_curbuf != NULL,
519186127Sjkoshy	    ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
520147191Sjkoshy
521147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
522186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
523147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
524147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
525186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
526147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
527147191Sjkoshy
528282658Sjhb	PMCDBG1(LOG,SIO, 1, "po=%p", po);
529147191Sjkoshy
530147191Sjkoshy	mtx_assert(&po->po_mtx, MA_OWNED);
531147191Sjkoshy
532147191Sjkoshy	/*
533147191Sjkoshy	 * Add the current buffer to the tail of the buffer list and
534147191Sjkoshy	 * wakeup the helper.
535147191Sjkoshy	 */
536147191Sjkoshy	TAILQ_INSERT_TAIL(&po->po_logbuffers, po->po_curbuf, plb_next);
537147191Sjkoshy	po->po_curbuf = NULL;
538147191Sjkoshy	wakeup_one(po);
539147191Sjkoshy}
540147191Sjkoshy
541147191Sjkoshy/*
542147191Sjkoshy * Stop the helper kthread.
543147191Sjkoshy */
544147191Sjkoshy
545147191Sjkoshystatic void
546147191Sjkoshypmclog_stop_kthread(struct pmc_owner *po)
547147191Sjkoshy{
548147191Sjkoshy	/*
549199763Sfabient	 * Close the file to force the thread out of fo_write,
550199763Sfabient	 * unset flag, wakeup the helper thread,
551147191Sjkoshy	 * wait for it to exit
552147191Sjkoshy	 */
553147191Sjkoshy
554199763Sfabient	if (po->po_file != NULL)
555199763Sfabient		fo_close(po->po_file, curthread);
556199763Sfabient
557199763Sfabient	mtx_lock(&pmc_kthread_mtx);
558147191Sjkoshy	po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
559147191Sjkoshy	wakeup_one(po);
560147191Sjkoshy	if (po->po_kthread)
561147867Sjkoshy		msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
562199763Sfabient	mtx_unlock(&pmc_kthread_mtx);
563147191Sjkoshy}
564147191Sjkoshy
565147191Sjkoshy/*
566147191Sjkoshy * Public functions
567147191Sjkoshy */
568147191Sjkoshy
569147191Sjkoshy/*
570147191Sjkoshy * Configure a log file for pmc owner 'po'.
571147191Sjkoshy *
572147191Sjkoshy * Parameter 'logfd' is a file handle referencing an open file in the
573147191Sjkoshy * owner process.  This file needs to have been opened for writing.
574147191Sjkoshy */
575147191Sjkoshy
576147191Sjkoshyint
577185363Sjkoshypmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
578147191Sjkoshy{
579147191Sjkoshy	int error;
580147191Sjkoshy	struct proc *p;
581255219Spjd	cap_rights_t rights;
582195005Sattilio	/*
583195005Sattilio	 * As long as it is possible to get a LOR between pmc_sx lock and
584195005Sattilio	 * proctree/allproc sx locks used for adding a new process, assure
585195005Sattilio	 * the former is not held here.
586195005Sattilio	 */
587195005Sattilio	sx_assert(&pmc_sx, SA_UNLOCKED);
588282658Sjhb	PMCDBG2(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
589147191Sjkoshy
590147191Sjkoshy	p = po->po_owner;
591147191Sjkoshy
592147191Sjkoshy	/* return EBUSY if a log file was already present */
593147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
594186127Sjkoshy		return (EBUSY);
595147191Sjkoshy
596147191Sjkoshy	KASSERT(po->po_kthread == NULL,
597186127Sjkoshy	    ("[pmclog,%d] po=%p kthread (%p) already present", __LINE__, po,
598147191Sjkoshy		po->po_kthread));
599147191Sjkoshy	KASSERT(po->po_file == NULL,
600186127Sjkoshy	    ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
601147191Sjkoshy		po->po_file));
602147191Sjkoshy
603147191Sjkoshy	/* get a reference to the file state */
604255219Spjd	error = fget_write(curthread, logfd,
605255219Spjd	    cap_rights_init(&rights, CAP_WRITE), &po->po_file);
606147191Sjkoshy	if (error)
607147191Sjkoshy		goto error;
608279894Srrs
609147191Sjkoshy	/* mark process as owning a log file */
610147191Sjkoshy	po->po_flags |= PMC_PO_OWNS_LOGFILE;
611172836Sjulian	error = kproc_create(pmclog_loop, po, &po->po_kthread,
612147191Sjkoshy	    RFHIGHPID, 0, "hwpmc: proc(%d)", p->p_pid);
613147191Sjkoshy	if (error)
614147191Sjkoshy		goto error;
615147191Sjkoshy
616147191Sjkoshy	/* mark process as using HWPMCs */
617147191Sjkoshy	PROC_LOCK(p);
618147191Sjkoshy	p->p_flag |= P_HWPMC;
619147191Sjkoshy	PROC_UNLOCK(p);
620147191Sjkoshy
621147191Sjkoshy	/* create a log initialization entry */
622147191Sjkoshy	PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
623147191Sjkoshy	    sizeof(struct pmclog_initialize));
624147191Sjkoshy	PMCLOG_EMIT32(PMC_VERSION);
625147191Sjkoshy	PMCLOG_EMIT32(md->pmd_cputype);
626147191Sjkoshy	PMCLOG_DESPATCH(po);
627147191Sjkoshy
628186127Sjkoshy	return (0);
629147191Sjkoshy
630147191Sjkoshy error:
631147191Sjkoshy	/* shutdown the thread */
632147191Sjkoshy	if (po->po_kthread)
633147191Sjkoshy		pmclog_stop_kthread(po);
634147191Sjkoshy
635186127Sjkoshy	KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
636186127Sjkoshy	    "stopped", __LINE__, po));
637147191Sjkoshy
638147191Sjkoshy	if (po->po_file)
639147191Sjkoshy		(void) fdrop(po->po_file, curthread);
640147191Sjkoshy	po->po_file  = NULL;	/* clear file and error state */
641147191Sjkoshy	po->po_error = 0;
642147191Sjkoshy
643186127Sjkoshy	return (error);
644147191Sjkoshy}
645147191Sjkoshy
646147191Sjkoshy
647147191Sjkoshy/*
648147191Sjkoshy * De-configure a log file.  This will throw away any buffers queued
649147191Sjkoshy * for this owner process.
650147191Sjkoshy */
651147191Sjkoshy
652147191Sjkoshyint
653147191Sjkoshypmclog_deconfigure_log(struct pmc_owner *po)
654147191Sjkoshy{
655147191Sjkoshy	int error;
656147191Sjkoshy	struct pmclog_buffer *lb;
657147191Sjkoshy
658282658Sjhb	PMCDBG1(LOG,CFG,1, "de-config po=%p", po);
659147191Sjkoshy
660147191Sjkoshy	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
661186127Sjkoshy		return (EINVAL);
662147191Sjkoshy
663156466Sjkoshy	KASSERT(po->po_sscount == 0,
664186127Sjkoshy	    ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
665147191Sjkoshy	KASSERT(po->po_file != NULL,
666186127Sjkoshy	    ("[pmclog,%d] po=%p no log file", __LINE__, po));
667147191Sjkoshy
668147191Sjkoshy	/* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
669233544Sfabient	pmclog_stop_kthread(po);
670147191Sjkoshy
671147191Sjkoshy	KASSERT(po->po_kthread == NULL,
672186127Sjkoshy	    ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
673147191Sjkoshy
674147191Sjkoshy	/* return all queued log buffers to the global pool */
675147191Sjkoshy	while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
676147191Sjkoshy		TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
677147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
678147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
679147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
680147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
681147191Sjkoshy	}
682147191Sjkoshy
683147191Sjkoshy	/* return the 'current' buffer to the global pool */
684147191Sjkoshy	if ((lb = po->po_curbuf) != NULL) {
685147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
686147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
687147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
688147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
689147191Sjkoshy	}
690147191Sjkoshy
691147191Sjkoshy	/* drop a reference to the fd */
692147191Sjkoshy	error = fdrop(po->po_file, curthread);
693147191Sjkoshy	po->po_file  = NULL;
694147191Sjkoshy	po->po_error = 0;
695147191Sjkoshy
696186127Sjkoshy	return (error);
697147191Sjkoshy}
698147191Sjkoshy
699147191Sjkoshy/*
700147191Sjkoshy * Flush a process' log buffer.
701147191Sjkoshy */
702147191Sjkoshy
703147191Sjkoshyint
704147191Sjkoshypmclog_flush(struct pmc_owner *po)
705147191Sjkoshy{
706204878Sfabient	int error;
707226514Sfabient	struct pmclog_buffer *lb;
708147191Sjkoshy
709282658Sjhb	PMCDBG1(LOG,FLS,1, "po=%p", po);
710147191Sjkoshy
711147191Sjkoshy	/*
712147191Sjkoshy	 * If there is a pending error recorded by the logger thread,
713147191Sjkoshy	 * return that.
714147191Sjkoshy	 */
715147191Sjkoshy	if (po->po_error)
716186127Sjkoshy		return (po->po_error);
717147191Sjkoshy
718147191Sjkoshy	error = 0;
719147191Sjkoshy
720147191Sjkoshy	/*
721147191Sjkoshy	 * Check that we do have an active log file.
722147191Sjkoshy	 */
723147191Sjkoshy	mtx_lock(&pmc_kthread_mtx);
724147191Sjkoshy	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
725147191Sjkoshy		error = EINVAL;
726147191Sjkoshy		goto error;
727147191Sjkoshy	}
728147191Sjkoshy
729147191Sjkoshy	/*
730226514Sfabient	 * Schedule the current buffer if any and not empty.
731147191Sjkoshy	 */
732147191Sjkoshy	mtx_lock_spin(&po->po_mtx);
733226514Sfabient	lb = po->po_curbuf;
734226514Sfabient	if (lb && lb->plb_ptr != lb->plb_base) {
735226514Sfabient		pmclog_schedule_io(po);
736226514Sfabient	} else
737226514Sfabient		error = ENOBUFS;
738226514Sfabient	mtx_unlock_spin(&po->po_mtx);
739226514Sfabient
740226514Sfabient error:
741226514Sfabient	mtx_unlock(&pmc_kthread_mtx);
742226514Sfabient
743226514Sfabient	return (error);
744226514Sfabient}
745226514Sfabient
746226514Sfabientint
747226514Sfabientpmclog_close(struct pmc_owner *po)
748226514Sfabient{
749226514Sfabient
750282658Sjhb	PMCDBG1(LOG,CLO,1, "po=%p", po);
751226514Sfabient
752226514Sfabient	mtx_lock(&pmc_kthread_mtx);
753226514Sfabient
754226514Sfabient	/*
755226514Sfabient	 * Schedule the current buffer.
756226514Sfabient	 */
757226514Sfabient	mtx_lock_spin(&po->po_mtx);
758147191Sjkoshy	if (po->po_curbuf)
759147191Sjkoshy		pmclog_schedule_io(po);
760226514Sfabient	else
761226514Sfabient		wakeup_one(po);
762147191Sjkoshy	mtx_unlock_spin(&po->po_mtx);
763147191Sjkoshy
764204878Sfabient	/*
765204878Sfabient	 * Initiate shutdown: no new data queued,
766204878Sfabient	 * thread will close file on last block.
767204878Sfabient	 */
768204878Sfabient	po->po_flags |= PMC_PO_SHUTDOWN;
769147191Sjkoshy
770147191Sjkoshy	mtx_unlock(&pmc_kthread_mtx);
771147191Sjkoshy
772226514Sfabient	return (0);
773147191Sjkoshy}
774147191Sjkoshy
775174395Sjkoshyvoid
776174395Sjkoshypmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
777174395Sjkoshy{
778174395Sjkoshy	int n, recordlen;
779174395Sjkoshy	uint32_t flags;
780174395Sjkoshy	struct pmc_owner *po;
781147191Sjkoshy
782282658Sjhb	PMCDBG3(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
783174395Sjkoshy	    ps->ps_nsamples);
784174395Sjkoshy
785174395Sjkoshy	recordlen = offsetof(struct pmclog_callchain, pl_pc) +
786174395Sjkoshy	    ps->ps_nsamples * sizeof(uintfptr_t);
787174395Sjkoshy	po = pm->pm_owner;
788174395Sjkoshy	flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
789174395Sjkoshy	PMCLOG_RESERVE(po, CALLCHAIN, recordlen);
790174395Sjkoshy	PMCLOG_EMIT32(ps->ps_pid);
791174395Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
792174395Sjkoshy	PMCLOG_EMIT32(flags);
793174395Sjkoshy	for (n = 0; n < ps->ps_nsamples; n++)
794174395Sjkoshy		PMCLOG_EMITADDR(ps->ps_pc[n]);
795174395Sjkoshy	PMCLOG_DESPATCH(po);
796174395Sjkoshy}
797174395Sjkoshy
798147191Sjkoshyvoid
799147191Sjkoshypmclog_process_closelog(struct pmc_owner *po)
800147191Sjkoshy{
801147191Sjkoshy	PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
802147191Sjkoshy	PMCLOG_DESPATCH(po);
803147191Sjkoshy}
804147191Sjkoshy
805147191Sjkoshyvoid
806147191Sjkoshypmclog_process_dropnotify(struct pmc_owner *po)
807147191Sjkoshy{
808147191Sjkoshy	PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
809147191Sjkoshy	PMCLOG_DESPATCH(po);
810147191Sjkoshy}
811147191Sjkoshy
812147191Sjkoshyvoid
813157144Sjkoshypmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
814157144Sjkoshy    const char *path)
815147191Sjkoshy{
816147191Sjkoshy	int pathlen, recordlen;
817147191Sjkoshy
818157144Sjkoshy	KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
819157144Sjkoshy
820147191Sjkoshy	pathlen = strlen(path) + 1;	/* #bytes for path name */
821157144Sjkoshy	recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
822147191Sjkoshy	    pathlen;
823147191Sjkoshy
824157144Sjkoshy	PMCLOG_RESERVE(po, MAP_IN, recordlen);
825157144Sjkoshy	PMCLOG_EMIT32(pid);
826147191Sjkoshy	PMCLOG_EMITADDR(start);
827147191Sjkoshy	PMCLOG_EMITSTRING(path,pathlen);
828147191Sjkoshy	PMCLOG_DESPATCH(po);
829147191Sjkoshy}
830147191Sjkoshy
831157144Sjkoshyvoid
832157144Sjkoshypmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
833157144Sjkoshy    uintfptr_t end)
834157144Sjkoshy{
835157144Sjkoshy	KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
836147191Sjkoshy
837157144Sjkoshy	PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
838157144Sjkoshy	PMCLOG_EMIT32(pid);
839157144Sjkoshy	PMCLOG_EMITADDR(start);
840157144Sjkoshy	PMCLOG_EMITADDR(end);
841157144Sjkoshy	PMCLOG_DESPATCH(po);
842157144Sjkoshy}
843157144Sjkoshy
844147191Sjkoshyvoid
845147191Sjkoshypmclog_process_pmcallocate(struct pmc *pm)
846147191Sjkoshy{
847147191Sjkoshy	struct pmc_owner *po;
848233628Sfabient	struct pmc_soft *ps;
849147191Sjkoshy
850147191Sjkoshy	po = pm->pm_owner;
851147191Sjkoshy
852282658Sjhb	PMCDBG1(LOG,ALL,1, "pm=%p", pm);
853147191Sjkoshy
854233628Sfabient	if (PMC_TO_CLASS(pm) == PMC_CLASS_SOFT) {
855233628Sfabient		PMCLOG_RESERVE(po, PMCALLOCATEDYN,
856233628Sfabient		    sizeof(struct pmclog_pmcallocatedyn));
857233628Sfabient		PMCLOG_EMIT32(pm->pm_id);
858233628Sfabient		PMCLOG_EMIT32(pm->pm_event);
859233628Sfabient		PMCLOG_EMIT32(pm->pm_flags);
860233628Sfabient		ps = pmc_soft_ev_acquire(pm->pm_event);
861233628Sfabient		if (ps != NULL)
862233628Sfabient			PMCLOG_EMITSTRING(ps->ps_ev.pm_ev_name,PMC_NAME_MAX);
863233628Sfabient		else
864233628Sfabient			PMCLOG_EMITNULLSTRING(PMC_NAME_MAX);
865233628Sfabient		pmc_soft_ev_release(ps);
866233628Sfabient		PMCLOG_DESPATCH(po);
867233628Sfabient	} else {
868233628Sfabient		PMCLOG_RESERVE(po, PMCALLOCATE,
869233628Sfabient		    sizeof(struct pmclog_pmcallocate));
870233628Sfabient		PMCLOG_EMIT32(pm->pm_id);
871233628Sfabient		PMCLOG_EMIT32(pm->pm_event);
872233628Sfabient		PMCLOG_EMIT32(pm->pm_flags);
873233628Sfabient		PMCLOG_DESPATCH(po);
874233628Sfabient	}
875147191Sjkoshy}
876147191Sjkoshy
877147191Sjkoshyvoid
878147191Sjkoshypmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
879147191Sjkoshy{
880147191Sjkoshy	int pathlen, recordlen;
881147191Sjkoshy	struct pmc_owner *po;
882147191Sjkoshy
883282658Sjhb	PMCDBG2(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
884147191Sjkoshy
885147191Sjkoshy	po = pm->pm_owner;
886147191Sjkoshy
887147191Sjkoshy	pathlen = strlen(path) + 1;	/* #bytes for the string */
888147191Sjkoshy	recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
889147191Sjkoshy
890147191Sjkoshy	PMCLOG_RESERVE(po, PMCATTACH, recordlen);
891147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
892147191Sjkoshy	PMCLOG_EMIT32(pid);
893147191Sjkoshy	PMCLOG_EMITSTRING(path, pathlen);
894147191Sjkoshy	PMCLOG_DESPATCH(po);
895147191Sjkoshy}
896147191Sjkoshy
897147191Sjkoshyvoid
898147191Sjkoshypmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
899147191Sjkoshy{
900147191Sjkoshy	struct pmc_owner *po;
901147191Sjkoshy
902282658Sjhb	PMCDBG2(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
903147191Sjkoshy
904147191Sjkoshy	po = pm->pm_owner;
905147191Sjkoshy
906147191Sjkoshy	PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
907147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
908147191Sjkoshy	PMCLOG_EMIT32(pid);
909147191Sjkoshy	PMCLOG_DESPATCH(po);
910147191Sjkoshy}
911147191Sjkoshy
912147191Sjkoshy/*
913147191Sjkoshy * Log a context switch event to the log file.
914147191Sjkoshy */
915147191Sjkoshy
916147191Sjkoshyvoid
917147191Sjkoshypmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
918147191Sjkoshy{
919147191Sjkoshy	struct pmc_owner *po;
920147191Sjkoshy
921147191Sjkoshy	KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
922147191Sjkoshy	    ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
923147191Sjkoshy
924282658Sjhb	PMCDBG3(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
925147191Sjkoshy	    v);
926147191Sjkoshy
927147191Sjkoshy	po = pm->pm_owner;
928147191Sjkoshy
929147191Sjkoshy	PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
930147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
931147191Sjkoshy	PMCLOG_EMIT64(v);
932147191Sjkoshy	PMCLOG_EMIT32(pp->pp_proc->p_pid);
933147191Sjkoshy	PMCLOG_DESPATCH(po);
934147191Sjkoshy}
935147191Sjkoshy
936147191Sjkoshyvoid
937147708Sjkoshypmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
938147708Sjkoshy    uintfptr_t startaddr, char *path)
939147191Sjkoshy{
940147191Sjkoshy	int pathlen, recordlen;
941147191Sjkoshy
942282658Sjhb	PMCDBG3(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
943147191Sjkoshy
944147191Sjkoshy	pathlen   = strlen(path) + 1;	/* #bytes for the path */
945147191Sjkoshy	recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
946147191Sjkoshy
947147191Sjkoshy	PMCLOG_RESERVE(po, PROCEXEC, recordlen);
948147191Sjkoshy	PMCLOG_EMIT32(pid);
949147708Sjkoshy	PMCLOG_EMITADDR(startaddr);
950147708Sjkoshy	PMCLOG_EMIT32(pmid);
951147191Sjkoshy	PMCLOG_EMITSTRING(path,pathlen);
952147191Sjkoshy	PMCLOG_DESPATCH(po);
953147191Sjkoshy}
954147191Sjkoshy
955147191Sjkoshy/*
956147191Sjkoshy * Log a process exit event (and accumulated pmc value) to the log file.
957147191Sjkoshy */
958147191Sjkoshy
959147191Sjkoshyvoid
960147191Sjkoshypmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
961147191Sjkoshy{
962147191Sjkoshy	int ri;
963147191Sjkoshy	struct pmc_owner *po;
964147191Sjkoshy
965147191Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
966282658Sjhb	PMCDBG3(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
967147191Sjkoshy	    pp->pp_pmcs[ri].pp_pmcval);
968147191Sjkoshy
969147191Sjkoshy	po = pm->pm_owner;
970147191Sjkoshy
971147191Sjkoshy	PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
972147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
973147191Sjkoshy	PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
974147191Sjkoshy	PMCLOG_EMIT32(pp->pp_proc->p_pid);
975147191Sjkoshy	PMCLOG_DESPATCH(po);
976147191Sjkoshy}
977147191Sjkoshy
978147191Sjkoshy/*
979147191Sjkoshy * Log a fork event.
980147191Sjkoshy */
981147191Sjkoshy
982147191Sjkoshyvoid
983147191Sjkoshypmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
984147191Sjkoshy{
985147191Sjkoshy	PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
986147191Sjkoshy	PMCLOG_EMIT32(oldpid);
987147191Sjkoshy	PMCLOG_EMIT32(newpid);
988147191Sjkoshy	PMCLOG_DESPATCH(po);
989147191Sjkoshy}
990147191Sjkoshy
991147191Sjkoshy/*
992147191Sjkoshy * Log a process exit event of the form suitable for system-wide PMCs.
993147191Sjkoshy */
994147191Sjkoshy
995147191Sjkoshyvoid
996147191Sjkoshypmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
997147191Sjkoshy{
998147191Sjkoshy	PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
999147191Sjkoshy	PMCLOG_EMIT32(pid);
1000147191Sjkoshy	PMCLOG_DESPATCH(po);
1001147191Sjkoshy}
1002147191Sjkoshy
1003147191Sjkoshy/*
1004147191Sjkoshy * Write a user log entry.
1005147191Sjkoshy */
1006147191Sjkoshy
1007147191Sjkoshyint
1008147191Sjkoshypmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
1009147191Sjkoshy{
1010147191Sjkoshy	int error;
1011147191Sjkoshy
1012282658Sjhb	PMCDBG2(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
1013147191Sjkoshy
1014147191Sjkoshy	error = 0;
1015147191Sjkoshy
1016147191Sjkoshy	PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
1017147191Sjkoshy	    sizeof(struct pmclog_userdata));
1018147191Sjkoshy	PMCLOG_EMIT32(wl->pm_userdata);
1019147191Sjkoshy	PMCLOG_DESPATCH(po);
1020147191Sjkoshy
1021147191Sjkoshy error:
1022186127Sjkoshy	return (error);
1023147191Sjkoshy}
1024147191Sjkoshy
1025147191Sjkoshy/*
1026147191Sjkoshy * Initialization.
1027147191Sjkoshy *
1028147191Sjkoshy * Create a pool of log buffers and initialize mutexes.
1029147191Sjkoshy */
1030147191Sjkoshy
1031147191Sjkoshyvoid
1032147191Sjkoshypmclog_initialize()
1033147191Sjkoshy{
1034147191Sjkoshy	int n;
1035147191Sjkoshy	struct pmclog_buffer *plb;
1036147191Sjkoshy
1037147191Sjkoshy	if (pmclog_buffer_size <= 0) {
1038186127Sjkoshy		(void) printf("hwpmc: tunable logbuffersize=%d must be "
1039186127Sjkoshy		    "greater than zero.\n", pmclog_buffer_size);
1040147191Sjkoshy		pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1041147191Sjkoshy	}
1042147191Sjkoshy
1043147191Sjkoshy	if (pmc_nlogbuffers <= 0) {
1044147191Sjkoshy		(void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
1045147191Sjkoshy		    "than zero.\n", pmc_nlogbuffers);
1046147191Sjkoshy		pmc_nlogbuffers = PMC_NLOGBUFFERS;
1047147191Sjkoshy	}
1048147191Sjkoshy
1049147191Sjkoshy	/* create global pool of log buffers */
1050147191Sjkoshy	for (n = 0; n < pmc_nlogbuffers; n++) {
1051184801Sjkoshy		plb = malloc(1024 * pmclog_buffer_size, M_PMC,
1052184801Sjkoshy		    M_WAITOK|M_ZERO);
1053147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
1054147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
1055147191Sjkoshy	}
1056168856Sjkoshy	mtx_init(&pmc_bufferlist_mtx, "pmc-buffer-list", "pmc-leaf",
1057168856Sjkoshy	    MTX_SPIN);
1058168856Sjkoshy	mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
1059147191Sjkoshy}
1060147191Sjkoshy
1061147191Sjkoshy/*
1062147191Sjkoshy * Shutdown logging.
1063147191Sjkoshy *
1064147191Sjkoshy * Destroy mutexes and release memory back the to free pool.
1065147191Sjkoshy */
1066147191Sjkoshy
1067147191Sjkoshyvoid
1068147191Sjkoshypmclog_shutdown()
1069147191Sjkoshy{
1070147191Sjkoshy	struct pmclog_buffer *plb;
1071147191Sjkoshy
1072147191Sjkoshy	mtx_destroy(&pmc_kthread_mtx);
1073147191Sjkoshy	mtx_destroy(&pmc_bufferlist_mtx);
1074147191Sjkoshy
1075147191Sjkoshy	while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
1076147191Sjkoshy		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
1077184205Sdes		free(plb, M_PMC);
1078147191Sjkoshy	}
1079147191Sjkoshy}
1080