xref: /netbsd/sys/uvm/uvm_stat.h (revision c4a72b64)
1 /*	$NetBSD: uvm_stat.h,v 1.25 2002/11/02 07:40:49 perry Exp $	*/
2 
3 /*
4  *
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor and
19  *      Washington University.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * from: Id: uvm_stat.h,v 1.1.2.4 1998/02/07 01:16:56 chs Exp
35  */
36 
37 #ifndef _UVM_UVM_STAT_H_
38 #define _UVM_UVM_STAT_H_
39 
40 #if defined(_KERNEL_OPT)
41 #include "opt_uvmhist.h"
42 #endif
43 
44 #include <sys/queue.h>
45 
46 /*
47  * uvm_stat: monitor what is going on with uvm (or whatever)
48  */
49 
50 /*
51  * counters  [XXX: maybe replace event counters with this]
52  */
53 
54 #define UVMCNT_MASK	0xf			/* rest are private */
55 #define UVMCNT_CNT	0			/* normal counter */
56 #define UVMCNT_DEV	1			/* device event counter */
57 
58 struct uvm_cnt {
59 	int c;					/* the value */
60 	int t;					/* type */
61 	struct uvm_cnt *next;			/* global list of cnts */
62 	char *name;				/* counter name */
63 	void *p;				/* private data */
64 };
65 
66 #ifdef _KERNEL
67 
68 extern struct uvm_cnt *uvm_cnt_head;
69 
70 /*
71  * counter operations.  assume spl is set ok.
72  */
73 
74 #define UVMCNT_INIT(CNT,TYP,VAL,NAM,PRIV) \
75 do { \
76 	CNT.c = VAL; \
77 	CNT.t = TYP; \
78 	CNT.next = uvm_cnt_head; \
79 	uvm_cnt_head = &CNT; \
80 	CNT.name = NAM; \
81 	CNT.p = PRIV; \
82 } while (/*CONSTCOND*/ 0)
83 
84 #define UVMCNT_SET(C,V) \
85 do { \
86 	(C).c = (V); \
87 } while (/*CONSTCOND*/ 0)
88 
89 #define UVMCNT_ADD(C,V) \
90 do { \
91 	(C).c += (V); \
92 } while (/*CONSTCOND*/ 0)
93 
94 #define UVMCNT_INCR(C) UVMCNT_ADD(C,1)
95 #define UVMCNT_DECR(C) UVMCNT_ADD(C,-1)
96 
97 #endif /* _KERNEL */
98 
99 /*
100  * history/tracing
101  */
102 
103 struct uvm_history_ent {
104 	struct timeval tv; 		/* time stamp */
105 	char *fmt; 			/* printf format */
106 	size_t fmtlen;			/* length of printf format */
107 	char *fn;			/* function name */
108 	size_t fnlen;			/* length of function name */
109 	u_long call;			/* function call number */
110 	u_long v[4];			/* values */
111 };
112 
113 struct uvm_history {
114 	const char *name;		/* name of this this history */
115 	size_t namelen;			/* length of name, not including null */
116 	LIST_ENTRY(uvm_history) list;	/* link on list of all histories */
117 	int n;				/* number of entries */
118 	int f; 				/* next free one */
119 	struct simplelock l;		/* lock on this history */
120 	struct uvm_history_ent *e;	/* the malloc'd entries */
121 };
122 
123 LIST_HEAD(uvm_history_head, uvm_history);
124 
125 /*
126  * grovelling lists all at once.  we currently do not allow more than
127  * 32 histories to exist, as the way to dump a number of them at once
128  * is by calling uvm_hist() with a bitmask.
129  */
130 
131 /* this is used to set the size of some arrays */
132 #define	MAXHISTS		32	/* do not change this! */
133 
134 /* and these are the bit values of each history */
135 #define	UVMHIST_MAPHIST		0x00000001	/* maphist */
136 #define	UVMHIST_PDHIST		0x00000002	/* pdhist */
137 #define	UVMHIST_UBCHIST		0x00000004	/* ubchist */
138 
139 #ifdef _KERNEL
140 
141 /*
142  * macros to use the history/tracing code.  note that UVMHIST_LOG
143  * must take 4 arguments (even if they are ignored by the format).
144  */
145 #ifndef UVMHIST
146 #define UVMHIST_DECL(NAME)
147 #define UVMHIST_INIT(NAME,N)
148 #define UVMHIST_INIT_STATIC(NAME,BUF)
149 #define UVMHIST_LOG(NAME,FMT,A,B,C,D)
150 #define UVMHIST_CALLED(NAME)
151 #define UVMHIST_FUNC(FNAME)
152 #define uvmhist_dump(NAME)
153 #else
154 #include <sys/kernel.h>		/* for "cold" variable */
155 
156 extern	struct uvm_history_head uvm_histories;
157 
158 #define UVMHIST_DECL(NAME) struct uvm_history NAME
159 
160 #define UVMHIST_INIT(NAME,N) \
161 do { \
162 	(NAME).name = __STRING(NAME); \
163 	(NAME).namelen = strlen((NAME).name); \
164 	(NAME).n = (N); \
165 	(NAME).f = 0; \
166 	simple_lock_init(&(NAME).l); \
167 	(NAME).e = (struct uvm_history_ent *) \
168 		malloc(sizeof(struct uvm_history_ent) * (N), M_TEMP, \
169 		    M_WAITOK); \
170 	memset((NAME).e, 0, sizeof(struct uvm_history_ent) * (N)); \
171 	LIST_INSERT_HEAD(&uvm_histories, &(NAME), list); \
172 } while (/*CONSTCOND*/ 0)
173 
174 #define UVMHIST_INIT_STATIC(NAME,BUF) \
175 do { \
176 	(NAME).name = __STRING(NAME); \
177 	(NAME).namelen = strlen((NAME).name); \
178 	(NAME).n = sizeof(BUF) / sizeof(struct uvm_history_ent); \
179 	(NAME).f = 0; \
180 	simple_lock_init(&(NAME).l); \
181 	(NAME).e = (struct uvm_history_ent *) (BUF); \
182 	memset((NAME).e, 0, sizeof(struct uvm_history_ent) * (NAME).n); \
183 	LIST_INSERT_HEAD(&uvm_histories, &(NAME), list); \
184 } while (/*CONSTCOND*/ 0)
185 
186 #if defined(UVMHIST_PRINT)
187 extern int uvmhist_print_enabled;
188 #define UVMHIST_PRINTNOW(E) \
189 do { \
190 		if (uvmhist_print_enabled) { \
191 			uvmhist_print(E); \
192 			DELAY(100000); \
193 		} \
194 } while (/*CONSTCOND*/ 0)
195 #else
196 #define UVMHIST_PRINTNOW(E) /* nothing */
197 #endif
198 
199 #define UVMHIST_LOG(NAME,FMT,A,B,C,D) \
200 do { \
201 	int _i_, _s_ = splhigh(); \
202 	simple_lock(&(NAME).l); \
203 	_i_ = (NAME).f; \
204 	(NAME).f = (_i_ + 1) % (NAME).n; \
205 	simple_unlock(&(NAME).l); \
206 	splx(_s_); \
207 	if (!cold) \
208 		microtime(&(NAME).e[_i_].tv); \
209 	(NAME).e[_i_].fmt = (FMT); \
210 	(NAME).e[_i_].fmtlen = strlen((NAME).e[_i_].fmt); \
211 	(NAME).e[_i_].fn = _uvmhist_name; \
212 	(NAME).e[_i_].fnlen = strlen((NAME).e[_i_].fn); \
213 	(NAME).e[_i_].call = _uvmhist_call; \
214 	(NAME).e[_i_].v[0] = (u_long)(A); \
215 	(NAME).e[_i_].v[1] = (u_long)(B); \
216 	(NAME).e[_i_].v[2] = (u_long)(C); \
217 	(NAME).e[_i_].v[3] = (u_long)(D); \
218 	UVMHIST_PRINTNOW(&((NAME).e[_i_])); \
219 } while (/*CONSTCOND*/ 0)
220 
221 #define UVMHIST_CALLED(NAME) \
222 do { \
223 	{ \
224 		int s = splhigh(); \
225 		simple_lock(&(NAME).l); \
226 		_uvmhist_call = _uvmhist_cnt++; \
227 		simple_unlock(&(NAME).l); \
228 		splx(s); \
229 	} \
230 	UVMHIST_LOG(NAME,"called!", 0, 0, 0, 0); \
231 } while (/*CONSTCOND*/ 0)
232 
233 #define UVMHIST_FUNC(FNAME) \
234 	static int _uvmhist_cnt = 0; \
235 	static char *_uvmhist_name = FNAME; \
236 	int _uvmhist_call;
237 
238 static __inline void uvmhist_print __P((struct uvm_history_ent *));
239 
240 static __inline void
241 uvmhist_print(e)
242 	struct uvm_history_ent *e;
243 {
244 	printf("%06ld.%06ld ", e->tv.tv_sec, e->tv.tv_usec);
245 	printf("%s#%ld: ", e->fn, e->call);
246 	printf(e->fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
247 	printf("\n");
248 }
249 #endif /* UVMHIST */
250 
251 #endif /* _KERNEL */
252 
253 #endif /* _UVM_UVM_STAT_H_ */
254