Ruby-prof doesn't work with ruby/tk?

I’ve tried on several platforms and get the follow error with any tk
code.

[Forrest@Mori ~/<1>masc_log_viewer/lib]$ ruby-prof tkcombobox.rb
/usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128:in
`stop’: Unknown singleton class: 2145471700 (RuntimeError)
from
/usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128

it’ll fail on a file that only has require ‘tk’ in it.

Is this a known problem? Any alternatives for profiling ruby/tk apps
that are quicker than the built in profiler?

Thanks

Forrest

From: “fkc_email-news @ yahoo dot com” [email protected]
Subject: ruby-prof doesn’t work with ruby/tk?
Date: Thu, 21 Dec 2006 10:15:09 +0900
Message-ID: [email protected]

I’ve tried on several platforms and get the follow error with any tk
code.

[Forrest@Mori ~/<1>masc_log_viewer/lib]$ ruby-prof tkcombobox.rb
/usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128:in
`stop’: Unknown singleton class: 2145471700 (RuntimeError)
from
/usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128
(snip)
Is this a known problem? Any alternatives for profiling ruby/tk apps
that are quicker than the built in profiler?

Please try the following patch.
And run “ruby-prof -s tkcombobox.rb”.
^^

diff -urN ruby-prof-0.4.1.orig/bin/ruby-prof
ruby-prof-0.4.1.modify/bin/ruby-prof
— ruby-prof-0.4.1.orig/bin/ruby-prof 2006-06-23 06:36:49.000000000
+0900
+++ ruby-prof-0.4.1.modify/bin/ruby-prof 2006-12-21 16:22:06.000000000
+0900
@@ -19,6 +19,7 @@

wall - Use wall time.

cpu - Use the CPU clock

counter

(only supported on

Pentium and PowerPCs).
+# -s, --set-program-name Set <script.rb> to $0.

-h, --help Show help message

–version Show version

@@ -90,6 +91,11 @@
end
end

  • opts.on(‘-s’, ‘–set-program-name’, ‘Set <script.rb> to $0.’) do
    |mode|
  •   options.set_progname = mode
    
  • end
  • opts.on_tail(“-h”, “–help”, “Show help message”) do
    puts opts
    exit
    @@ -118,6 +124,56 @@
    exit(-1)
    end

+# replace $0 and $PROGRAM_NAME
+if options.set_progname

  • progname_obj = Object.new
  • progname_obj.instance_variable_set(:@progname, $0)
  • class << progname_obj
  • def set_progname(name)
  •  @progname = name.to_s
    
  • end
  • def to_str
  •  @progname
    
  • end
  • alias to_s to_str
  • alias inspect to_str
  • def ===(other)
  •  other === self.to_str
    
  • end
  • def ==(other)
  •  other == self.to_str
    
  • end
  • def equal?(other)
  •  other.equal?(self.to_str)
    
  • end
  • def eql?(other)
  •  other.eql?(self.to_str)
    
  • end
  • def method_missing(id, *args)
  •  self.to_str.__send__(id, *args)
    
  • end
  • end
  • $program_name = progname_obj
  • alias $REAL_PROGRAM_NAME $0
  • alias $PROGRAM_NAME $program_name
  • alias $0 $program_name
  • trace_var(:$program_name){|val|
  • unless progname_obj.object_id == val.object_id
  •  progname_obj.__set_progname__(val)
    
  •  $program_name = progname_obj
    
  • end
  • }
    +end

Install at_exit handler. It is important that we do this

before loading the scripts so our at_exit handler run

@@ -146,6 +202,7 @@

Get the script we will execute

script = ARGV.shift
+$0 = script if options.set_progname

Start profiling

RubyProf.start
diff -urN ruby-prof-0.4.1.orig/ext/ruby_prof.c
ruby-prof-0.4.1.modify/ext/ruby_prof.c
— ruby-prof-0.4.1.orig/ext/ruby_prof.c 2006-06-23 06:36:49.000000000
+0900
+++ ruby-prof-0.4.1.modify/ext/ruby_prof.c 2006-12-21 16:20:58.000000000
+0900
@@ -304,7 +304,7 @@
result = rb_str_new2(“<Unknown:”);
rb_str_append(result, rb_inspect(klass));
rb_str_cat2(result, “>#”);

  •    rb_raise(rb_eRuntimeError, "Unknown singleton class: %i", 
    

result);

  •    rb_warn("Unknown singleton class: %i", result);
    

    }

    return result;

That worked for the most part. I had a failure with one chunk on the
patch, but hand added that, and I had to add code to change foo.rb to
./foo.rb for proper $0 substitution on my system (cygwin ruby).

Thanks for the quick and useful response. Here and all the other Tk
questions you answer so quickly. Ever consider getting your Tk book
translated?

Forrest