1 /* $NetBSD: uvm_stat.h,v 1.47 2010/07/07 01:08:51 chs 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.h,v 1.1.2.4 1998/02/07 01:16:56 chs Exp 35 */ 36 37 #ifndef _UVM_UVM_STAT_H_ 38 #define _UVM_UVM_STAT_H_ 39 40 #if defined(_KERNEL_OPT) 41 #include "opt_uvmhist.h" 42 #endif 43 44 #include <sys/queue.h> 45 #ifdef UVMHIST 46 #include <sys/cpu.h> 47 #include <sys/malloc.h> 48 #endif 49 50 /* 51 * uvm_stat: monitor what is going on with uvm (or whatever) 52 */ 53 54 /* 55 * history/tracing 56 */ 57 58 struct uvm_history_ent { 59 struct timeval tv; /* time stamp */ 60 int cpunum; 61 const char *fmt; /* printf format */ 62 size_t fmtlen; /* length of printf format */ 63 const char *fn; /* function name */ 64 size_t fnlen; /* length of function name */ 65 u_long call; /* function call number */ 66 u_long v[4]; /* values */ 67 }; 68 69 struct uvm_history { 70 const char *name; /* name of this history */ 71 size_t namelen; /* length of name, not including null */ 72 LIST_ENTRY(uvm_history) list; /* link on list of all histories */ 73 unsigned int n; /* number of entries */ 74 unsigned int f; /* next free one */ 75 struct uvm_history_ent *e; /* the malloc'd entries */ 76 }; 77 78 LIST_HEAD(uvm_history_head, uvm_history); 79 80 /* 81 * grovelling lists all at once. we currently do not allow more than 82 * 32 histories to exist, as the way to dump a number of them at once 83 * is by calling uvm_hist() with a bitmask. 84 */ 85 86 /* this is used to set the size of some arrays */ 87 #define MAXHISTS 32 /* do not change this! */ 88 89 /* and these are the bit values of each history */ 90 #define UVMHIST_MAPHIST 0x00000001 /* maphist */ 91 #define UVMHIST_PDHIST 0x00000002 /* pdhist */ 92 #define UVMHIST_UBCHIST 0x00000004 /* ubchist */ 93 #define UVMHIST_LOANHIST 0x00000008 /* loanhist */ 94 95 #ifdef _KERNEL 96 97 /* 98 * macros to use the history/tracing code. note that UVMHIST_LOG 99 * must take 4 arguments (even if they are ignored by the format). 100 */ 101 #ifndef UVMHIST 102 #define UVMHIST_DECL(NAME) 103 #define UVMHIST_INIT(NAME,N) 104 #define UVMHIST_INIT_STATIC(NAME,BUF) 105 #define UVMHIST_LOG(NAME,FMT,A,B,C,D) 106 #define UVMHIST_CALLED(NAME) 107 #define UVMHIST_FUNC(FNAME) 108 #define uvmhist_dump(NAME) 109 #else 110 #include <sys/kernel.h> /* for "cold" variable */ 111 #include <sys/atomic.h> 112 113 extern struct uvm_history_head uvm_histories; 114 115 #define UVMHIST_DECL(NAME) struct uvm_history NAME 116 117 #define UVMHIST_INIT(NAME,N) \ 118 do { \ 119 (NAME).name = __STRING(NAME); \ 120 (NAME).namelen = strlen(__STRING(NAME)); \ 121 (NAME).n = (N); \ 122 (NAME).f = 0; \ 123 (NAME).e = (struct uvm_history_ent *) \ 124 malloc(sizeof(struct uvm_history_ent) * (N), M_TEMP, \ 125 M_WAITOK); \ 126 memset((NAME).e, 0, sizeof(struct uvm_history_ent) * (N)); \ 127 LIST_INSERT_HEAD(&uvm_histories, &(NAME), list); \ 128 } while (/*CONSTCOND*/ 0) 129 130 #define UVMHIST_INIT_STATIC(NAME,BUF) \ 131 do { \ 132 (NAME).name = __STRING(NAME); \ 133 (NAME).namelen = strlen(__STRING(NAME)); \ 134 (NAME).n = sizeof(BUF) / sizeof(struct uvm_history_ent); \ 135 (NAME).f = 0; \ 136 (NAME).e = (struct uvm_history_ent *) (BUF); \ 137 memset((NAME).e, 0, sizeof(struct uvm_history_ent) * (NAME).n); \ 138 LIST_INSERT_HEAD(&uvm_histories, &(NAME), list); \ 139 } while (/*CONSTCOND*/ 0) 140 141 #if defined(UVMHIST_PRINT) 142 extern int uvmhist_print_enabled; 143 #define UVMHIST_PRINTNOW(E) \ 144 do { \ 145 if (uvmhist_print_enabled) { \ 146 uvmhist_entry_print(E); \ 147 DELAY(100000); \ 148 } \ 149 } while (/*CONSTCOND*/ 0) 150 #else 151 #define UVMHIST_PRINTNOW(E) /* nothing */ 152 #endif 153 154 #define UVMHIST_LOG(NAME,FMT,A,B,C,D) \ 155 do { \ 156 unsigned int _i_, _j_; \ 157 do { \ 158 _i_ = (NAME).f; \ 159 _j_ = (_i_ + 1 < (NAME).n) ? _i_ + 1 : 0; \ 160 } while (atomic_cas_uint(&(NAME).f, _i_, _j_) != _i_); \ 161 if (!cold) \ 162 microtime(&(NAME).e[_i_].tv); \ 163 (NAME).e[_i_].cpunum = cpu_number(); \ 164 (NAME).e[_i_].fmt = (FMT); \ 165 (NAME).e[_i_].fmtlen = strlen(FMT); \ 166 (NAME).e[_i_].fn = _uvmhist_name; \ 167 (NAME).e[_i_].fnlen = strlen(_uvmhist_name); \ 168 (NAME).e[_i_].call = _uvmhist_call; \ 169 (NAME).e[_i_].v[0] = (u_long)(A); \ 170 (NAME).e[_i_].v[1] = (u_long)(B); \ 171 (NAME).e[_i_].v[2] = (u_long)(C); \ 172 (NAME).e[_i_].v[3] = (u_long)(D); \ 173 UVMHIST_PRINTNOW(&((NAME).e[_i_])); \ 174 } while (/*CONSTCOND*/ 0) 175 176 #define UVMHIST_CALLED(NAME) \ 177 do { \ 178 _uvmhist_call = atomic_inc_uint_nv(&_uvmhist_cnt); \ 179 UVMHIST_LOG(NAME,"called!", 0, 0, 0, 0); \ 180 } while (/*CONSTCOND*/ 0) 181 182 #define UVMHIST_FUNC(FNAME) \ 183 static unsigned int _uvmhist_cnt = 0; \ 184 static const char *const _uvmhist_name = FNAME; \ 185 int _uvmhist_call; 186 187 static inline void uvmhist_entry_print(struct uvm_history_ent *); 188 189 static inline void 190 uvmhist_entry_print(e) 191 struct uvm_history_ent *e; 192 { 193 printf("%06" PRIu64 ".%06d ", e->tv.tv_sec, e->tv.tv_usec); 194 printf("%s#%ld@%d: ", e->fn, e->call, e->cpunum); 195 printf(e->fmt, e->v[0], e->v[1], e->v[2], e->v[3]); 196 printf("\n"); 197 } 198 #endif /* UVMHIST */ 199 200 #endif /* _KERNEL */ 201 202 #endif /* _UVM_UVM_STAT_H_ */ 203