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