Skip to content

Commit

Permalink
Initial calls strategy + cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
methodmissing committed Apr 24, 2009
1 parent caabd86 commit e7bea4f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/instrumentation.rb
Expand Up @@ -23,6 +23,7 @@ module Strategy
autoload :Calltime, 'mri/instrumentation/strategy/calltime' autoload :Calltime, 'mri/instrumentation/strategy/calltime'
autoload :Interval, 'mri/instrumentation/strategy/interval' autoload :Interval, 'mri/instrumentation/strategy/interval'
autoload :Flow, 'mri/instrumentation/strategy/flow' autoload :Flow, 'mri/instrumentation/strategy/flow'
autoload :Calls, 'mri/instrumentation/strategy/calls'


end end


Expand Down
6 changes: 6 additions & 0 deletions lib/mri/instrumentation/probe.rb
Expand Up @@ -31,6 +31,12 @@ def argument_size
def length def length
@length ||= self.name.size @length ||= self.name.size
end end

# Function declaration
#
def function
@function ||= "pid$target*:::#{self.name}"
end


# Entry declaration # Entry declaration
# #
Expand Down
17 changes: 17 additions & 0 deletions lib/mri/instrumentation/strategy/base.rb
Expand Up @@ -39,6 +39,15 @@ def entry( contents = '' )
}\n ] }\n ]
end end


# Probe function definition
#
def function( contents = '' )
%[ #{@probe.function}
{
#{contents}
}\n ]
end

# Any predicate conditions # Any predicate conditions
# #
def predicate def predicate
Expand Down Expand Up @@ -73,6 +82,14 @@ def build
#{report} ] #{report} ]
end end


# The function definitions to build out
#
def functions
fncs = [:entry]
fncs << :return unless void?
fncs
end

# Respect methods on the probe collection first, then cascade down to the # Respect methods on the probe collection first, then cascade down to the
# current probe definition if any. # current probe definition if any.
# #
Expand Down
5 changes: 3 additions & 2 deletions lib/mri/instrumentation/strategy/builder.rb
Expand Up @@ -37,8 +37,9 @@ def build
def build_each( buf = '' ) def build_each( buf = '' )
@probes.each do |probe| @probes.each do |probe|
strategy_instance = strategy.new( probe, @probes ) strategy_instance = strategy.new( probe, @probes )
@buffer << strategy_instance.entry strategy_instance.functions.each do |func|
@buffer << strategy_instance.return unless strategy_instance.void? @buffer << strategy_instance.send(func)
end
end end
end end


Expand Down
27 changes: 27 additions & 0 deletions lib/mri/instrumentation/strategy/calls.rb
@@ -0,0 +1,27 @@
module Mri
module Instrumentation
module Strategy
class Calls < Base

def setup
super %[ printf("Tracing... Hit Ctrl-C to end.\\n");\n ]
end

def function
super %[ #{assign_arguments}
@calls[#{arguments_list( arguments_size )}] = count(); ]
end

def functions
[:function]
end

def report
super %[ printf(" #{header_format}\\n", #{report_header}, "CALLS");
printa(" #{report_format}\\n", @calls); ]
end

end
end
end
end
8 changes: 4 additions & 4 deletions lib/mri/instrumentation/strategy/calltime.rb
Expand Up @@ -3,11 +3,11 @@ module Instrumentation
module Strategy module Strategy
class Calltime < Base class Calltime < Base


def setup( contents = '' ) def setup
super %[ printf("Tracing... Hit Ctrl-C to end.\\n");\n ] super %[ printf("Tracing... Hit Ctrl-C to end.\\n");\n ]
end end


def entry( contents = '' ) def entry
super %[ self->depth++; super %[ self->depth++;
self->exclude[self->depth] = 0; self->exclude[self->depth] = 0;
#{assign_arguments} #{assign_arguments}
Expand All @@ -20,7 +20,7 @@ def predicate
"/self->#{name}[self->depth]/" "/self->#{name}[self->depth]/"
end end


def return( contents = '' ) def return
super %[ this->elapsed_incl = timestamp - self->#{name}[self->depth]; super %[ this->elapsed_incl = timestamp - self->#{name}[self->depth];
this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth]; this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];
self->#{name}[self->depth] = 0; self->#{name}[self->depth] = 0;
Expand All @@ -33,7 +33,7 @@ def return( contents = '' )
self->exclude[self->depth] += this->elapsed_incl; ] self->exclude[self->depth] += this->elapsed_incl; ]
end end


def report( contents = '' ) def report
super %[ printf("\\nCount,\\n"); super %[ printf("\\nCount,\\n");
printf(" #{header_format}\\n", #{report_header}, "COUNT"); printf(" #{header_format}\\n", #{report_header}, "COUNT");
printa(" #{report_format}\\n", @num); printa(" #{report_format}\\n", @num);
Expand Down
4 changes: 4 additions & 0 deletions test/instrumentation/probe_test.rb
Expand Up @@ -35,6 +35,10 @@ def setup
assert_equal 'pid$target::ruby_xrealloc:return', @probe.function_return assert_equal 'pid$target::ruby_xrealloc:return', @probe.function_return
end end


test "should be able to yield a function name" do
assert_equal 'pid$target*:::ruby_xrealloc', @probe.function
end

test "should be able to yield an arguments list" do test "should be able to yield an arguments list" do
assert_equal 'this->type, this->arg0, this->arg1', @probe.arguments_list assert_equal 'this->type, this->arg0, this->arg1', @probe.arguments_list
assert_equal "this->type, this->arg0, this->arg1, \"\", \"\"", @probe.arguments_list( 5 ) assert_equal "this->type, this->arg0, this->arg1, \"\", \"\"", @probe.arguments_list( 5 )
Expand Down
4 changes: 4 additions & 0 deletions test/instrumentation/strategy/base_test.rb
Expand Up @@ -11,6 +11,10 @@ def setup
assert_equal 'pid$target::ruby_xrealloc:return', @strategy.function_return assert_equal 'pid$target::ruby_xrealloc:return', @strategy.function_return
end end


test "should be able to yield function definitions to build" do
assert_equal [:entry, :return], @strategy.functions
end

test "should be able to yield a header" do test "should be able to yield a header" do
assert_match /pragma/, @strategy.header assert_match /pragma/, @strategy.header
end end
Expand Down
28 changes: 28 additions & 0 deletions test/instrumentation/strategy/calls_test.rb
@@ -0,0 +1,28 @@
require File.join( File.dirname( __FILE__ ), '..', '..', 'helper' )

class CallsStrategyTest < Test::Unit::TestCase

def setup
@probe = Mri::Instrumentation::Test.probe
@probes = Mri::Instrumentation.probes( Mri::Instrumentation::Test::PROBES )
@probe_collection = Mri::Instrumentation::ProbeCollection.new( @probes )
@strategy = Mri::Instrumentation::Strategy::Calls.new( @probe, @probe_collection )
end

test "should be able to yield a header" do
assert_equal " #!/usr/sbin/dtrace -Zs \n #pragma D option quiet\n\n #pragma D option dynvarsize=64m\n\n \n ", @strategy.header
end

test "should be able to yield a setup function" do
assert_equal " dtrace:::BEGIN\n { \n printf(\"Tracing... Hit Ctrl-C to end.\\n\");\n \n }\n ", @strategy.setup
end

test "should be able to yield a function definition" do
assert_equal " pid$target*:::ruby_xrealloc\n {\n this->type = probefunc;\nthis->arg0 = arg0;\nthis->arg1 = stringof( arg1 );\n @calls[this->type, this->arg0, this->arg1] = count(); \n }\n ", @strategy.function
end

test "should be able to yield a report" do
assert_equal " dtrace:::END\n {\n printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"CALLS\");\n \t printa(\" %-24s %-24s %@20d\\n\", @calls); \n }\n ", @strategy.report
end

end
7 changes: 1 addition & 6 deletions test/instrumentation/strategy/calltime_test.rb
Expand Up @@ -10,27 +10,22 @@ def setup
end end


test "should be able to yield a header" do test "should be able to yield a header" do
assert_match /pragma/, @strategy.header
assert_equal " #!/usr/sbin/dtrace -Zs \n #pragma D option quiet\n\n #pragma D option dynvarsize=64m\n\n \n ", @strategy.header assert_equal " #!/usr/sbin/dtrace -Zs \n #pragma D option quiet\n\n #pragma D option dynvarsize=64m\n\n \n ", @strategy.header
end end


test "should be able to yield a setup function" do test "should be able to yield a setup function" do
assert_match /BEGIN/, @strategy.setup
assert_equal " dtrace:::BEGIN\n { \n printf(\"Tracing... Hit Ctrl-C to end.\\n\");\n \n }\n ", @strategy.setup assert_equal " dtrace:::BEGIN\n { \n printf(\"Tracing... Hit Ctrl-C to end.\\n\");\n \n }\n ", @strategy.setup
end end


test "should be able to yield a function entry definition" do test "should be able to yield a function entry definition" do
assert_match /ruby_xrealloc:entry/, @strategy.entry
assert_equal " pid$target::ruby_xrealloc:entry\n {\n self->depth++;\n \t self->exclude[self->depth] = 0;\n \t this->type = probefunc;\nthis->arg0 = arg0;\nthis->arg1 = stringof( arg1 );\n \t @num[this->type, this->arg0, this->arg1] = count();\n \t self->ruby_xrealloc[self->depth] = timestamp; \n }\n ", @strategy.entry assert_equal " pid$target::ruby_xrealloc:entry\n {\n self->depth++;\n \t self->exclude[self->depth] = 0;\n \t this->type = probefunc;\nthis->arg0 = arg0;\nthis->arg1 = stringof( arg1 );\n \t @num[this->type, this->arg0, this->arg1] = count();\n \t self->ruby_xrealloc[self->depth] = timestamp; \n }\n ", @strategy.entry
end end


test "should be able to yield a function return definition" do test "should be able to yield a function return definition" do
assert_match /ruby_xrealloc:return/, @strategy.return
assert_equal " pid$target::ruby_xrealloc:return\n /self->ruby_xrealloc[self->depth]/\n {\n \t this->elapsed_incl = timestamp - self->ruby_xrealloc[self->depth];\n this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];\n \t self->ruby_xrealloc[self->depth] = 0;\n \t self->exclude[self->depth] = 0;\n \t this->type = probefunc;\nthis->arg0 = arg0;\nthis->arg1 = stringof( arg1 );\n \t @types_incl[this->type, this->arg0, this->arg1] = sum(this->elapsed_incl);\n \t @types_excl[this->type, this->arg0, this->arg1] = sum(this->elapsed_excl);\n\n \t self->depth--;\n \t self->exclude[self->depth] += this->elapsed_incl; \n }\n ", @strategy.return assert_equal " pid$target::ruby_xrealloc:return\n /self->ruby_xrealloc[self->depth]/\n {\n \t this->elapsed_incl = timestamp - self->ruby_xrealloc[self->depth];\n this->elapsed_excl = this->elapsed_incl - self->exclude[self->depth];\n \t self->ruby_xrealloc[self->depth] = 0;\n \t self->exclude[self->depth] = 0;\n \t this->type = probefunc;\nthis->arg0 = arg0;\nthis->arg1 = stringof( arg1 );\n \t @types_incl[this->type, this->arg0, this->arg1] = sum(this->elapsed_incl);\n \t @types_excl[this->type, this->arg0, this->arg1] = sum(this->elapsed_excl);\n\n \t self->depth--;\n \t self->exclude[self->depth] += this->elapsed_incl; \n }\n ", @strategy.return
end end


test "should be able to yield a reporting function" do test "should be able to yield a reporting function" do
assert_match /END/, @strategy.report
assert_equal " dtrace:::END\n {\n printf(\"\\nCount,\\n\");\n printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"COUNT\");\n \t printa(\" %-24s %-24s %@20d\\n\", @num);\n normalize(@types_excl, 1000);\n \t printf(\"\\nExclusive function elapsed times (us),\\n\");\n \t printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"TOTAL\");\n \t printa(\" %-24s %-24s %@20d\\n\", @types_excl);\n\n \t normalize(@types_incl, 1000);\n \t printf(\"\\nInclusive function elapsed times (us),\\n\");\n \t printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"TOTAL\");\n \t printa(\" %-24s %-24s %@20d\\n\", @types_incl); \n }\n ", @strategy.report assert_equal " dtrace:::END\n {\n printf(\"\\nCount,\\n\");\n printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"COUNT\");\n \t printa(\" %-24s %-24s %@20d\\n\", @num);\n normalize(@types_excl, 1000);\n \t printf(\"\\nExclusive function elapsed times (us),\\n\");\n \t printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"TOTAL\");\n \t printa(\" %-24s %-24s %@20d\\n\", @types_excl);\n\n \t normalize(@types_incl, 1000);\n \t printf(\"\\nInclusive function elapsed times (us),\\n\");\n \t printf(\" %-24s %-24s %20s\\n\", \"Probe\", \"arg0\", \"TOTAL\");\n \t printa(\" %-24s %-24s %@20d\\n\", @types_incl); \n }\n ", @strategy.report
end end


Expand Down

0 comments on commit e7bea4f

Please sign in to comment.