The DTrace One-Liner Tutorial

This teaches you DTrace for FreeBSD in 12 easy lessons, where each lesson is a one-liner you can try running. This series of one-liners introduces concepts which are summarized as bullet points. For an online DTrace reference, see the DTrace Guide, which contains a longer tutorial in Chapter 1.

Contributed by Brendan Gregg (2014), primary author of "DTrace: Dynamic Tracing in Oracle Solaris, Mac OS X, and FreeBSD" (Prentice Hall, 2011).

Lesson 1. Listing Probes

dtrace -l | grep 'syscall.*read'

Lesson 2. Hello World

dtrace -n 'dtrace:::BEGIN { printf("Hello FreeBSD!\n"); }'

Lesson 3. File Opens

dtrace -n 'syscall::open*:entry { printf("%s %s", execname, copyinstr(arg0)); }'

Lesson 4. Syscall Counts By Process

dtrace -n 'syscall:::entry { @[execname, probefunc] = count(); }'

Lesson 5. Distribution of read() Bytes

dtrace -n 'syscall::read:return /execname == "sshd"/ { @ = quantize(arg0); }'

Lesson 6. Timing read() Syscall

dtrace -n 'syscall::read:entry { self->ts = timestamp; } syscall::read:return /self->ts/ {
    @ = quantize(timestamp - self->ts); self->ts = 0; }'

Lesson 7. Measuring CPU Time in read()

dtrace -n 'syscall::read:entry { self->vts = vtimestamp; } syscall::read:return /self->vts/ {
    @["On-CPU us:"] = lquantize((vtimestamp - self->vts) / 1000, 0, 10000, 10); self->vts = 0; }'

Lesson 8. Count Process-Level Events

dtrace -n 'proc::: { @[probename] = count(); } tick-5s { exit(0); }'

Lesson 9. Profile On-CPU Kernel Stacks

dtrace -x stackframes=100 -n 'profile-99 /arg0/ { @[stack()] = count(); }'

Lesson 10. Scheduler Tracing

dtrace -n 'sched:::off-cpu { @[stack(8)] = count(); }'

Lesson 11. TCP Inbound Connections

dtrace -n 'tcp:::accept-established { @[args[3]->tcps_raddr] = count(); }'

Lesson 12. Raw Kernel Tracing

dtrace -n 'fbt::vmem_alloc:entry { @[curthread->td_name, args[0]->vm_name] = sum(arg1); }'

At this point you understand much of DTrace, and can begin to use and write powerful one-liners. You can browse the full list of DTrace One-Liners to pick up more DTrace functionality.


CategoryDtrace CategoryHowTo

DTrace/Tutorial (last edited 2018-03-11T21:43:30+0000 by MarkLinimon)