#if !defined(__APPLE__)
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/systm.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cpuvar.h>
#include <sys/kmem.h>
#include <sys/strsubr.h>
#include <sys/dtrace.h>
#include <sys/cyclic.h>
#include <sys/atomic.h>
#else
#ifdef KERNEL
#ifndef _KERNEL
#define _KERNEL
#endif
#endif
#define MACH__POSIX_C_SOURCE_PRIVATE 1
#include <kern/cpu_data.h>
#include <kern/thread.h>
#include <mach/thread_status.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <miscfs/devfs/devfs.h>
#include <sys/dtrace.h>
#include <sys/dtrace_impl.h>
#include <sys/dtrace_glue.h>
#if defined(__ppc__) || defined(__ppc64__)
extern struct savearea *find_kern_regs(thread_t);
#elif defined(__i386__) || defined(__x86_64__)
extern x86_saved_state_t *find_kern_regs(thread_t);
#else
#error Unknown architecture
#endif
#undef ASSERT
#define ASSERT(x) do {} while(0)
extern void profile_init(void);
#endif
static dev_info_t *profile_devi;
static dtrace_provider_id_t profile_id;
#if !defined(__APPLE__)
#ifdef __x86
#define PROF_ARTIFICIAL_FRAMES 10
#else
#ifdef __sparc
#if DEBUG
#define PROF_ARTIFICIAL_FRAMES 4
#else
#define PROF_ARTIFICIAL_FRAMES 3
#endif
#endif
#endif
#else
#if defined(__ppc__) || defined(__ppc64__)
#define PROF_ARTIFICIAL_FRAMES 8
#elif defined(__i386__) || defined(__x86_64__)
#define PROF_ARTIFICIAL_FRAMES 9
#else
#error Unknown architecture
#endif
#endif
#define PROF_NAMELEN 15
#define PROF_PROFILE 0
#define PROF_TICK 1
#define PROF_PREFIX_PROFILE "profile-"
#define PROF_PREFIX_TICK "tick-"
typedef struct profile_probe {
char prof_name[PROF_NAMELEN];
dtrace_id_t prof_id;
int prof_kind;
hrtime_t prof_interval;
cyclic_id_t prof_cyclic;
} profile_probe_t;
typedef struct profile_probe_percpu {
hrtime_t profc_expected;
hrtime_t profc_interval;
profile_probe_t *profc_probe;
} profile_probe_percpu_t;
hrtime_t profile_interval_min = NANOSEC / 5000;
int profile_aframes = 0;
static int profile_rates[] = {
97, 199, 499, 997, 1999,
4001, 4999, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
};
static int profile_ticks[] = {
1, 10, 100, 500, 1000,
5000, 0, 0, 0, 0,
0, 0, 0, 0, 0
};
#define PROFILE_MAX_DEFAULT 1000
static uint32_t profile_max;
static uint32_t profile_total;
static void
profile_fire(void *arg)
{
profile_probe_percpu_t *pcpu = arg;
profile_probe_t *prof = pcpu->profc_probe;
hrtime_t late;
late = dtrace_gethrtime() - pcpu->profc_expected;
pcpu->profc_expected += pcpu->profc_interval;
#if !defined(__APPLE__)
dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
CPU->cpu_profile_upc, late, 0, 0);
#else
#if defined(__ppc__) || defined(__ppc64__)
{
struct savearea *sv = find_kern_regs(current_thread());
if (sv) {
if (USERMODE(sv->save_srr1)) {
dtrace_probe(prof->prof_id, 0x0, sv->save_srr0, late, 0, 0);
} else {
dtrace_probe(prof->prof_id, sv->save_srr0, 0x0, late, 0, 0);
}
} else {
dtrace_probe(prof->prof_id, 0xcafebabe,
0x0, late, 0, 0);
}
}
#elif defined(__i386__) || defined(__x86_64__)
x86_saved_state_t *kern_regs = find_kern_regs(current_thread());
if (NULL != kern_regs) {
#if defined(__i386__)
dtrace_probe(prof->prof_id, saved_state32(kern_regs)->eip, 0x0, 0, 0, 0);
#elif defined(__x86_64__)
dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, 0, 0, 0);
#else
#error Unknown arch
#endif
} else {
x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread());
if (NULL == tagged_regs) {
dtrace_probe(prof->prof_id, 0xcafebabe,
0x0, 0, 0, 0);
} else if (is_saved_state64(tagged_regs)) {
x86_saved_state64_t *regs = saved_state64(tagged_regs);
dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0);
} else {
x86_saved_state32_t *regs = saved_state32(tagged_regs);
dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0);
}
}
#else
#error Unknown architecture
#endif
#endif
}
static void
profile_tick(void *arg)
{
profile_probe_t *prof = arg;
#if !defined(__APPLE__)
dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
CPU->cpu_profile_upc, 0, 0, 0);
#else
#if defined(__ppc__) || defined(__ppc64__)
{
struct savearea *sv = find_kern_regs(current_thread());
if (sv) {
if (USERMODE(sv->save_srr1)) {
dtrace_probe(prof->prof_id, 0x0, sv->save_srr0, 0, 0, 0);
} else {
dtrace_probe(prof->prof_id, sv->save_srr0, 0x0, 0, 0, 0);
}
} else {
dtrace_probe(prof->prof_id, 0xcafebabe,
0x0, 0, 0, 0);
}
}
#elif defined(__i386__) || defined(__x86_64__)
x86_saved_state_t *kern_regs = find_kern_regs(current_thread());
if (NULL != kern_regs) {
#if defined(__i386__)
dtrace_probe(prof->prof_id, saved_state32(kern_regs)->eip, 0x0, 0, 0, 0);
#elif defined(__x86_64__)
dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, 0, 0, 0);
#else
#error Unknown arch
#endif
} else {
x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread());
if (NULL == tagged_regs) {
dtrace_probe(prof->prof_id, 0xcafebabe,
0x0, 0, 0, 0);
} else if (is_saved_state64(tagged_regs)) {
x86_saved_state64_t *regs = saved_state64(tagged_regs);
dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0);
} else {
x86_saved_state32_t *regs = saved_state32(tagged_regs);
dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0);
}
}
#else
#error Unknown architecture
#endif
#endif
}
static void
profile_create(hrtime_t interval, const char *name, int kind)
{
profile_probe_t *prof;
if (interval < profile_interval_min)
return;
if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
return;
atomic_add_32(&profile_total, 1);
if (profile_total > profile_max) {
atomic_add_32(&profile_total, -1);
return;
}
#if !defined(__APPLE__)
prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
#else
if (PROF_TICK == kind)
prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
else
prof = kmem_zalloc(sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t), KM_SLEEP);
#endif
(void) strlcpy(prof->prof_name, name, sizeof(prof->prof_name));
prof->prof_interval = interval;
prof->prof_cyclic = CYCLIC_NONE;
prof->prof_kind = kind;
prof->prof_id = dtrace_probe_create(profile_id,
NULL, NULL, name,
profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
}
static void
profile_provide(void *arg, const dtrace_probedesc_t *desc)
{
#pragma unused(arg)
int i, j, rate, kind;
hrtime_t val = 0, mult = 1, len;
const char *name, *suffix = NULL;
#if !defined(__APPLE__)
const struct {
char *prefix;
int kind;
} types[] = {
{ PROF_PREFIX_PROFILE, PROF_PROFILE },
{ PROF_PREFIX_TICK, PROF_TICK },
{ NULL, NULL }
};
const struct {
char *name;
hrtime_t mult;
} suffixes[] = {
{ "ns", NANOSEC / NANOSEC },
{ "nsec", NANOSEC / NANOSEC },
{ "us", NANOSEC / MICROSEC },
{ "usec", NANOSEC / MICROSEC },
{ "ms", NANOSEC / MILLISEC },
{ "msec", NANOSEC / MILLISEC },
{ "s", NANOSEC / SEC },
{ "sec", NANOSEC / SEC },
{ "m", NANOSEC * (hrtime_t)60 },
{ "min", NANOSEC * (hrtime_t)60 },
{ "h", NANOSEC * (hrtime_t)(60 * 60) },
{ "hour", NANOSEC * (hrtime_t)(60 * 60) },
{ "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
{ "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
{ "hz", 0 },
{ NULL }
};
#else
const struct {
const char *prefix;
int kind;
} types[] = {
{ PROF_PREFIX_PROFILE, PROF_PROFILE },
{ PROF_PREFIX_TICK, PROF_TICK },
{ NULL, 0 }
};
const struct {
const char *name;
hrtime_t mult;
} suffixes[] = {
{ "ns", NANOSEC / NANOSEC },
{ "nsec", NANOSEC / NANOSEC },
{ "us", NANOSEC / MICROSEC },
{ "usec", NANOSEC / MICROSEC },
{ "ms", NANOSEC / MILLISEC },
{ "msec", NANOSEC / MILLISEC },
{ "s", NANOSEC / SEC },
{ "sec", NANOSEC / SEC },
{ "m", NANOSEC * (hrtime_t)60 },
{ "min", NANOSEC * (hrtime_t)60 },
{ "h", NANOSEC * (hrtime_t)(60 * 60) },
{ "hour", NANOSEC * (hrtime_t)(60 * 60) },
{ "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
{ "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
{ "hz", 0 },
{ NULL, 0 }
};
#endif
if (desc == NULL) {
char n[PROF_NAMELEN];
#if !defined(__APPLE__)
for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
#else
for (i = 0; i < (int)(sizeof (profile_rates) / sizeof (int)); i++) {
#endif
if ((rate = profile_rates[i]) == 0)
continue;
(void) snprintf(n, PROF_NAMELEN, "%s%d",
PROF_PREFIX_PROFILE, rate);
profile_create(NANOSEC / rate, n, PROF_PROFILE);
}
#if !defined(__APPLE__)
for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
#else
for (i = 0; i < (int)(sizeof (profile_ticks) / sizeof (int)); i++) {
#endif
if ((rate = profile_ticks[i]) == 0)
continue;
(void) snprintf(n, PROF_NAMELEN, "%s%d",
PROF_PREFIX_TICK, rate);
profile_create(NANOSEC / rate, n, PROF_TICK);
}
return;
}
name = desc->dtpd_name;
for (i = 0; types[i].prefix != NULL; i++) {
len = strlen(types[i].prefix);
if (strncmp(name, types[i].prefix, len) != 0)
continue;
break;
}
if (types[i].prefix == NULL)
return;
kind = types[i].kind;
j = strlen(name) - len;
for (j = strlen(name); j >= len; j--) {
if (name[j] >= '0' && name[j] <= '9')
break;
suffix = &name[j];
}
ASSERT(suffix != NULL);
for (; j >= len; j--) {
if (name[j] < '0' || name[j] > '9')
return;
val += (name[j] - '0') * mult;
mult *= (hrtime_t)10;
}
if (val == 0)
return;
for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
#if !defined(__APPLE__)
if (strcasecmp(suffixes[i].name, suffix) == 0) {
mult = suffixes[i].mult;
break;
}
#else
if (strncasecmp(suffixes[i].name, suffix, strlen(suffixes[i].name) + 1) == 0) {
mult = suffixes[i].mult;
break;
}
#endif
}
if (suffixes[i].name == NULL && *suffix != '\0')
return;
if (mult == 0) {
val = NANOSEC / val;
} else {
val *= mult;
}
profile_create(val, name, kind);
}
static void
profile_destroy(void *arg, dtrace_id_t id, void *parg)
{
#pragma unused(arg,id)
profile_probe_t *prof = parg;
ASSERT(prof->prof_cyclic == CYCLIC_NONE);
#if !defined(__APPLE__)
kmem_free(prof, sizeof (profile_probe_t));
#else
if (prof->prof_kind == PROF_TICK)
kmem_free(prof, sizeof (profile_probe_t));
else
kmem_free(prof, sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t));
#endif
ASSERT(profile_total >= 1);
atomic_add_32(&profile_total, -1);
}
static void
profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
{
#pragma unused(cpu)
profile_probe_t *prof = arg;
profile_probe_percpu_t *pcpu;
#if !defined(__APPLE__)
pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
#else
pcpu = ((profile_probe_percpu_t *)(&(prof[1]))) + cpu_number();
#endif
pcpu->profc_probe = prof;
hdlr->cyh_func = profile_fire;
hdlr->cyh_arg = pcpu;
hdlr->cyh_level = CY_HIGH_LEVEL;
when->cyt_interval = prof->prof_interval;
#if !defined(__APPLE__)
when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
#else
when->cyt_when = 0;
#endif
pcpu->profc_expected = when->cyt_when;
pcpu->profc_interval = when->cyt_interval;
}
static void
profile_offline(void *arg, cpu_t *cpu, void *oarg)
{
profile_probe_percpu_t *pcpu = oarg;
ASSERT(pcpu->profc_probe == arg);
#if !defined(__APPLE__)
kmem_free(pcpu, sizeof (profile_probe_percpu_t));
#else
#pragma unused(pcpu,arg,cpu)
#endif
}
static void
profile_enable(void *arg, dtrace_id_t id, void *parg)
{
#pragma unused(arg,id)
profile_probe_t *prof = parg;
cyc_omni_handler_t omni;
cyc_handler_t hdlr;
cyc_time_t when;
ASSERT(prof->prof_interval != 0);
ASSERT(MUTEX_HELD(&cpu_lock));
if (prof->prof_kind == PROF_TICK) {
hdlr.cyh_func = profile_tick;
hdlr.cyh_arg = prof;
hdlr.cyh_level = CY_HIGH_LEVEL;
when.cyt_interval = prof->prof_interval;
#if !defined(__APPLE__)
when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
#else
when.cyt_when = 0;
#endif
} else {
ASSERT(prof->prof_kind == PROF_PROFILE);
omni.cyo_online = profile_online;
omni.cyo_offline = profile_offline;
omni.cyo_arg = prof;
}
#if !defined(__APPLE__)
if (prof->prof_kind == PROF_TICK) {
prof->prof_cyclic = cyclic_add(&hdlr, &when);
} else {
prof->prof_cyclic = cyclic_add_omni(&omni);
}
#else
if (prof->prof_kind == PROF_TICK) {
prof->prof_cyclic = cyclic_timer_add(&hdlr, &when);
} else {
prof->prof_cyclic = (cyclic_id_t)cyclic_add_omni(&omni);
}
#endif
}
static void
profile_disable(void *arg, dtrace_id_t id, void *parg)
{
profile_probe_t *prof = parg;
ASSERT(prof->prof_cyclic != CYCLIC_NONE);
ASSERT(MUTEX_HELD(&cpu_lock));
#if !defined(__APPLE__)
cyclic_remove(prof->prof_cyclic);
#else
#pragma unused(arg,id)
if (prof->prof_kind == PROF_TICK) {
cyclic_timer_remove(prof->prof_cyclic);
} else {
cyclic_remove_omni((cyclic_id_list_t)prof->prof_cyclic);
}
#endif
prof->prof_cyclic = CYCLIC_NONE;
}
#if !defined(__APPLE__)
static int
profile_usermode(void *arg, dtrace_id_t id, void *parg)
{
return (CPU->cpu_profile_pc == 0);
}
#else
static int
profile_usermode(void *arg, dtrace_id_t id, void *parg)
{
#pragma unused(arg,id,parg)
return 1;
}
#endif
static dtrace_pattr_t profile_attr = {
{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
};
static dtrace_pops_t profile_pops = {
profile_provide,
NULL,
profile_enable,
profile_disable,
NULL,
NULL,
NULL,
NULL,
profile_usermode,
profile_destroy
};
static int
profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
#if !defined(__APPLE__)
if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
DDI_PSEUDO, NULL) == DDI_FAILURE ||
dtrace_register("profile", &profile_attr,
DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
&profile_pops, NULL, &profile_id) != 0) {
ddi_remove_minor_node(devi, NULL);
return (DDI_FAILURE);
}
profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
"profile-max-probes", PROFILE_MAX_DEFAULT);
#else
if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
DDI_PSEUDO, 0) == DDI_FAILURE ||
dtrace_register("profile", &profile_attr,
DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
&profile_pops, NULL, &profile_id) != 0) {
ddi_remove_minor_node(devi, NULL);
return (DDI_FAILURE);
}
profile_max = PROFILE_MAX_DEFAULT;
#endif
ddi_report_dev(devi);
profile_devi = devi;
return (DDI_SUCCESS);
}
#if !defined(__APPLE__)
static int
profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
switch (cmd) {
case DDI_DETACH:
break;
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (dtrace_unregister(profile_id) != 0)
return (DDI_FAILURE);
ddi_remove_minor_node(devi, NULL);
return (DDI_SUCCESS);
}
static int
profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = (void *)profile_devi;
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
static int
profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
return (0);
}
static struct cb_ops profile_cb_ops = {
profile_open,
nodev,
nulldev,
nulldev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
0,
D_NEW | D_MP
};
static struct dev_ops profile_ops = {
DEVO_REV,
0,
profile_info,
nulldev,
nulldev,
profile_attach,
profile_detach,
nodev,
&profile_cb_ops,
NULL,
nodev
};
static struct modldrv modldrv = {
&mod_driverops,
"Profile Interrupt Tracing",
&profile_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modldrv,
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
#else
d_open_t _profile_open;
int _profile_open(dev_t dev, int flags, int devtype, struct proc *p)
{
#pragma unused(dev,flags,devtype,p)
return 0;
}
#define PROFILE_MAJOR -24
static struct cdevsw profile_cdevsw =
{
_profile_open,
eno_opcl,
eno_rdwrt,
eno_rdwrt,
eno_ioctl,
(stop_fcn_t *)nulldev,
(reset_fcn_t *)nulldev,
NULL,
eno_select,
eno_mmap,
eno_strat,
eno_getc,
eno_putc,
0
};
static int gProfileInited = 0;
void profile_init( void )
{
if (0 == gProfileInited)
{
int majdevno = cdevsw_add(PROFILE_MAJOR, &profile_cdevsw);
if (majdevno < 0) {
printf("profile_init: failed to allocate a major number!\n");
gProfileInited = 0;
return;
}
profile_attach( (dev_info_t *)(uintptr_t)majdevno, DDI_ATTACH );
gProfileInited = 1;
} else
panic("profile_init: called twice!\n");
}
#undef PROFILE_MAJOR
#endif