Module RubyProf
In: lib/ruby-prof.rb
lib/ruby-prof/flat_printer.rb
lib/ruby-prof/graph_html_printer.rb
lib/ruby-prof/graph_printer.rb
lib/ruby-prof/profiletask.rb
ext/ruby_prof.c

Methods

Classes and Modules

Class RubyProf::CallInfo
Class RubyProf::FlatPrinter
Class RubyProf::GraphHtmlPrinter
Class RubyProf::GraphPrinter
Class RubyProf::MethodInfo
Class RubyProf::ProfileTask
Class RubyProf::Result

Constants

VERSION = rb_str_new2(PROF_VERSION)
PROCESS_TIME = INT2NUM(CLOCK_MODE_PROCESS)
WALL_TIME = INT2NUM(CLOCK_MODE_WALL)
CPU_TIME = INT2NUM(CLOCK_MODE_CPU)

Public Class methods

Returns the current clock mode. Valid values include:

 RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock function in the C Runtime library.
 RubyProf::WALL_TIME - Measure wall time using gettimeofday on Linx and GetLocalTime on Windows
 RubyProf::CPU_TIME - Measure time using the CPU clock counter.  This mode is only supported on Pentium or PowerPC platforms.

[Source]

/* call-seq:
   clock_mode -> clock_mode
   
   Returns the current clock mode.  Valid values include:
   *RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock function in the C Runtime library.
   *RubyProf::WALL_TIME - Measure wall time using gettimeofday on Linx and GetLocalTime on Windows
   *RubyProf::CPU_TIME - Measure time using the CPU clock counter.  This mode is only supported on Pentium or PowerPC platforms. */
static VALUE
prof_get_clock_mode(VALUE self)
{
    return INT2NUM(clock_mode);
}

Specifies the method ruby-prof uses to measure time. Valid values include:

 RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock function in the C Runtime library.
 RubyProf::WALL_TIME - Measure wall time using gettimeofday on Linx and GetLocalTime on Windows
 RubyProf::CPU_TIME - Measure time using the CPU clock counter.  This mode is only supported on Pentium or PowerPC platforms.

[Source]

/* call-seq:
   clock_mode=value -> void
   
   Specifies the method ruby-prof uses to measure time.  Valid values include:
   *RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock function in the C Runtime library.
   *RubyProf::WALL_TIME - Measure wall time using gettimeofday on Linx and GetLocalTime on Windows
   *RubyProf::CPU_TIME - Measure time using the CPU clock counter.  This mode is only supported on Pentium or PowerPC platforms. */
static VALUE
prof_set_clock_mode(VALUE self, VALUE val)
{
    int mode = NUM2INT(val);

    if (threads_tbl)
    {
            rb_raise(rb_eRuntimeError, "can't set clock_mode while profiling");
    }

    switch (mode) {
    case CLOCK_MODE_PROCESS:
        get_clock = clock_get_clock;
            clock2sec = clock_clock2sec;
        break;
    case CLOCK_MODE_WALL:
            get_clock = gettimeofday_get_clock;
            clock2sec = gettimeofday_clock2sec;
            break;
#ifdef CLOCK_MODE_CPU
    case CLOCK_MODE_CPU:
            if (cpu_frequency == 0)
                cpu_frequency = get_cpu_frequency();
                get_clock = cpu_get_clock;
                clock2sec = cpu_clock2sec;
            break;
#endif
    default:
            rb_raise(rb_eArgError, "invalid mode: %d", mode);
        break;
    }
    clock_mode = mode;
    return val;
}

Returns the cpu’s frequency. This value is needed when using the cpu RubyProf::clock_mode.

[Source]

/* call-seq:
   cpu_frequency -> int

Returns the cpu's frequency.  This value is needed when using the 
cpu RubyProf::clock_mode. */
static VALUE
prof_get_cpu_frequency(VALUE self)
{
    return rb_float_new(cpu_frequency);
}

Sets the cpu’s frequency. This value is needed when using the cpu RubyProf::clock_mode.

[Source]

/* call-seq:
   cpu_frequency=value -> void

Sets the cpu's frequency.  This value is needed when using the 
cpu RubyProf::clock_mode. */
static VALUE
prof_set_cpu_freqeuncy(VALUE self, VALUE val)
{
    cpu_frequency = NUM2DBL(val);
    return val;
}

See if the user specified the clock mode via the RUBY_PROF_CLOCK_MODE environment variable

[Source]

    # File lib/ruby-prof.rb, line 10
10:   def self.figure_clock_mode
11:     case ENV["RUBY_PROF_CLOCK_MODE"]
12:           when "wall" || "wall_time"
13:                   RubyProf.clock_mode = RubyProf::WALL_TIME
14:           when "cpu" || "cpu_time"
15:           if ENV.key?("RUBY_PROF_CPU_FREQUENCY")
16:                   RubyProf.cpu_frequency = ENV["RUBY_PROF_CPU_FREQUENCY"].to_f
17:           else
18:                   begin
19:                   open("/proc/cpuinfo") do |f|
20:                   f.each_line do |line|
21:                           s = line.slice(/cpu MHz\s*:\s*(.*)/, 1)
22:                           if s
23:                           RubyProf.cpu_frequency = s.to_f * 1000000
24:                           break
25:                           end
26:                   end
27:                   end
28:                   rescue Errno::ENOENT
29:                   end
30:           end
31:           RubyProf.clock_mode = RubyProf::CPU_TIME
32:           else
33:           RubyProf.clock_mode = RubyProf::PROCESS_TIME
34:         end
35:         end

Public Instance methods

Profiles the specified block and returns a RubyProf::Result object.

[Source]

/* call-seq:
   profile {block} -> RubyProf::Result

Profiles the specified block and returns a RubyProf::Result object. */
static VALUE
prof_profile(VALUE self)
{
    if (!rb_block_given_p())
    {
        rb_raise(rb_eArgError, "A block must be provided to the profile method.");
    }

    prof_start(self);
    rb_yield(Qnil);
    return prof_stop(self);
}

Starts recording profile data.

[Source]

/* call-seq:
   start -> void
   
   Starts recording profile data.*/
static VALUE
prof_start(VALUE self)
{
    toplevel_id = rb_intern("toplevel");
    toplevel_key = method_key(Qnil, toplevel_id);

    if (threads_tbl != NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
    }

    /* Setup globals */
    class_tbl = rb_hash_new();
    threads_tbl = threads_table_create();
    
    rb_add_event_hook(prof_event_hook,
        RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
        RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN);

    return Qnil;
}

Stops collecting profile data and returns a RubyProf::Result object.

[Source]

/* call-seq:
   stop -> RubyProf::Result

   Stops collecting profile data and returns a RubyProf::Result object. */
static VALUE
prof_stop(VALUE self)
{
    VALUE result = Qnil;

    if (threads_tbl == NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start is not called yet");
    }

    /* Now unregister from event   */
    rb_remove_event_hook(prof_event_hook);

    /* Create the result */
    result = prof_result_new();

    /* Free threads table */
    free_threads(threads_tbl);
    threads_table_free(threads_tbl);
    threads_tbl = NULL;

    /* Free reference to class_tbl */
    class_tbl = Qnil;
    
    return result;
}

[Validate]