1 /*	$OpenBSD: uvm_stat.c,v 1.15 2004/04/28 02:20:58 markus Exp $	 */
2 /*	$NetBSD: uvm_stat.c,v 1.18 2001/03/09 01:02:13 chs 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.c,v 1.1.2.3 1997/12/19 15:01:00 mrg Exp
36  */
37 
38 /*
39  * uvm_stat.c
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 
45 #include <uvm/uvm.h>
46 #include <uvm/uvm_ddb.h>
47 
48 /*
49  * globals
50  */
51 
52 struct uvm_cnt *uvm_cnt_head = NULL;
53 
54 #ifdef UVMHIST
55 struct uvm_history_head uvm_histories;
56 #endif
57 
58 #ifdef UVMHIST_PRINT
59 int uvmhist_print_enabled = 1;
60 #endif
61 
62 #ifdef DDB
63 
64 /*
65  * prototypes
66  */
67 
68 #ifdef UVMHIST
69 void uvmhist_dump(struct uvm_history *);
70 void uvm_hist(u_int32_t);
71 static void uvmhist_dump_histories(struct uvm_history *[]);
72 #endif
73 void uvmcnt_dump(void);
74 
75 
76 #ifdef UVMHIST
77 /* call this from ddb */
78 void
uvmhist_dump(l)79 uvmhist_dump(l)
80 	struct uvm_history *l;
81 {
82 	int lcv, s;
83 
84 	s = splhigh();
85 	lcv = l->f;
86 	do {
87 		if (l->e[lcv].fmt)
88 			uvmhist_print(&l->e[lcv]);
89 		lcv = (lcv + 1) % l->n;
90 	} while (lcv != l->f);
91 	splx(s);
92 }
93 
94 /*
95  * print a merged list of uvm_history structures
96  */
97 static void
uvmhist_dump_histories(hists)98 uvmhist_dump_histories(hists)
99 	struct uvm_history *hists[];
100 {
101 	struct timeval  tv;
102 	int	cur[MAXHISTS];
103 	int	s, lcv, hi;
104 
105 	/* so we don't get corrupted lists! */
106 	s = splhigh();
107 
108 	/* find the first of each list */
109 	for (lcv = 0; hists[lcv]; lcv++)
110 		 cur[lcv] = hists[lcv]->f;
111 
112 	/*
113 	 * here we loop "forever", finding the next earliest
114 	 * history entry and printing it.  cur[X] is the current
115 	 * entry to test for the history in hists[X].  if it is
116 	 * -1, then this history is finished.
117 	 */
118 	for (;;) {
119 		hi = -1;
120 		tv.tv_sec = tv.tv_usec = 0;
121 
122 		/* loop over each history */
123 		for (lcv = 0; hists[lcv]; lcv++) {
124 restart:
125 			if (cur[lcv] == -1)
126 				continue;
127 
128 			/*
129 			 * if the format is empty, go to the next entry
130 			 * and retry.
131 			 */
132 			if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
133 				cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
134 				if (cur[lcv] == hists[lcv]->f)
135 					cur[lcv] = -1;
136 				goto restart;
137 			}
138 
139 			/*
140 			 * if the time hasn't been set yet, or this entry is
141 			 * earlier than the current tv, set the time and history
142 			 * index.
143 			 */
144 			if (tv.tv_sec == 0 ||
145 			    timercmp(&hists[lcv]->e[cur[lcv]].tv, &tv, <)) {
146 				tv = hists[lcv]->e[cur[lcv]].tv;
147 				hi = lcv;
148 			}
149 		}
150 
151 		/* if we didn't find any entries, we must be done */
152 		if (hi == -1)
153 			break;
154 
155 		/* print and move to the next entry */
156 		uvmhist_print(&hists[hi]->e[cur[hi]]);
157 		cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
158 		if (cur[hi] == hists[hi]->f)
159 			cur[hi] = -1;
160 	}
161 
162 	/* done! */
163 	splx(s);
164 }
165 
166 /*
167  * call this from ddb.  `bitmask' is from <uvm/uvm_stat.h>.  it
168  * merges the named histories.
169  */
170 void
uvm_hist(bitmask)171 uvm_hist(bitmask)
172 	u_int32_t	bitmask;	/* XXX only support 32 hists */
173 {
174 	struct uvm_history *hists[MAXHISTS + 1];
175 	int i = 0;
176 
177 	if ((bitmask & UVMHIST_MAPHIST) || bitmask == 0)
178 		hists[i++] = &maphist;
179 
180 	if ((bitmask & UVMHIST_PDHIST) || bitmask == 0)
181 		hists[i++] = &pdhist;
182 
183 	hists[i] = NULL;
184 
185 	uvmhist_dump_histories(hists);
186 }
187 #endif /* UVMHIST */
188 
189 void
uvmcnt_dump()190 uvmcnt_dump()
191 {
192 	struct uvm_cnt *uvc = uvm_cnt_head;
193 
194 	while (uvc) {
195 		if ((uvc->t & UVMCNT_MASK) != UVMCNT_CNT)
196 			continue;
197 		printf("%s = %d\n", uvc->name, uvc->c);
198 		uvc = uvc->next;
199 	}
200 }
201 
202 /*
203  * uvmexp_print: ddb hook to print interesting uvm counters
204  */
205 void
uvmexp_print(int (* pr)(const char *,...))206 uvmexp_print(int (*pr)(const char *, ...))
207 {
208 
209 	(*pr)("Current UVM status:\n");
210 	(*pr)("  pagesize=%d (0x%x), pagemask=0x%x, pageshift=%d\n",
211 	    uvmexp.pagesize, uvmexp.pagesize, uvmexp.pagemask,
212 	    uvmexp.pageshift);
213 	(*pr)("  %d VM pages: %d active, %d inactive, %d wired, %d free\n",
214 	    uvmexp.npages, uvmexp.active, uvmexp.inactive, uvmexp.wired,
215 	    uvmexp.free);
216 	(*pr)("  min  %d%% (%d) anon, %d%% (%d) vnode, %d%% (%d) vtext\n",
217 	    uvmexp.anonminpct, uvmexp.anonmin, uvmexp.vnodeminpct,
218 	    uvmexp.vnodemin, uvmexp.vtextminpct, uvmexp.vtextmin);
219 	(*pr)("  pages  %d anon, %d vnode, %d vtext\n",
220 	    uvmexp.anonpages, uvmexp.vnodepages, uvmexp.vtextpages);
221 	(*pr)("  freemin=%d, free-target=%d, inactive-target=%d, "
222 	    "wired-max=%d\n", uvmexp.freemin, uvmexp.freetarg, uvmexp.inactarg,
223 	    uvmexp.wiredmax);
224 	(*pr)("  faults=%d, traps=%d, intrs=%d, ctxswitch=%d\n",
225 	    uvmexp.faults, uvmexp.traps, uvmexp.intrs, uvmexp.swtch);
226 	(*pr)("  softint=%d, syscalls=%d, swapins=%d, swapouts=%d\n",
227 	    uvmexp.softs, uvmexp.syscalls, uvmexp.swapins, uvmexp.swapouts);
228 
229 	(*pr)("  fault counts:\n");
230 	(*pr)("    noram=%d, noanon=%d, pgwait=%d, pgrele=%d\n",
231 	    uvmexp.fltnoram, uvmexp.fltnoanon, uvmexp.fltpgwait,
232 	    uvmexp.fltpgrele);
233 	(*pr)("    ok relocks(total)=%d(%d), anget(retrys)=%d(%d), "
234 	    "amapcopy=%d\n", uvmexp.fltrelckok, uvmexp.fltrelck,
235 	    uvmexp.fltanget, uvmexp.fltanretry, uvmexp.fltamcopy);
236 	(*pr)("    neighbor anon/obj pg=%d/%d, gets(lock/unlock)=%d/%d\n",
237 	    uvmexp.fltnamap, uvmexp.fltnomap, uvmexp.fltlget, uvmexp.fltget);
238 	(*pr)("    cases: anon=%d, anoncow=%d, obj=%d, prcopy=%d, przero=%d\n",
239 	    uvmexp.flt_anon, uvmexp.flt_acow, uvmexp.flt_obj, uvmexp.flt_prcopy,
240 	    uvmexp.flt_przero);
241 
242 	(*pr)("  daemon and swap counts:\n");
243 	(*pr)("    woke=%d, revs=%d, scans=%d, obscans=%d, anscans=%d\n",
244 	    uvmexp.pdwoke, uvmexp.pdrevs, uvmexp.pdscans, uvmexp.pdobscan,
245 	    uvmexp.pdanscan);
246 	(*pr)("    busy=%d, freed=%d, reactivate=%d, deactivate=%d\n",
247 	    uvmexp.pdbusy, uvmexp.pdfreed, uvmexp.pdreact, uvmexp.pddeact);
248 	(*pr)("    pageouts=%d, pending=%d, nswget=%d\n", uvmexp.pdpageouts,
249 	    uvmexp.pdpending, uvmexp.nswget);
250 	(*pr)("    nswapdev=%d, nanon=%d, nanonneeded=%d nfreeanon=%d\n",
251 	    uvmexp.nswapdev, uvmexp.nanon, uvmexp.nanonneeded,
252 	    uvmexp.nfreeanon);
253 	(*pr)("    swpages=%d, swpginuse=%d, swpgonly=%d paging=%d\n",
254 	    uvmexp.swpages, uvmexp.swpginuse, uvmexp.swpgonly, uvmexp.paging);
255 
256 	(*pr)("  kernel pointers:\n");
257 	(*pr)("    objs(kern/kmem)=%p/%p\n", uvm.kernel_object,
258 	    uvmexp.kmem_object);
259 }
260 #endif
261