Class: RubyProf::Thread

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

Overview

The Thread class contains profile results for a single fiber (note a Ruby thread can run multiple fibers). You cannot create an instance of RubyProf::Thread, instead you access it from a RubyProf::Profile object.

profile = RubyProf::Profile.profile do
            ...
          end

profile.threads.each do |thread|
  thread.root_methods.sort.each do |method|
    puts method.total_time
  end
end

Instance Method Summary collapse

Instance Method Details

#_dump_dataObject

:nodoc:



317
318
319
320
321
322
323
324
325
326
327
# File 'ext/ruby_prof/rp_thread.c', line 317

static VALUE prof_thread_dump(VALUE self)
{
    thread_data_t* thread_data = RTYPEDDATA_DATA(self);

    VALUE result = rb_hash_new();
    rb_hash_aset(result, ID2SYM(rb_intern("fiber_id")), thread_data->fiber_id);
    rb_hash_aset(result, ID2SYM(rb_intern("methods")), prof_thread_methods(self));
    rb_hash_aset(result, ID2SYM(rb_intern("call_tree")), prof_call_tree(self));

    return result;
}

#_load_data(data) ⇒ Object

:nodoc:



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'ext/ruby_prof/rp_thread.c', line 330

static VALUE prof_thread_load(VALUE self, VALUE data)
{
    thread_data_t* thread_data = RTYPEDDATA_DATA(self);

    VALUE call_tree = rb_hash_aref(data, ID2SYM(rb_intern("call_tree")));
    thread_data->call_tree = prof_get_call_tree(call_tree);

    thread_data->fiber_id = rb_hash_aref(data, ID2SYM(rb_intern("fiber_id")));

    VALUE methods = rb_hash_aref(data, ID2SYM(rb_intern("methods")));
    for (int i = 0; i < rb_array_len(methods); i++)
    {
        VALUE method = rb_ary_entry(methods, i);
        prof_method_t* method_data = RTYPEDDATA_DATA(method);
        method_table_insert(thread_data->method_table, method_data->key, method_data);
    }

    return data;
}

#call_treeCallTree

Returns the root of the call tree.

Returns:



294
295
296
297
298
# File 'ext/ruby_prof/rp_thread.c', line 294

static VALUE prof_call_tree(VALUE self)
{
    thread_data_t* thread = prof_get_thread(self);
    return prof_call_tree_wrap(thread->call_tree);
}

#fiber_idNumeric

Returns the fiber id of this thread.

Returns:

  • (Numeric)


284
285
286
287
288
# File 'ext/ruby_prof/rp_thread.c', line 284

static VALUE prof_fiber_id(VALUE self)
{
    thread_data_t* thread = prof_get_thread(self);
    return thread->fiber_id;
}

#idNumeric

Returns the thread id of this thread.

Returns:

  • (Numeric)


274
275
276
277
278
# File 'ext/ruby_prof/rp_thread.c', line 274

static VALUE prof_thread_id(VALUE self)
{
    thread_data_t* thread = prof_get_thread(self);
    return thread->thread_id;
}

#methodsArray

Returns an array of methods that were called from this thread during program execution.

Returns:

  • (Array)


305
306
307
308
309
310
311
312
313
314
# File 'ext/ruby_prof/rp_thread.c', line 305

static VALUE prof_thread_methods(VALUE self)
{
    thread_data_t* thread = prof_get_thread(self);
    if (thread->methods == Qnil)
    {
        thread->methods = rb_ary_new();
        rb_st_foreach(thread->method_table, collect_methods, thread->methods);
    }
    return thread->methods;
}

#total_timeObject

Returns the total time this thread was executed.



4
5
6
# File 'lib/ruby-prof/thread.rb', line 4

def total_time
  self.call_tree.total_time
end

#wait_timeObject

Returns the amount of time this thread waited while other thread executed.



9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby-prof/thread.rb', line 9

def wait_time
  # wait_time, like self:time, is always method local
  # thus we need to sum over all methods and call infos
  self.methods.inject(0) do |sum, method_info|
    method_info.callers.each do |call_tree|
      sum += call_tree.wait_time
    end
    sum
  end
end