Module: RubyProf

Defined in:
lib/ruby-prof.rb,
lib/ruby-prof/task.rb,
lib/ruby-prof/test.rb,
lib/ruby-prof/call_info.rb,
lib/ruby-prof/method_info.rb,
lib/ruby-prof/flat_printer.rb,
lib/ruby-prof/graph_printer.rb,
lib/ruby-prof/abstract_printer.rb,
lib/ruby-prof/call_tree_printer.rb,
lib/ruby-prof/graph_html_printer.rb,
lib/ruby-prof/aggregate_call_info.rb,
lib/ruby-prof/call_tree/html_printer.rb,
lib/ruby-prof/call_tree/text_printer.rb,
lib/ruby-prof/call_tree/abstract_printer.rb,
lib/ruby-prof/flat_printer_with_line_numbers.rb,
ext/ruby_prof/ruby_prof.c

Defined Under Namespace

Modules: Test Classes: AbstractPrinter, AggregateCallInfo, CallInfo, CallTreeAbstractPrinter, CallTreeHtmlPrinter, CallTreePrinter, CallTreeTextPrinter, FlatPrinter, FlatPrinterWithLineNumbers, GraphHtmlPrinter, GraphPrinter, MethodInfo, ProfileTask, Result

Constant Summary collapse

VERSION =
rb_str_new2(RUBY_PROF_VERSION)
CLOCKS_PER_SEC =
INT2NUM(CLOCKS_PER_SEC)
PROCESS_TIME =
INT2NUM(MEASURE_PROCESS_TIME)
WALL_TIME =

in measure_process_time.h

INT2NUM(MEASURE_WALL_TIME)
CPU_TIME =
INT2NUM(MEASURE_CPU_TIME)
ALLOCATIONS =
INT2NUM(MEASURE_ALLOCATIONS)
MEMORY =
INT2NUM(MEASURE_MEMORY)
GC_RUNS =
INT2NUM(MEASURE_GC_RUNS)
GC_TIME =
INT2NUM(MEASURE_GC_TIME)

Class Method Summary collapse

Class Method Details

.call_tree_profile_onBoolean

Returns whether ruby-prof is recording the full call tree information

Returns:

  • (Boolean)


196
197
198
199
200
201
# File 'ext/ruby_prof/ruby_prof.c', line 196

static VALUE
prof_get_call_tree_profile_on(VALUE self)
{
   if (call_tree_profile_on) { return Qtrue; }
   else                      { return Qfalse;}
}

.call_tree_profile_onBoolean

Returns whether ruby-prof is recording the full call tree information

Returns:

  • (Boolean)


207
208
209
210
211
212
213
214
215
216
217
# File 'ext/ruby_prof/ruby_prof.c', line 207

static VALUE
prof_set_call_tree_profile_on(VALUE self, VALUE val)
{
    if (threads_tbl)
    {
      rb_raise(rb_eRuntimeError, "can't set call_tree_profile while profiling");
    }

    call_tree_profile_on = RTEST(val);
    return val;
}

.cpu_frequencyObject

.cpu_frequency=Object

.exclude_threads=Object

Specifies what threads ruby-prof should exclude from profiling



1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
# File 'ext/ruby_prof/ruby_prof.c', line 1580

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_modeObject

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby-prof.rb', line 27

def self.figure_measure_mode
  case ENV["RUBY_PROF_MEASURE_MODE"]
  when "wall" || "wall_time"
    RubyProf.measure_mode = RubyProf::WALL_TIME
  when "cpu" || "cpu_time"
    if ENV.key?("RUBY_PROF_CPU_FREQUENCY")
      RubyProf.cpu_frequency = ENV["RUBY_PROF_CPU_FREQUENCY"].to_f
    else
      begin
        open("/proc/cpuinfo") do |f|
          f.each_line do |line|
            s = line.slice(/cpu MHz\s*:\s*(.*)/, 1)
            if s
              RubyProf.cpu_frequency = s.to_f * 1000000
              break
            end
          end
        end
      rescue Errno::ENOENT
      end
    end
    RubyProf.measure_mode = RubyProf::CPU_TIME
  when "allocations"
    RubyProf.measure_mode = RubyProf::ALLOCATIONS
  when "memory"
    RubyProf.measure_mode = RubyProf::MEMORY
  else
    RubyProf.measure_mode = RubyProf::PROCESS_TIME
  end
end

.measure_allocationsObject

.measure_cpu_timeObject

.measure_gc_runsObject

.measure_gc_timeObject

.measure_memoryObject

.measure_modeObject

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.



1491
1492
1493
1494
1495
# File 'ext/ruby_prof/ruby_prof.c', line 1491

static VALUE
prof_get_measure_mode(VALUE self)
{
    return INT2NUM(measure_mode);
}

.measure_mode=Object

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.



1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
# File 'ext/ruby_prof/ruby_prof.c', line 1509

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_timeObject

.measure_wall_timeObject

.pauseRubyProf

Pauses collecting profile data.

Returns:



1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
# File 'ext/ruby_prof/ruby_prof.c', line 1690

static VALUE
prof_pause(VALUE self)
{
    if (threads_tbl == NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf is not running.");
    }

    prof_remove_hook();
    return self;
}

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

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

Yields:

Returns:



1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
# File 'ext/ruby_prof/ruby_prof.c', line 1762

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 { ... } ⇒ RubyProf

Resumes recording profile data.

Yields:

Returns:



1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
# File 'ext/ruby_prof/ruby_prof.c', line 1706

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.

Returns:

  • (Boolean)


1651
1652
1653
1654
1655
1656
1657
1658
# File 'ext/ruby_prof/ruby_prof.c', line 1651

static VALUE
prof_running(VALUE self)
{
    if (threads_tbl != NULL)
        return Qtrue;
    else
        return Qfalse;
}

.startRubyProf

Starts recording profile data.

Returns:



1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
# File 'ext/ruby_prof/ruby_prof.c', line 1664

static VALUE
prof_start(VALUE self)
{
    if (call_tree_profile_on)
    {
      call_tree_prof_start(self);
    }
    else 
    {
      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;
}

.stopRubyProf::Result

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

Returns:



1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
# File 'ext/ruby_prof/ruby_prof.c', line 1730

static VALUE
prof_stop(VALUE self)
{
    prof_remove_hook();

    if (call_tree_profile_on) 
    {
      return call_tree_prof_stop(self);
    }
    else
    {
      VALUE result = Qnil;

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