Class: RubyProf::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prof/profile.rb,
ext/ruby_prof/ruby_prof.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#RubyProf::Profile.new(mode, exclude_threads) ⇒ Object

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).


477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'ext/ruby_prof/ruby_prof.c', line 477

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 = MEASURE_WALL_TIME;
    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);

    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;
}

Class Method Details

.profile { ... } ⇒ RubyProf::Result

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

Yields:

Returns:

  • (RubyProf::Result)


707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'ext/ruby_prof/ruby_prof.c', line 707

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);
}

Instance Method Details

#detect_recursion(thread) ⇒ Object

This method detect recursive calls in the call graph.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby-prof/profile.rb', line 16

def detect_recursion(thread)
  visited_methods = Hash.new do |hash, key|
    hash[key] = 0
  end

  visitor = CallInfoVisitor.new(thread)
  visitor.visit do |call_info, event|
    case event
    when :enter
      visited_methods[call_info.target] += 1
      call_info.recursive = (visited_methods[call_info.target] > 1)
    when :exit
      visited_methods[call_info.target] -= 1
      if visited_methods[call_info.target] == 0
        visited_methods.delete(call_info.target)
      end
    end
  end
end

#eliminate_methods!(matchers) ⇒ Object

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.



38
39
40
41
42
43
44
45
# File 'lib/ruby-prof/profile.rb', line 38

def eliminate_methods!(matchers)
  matchers = read_regexps_from_file(matchers) if matchers.is_a?(String)
  eliminated = []
  threads.each do |thread|
    matchers.each{ |matcher| eliminated.concat(eliminate_methods(thread.methods, matcher)) }
  end
  eliminated
end

#pauseRubyProf

Pauses collecting profile data.

Returns:



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'ext/ruby_prof/ruby_prof.c', line 618

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.");
    }

    if (profile->paused == Qfalse)
    {
        profile->paused = Qtrue;
        profile->measurement_at_pause_resume = profile->measurer->measure();
        st_foreach(profile->threads_tbl, pause_thread, (st_data_t) profile);
    }

    return self;
}

#paused?Boolean

Returns whether a profile is currently paused.

Returns:

  • (Boolean)


546
547
548
549
550
551
# File 'ext/ruby_prof/ruby_prof.c', line 546

static VALUE
prof_paused(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    return profile->paused;
}

#post_processObject

This method gets called once profiling has been completed but before results are returned to the user. Thus it provides a hook to do any necessary post-processing on the call graph.



9
10
11
12
13
# File 'lib/ruby-prof/profile.rb', line 9

def post_process
  self.threads.each do |thread|
    detect_recursion(thread)
  end
end

#resume { ... } ⇒ RubyProf

Resumes recording profile data.

Yields:

Returns:



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'ext/ruby_prof/ruby_prof.c', line 641

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

    if (profile->paused == Qtrue)
    {
        profile->paused = Qfalse;
        profile->measurement_at_pause_resume = profile->measurer->measure();
        st_foreach(profile->threads_tbl, unpause_thread, (st_data_t) profile);
    }

    return rb_block_given_p() ? rb_ensure(rb_yield, self, prof_pause, self) : self;
}

#running?Boolean

Returns whether a profile is currently running.

Returns:

  • (Boolean)


557
558
559
560
561
562
# File 'ext/ruby_prof/ruby_prof.c', line 557

static VALUE
prof_running(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    return profile->running;
}

#startRubyProf

Starts recording profile data.

Returns:



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'ext/ruby_prof/ruby_prof.c', line 568

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");
    }

#ifndef RUBY_VM
	if (pCurrentProfile != NULL)
    {
        rb_raise(rb_eRuntimeError, "Only one profile can run at a time on Ruby 1.8.*");
    }
#endif

    profile->running = Qtrue;
    profile->paused = Qfalse;
    profile->last_thread_data = NULL;


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

    prof_install_hook(self);
    return self;
}

#stopself

Stops collecting profile data.

Returns:

  • (self)


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'ext/ruby_prof/ruby_prof.c', line 664

static VALUE
prof_stop(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);

    if (profile->running == Qfalse)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was not yet called");
    }
  
    prof_remove_hook();

    /* close trace file if open */
    if (trace_file != NULL) 
    {
      if (trace_file !=stderr && trace_file != stdout)
      {
#ifdef _MSC_VER
          _fcloseall();
#else
        fclose(trace_file);
#endif
      }
      trace_file = NULL;
    }
    
    prof_pop_threads(profile);

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

    /* Post process result */
    rb_funcall(self, rb_intern("post_process") , 0);

    return self;
}

#threadsArray of RubyProf::Thread

Returns an array of RubyProf::Thread instances that were executed while the the program was being run.

Returns:



728
729
730
731
732
733
734
735
# File 'ext/ruby_prof/ruby_prof.c', line 728

static VALUE
prof_threads(VALUE self)
{
	VALUE result = rb_ary_new();
    prof_profile_t* profile = prof_get_profile(self);
    st_foreach(profile->threads_tbl, collect_threads, result);
    return result;
}