http://googya.github.io/blog/categories/dtrace/

最近看了点关于Dtrace的东西,它是个通用型的工具,但我主要集中于分析ruby程序的执行上面。关于操作系统的性能分析,比如cpu、内存、io、文件系统等,使用起来貌似挺复杂,木有细看。

这里简单的输出一条命令:

sudo dtrace -n 'ruby$target:::object-create {@objects[copyinstr(arg0)]=count();}' -c 'ruby -e "puts :hello"'

输出的结果是:

    dtrace: description 'ruby$target:::object-create ' matched 1 probe
-e:1: unterminated string meets end of file
dtrace: pid 15203 has exited #<Class:0x007fabf38e0700> 1
ARGF.class 1
IOError 1
Mutex 1
NoMemoryError 1
SyntaxError 1
SystemStackError 1
ThreadGroup 1
Time 1
LoadError 2
Object 2
Gem::Specification 8
Gem::Version 10
Hash 11
Gem::Requirement 23
Array 96
String 260

ruby2.0已经有支持probe了,所以可以使用dtrace

2.0之前的如果要使用dtrace的话,要使用 ruby-dtrace这个gem

另外在学习的时候,写了dtrace的脚本,其实就是D语言了, rb_flowinfo.d,查看ruby方法的调用过程:

    #!/usr/sbin/dtrace -Zs
#pragma D option quiet
#pragma D option switchrate=10 self int depth; dtrace:::BEGIN
{
printf("%s %6s %10s %16s:%-4s %-8s -- %s\n", "C", "PID", "DELTA(us)", "FILE", "LINE", "TYPE", "NAME");
} ruby*:::method-entry,
ruby*:::method-return
/self->last == 0/
{
self->last = timestamp;
} ruby*:::method-entry
/copyinstr(arg0) == "Object"/
{
this->delta = (timestamp - self->last) / 1000;
this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
printf("%d %6d %10d %16s:%-4d %-8s %*s-> %s\n", cpu, pid, this->delta,
basename(copyinstr(arg2)), arg3, "method", self->depth * 2, "", this->name); self->depth++;
self->last = timestamp;
} ruby*:::method-return
/copyinstr(arg0) == "Object"/
{
this->delta = (timestamp - self->last) / 1000;
self->depth -= self->depth > 0 ? 1 : 0;
this->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
printf("%d %6d %10d %16s:%-4d %-8s %*s<- %s\n", cpu, pid, this->delta,
basename(copyinstr(arg2)), arg3, "method", self->depth * 2, "", this->name);
self->last = timestamp;
}

用于测试的ruby脚本, trace_method_call.rb

    dtrace_ruby $ cat trace_method_call.rb
#!/Users/wenleslie/.rvm/rubies/ruby-2.0.0-p0/bin/ruby
def func_c
puts "Function C"
sleep 1
end def func_b
puts "Function B"
sleep 6
func_c
end def func_a
puts "Function A"
func_b
end func_a

执行:

    sudo ./rb_flowinfo.d -c ./trace_method_call.rb

结果如下:

C    PID  DELTA(us)             FILE:LINE TYPE     -- NAME
Function A
Function B
4 15702 35986 trace_method_call.rb:13 method -> Object::func_a
4 15702 55 trace_method_call.rb:7 method -> Object::func_b
Function C0 15702 6001065 trace_method_call.rb:2 method -> Object::func_c 6 15702 1000922 trace_method_call.rb:5 method <- Object::func_c
6 15702 27 trace_method_call.rb:11 method <- Object::func_b
6 15702 19 trace_method_call.rb:16 method <- Object::func_a
^C

我的evernote上关于dtrace笔记有稍微详细一点的内容,有兴趣的话,可以看看

How to Use Dtrace Tracing Ruby Executing的更多相关文章

  1. dtrace 语法

    Usage: dtrace [-aACeFHlqSvVwZ] [-arch i386|x86_64] [-b bufsz] [-c cmd] [-D name[=def]]      [-I path ...

  2. Linux Perf Probes for Oracle Tracing

    Luca Canali on 21 Jan 2016 Topic: this post is about Linux perf and uprobes for tracing and profilin ...

  3. Adding DTrace Probes to PHP Extensions

      By cj on Dec 06, 2012 The powerful DTrace tracing facility has some PHP-specific probes that can b ...

  4. [转]Top 10 DTrace scripts for Mac OS X

    org link: http://dtrace.org/blogs/brendan/2011/10/10/top-10-dtrace-scripts-for-mac-os-x/ Top 10 DTra ...

  5. End-to-End Tracing of Ajax/Java Applications Using DTrace

    End-to-End Tracing of Ajax/Java Applications Using DTrace         By Amit Hurvitz, July 2007     Aja ...

  6. Tracing mysqld Using DTrace

    http://dev.mysql.com/doc/refman/5.6/en/dba-dtrace-server.html MySQL 5.6 Reference Manual -> 5 MyS ...

  7. Using Dtrace OEL 6.X and Oracle® Solaris 11.3 DTrace (Dynamic Tracing) Guide

    http://www.hhutzler.de/blog/using-dtrace/ https://docs.oracle.com/cd/E53394_01/html/E53395/gkyaz.htm ...

  8. [转]Breaking Bad With DTrace

    Source:http://initwithfunk.com/blog/2013/05/31/breaking-bad-with-dtrace/ I’ve spent an unwise amount ...

  9. Life of an Oracle I/O: tracing logical and physical I/O with systemtap

    https://db-blog.web.cern.ch/blog/luca-canali/2014-12-life-oracle-io-tracing-logical-and-physical-io- ...

随机推荐

  1. 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)

    老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...

  2. HT1621控制的段式液晶驱动程序

    MCU是STM8S207 /*LED 字模结构*/ typedef struct { char mChar; u8 mModal; }LED_MODAL_DEFINE; typedef struct ...

  3. 大数据学习——scala集合练习

    package com /** * Created by ZX on 2016/4/5. */ object ListTest { def main(args: Array[String]) { // ...

  4. python之路 --- python基础

    一.python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  5. AtCoder Grand Contest 022

    A - Diverse Word Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Gotou ...

  6. Oracle的表空间、数据文件、用户

          每一个Oracle数据库都是由三种类型的文件组成:数据文件(Data File).日志文件(Log File)和控制文件(Control File).数据库的文件为数据库信息提供真正的物理 ...

  7. NuGet安装本地包命令行

    尝试安装本地的NuGet包. 键入 "get-help NuGet" 可查看所有可用的 NuGet 命令. install-package Polly.Net40Async-Sig ...

  8. BZOJ-3040 最短路

    最短路+堆优化. 普通的堆还不行,自己用的是配对堆(貌似斐波那契堆也行?毕竟理论复杂度) 然后发现自己的配对堆比云神的不知快了多少...我照着他的模版打的喂.. 然后发现前T条边不理都能A... 数据 ...

  9. [暑假集训--数位dp]cf55D Beautiful numbers

    Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...

  10. 洛谷P1101 单词方阵

    题目描述 给一nXn的字母方阵,内可能蕴含多个“yizhong”单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着8个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间[color=red ...