Class: RubyProf::Thread

Inherits:
Data
  • 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:



294
295
296
297
298
299
300
301
302
303
304
# File 'ext/ruby_prof/rp_thread.c', line 294

static VALUE
prof_thread_dump(VALUE self)
{
    thread_data_t* thread_data = DATA_PTR(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));

    return result;
}

#_load_data(data) ⇒ Object

:nodoc:



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'ext/ruby_prof/rp_thread.c', line 307

static VALUE
prof_thread_load(VALUE self, VALUE data)
{
    thread_data_t* thread_data = DATA_PTR(self);
    thread_data->object = self;

    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 = DATA_PTR(method);
        method_table_insert(thread_data->method_table, method_data->key, method_data);
    }

    return data;
}

#fiber_idNumeric

Returns the fiber id of this thread.

Returns:

  • (Numeric)


269
270
271
272
273
274
# File 'ext/ruby_prof/rp_thread.c', line 269

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)


258
259
260
261
262
263
# File 'ext/ruby_prof/rp_thread.c', line 258

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)


281
282
283
284
285
286
287
288
289
290
291
# File 'ext/ruby_prof/rp_thread.c', line 281

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

#root_methodsObject

Returns the root methods (ie, methods that were not called by other methods) that were profiled while this thread was executing. Generally there is only one root method (multiple root methods can occur when Profile#pause is used). By starting with the root methods, you can descend down the profile call tree.



7
8
9
10
11
# File 'lib/ruby-prof/thread.rb', line 7

def root_methods
  self.methods.select do |method_info|
    method_info.root?
  end
end

#total_timeObject

Returns the total time this thread was executed.



14
15
16
17
18
19
20
21
# File 'lib/ruby-prof/thread.rb', line 14

def total_time
  self.root_methods.inject(0) do |sum, method_info|
    method_info.callers.each do |call_info|
      sum += call_info.total_time
    end
    sum
  end
end

#wait_timeObject

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-prof/thread.rb', line 24

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_info|
      sum += call_info.wait_time
    end
    sum
  end
end