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