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

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

主站蜘蛛池模板: 高淳县| 盘山县| 任丘市| 中卫市| 克拉玛依市| 南岸区| 普定县| 新龙县| 比如县| 津南区| 陆丰市| 吉林省| 龙山县| 扎赉特旗| 刚察县| 怀宁县| 沧源| 南宁市| 翁牛特旗| 门源| 泗阳县| 合江县| 鹿泉市| 利川市| 东兰县| 运城市| 长沙市| 安仁县| 浦北县| 来宾市| 郸城县| 南江县| 盐边县| 治多县| 文安县| 通城县| 无为县| 额济纳旗| 耿马| 安龙县| 广南县|