class RubyProf::Profile

  1. ext/ruby_prof/ruby_prof.c
  2. lib/ruby-prof/profile.rb
  3. show all
Parent: RubyProf

Public Class methods

RubyProf::Profile.new(mode, exclude_threads) -> instance

Returns a new profiler.

Parameters

mode

Measure mode (optional). Specifies the profile measure mode. If not specified, defaults to RubyProf::WALL_TIME.

exclude_threads

Threads to exclude from the profiling results (optional).

[show source]
static VALUE
prof_initialize(int argc,  VALUE *argv, VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    VALUE mode;
        prof_measure_mode_t measurer;
    VALUE exclude_threads;
        int i;
    
    switch (rb_scan_args(argc, argv, "02", &mode, &exclude_threads))
    {
      case 0:
      {
        measurer = MEASURE_WALL_TIME;
        exclude_threads = rb_ary_new();
                break;
      }
      case 1:
      {
        measurer = (prof_measure_mode_t)NUM2INT(mode);
        exclude_threads = rb_ary_new();
                break;
      }
      case 2:
      {
                Check_Type(exclude_threads, T_ARRAY);
        measurer = (prof_measure_mode_t)NUM2INT(mode);
                break;
      }
    }

    profile->measurer = prof_get_measurer(measurer);
    profile->threads = rb_hash_new();


        for (i = 0; i < RARRAY_LEN(exclude_threads); i++)
        {
                VALUE thread = rb_ary_entry(exclude_threads, i);
                VALUE thread_id = rb_obj_id(thread);
            st_insert(profile->exclude_threads_tbl, thread_id, Qtrue);
        }

    return self;
}
profile {block} -> RubyProf::Result

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

[show source]
static VALUE
prof_profile(int argc,  VALUE *argv, VALUE klass)
{
    int result;
    VALUE profile = rb_class_new_instance(argc, argv, cProfile);

    if (!rb_block_given_p())
    {
        rb_raise(rb_eArgError, "A block must be provided to the profile method.");
    }

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

Public Instance methods

compute_minimality ()

this method gets called internally when profiling is stopped. it determines for each call_info whether it is minimal: a call_info is minimal in a call tree if the call_info is not a descendant of a call_info of the same method

[show source]
# File lib/ruby-prof/profile.rb, line 9
def compute_minimality
  threads.each do |threadid, method_infos|
    root_methods = method_infos.select{|mi| mi.root?}
    root_methods.each do |mi|
      mi.call_infos.select{|ci| ci.root?}.each do |call_info_root|
        call_info_root.compute_minimality(Set.new)
      end
    end
  end
end
dump ()
[show source]
# File lib/ruby-prof/profile.rb, line 32
def dump
  threads.each do |thread_id, methods|
    $stderr.puts "Call Info Dump for thread id #{thread_id}"
    methods.each do |method_info|
      $stderr.puts method_info.dump
    end
  end
end
eliminate_methods! (matchers)

eliminate some calls from the graph by merging the information into callers. matchers can be a list of strings or regular expressions or the name of a file containing regexps.

[show source]
# File lib/ruby-prof/profile.rb, line 22
def eliminate_methods!(matchers)
  matchers = read_regexps_from_file(matchers) if matchers.is_a?(String)
  eliminated = []
  threads.each do |thread_id, methods|
    matchers.each{ |matcher| eliminated.concat(eliminate_methods(methods, matcher)) }
  end
  compute_minimality # is this really necessary?
  eliminated
end
pause -> RubyProf

Pauses collecting profile data.

[show source]
static VALUE
prof_pause(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    if (profile->running == Qfalse)
    {
        rb_raise(rb_eRuntimeError, "RubyProf is not running.");
    }

    profile->running = Qfalse;

    prof_remove_hook();
    return self;
}
resume {block} -> RubyProf

Resumes recording profile data.

[show source]
static VALUE
prof_resume(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    if (profile->running == Qfalse)
    {
        prof_start(self);
    }
    else
    {
        profile->running = Qtrue;
        prof_install_hook(self);
    }

    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]
static VALUE
prof_running(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    return profile->running;
}
start -> RubyProf

Starts recording profile data.

[show source]
static VALUE
prof_start(VALUE self)
{
        char* trace_file_name;
    prof_profile_t* profile = prof_get_profile(self);
        
    if (profile->running == Qtrue)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
    }

    profile->running = Qtrue;
    profile->last_thread_data = NULL;
    profile->threads_tbl = threads_table_create();

    /* open trace file if environment wants it */
    trace_file_name = getenv("RUBY_PROF_TRACE");
    if (trace_file_name != NULL) {
      if (0==strcmp(trace_file_name, "stdout")) {
        trace_file = stdout;
      } else if (0==strcmp(trace_file_name, "stderr")) {
        trace_file = stderr;
      } else {
        trace_file = fopen(trace_file_name, "a");
      }
    }

    prof_install_hook(self);
    return self;
}
stop -> self

Stops collecting profile data.

[show source]
static VALUE
prof_stop(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);

        /* get 'now' before prof emove hook because it calls GC.disable_stats
      which makes the call within prof_pop_threads of now return 0, which is wrong
    */
    profile->measurement = profile->measurer->measure();
    if (profile->running == Qfalse)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was not yet called");
    }
  
    /* close trace file if open */
    if (trace_file != NULL) {
      if (trace_file!=stderr && trace_file!=stdout)
        fclose(trace_file);
      trace_file = NULL;
    }
    
    prof_remove_hook();
    prof_pop_threads(profile);

    /* Unset the last_thread_data (very important!)
       and the threads table */
    profile->running = Qfalse;
    profile->last_thread_data = NULL;

    /* Save the result */
    st_foreach(profile->threads_tbl, collect_threads, profile->threads);
        threads_table_free(profile->threads_tbl);
        profile->threads_tbl = NULL;

    /* compute minimality of call_infos */
    rb_funcall(self, rb_intern("compute_minimality") , 0);

    return self;
}
threads -> Hash

Returns a hash table keyed on thread ID. For each thread id, the hash table stores another hash table that contains profiling information for each method called during the threads execution. That hash table is keyed on method name and contains RubyProf::MethodInfo objects.

[show source]
static VALUE
prof_threads(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    return profile->threads;
}