| VERSION | = | rb_str_new2(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) |
See if the user specified the clock mode via the RUBY_PROF_MEASURE_MODE environment variable
# File lib/ruby-prof.rb, line 11
11: def self.figure_measure_mode
12: case ENV["RUBY_PROF_MEASURE_MODE"]
13: when "wall" || "wall_time"
14: RubyProf.measure_mode = RubyProf::WALL_TIME
15: when "cpu" || "cpu_time"
16: if ENV.key?("RUBY_PROF_CPU_FREQUENCY")
17: RubyProf.cpu_frequency = ENV["RUBY_PROF_CPU_FREQUENCY"].to_f
18: else
19: begin
20: open("/proc/cpuinfo") do |f|
21: f.each_line do |line|
22: s = line.slice(/cpu MHz\s*:\s*(.*)/, 1)
23: if s
24: RubyProf.cpu_frequency = s.to_f * 1000000
25: break
26: end
27: end
28: end
29: rescue Errno::ENOENT
30: end
31: end
32: RubyProf.measure_mode = RubyProf::CPU_TIME
33: when "allocations"
34: RubyProf.measure_mode = RubyProf::ALLOCATIONS
35: when "memory"
36: RubyProf.measure_mode = RubyProf::MEMORY
37: else
38: RubyProf.measure_mode = RubyProf::PROCESS_TIME
39: end
40: end
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.
/* 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.*/
static VALUE
prof_get_measure_mode(VALUE self)
{
return INT2NUM(measure_mode);
}
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.
/* 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.*/
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 = measure_cpu_time();
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
default:
rb_raise(rb_eArgError, "invalid mode: %d", mode);
break;
}
measure_mode = mode;
return val;
}
Profiles the specified block and returns a RubyProf::Result object.
/* 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);
}
Returns whether a profile is currently running.
/* 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;
}
Starts recording profile data.
/* call-seq:
start -> void
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();
#ifdef RUBY_VM
rb_add_event_hook(prof_event_hook,
RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN
| RUBY_EVENT_LINE, Qnil);
#else
rb_add_event_hook(prof_event_hook,
RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN
| RUBY_EVENT_LINE);
#endif
#if defined(MEASURE_MEMORY)
rb_gc_enable_stats();
#endif
return Qnil;
}
Stops collecting profile data and returns a RubyProf::Result object.
/* call-seq:
stop -> RubyProf::Result
Stops collecting profile data and returns a RubyProf::Result object. */
static VALUE
prof_stop(VALUE self)
{
#if defined(MEASURE_MEMORY)
rb_gc_disable_stats();
#endif
VALUE result = Qnil;
if (threads_tbl == NULL)
{
rb_raise(rb_eRuntimeError, "RubyProf is not running.");
}
/* Now unregister from event */
rb_remove_event_hook(prof_event_hook);
/* 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;
}