注册

Erlang虚拟机基础设施dtrace探测点介绍和使用

最新的Erlang虚拟机(R15B01)很大的一个改进就是加入了对dtrace探测点的支持了, 具体参见这里, 主要目标是方便在生产实践中定位复杂的性能问题。

目前Erlang的虚拟机的探测点支持Linux的**tap和freebsd的dtrace,我们刚好能够享受的到。

作者Scott Lystig Fritchie在去年的euc中做了个很有意思的报告,参见这里,该PPT很详细的介绍了利用dtrace的探测点可以观察到erlang的行为如下:

Processes: spawn, exit, hibernate, scheduled, …
Messages: send, queued, received, exit signals
Memory: GC minor & major, proc heap grow & shrink
Data copy: within heap, across heaps
Function calls: function & BIF & NIF, entry & return
Network distribution: monitor, port busy, output **s
Ports: open, command, control, busy/not busy
Drivers: callback API 100% instrumented
efile_drv.c fifile I/O driver: 100% instrumented

这些探测点基本上属于IO,进程调度,消息发送,driver等虚拟机最底层的和**作系统耦合的部分,对性能的影响巨大。

目前Erlang自己的基础设施并没有涵盖到这部分内容,如dbg,trace机制都无法了解到这些数据,导致在大型的集群系统里面一旦发现性能问题无法很好的展开调查,所以这些探测点刚好填补了空白。

目前这些dtrace probe点是用static marker实现的,细节可以参看这里 和 这里,好处是不用这些特性的时候,不会对性能有任何的影响,只需要为使用付代价。目前支持这种机制的语言和系统有java,python,mysql,pgsql等,可见威力强劲。

好拉,废话少说,我们来体验下。

下载最新的otp, 目前是R15B01, 简单的运行:
$ uname -r
2
2.6.32-131.21.1.tb477.el6.x86_64
3
$ git clone git://github.com/erlang/otp.git
4
$ cd otp

源码目录下有个README.**tap.md,会简单的教你如何编译和使用。

由于我们用的是RHEL 6U2, 在编译之前你要确保你的**tap和**tap-sdt-devel已经安装, 直接运行

yum -y install **tap **tap-sdt-devel

就搞定了,实在搞不懂的可以参考作者的这篇教程

我简单的演示下:

$ ./otp_build autoconf
2
$ ./configure --with-dynamic-trace=**tap
3
$ make
4
...
#出错

由于这个版本的实现存在bug, 在实现DTRACE_GLOBAL_CALL功能的时候,没搞好细节,编译的时候会出错:
beam_emu.c:L1562, L1578, L1592,简单的把这几行注释掉,继续编译就好了,就是global函数的trace功能不能使用而已,无伤大雅。

好了,如果不出意外,带dtrace功能的beam就生成了,我们如何确认呢?

$ stap -L 'process("bin/x86_64-unknown-linux-gnu/beam").mark("*")'
02
process("bin/x86_64-unknown-linux-gnu/beam").mark("aio_pool__add") $arg1:long $arg2:long
03
process("bin/x86_64-unknown-linux-gnu/beam").mark("aio_pool__get") $arg1:long $arg2:long
04
process("bin/x86_64-unknown-linux-gnu/beam").mark("bif__entry") $arg1:long $arg2:long
05
process("bin/x86_64-unknown-linux-gnu/beam").mark("bif__return") $arg1:long $arg2:long
06
process("bin/x86_64-unknown-linux-gnu/beam").mark("copy__object") $arg1:long $arg2:long
07
...
08
#或者
09
$ bin/erl
10
Erlang R16B (erts-5.10) 1
11

12
Eshell V5.10 (abort with ^G)
13
1> %%在shell的能力里面看到
这样就说明DTRACE的功能已经启用,接着演示下如何使用.
该发行版带了不少的例子程序位于:
$ ls lib/runtime_tools/examples/*.**tap
02
lib/runtime_tools/examples/dist.**tap lib/runtime_tools/examples/messages.**tap
03
lib/runtime_tools/examples/driver1.**tap lib/runtime_tools/examples/port1.**tap
04
lib/runtime_tools/examples/efile_drv.**tap lib/runtime_tools/examples/process-scheduling.**tap
05
lib/runtime_tools/examples/function-calls.**tap lib/runtime_tools/examples/spawn-exit.**tap
06
lib/runtime_tools/examples/garbage-collection.**tap lib/runtime_tools/examples/user-probe.**tap
07
lib/runtime_tools/examples/memory1.**tap
08
#运行我们的脚本,开始体验
09
$ PATH=bin/x86_64-unknown-linux-gnu/:$PATH sudo stap lib/runtime_tools/examples/spawn-exit.**tap
10
pid <0.0.0> mfa otp_ring0:start/2
11
pid <0.1.0> mfa erlang:apply/2
12
pid <0.2.0> mfa erlang:apply/2
13
pid <0.3.0> mfa erlang:apply/2
14
pid <0.4.0> mfa heart:init/2
15
pid <0.4.0> reason normal
16
pid <0.5.0> mfa proc_lib:init_p/5
17
pid <0.6.0> mfa erlang:apply/2
18
pid <0.7.0> mfa application_controller:init_starter/4
19
pid <0.8.0> mfa proc_lib:init_p/5
20
pid <0.9.0> mfa application_**:start_it/4
21
pid <0.10.0> mfa proc_lib:init_p/5
22
pid <0.11.0> mfa proc_lib:init_p/5
23
pid <0.12.0> mfa proc_lib:init_p/5
24
pid <0.13.0> mfa erlang:apply/2
25
pid <0.14.0> mfa erlang:apply/2
26
pid <0.15.0> mfa proc_lib:init_p/5
27
pid <0.16.0> mfa proc_lib:init_p/5
28
pid <0.17.0> mfa proc_lib:init_p/5
29
pid <0.18.0> mfa erlang:apply/2
30
pid <0.19.0> mfa proc_lib:init_p/5
31
pid <0.20.0> mfa erlang:apply/2
32
pid <0.21.0> mfa proc_lib:init_p/5
33
pid <0.22.0> mfa user_drv:**/2
34
pid <0.23.0> mfa group:**/3
35
pid <0.24.0> mfa group:**/3
36
pid <0.25.0> mfa erlang:apply/2
37
pid <0.26.0> mfa proc_lib:init_p/5
38
pid <0.27.0> mfa proc_lib:init_p/5
39
pid <0.1.0> reason on_load_done
40
pid <0.7.0> reason normal
41
sender <0.7.0> -> pid <0.6.0> reason normal
42
pid <0.28.0> mfa application_controller:init_starter/4
43
pid <0.28.0> reason normal
44
sender <0.28.0> -> pid <0.6.0> reason normal
45
pid <0.29.0> mfa erlang:apply/2
46
pid <0.29.0> reason enoent
47
sender <0.29.0> -> pid <0.17.0> reason enoent
48
pid <0.30.0> mfa erlang:apply/2
49
pid <0.30.0> reason enoent
50
sender <0.30.0> -> pid <0.17.0> reason enoent
51
pid <0.2.0> reason normal
52
sender <0.2.0> -> pid <0.0.0> reason normal
53
pid <0.31.0> mfa erlang:apply/2
54
pid <0.32.0> mfa erlang:apply/2
在另外一个shell里面运行我们的erlang虚拟机,模拟我们的应用系统,我们就可以看到上面的输出。
$ bin/erl -smp disable
2
Erlang R16B (erts-5.10) 1
3

4
Eshell V5.10 (abort with ^G)
5
1> os:getpid().
6
"7149"


这里要注意的是我们使用的-smp disable参数,使用的是beam的plain版本,它的进程名是beam

$ pstree -p |grep 7149
2
|-sshd(1847)-+-sshd(7081)—sshd(7101)—bash(7102)-+-beam(7149)
如果使用多处理器版本,那么进程名是beam.smp,需要修改相应的**tap脚本里面的进程名。

小结:**tap无敌,erlang与时俱进!

祝玩得开心!

已邀请:

要回复问题请先登录注册