- Go Systems Programming
- Mihalis Tsoukalos
- 458字
- 2021-07-02 18:08:04
The DTrace utility
Although debugging utilities, such as strace(1) and truss(1), which work on FreeBSD, 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(1), which uses the DTrace facility, 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.
This subsection will use the dtruss(1) command-line utility, which is just a dtrace(1) script, that shows the system calls of a process. The output that dtruss(1) generates when examining the addCLAImproved.go executable on a macOS machine looks similar to the one that you can see in the following screenshot:

Once again, the following part of the output verifies that at the end of the day, everything on Unix machines is translated into C system calls and functions because this is the only way to communicate with the Unix kernel. You can display all the calls to the write(2) system call as follows:
$ sudo dtruss -c ./addCLAImproved 2000 2>&1 | grep write
However, this time you are going to get lots of output because the macOS executable uses write(2) multiple times instead of just once to print the same output.
What is really interesting is the last part of the output of the following command:
$ sudo dtruss -c ./addCLAImproved 2000 CALL COUNT __pthread_sigmask 1 exit 1 getpid 1 ioctl 1 issetugid 1 read 1 thread_selfid 1 ulock_wake 1 bsdthread_register 2 close 2 csops 2 open 2 select 2 sysctl 3 mmap 7 mprotect 8 stat64 41 write 83
The reason you get this output is the -c option that tells dtruss(1) to count all system calls and print a summary of them, which in this case shows that write(2) has been called 83 times and stat64(2) 41 times.
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 and by visiting http://dtrace.org/.