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