Module: RubyProf

Defined in:
lib/ruby-prof.rb,
lib/ruby-prof/profiletask.rb,
lib/ruby-prof/flat_printer.rb,
lib/ruby-prof/graph_printer.rb,
lib/ruby-prof/graph_html_printer.rb,
ext/ruby_prof.c

Defined Under Namespace

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

Constant Summary collapse

VERSION =
rb_str_new2(PROF_VERSION)
PROCESS_TIME =
INT2NUM(CLOCK_MODE_PROCESS)
WALL_TIME =
INT2NUM(CLOCK_MODE_WALL)
CPU_TIME =
INT2NUM(CLOCK_MODE_CPU)

Class Method Summary collapse

Class Method Details

.clock_modeObject

Returns the current clock mode. Valid values include: RubyProf::PROCESS_TIME - Measure process time. This is default. It is implemented using the clock function 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.



1247
1248
1249
1250
1251
# File 'ext/ruby_prof.c', line 1247

static VALUE
prof_get_clock_mode(VALUE self)
{
    return INT2NUM(clock_mode);
}

.clock_mode=Object

Specifies the method ruby-prof uses to measure time. Valid values include: RubyProf::PROCESS_TIME - Measure process time. This is default. It is implemented using the clock function 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.



1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
# File 'ext/ruby_prof.c', line 1260

static VALUE
prof_set_clock_mode(VALUE self, VALUE val)
{
    int mode = NUM2INT(val);

    if (threads_tbl)
    {
	    rb_raise(rb_eRuntimeError, "can't set clock_mode while profiling");
    }

    switch (mode) {
    case CLOCK_MODE_PROCESS:
    	get_clock = clock_get_clock;
	    clock2sec = clock_clock2sec;
    	break;
    case CLOCK_MODE_WALL:
	    get_clock = gettimeofday_get_clock;
	    clock2sec = gettimeofday_clock2sec;
	    break;
#ifdef CLOCK_MODE_CPU
    case CLOCK_MODE_CPU:
	    if (cpu_frequency == 0)
	        cpu_frequency = get_cpu_frequency();
	        get_clock = cpu_get_clock;
	        clock2sec = cpu_clock2sec;
	    break;
#endif
    default:
	    rb_raise(rb_eArgError, "invalid mode: %d", mode);
	break;
    }
    clock_mode = mode;
    return val;
}

.cpu_frequencyInteger

Returns the cpu’s frequency. This value is needed when using the cpu RubyProf::clock_mode.

Returns:

  • (Integer)


228
229
230
231
232
# File 'ext/ruby_prof.c', line 228

static VALUE
prof_get_cpu_frequency(VALUE self)
{
    return rb_float_new(cpu_frequency);
}

.cpu_frequency=Object

Sets the cpu’s frequency. This value is needed when using the cpu RubyProf::clock_mode.



239
240
241
242
243
244
# File 'ext/ruby_prof.c', line 239

static VALUE
prof_set_cpu_freqeuncy(VALUE self, VALUE val)
{
    cpu_frequency = NUM2DBL(val);
    return val;
}

.figure_clock_modeObject

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



10
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
# File 'lib/ruby-prof.rb', line 10

def self.figure_clock_mode
  case ENV["RUBY_PROF_CLOCK_MODE"]
 when "wall" || "wall_time"
  RubyProf.clock_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.clock_mode = RubyProf::CPU_TIME
 else
	  RubyProf.clock_mode = RubyProf::PROCESS_TIME
	end
end

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

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

Yields:

Returns:



1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
# File 'ext/ruby_prof.c', line 1374

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)


1302
1303
1304
1305
1306
1307
1308
1309
# File 'ext/ruby_prof.c', line 1302

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

.startObject

Starts recording profile data.



1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
# File 'ext/ruby_prof.c', line 1315

static VALUE
prof_start(VALUE self)
{
    toplevel_id = rb_intern("toplevel");
    toplevel_key = method_key(Qnil, toplevel_id);

    if (threads_tbl != NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
    }

    /* Setup globals */
    class_tbl = rb_hash_new();
    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);

    return Qnil;
}

.stopRubyProf::Result

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

Returns:



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
# File 'ext/ruby_prof.c', line 1342

static VALUE
prof_stop(VALUE self)
{
    VALUE result = Qnil;

    if (threads_tbl == NULL)
    {
        rb_raise(rb_eRuntimeError, "RubyProf.start is not called yet");
    }

    /* Now unregister from event   */
    rb_remove_event_hook(prof_event_hook);

    /* Create the result */
    result = prof_result_new();

    /* Free threads table */
    free_threads(threads_tbl);
    threads_table_free(threads_tbl);
    threads_tbl = NULL;

    /* Free reference to class_tbl */
    class_tbl = Qnil;
    
    return result;
}