#ifndef _SYS_LOCKSTAT_H_
#define _SYS_LOCKSTAT_H_
#ifdef _KERNEL_OPT
#include "opt_dtrace.h"
#include <lockstat.h>
#endif
#include <sys/types.h>
#include <sys/ioccom.h>
#include <sys/lock.h>
#include <sys/queue.h>
#include <sys/time.h>
#if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER)
#include <machine/cpu_counter.h>
#endif
#define IOC_LOCKSTAT_GVERSION _IOR('L', 0, int)
#define LS_VERSION 5
#define IOC_LOCKSTAT_ENABLE _IOW('L', 1, lsenable_t)
#define LE_CALLSITE 0x01
#define LE_ONE_CALLSITE 0x02
#define LE_ONE_LOCK 0x04
#define LE_LOCK 0x08
typedef struct lsenable {
uintptr_t le_csstart;
uintptr_t le_csend;
uintptr_t le_lockstart;
uintptr_t le_lockend;
uintptr_t le_nbufs;
u_int le_flags;
u_int le_mask;
} lsenable_t;
#define IOC_LOCKSTAT_DISABLE _IOR('L', 2, lsdisable_t)
typedef struct lsdisable {
size_t ld_size;
struct timespec ld_time;
uint64_t ld_freq[64];
} lsdisable_t;
#define LB_SPIN 0x00000001
#define LB_SLEEP1 0x00000002
#define LB_SLEEP2 0x00000003
#define LB_NEVENT 0x00000003
#define LB_EVENT_MASK 0x000000ff
#define LB_ADAPTIVE_MUTEX 0x00000100
#define LB_SPIN_MUTEX 0x00000200
#define LB_RWLOCK 0x00000300
#define LB_NOPREEMPT 0x00000400
#define LB_KERNEL_LOCK 0x00000500
#define LB_MISC 0x00000600
#define LB_NLOCK 0x00000600
#define LB_LOCK_MASK 0x0000ff00
#define LB_LOCK_SHIFT 8
#define LB_DTRACE 0x00010000
typedef struct lsbuf {
union {
LIST_ENTRY(lsbuf) list;
SLIST_ENTRY(lsbuf) slist;
TAILQ_ENTRY(lsbuf) tailq;
} lb_chain;
uintptr_t lb_lock;
uintptr_t lb_callsite;
uint64_t lb_times[LB_NEVENT];
uint32_t lb_counts[LB_NEVENT];
uint16_t lb_flags;
uint16_t lb_cpu;
} lsbuf_t;
#if defined(_KERNEL) && defined(__HAVE_CPU_COUNTER) && NLOCKSTAT > 0
#define LOCKSTAT_EVENT(flag, lock, type, count, time) \
do { \
if (__predict_false(flag)) \
lockstat_event((uintptr_t)(lock), \
(uintptr_t)__builtin_return_address(0), \
(type), (count), (time)); \
} while ( 0);
#define LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra) \
do { \
if (__predict_false(flag)) \
lockstat_event((uintptr_t)(lock), (uintptr_t)ra, \
(type), (count), (time)); \
} while ( 0);
#define LOCKSTAT_TIMER(name) uint64_t name = 0
#define LOCKSTAT_COUNTER(name) uint64_t name = 0
#define LOCKSTAT_FLAG(name) int name
#define LOCKSTAT_ENTER(name) name = atomic_load_relaxed(&lockstat_enabled)
#define LOCKSTAT_EXIT(name)
#define LOCKSTAT_START_TIMER(flag, name) \
do { \
if (__predict_false(flag)) \
(name) -= cpu_counter(); \
} while ( 0)
#define LOCKSTAT_STOP_TIMER(flag, name) \
do { \
if (__predict_false(flag)) \
(name) += cpu_counter(); \
} while ( 0)
#define LOCKSTAT_COUNT(name, inc) \
do { \
(name) += (inc); \
} while ( 0)
void lockstat_event(uintptr_t, uintptr_t, u_int, u_int, uint64_t);
#else
#define LOCKSTAT_FLAG(name)
#define LOCKSTAT_ENTER(name)
#define LOCKSTAT_EXIT(name)
#define LOCKSTAT_EVENT(flag, lock, type, count, time)
#define LOCKSTAT_EVENT_RA(flag, lock, type, count, time, ra)
#define LOCKSTAT_TIMER(void)
#define LOCKSTAT_COUNTER(void)
#define LOCKSTAT_START_TIMER(flag, void)
#define LOCKSTAT_STOP_TIMER(flag, void)
#define LOCKSTAT_COUNT(name, int)
#endif
#ifdef KDTRACE_HOOKS
extern volatile u_int lockstat_dtrace_enabled;
#define KDTRACE_LOCKSTAT_ENABLED lockstat_dtrace_enabled
#define LS_COMPRESS(f) \
((((f) & 0x3) | (((f) & 0x700) >> 6)) & (LS_NPROBES - 1))
#define LS_NPROBES 0x20
extern uint32_t lockstat_probemap[];
extern void (*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
uintptr_t, uintptr_t, uintptr_t);
void lockstat_probe_stub(uint32_t, uintptr_t, uintptr_t,
uintptr_t, uintptr_t, uintptr_t);
#else
#define KDTRACE_LOCKSTAT_ENABLED 0
#endif
#if defined(_KERNEL) && NLOCKSTAT > 0
extern __cpu_simple_lock_t lockstat_enabled_lock;
extern volatile u_int lockstat_enabled;
extern volatile u_int lockstat_dev_enabled;
#define LOCKSTAT_ENABLED_UPDATE_BEGIN() do \
{ \
__cpu_simple_lock(&lockstat_enabled_lock); \
} while (0)
#define LOCKSTAT_ENABLED_UPDATE_END() do \
{ \
atomic_store_relaxed(&lockstat_enabled, \
lockstat_dev_enabled | KDTRACE_LOCKSTAT_ENABLED); \
__cpu_simple_unlock(&lockstat_enabled_lock); \
} while (0)
#endif
#endif