xref: /minix3/sys/sys/kernhist.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: kernhist.h,v 1.9 2014/03/30 15:53:37 matt Exp $	*/
26c8f7fc3SBen Gras 
36c8f7fc3SBen Gras /*
46c8f7fc3SBen Gras  * Copyright (c) 1997 Charles D. Cranor and Washington University.
56c8f7fc3SBen Gras  * All rights reserved.
66c8f7fc3SBen Gras  *
76c8f7fc3SBen Gras  * Redistribution and use in source and binary forms, with or without
86c8f7fc3SBen Gras  * modification, are permitted provided that the following conditions
96c8f7fc3SBen Gras  * are met:
106c8f7fc3SBen Gras  * 1. Redistributions of source code must retain the above copyright
116c8f7fc3SBen Gras  *    notice, this list of conditions and the following disclaimer.
126c8f7fc3SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
136c8f7fc3SBen Gras  *    notice, this list of conditions and the following disclaimer in the
146c8f7fc3SBen Gras  *    documentation and/or other materials provided with the distribution.
156c8f7fc3SBen Gras  *
166c8f7fc3SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176c8f7fc3SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186c8f7fc3SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196c8f7fc3SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206c8f7fc3SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216c8f7fc3SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226c8f7fc3SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236c8f7fc3SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246c8f7fc3SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
256c8f7fc3SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266c8f7fc3SBen Gras  *
276c8f7fc3SBen Gras  * from: NetBSD: uvm_stat.h,v 1.49 2011/04/23 18:14:13 rmind Exp
286c8f7fc3SBen Gras  * from: Id: uvm_stat.h,v 1.1.2.4 1998/02/07 01:16:56 chs Exp
296c8f7fc3SBen Gras  */
306c8f7fc3SBen Gras 
316c8f7fc3SBen Gras #ifndef _SYS_KERNHIST_H_
326c8f7fc3SBen Gras #define _SYS_KERNHIST_H_
336c8f7fc3SBen Gras 
346c8f7fc3SBen Gras #if defined(_KERNEL_OPT)
35*0a6a1f1dSLionel Sambuc #include "opt_ddb.h"
366c8f7fc3SBen Gras #include "opt_kernhist.h"
376c8f7fc3SBen Gras #endif
386c8f7fc3SBen Gras 
396c8f7fc3SBen Gras #include <sys/queue.h>
406c8f7fc3SBen Gras #ifdef KERNHIST
416c8f7fc3SBen Gras #include <sys/cpu.h>
426c8f7fc3SBen Gras #endif
436c8f7fc3SBen Gras 
446c8f7fc3SBen Gras /*
456c8f7fc3SBen Gras  * kernel history/tracing, was uvm_stat
466c8f7fc3SBen Gras  */
476c8f7fc3SBen Gras 
486c8f7fc3SBen Gras struct kern_history_ent {
496c8f7fc3SBen Gras 	struct timeval tv; 		/* time stamp */
506c8f7fc3SBen Gras 	int cpunum;
516c8f7fc3SBen Gras 	const char *fmt;		/* printf format */
526c8f7fc3SBen Gras 	size_t fmtlen;			/* length of printf format */
536c8f7fc3SBen Gras 	const char *fn;			/* function name */
546c8f7fc3SBen Gras 	size_t fnlen;			/* length of function name */
556c8f7fc3SBen Gras 	u_long call;			/* function call number */
566c8f7fc3SBen Gras 	u_long v[4];			/* values */
576c8f7fc3SBen Gras };
586c8f7fc3SBen Gras 
596c8f7fc3SBen Gras struct kern_history {
606c8f7fc3SBen Gras 	const char *name;		/* name of this history */
616c8f7fc3SBen Gras 	size_t namelen;			/* length of name, not including null */
626c8f7fc3SBen Gras 	LIST_ENTRY(kern_history) list;	/* link on list of all histories */
636c8f7fc3SBen Gras 	unsigned int n;			/* number of entries */
646c8f7fc3SBen Gras 	unsigned int f;			/* next free one */
656c8f7fc3SBen Gras 	struct kern_history_ent *e;	/* the allocated entries */
666c8f7fc3SBen Gras };
676c8f7fc3SBen Gras 
686c8f7fc3SBen Gras LIST_HEAD(kern_history_head, kern_history);
696c8f7fc3SBen Gras 
706c8f7fc3SBen Gras /*
716c8f7fc3SBen Gras  * grovelling lists all at once.  we currently do not allow more than
726c8f7fc3SBen Gras  * 32 histories to exist, as the way to dump a number of them at once
736c8f7fc3SBen Gras  * is by calling kern_hist() with a bitmask.
746c8f7fc3SBen Gras  *
756c8f7fc3SBen Gras  * XXX extend this to have a registration function?  however, there
766c8f7fc3SBen Gras  * needs to be static ones as UVM requires this before almost anything
776c8f7fc3SBen Gras  * else is setup.
786c8f7fc3SBen Gras  */
796c8f7fc3SBen Gras 
806c8f7fc3SBen Gras /* this is used to set the size of some arrays */
816c8f7fc3SBen Gras #define	MAXHISTS		32
826c8f7fc3SBen Gras 
836c8f7fc3SBen Gras /* and these are the bit values of each history */
846c8f7fc3SBen Gras #define	KERNHIST_UVMMAPHIST	0x00000001	/* maphist */
856c8f7fc3SBen Gras #define	KERNHIST_UVMPDHIST	0x00000002	/* pdhist */
866c8f7fc3SBen Gras #define	KERNHIST_UVMUBCHIST	0x00000004	/* ubchist */
876c8f7fc3SBen Gras #define	KERNHIST_UVMLOANHIST	0x00000008	/* loanhist */
886c8f7fc3SBen Gras 
896c8f7fc3SBen Gras #ifdef _KERNEL
906c8f7fc3SBen Gras 
916c8f7fc3SBen Gras /*
926c8f7fc3SBen Gras  * macros to use the history/tracing code.  note that KERNHIST_LOG
936c8f7fc3SBen Gras  * must take 4 arguments (even if they are ignored by the format).
946c8f7fc3SBen Gras  */
956c8f7fc3SBen Gras #ifndef KERNHIST
966c8f7fc3SBen Gras #define KERNHIST_DECL(NAME)
976c8f7fc3SBen Gras #define KERNHIST_DEFINE(NAME)
986c8f7fc3SBen Gras #define KERNHIST_INIT(NAME,N)
996c8f7fc3SBen Gras #define KERNHIST_INIT_STATIC(NAME,BUF)
1006c8f7fc3SBen Gras #define KERNHIST_LOG(NAME,FMT,A,B,C,D)
10184d9c625SLionel Sambuc #define KERNHIST_CALLARGS(NAME,FMT,A,B,C,D)
1026c8f7fc3SBen Gras #define KERNHIST_CALLED(NAME)
1036c8f7fc3SBen Gras #define KERNHIST_FUNC(FNAME)
104*0a6a1f1dSLionel Sambuc #define KERNHIST_DUMP(NAME)
1056c8f7fc3SBen Gras #else
1066c8f7fc3SBen Gras #include <sys/kernel.h>		/* for "cold" variable */
1076c8f7fc3SBen Gras #include <sys/atomic.h>
1086c8f7fc3SBen Gras #include <sys/kmem.h>
1096c8f7fc3SBen Gras 
1106c8f7fc3SBen Gras extern	struct kern_history_head kern_histories;
1116c8f7fc3SBen Gras 
1126c8f7fc3SBen Gras #define KERNHIST_DECL(NAME) extern struct kern_history NAME
1136c8f7fc3SBen Gras #define KERNHIST_DEFINE(NAME) struct kern_history NAME
1146c8f7fc3SBen Gras 
1156c8f7fc3SBen Gras #define KERNHIST_INIT(NAME,N) \
1166c8f7fc3SBen Gras do { \
1176c8f7fc3SBen Gras 	(NAME).name = __STRING(NAME); \
1186c8f7fc3SBen Gras 	(NAME).namelen = strlen(__STRING(NAME)); \
1196c8f7fc3SBen Gras 	(NAME).n = (N); \
1206c8f7fc3SBen Gras 	(NAME).f = 0; \
1216c8f7fc3SBen Gras 	(NAME).e = (struct kern_history_ent *) \
1226c8f7fc3SBen Gras 		kmem_zalloc(sizeof(struct kern_history_ent) * (N), KM_SLEEP); \
1236c8f7fc3SBen Gras 	LIST_INSERT_HEAD(&kern_histories, &(NAME), list); \
1246c8f7fc3SBen Gras } while (/*CONSTCOND*/ 0)
1256c8f7fc3SBen Gras 
126*0a6a1f1dSLionel Sambuc #define KERNHIST_INITIALIZER(NAME,BUF) \
127*0a6a1f1dSLionel Sambuc { \
128*0a6a1f1dSLionel Sambuc 	.name = __STRING(NAME), \
129*0a6a1f1dSLionel Sambuc 	.namelen = sizeof(__STRING(NAME)) - 1, \
130*0a6a1f1dSLionel Sambuc 	.n = sizeof(BUF) / sizeof(struct kern_history_ent), \
131*0a6a1f1dSLionel Sambuc 	.f = 0, \
132*0a6a1f1dSLionel Sambuc 	.e = (struct kern_history_ent *) (BUF), \
133*0a6a1f1dSLionel Sambuc 	/* BUF will inititalized to zeroes by being in .bss */ \
134*0a6a1f1dSLionel Sambuc }
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc #define KERNHIST_LINK_STATIC(NAME) \
137*0a6a1f1dSLionel Sambuc 	LIST_INSERT_HEAD(&kern_histories, &(NAME), list)
138*0a6a1f1dSLionel Sambuc 
1396c8f7fc3SBen Gras #define KERNHIST_INIT_STATIC(NAME,BUF) \
1406c8f7fc3SBen Gras do { \
1416c8f7fc3SBen Gras 	(NAME).name = __STRING(NAME); \
1426c8f7fc3SBen Gras 	(NAME).namelen = strlen(__STRING(NAME)); \
1436c8f7fc3SBen Gras 	(NAME).n = sizeof(BUF) / sizeof(struct kern_history_ent); \
1446c8f7fc3SBen Gras 	(NAME).f = 0; \
1456c8f7fc3SBen Gras 	(NAME).e = (struct kern_history_ent *) (BUF); \
1466c8f7fc3SBen Gras 	memset((NAME).e, 0, sizeof(struct kern_history_ent) * (NAME).n); \
147*0a6a1f1dSLionel Sambuc 	KERNHIST_LINK_STATIC(NAME); \
1486c8f7fc3SBen Gras } while (/*CONSTCOND*/ 0)
1496c8f7fc3SBen Gras 
15084d9c625SLionel Sambuc #ifndef KERNHIST_DELAY
15184d9c625SLionel Sambuc #define KERNHIST_DELAY	100000
15284d9c625SLionel Sambuc #endif
15384d9c625SLionel Sambuc 
1546c8f7fc3SBen Gras #if defined(KERNHIST_PRINT)
1556c8f7fc3SBen Gras extern int kernhist_print_enabled;
1566c8f7fc3SBen Gras #define KERNHIST_PRINTNOW(E) \
1576c8f7fc3SBen Gras do { \
1586c8f7fc3SBen Gras 		if (kernhist_print_enabled) { \
1596c8f7fc3SBen Gras 			kernhist_entry_print(E); \
16084d9c625SLionel Sambuc 			if (KERNHIST_DELAY != 0) \
16184d9c625SLionel Sambuc 				DELAY(KERNHIST_DELAY); \
1626c8f7fc3SBen Gras 		} \
1636c8f7fc3SBen Gras } while (/*CONSTCOND*/ 0)
1646c8f7fc3SBen Gras #else
1656c8f7fc3SBen Gras #define KERNHIST_PRINTNOW(E) /* nothing */
1666c8f7fc3SBen Gras #endif
1676c8f7fc3SBen Gras 
1686c8f7fc3SBen Gras #define KERNHIST_LOG(NAME,FMT,A,B,C,D) \
1696c8f7fc3SBen Gras do { \
1706c8f7fc3SBen Gras 	unsigned int _i_, _j_; \
1716c8f7fc3SBen Gras 	do { \
1726c8f7fc3SBen Gras 		_i_ = (NAME).f; \
1736c8f7fc3SBen Gras 		_j_ = (_i_ + 1 < (NAME).n) ? _i_ + 1 : 0; \
1746c8f7fc3SBen Gras 	} while (atomic_cas_uint(&(NAME).f, _i_, _j_) != _i_); \
1756c8f7fc3SBen Gras 	struct kern_history_ent * const _e_ = &(NAME).e[_i_]; \
1766c8f7fc3SBen Gras 	if (__predict_true(!cold)) \
1776c8f7fc3SBen Gras 		microtime(&_e_->tv); \
1786c8f7fc3SBen Gras 	_e_->cpunum = cpu_number(); \
1796c8f7fc3SBen Gras 	_e_->fmt = (FMT); \
1806c8f7fc3SBen Gras 	_e_->fmtlen = strlen(FMT); \
1816c8f7fc3SBen Gras 	_e_->fn = _kernhist_name; \
1826c8f7fc3SBen Gras 	_e_->fnlen = strlen(_kernhist_name); \
1836c8f7fc3SBen Gras 	_e_->call = _kernhist_call; \
1846c8f7fc3SBen Gras 	_e_->v[0] = (u_long)(A); \
1856c8f7fc3SBen Gras 	_e_->v[1] = (u_long)(B); \
1866c8f7fc3SBen Gras 	_e_->v[2] = (u_long)(C); \
1876c8f7fc3SBen Gras 	_e_->v[3] = (u_long)(D); \
1886c8f7fc3SBen Gras 	KERNHIST_PRINTNOW(_e_); \
1896c8f7fc3SBen Gras } while (/*CONSTCOND*/ 0)
1906c8f7fc3SBen Gras 
1916c8f7fc3SBen Gras #define KERNHIST_CALLED(NAME) \
1926c8f7fc3SBen Gras do { \
1936c8f7fc3SBen Gras 	_kernhist_call = atomic_inc_uint_nv(&_kernhist_cnt); \
1946c8f7fc3SBen Gras 	KERNHIST_LOG(NAME, "called!", 0, 0, 0, 0); \
1956c8f7fc3SBen Gras } while (/*CONSTCOND*/ 0)
1966c8f7fc3SBen Gras 
19784d9c625SLionel Sambuc /*
19884d9c625SLionel Sambuc  * This extends kernhist to avoid wasting a separate "called!" entry on every
19984d9c625SLionel Sambuc  * function.
20084d9c625SLionel Sambuc  */
20184d9c625SLionel Sambuc #define KERNHIST_CALLARGS(NAME, FMT, A, B, C, D) \
20284d9c625SLionel Sambuc do { \
20384d9c625SLionel Sambuc 	_kernhist_call = atomic_inc_uint_nv(&_kernhist_cnt); \
20484d9c625SLionel Sambuc 	KERNHIST_LOG(NAME, "called: "FMT, (A), (B), (C), (D)); \
20584d9c625SLionel Sambuc } while (/*CONSTCOND*/ 0)
20684d9c625SLionel Sambuc 
2076c8f7fc3SBen Gras #define KERNHIST_FUNC(FNAME) \
2086c8f7fc3SBen Gras 	static unsigned int _kernhist_cnt = 0; \
2096c8f7fc3SBen Gras 	static const char *const _kernhist_name = FNAME; \
21084d9c625SLionel Sambuc 	unsigned int _kernhist_call = 0;
2116c8f7fc3SBen Gras 
212*0a6a1f1dSLionel Sambuc #ifdef DDB
213*0a6a1f1dSLionel Sambuc #define KERNHIST_DUMP(NAME)	kernhist_dump(&NAME)
214*0a6a1f1dSLionel Sambuc #else
215*0a6a1f1dSLionel Sambuc #define KERNHIST_DUMP(NAME)
216*0a6a1f1dSLionel Sambuc #endif
217*0a6a1f1dSLionel Sambuc 
218*0a6a1f1dSLionel Sambuc 
2196c8f7fc3SBen Gras static inline void kernhist_entry_print(const struct kern_history_ent *);
2206c8f7fc3SBen Gras 
2216c8f7fc3SBen Gras static inline void
kernhist_entry_print(const struct kern_history_ent * e)2226c8f7fc3SBen Gras kernhist_entry_print(const struct kern_history_ent *e)
2236c8f7fc3SBen Gras {
2246c8f7fc3SBen Gras 	printf("%06" PRIu64 ".%06d ", e->tv.tv_sec, e->tv.tv_usec);
2256c8f7fc3SBen Gras 	printf("%s#%ld@%d: ", e->fn, e->call, e->cpunum);
2266c8f7fc3SBen Gras 	printf(e->fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
2276c8f7fc3SBen Gras 	printf("\n");
2286c8f7fc3SBen Gras }
2296c8f7fc3SBen Gras 
2306c8f7fc3SBen Gras #if defined(DDB)
231*0a6a1f1dSLionel Sambuc void	kernhist_dump(struct kern_history *);
2326c8f7fc3SBen Gras void	kernhist_print(void (*)(const char *, ...) __printflike(1, 2));
2336c8f7fc3SBen Gras #endif /* DDB */
2346c8f7fc3SBen Gras 
2356c8f7fc3SBen Gras #endif /* KERNHIST */
2366c8f7fc3SBen Gras 
2376c8f7fc3SBen Gras #endif /* _KERNEL */
2386c8f7fc3SBen Gras 
2396c8f7fc3SBen Gras #endif /* _SYS_KERNHIST_H_ */
240