#include "opt_cpu.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/caps.h>
#include <sys/flame_graph.h>
#include <sys/thread2.h>
#include <machine/atomic.h>
#include <machine/cpufunc.h>
#include <machine/cputypes.h>
#include <machine/psl.h>
#include <machine/segments.h>
#include <machine/tss.h>
#include <machine/specialreg.h>
#include <machine/globaldata.h>
#include <machine/md_var.h>
__read_frequently static struct flame_graph_pcpu *flame_graph_array;
__read_frequently static int flame_graph_enable;
SYSCTL_INT(_debug, OID_AUTO, flame_graph_enable, CTLFLAG_RW,
&flame_graph_enable, 0, "Collect data for flame graphs");
SYSCTL_LONG(_debug, OID_AUTO, flame_graph_array, CTLFLAG_RD,
&flame_graph_array, 0, "Collect data for flame graphs");
static MALLOC_DEFINE(M_FLAME, "flame_graphs", "Flame Graphs");
static void
hard_sniff_init(void *arg)
{
struct flame_graph_pcpu *fg;
struct flame_graph_entry *fge;
int n;
flame_graph_array = kmalloc(sizeof(*fg) * ncpus,
M_FLAME, M_WAITOK | M_CACHEALIGN | M_ZERO);
for (n = 0; n < ncpus; ++n) {
fge = kmalloc(sizeof(*fge) * FLAME_GRAPH_NENTRIES,
M_FLAME, M_WAITOK | M_CACHEALIGN | M_ZERO);
fg = &flame_graph_array[n];
fg->nentries = FLAME_GRAPH_NENTRIES;
fg->fge = fge;
}
}
SYSINIT(swi_vm_setup, SI_BOOT2_MACHDEP, SI_ORDER_ANY, hard_sniff_init, NULL);
void
hard_sniff(struct trapframe *tf)
{
globaldata_t gd = mycpu;
thread_t td;
char *top;
char *bot;
char *rbp;
char *rip;
struct flame_graph_pcpu *fg;
struct flame_graph_entry *fge;
int n;
gd->gd_sample_pc = (void *)(intptr_t)tf->tf_rip;
gd->gd_sample_sp = (void *)(intptr_t)tf->tf_rsp;
if (flame_graph_enable == 0)
return;
td = gd->gd_curthread;
if (td == NULL)
return;
bot = (char *)td->td_kstack + PAGE_SIZE;
top = (char *)td->td_kstack + td->td_kstack_size;
if (bot >= top)
return;
fg = &flame_graph_array[gd->gd_cpuid];
fge = &fg->fge[fg->windex % FLAME_GRAPH_NENTRIES];
rip = (char *)(intptr_t)tf->tf_rip;
fge->rips[0] = (intptr_t)rip;
rbp = (char *)(intptr_t)tf->tf_rbp;
for (n = 1; n < FLAME_GRAPH_FRAMES - 1; ++n) {
if (rbp < bot || rbp > top - 8 || ((intptr_t)rbp & 7))
break;
fge->rips[n] = (intptr_t)*(char **)(rbp + 8);
if (*(char **)rbp <= rbp)
break;
rbp = *(char **)rbp;
}
fge->rips[n] = 0;
cpu_sfence();
++fg->windex;
}
static int
sysctl_flame_graph_data(SYSCTL_HANDLER_ARGS)
{
int error;
int n;
size_t ebytes;
error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT);
if (error)
return error;
if (flame_graph_array == NULL)
return EOPNOTSUPP;
ebytes = sizeof(struct flame_graph_pcpu) +
sizeof(struct flame_graph_entry);
for (n = 0; n < ncpus && error == 0; ++n) {
error = SYSCTL_OUT(req, &ebytes, sizeof(ebytes));
if (error == 0)
error = SYSCTL_OUT(req, flame_graph_array + n,
sizeof(*flame_graph_array));
if (error == 0)
error = SYSCTL_OUT(req, flame_graph_array[n].fge,
sizeof(*flame_graph_array->fge) *
FLAME_GRAPH_NENTRIES);
}
return error;
}
SYSCTL_PROC(_debug, OID_AUTO, flame_graph_data,
(CTLTYPE_OPAQUE|CTLFLAG_RD), 0, 0,
sysctl_flame_graph_data, "S,flames", "Flame Graph Data");
static int
sysctl_flame_graph_sniff(SYSCTL_HANDLER_ARGS)
{
int error;
error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT);
if (error)
return error;
if (flame_graph_enable == 0)
return EINVAL;
if (req->newptr)
smp_sniff();
return(SYSCTL_OUT(req, &error, sizeof(int)));
}
SYSCTL_PROC(_debug, OID_AUTO, flame_graph_sniff,
(CTLTYPE_UINT|CTLFLAG_RW), 0, 0,
sysctl_flame_graph_sniff, "IU", "Flame Graph Poll");