To enable ensure kernel is compiled with:

makeoptions     DEBUG=-g                # Build kernel with gdb(1) debug symbols
makeoptions     WITH_CTF=1              # Run ctfconvert(1) for DTrace support
options         KDTRACE_FRAME           # Ensure frames are compiled in
options         KDTRACE_HOOKS           # Kernel DTrace hooks
options         DDB_CTF                 # Kernel ELF linker loads CTF data

Build kernel, reboot:

kldload dtraceall

To check for example redis-server:

dtrace -x ustackframes=100 -n 'profile-197 /execname == "redis-server" && arg1/ {@[ustack()] = count(); } tick-60s { exit(0); }' -o out.stacks

if you do a dtrace -l will find:

47296    profile    profile-197
47297    profile    tick-60s
47298    profile    tick-10s
47299    profile    profile-99

arg1 is for user land, arg0 for the kernel.

To sample the kernel:

dtrace -x stackframes=100 -n 'profile-197 /arg0/ { @[stack()] = count(); } tick-60s { exit(0); }' -o out.stacks

to create a flame graph:

git clone https://github.com/brendangregg/FlameGraph
cd FlameGraph
./stackcollapse.pl out.stacks > out.folded
./flamegraph.pl out.folded > out.svg

Check https://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html