165556Sjasone/*-
265556Sjasone * Copyright (c) 1996 Berkeley Software Design, Inc. All rights reserved.
365556Sjasone *
465556Sjasone * Redistribution and use in source and binary forms, with or without
565556Sjasone * modification, are permitted provided that the following conditions
665556Sjasone * are met:
765556Sjasone * 1. Redistributions of source code must retain the above copyright
865556Sjasone *    notice, this list of conditions and the following disclaimer.
965556Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1065556Sjasone *    notice, this list of conditions and the following disclaimer in the
1165556Sjasone *    documentation and/or other materials provided with the distribution.
1265556Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or
1365556Sjasone *    promote products derived from this software without specific prior
1465556Sjasone *    written permission.
1565556Sjasone *
1665556Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
1765556Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1865556Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1965556Sjasone * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
2065556Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2165556Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2265556Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2365556Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2465556Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2565556Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2665556Sjasone * SUCH DAMAGE.
2765556Sjasone *
2865556Sjasone *	from BSDI $Id: ktr.h,v 1.10.2.7 2000/03/16 21:44:42 cp Exp $
2965556Sjasone * $FreeBSD$
3065556Sjasone */
3165556Sjasone
3265556Sjasone/*
3365556Sjasone *	Wraparound kernel trace buffer support.
3465556Sjasone */
3565556Sjasone
3665556Sjasone#ifndef _SYS_KTR_H_
3765556Sjasone#define _SYS_KTR_H_
3865556Sjasone
3965556Sjasone/*
4065556Sjasone * Trace classes
41154790Sjhb *
42154790Sjhb * Two of the trace classes (KTR_DEV and KTR_SUBSYS) are special in that
43154790Sjhb * they are really placeholders so that indvidual drivers and subsystems
44154790Sjhb * can map their internal tracing to the general class when they wish to
45154790Sjhb * have tracing enabled and map it to 0 when they don't.
4665556Sjasone */
4765556Sjasone#define	KTR_GEN		0x00000001		/* General (TR) */
4865556Sjasone#define	KTR_NET		0x00000002		/* Network */
4965556Sjasone#define	KTR_DEV		0x00000004		/* Device driver */
5065556Sjasone#define	KTR_LOCK	0x00000008		/* MP locking */
5165556Sjasone#define	KTR_SMP		0x00000010		/* MP general */
52154790Sjhb#define	KTR_SUBSYS	0x00000020		/* Subsystem. */
53153493Snjl#define	KTR_PMAP	0x00000040		/* Pmap tracing */
54153493Snjl#define	KTR_MALLOC	0x00000080		/* Malloc tracing */
5565556Sjasone#define	KTR_TRAP	0x00000100		/* Trap processing */
5665556Sjasone#define	KTR_INTR	0x00000200		/* Interrupt tracing */
57153493Snjl#define	KTR_SIG		0x00000400		/* Signal processing */
58191653Sbms#define	KTR_SPARE2	0x00000800		/* XXX Used by cxgb */
5965556Sjasone#define	KTR_PROC	0x00001000		/* Process scheduling */
6065556Sjasone#define	KTR_SYSC	0x00002000		/* System call */
6165556Sjasone#define	KTR_INIT	0x00004000		/* System initialization */
62191653Sbms#define	KTR_SPARE3	0x00008000		/* XXX Used by cxgb */
63191653Sbms#define	KTR_SPARE4	0x00010000		/* XXX Used by cxgb */
64112109Sjhb#define	KTR_EVH		0x00020000		/* Eventhandler */
65153493Snjl#define	KTR_VFS		0x00040000		/* VFS events */
66153493Snjl#define	KTR_VOP		0x00080000		/* Auto-generated vop events */
67153493Snjl#define	KTR_VM		0x00100000		/* The virtual memory system */
68191653Sbms#define	KTR_INET	0x00200000		/* IPv4 stack */
6972376Sjake#define	KTR_RUNQ	0x00400000		/* Run queue */
70102450Siedowse#define	KTR_CONTENTION	0x00800000		/* Lock contention */
71133228Srwatson#define	KTR_UMA		0x01000000		/* UMA slab allocator */
72133228Srwatson#define	KTR_CALLOUT	0x02000000		/* Callouts and timeouts */
73136755Srwatson#define	KTR_GEOM	0x04000000		/* GEOM I/O events */
74136805Srwatson#define	KTR_BUSDMA	0x08000000		/* busdma(9) events */
75191653Sbms#define	KTR_INET6	0x10000000		/* IPv6 stack */
76139314Sjeff#define	KTR_SCHED	0x20000000		/* Machine parsed sched info. */
77140696Sjeff#define	KTR_BUF		0x40000000		/* Buffer cache */
78284343Sjhb#define	KTR_PTRACE	0x80000000		/* Process debugging. */
79284343Sjhb#define	KTR_ALL		0xffffffff
8065556Sjasone
8165556Sjasone/* Trace classes to compile in */
82153174Smarius#ifdef KTR
8365556Sjasone#ifndef KTR_COMPILE
84149734Sglebius#define	KTR_COMPILE	(KTR_ALL)
8565556Sjasone#endif
86153174Smarius#else	/* !KTR */
87153174Smarius#undef KTR_COMPILE
88153174Smarius#define KTR_COMPILE 0
89153174Smarius#endif	/* KTR */
9065556Sjasone
9193503Sjake/*
9293503Sjake * Version number for ktr_entry struct.  Increment this when you break binary
9393503Sjake * compatibility.
9493503Sjake */
95147279Sjeff#define	KTR_VERSION	2
9665556Sjasone
9793503Sjake#define	KTR_PARMS	6
9865556Sjasone
9993503Sjake#ifndef LOCORE
10078784Sjhb
101222813Sattilio#include <sys/param.h>
102222813Sattilio#include <sys/_cpuset.h>
103222813Sattilio
10465556Sjasonestruct ktr_entry {
10593503Sjake	u_int64_t ktr_timestamp;
10678784Sjhb	int	ktr_cpu;
10765556Sjasone	int	ktr_line;
10893503Sjake	const	char *ktr_file;
10972761Sjhb	const	char *ktr_desc;
110147278Sjeff	struct	thread *ktr_thread;
11193503Sjake	u_long	ktr_parms[KTR_PARMS];
11265556Sjasone};
11365556Sjasone
114222813Sattilioextern cpuset_t ktr_cpumask;
11565556Sjasoneextern int ktr_mask;
11665556Sjasoneextern int ktr_entries;
11768420Sjhbextern int ktr_verbose;
11865556Sjasone
11965556Sjasoneextern volatile int ktr_idx;
120243046Sjeffextern struct ktr_entry *ktr_buf;
12165556Sjasone
12265556Sjasone#ifdef KTR
12365556Sjasone
12493503Sjakevoid	ktr_tracepoint(u_int mask, const char *file, int line,
12593503Sjake	    const char *format, u_long arg1, u_long arg2, u_long arg3,
12693503Sjake	    u_long arg4, u_long arg5, u_long arg6);
12765556Sjasone
12887592Sobrien#define CTR6(m, format, p1, p2, p3, p4, p5, p6) do {			\
12987592Sobrien	if (KTR_COMPILE & (m))						\
13093503Sjake		ktr_tracepoint((m), __FILE__, __LINE__, format,		\
13193573Sjhb		    (u_long)(p1), (u_long)(p2), (u_long)(p3),		\
13293573Sjhb		    (u_long)(p4), (u_long)(p5), (u_long)(p6));		\
13387592Sobrien	} while(0)
13487621Sobrien#define CTR0(m, format)			CTR6(m, format, 0, 0, 0, 0, 0, 0)
13587621Sobrien#define CTR1(m, format, p1)		CTR6(m, format, p1, 0, 0, 0, 0, 0)
13687621Sobrien#define	CTR2(m, format, p1, p2)		CTR6(m, format, p1, p2, 0, 0, 0, 0)
13787621Sobrien#define	CTR3(m, format, p1, p2, p3)	CTR6(m, format, p1, p2, p3, 0, 0, 0)
13887621Sobrien#define	CTR4(m, format, p1, p2, p3, p4)	CTR6(m, format, p1, p2, p3, p4, 0, 0)
13987621Sobrien#define	CTR5(m, format, p1, p2, p3, p4, p5)	CTR6(m, format, p1, p2, p3, p4, p5, 0)
14065556Sjasone#else	/* KTR */
141194578Srdivacky#define	CTR0(m, d)			(void)0
142194578Srdivacky#define	CTR1(m, d, p1)			(void)0
143194578Srdivacky#define	CTR2(m, d, p1, p2)		(void)0
144194578Srdivacky#define	CTR3(m, d, p1, p2, p3)		(void)0
145194578Srdivacky#define	CTR4(m, d, p1, p2, p3, p4)	(void)0
146194578Srdivacky#define	CTR5(m, d, p1, p2, p3, p4, p5)	(void)0
147194578Srdivacky#define	CTR6(m, d, p1, p2, p3, p4, p5, p6)	(void)0
14865556Sjasone#endif	/* KTR */
14965556Sjasone
15065556Sjasone#define	TR0(d)				CTR0(KTR_GEN, d)
15165556Sjasone#define	TR1(d, p1)			CTR1(KTR_GEN, d, p1)
15265556Sjasone#define	TR2(d, p1, p2)			CTR2(KTR_GEN, d, p1, p2)
15365556Sjasone#define	TR3(d, p1, p2, p3)		CTR3(KTR_GEN, d, p1, p2, p3)
15465556Sjasone#define	TR4(d, p1, p2, p3, p4)		CTR4(KTR_GEN, d, p1, p2, p3, p4)
15565556Sjasone#define	TR5(d, p1, p2, p3, p4, p5)	CTR5(KTR_GEN, d, p1, p2, p3, p4, p5)
15687957Sjhb#define	TR6(d, p1, p2, p3, p4, p5, p6)	CTR6(KTR_GEN, d, p1, p2, p3, p4, p5, p6)
15765556Sjasone
15865556Sjasone/*
159187357Sjeff * The event macros implement KTR graphic plotting facilities provided
160187357Sjeff * by src/tools/sched/schedgraph.py.  Three generic types of events are
161187357Sjeff * supported: states, counters, and points.
162187357Sjeff *
163187357Sjeff * m is the ktr class for ktr_mask.
164187357Sjeff * ident is the string identifier that owns the event (ie: "thread 10001")
165187357Sjeff * etype is the type of event to plot (state, counter, point)
166187357Sjeff * edat is the event specific data (state name, counter value, point name)
167187357Sjeff * up to four attributes may be supplied as a name, value pair of arguments.
168187357Sjeff *
169187357Sjeff * etype and attribute names must be string constants.  This minimizes the
170187357Sjeff * number of ktr slots required by construction the final format strings
171187357Sjeff * at compile time.  Both must also include a colon and format specifier
172187357Sjeff * (ie. "prio:%d", prio).  It is recommended that string arguments be
173187357Sjeff * contained within escaped quotes if they may contain ',' or ':' characters.
174187357Sjeff *
175187357Sjeff * The special attribute (KTR_ATTR_LINKED, ident) creates a reference to another
176187357Sjeff * id on the graph for easy traversal of related graph elements.
177187357Sjeff */
178187357Sjeff
179187357Sjeff#define	KTR_ATTR_LINKED	"linkedto:\"%s\""
180187357Sjeff#define	KTR_EFMT(egroup, ident, etype)					\
181187357Sjeff	    "KTRGRAPH group:\"" egroup "\", id:\"%s\", " etype ", attributes: "
182187357Sjeff
183187357Sjeff#define	KTR_EVENT0(m, egroup, ident, etype, edat)			\
184187357Sjeff	CTR2(m,	KTR_EFMT(egroup, ident, etype) "none", ident, edat)
185187357Sjeff#define	KTR_EVENT1(m, egroup, ident, etype, edat, a0, v0)		\
186187357Sjeff	CTR3(m, KTR_EFMT(egroup, ident, etype) a0, ident, edat, (v0))
187187357Sjeff#define	KTR_EVENT2(m, egroup, ident, etype, edat, a0, v0, a1, v1)	\
188187357Sjeff	CTR4(m, KTR_EFMT(egroup, ident, etype) a0 ", " a1,		\
189187357Sjeff	    ident, edat, (v0), (v1))
190187357Sjeff#define	KTR_EVENT3(m, egroup, ident, etype, edat, a0, v0, a1, v1, a2, v2)\
191187357Sjeff	CTR5(m,KTR_EFMT(egroup, ident, etype) a0 ", " a1 ", " a2,	\
192187357Sjeff	    ident, edat, (v0), (v1), (v2))
193187357Sjeff#define	KTR_EVENT4(m, egroup, ident, etype, edat,			\
194187357Sjeff	    a0, v0, a1, v1, a2, v2, a3, v3)				\
195210443Smav	CTR6(m,KTR_EFMT(egroup, ident, etype) a0 ", " a1 ", " a2 ", " a3,\
196187357Sjeff	     ident, edat, (v0), (v1), (v2), (v3))
197187357Sjeff
198187357Sjeff/*
199187357Sjeff * State functions graph state changes on an ident.
200187357Sjeff */
201187357Sjeff#define KTR_STATE0(m, egroup, ident, state)				\
202187357Sjeff	KTR_EVENT0(m, egroup, ident, "state:\"%s\"", state)
203187357Sjeff#define KTR_STATE1(m, egroup, ident, state, a0, v0)			\
204187357Sjeff	KTR_EVENT1(m, egroup, ident, "state:\"%s\"", state, a0, (v0))
205187357Sjeff#define KTR_STATE2(m, egroup, ident, state, a0, v0, a1, v1)		\
206187357Sjeff	KTR_EVENT2(m, egroup, ident, "state:\"%s\"", state, a0, (v0), a1, (v1))
207187357Sjeff#define KTR_STATE3(m, egroup, ident, state, a0, v0, a1, v1, a2, v2)	\
208187357Sjeff	KTR_EVENT3(m, egroup, ident, "state:\"%s\"",			\
209187357Sjeff	    state, a0, (v0), a1, (v1), a2, (v2))
210187357Sjeff#define KTR_STATE4(m, egroup, ident, state, a0, v0, a1, v1, a2, v2, a3, v3)\
211187357Sjeff	KTR_EVENT4(m, egroup, ident, "state:\"%s\"",			\
212187357Sjeff	    state, a0, (v0), a1, (v1), a2, (v2), a3, (v3))
213187357Sjeff
214187357Sjeff/*
215187357Sjeff * Counter functions graph counter values.  The counter id
216187357Sjeff * must not be intermixed with a state id.
217187357Sjeff */
218187357Sjeff#define	KTR_COUNTER0(m, egroup, ident, counter)				\
219187357Sjeff	KTR_EVENT0(m, egroup, ident, "counter:%d", counter)
220187357Sjeff#define	KTR_COUNTER1(m, egroup, ident, edat, a0, v0)			\
221187357Sjeff	KTR_EVENT1(m, egroup, ident, "counter:%d", counter, a0, (v0))
222187357Sjeff#define	KTR_COUNTER2(m, egroup, ident, counter, a0, v0, a1, v1)		\
223187357Sjeff	KTR_EVENT2(m, egroup, ident, "counter:%d", counter, a0, (v0), a1, (v1))
224187357Sjeff#define	KTR_COUNTER3(m, egroup, ident, counter, a0, v0, a1, v1, a2, v2)	\
225187357Sjeff	KTR_EVENT3(m, egroup, ident, "counter:%d",			\
226187357Sjeff	    counter, a0, (v0), a1, (v1), a2, (v2))
227187357Sjeff#define	KTR_COUNTER4(m, egroup, ident, counter, a0, v0, a1, v1, a2, v2, a3, v3)\
228187357Sjeff	KTR_EVENT4(m, egroup, ident, "counter:%d",			\
229187357Sjeff	    counter, a0, (v0), a1, (v1), a2, (v2), a3, (v3))
230187357Sjeff
231187357Sjeff/*
232187357Sjeff * Point functions plot points of interest on counter or state graphs.
233187357Sjeff */
234187357Sjeff#define	KTR_POINT0(m, egroup, ident, point)				\
235187357Sjeff	KTR_EVENT0(m, egroup, ident, "point:\"%s\"", point)
236187357Sjeff#define	KTR_POINT1(m, egroup, ident, point, a0, v0)			\
237187357Sjeff	KTR_EVENT1(m, egroup, ident, "point:\"%s\"", point, a0, (v0))
238187357Sjeff#define	KTR_POINT2(m, egroup, ident, point, a0, v0, a1, v1)		\
239187357Sjeff	KTR_EVENT2(m, egroup, ident, "point:\"%s\"", point, a0, (v0), a1, (v1))
240187357Sjeff#define	KTR_POINT3(m, egroup, ident, point, a0, v0, a1, v1, a2, v2)	\
241187357Sjeff	KTR_EVENT3(m, egroup, ident, "point:\"%s\"", point,		\
242187357Sjeff	    a0, (v0), a1, (v1), a2, (v2))
243187357Sjeff#define	KTR_POINT4(m, egroup, ident, point, a0, v0, a1, v1, a2, v2, a3, v3)\
244187357Sjeff	KTR_EVENT4(m, egroup, ident, "point:\"%s\"",			\
245187357Sjeff	    point, a0, (v0), a1, (v1), a2, (v2), a3, (v3))
246187357Sjeff
247187357Sjeff/*
248244445Sjeff * Start functions denote the start of a region of code or operation
249244445Sjeff * and should be paired with stop functions for timing of nested
250244445Sjeff * sequences.
251244445Sjeff *
252244445Sjeff * Specifying extra attributes with the name "key" will result in
253244445Sjeff * multi-part keys.  For example a block device and offset pair
254244445Sjeff * might be used to describe a buf undergoing I/O.
255244445Sjeff */
256244445Sjeff#define	KTR_START0(m, egroup, ident, key)				\
257244445Sjeff	KTR_EVENT0(m, egroup, ident, "start:0x%jX", (uintmax_t)key)
258244445Sjeff#define	KTR_START1(m, egroup, ident, key, a0, v0)			\
259244445Sjeff	KTR_EVENT1(m, egroup, ident, "start:0x%jX", (uintmax_t)key, a0, (v0))
260244445Sjeff#define	KTR_START2(m, egroup, ident, key, a0, v0, a1, v1)		\
261244445Sjeff	KTR_EVENT2(m, egroup, ident, "start:0x%jX", (uintmax_t)key,	\
262244445Sjeff	    a0, (v0), a1, (v1))
263244445Sjeff#define	KTR_START3(m, egroup, ident, key, a0, v0, a1, v1, a2, v2)\
264244445Sjeff	KTR_EVENT3(m, egroup, ident, "start:0x%jX", (uintmax_t)key,	\
265244445Sjeff	    a0, (v0), a1, (v1), a2, (v2))
266244445Sjeff#define	KTR_START4(m, egroup, ident, key,				\
267244445Sjeff	    a0, v0, a1, v1, a2, v2, a3, v3)				\
268244445Sjeff	KTR_EVENT4(m, egroup, ident, "start:0x%jX", (uintmax_t)key,	\
269244445Sjeff	    a0, (v0), a1, (v1), a2, (v2), a3, (v3))
270244445Sjeff
271244445Sjeff/*
272244445Sjeff * Stop functions denote the end of a region of code or operation
273244445Sjeff * and should be paired with start functions for timing of nested
274244445Sjeff * sequences.
275244445Sjeff */
276244445Sjeff#define	KTR_STOP0(m, egroup, ident, key)				\
277244445Sjeff	KTR_EVENT0(m, egroup, ident, "stop:0x%jX", (uintmax_t)key)
278244445Sjeff#define	KTR_STOP1(m, egroup, ident, key, a0, v0)			\
279244445Sjeff	KTR_EVENT1(m, egroup, ident, "stop:0x%jX", (uintmax_t)key, a0, (v0))
280244445Sjeff#define	KTR_STOP2(m, egroup, ident, key, a0, v0, a1, v1)		\
281244445Sjeff	KTR_EVENT2(m, egroup, ident, "stop:0x%jX", (uintmax_t)key,	\
282244445Sjeff	    a0, (v0), a1, (v1))
283244445Sjeff#define	KTR_STOP3(m, egroup, ident, key, a0, v0, a1, v1, a2, v2)\
284244445Sjeff	KTR_EVENT3(m, egroup, ident, "stop:0x%jX", (uintmax_t)key,	\
285244445Sjeff	    a0, (v0), a1, (v1), a2, (v2))
286244445Sjeff#define	KTR_STOP4(m, egroup, ident, 					\
287244445Sjeff	    key, a0, v0, a1, v1, a2, v2, a3, v3)			\
288244445Sjeff	KTR_EVENT4(m, egroup, ident, "stop:0x%jX", (uintmax_t)key,	\
289244445Sjeff	    a0, (v0), a1, (v1), a2, (v2), a3, (v3))
290244445Sjeff
291244445Sjeff/*
29265556Sjasone * Trace initialization events, similar to CTR with KTR_INIT, but
29365556Sjasone * completely ifdef'ed out if KTR_INIT isn't in KTR_COMPILE (to
29465556Sjasone * save string space, the compiler doesn't optimize out strings
29565556Sjasone * for the conditional ones above).
29665556Sjasone */
29765556Sjasone#if (KTR_COMPILE & KTR_INIT) != 0
29865556Sjasone#define	ITR0(d)				CTR0(KTR_INIT, d)
29965556Sjasone#define	ITR1(d, p1)			CTR1(KTR_INIT, d, p1)
30065556Sjasone#define	ITR2(d, p1, p2)			CTR2(KTR_INIT, d, p1, p2)
30165556Sjasone#define	ITR3(d, p1, p2, p3)		CTR3(KTR_INIT, d, p1, p2, p3)
30265556Sjasone#define	ITR4(d, p1, p2, p3, p4)		CTR4(KTR_INIT, d, p1, p2, p3, p4)
30365556Sjasone#define	ITR5(d, p1, p2, p3, p4, p5)	CTR5(KTR_INIT, d, p1, p2, p3, p4, p5)
30487957Sjhb#define	ITR6(d, p1, p2, p3, p4, p5, p6)	CTR6(KTR_INIT, d, p1, p2, p3, p4, p5, p6)
30565556Sjasone#else
30665556Sjasone#define	ITR0(d)
30765556Sjasone#define	ITR1(d, p1)
30865556Sjasone#define	ITR2(d, p1, p2)
30965556Sjasone#define	ITR3(d, p1, p2, p3)
31065556Sjasone#define	ITR4(d, p1, p2, p3, p4)
31165556Sjasone#define	ITR5(d, p1, p2, p3, p4, p5)
31287621Sobrien#define	ITR6(d, p1, p2, p3, p4, p5, p6)
31365556Sjasone#endif
31465556Sjasone
315153174Smarius#endif /* !LOCORE */
316153174Smarius
31765556Sjasone#endif /* !_SYS_KTR_H_ */
318