Home | History | Annotate | Line # | Download | only in uvm
uvm_stat.h revision 1.43
      1 /*	$NetBSD: uvm_stat.h,v 1.43 2008/05/25 16:02:08 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 	int n;				/* number of entries */
     74 	int f; 				/* next free one */
     75 	int unused;			/* old location of lock */
     76 	struct uvm_history_ent *e;	/* the malloc'd entries */
     77 	kmutex_t l;			/* lock on this history */
     78 };
     79 
     80 LIST_HEAD(uvm_history_head, uvm_history);
     81 
     82 /*
     83  * grovelling lists all at once.  we currently do not allow more than
     84  * 32 histories to exist, as the way to dump a number of them at once
     85  * is by calling uvm_hist() with a bitmask.
     86  */
     87 
     88 /* this is used to set the size of some arrays */
     89 #define	MAXHISTS		32	/* do not change this! */
     90 
     91 /* and these are the bit values of each history */
     92 #define	UVMHIST_MAPHIST		0x00000001	/* maphist */
     93 #define	UVMHIST_PDHIST		0x00000002	/* pdhist */
     94 #define	UVMHIST_UBCHIST		0x00000004	/* ubchist */
     95 #define	UVMHIST_LOANHIST	0x00000008	/* loanhist */
     96 
     97 #ifdef _KERNEL
     98 
     99 /*
    100  * macros to use the history/tracing code.  note that UVMHIST_LOG
    101  * must take 4 arguments (even if they are ignored by the format).
    102  */
    103 #ifndef UVMHIST
    104 #define UVMHIST_DECL(NAME)
    105 #define UVMHIST_INIT(NAME,N)
    106 #define UVMHIST_INIT_STATIC(NAME,BUF)
    107 #define UVMHIST_LOG(NAME,FMT,A,B,C,D)
    108 #define UVMHIST_CALLED(NAME)
    109 #define UVMHIST_FUNC(FNAME)
    110 #define uvmhist_dump(NAME)
    111 #else
    112 #include <sys/kernel.h>		/* for "cold" variable */
    113 
    114 extern	struct uvm_history_head uvm_histories;
    115 
    116 #define UVMHIST_DECL(NAME) struct uvm_history NAME
    117 
    118 #define UVMHIST_INIT(NAME,N) \
    119 do { \
    120 	(NAME).name = __STRING(NAME); \
    121 	(NAME).namelen = strlen(__STRING(NAME)); \
    122 	(NAME).n = (N); \
    123 	(NAME).f = 0; \
    124 	mutex_init(&(NAME).l, MUTEX_SPIN, IPL_HIGH); \
    125 	(NAME).e = (struct uvm_history_ent *) \
    126 		malloc(sizeof(struct uvm_history_ent) * (N), M_TEMP, \
    127 		    M_WAITOK); \
    128 	memset((NAME).e, 0, sizeof(struct uvm_history_ent) * (N)); \
    129 	LIST_INSERT_HEAD(&uvm_histories, &(NAME), list); \
    130 } while (/*CONSTCOND*/ 0)
    131 
    132 #define UVMHIST_INIT_STATIC(NAME,BUF) \
    133 do { \
    134 	(NAME).name = __STRING(NAME); \
    135 	(NAME).namelen = strlen(__STRING(NAME)); \
    136 	(NAME).n = sizeof(BUF) / sizeof(struct uvm_history_ent); \
    137 	(NAME).f = 0; \
    138 	mutex_init(&(NAME).l, MUTEX_SPIN, IPL_HIGH); \
    139 	(NAME).e = (struct uvm_history_ent *) (BUF); \
    140 	memset((NAME).e, 0, sizeof(struct uvm_history_ent) * (NAME).n); \
    141 	LIST_INSERT_HEAD(&uvm_histories, &(NAME), list); \
    142 } while (/*CONSTCOND*/ 0)
    143 
    144 #if defined(UVMHIST_PRINT)
    145 extern int uvmhist_print_enabled;
    146 #define UVMHIST_PRINTNOW(E) \
    147 do { \
    148 		if (uvmhist_print_enabled) { \
    149 			uvmhist_print(E); \
    150 			DELAY(100000); \
    151 		} \
    152 } while (/*CONSTCOND*/ 0)
    153 #else
    154 #define UVMHIST_PRINTNOW(E) /* nothing */
    155 #endif
    156 
    157 #define UVMHIST_LOG(NAME,FMT,A,B,C,D) \
    158 do { \
    159 	int _i_; \
    160 	mutex_enter(&(NAME).l); \
    161 	_i_ = (NAME).f; \
    162 	(NAME).f = (_i_ + 1 < (NAME).n) ? _i_ + 1 : 0; \
    163 	mutex_exit(&(NAME).l); \
    164 	if (!cold) \
    165 		microtime(&(NAME).e[_i_].tv); \
    166 	(NAME).e[_i_].cpunum = cpu_number(); \
    167 	(NAME).e[_i_].fmt = (FMT); \
    168 	(NAME).e[_i_].fmtlen = strlen(FMT); \
    169 	(NAME).e[_i_].fn = _uvmhist_name; \
    170 	(NAME).e[_i_].fnlen = strlen(_uvmhist_name); \
    171 	(NAME).e[_i_].call = _uvmhist_call; \
    172 	(NAME).e[_i_].v[0] = (u_long)(A); \
    173 	(NAME).e[_i_].v[1] = (u_long)(B); \
    174 	(NAME).e[_i_].v[2] = (u_long)(C); \
    175 	(NAME).e[_i_].v[3] = (u_long)(D); \
    176 	UVMHIST_PRINTNOW(&((NAME).e[_i_])); \
    177 } while (/*CONSTCOND*/ 0)
    178 
    179 #define UVMHIST_CALLED(NAME) \
    180 do { \
    181 	{ \
    182 		mutex_enter(&(NAME).l); \
    183 		_uvmhist_call = _uvmhist_cnt++; \
    184 		mutex_exit(&(NAME).l); \
    185 	} \
    186 	UVMHIST_LOG(NAME,"called!", 0, 0, 0, 0); \
    187 } while (/*CONSTCOND*/ 0)
    188 
    189 #define UVMHIST_FUNC(FNAME) \
    190 	static int _uvmhist_cnt = 0; \
    191 	static const char *const _uvmhist_name = FNAME; \
    192 	int _uvmhist_call;
    193 
    194 static __inline void uvmhist_print(struct uvm_history_ent *);
    195 
    196 static __inline void
    197 uvmhist_print(e)
    198 	struct uvm_history_ent *e;
    199 {
    200 	printf("%06ld.%06ld ", e->tv.tv_sec, e->tv.tv_usec);
    201 	printf("%s#%ld@%d: ", e->fn, e->call, e->cpunum);
    202 	printf(e->fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
    203 	printf("\n");
    204 }
    205 #endif /* UVMHIST */
    206 
    207 #endif /* _KERNEL */
    208 
    209 #endif /* _UVM_UVM_STAT_H_ */
    210