Class: RubyProf::Profile

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

Defined Under Namespace

Classes: ExcludeCommonMethods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(options) ⇒ Object

Returns a new profiler. Possible options for the options hash are:

measure_modes::    Measure mode. Specifies the profile measure mode.
                  If not specified, defaults to RubyProf::WALL_TIME.
exclude_threads:: Threads to exclude from the profiling results.
include_threads:: Focus profiling on only the given threads. This will ignore
                  all other threads.
merge_fibers::    Whether to merge all fibers under a given thread. This should be
                  used when profiling for a callgrind printer.


450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
519
520
521
522
523
524
# File 'ext/ruby_prof/ruby_prof.c', line 450

static VALUE
prof_initialize(VALUE self, VALUE options)
{
    prof_profile_t* profile = prof_get_profile(self);
    VALUE modes = Qnil;
    VALUE exclude_threads = Qnil;
    VALUE include_threads = Qnil;
    VALUE merge_fibers = Qnil;
    VALUE exclude_common = Qnil;
    int i;
    prof_measure_mode_t* measure_modes = NULL;
    size_t measure_modes_len = 0;

    Check_Type(options, T_HASH);
    modes = rb_hash_aref(options, ID2SYM(rb_intern("measure_modes")));
    merge_fibers = rb_hash_aref(options, ID2SYM(rb_intern("merge_fibers")));
    exclude_common = rb_hash_aref(options, ID2SYM(rb_intern("exclude_common")));
    exclude_threads = rb_hash_aref(options, ID2SYM(rb_intern("exclude_threads")));
    include_threads = rb_hash_aref(options, ID2SYM(rb_intern("include_threads")));

    if (modes != Qnil) {
        Check_Type(modes, T_ARRAY);
        measure_modes_len = RARRAY_LEN(modes);

        if (measure_modes_len > 0) {
            measure_modes = ALLOC_N(prof_measure_mode_t, measure_modes_len);

            for (size_t j = 0; j < measure_modes_len; j++) {
                measure_modes[j] = NUM2INT(rb_ary_entry(modes, j));
            }
        }
    }

    if (measure_modes == NULL) {
        measure_modes_len = 1;
        measure_modes = ALLOC_N(prof_measure_mode_t, measure_modes_len);
        measure_modes[0] = MEASURE_WALL_TIME;
    }

    profile->measurer = prof_get_measurer(measure_modes, measure_modes_len);
    profile->measurements = ruby_xmalloc(sizeof(prof_measurements_t) + profile->measurer->len * sizeof(double));
    profile->measurements->len = profile->measurer->len;
    profile->measurements_at_pause_resume = ruby_xmalloc(sizeof(prof_measurements_t) + profile->measurer->len * sizeof(double));
    profile->measurements_at_pause_resume->len = profile->measurer->len;

    profile->merge_fibers = merge_fibers != Qnil && merge_fibers != Qfalse;

    if (exclude_threads != Qnil) {
        Check_Type(exclude_threads, T_ARRAY);
        assert(profile->exclude_threads_tbl == NULL);
        profile->exclude_threads_tbl = threads_table_create();
        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);
        }
    }

    if (include_threads != Qnil) {
        Check_Type(include_threads, T_ARRAY);
        assert(profile->include_threads_tbl == NULL);
        profile->include_threads_tbl = threads_table_create();
        for (i = 0; i < RARRAY_LEN(include_threads); i++) {
            VALUE thread = rb_ary_entry(include_threads, i);
            VALUE thread_id = rb_obj_id(thread);
            st_insert(profile->include_threads_tbl, thread_id, Qtrue);
        }
    }

    if (RTEST(exclude_common)) {
        prof_exclude_common_methods(self);
    }

    return self;
}

Class Method Details

.profile(&block) ⇒ self .profile(options, &block) ⇒ self

Profiles the specified block and returns a RubyProf::Profile object. Arguments are passed to Profile initialize method.

Overloads:

  • .profile(&block) ⇒ self

    Returns:

    • (self)
  • .profile(options, &block) ⇒ self

    Returns:

    • (self)


699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'ext/ruby_prof/ruby_prof.c', line 699

static VALUE
prof_profile_class(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

#exclude_common_methods!Object

Hides methods that, when represented as a call graph, have extremely large in and out degrees and make navigation impossible.



8
9
10
# File 'lib/ruby-prof/profile.rb', line 8

def exclude_common_methods!
  ExcludeCommonMethods.apply!(self)
end

#exclude_method!(klass, sym) ⇒ Object



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'ext/ruby_prof/ruby_prof.c', line 734

static VALUE
prof_exclude_method(VALUE self, VALUE klass, VALUE sym)
{
    prof_profile_t* profile = prof_get_profile(self);
    ID mid = SYM2ID(sym);

    prof_method_key_t key;
    prof_method_t *method;

    if (profile->running == Qtrue)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
    }

    method_key(&key, klass, mid);
    method = method_table_lookup(profile->exclude_methods_tbl, &key);

    if (!method) {
      method = prof_method_create_excluded(klass, mid);
      method_table_insert(profile->exclude_methods_tbl, method->key, method);
    }

    return self;
}

#exclude_methods!(mod, *method_or_methods) ⇒ Object



12
13
14
15
16
# File 'lib/ruby-prof/profile.rb', line 12

def exclude_methods!(mod, *method_or_methods)
  [method_or_methods].flatten.each do |name|
    exclude_method!(mod, name)
  end
end

#exclude_singleton_methods!(mod, *method_or_methods) ⇒ Object



18
19
20
# File 'lib/ruby-prof/profile.rb', line 18

def exclude_singleton_methods!(mod, *method_or_methods)
  exclude_methods!(mod.singleton_class, *method_or_methods)
end

#measure_modesObject



759
760
761
762
763
764
765
766
767
768
769
# File 'ext/ruby_prof/ruby_prof.c', line 759

static VALUE
prof_measure_modes(VALUE self)
{
    prof_profile_t* profile = prof_get_profile(self);
    VALUE ary = rb_ary_new2(profile->measurer->len);
    for(size_t i = 0; i < profile->measurer->len; i++) {
        rb_ary_store(ary, i, INT2NUM(profile->measurer->measure_modes[i]));
    }

    return ary;
}

#pauseself

Pauses collecting profile data.

Returns:

  • (self)


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 595

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;
        prof_measurer_take_measurements(profile->measurer, profile->measurements_at_pause_resume);
        st_foreach(profile->threads_tbl, pause_thread, (st_data_t) profile);
    }

    return self;
}

#paused?Boolean

Returns whether a profile is currently paused.

Returns:

  • (Boolean)


530
531
532
533
534
535
# File 'ext/ruby_prof/ruby_prof.c', line 530

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

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

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

Yields:

Returns:

  • (RubyProf::Result)


719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'ext/ruby_prof/ruby_prof.c', line 719

static VALUE
prof_profile_object(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);

}

#resumeself #resume(&block) ⇒ self

Resumes recording profile data.

Overloads:

  • #resumeself

    Returns:

    • (self)
  • #resume(&block) ⇒ self

    Returns:

    • (self)


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

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;
        prof_measurer_take_measurements(profile->measurer, profile->measurements_at_pause_resume);
        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)


541
542
543
544
545
546
# File 'ext/ruby_prof/ruby_prof.c', line 541

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

#startself

Starts recording profile data.

Returns:

  • (self)


552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'ext/ruby_prof/ruby_prof.c', line 552

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


642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'ext/ruby_prof/ruby_prof.c', line 642

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;

    return self;
}

#threadsArray of RubyProf::Thread

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

Returns:



683
684
685
686
687
688
689
690
# File 'ext/ruby_prof/ruby_prof.c', line 683

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