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