#ifndef _SYS_KERNHIST_H_
#define _SYS_KERNHIST_H_
#if defined(_KERNEL_OPT)
#include "opt_ddb.h"
#include "opt_kernhist.h"
#endif
#include <sys/queue.h>
#ifdef KERNHIST
#include <sys/cpu.h>
#endif
struct kern_history_ent {
struct bintime bt;
uint32_t cpunum;
uint32_t call;
const char *fmt;
size_t fmtlen;
const char *fn;
size_t fnlen;
uintmax_t v[4];
};
struct kern_history {
const char *name;
size_t namelen;
LIST_ENTRY(kern_history) list;
uint32_t n;
uint32_t f;
struct kern_history_ent *e;
int s;
};
#define KERNHIST_SYSCTL_VERSION 1
struct sysctl_history_event {
struct bintime she_bintime;
uintmax_t she_values[4];
uint32_t she_callnumber;
uint32_t she_cpunum;
uint32_t she_fmtoffset;
uint32_t she_funcoffset;
};
struct sysctl_history {
uint32_t filler;
uint32_t sh_nameoffset;
uint32_t sh_numentries;
uint32_t sh_nextfree;
struct sysctl_history_event
sh_events[];
};
LIST_HEAD(kern_history_head, kern_history);
#define MAXHISTS 32
#define KERNHIST_UVMMAPHIST __BIT(0)
#define KERNHIST_UVMPDHIST __BIT(1)
#define KERNHIST_UVMUBCHIST __BIT(2)
#define KERNHIST_UVMLOANHIST __BIT(3)
#define KERNHIST_USBHIST __BIT(4)
#define KERNHIST_SCDEBUGHIST __BIT(5)
#define KERNHIST_BIOHIST __BIT(6)
#ifdef _KERNEL
#ifndef KERNHIST
#define KERNHIST_DECL(NAME)
#define KERNHIST_DEFINE(NAME)
#define KERNHIST_INIT(NAME,N)
#define KERNHIST_LOG(NAME,FMT,A,B,C,D)
#define KERNHIST_CALLARGS(NAME,FMT,A,B,C,D)
#define KERNHIST_CALLED(NAME)
#define KERNHIST_FUNC(FNAME)
#define KERNHIST_DUMP(NAME)
#else
#include <sys/kernel.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
extern struct kern_history_head kern_histories;
#define KERNHIST_DECL(NAME) extern struct kern_history NAME
#define KERNHIST_DEFINE(NAME) struct kern_history NAME
#define KERNHIST_LINK_STATIC(NAME) \
do { \
LIST_INSERT_HEAD(&kern_histories, &(NAME), list); \
sysctl_kernhist_new(&(NAME)); \
} while (0)
#define KERNHIST_INIT(NAME,N) \
do { \
(NAME).name = __STRING(NAME); \
(NAME).namelen = strlen(__STRING(NAME)); \
(NAME).n = (N); \
(NAME).f = 0; \
(NAME).e = (struct kern_history_ent *) \
kmem_zalloc(sizeof(struct kern_history_ent) * (N), KM_SLEEP); \
(NAME).s = 0; \
KERNHIST_LINK_STATIC(NAME); \
} while (0)
#define KERNHIST_INITIALIZER(NAME,BUF) \
{ \
.name = __STRING(NAME), \
.namelen = sizeof(__STRING(NAME)) - 1, \
.n = sizeof(BUF) / sizeof(struct kern_history_ent), \
.f = 0, \
.e = (struct kern_history_ent *) (BUF), \
.s = 0, \
\
}
#ifndef KERNHIST_DELAY
#define KERNHIST_DELAY 100000
#endif
#if defined(KERNHIST_PRINT)
extern int kernhist_print_enabled;
#define KERNHIST_PRINTNOW(E) \
do { \
if (kernhist_print_enabled) { \
kernhist_entry_print(E, printf); \
if (KERNHIST_DELAY != 0) \
DELAY(KERNHIST_DELAY); \
} \
} while (0)
#else
#define KERNHIST_PRINTNOW(E)
#endif
#define KERNHIST_LOG(NAME,FMT,A,B,C,D) \
do { \
unsigned int _i_, _j_; \
do { \
_i_ = (NAME).f; \
_j_ = (_i_ + 1 < (NAME).n) ? _i_ + 1 : 0; \
} while (atomic_cas_uint(&(NAME).f, _i_, _j_) != _i_); \
struct kern_history_ent * const _e_ = &(NAME).e[_i_]; \
if (__predict_true(!cold)) \
bintime(&_e_->bt); \
_e_->cpunum = (uint32_t)cpu_number(); \
_e_->fmt = (FMT); \
_e_->fmtlen = strlen(FMT); \
_e_->fn = _kernhist_name; \
_e_->fnlen = strlen(_kernhist_name); \
_e_->call = _kernhist_call; \
_e_->v[0] = (uintmax_t)(A); \
_e_->v[1] = (uintmax_t)(B); \
_e_->v[2] = (uintmax_t)(C); \
_e_->v[3] = (uintmax_t)(D); \
KERNHIST_PRINTNOW(_e_); \
} while (0)
#define KERNHIST_CALLED(NAME) \
do { \
_kernhist_call = atomic_inc_32_nv(&_kernhist_cnt); \
KERNHIST_LOG(NAME, "called!", 0, 0, 0, 0); \
} while (0)
#define KERNHIST_CALLARGS(NAME, FMT, A, B, C, D) \
do { \
_kernhist_call = atomic_inc_32_nv(&_kernhist_cnt); \
KERNHIST_LOG(NAME, "called: "FMT, (A), (B), (C), (D)); \
} while (0)
#define KERNHIST_FUNC(FNAME) \
static uint32_t _kernhist_cnt = 0; \
static const char *const _kernhist_name = FNAME; \
uint32_t _kernhist_call = 0;
#ifdef DDB
#define KERNHIST_DUMP(NAME) kernhist_dump(&NAME, 0, printf)
#else
#define KERNHIST_DUMP(NAME)
#endif
static __inline void
kernhist_entry_print(const struct kern_history_ent *e, void (*pr)(const char *, ...) __printflike(1, 2))
{
struct timeval tv;
bintime2timeval(&e->bt, &tv);
pr("%06ld.%06ld ", (long int)tv.tv_sec, (long int)tv.tv_usec);
pr("%s#%" PRIu32 "@%" PRIu32 ": ", e->fn, e->call, e->cpunum);
pr(e->fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
pr("\n");
}
#if defined(DDB)
void kernhist_dump(struct kern_history *, size_t, void (*)(const char *, ...) __printflike(1, 2));
void kernhist_print(void *, size_t, const char *, void (*)(const char *, ...) __printflike(1, 2));
#endif
void sysctl_kernhist_init(void);
void sysctl_kernhist_new(struct kern_history *);
#endif
#endif
#endif