Module: RubyProf

Defined in:
lib/ruby-prof.rb,
lib/ruby-prof/task.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,
ext/ruby_prof.c

Defined Under Namespace

Classes: AbstractPrinter, CallInfo, CallTreePrinter, FlatPrinter, GraphHtmlPrinter, GraphPrinter, MethodInfo, ProfileTask, Result

Constant Summary collapse

VERSION =
rb_str_new2(PROF_VERSION)
PROCESS_TIME =
INT2NUM(MEASURE_PROCESS_TIME)
WALL_TIME =
INT2NUM(MEASURE_WALL_TIME)
CPU_TIME =
INT2NUM(MEASURE_CPU_TIME)
ALLOCATED_OBJECTS =
INT2NUM(MEASURE_ALLOCATIONS)

Class Method Summary collapse

Class Method Details

.cpu_frequencyObject

.cpu_frequency=Object

.figure_measure_modeObject

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-prof.rb', line 11

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
  else
    RubyProf.measure_mode = RubyProf::PROCESS_TIME
  end
end

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



1316
1317
1318
1319
1320
# File 'ext/ruby_prof.c', line 1316

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.



1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
# File 'ext/ruby_prof.c', line 1331

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
        
      default:
        rb_raise(rb_eArgError, "invalid mode: %d", mode);
        break;
    }
    
    measure_mode = mode;
    return val;
}

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

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

Yields:

Returns:



1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
# File 'ext/ruby_prof.c', line 1452

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

.running?Boolean

Returns whether a profile is currently running.

Returns:

  • (Boolean)


1384
1385
1386
1387
1388
1389
1390
1391
# File 'ext/ruby_prof.c', line 1384

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

.startObject

Starts recording profile data.



1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
# File 'ext/ruby_prof.c', line 1397

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();
    
    rb_add_event_hook(prof_event_hook,
          RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
          RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN 
          | RUBY_EVENT_LINE);

    return Qnil;
}

.stopRubyProf::Result

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

Returns:



1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
# File 'ext/ruby_prof.c', line 1422

static VALUE
prof_stop(VALUE self)
{
    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;
}