官术网_书友最值得收藏!

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:

Using the dtruss(1) command on a macOS machine

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.

Starting to realize that not all Unix systems work the same way, despite their numerous similarities, is marvelous. But this also means that you should not make any assumptions about the way a Unix system works behind the scenes.

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.

The dtrace(1) utility is much more powerful than strace(1) and has its own programming language but is more difficult to learn. Additionally, even though there is a Linux version of dtrace(1), strace(1) is more mature on Linux systems and does the job of tracing system calls in a simpler way.

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/.

主站蜘蛛池模板: 上饶县| 垫江县| 临西县| 格尔木市| 安福县| 革吉县| 房产| 武强县| 濮阳县| 屯留县| 吉安市| 贵港市| 修文县| 阿坝县| 达孜县| 萨迦县| 凯里市| 商洛市| 沧州市| 张家川| 隆林| 犍为县| 无极县| 浪卡子县| 南宫市| 江川县| 珠海市| 句容市| 富平县| 宜丰县| 乌苏市| 偃师市| 满城县| 湘潭市| 沙湾县| 新沂市| 亚东县| 高雄市| 渝中区| 贵溪市| 贵州省|