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