xref: /minix3/sys/sys/kernhist.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: kernhist.h,v 1.9 2014/03/30 15:53:37 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * from: NetBSD: uvm_stat.h,v 1.49 2011/04/23 18:14:13 rmind Exp
28  * from: Id: uvm_stat.h,v 1.1.2.4 1998/02/07 01:16:56 chs Exp
29  */
30 
31 #ifndef _SYS_KERNHIST_H_
32 #define _SYS_KERNHIST_H_
33 
34 #if defined(_KERNEL_OPT)
35 #include "opt_ddb.h"
36 #include "opt_kernhist.h"
37 #endif
38 
39 #include <sys/queue.h>
40 #ifdef KERNHIST
41 #include <sys/cpu.h>
42 #endif
43 
44 /*
45  * kernel history/tracing, was uvm_stat
46  */
47 
48 struct kern_history_ent {
49 	struct timeval tv; 		/* time stamp */
50 	int cpunum;
51 	const char *fmt;		/* printf format */
52 	size_t fmtlen;			/* length of printf format */
53 	const char *fn;			/* function name */
54 	size_t fnlen;			/* length of function name */
55 	u_long call;			/* function call number */
56 	u_long v[4];			/* values */
57 };
58 
59 struct kern_history {
60 	const char *name;		/* name of this history */
61 	size_t namelen;			/* length of name, not including null */
62 	LIST_ENTRY(kern_history) list;	/* link on list of all histories */
63 	unsigned int n;			/* number of entries */
64 	unsigned int f;			/* next free one */
65 	struct kern_history_ent *e;	/* the allocated entries */
66 };
67 
68 LIST_HEAD(kern_history_head, kern_history);
69 
70 /*
71  * grovelling lists all at once.  we currently do not allow more than
72  * 32 histories to exist, as the way to dump a number of them at once
73  * is by calling kern_hist() with a bitmask.
74  *
75  * XXX extend this to have a registration function?  however, there
76  * needs to be static ones as UVM requires this before almost anything
77  * else is setup.
78  */
79 
80 /* this is used to set the size of some arrays */
81 #define	MAXHISTS		32
82 
83 /* and these are the bit values of each history */
84 #define	KERNHIST_UVMMAPHIST	0x00000001	/* maphist */
85 #define	KERNHIST_UVMPDHIST	0x00000002	/* pdhist */
86 #define	KERNHIST_UVMUBCHIST	0x00000004	/* ubchist */
87 #define	KERNHIST_UVMLOANHIST	0x00000008	/* loanhist */
88 
89 #ifdef _KERNEL
90 
91 /*
92  * macros to use the history/tracing code.  note that KERNHIST_LOG
93  * must take 4 arguments (even if they are ignored by the format).
94  */
95 #ifndef KERNHIST
96 #define KERNHIST_DECL(NAME)
97 #define KERNHIST_DEFINE(NAME)
98 #define KERNHIST_INIT(NAME,N)
99 #define KERNHIST_INIT_STATIC(NAME,BUF)
100 #define KERNHIST_LOG(NAME,FMT,A,B,C,D)
101 #define KERNHIST_CALLARGS(NAME,FMT,A,B,C,D)
102 #define KERNHIST_CALLED(NAME)
103 #define KERNHIST_FUNC(FNAME)
104 #define KERNHIST_DUMP(NAME)
105 #else
106 #include <sys/kernel.h>		/* for "cold" variable */
107 #include <sys/atomic.h>
108 #include <sys/kmem.h>
109 
110 extern	struct kern_history_head kern_histories;
111 
112 #define KERNHIST_DECL(NAME) extern struct kern_history NAME
113 #define KERNHIST_DEFINE(NAME) struct kern_history NAME
114 
115 #define KERNHIST_INIT(NAME,N) \
116 do { \
117 	(NAME).name = __STRING(NAME); \
118 	(NAME).namelen = strlen(__STRING(NAME)); \
119 	(NAME).n = (N); \
120 	(NAME).f = 0; \
121 	(NAME).e = (struct kern_history_ent *) \
122 		kmem_zalloc(sizeof(struct kern_history_ent) * (N), KM_SLEEP); \
123 	LIST_INSERT_HEAD(&kern_histories, &(NAME), list); \
124 } while (/*CONSTCOND*/ 0)
125 
126 #define KERNHIST_INITIALIZER(NAME,BUF) \
127 { \
128 	.name = __STRING(NAME), \
129 	.namelen = sizeof(__STRING(NAME)) - 1, \
130 	.n = sizeof(BUF) / sizeof(struct kern_history_ent), \
131 	.f = 0, \
132 	.e = (struct kern_history_ent *) (BUF), \
133 	/* BUF will inititalized to zeroes by being in .bss */ \
134 }
135 
136 #define KERNHIST_LINK_STATIC(NAME) \
137 	LIST_INSERT_HEAD(&kern_histories, &(NAME), list)
138 
139 #define KERNHIST_INIT_STATIC(NAME,BUF) \
140 do { \
141 	(NAME).name = __STRING(NAME); \
142 	(NAME).namelen = strlen(__STRING(NAME)); \
143 	(NAME).n = sizeof(BUF) / sizeof(struct kern_history_ent); \
144 	(NAME).f = 0; \
145 	(NAME).e = (struct kern_history_ent *) (BUF); \
146 	memset((NAME).e, 0, sizeof(struct kern_history_ent) * (NAME).n); \
147 	KERNHIST_LINK_STATIC(NAME); \
148 } while (/*CONSTCOND*/ 0)
149 
150 #ifndef KERNHIST_DELAY
151 #define KERNHIST_DELAY	100000
152 #endif
153 
154 #if defined(KERNHIST_PRINT)
155 extern int kernhist_print_enabled;
156 #define KERNHIST_PRINTNOW(E) \
157 do { \
158 		if (kernhist_print_enabled) { \
159 			kernhist_entry_print(E); \
160 			if (KERNHIST_DELAY != 0) \
161 				DELAY(KERNHIST_DELAY); \
162 		} \
163 } while (/*CONSTCOND*/ 0)
164 #else
165 #define KERNHIST_PRINTNOW(E) /* nothing */
166 #endif
167 
168 #define KERNHIST_LOG(NAME,FMT,A,B,C,D) \
169 do { \
170 	unsigned int _i_, _j_; \
171 	do { \
172 		_i_ = (NAME).f; \
173 		_j_ = (_i_ + 1 < (NAME).n) ? _i_ + 1 : 0; \
174 	} while (atomic_cas_uint(&(NAME).f, _i_, _j_) != _i_); \
175 	struct kern_history_ent * const _e_ = &(NAME).e[_i_]; \
176 	if (__predict_true(!cold)) \
177 		microtime(&_e_->tv); \
178 	_e_->cpunum = cpu_number(); \
179 	_e_->fmt = (FMT); \
180 	_e_->fmtlen = strlen(FMT); \
181 	_e_->fn = _kernhist_name; \
182 	_e_->fnlen = strlen(_kernhist_name); \
183 	_e_->call = _kernhist_call; \
184 	_e_->v[0] = (u_long)(A); \
185 	_e_->v[1] = (u_long)(B); \
186 	_e_->v[2] = (u_long)(C); \
187 	_e_->v[3] = (u_long)(D); \
188 	KERNHIST_PRINTNOW(_e_); \
189 } while (/*CONSTCOND*/ 0)
190 
191 #define KERNHIST_CALLED(NAME) \
192 do { \
193 	_kernhist_call = atomic_inc_uint_nv(&_kernhist_cnt); \
194 	KERNHIST_LOG(NAME, "called!", 0, 0, 0, 0); \
195 } while (/*CONSTCOND*/ 0)
196 
197 /*
198  * This extends kernhist to avoid wasting a separate "called!" entry on every
199  * function.
200  */
201 #define KERNHIST_CALLARGS(NAME, FMT, A, B, C, D) \
202 do { \
203 	_kernhist_call = atomic_inc_uint_nv(&_kernhist_cnt); \
204 	KERNHIST_LOG(NAME, "called: "FMT, (A), (B), (C), (D)); \
205 } while (/*CONSTCOND*/ 0)
206 
207 #define KERNHIST_FUNC(FNAME) \
208 	static unsigned int _kernhist_cnt = 0; \
209 	static const char *const _kernhist_name = FNAME; \
210 	unsigned int _kernhist_call = 0;
211 
212 #ifdef DDB
213 #define KERNHIST_DUMP(NAME)	kernhist_dump(&NAME)
214 #else
215 #define KERNHIST_DUMP(NAME)
216 #endif
217 
218 
219 static inline void kernhist_entry_print(const struct kern_history_ent *);
220 
221 static inline void
kernhist_entry_print(const struct kern_history_ent * e)222 kernhist_entry_print(const struct kern_history_ent *e)
223 {
224 	printf("%06" PRIu64 ".%06d ", e->tv.tv_sec, e->tv.tv_usec);
225 	printf("%s#%ld@%d: ", e->fn, e->call, e->cpunum);
226 	printf(e->fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
227 	printf("\n");
228 }
229 
230 #if defined(DDB)
231 void	kernhist_dump(struct kern_history *);
232 void	kernhist_print(void (*)(const char *, ...) __printflike(1, 2));
233 #endif /* DDB */
234 
235 #endif /* KERNHIST */
236 
237 #endif /* _KERNEL */
238 
239 #endif /* _SYS_KERNHIST_H_ */
240