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$");
38147191Sjkoshy
39147191Sjkoshy#include <sys/param.h>
40280455Srrs#if (__FreeBSD_version >= 1100000)
41280258Srwatson#include <sys/capsicum.h>
42280455Srrs#else
43280455Srrs#include <sys/capability.h>
44280455Srrs#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;
73280455SrrsSYSCTL_INT(_kern_hwpmc, OID_AUTO, logbuffersize, CTLFLAG_RDTUN,
74147191Sjkoshy    &pmclog_buffer_size, 0, "size of log buffers in kilobytes");
75147191Sjkoshy
76147191Sjkoshy/*
77147191Sjkoshy * kern.hwpmc.nbuffer -- number of global log buffers
78147191Sjkoshy */
79147191Sjkoshy
80147191Sjkoshystatic int pmc_nlogbuffers = PMC_NLOGBUFFERS;
81280455SrrsSYSCTL_INT(_kern_hwpmc, OID_AUTO, nbuffers, CTLFLAG_RDTUN,
82147191Sjkoshy    &pmc_nlogbuffers, 0, "number of global log buffers");
83147191Sjkoshy
84147191Sjkoshy/*
85147191Sjkoshy * Global log buffer list and associated spin lock.
86147191Sjkoshy */
87147191Sjkoshy
88147191SjkoshyTAILQ_HEAD(, pmclog_buffer) pmc_bufferlist =
89147191Sjkoshy	TAILQ_HEAD_INITIALIZER(pmc_bufferlist);
90147191Sjkoshystatic struct mtx pmc_bufferlist_mtx;	/* spin lock */
91147191Sjkoshystatic struct mtx pmc_kthread_mtx;	/* sleep lock */
92147191Sjkoshy
93147191Sjkoshy#define	PMCLOG_INIT_BUFFER_DESCRIPTOR(D) do {				\
94147191Sjkoshy		const int __roundup = roundup(sizeof(*D),		\
95147191Sjkoshy			sizeof(uint32_t));				\
96147191Sjkoshy		(D)->plb_fence = ((char *) (D)) +			\
97147191Sjkoshy			 1024*pmclog_buffer_size;			\
98147191Sjkoshy		(D)->plb_base  = (D)->plb_ptr = ((char *) (D)) +	\
99147191Sjkoshy			__roundup;					\
100147191Sjkoshy	} while (0)
101147191Sjkoshy
102147191Sjkoshy
103147191Sjkoshy/*
104147191Sjkoshy * Log file record constructors.
105147191Sjkoshy */
106147867Sjkoshy#define	_PMCLOG_TO_HEADER(T,L)						\
107147867Sjkoshy	((PMCLOG_HEADER_MAGIC << 24) |					\
108147867Sjkoshy	 (PMCLOG_TYPE_ ## T << 16)   |					\
109147867Sjkoshy	 ((L) & 0xFFFF))
110147867Sjkoshy
111147191Sjkoshy/* reserve LEN bytes of space and initialize the entry header */
112147191Sjkoshy#define	_PMCLOG_RESERVE(PO,TYPE,LEN,ACTION) do {			\
113147191Sjkoshy		uint32_t *_le;						\
114147191Sjkoshy		int _len = roundup((LEN), sizeof(uint32_t));		\
115147191Sjkoshy		if ((_le = pmclog_reserve((PO), _len)) == NULL) {	\
116147191Sjkoshy			ACTION;						\
117147191Sjkoshy		}							\
118147867Sjkoshy		*_le = _PMCLOG_TO_HEADER(TYPE,_len);			\
119147191Sjkoshy		_le += 3	/* skip over timestamp */
120147191Sjkoshy
121147191Sjkoshy#define	PMCLOG_RESERVE(P,T,L)		_PMCLOG_RESERVE(P,T,L,return)
122147191Sjkoshy#define	PMCLOG_RESERVE_WITH_ERROR(P,T,L) _PMCLOG_RESERVE(P,T,L,		\
123147191Sjkoshy	error=ENOMEM;goto error)
124147191Sjkoshy
125147191Sjkoshy#define	PMCLOG_EMIT32(V)	do { *_le++ = (V); } while (0)
126147191Sjkoshy#define	PMCLOG_EMIT64(V)	do { 					\
127147191Sjkoshy		*_le++ = (uint32_t) ((V) & 0xFFFFFFFF);			\
128147191Sjkoshy		*_le++ = (uint32_t) (((V) >> 32) & 0xFFFFFFFF);		\
129147191Sjkoshy	} while (0)
130147191Sjkoshy
131147191Sjkoshy
132147191Sjkoshy/* Emit a string.  Caution: does NOT update _le, so needs to be last */
133147191Sjkoshy#define	PMCLOG_EMITSTRING(S,L)	do { bcopy((S), _le, (L)); } while (0)
134233628Sfabient#define	PMCLOG_EMITNULLSTRING(L) do { bzero(_le, (L)); } while (0)
135147191Sjkoshy
136147191Sjkoshy#define	PMCLOG_DESPATCH(PO)						\
137147191Sjkoshy		pmclog_release((PO));					\
138147191Sjkoshy	} while (0)
139147191Sjkoshy
140147191Sjkoshy
141147191Sjkoshy/*
142147191Sjkoshy * Assertions about the log file format.
143147191Sjkoshy */
144147191Sjkoshy
145174395SjkoshyCTASSERT(sizeof(struct pmclog_callchain) == 6*4 +
146174395Sjkoshy    PMC_CALLCHAIN_DEPTH_MAX*sizeof(uintfptr_t));
147147191SjkoshyCTASSERT(sizeof(struct pmclog_closelog) == 3*4);
148147191SjkoshyCTASSERT(sizeof(struct pmclog_dropnotify) == 3*4);
149157144SjkoshyCTASSERT(sizeof(struct pmclog_map_in) == PATH_MAX +
150157144Sjkoshy    4*4 + sizeof(uintfptr_t));
151157144SjkoshyCTASSERT(offsetof(struct pmclog_map_in,pl_pathname) ==
152157144Sjkoshy    4*4 + sizeof(uintfptr_t));
153157144SjkoshyCTASSERT(sizeof(struct pmclog_map_out) == 4*4 + 2*sizeof(uintfptr_t));
154147708SjkoshyCTASSERT(sizeof(struct pmclog_pcsample) == 6*4 + sizeof(uintfptr_t));
155147191SjkoshyCTASSERT(sizeof(struct pmclog_pmcallocate) == 6*4);
156147191SjkoshyCTASSERT(sizeof(struct pmclog_pmcattach) == 5*4 + PATH_MAX);
157147191SjkoshyCTASSERT(offsetof(struct pmclog_pmcattach,pl_pathname) == 5*4);
158147191SjkoshyCTASSERT(sizeof(struct pmclog_pmcdetach) == 5*4);
159147191SjkoshyCTASSERT(sizeof(struct pmclog_proccsw) == 5*4 + 8);
160147708SjkoshyCTASSERT(sizeof(struct pmclog_procexec) == 5*4 + PATH_MAX +
161147708Sjkoshy    sizeof(uintfptr_t));
162147708SjkoshyCTASSERT(offsetof(struct pmclog_procexec,pl_pathname) == 5*4 +
163147708Sjkoshy    sizeof(uintfptr_t));
164147191SjkoshyCTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8);
165147191SjkoshyCTASSERT(sizeof(struct pmclog_procfork) == 5*4);
166147191SjkoshyCTASSERT(sizeof(struct pmclog_sysexit) == 4*4);
167147191SjkoshyCTASSERT(sizeof(struct pmclog_userdata) == 4*4);
168147191Sjkoshy
169147191Sjkoshy/*
170147191Sjkoshy * Log buffer structure
171147191Sjkoshy */
172147191Sjkoshy
173147191Sjkoshystruct pmclog_buffer {
174147191Sjkoshy	TAILQ_ENTRY(pmclog_buffer) plb_next;
175147191Sjkoshy	char 		*plb_base;
176147191Sjkoshy	char		*plb_ptr;
177147191Sjkoshy	char 		*plb_fence;
178147191Sjkoshy};
179147191Sjkoshy
180147191Sjkoshy/*
181147191Sjkoshy * Prototypes
182147191Sjkoshy */
183147191Sjkoshy
184147191Sjkoshystatic int pmclog_get_buffer(struct pmc_owner *po);
185147191Sjkoshystatic void pmclog_loop(void *arg);
186147191Sjkoshystatic void pmclog_release(struct pmc_owner *po);
187147191Sjkoshystatic uint32_t *pmclog_reserve(struct pmc_owner *po, int length);
188147191Sjkoshystatic void pmclog_schedule_io(struct pmc_owner *po);
189147191Sjkoshystatic void pmclog_stop_kthread(struct pmc_owner *po);
190147191Sjkoshy
191147191Sjkoshy/*
192147191Sjkoshy * Helper functions
193147191Sjkoshy */
194147191Sjkoshy
195147191Sjkoshy/*
196147191Sjkoshy * Get a log buffer
197147191Sjkoshy */
198147191Sjkoshy
199147191Sjkoshystatic int
200147191Sjkoshypmclog_get_buffer(struct pmc_owner *po)
201147191Sjkoshy{
202147191Sjkoshy	struct pmclog_buffer *plb;
203147191Sjkoshy
204147191Sjkoshy	mtx_assert(&po->po_mtx, MA_OWNED);
205147191Sjkoshy
206147191Sjkoshy	KASSERT(po->po_curbuf == NULL,
207186127Sjkoshy	    ("[pmclog,%d] po=%p current buffer still valid", __LINE__, po));
208147191Sjkoshy
209147191Sjkoshy	mtx_lock_spin(&pmc_bufferlist_mtx);
210147191Sjkoshy	if ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL)
211147191Sjkoshy		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
212147191Sjkoshy	mtx_unlock_spin(&pmc_bufferlist_mtx);
213147191Sjkoshy
214283884Sjhb	PMCDBG2(LOG,GTB,1, "po=%p plb=%p", po, plb);
215147191Sjkoshy
216283884Sjhb#ifdef	HWPMC_DEBUG
217147191Sjkoshy	if (plb)
218147191Sjkoshy		KASSERT(plb->plb_ptr == plb->plb_base &&
219147191Sjkoshy		    plb->plb_base < plb->plb_fence,
220186127Sjkoshy		    ("[pmclog,%d] po=%p buffer invariants: ptr=%p "
221147191Sjkoshy		    "base=%p fence=%p", __LINE__, po, plb->plb_ptr,
222147191Sjkoshy		    plb->plb_base, plb->plb_fence));
223147191Sjkoshy#endif
224147191Sjkoshy
225147191Sjkoshy	po->po_curbuf = plb;
226147191Sjkoshy
227147191Sjkoshy	/* update stats */
228147191Sjkoshy	atomic_add_int(&pmc_stats.pm_buffer_requests, 1);
229147191Sjkoshy	if (plb == NULL)
230147191Sjkoshy		atomic_add_int(&pmc_stats.pm_buffer_requests_failed, 1);
231147191Sjkoshy
232186127Sjkoshy	return (plb ? 0 : ENOMEM);
233147191Sjkoshy}
234147191Sjkoshy
235147191Sjkoshy/*
236147191Sjkoshy * Log handler loop.
237147191Sjkoshy *
238147191Sjkoshy * This function is executed by each pmc owner's helper thread.
239147191Sjkoshy */
240147191Sjkoshy
241147191Sjkoshystatic void
242147191Sjkoshypmclog_loop(void *arg)
243147191Sjkoshy{
244226514Sfabient	int error;
245147191Sjkoshy	struct pmc_owner *po;
246147191Sjkoshy	struct pmclog_buffer *lb;
247199763Sfabient	struct proc *p;
248147191Sjkoshy	struct ucred *ownercred;
249147191Sjkoshy	struct ucred *mycred;
250147191Sjkoshy	struct thread *td;
251147191Sjkoshy	struct uio auio;
252147191Sjkoshy	struct iovec aiov;
253147191Sjkoshy	size_t nbytes;
254147191Sjkoshy
255147191Sjkoshy	po = (struct pmc_owner *) arg;
256199763Sfabient	p = po->po_owner;
257147191Sjkoshy	td = curthread;
258147191Sjkoshy	mycred = td->td_ucred;
259147191Sjkoshy
260199763Sfabient	PROC_LOCK(p);
261199763Sfabient	ownercred = crhold(p->p_ucred);
262199763Sfabient	PROC_UNLOCK(p);
263147191Sjkoshy
264283884Sjhb	PMCDBG2(LOG,INI,1, "po=%p kt=%p", po, po->po_kthread);
265147191Sjkoshy	KASSERT(po->po_kthread == curthread->td_proc,
266186127Sjkoshy	    ("[pmclog,%d] proc mismatch po=%p po/kt=%p curproc=%p", __LINE__,
267147191Sjkoshy		po, po->po_kthread, curthread->td_proc));
268147191Sjkoshy
269147191Sjkoshy	lb = NULL;
270147191Sjkoshy
271147191Sjkoshy
272147191Sjkoshy	/*
273147191Sjkoshy	 * Loop waiting for I/O requests to be added to the owner
274147191Sjkoshy	 * struct's queue.  The loop is exited when the log file
275147191Sjkoshy	 * is deconfigured.
276147191Sjkoshy	 */
277147191Sjkoshy
278147191Sjkoshy	mtx_lock(&pmc_kthread_mtx);
279147191Sjkoshy
280147191Sjkoshy	for (;;) {
281147191Sjkoshy
282147191Sjkoshy		/* check if we've been asked to exit */
283147191Sjkoshy		if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
284147191Sjkoshy			break;
285147191Sjkoshy
286147191Sjkoshy		if (lb == NULL) { /* look for a fresh buffer to write */
287147191Sjkoshy			mtx_lock_spin(&po->po_mtx);
288147191Sjkoshy			if ((lb = TAILQ_FIRST(&po->po_logbuffers)) == NULL) {
289147191Sjkoshy				mtx_unlock_spin(&po->po_mtx);
290147191Sjkoshy
291233544Sfabient				/* No more buffers and shutdown required. */
292226514Sfabient				if (po->po_flags & PMC_PO_SHUTDOWN) {
293226514Sfabient					mtx_unlock(&pmc_kthread_mtx);
294226514Sfabient					/*
295226514Sfabient			 		 * Close the file to get PMCLOG_EOF
296226514Sfabient					 * error in pmclog(3).
297226514Sfabient					 */
298226514Sfabient					fo_close(po->po_file, curthread);
299226514Sfabient					mtx_lock(&pmc_kthread_mtx);
300233544Sfabient					break;
301226514Sfabient				}
302226514Sfabient
303185465Sjkoshy				(void) msleep(po, &pmc_kthread_mtx, PWAIT,
304185465Sjkoshy				    "pmcloop", 0);
305147191Sjkoshy				continue;
306147191Sjkoshy			}
307147191Sjkoshy
308147191Sjkoshy			TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
309147191Sjkoshy			mtx_unlock_spin(&po->po_mtx);
310147191Sjkoshy		}
311147191Sjkoshy
312147191Sjkoshy		mtx_unlock(&pmc_kthread_mtx);
313147191Sjkoshy
314147191Sjkoshy		/* process the request */
315283884Sjhb		PMCDBG3(LOG,WRI,2, "po=%p base=%p ptr=%p", po,
316147191Sjkoshy		    lb->plb_base, lb->plb_ptr);
317147191Sjkoshy		/* change our thread's credentials before issuing the I/O */
318147191Sjkoshy
319147191Sjkoshy		aiov.iov_base = lb->plb_base;
320147191Sjkoshy		aiov.iov_len  = nbytes = lb->plb_ptr - lb->plb_base;
321147191Sjkoshy
322147191Sjkoshy		auio.uio_iov    = &aiov;
323147191Sjkoshy		auio.uio_iovcnt = 1;
324147191Sjkoshy		auio.uio_offset = -1;
325147191Sjkoshy		auio.uio_resid  = nbytes;
326147191Sjkoshy		auio.uio_rw     = UIO_WRITE;
327147191Sjkoshy		auio.uio_segflg = UIO_SYSSPACE;
328147191Sjkoshy		auio.uio_td     = td;
329147191Sjkoshy
330147191Sjkoshy		/* switch thread credentials -- see kern_ktrace.c */
331147191Sjkoshy		td->td_ucred = ownercred;
332147191Sjkoshy		error = fo_write(po->po_file, &auio, ownercred, 0, td);
333147191Sjkoshy		td->td_ucred = mycred;
334147191Sjkoshy
335147191Sjkoshy		if (error) {
336147191Sjkoshy			/* XXX some errors are recoverable */
337147191Sjkoshy			/* send a SIGIO to the owner and exit */
338199763Sfabient			PROC_LOCK(p);
339225617Skmacy			kern_psignal(p, SIGIO);
340199763Sfabient			PROC_UNLOCK(p);
341147191Sjkoshy
342199763Sfabient			mtx_lock(&pmc_kthread_mtx);
343199763Sfabient
344147191Sjkoshy			po->po_error = error; /* save for flush log */
345147191Sjkoshy
346283884Sjhb			PMCDBG2(LOG,WRI,2, "po=%p error=%d", po, error);
347147191Sjkoshy
348147191Sjkoshy			break;
349147191Sjkoshy		}
350147191Sjkoshy
351199763Sfabient		mtx_lock(&pmc_kthread_mtx);
352199763Sfabient
353147191Sjkoshy		/* put the used buffer back into the global pool */
354147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
355147191Sjkoshy
356147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
357147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
358147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
359147191Sjkoshy
360147191Sjkoshy		lb = NULL;
361147191Sjkoshy	}
362147191Sjkoshy
363233544Sfabient	wakeup_one(po->po_kthread);
364147191Sjkoshy	po->po_kthread = NULL;
365147191Sjkoshy
366147191Sjkoshy	mtx_unlock(&pmc_kthread_mtx);
367147191Sjkoshy
368147191Sjkoshy	/* return the current I/O buffer to the global pool */
369147191Sjkoshy	if (lb) {
370147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
371147191Sjkoshy
372147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
373147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
374147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
375147191Sjkoshy	}
376147191Sjkoshy
377147191Sjkoshy	/*
378147191Sjkoshy	 * Exit this thread, signalling the waiter
379147191Sjkoshy	 */
380147191Sjkoshy
381147191Sjkoshy	crfree(ownercred);
382147191Sjkoshy
383172836Sjulian	kproc_exit(0);
384147191Sjkoshy}
385147191Sjkoshy
386147191Sjkoshy/*
387147191Sjkoshy * Release and log entry and schedule an I/O if needed.
388147191Sjkoshy */
389147191Sjkoshy
390147191Sjkoshystatic void
391147191Sjkoshypmclog_release(struct pmc_owner *po)
392147191Sjkoshy{
393147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
394186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
395147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
396147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
397186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
398147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
399147191Sjkoshy
400147191Sjkoshy	/* schedule an I/O if we've filled a buffer */
401147191Sjkoshy	if (po->po_curbuf->plb_ptr >= po->po_curbuf->plb_fence)
402147191Sjkoshy		pmclog_schedule_io(po);
403147191Sjkoshy
404147191Sjkoshy	mtx_unlock_spin(&po->po_mtx);
405147191Sjkoshy
406283884Sjhb	PMCDBG1(LOG,REL,1, "po=%p", po);
407147191Sjkoshy}
408147191Sjkoshy
409147191Sjkoshy
410147191Sjkoshy/*
411147191Sjkoshy * Attempt to reserve 'length' bytes of space in an owner's log
412147191Sjkoshy * buffer.  The function returns a pointer to 'length' bytes of space
413147191Sjkoshy * if there was enough space or returns NULL if no space was
414147191Sjkoshy * available.  Non-null returns do so with the po mutex locked.  The
415147191Sjkoshy * caller must invoke pmclog_release() on the pmc owner structure
416147191Sjkoshy * when done.
417147191Sjkoshy */
418147191Sjkoshy
419147191Sjkoshystatic uint32_t *
420147191Sjkoshypmclog_reserve(struct pmc_owner *po, int length)
421147191Sjkoshy{
422147867Sjkoshy	uintptr_t newptr, oldptr;
423147191Sjkoshy	uint32_t *lh;
424147191Sjkoshy	struct timespec ts;
425147191Sjkoshy
426283884Sjhb	PMCDBG2(LOG,ALL,1, "po=%p len=%d", po, length);
427147191Sjkoshy
428147191Sjkoshy	KASSERT(length % sizeof(uint32_t) == 0,
429147191Sjkoshy	    ("[pmclog,%d] length not a multiple of word size", __LINE__));
430147191Sjkoshy
431147191Sjkoshy	mtx_lock_spin(&po->po_mtx);
432147191Sjkoshy
433204878Sfabient	/* No more data when shutdown in progress. */
434204878Sfabient	if (po->po_flags & PMC_PO_SHUTDOWN) {
435204878Sfabient		mtx_unlock_spin(&po->po_mtx);
436204878Sfabient		return (NULL);
437204878Sfabient	}
438204878Sfabient
439147191Sjkoshy	if (po->po_curbuf == NULL)
440147191Sjkoshy		if (pmclog_get_buffer(po) != 0) {
441147191Sjkoshy			mtx_unlock_spin(&po->po_mtx);
442186127Sjkoshy			return (NULL);
443147191Sjkoshy		}
444147191Sjkoshy
445147191Sjkoshy	KASSERT(po->po_curbuf != NULL,
446186127Sjkoshy	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
447147191Sjkoshy
448147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base &&
449147191Sjkoshy	    po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
450186127Sjkoshy	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
451147191Sjkoshy		__LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
452147191Sjkoshy		po->po_curbuf->plb_fence));
453147191Sjkoshy
454147867Sjkoshy	oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
455147191Sjkoshy	newptr = oldptr + length;
456147191Sjkoshy
457147867Sjkoshy	KASSERT(oldptr != (uintptr_t) NULL,
458186127Sjkoshy	    ("[pmclog,%d] po=%p Null log buffer pointer", __LINE__, po));
459147191Sjkoshy
460147191Sjkoshy	/*
461147191Sjkoshy	 * If we have space in the current buffer, return a pointer to
462147191Sjkoshy	 * available space with the PO structure locked.
463147191Sjkoshy	 */
464147867Sjkoshy	if (newptr <= (uintptr_t) po->po_curbuf->plb_fence) {
465147867Sjkoshy		po->po_curbuf->plb_ptr = (char *) newptr;
466147191Sjkoshy		goto done;
467147191Sjkoshy	}
468147191Sjkoshy
469147867Sjkoshy	/*
470147867Sjkoshy	 * Otherwise, schedule the current buffer for output and get a
471147867Sjkoshy	 * fresh buffer.
472147867Sjkoshy	 */
473147191Sjkoshy	pmclog_schedule_io(po);
474147191Sjkoshy
475147191Sjkoshy	if (pmclog_get_buffer(po) != 0) {
476147191Sjkoshy		mtx_unlock_spin(&po->po_mtx);
477186127Sjkoshy		return (NULL);
478147191Sjkoshy	}
479147191Sjkoshy
480147191Sjkoshy	KASSERT(po->po_curbuf != NULL,
481186127Sjkoshy	    ("[pmclog,%d] po=%p no current buffer", __LINE__, po));
482147191Sjkoshy
483147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr != NULL,
484186127Sjkoshy	    ("[pmclog,%d] null return from pmc_get_log_buffer", __LINE__));
485147191Sjkoshy
486147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr == po->po_curbuf->plb_base &&
487147191Sjkoshy	    po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
488186127Sjkoshy	    ("[pmclog,%d] po=%p buffer invariants: ptr=%p base=%p fence=%p",
489147191Sjkoshy		__LINE__, po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base,
490147191Sjkoshy		po->po_curbuf->plb_fence));
491147191Sjkoshy
492147867Sjkoshy	oldptr = (uintptr_t) po->po_curbuf->plb_ptr;
493147191Sjkoshy
494147191Sjkoshy done:
495147867Sjkoshy	lh = (uint32_t *) oldptr;
496147867Sjkoshy	lh++;				/* skip header */
497147867Sjkoshy	getnanotime(&ts);		/* fill in the timestamp */
498147191Sjkoshy	*lh++ = ts.tv_sec & 0xFFFFFFFF;
499147191Sjkoshy	*lh++ = ts.tv_nsec & 0xFFFFFFF;
500186127Sjkoshy	return ((uint32_t *) oldptr);
501147191Sjkoshy}
502147191Sjkoshy
503147191Sjkoshy/*
504147191Sjkoshy * Schedule an I/O.
505147191Sjkoshy *
506147191Sjkoshy * Transfer the current buffer to the helper kthread.
507147191Sjkoshy */
508147191Sjkoshy
509147191Sjkoshystatic void
510147191Sjkoshypmclog_schedule_io(struct pmc_owner *po)
511147191Sjkoshy{
512147191Sjkoshy	KASSERT(po->po_curbuf != NULL,
513186127Sjkoshy	    ("[pmclog,%d] schedule_io with null buffer po=%p", __LINE__, po));
514147191Sjkoshy
515147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr >= po->po_curbuf->plb_base,
516186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p base=%p", __LINE__,
517147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_base));
518147191Sjkoshy	KASSERT(po->po_curbuf->plb_ptr <= po->po_curbuf->plb_fence,
519186127Sjkoshy	    ("[pmclog,%d] buffer invariants po=%p ptr=%p fenc=%p", __LINE__,
520147191Sjkoshy		po, po->po_curbuf->plb_ptr, po->po_curbuf->plb_fence));
521147191Sjkoshy
522283884Sjhb	PMCDBG1(LOG,SIO, 1, "po=%p", po);
523147191Sjkoshy
524147191Sjkoshy	mtx_assert(&po->po_mtx, MA_OWNED);
525147191Sjkoshy
526147191Sjkoshy	/*
527147191Sjkoshy	 * Add the current buffer to the tail of the buffer list and
528147191Sjkoshy	 * wakeup the helper.
529147191Sjkoshy	 */
530147191Sjkoshy	TAILQ_INSERT_TAIL(&po->po_logbuffers, po->po_curbuf, plb_next);
531147191Sjkoshy	po->po_curbuf = NULL;
532147191Sjkoshy	wakeup_one(po);
533147191Sjkoshy}
534147191Sjkoshy
535147191Sjkoshy/*
536147191Sjkoshy * Stop the helper kthread.
537147191Sjkoshy */
538147191Sjkoshy
539147191Sjkoshystatic void
540147191Sjkoshypmclog_stop_kthread(struct pmc_owner *po)
541147191Sjkoshy{
542147191Sjkoshy	/*
543199763Sfabient	 * Close the file to force the thread out of fo_write,
544199763Sfabient	 * unset flag, wakeup the helper thread,
545147191Sjkoshy	 * wait for it to exit
546147191Sjkoshy	 */
547147191Sjkoshy
548199763Sfabient	if (po->po_file != NULL)
549199763Sfabient		fo_close(po->po_file, curthread);
550199763Sfabient
551199763Sfabient	mtx_lock(&pmc_kthread_mtx);
552147191Sjkoshy	po->po_flags &= ~PMC_PO_OWNS_LOGFILE;
553147191Sjkoshy	wakeup_one(po);
554147191Sjkoshy	if (po->po_kthread)
555147867Sjkoshy		msleep(po->po_kthread, &pmc_kthread_mtx, PPAUSE, "pmckstp", 0);
556199763Sfabient	mtx_unlock(&pmc_kthread_mtx);
557147191Sjkoshy}
558147191Sjkoshy
559147191Sjkoshy/*
560147191Sjkoshy * Public functions
561147191Sjkoshy */
562147191Sjkoshy
563147191Sjkoshy/*
564147191Sjkoshy * Configure a log file for pmc owner 'po'.
565147191Sjkoshy *
566147191Sjkoshy * Parameter 'logfd' is a file handle referencing an open file in the
567147191Sjkoshy * owner process.  This file needs to have been opened for writing.
568147191Sjkoshy */
569147191Sjkoshy
570147191Sjkoshyint
571185363Sjkoshypmclog_configure_log(struct pmc_mdep *md, struct pmc_owner *po, int logfd)
572147191Sjkoshy{
573147191Sjkoshy	int error;
574147191Sjkoshy	struct proc *p;
575255219Spjd	cap_rights_t rights;
576195005Sattilio	/*
577195005Sattilio	 * As long as it is possible to get a LOR between pmc_sx lock and
578195005Sattilio	 * proctree/allproc sx locks used for adding a new process, assure
579195005Sattilio	 * the former is not held here.
580195005Sattilio	 */
581195005Sattilio	sx_assert(&pmc_sx, SA_UNLOCKED);
582283884Sjhb	PMCDBG2(LOG,CFG,1, "config po=%p logfd=%d", po, logfd);
583147191Sjkoshy
584147191Sjkoshy	p = po->po_owner;
585147191Sjkoshy
586147191Sjkoshy	/* return EBUSY if a log file was already present */
587147191Sjkoshy	if (po->po_flags & PMC_PO_OWNS_LOGFILE)
588186127Sjkoshy		return (EBUSY);
589147191Sjkoshy
590147191Sjkoshy	KASSERT(po->po_kthread == NULL,
591186127Sjkoshy	    ("[pmclog,%d] po=%p kthread (%p) already present", __LINE__, po,
592147191Sjkoshy		po->po_kthread));
593147191Sjkoshy	KASSERT(po->po_file == NULL,
594186127Sjkoshy	    ("[pmclog,%d] po=%p file (%p) already present", __LINE__, po,
595147191Sjkoshy		po->po_file));
596147191Sjkoshy
597147191Sjkoshy	/* get a reference to the file state */
598255219Spjd	error = fget_write(curthread, logfd,
599255219Spjd	    cap_rights_init(&rights, CAP_WRITE), &po->po_file);
600147191Sjkoshy	if (error)
601147191Sjkoshy		goto error;
602147191Sjkoshy
603147191Sjkoshy	/* mark process as owning a log file */
604147191Sjkoshy	po->po_flags |= PMC_PO_OWNS_LOGFILE;
605172836Sjulian	error = kproc_create(pmclog_loop, po, &po->po_kthread,
606147191Sjkoshy	    RFHIGHPID, 0, "hwpmc: proc(%d)", p->p_pid);
607147191Sjkoshy	if (error)
608147191Sjkoshy		goto error;
609147191Sjkoshy
610147191Sjkoshy	/* mark process as using HWPMCs */
611147191Sjkoshy	PROC_LOCK(p);
612147191Sjkoshy	p->p_flag |= P_HWPMC;
613147191Sjkoshy	PROC_UNLOCK(p);
614147191Sjkoshy
615147191Sjkoshy	/* create a log initialization entry */
616147191Sjkoshy	PMCLOG_RESERVE_WITH_ERROR(po, INITIALIZE,
617147191Sjkoshy	    sizeof(struct pmclog_initialize));
618147191Sjkoshy	PMCLOG_EMIT32(PMC_VERSION);
619147191Sjkoshy	PMCLOG_EMIT32(md->pmd_cputype);
620147191Sjkoshy	PMCLOG_DESPATCH(po);
621147191Sjkoshy
622186127Sjkoshy	return (0);
623147191Sjkoshy
624147191Sjkoshy error:
625147191Sjkoshy	/* shutdown the thread */
626147191Sjkoshy	if (po->po_kthread)
627147191Sjkoshy		pmclog_stop_kthread(po);
628147191Sjkoshy
629186127Sjkoshy	KASSERT(po->po_kthread == NULL, ("[pmclog,%d] po=%p kthread not "
630186127Sjkoshy	    "stopped", __LINE__, po));
631147191Sjkoshy
632147191Sjkoshy	if (po->po_file)
633147191Sjkoshy		(void) fdrop(po->po_file, curthread);
634147191Sjkoshy	po->po_file  = NULL;	/* clear file and error state */
635147191Sjkoshy	po->po_error = 0;
636147191Sjkoshy
637186127Sjkoshy	return (error);
638147191Sjkoshy}
639147191Sjkoshy
640147191Sjkoshy
641147191Sjkoshy/*
642147191Sjkoshy * De-configure a log file.  This will throw away any buffers queued
643147191Sjkoshy * for this owner process.
644147191Sjkoshy */
645147191Sjkoshy
646147191Sjkoshyint
647147191Sjkoshypmclog_deconfigure_log(struct pmc_owner *po)
648147191Sjkoshy{
649147191Sjkoshy	int error;
650147191Sjkoshy	struct pmclog_buffer *lb;
651147191Sjkoshy
652283884Sjhb	PMCDBG1(LOG,CFG,1, "de-config po=%p", po);
653147191Sjkoshy
654147191Sjkoshy	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0)
655186127Sjkoshy		return (EINVAL);
656147191Sjkoshy
657156466Sjkoshy	KASSERT(po->po_sscount == 0,
658186127Sjkoshy	    ("[pmclog,%d] po=%p still owning SS PMCs", __LINE__, po));
659147191Sjkoshy	KASSERT(po->po_file != NULL,
660186127Sjkoshy	    ("[pmclog,%d] po=%p no log file", __LINE__, po));
661147191Sjkoshy
662147191Sjkoshy	/* stop the kthread, this will reset the 'OWNS_LOGFILE' flag */
663233544Sfabient	pmclog_stop_kthread(po);
664147191Sjkoshy
665147191Sjkoshy	KASSERT(po->po_kthread == NULL,
666186127Sjkoshy	    ("[pmclog,%d] po=%p kthread not stopped", __LINE__, po));
667147191Sjkoshy
668147191Sjkoshy	/* return all queued log buffers to the global pool */
669147191Sjkoshy	while ((lb = TAILQ_FIRST(&po->po_logbuffers)) != NULL) {
670147191Sjkoshy		TAILQ_REMOVE(&po->po_logbuffers, lb, plb_next);
671147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
672147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
673147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
674147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
675147191Sjkoshy	}
676147191Sjkoshy
677147191Sjkoshy	/* return the 'current' buffer to the global pool */
678147191Sjkoshy	if ((lb = po->po_curbuf) != NULL) {
679147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(lb);
680147191Sjkoshy		mtx_lock_spin(&pmc_bufferlist_mtx);
681147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, lb, plb_next);
682147191Sjkoshy		mtx_unlock_spin(&pmc_bufferlist_mtx);
683147191Sjkoshy	}
684147191Sjkoshy
685147191Sjkoshy	/* drop a reference to the fd */
686147191Sjkoshy	error = fdrop(po->po_file, curthread);
687147191Sjkoshy	po->po_file  = NULL;
688147191Sjkoshy	po->po_error = 0;
689147191Sjkoshy
690186127Sjkoshy	return (error);
691147191Sjkoshy}
692147191Sjkoshy
693147191Sjkoshy/*
694147191Sjkoshy * Flush a process' log buffer.
695147191Sjkoshy */
696147191Sjkoshy
697147191Sjkoshyint
698147191Sjkoshypmclog_flush(struct pmc_owner *po)
699147191Sjkoshy{
700204878Sfabient	int error;
701226514Sfabient	struct pmclog_buffer *lb;
702147191Sjkoshy
703283884Sjhb	PMCDBG1(LOG,FLS,1, "po=%p", po);
704147191Sjkoshy
705147191Sjkoshy	/*
706147191Sjkoshy	 * If there is a pending error recorded by the logger thread,
707147191Sjkoshy	 * return that.
708147191Sjkoshy	 */
709147191Sjkoshy	if (po->po_error)
710186127Sjkoshy		return (po->po_error);
711147191Sjkoshy
712147191Sjkoshy	error = 0;
713147191Sjkoshy
714147191Sjkoshy	/*
715147191Sjkoshy	 * Check that we do have an active log file.
716147191Sjkoshy	 */
717147191Sjkoshy	mtx_lock(&pmc_kthread_mtx);
718147191Sjkoshy	if ((po->po_flags & PMC_PO_OWNS_LOGFILE) == 0) {
719147191Sjkoshy		error = EINVAL;
720147191Sjkoshy		goto error;
721147191Sjkoshy	}
722147191Sjkoshy
723147191Sjkoshy	/*
724226514Sfabient	 * Schedule the current buffer if any and not empty.
725147191Sjkoshy	 */
726147191Sjkoshy	mtx_lock_spin(&po->po_mtx);
727226514Sfabient	lb = po->po_curbuf;
728226514Sfabient	if (lb && lb->plb_ptr != lb->plb_base) {
729226514Sfabient		pmclog_schedule_io(po);
730226514Sfabient	} else
731226514Sfabient		error = ENOBUFS;
732226514Sfabient	mtx_unlock_spin(&po->po_mtx);
733226514Sfabient
734226514Sfabient error:
735226514Sfabient	mtx_unlock(&pmc_kthread_mtx);
736226514Sfabient
737226514Sfabient	return (error);
738226514Sfabient}
739226514Sfabient
740226514Sfabientint
741226514Sfabientpmclog_close(struct pmc_owner *po)
742226514Sfabient{
743226514Sfabient
744283884Sjhb	PMCDBG1(LOG,CLO,1, "po=%p", po);
745226514Sfabient
746226514Sfabient	mtx_lock(&pmc_kthread_mtx);
747226514Sfabient
748226514Sfabient	/*
749226514Sfabient	 * Schedule the current buffer.
750226514Sfabient	 */
751226514Sfabient	mtx_lock_spin(&po->po_mtx);
752147191Sjkoshy	if (po->po_curbuf)
753147191Sjkoshy		pmclog_schedule_io(po);
754226514Sfabient	else
755226514Sfabient		wakeup_one(po);
756147191Sjkoshy	mtx_unlock_spin(&po->po_mtx);
757147191Sjkoshy
758204878Sfabient	/*
759204878Sfabient	 * Initiate shutdown: no new data queued,
760204878Sfabient	 * thread will close file on last block.
761204878Sfabient	 */
762204878Sfabient	po->po_flags |= PMC_PO_SHUTDOWN;
763147191Sjkoshy
764147191Sjkoshy	mtx_unlock(&pmc_kthread_mtx);
765147191Sjkoshy
766226514Sfabient	return (0);
767147191Sjkoshy}
768147191Sjkoshy
769174395Sjkoshyvoid
770174395Sjkoshypmclog_process_callchain(struct pmc *pm, struct pmc_sample *ps)
771174395Sjkoshy{
772174395Sjkoshy	int n, recordlen;
773174395Sjkoshy	uint32_t flags;
774174395Sjkoshy	struct pmc_owner *po;
775147191Sjkoshy
776283884Sjhb	PMCDBG3(LOG,SAM,1,"pm=%p pid=%d n=%d", pm, ps->ps_pid,
777174395Sjkoshy	    ps->ps_nsamples);
778174395Sjkoshy
779174395Sjkoshy	recordlen = offsetof(struct pmclog_callchain, pl_pc) +
780174395Sjkoshy	    ps->ps_nsamples * sizeof(uintfptr_t);
781174395Sjkoshy	po = pm->pm_owner;
782174395Sjkoshy	flags = PMC_CALLCHAIN_TO_CPUFLAGS(ps->ps_cpu,ps->ps_flags);
783174395Sjkoshy	PMCLOG_RESERVE(po, CALLCHAIN, recordlen);
784174395Sjkoshy	PMCLOG_EMIT32(ps->ps_pid);
785174395Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
786174395Sjkoshy	PMCLOG_EMIT32(flags);
787174395Sjkoshy	for (n = 0; n < ps->ps_nsamples; n++)
788174395Sjkoshy		PMCLOG_EMITADDR(ps->ps_pc[n]);
789174395Sjkoshy	PMCLOG_DESPATCH(po);
790174395Sjkoshy}
791174395Sjkoshy
792147191Sjkoshyvoid
793147191Sjkoshypmclog_process_closelog(struct pmc_owner *po)
794147191Sjkoshy{
795147191Sjkoshy	PMCLOG_RESERVE(po,CLOSELOG,sizeof(struct pmclog_closelog));
796147191Sjkoshy	PMCLOG_DESPATCH(po);
797147191Sjkoshy}
798147191Sjkoshy
799147191Sjkoshyvoid
800147191Sjkoshypmclog_process_dropnotify(struct pmc_owner *po)
801147191Sjkoshy{
802147191Sjkoshy	PMCLOG_RESERVE(po,DROPNOTIFY,sizeof(struct pmclog_dropnotify));
803147191Sjkoshy	PMCLOG_DESPATCH(po);
804147191Sjkoshy}
805147191Sjkoshy
806147191Sjkoshyvoid
807157144Sjkoshypmclog_process_map_in(struct pmc_owner *po, pid_t pid, uintfptr_t start,
808157144Sjkoshy    const char *path)
809147191Sjkoshy{
810147191Sjkoshy	int pathlen, recordlen;
811147191Sjkoshy
812157144Sjkoshy	KASSERT(path != NULL, ("[pmclog,%d] map-in, null path", __LINE__));
813157144Sjkoshy
814147191Sjkoshy	pathlen = strlen(path) + 1;	/* #bytes for path name */
815157144Sjkoshy	recordlen = offsetof(struct pmclog_map_in, pl_pathname) +
816147191Sjkoshy	    pathlen;
817147191Sjkoshy
818157144Sjkoshy	PMCLOG_RESERVE(po, MAP_IN, recordlen);
819157144Sjkoshy	PMCLOG_EMIT32(pid);
820147191Sjkoshy	PMCLOG_EMITADDR(start);
821147191Sjkoshy	PMCLOG_EMITSTRING(path,pathlen);
822147191Sjkoshy	PMCLOG_DESPATCH(po);
823147191Sjkoshy}
824147191Sjkoshy
825157144Sjkoshyvoid
826157144Sjkoshypmclog_process_map_out(struct pmc_owner *po, pid_t pid, uintfptr_t start,
827157144Sjkoshy    uintfptr_t end)
828157144Sjkoshy{
829157144Sjkoshy	KASSERT(start <= end, ("[pmclog,%d] start > end", __LINE__));
830147191Sjkoshy
831157144Sjkoshy	PMCLOG_RESERVE(po, MAP_OUT, sizeof(struct pmclog_map_out));
832157144Sjkoshy	PMCLOG_EMIT32(pid);
833157144Sjkoshy	PMCLOG_EMITADDR(start);
834157144Sjkoshy	PMCLOG_EMITADDR(end);
835157144Sjkoshy	PMCLOG_DESPATCH(po);
836157144Sjkoshy}
837157144Sjkoshy
838147191Sjkoshyvoid
839147191Sjkoshypmclog_process_pmcallocate(struct pmc *pm)
840147191Sjkoshy{
841147191Sjkoshy	struct pmc_owner *po;
842233628Sfabient	struct pmc_soft *ps;
843147191Sjkoshy
844147191Sjkoshy	po = pm->pm_owner;
845147191Sjkoshy
846283884Sjhb	PMCDBG1(LOG,ALL,1, "pm=%p", pm);
847147191Sjkoshy
848233628Sfabient	if (PMC_TO_CLASS(pm) == PMC_CLASS_SOFT) {
849233628Sfabient		PMCLOG_RESERVE(po, PMCALLOCATEDYN,
850233628Sfabient		    sizeof(struct pmclog_pmcallocatedyn));
851233628Sfabient		PMCLOG_EMIT32(pm->pm_id);
852233628Sfabient		PMCLOG_EMIT32(pm->pm_event);
853233628Sfabient		PMCLOG_EMIT32(pm->pm_flags);
854233628Sfabient		ps = pmc_soft_ev_acquire(pm->pm_event);
855233628Sfabient		if (ps != NULL)
856233628Sfabient			PMCLOG_EMITSTRING(ps->ps_ev.pm_ev_name,PMC_NAME_MAX);
857233628Sfabient		else
858233628Sfabient			PMCLOG_EMITNULLSTRING(PMC_NAME_MAX);
859233628Sfabient		pmc_soft_ev_release(ps);
860233628Sfabient		PMCLOG_DESPATCH(po);
861233628Sfabient	} else {
862233628Sfabient		PMCLOG_RESERVE(po, PMCALLOCATE,
863233628Sfabient		    sizeof(struct pmclog_pmcallocate));
864233628Sfabient		PMCLOG_EMIT32(pm->pm_id);
865233628Sfabient		PMCLOG_EMIT32(pm->pm_event);
866233628Sfabient		PMCLOG_EMIT32(pm->pm_flags);
867233628Sfabient		PMCLOG_DESPATCH(po);
868233628Sfabient	}
869147191Sjkoshy}
870147191Sjkoshy
871147191Sjkoshyvoid
872147191Sjkoshypmclog_process_pmcattach(struct pmc *pm, pid_t pid, char *path)
873147191Sjkoshy{
874147191Sjkoshy	int pathlen, recordlen;
875147191Sjkoshy	struct pmc_owner *po;
876147191Sjkoshy
877283884Sjhb	PMCDBG2(LOG,ATT,1,"pm=%p pid=%d", pm, pid);
878147191Sjkoshy
879147191Sjkoshy	po = pm->pm_owner;
880147191Sjkoshy
881147191Sjkoshy	pathlen = strlen(path) + 1;	/* #bytes for the string */
882147191Sjkoshy	recordlen = offsetof(struct pmclog_pmcattach, pl_pathname) + pathlen;
883147191Sjkoshy
884147191Sjkoshy	PMCLOG_RESERVE(po, PMCATTACH, recordlen);
885147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
886147191Sjkoshy	PMCLOG_EMIT32(pid);
887147191Sjkoshy	PMCLOG_EMITSTRING(path, pathlen);
888147191Sjkoshy	PMCLOG_DESPATCH(po);
889147191Sjkoshy}
890147191Sjkoshy
891147191Sjkoshyvoid
892147191Sjkoshypmclog_process_pmcdetach(struct pmc *pm, pid_t pid)
893147191Sjkoshy{
894147191Sjkoshy	struct pmc_owner *po;
895147191Sjkoshy
896283884Sjhb	PMCDBG2(LOG,ATT,1,"!pm=%p pid=%d", pm, pid);
897147191Sjkoshy
898147191Sjkoshy	po = pm->pm_owner;
899147191Sjkoshy
900147191Sjkoshy	PMCLOG_RESERVE(po, PMCDETACH, sizeof(struct pmclog_pmcdetach));
901147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
902147191Sjkoshy	PMCLOG_EMIT32(pid);
903147191Sjkoshy	PMCLOG_DESPATCH(po);
904147191Sjkoshy}
905147191Sjkoshy
906147191Sjkoshy/*
907147191Sjkoshy * Log a context switch event to the log file.
908147191Sjkoshy */
909147191Sjkoshy
910147191Sjkoshyvoid
911147191Sjkoshypmclog_process_proccsw(struct pmc *pm, struct pmc_process *pp, pmc_value_t v)
912147191Sjkoshy{
913147191Sjkoshy	struct pmc_owner *po;
914147191Sjkoshy
915147191Sjkoshy	KASSERT(pm->pm_flags & PMC_F_LOG_PROCCSW,
916147191Sjkoshy	    ("[pmclog,%d] log-process-csw called gratuitously", __LINE__));
917147191Sjkoshy
918283884Sjhb	PMCDBG3(LOG,SWO,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
919147191Sjkoshy	    v);
920147191Sjkoshy
921147191Sjkoshy	po = pm->pm_owner;
922147191Sjkoshy
923147191Sjkoshy	PMCLOG_RESERVE(po, PROCCSW, sizeof(struct pmclog_proccsw));
924147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
925147191Sjkoshy	PMCLOG_EMIT64(v);
926147191Sjkoshy	PMCLOG_EMIT32(pp->pp_proc->p_pid);
927147191Sjkoshy	PMCLOG_DESPATCH(po);
928147191Sjkoshy}
929147191Sjkoshy
930147191Sjkoshyvoid
931147708Sjkoshypmclog_process_procexec(struct pmc_owner *po, pmc_id_t pmid, pid_t pid,
932147708Sjkoshy    uintfptr_t startaddr, char *path)
933147191Sjkoshy{
934147191Sjkoshy	int pathlen, recordlen;
935147191Sjkoshy
936283884Sjhb	PMCDBG3(LOG,EXC,1,"po=%p pid=%d path=\"%s\"", po, pid, path);
937147191Sjkoshy
938147191Sjkoshy	pathlen   = strlen(path) + 1;	/* #bytes for the path */
939147191Sjkoshy	recordlen = offsetof(struct pmclog_procexec, pl_pathname) + pathlen;
940147191Sjkoshy
941147191Sjkoshy	PMCLOG_RESERVE(po, PROCEXEC, recordlen);
942147191Sjkoshy	PMCLOG_EMIT32(pid);
943147708Sjkoshy	PMCLOG_EMITADDR(startaddr);
944147708Sjkoshy	PMCLOG_EMIT32(pmid);
945147191Sjkoshy	PMCLOG_EMITSTRING(path,pathlen);
946147191Sjkoshy	PMCLOG_DESPATCH(po);
947147191Sjkoshy}
948147191Sjkoshy
949147191Sjkoshy/*
950147191Sjkoshy * Log a process exit event (and accumulated pmc value) to the log file.
951147191Sjkoshy */
952147191Sjkoshy
953147191Sjkoshyvoid
954147191Sjkoshypmclog_process_procexit(struct pmc *pm, struct pmc_process *pp)
955147191Sjkoshy{
956147191Sjkoshy	int ri;
957147191Sjkoshy	struct pmc_owner *po;
958147191Sjkoshy
959147191Sjkoshy	ri = PMC_TO_ROWINDEX(pm);
960283884Sjhb	PMCDBG3(LOG,EXT,1,"pm=%p pid=%d v=%jx", pm, pp->pp_proc->p_pid,
961147191Sjkoshy	    pp->pp_pmcs[ri].pp_pmcval);
962147191Sjkoshy
963147191Sjkoshy	po = pm->pm_owner;
964147191Sjkoshy
965147191Sjkoshy	PMCLOG_RESERVE(po, PROCEXIT, sizeof(struct pmclog_procexit));
966147191Sjkoshy	PMCLOG_EMIT32(pm->pm_id);
967147191Sjkoshy	PMCLOG_EMIT64(pp->pp_pmcs[ri].pp_pmcval);
968147191Sjkoshy	PMCLOG_EMIT32(pp->pp_proc->p_pid);
969147191Sjkoshy	PMCLOG_DESPATCH(po);
970147191Sjkoshy}
971147191Sjkoshy
972147191Sjkoshy/*
973147191Sjkoshy * Log a fork event.
974147191Sjkoshy */
975147191Sjkoshy
976147191Sjkoshyvoid
977147191Sjkoshypmclog_process_procfork(struct pmc_owner *po, pid_t oldpid, pid_t newpid)
978147191Sjkoshy{
979147191Sjkoshy	PMCLOG_RESERVE(po, PROCFORK, sizeof(struct pmclog_procfork));
980147191Sjkoshy	PMCLOG_EMIT32(oldpid);
981147191Sjkoshy	PMCLOG_EMIT32(newpid);
982147191Sjkoshy	PMCLOG_DESPATCH(po);
983147191Sjkoshy}
984147191Sjkoshy
985147191Sjkoshy/*
986147191Sjkoshy * Log a process exit event of the form suitable for system-wide PMCs.
987147191Sjkoshy */
988147191Sjkoshy
989147191Sjkoshyvoid
990147191Sjkoshypmclog_process_sysexit(struct pmc_owner *po, pid_t pid)
991147191Sjkoshy{
992147191Sjkoshy	PMCLOG_RESERVE(po, SYSEXIT, sizeof(struct pmclog_sysexit));
993147191Sjkoshy	PMCLOG_EMIT32(pid);
994147191Sjkoshy	PMCLOG_DESPATCH(po);
995147191Sjkoshy}
996147191Sjkoshy
997147191Sjkoshy/*
998147191Sjkoshy * Write a user log entry.
999147191Sjkoshy */
1000147191Sjkoshy
1001147191Sjkoshyint
1002147191Sjkoshypmclog_process_userlog(struct pmc_owner *po, struct pmc_op_writelog *wl)
1003147191Sjkoshy{
1004147191Sjkoshy	int error;
1005147191Sjkoshy
1006283884Sjhb	PMCDBG2(LOG,WRI,1, "writelog po=%p ud=0x%x", po, wl->pm_userdata);
1007147191Sjkoshy
1008147191Sjkoshy	error = 0;
1009147191Sjkoshy
1010147191Sjkoshy	PMCLOG_RESERVE_WITH_ERROR(po, USERDATA,
1011147191Sjkoshy	    sizeof(struct pmclog_userdata));
1012147191Sjkoshy	PMCLOG_EMIT32(wl->pm_userdata);
1013147191Sjkoshy	PMCLOG_DESPATCH(po);
1014147191Sjkoshy
1015147191Sjkoshy error:
1016186127Sjkoshy	return (error);
1017147191Sjkoshy}
1018147191Sjkoshy
1019147191Sjkoshy/*
1020147191Sjkoshy * Initialization.
1021147191Sjkoshy *
1022147191Sjkoshy * Create a pool of log buffers and initialize mutexes.
1023147191Sjkoshy */
1024147191Sjkoshy
1025147191Sjkoshyvoid
1026147191Sjkoshypmclog_initialize()
1027147191Sjkoshy{
1028147191Sjkoshy	int n;
1029147191Sjkoshy	struct pmclog_buffer *plb;
1030147191Sjkoshy
1031147191Sjkoshy	if (pmclog_buffer_size <= 0) {
1032186127Sjkoshy		(void) printf("hwpmc: tunable logbuffersize=%d must be "
1033186127Sjkoshy		    "greater than zero.\n", pmclog_buffer_size);
1034147191Sjkoshy		pmclog_buffer_size = PMC_LOG_BUFFER_SIZE;
1035147191Sjkoshy	}
1036147191Sjkoshy
1037147191Sjkoshy	if (pmc_nlogbuffers <= 0) {
1038147191Sjkoshy		(void) printf("hwpmc: tunable nlogbuffers=%d must be greater "
1039147191Sjkoshy		    "than zero.\n", pmc_nlogbuffers);
1040147191Sjkoshy		pmc_nlogbuffers = PMC_NLOGBUFFERS;
1041147191Sjkoshy	}
1042147191Sjkoshy
1043147191Sjkoshy	/* create global pool of log buffers */
1044147191Sjkoshy	for (n = 0; n < pmc_nlogbuffers; n++) {
1045184801Sjkoshy		plb = malloc(1024 * pmclog_buffer_size, M_PMC,
1046184801Sjkoshy		    M_WAITOK|M_ZERO);
1047147191Sjkoshy		PMCLOG_INIT_BUFFER_DESCRIPTOR(plb);
1048147191Sjkoshy		TAILQ_INSERT_HEAD(&pmc_bufferlist, plb, plb_next);
1049147191Sjkoshy	}
1050168856Sjkoshy	mtx_init(&pmc_bufferlist_mtx, "pmc-buffer-list", "pmc-leaf",
1051168856Sjkoshy	    MTX_SPIN);
1052168856Sjkoshy	mtx_init(&pmc_kthread_mtx, "pmc-kthread", "pmc-sleep", MTX_DEF);
1053147191Sjkoshy}
1054147191Sjkoshy
1055147191Sjkoshy/*
1056147191Sjkoshy * Shutdown logging.
1057147191Sjkoshy *
1058147191Sjkoshy * Destroy mutexes and release memory back the to free pool.
1059147191Sjkoshy */
1060147191Sjkoshy
1061147191Sjkoshyvoid
1062147191Sjkoshypmclog_shutdown()
1063147191Sjkoshy{
1064147191Sjkoshy	struct pmclog_buffer *plb;
1065147191Sjkoshy
1066147191Sjkoshy	mtx_destroy(&pmc_kthread_mtx);
1067147191Sjkoshy	mtx_destroy(&pmc_bufferlist_mtx);
1068147191Sjkoshy
1069147191Sjkoshy	while ((plb = TAILQ_FIRST(&pmc_bufferlist)) != NULL) {
1070147191Sjkoshy		TAILQ_REMOVE(&pmc_bufferlist, plb, plb_next);
1071184205Sdes		free(plb, M_PMC);
1072147191Sjkoshy	}
1073147191Sjkoshy}
1074