Module RubyProf

  1. lib/ruby-prof/abstract_printer.rb
  2. lib/ruby-prof/aggregate_call_info.rb
  3. lib/ruby-prof/call_info.rb
  4. lib/ruby-prof/call_tree_printer.rb
  5. lib/ruby-prof/flat_printer.rb
  6. lib/ruby-prof/graph_html_printer.rb
  7. lib/ruby-prof/graph_printer.rb
  8. lib/ruby-prof/method_info.rb
  9. lib/ruby-prof/task.rb
  10. lib/ruby-prof/test.rb
  11. lib/ruby-prof.rb
  12. ext/ruby_prof.c
  13. show all
   Document-method: measure_gc_time
   call-seq:
     gc_time -> Integer

Returns the time spent doing garbage collections in microseconds.

Methods

public class

  1. figure_measure_mode

Constants

VERSION = rb_str_new2(RUBY_PROF_VERSION)
CLOCKS_PER_SEC = INT2NUM(CLOCKS_PER_SEC)
PROCESS_TIME = INT2NUM(MEASURE_PROCESS_TIME)
WALL_TIME = INT2NUM(MEASURE_WALL_TIME)
CPU_TIME = Qnil
CPU_TIME = INT2NUM(MEASURE_CPU_TIME)
ALLOCATIONS = Qnil
ALLOCATIONS = INT2NUM(MEASURE_ALLOCATIONS)
MEMORY = Qnil
MEMORY = INT2NUM(MEASURE_MEMORY)
GC_RUNS = Qnil
GC_RUNS = INT2NUM(MEASURE_GC_RUNS)
GC_TIME = Qnil
GC_TIME = INT2NUM(MEASURE_GC_TIME)

Public class methods


cpu_frequency → int

Returns the cpu’s frequency. This value is needed when RubyProf::measure_mode is set to CPU_TIME.


cpu_frequency = frequency

Sets the cpu’s frequency. This value is needed when RubyProf::measure_mode is set to CPU_TIME.


exclude_threads= → void

Specifies what threads ruby-prof should exclude from profiling

[show source]
/* call-seq:
   exclude_threads= -> void

   Specifies what threads ruby-prof should exclude from profiling */
static VALUE
prof_set_exclude_threads(VALUE self, VALUE threads)
{
    int i;

    if (threads_tbl != NULL)
    {
      rb_raise(rb_eRuntimeError, "can't set exclude_threads while profiling");
    }

    /* Stay simple, first free the old hash table */
    if (exclude_threads_tbl)
    {
      st_free_table(exclude_threads_tbl);
      exclude_threads_tbl = NULL;
    }

    /* Now create a new one if the user passed in any threads */
    if (threads != Qnil)
    {
      Check_Type(threads, T_ARRAY);
      exclude_threads_tbl = st_init_numtable();

      for (i=0; i < RARRAY_LEN(threads); ++i) 
      {
        VALUE thread = rb_ary_entry(threads, i);
        st_insert(exclude_threads_tbl, (st_data_t) rb_obj_id(thread), 0);
      }
    }    
    return threads;
}
figure_measure_mode ()

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

[show source]
    # File lib/ruby-prof.rb, line 16
16:   def self.figure_measure_mode
17:     case ENV["RUBY_PROF_MEASURE_MODE"]
18:     when "wall" || "wall_time"
19:       RubyProf.measure_mode = RubyProf::WALL_TIME
20:     when "cpu" || "cpu_time"
21:       if ENV.key?("RUBY_PROF_CPU_FREQUENCY")
22:         RubyProf.cpu_frequency = ENV["RUBY_PROF_CPU_FREQUENCY"].to_f
23:       else
24:         begin
25:           open("/proc/cpuinfo") do |f|
26:             f.each_line do |line|
27:               s = line.slice(/cpu MHz\s*:\s*(.*)/, 1)
28:               if s
29:                 RubyProf.cpu_frequency = s.to_f * 1000000
30:                 break
31:               end
32:             end
33:           end
34:         rescue Errno::ENOENT
35:         end
36:       end
37:       RubyProf.measure_mode = RubyProf::CPU_TIME
38:     when "allocations"
39:       RubyProf.measure_mode = RubyProf::ALLOCATIONS
40:     when "memory"
41:       RubyProf.measure_mode = RubyProf::MEMORY
42:     else
43:       RubyProf.measure_mode = RubyProf::PROCESS_TIME
44:     end
45:   end

measure_allocations → int

Returns the total number of object allocations since Ruby started.


measure_cpu_time → float

Returns the cpu time.


gc_runs → Integer

Returns the total number of garbage collections.


gc_time → Integer

Returns the time spent doing garbage collections in microseconds.


measure_memory → int

Returns total allocated memory in bytes.


measure_mode → measure_mode

Returns what ruby-prof is measuring. Valid values include:

 RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock functions 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.
 RubyProf::ALLOCATIONS - Measure object allocations.  This requires a patched Ruby interpreter.
 RubyProf::MEMORY - Measure memory size.  This requires a patched Ruby interpreter.
 RubyProf::GC_RUNS - Measure number of garbage collections.  This requires a patched Ruby interpreter.
 RubyProf::GC_TIME - Measure time spent doing garbage collection.  This requires a patched Ruby interpreter.
[show source]
/* call-seq:
   measure_mode -> measure_mode
   
   Returns what ruby-prof is measuring.  Valid values include:
   
   *RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock functions 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. 
   *RubyProf::ALLOCATIONS - Measure object allocations.  This requires a patched Ruby interpreter.
   *RubyProf::MEMORY - Measure memory size.  This requires a patched Ruby interpreter.
   *RubyProf::GC_RUNS - Measure number of garbage collections.  This requires a patched Ruby interpreter.
   *RubyProf::GC_TIME - Measure time spent doing garbage collection.  This requires a patched Ruby interpreter.*/
static VALUE
prof_get_measure_mode(VALUE self)
{
    return INT2NUM(measure_mode);
}

measure_mode=value → void

Specifies what ruby-prof should measure. Valid values include:

 RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock functions 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.
 RubyProf::ALLOCATIONS - Measure object allocations.  This requires a patched Ruby interpreter.
 RubyProf::MEMORY - Measure memory size.  This requires a patched Ruby interpreter.
 RubyProf::GC_RUNS - Measure number of garbage collections.  This requires a patched Ruby interpreter.
 RubyProf::GC_TIME - Measure time spent doing garbage collection.  This requires a patched Ruby interpreter.
[show source]
/* call-seq:
   measure_mode=value -> void
   
   Specifies what ruby-prof should measure.  Valid values include:
   
   *RubyProf::PROCESS_TIME - Measure process time.  This is default.  It is implemented using the clock functions 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. 
   *RubyProf::ALLOCATIONS - Measure object allocations.  This requires a patched Ruby interpreter.
   *RubyProf::MEMORY - Measure memory size.  This requires a patched Ruby interpreter.
   *RubyProf::GC_RUNS - Measure number of garbage collections.  This requires a patched Ruby interpreter.
   *RubyProf::GC_TIME - Measure time spent doing garbage collection.  This requires a patched Ruby interpreter.*/
static VALUE
prof_set_measure_mode(VALUE self, VALUE val)
{
    long mode = NUM2LONG(val);

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

    switch (mode) {
      case MEASURE_PROCESS_TIME:
        get_measurement = measure_process_time;
        convert_measurement = convert_process_time;
        break;
        
      case MEASURE_WALL_TIME:
        get_measurement = measure_wall_time;
        convert_measurement = convert_wall_time;
        break;
        
      #if defined(MEASURE_CPU_TIME)
      case MEASURE_CPU_TIME:
        if (cpu_frequency == 0)
            cpu_frequency = get_cpu_frequency();
        get_measurement = measure_cpu_time;
        convert_measurement = convert_cpu_time;
        break;
      #endif
              
      #if defined(MEASURE_ALLOCATIONS)
      case MEASURE_ALLOCATIONS:
        get_measurement = measure_allocations;
        convert_measurement = convert_allocations;
        break;
      #endif
        
      #if defined(MEASURE_MEMORY)
      case MEASURE_MEMORY:
        get_measurement = measure_memory;
        convert_measurement = convert_memory;
        break;
      #endif

      #if defined(MEASURE_GC_RUNS)
      case MEASURE_GC_RUNS:
        get_measurement = measure_gc_runs;
        convert_measurement = convert_gc_runs;
        break;
      #endif

      #if defined(MEASURE_GC_TIME)
      case MEASURE_GC_TIME:
        get_measurement = measure_gc_time;
        convert_measurement = convert_gc_time;
        break;
      #endif

      default:
        rb_raise(rb_eArgError, "invalid mode: %ld", mode);
        break;
    }
    
    measure_mode = mode;
    return val;
}

measure_process_time → float

Returns the process time.


measure_wall_time → float

Returns the wall time.


pause → RubyProf

Pauses collecting profile data.

[show source]
/* call-seq:
   pause -> RubyProf

   Pauses collecting profile data. */
static VALUE
prof_pause(VALUE self)
{
    if (threads_tbl == NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf is not running.");
    }

    prof_remove_hook();
    return self;
}

profile {block} → RubyProf::Result

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

[show source]
/* call-seq:
   profile {block} -> RubyProf::Result

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

    prof_start(self);
    rb_protect(rb_yield, self, &result);
    return prof_stop(self);
}

resume {block} → RubyProf

Resumes recording profile data.

[show source]
/* call-seq:
   resume {block} -> RubyProf
   
   Resumes recording profile data.*/
static VALUE
prof_resume(VALUE self)
{
    if (threads_tbl == NULL)
    { 
        prof_start(self);
    }
    else
    { 
        prof_install_hook();
    }
    
    if (rb_block_given_p())
    {
      rb_ensure(rb_yield, self, prof_pause, self);
    }

    return self;
}

running? → boolean

Returns whether a profile is currently running.

[show source]
/* call-seq:
   running? -> boolean
   
   Returns whether a profile is currently running.*/
static VALUE
prof_running(VALUE self)
{
    if (threads_tbl != NULL)
        return Qtrue;
    else
        return Qfalse;
}

start → RubyProf

Starts recording profile data.

[show source]
/* call-seq:
   start -> RubyProf
   
   Starts recording profile data.*/
static VALUE
prof_start(VALUE self)
{
    if (threads_tbl != NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
    }

    /* Setup globals */
    last_thread_data = NULL;
    threads_tbl = threads_table_create();

    prof_install_hook();              
    return self;
}

stop → RubyProf::Result

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

[show 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;
    
    prof_remove_hook();

    prof_pop_threads();

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

    /* Unset the last_thread_data (very important!) 
       and the threads table */
    last_thread_data = NULL;
    threads_table_free(threads_tbl);
    threads_tbl = NULL;

    return result;
}