The dtrace tool

Although debugging utilities such as strace(1) and truss(1) can trace system calls produced by a process, they can be slow and therefore not appropriate for solving performance problems on busy Unix systems. Another tool named DTrace allows you to see what happens behind the scenes on a system-wide basis without the need to modify or recompile anything. It also allows you to work on production systems and watch running programs or server processes dynamically without introducing a big overhead.

Although there is a version of dtrace(1) that works on Linux, the dtrace(1) tool works best on macOS and the other FreeBSD variants.

This subsection will use the dtruss(1) command-line utility that comes with macOS, which is just a dtrace(1) script that shows the system calls of a process and saves you from having to write dtrace(1) code. Note that both dtrace(1) and dtruss(1) need root privileges to run.

The output that dtruss(1) generates looks like the following:

$ sudo dtruss godoc
ioctl(0x3, 0x80086804, 0x7FFEEFBFEC20)           = 0 0
close(0x3)         = 0 0
access("/AppleInternal/XBS/.isChrooted", 0x0, 0x0)   = -1 Err#2
thread_selfid(0x0, 0x0, 0x0)         = 1895378 0
geteuid(0x0, 0x0, 0x0)         = 0 0
getegid(0x0, 0x0, 0x0)         = 0 0

So, dtruss(1) works in the same way as the strace(1) utility. Analogously to strace(1), dtruss(1) will print system call counts when used with the -c parameter:

$ sudo dtruss -c go run unsafe.go 2>&1
    
CALL                                        COUNT
access                                          1
bsdthread_register                              1
getuid                                          1
ioctl                                           1
issetugid                                       1
kqueue                                          1
write                                           1
read                                          244
kevent                                        474
fcntl                                         479
lstat64                                       553

The preceding output will quickly inform you about potential bottlenecks in your Go code or allow you to compare the performance of two different command-line programs.

You need to get used to using utilities such as strace(1), dtrace(1), and dtruss(1), but such tools can make your lives so much easier and better. I strongly suggest that you start learning at least one such tool right now!

You can learn more about the dtrace(1) utility by reading DTrace: Dynamic Tracing in Oracle Solaris, Mac OS X and FreeBSD by Brendan Gregg and Jim Mauro (Prentice Hall, 2011) and by visiting http://dtrace.org/. Please bear in mind that dtrace(1) is much more powerful than strace(1) because it has its own programming language. However, strace(1) is more versatile when all you want to do is to watch the system calls of an executable file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155