View | Details | Raw Unified | Return to bug 270926
Collapse All | Expand All

(-)b/documentation/content/zh-tw/books/handbook/dtrace/_index.adoc (-32 / +76 lines)
Lines 2-12 Link Here
2
title: 章 24. DTrace
2
title: 章 24. DTrace
3
part: 部 III. 系統管理
3
part: 部 III. 系統管理
4
prev: books/handbook/cutting-edge
4
prev: books/handbook/cutting-edge
5
next: books/handbook/usb-device-mode
5
next: books/handbook/usb-device-mode
6
description: This chapter explains how to use DTrace in FreeBSD
7
tags: ["DTrace", "features", "guide", "tutorial", "kldload"]
6
showBookMenu: true
8
showBookMenu: true
7
weight: 28
9
weight: 30
8
path: "/books/handbook/"
10
path: "/books/handbook/"
11
aliases: ["/en/books/handbook/dtrace-implementation/","/en/books/handbook/dtrace-enable/","/en/books/handbook/dtrace-using/"]
9
---
12
---
10
13
11
[[dtrace]]
14
[[dtrace]]
12
= DTrace
15
= DTrace
Lines 48-98 Link Here
48
51
49
[[dtrace-synopsis]]
52
[[dtrace-synopsis]]
50
== 概述
53
== 概述
51
54
52
DTrace, also known as Dynamic Tracing, was developed by Sun(TM) as a tool for locating performance bottlenecks in production and pre-production systems. In addition to diagnosing performance problems, DTrace can be used to help investigate and debug unexpected behavior in both the FreeBSD kernel and in userland programs.
55
DTrace,又被稱作 Dynamic Tracing ,由 Sun(TM) 開發,用在生產 (production) 跟預生產 (pre-production) 系統中找出效能瓶頸的工具。
56
除了診斷性能問題外,DTrace 還可以用於查詢以及除錯 FreeBSD 核心和使用者層級程式的未預期行為。
53
57
54
DTrace is a remarkable profiling tool, with an impressive array of features for diagnosing system issues. It may also be used to run pre-written scripts to take advantage of its capabilities. Users can author their own utilities using the DTrace D Language, allowing them to customize their profiling based on specific needs.
58
DTrace 是一個卓越的分析工具,具有一系列令人驚豔、用於診斷系統問題的功能。
59
它還可以執行預先寫好的腳本,以使用其功能。
60
使用者可以用 DTrace D 語言編寫自己的工具,從而允許他們根據特定的需求客製化。
55
61
56
The FreeBSD implementation provides full support for kernel DTrace and experimental support for userland DTrace. Userland DTrace allows users to perform function boundary tracing for userland programs using the `pid` provider, and to insert static probes into userland programs for later tracing. Some ports, such as package:databases/postgres-server[] and package:lang/php56[] have a DTrace option to enable static probes. FreeBSD 10.0-RELEASE has reasonably good userland DTrace support, but it is not considered production ready. In particular, it is possible to crash traced programs.
62
FreeBSD 實做提供對核心層級的 DTrace 全面的支援,以及對使用者層級的 DTrace 實驗性的支援。
63
使用者層級的 DTrace 允許使用者使用 `pid` 執行函式邊界追蹤 (function boundary tracing),並將 static probes 插入到使用者程式以供之後追蹤。
64
一些 ports,像是 package:databases/postgresql12-server[] 和 package:lang/php74[] 提供 DTrace 選項,以提供 static probes 功能。
57
65
58
The official guide to DTrace is maintained by the Illumos project at http://dtrace.org/guide[DTrace Guide].
66
DTrace 的官方指南由 Illumos 維護,在 http://dtrace.org/guide[DTrace Guide]。
59
67
60
讀完這章,您將了解:
68
讀完這章,您將了解:
61
69
62
* What DTrace is and what features it provides.
70
* 什麼是 DTrace 以及其提供的功能。
63
* Differences between the Solaris(TM) DTrace implementation and the one provided by FreeBSD.
71
* Solaris(TM) 實做的 DTrace 跟 FreeBSD 提供的 DTrace 之間的不同之處。
64
* How to enable and use DTrace on FreeBSD.
72
* 如何在 FreeBSD 上啟用和使用 DTrace。
65
73
66
在開始閱讀這章之前,您需要:
74
在開始閱讀這章之前,您需要:
67
75
68
* 了解 UNIX(TM) 及 FreeBSD 基礎 (crossref:basics[basics,FreeBSD 基礎])。
76
* 了解 UNIX(TM) 及 FreeBSD 基礎 (crossref:basics[basics,FreeBSD 基礎])。
69
* Have some familiarity with security and how it pertains to FreeBSD (crossref:security[security,安全性]).
77
* 了解安全性以及其跟 FreeBSD 的關係 (crossref:security[security,安全性])。
70
78
71
[[dtrace-implementation]]
79
[[dtrace-implementation]]
72
== 實作差異
80
== 實作差異
73
81
74
While the DTrace in FreeBSD is similar to that found in Solaris(TM), differences do exist. The primary difference is that in FreeBSD, DTrace is implemented as a set of kernel modules and DTrace can not be used until the modules are loaded. To load all of the necessary modules:
82
雖然 FreeBSD 的 DTrace 和 Solaris(TM) 的 DTrace 類似,但是還是有存在差異。
83
最重要的區別為,在 FreeBSD 中,DTrace 是作為一組核心模組 (kernel modules) 實做的,並且在載入模組之前無法使用。
84
要載入所有需要的模組:
75
85
76
[source,shell]
86
[source,shell]
77
....
87
....
78
# kldload dtraceall
88
# kldload dtraceall
79
....
89
....
80
90
81
Beginning with FreeBSD 10.0-RELEASE, the modules are automatically loaded when `dtrace` is run.
91
從 FreeBSD 10.0-RELEASE 之後,模組會在執行 `dtrace` 時自動載入。
82
92
83
FreeBSD uses the `DDB_CTF` kernel option to enable support for loading CTF data from kernel modules and the kernel itself. CTF is the Solaris(TM) Compact C Type Format which encapsulates a reduced form of debugging information similar to DWARF and the venerable stabs. CTF data is added to binaries by the `ctfconvert` and `ctfmerge` build tools. The `ctfconvert` utility parses DWARFELF debug sections created by the compiler and `ctfmerge` merges CTFELF sections from objects into either executables or shared libraries.
93
FreeBSD 使用 `DDB_CTF` 核心選項來支援從核心模組和核心本身載入 `CTF` 資料。
94
`CTF` 是 Solaris(TM) Compact C Type Format,它封裝了一種簡化形式的除錯資訊,類似於 `DWARF` 和 古老的 stabs。
95
`CTF` 資料通過`ctfconvert` and `ctfmerge` 建構工具,加入到二進制文件中。
96
`ctfconvert` 工具分析編譯器創建的 `DWARF``ELF` 除錯部份,而 `ctfmerge` 將目標的 `CTF``ELF` 部份合併到執行檔或函式庫中。
84
97
85
Some different providers exist for FreeBSD than for Solaris(TM). Most notable is the `dtmalloc` provider, which allows tracing `malloc()` by type in the FreeBSD kernel. Some of the providers found in Solaris(TM), such as `cpc` and `mib`, are not present in FreeBSD. These may appear in future versions of FreeBSD. Moreover, some of the providers available in both operating systems are not compatible, in the sense that their probes have different argument types. Thus, D scripts written on Solaris(TM) may or may not work unmodified on FreeBSD, and vice versa.
98
與 Solaris(TM) 相比,FreeBSD 存在一些不同的 providers。
99
最值得注意的是 `dtmalloc` provider 允許在 FreeBSD 核心中按照類型 (type) 追蹤 `malloc()`。
100
Solaris(TM) 中的一些 providers,例如 `cpc` 和 `mib`,在 FreeBSD 中則不存在。
101
這些可能會在 FreeBSD 未來的版本中出現。
102
此外,兩個作業系統中一些可用的 providers 是不相容的,因為他們具有不同的參數類型。
103
因此,在 Solaris(TM) 上拓寫的 `D` 腳本在未經修改的情況下可能可以或不可以在 FreeBSD 上執行,反之亦然。
86
104
87
Due to security differences, only `root` may use DTrace on FreeBSD. Solaris(TM) has a few low level security checks which do not yet exist in FreeBSD. As such, the [.filename]#/dev/dtrace/dtrace# is strictly limited to `root`.
105
因為安全的差異,只有 `root` 可以在 FreeBSD 上使用 DTrace。
106
Solaris(TM) 擁有一些 FreeBSD 中還不存在的低階 (low level) 安全檢查。
107
因此 [.filename]#/dev/dtrace/dtrace# 被嚴格限制成 `root`。
88
108
89
DTrace falls under the Common Development and Distribution License (CDDL) license. To view this license on FreeBSD, see [.filename]#/usr/src/cddl/contrib/opensolaris/OPENSOLARIS.LICENSE# or view it online at http://opensource.org/licenses/CDDL-1.0[http://opensource.org/licenses/CDDL-1.0]. While a FreeBSD kernel with DTrace support is BSD licensed, the CDDL is used when the modules are distributed in binary form or the binaries are loaded.
109
DTrace 使用 Common Development and Distribution License (`CDDL`) 授權。
110
要在 FreeBSD 上查看此授權, 請參閱 [.filename]#/usr/src/cddl/contrib/opensolaris/OPENSOLARIS.LICENSE# 或者在 http://opensource.org/licenses/CDDL-1.0[http://opensource.org/licenses/CDDL-1.0] 線上查看。
111
雖然具有 DTrace 支援的 FreeBSD 核心使用 `BSD` 授權,但當模組使用二進制形式或者二進制文件發布時,將使用 `CDDL` 授權。
90
112
91
[[dtrace-enable]]
113
[[dtrace-enable]]
92
== 開啟 DTrace 支援
114
== 開啟 DTrace 支援
93
115
94
In FreeBSD 9.2 and 10.0, DTrace support is built into the [.filename]#GENERIC# kernel. Users of earlier versions of FreeBSD or who prefer to statically compile in DTrace support should add the following lines to a custom kernel configuration file and recompile the kernel using the instructions in crossref:kernelconfig[kernelconfig,設定 FreeBSD 核心]:
116
在 FreeBSD 9.2 和 10.0 中,DTrace 內建於 [.filename]#GENERIC#  核心裡。
117
FreeBSD 早期版本的使用者或喜歡在 DTrace 支援下靜態編譯的使用者應加入下列幾行到客製化核心配置文件,並根據 crossref:kernelconfig[kernelconfig,Configuring the FreeBSD Kernel] 中的說明重新編譯核心:
95
118
96
[.programlisting]
119
[.programlisting]
97
....
120
....
98
options         KDTRACE_HOOKS
121
options         KDTRACE_HOOKS
Lines 100-153 Link Here
100
makeoptions	DEBUG=-g
123
makeoptions	DEBUG=-g
101
makeoptions	WITH_CTF=1
124
makeoptions	WITH_CTF=1
102
....
125
....
103
126
104
Users of the AMD64 architecture should also add this line:
127
AMD64 架構的使用者應加入下列幾行:
105
128
106
[.programlisting]
129
[.programlisting]
107
....
130
....
108
options         KDTRACE_FRAME
131
options         KDTRACE_FRAME
109
....
132
....
110
133
111
This option provides support for FBT. While DTrace will work without this option, there will be limited support for function boundary tracing.
134
此選項提供對 `FBT` 的支援,
135
雖然 DTrace 可以在沒有此選項的情況下運作,但對函式邊界追蹤的支援有限。
112
136
113
Once the FreeBSD system has rebooted into the new kernel, or the DTrace kernel modules have been loaded using `kldload dtraceall`, the system will need support for the Korn shell as the DTrace Toolkit has several utilities written in `ksh`. Make sure that the package:shells/ksh93[] package or port is installed. It is also possible to run these tools under package:shells/pdksh[] or package:shells/mksh[].
137
一旦 FreeBSD 系統使用新的核心重新啟動,或者使用 `kldload dtraceall` 載入 DTrace 核心模組後,系統需要支援 Korn shell,因為 DTrace 工具箱有幾個用 `ksh` 拓寫的工具。
138
確保已經安裝 package:shells/ksh93[] 套件或者 port,
139
也可以在 package:shells/pdksh[] 或者 package:shells/mksh[] 下執行這些工具。
114
140
115
Finally, install the current DTrace Toolkit, a collection of ready-made scripts for collecting system information. There are scripts to check open files, memory, CPU usage, and a lot more. FreeBSD 10 installs a few of these scripts into [.filename]#/usr/shared/dtrace#. On other FreeBSD versions, or to install the full DTrace Toolkit, use the package:sysutils/dtrace-toolkit[] package or port.
141
最後,安裝目前的 DTrace 工具箱,這是一組用於收集系統資訊的現成腳本,
142
有一些腳本可以檢查打開的文件、記憶體、`CPU` 使用情況等等。
143
FreeBSD 10 將其中一些腳本安裝在 [.filename]#/usr/share/dtrace# 中。
144
在其他 FreeBSD 的版本中,要安裝 DTrace 工具箱,請使用 package:sysutils/dtrace-toolkit[] 套件或者 port。
116
145
117
[NOTE]
146
[NOTE]
118
====
147
====
119
The scripts found in [.filename]#/usr/shared/dtrace# have been specifically ported to FreeBSD. Not all of the scripts found in the DTrace Toolkit will work as-is on FreeBSD and some scripts may require some effort in order for them to work on FreeBSD.
148
[.filename]#/usr/share/dtrace# 中的腳本已專門移植到 FreeBSD,
149
並非所有在 DTrace 工具箱中的所有腳本都能在 FreeBSD 上按照原樣運作,一些腳本可能需要一些修改才能在 FreeBSD 上運作。
120
====
150
====
121
151
122
The DTrace Toolkit includes many scripts in the special language of DTrace. This language is called the D language and it is very similar to C++. An in depth discussion of the language is beyond the scope of this document. It is covered extensively in the http://www.dtrace.org/guide[Illumos Dynamic Tracing Guide].
152
DTrace 工具箱包含許多使用 DTrace 特殊語言的腳本,
153
這種語言被稱為 D 語言,它與 C++ 非常類似,
154
對於該語言的深度討論超出了此文件的範圍,
155
他在 http://www.dtrace.org/guide[Illumos Dynamic Tracing Guide] 有廣泛的介紹。
123
156
124
[[dtrace-using]]
157
[[dtrace-using]]
125
== 使用 DTrace
158
== 使用 DTrace
126
159
127
DTrace scripts consist of a list of one or more _probes_, or instrumentation points, where each probe is associated with an action. Whenever the condition for a probe is met, the associated action is executed. For example, an action may occur when a file is opened, a process is started, or a line of code is executed. The action might be to log some information or to modify context variables. The reading and writing of context variables allows probes to share information and to cooperatively analyze the correlation of different events.
160
DTrace 腳本由一個或多個 _probes_ 或檢查點 (instrumentation points) 的列表組成,其中每個 probe 都與一個行為有關,
161
只要能滿足 probe 的條件,就會執行相關的行為,
162
舉例來說,打開文件、啟動一個行程或執行一行程式。
163
該行為可能是紀錄一些資訊,或修改上下文變數 (context variables),
164
上下文變數的讀寫允許 probes 分享資訊和共同分析不同事件的相關性。
128
165
129
To view all probes, the administrator can execute the following command:
166
想要查看所有的 probes,系統管理員可以執行以下指令:
130
167
131
[source,shell]
168
[source,shell]
132
....
169
....
133
# dtrace -l | more
170
# dtrace -l | more
134
....
171
....
135
172
136
Each probe has an `ID`, a `PROVIDER` (dtrace or fbt), a `MODULE`, and a `FUNCTION NAME`. Refer to man:dtrace[1] for more information about this command.
173
每個 probe 都有一個 `ID`、一個 `PROVIDER` (dtrace 或者 fbt)、一個 `MODULE` 和一個 `FUNCTION NAME`。
174
有關此指令的更多資訊,請參閱 man:dtrace[1]。
137
175
138
The examples in this section provide an overview of how to use two of the fully supported scripts from the DTrace Toolkit: the [.filename]#hotkernel# and [.filename]#procsystime# scripts.
176
本節中的例子概述如何使用 DTrace 工具箱中完全支援的兩個腳本: [.filename]#hotkernel# 和 [.filename]#procsystime# 腳本。
139
177
140
The [.filename]#hotkernel# script is designed to identify which function is using the most kernel time. It will produce output similar to the following:
178
[.filename]#hotkernel# 腳本設計成觀察哪個函式使用的核心時間最多,
179
它會產生類似於以下內容的輸出:
141
180
142
[source,shell]
181
[source,shell]
143
....
182
....
144
# cd /usr/shared/dtrace/toolkit
183
# cd /usr/local/share/dtrace-toolkit
145
# ./hotkernel
184
# ./hotkernel
146
Sampling... Hit Ctrl-C to end.
185
Sampling... Hit Ctrl-C to end.
147
....
186
....
148
187
149
As instructed, use the kbd:[Ctrl+C] key combination to stop the process. Upon termination, the script will display a list of kernel functions and timing information, sorting the output in increasing order of time:
188
按照說明,使用 kbd:[Ctrl+C] 組合鍵停止行程,
189
中止後,腳本將顯示一整列的核心函式和時間資訊,按照時間遞增排序:
150
190
151
[source,shell]
191
[source,shell]
152
....
192
....
153
kernel`_thread_lock_flags                                   2   0.0%
193
kernel`_thread_lock_flags                                   2   0.0%
Lines 177-185 Link Here
177
kernel`sched_idletd                                       137   0.3%
217
kernel`sched_idletd                                       137   0.3%
178
0xc10981a5                                              42139  99.3%
218
0xc10981a5                                              42139  99.3%
179
....
219
....
180
220
181
This script will also work with kernel modules. To use this feature, run the script with `-m`:
221
此腳本也是用於核心模組,
222
要使用此功能,請使用 `-m` 執行腳本:
182
223
183
[source,shell]
224
[source,shell]
184
....
225
....
185
# ./hotkernel -m
226
# ./hotkernel -m
Lines 199-207 Link Here
199
kernel                                                    874   0.4%
240
kernel                                                    874   0.4%
200
0xc10981a5                                             213781  99.6%
241
0xc10981a5                                             213781  99.6%
201
....
242
....
202
243
203
The [.filename]#procsystime# script captures and prints the system call time usage for a given process ID (PID) or process name. In the following example, a new instance of [.filename]#/bin/csh# was spawned. Then, [.filename]#procsystime# was executed and remained waiting while a few commands were typed on the other incarnation of `csh`. These are the results of this test:
244
[.filename]#procsystime# 抓取和輸出系統調用時間,給設定行程 ID (PID) 或行程名稱的行程。
245
在以下的例子中,生成了 [.filename]#/bin/csh# 新物件,
246
然後,[.filename]#procsystime# 被執行並一直等待,同時在 `csh` 的另一個化身上輸入一些指令,
247
以下是本次測試的結果:
204
248
205
[source,shell]
249
[source,shell]
206
....
250
....
207
# ./procsystime -n csh
251
# ./procsystime -n csh
Lines 230-234 Link Here
230
      sigsuspend            6985124
274
      sigsuspend            6985124
231
            read         3988049784
275
            read         3988049784
232
....
276
....
233
277
234
As shown, the `read()` system call used the most time in nanoseconds while the `getpid()` system call used the least amount of time.
278
如圖所示,`read()` 系統調用使用的時間最多(以奈秒為單位),而 `getpid()` 系統調用使用的時間最少。

Return to bug 270926