技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 系统运维 --> Erlang虚拟机基础设施dtrace探测点介绍和使用

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

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

    目前Erlang的虚拟机的探测点支持Linux的systemtap和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 events

     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.6.32-131.21.1.tb477.el6.x86_64
$ git clone git://github.com/erlang/otp.git
$ cd otp

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

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

    yum -y install systemtap systemtap-sdt-devel

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

    我简单的演示下:

$ ./otp_build autoconf
$ ./configure --with-dynamic-trace=systemtap
$ make
...
#出错

    由于这个版本的实现存在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("*")\'
process("bin/x86_64-unknown-linux-gnu/beam").mark("aio_pool__add") $arg1:long $arg2:long
process("bin/x86_64-unknown-linux-gnu/beam").mark("aio_pool__get") $arg1:long $arg2:long
process("bin/x86_64-unknown-linux-gnu/beam").mark("bif__entry") $arg1:long $arg2:long
process("bin/x86_64-unknown-linux-gnu/beam").mark("bif__return") $arg1:long $arg2:long
process("bin/x86_64-unknown-linux-gnu/beam").mark("copy__object") $arg1:long $arg2:long
...
#或者
$ bin/erl
Erlang R16B (erts-5.10) 1 [64-bit] [smp:16:16] [async-threads:0] [hipe] [kernel-poll:false] [systemtap]

Eshell V5.10  (abort with ^G)
1> %%在shell的能力里面看到 [systemtap]

    这样就说明DTRACE的功能已经启用,接着演示下如何使用.

     该发行版带了不少的例子程序位于:

$ ls lib/runtime_tools/examples/*.systemtap
lib/runtime_tools/examples/dist.systemtap                lib/runtime_tools/examples/messages.systemtap
lib/runtime_tools/examples/driver1.systemtap             lib/runtime_tools/examples/port1.systemtap
lib/runtime_tools/examples/efile_drv.systemtap           lib/runtime_tools/examples/process-scheduling.systemtap
lib/runtime_tools/examples/function-calls.systemtap      lib/runtime_tools/examples/spawn-exit.systemtap
lib/runtime_tools/examples/garbage-collection.systemtap  lib/runtime_tools/examples/user-probe.systemtap
lib/runtime_tools/examples/memory1.systemtap
#运行我们的脚本,开始体验
$ PATH=bin/x86_64-unknown-linux-gnu/:$PATH sudo stap lib/runtime_tools/examples/spawn-exit.systemtap
pid <0.0.0> mfa otp_ring0:start/2
pid <0.1.0> mfa erlang:apply/2
pid <0.2.0> mfa erlang:apply/2
pid <0.3.0> mfa erlang:apply/2
pid <0.4.0> mfa heart:init/2
pid <0.4.0> reason normal
pid <0.5.0> mfa proc_lib:init_p/5
pid <0.6.0> mfa erlang:apply/2
pid <0.7.0> mfa application_controller:init_starter/4
pid <0.8.0> mfa proc_lib:init_p/5
pid <0.9.0> mfa application_master:start_it/4
pid <0.10.0> mfa proc_lib:init_p/5
pid <0.11.0> mfa proc_lib:init_p/5
pid <0.12.0> mfa proc_lib:init_p/5
pid <0.13.0> mfa erlang:apply/2
pid <0.14.0> mfa erlang:apply/2
pid <0.15.0> mfa proc_lib:init_p/5
pid <0.16.0> mfa proc_lib:init_p/5
pid <0.17.0> mfa proc_lib:init_p/5
pid <0.18.0> mfa erlang:apply/2
pid <0.19.0> mfa proc_lib:init_p/5
pid <0.20.0> mfa erlang:apply/2
pid <0.21.0> mfa proc_lib:init_p/5
pid <0.22.0> mfa user_drv:server/2
pid <0.23.0> mfa group:server/3
pid <0.24.0> mfa group:server/3
pid <0.25.0> mfa erlang:apply/2
pid <0.26.0> mfa proc_lib:init_p/5
pid <0.27.0> mfa proc_lib:init_p/5
pid <0.1.0> reason on_load_done
pid <0.7.0> reason normal
sender <0.7.0> -> pid <0.6.0> reason normal
pid <0.28.0> mfa application_controller:init_starter/4
pid <0.28.0> reason normal
sender <0.28.0> -> pid <0.6.0> reason normal
pid <0.29.0> mfa erlang:apply/2
pid <0.29.0> reason enoent
sender <0.29.0> -> pid <0.17.0> reason enoent
pid <0.30.0> mfa erlang:apply/2
pid <0.30.0> reason enoent
sender <0.30.0> -> pid <0.17.0> reason enoent
pid <0.2.0> reason normal
sender <0.2.0> -> pid <0.0.0> reason normal
pid <0.31.0> mfa erlang:apply/2
pid <0.32.0> mfa erlang:apply/2

    在另外一个shell里面运行我们的erlang虚拟机,模拟我们的应用系统,我们就可以看到上面的输出。

$ bin/erl -smp disable
Erlang R16B (erts-5.10) 1 [64-bit] [async-threads:0] [hipe] [kernel-poll:false] [systemtap]

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

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

     $ pstree -p |grep 7149

     |-sshd(1847)-+-sshd(7081)—sshd(7101)—bash(7102)-+-beam(7149)

    如果使用多处理器版本,那么进程名是beam.smp,需要修改相应的systemtap脚本里面的进程名。

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

    祝玩得开心!

建议继续学习:

  1. Erlang match_spec引擎介绍和应用    (阅读:4445)
  2. whatsapp深度使用Erlang有感    (阅读:4419)
  3. php-erlang    (阅读:4247)
  4. gen_tcp调用进程收到{empty_out_q, Port}消息奇怪行为分析    (阅读:3485)
  5. hibernate使用注意事项    (阅读:3150)
  6. Erlang linkin driver用port_control方式时的一些经验分享    (阅读:2910)
  7. Erlang如何限制节点对集群的访问之net_kernel:allow    (阅读:2834)
  8. ERLANG OTP源码分析 – gen_server    (阅读:2819)
  9. erlang学习手记    (阅读:2654)
  10. gen_tcp容易误用的一点解释    (阅读:2572)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1