Skip to content

Commit

Permalink
Introduce an Instrumentation::Runner::* namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
methodmissing committed Apr 19, 2009
1 parent 6c5186f commit dfb737b
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 167 deletions.
6 changes: 6 additions & 0 deletions bin/instrument
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'instrumentation'

13 changes: 10 additions & 3 deletions lib/instrumentation.rb
Expand Up @@ -14,7 +14,6 @@ module Instrumentation
autoload :Probe, 'mri/instrumentation/probe'
autoload :Argument, 'mri/instrumentation/argument'
autoload :ProbeCollection, 'mri/instrumentation/probe_collection'
autoload :Runner, 'mri/instrumentation/runner'

module Strategy

Expand All @@ -24,6 +23,12 @@ module Strategy

end

module Runner

autoload :Base, 'mri/instrumentation/runner/base'

end

# Setup all probe definitions from a given path
#
def self.definitions( path = PROBES )
Expand All @@ -34,13 +39,15 @@ def self.definitions( path = PROBES )
# Infer all available probes
#
def self.probes( path = PROBES )
@probes ||= definitions( path ).map{|p| p.probes }.flatten
@probes ||= {}
@probes[path] ||= definitions( path ).map{|p| p.probes }.flatten
end

# Infer all probe groups from a given path
#
def self.groups( path = PROBES )
@groups ||= definitions( path ).map{|p| p.group }
@groups ||= {}
@groups[path] ||= definitions( path ).map{|p| p.group }
end

end
Expand Down
160 changes: 0 additions & 160 deletions lib/mri/instrumentation/runner.rb

This file was deleted.

162 changes: 162 additions & 0 deletions lib/mri/instrumentation/runner/base.rb
@@ -0,0 +1,162 @@
module Mri
module Instrumentation
module Runner
class Base

attr_reader :run_with,
:result

def initialize( &block )
@probes = []
instance_eval( &block ) if block_given?
end

# Instrument with the given probes
#
def probes( *probes )
probes.empty? ? @probes : self.probes=( probes )
end

# Attach to a given PID
#
def pid( pid = nil )
pid ? self.pid=( pid ) : @pid
end

# Run a given command
#
def command( command = nil )
command ? self.command=( command ) : @command
end

# The desired profiling strategy
#
def strategy( strategy = nil )
if strategy
self.strategy=( strategy )
else
@strategy_instance ||= setup_strategy( @strategy || :calltime )
end
end

# Run this definition
#
def run!
generate do
run_command? ? run_command : attach_to_pid
end
end

private

# Probes setter
#
def probes=( probes )
@probes = setup_probes( probes.map{|p| p.to_s } )
end

# Pid setter
#
def pid=( pid )
@run_with = :pid
@pid = pid
end

# Command setter
#
def command=( command )
@run_with = :command
@command = command
end

# Strategy setter
#
def strategy=( strategy )
@strategy = strategy
end

# Generates a dtrace script / stream
#
def generate
Tempfile.open( 'mri_instrumentation' ) do |file|
file << d_stream()
file.flush
cmd = "sudo dtrace -s #{file.path} #{yield}"
@result = %x[#{cmd}]
end
end

# Are we running a command ?
#
def run_command?
@run_with == :command
end

# Attach to the given PID
#
def attach_to_pid
"-p #{@pid.to_s}"
end

# Run the given command
#
def run_command
"-c '#{@command}'"
end

# Convert given probe signatures to probe instances
#
def setup_probes( probes )
probes.map do |probe|
probe_group?( probe ) ? probes_grouped( probe ) : probe_definition( probe )
end.flatten
end

# Yields a probe definition from a probe
#
def probe_definition( probe )
Mri::Instrumentation.probes.detect{|p| p.name == probe }
end

# Yields all probes that's a member of the given group signature
#
def probes_grouped( group )
Mri::Instrumentation.probes.select{|p| p.group == group.to_sym }
end

# Is the given probe signature a probe group signature ?
#
def probe_group?( probe )
Mri::Instrumentation.groups.include?( probe.to_sym )
end

# Returns qualified
#
def setup_strategy( strategy )
Object.module_eval("Mri::Instrumentation::Strategy::#{strategy_const( strategy )}", __FILE__, __LINE__)
end

# Constant representation of a given strategy signature
#
def strategy_const( strategy )
strategy.to_s.gsub( /\/(.?)/ ) { "::#{$1.upcase}" }.gsub( /(?:^|_)(.)/ ) { $1.upcase }
end

# Build the D stream
#
def d_stream
Mri::Instrumentation::Strategy::Builder.new( self.strategy, self.probes ).to_s
end

end
end
end
end

=begin
Mri::Instrumentation::Runner::Base.new do
probes :gc
command 'ruby -v'
strategy :calltime
end
=end
@@ -1,10 +1,10 @@
require File.join( File.dirname( __FILE__ ), '..', 'helper' )
require File.join( File.dirname( __FILE__ ), '..', '..', 'helper' )

class RunnerTest < Test::Unit::TestCase
class BaseRunnerTest < Test::Unit::TestCase

def setup
@probe = Mri::Instrumentation::Test.probe
@runner = Mri::Instrumentation::Runner.new
@runner = Mri::Instrumentation::Runner::Base.new
end

test "should be able to have a process identifier set" do
Expand All @@ -23,7 +23,7 @@ def setup

test "should be able to yield instrumentation probes from a given group signature" do
@runner.probes :gc
assert_equal 3, @runner.probes.size
assert_equal 43, @runner.probes.size
end

test "should be able to yield instrumentation probes from a given group of probe signatures" do
Expand Down

0 comments on commit dfb737b

Please sign in to comment.