kern_history.c revision 1.1 1 1.1 mrg /* $NetBSD: kern_history.c,v 1.1 2011/05/17 04:18:06 mrg Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg *
16 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 mrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 mrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 mrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 mrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 mrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 mrg *
27 1.1 mrg * from: NetBSD: uvm_stat.c,v 1.36 2011/02/02 15:13:34 chuck Exp
28 1.1 mrg * from: Id: uvm_stat.c,v 1.1.2.3 1997/12/19 15:01:00 mrg Exp
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg * subr_kernhist.c
33 1.1 mrg */
34 1.1 mrg
35 1.1 mrg #include <sys/cdefs.h>
36 1.1 mrg __KERNEL_RCSID(0, "$NetBSD: kern_history.c,v 1.1 2011/05/17 04:18:06 mrg Exp $");
37 1.1 mrg
38 1.1 mrg #include "opt_kernhist.h"
39 1.1 mrg #include "opt_uvmhist.h"
40 1.1 mrg #include "opt_ddb.h"
41 1.1 mrg
42 1.1 mrg #include <sys/param.h>
43 1.1 mrg #include <sys/systm.h>
44 1.1 mrg #include <sys/cpu.h>
45 1.1 mrg #include <sys/kernhist.h>
46 1.1 mrg
47 1.1 mrg #include <uvm/uvm.h>
48 1.1 mrg
49 1.1 mrg /*
50 1.1 mrg * globals
51 1.1 mrg */
52 1.1 mrg
53 1.1 mrg struct kern_history_head kern_histories;
54 1.1 mrg
55 1.1 mrg int kernhist_print_enabled = 1;
56 1.1 mrg
57 1.1 mrg #ifdef DDB
58 1.1 mrg
59 1.1 mrg /*
60 1.1 mrg * prototypes
61 1.1 mrg */
62 1.1 mrg
63 1.1 mrg void kernhist_dump(struct kern_history *);
64 1.1 mrg void kernhist_dumpmask(u_int32_t);
65 1.1 mrg static void kernhist_dump_histories(struct kern_history *[]);
66 1.1 mrg
67 1.1 mrg
68 1.1 mrg /*
69 1.1 mrg * call this from ddb
70 1.1 mrg *
71 1.1 mrg * expects the system to be quiesced, no locking
72 1.1 mrg */
73 1.1 mrg void
74 1.1 mrg kernhist_dump(struct kern_history *l)
75 1.1 mrg {
76 1.1 mrg int lcv;
77 1.1 mrg
78 1.1 mrg lcv = l->f;
79 1.1 mrg do {
80 1.1 mrg if (l->e[lcv].fmt)
81 1.1 mrg kernhist_entry_print(&l->e[lcv]);
82 1.1 mrg lcv = (lcv + 1) % l->n;
83 1.1 mrg } while (lcv != l->f);
84 1.1 mrg }
85 1.1 mrg
86 1.1 mrg /*
87 1.1 mrg * print a merged list of kern_history structures
88 1.1 mrg */
89 1.1 mrg static void
90 1.1 mrg kernhist_dump_histories(struct kern_history *hists[])
91 1.1 mrg {
92 1.1 mrg struct timeval tv;
93 1.1 mrg int cur[MAXHISTS];
94 1.1 mrg int lcv, hi;
95 1.1 mrg
96 1.1 mrg /* find the first of each list */
97 1.1 mrg for (lcv = 0; hists[lcv]; lcv++)
98 1.1 mrg cur[lcv] = hists[lcv]->f;
99 1.1 mrg
100 1.1 mrg /*
101 1.1 mrg * here we loop "forever", finding the next earliest
102 1.1 mrg * history entry and printing it. cur[X] is the current
103 1.1 mrg * entry to test for the history in hists[X]. if it is
104 1.1 mrg * -1, then this history is finished.
105 1.1 mrg */
106 1.1 mrg for (;;) {
107 1.1 mrg hi = -1;
108 1.1 mrg tv.tv_sec = tv.tv_usec = 0;
109 1.1 mrg
110 1.1 mrg /* loop over each history */
111 1.1 mrg for (lcv = 0; hists[lcv]; lcv++) {
112 1.1 mrg restart:
113 1.1 mrg if (cur[lcv] == -1)
114 1.1 mrg continue;
115 1.1 mrg
116 1.1 mrg /*
117 1.1 mrg * if the format is empty, go to the next entry
118 1.1 mrg * and retry.
119 1.1 mrg */
120 1.1 mrg if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
121 1.1 mrg cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
122 1.1 mrg if (cur[lcv] == hists[lcv]->f)
123 1.1 mrg cur[lcv] = -1;
124 1.1 mrg goto restart;
125 1.1 mrg }
126 1.1 mrg
127 1.1 mrg /*
128 1.1 mrg * if the time hasn't been set yet, or this entry is
129 1.1 mrg * earlier than the current tv, set the time and history
130 1.1 mrg * index.
131 1.1 mrg */
132 1.1 mrg if (tv.tv_sec == 0 ||
133 1.1 mrg timercmp(&hists[lcv]->e[cur[lcv]].tv, &tv, <)) {
134 1.1 mrg tv = hists[lcv]->e[cur[lcv]].tv;
135 1.1 mrg hi = lcv;
136 1.1 mrg }
137 1.1 mrg }
138 1.1 mrg
139 1.1 mrg /* if we didn't find any entries, we must be done */
140 1.1 mrg if (hi == -1)
141 1.1 mrg break;
142 1.1 mrg
143 1.1 mrg /* print and move to the next entry */
144 1.1 mrg kernhist_entry_print(&hists[hi]->e[cur[hi]]);
145 1.1 mrg cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
146 1.1 mrg if (cur[hi] == hists[hi]->f)
147 1.1 mrg cur[hi] = -1;
148 1.1 mrg }
149 1.1 mrg }
150 1.1 mrg
151 1.1 mrg /*
152 1.1 mrg * call this from ddb. `bitmask' is from <sys/kernhist.h>. it
153 1.1 mrg * merges the named histories.
154 1.1 mrg *
155 1.1 mrg * expects the system to be quiesced, no locking
156 1.1 mrg */
157 1.1 mrg void
158 1.1 mrg kernhist_dumpmask(u_int32_t bitmask) /* XXX only support 32 hists */
159 1.1 mrg {
160 1.1 mrg struct kern_history *hists[MAXHISTS + 1];
161 1.1 mrg int i = 0;
162 1.1 mrg
163 1.1 mrg #ifdef UVMHIST
164 1.1 mrg if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
165 1.1 mrg hists[i++] = &maphist;
166 1.1 mrg
167 1.1 mrg if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
168 1.1 mrg hists[i++] = &pdhist;
169 1.1 mrg
170 1.1 mrg if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
171 1.1 mrg hists[i++] = &ubchist;
172 1.1 mrg
173 1.1 mrg if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
174 1.1 mrg hists[i++] = &loanhist;
175 1.1 mrg #endif
176 1.1 mrg
177 1.1 mrg hists[i] = NULL;
178 1.1 mrg
179 1.1 mrg kernhist_dump_histories(hists);
180 1.1 mrg }
181 1.1 mrg
182 1.1 mrg /*
183 1.1 mrg * kernhist_print: ddb hook to print kern history
184 1.1 mrg */
185 1.1 mrg void
186 1.1 mrg kernhist_print(void (*pr)(const char *, ...))
187 1.1 mrg {
188 1.1 mrg kernhist_dump(LIST_FIRST(&kern_histories));
189 1.1 mrg }
190 1.1 mrg
191 1.1 mrg #endif
192