Skip to content

Instantly share code, notes, and snippets.

@aghyad
Created December 3, 2013 22:38
Show Gist options
  • Save aghyad/7778876 to your computer and use it in GitHub Desktop.
Save aghyad/7778876 to your computer and use it in GitHub Desktop.
Experimenting with Ruby 2.0 and DTrace scripts
Experimenting with Ruby 2.0 and DTrace scripts
I was very excited when I heard about the new Ruby 2.0 features. I am not going to delve into Ruby 2.0 features here, but I will talk about one feature I consider the best of Ruby 2: integration of DTrace.
I've never used DTrace before, much less I thought it can be used efficiently with Ruby the same way debugger is being used with ruby on rails.
DTrace has been for years the most famous tracing framework and now it's time to start using it with Ruby 2. So, after readings about DTrace, I tried to put it in action. I will be using Mac OS X 10.7.
Installing Ruby 2.0
If you did not yet, here's how to install Ruby:
- Install OpenSSL: Ruby 2.0 is not compatible with the OpenSSL of OSX, so it's recommended you install openssl. If you have HomeBrew, it'll make your life better:
brew install openssl
- Clone ruby from github:
git clone [email protected]:ruby/ruby.git
or you can install ruby from HomeBrew.
- configure, make, then install it:
./configure
make
make install
This is what you at least need to install ruby 2.0 on you machine. You may want to go with Homebrew if you have it.
Any way, after making sure you installed Ruby 2.0 (how? ---> ruby --version), let's play around with DTrace.
DTrace with ruby scripts
DTrace helps you monitor and examine the behavior of your programs, the resources programs use and the operating system. To do that you use the D scripting language. DTrace is now supported by Oracle, and you can find an extensive tutorial on how to write all kinds of simple to advanced scripts on it here:
http://docs.oracle.com/cd/E18752_01/html/819-5488/gbwaz.html
The tutorial is a great documentation of the structure and architecture of DTrace and it helps you understand the naming and behavior of this framework. The documentation also provides a very good explanation to the scripting language.
D scripts are saved in files with extension .d, and are run against a specific program (in our case a ruby program), using this command:
sudo dtrace -s [dtrace-script-name.d] -c [/path/to/ruby] [ruby-script-name.rb]
Let's take an example of a simple script
open a ruby file - name it "hello_world.rb", and save it with this simple code:
puts "Hello World ... I am testing DTrace for the first time!"
open a new scripting file and save it "dtrace_testing.d" with any d scripting you have.
Then run:
sudo dtrace -s dtrace_testing.d -c /your/path/to/ruby hello_world.rb
Alternatively, you can save at the beginning of your ruby script the following:
#!/your/path/to/ruby
and save the ruby script, then:
chmod +x ./hello_world.rb
to make it executable without the need to call ruby everytime like this:
sudo dtrace -s dtrace_testing.d -c ./hello_world.rb
You also can add a symlink to this ruby file and use the symlink without the .rb, like this:
ln -s ./hello_world.rb hw
sudo dtrace -s dtrace_testing.d -c ./hw
Also you can minimize the command by doing the same thing for the dtrace script file:
Add this to the top of file dtrace_testing.d:
#!/usr/sbin/dtrace -s
Then chmod it into executable, then add a symlink for it:
chmod +x ./dtrace_testing.d
ln -s ./dtrace_testing.d dtt
So you minimized the DTrace command to execute that d script on the program into:
sudo ./dtt -c ./hw
Notice that you need to call the symlinks with ./ to specify the current directory, since the kernel won't recognize it without it. Also notice that you can not execute any dtrace script without using sudo.
I will be writing soon more tutorials about the d scripting language itself. In the meanwhile feel free to read through the Oracle docs of DTrace, and try writing your own d scripts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment