[Feature #10602] Add new API rb_profile_thread_frames()

Add a new API rb_profile_thread_frames(), which is essentialy a
per-thread version of rb_profile_frames().

While the original rb_profile_frames() always returns results about the
current active thread obtained by GET_EC(), this new API takes a Thread
to be profiled as an argument.

This should come in handy when profiling I/O-bound programs such as
webapps, since this new API allows us to learn about Threads performing
I/O (which do not have the GVL).

Profiling worker threads (such as Sidekiq workers) may be another
application.

Implements [Feature #10602]

Co-authored-by: Mike Perham <mike@perham.net>
This commit is contained in:
Daisuke Aritomo 2023-09-29 15:35:36 +09:00 коммит произвёл Koichi Sasada
Родитель 02ecc3c855
Коммит 4adf418be9
4 изменённых файлов: 103 добавлений и 3 удалений

Просмотреть файл

@ -37,8 +37,29 @@ profile_frames(VALUE self, VALUE start_v, VALUE num_v)
return result;
}
static VALUE
profile_thread_frames(VALUE self, VALUE thread, VALUE start_v, VALUE num_v)
{
int i, collected_size;
int start = NUM2INT(start_v);
int buff_size = NUM2INT(num_v);
VALUE buff[MAX_BUF_SIZE];
int lines[MAX_BUF_SIZE];
VALUE result = rb_ary_new();
if (buff_size > MAX_BUF_SIZE) rb_raise(rb_eRuntimeError, "too long buff_size");
collected_size = rb_profile_thread_frames(thread, start, buff_size, buff, lines);
for (i=0; i<collected_size; i++) {
rb_ary_push(result, rb_profile_frame_full_label(buff[i]));
}
return result;
}
void
Init_profile_frames(VALUE klass)
{
rb_define_module_function(klass, "profile_frames", profile_frames, 2);
rb_define_module_function(klass, "profile_thread_frames", profile_thread_frames, 3);
}

Просмотреть файл

@ -50,6 +50,25 @@ RBIMPL_ATTR_NONNULL((3))
*/
int rb_profile_frames(int start, int limit, VALUE *buff, int *lines);
/**
* Queries mysterious "frame"s of the given range.
*
* A per-thread version of rb_profile_frames().
* Arguments and return values are the same with rb_profile_frames() with the
* exception of the first argument _thread_, which accepts the Thread to be
* profiled/queried.
*
* @param[in] thread The Ruby Thread to be profiled.
* @param[in] start Start position (0 means the topmost).
* @param[in] limit Number objects of `buff`.
* @param[out] buff Return buffer.
* @param[out] lines Return buffer.
* @return Number of objects filled into `buff`.
* @post `buff` is filled with backtrace pointers.
* @post `lines` is filled with `__LINE__` of each backtraces.
*/
int rb_profile_thread_frames(VALUE thread, int start, int limit, VALUE *buff, int *lines);
/**
* Queries the path of the passed backtrace.
*

Просмотреть файл

@ -39,6 +39,20 @@ class SampleClassForTestProfileFrames
end
end
class SampleClassForTestProfileThreadFrames
def initialize(mutex)
@mutex = mutex
end
def foo(block)
bar(block)
end
def bar(block)
block.call
end
end
class TestProfileFrames < Test::Unit::TestCase
def test_profile_frames
obj, frames = Fiber.new{
@ -139,6 +153,39 @@ class TestProfileFrames < Test::Unit::TestCase
}
end
def test_profile_thread_frames
mutex = Mutex.new
th = Thread.new do
mutex.lock
Thread.stop
SampleClassForTestProfileThreadFrames.new(mutex).foo(lambda { mutex.unlock; loop { sleep(1) } } )
end
# ensure execution has reached SampleClassForTestProfileThreadFrames#bar before running profile_thread_frames
loop { break if th.status == "sleep"; sleep 0.1 }
th.run
mutex.lock # wait until SampleClassForTestProfileThreadFrames#bar has been called
frames = Bug::Debug.profile_thread_frames(th, 0, 10)
full_labels = [
"Kernel#sleep",
"TestProfileFrames#test_profile_thread_frames",
"Kernel#loop",
"TestProfileFrames#test_profile_thread_frames",
"SampleClassForTestProfileThreadFrames#bar",
"SampleClassForTestProfileThreadFrames#foo",
"TestProfileFrames#test_profile_thread_frames",
]
frames.each.with_index do |frame, i|
assert_equal(full_labels[i], frame)
end
th.kill
end
def test_matches_backtrace_locations_main_thread
assert_equal(Thread.current, Thread.main)

Просмотреть файл

@ -1584,11 +1584,10 @@ rb_debug_inspector_backtrace_locations(const rb_debug_inspector_t *dc)
return dc->backtrace;
}
int
rb_profile_frames(int start, int limit, VALUE *buff, int *lines)
static int
thread_profile_frames(rb_execution_context_t *ec, int start, int limit, VALUE *buff, int *lines)
{
int i;
const rb_execution_context_t *ec = GET_EC();
const rb_control_frame_t *cfp = ec->cfp, *end_cfp = RUBY_VM_END_CONTROL_FRAME(ec);
const rb_control_frame_t *top = cfp;
const rb_callable_method_entry_t *cme;
@ -1650,6 +1649,20 @@ rb_profile_frames(int start, int limit, VALUE *buff, int *lines)
return i;
}
int
rb_profile_frames(int start, int limit, VALUE *buff, int *lines)
{
rb_execution_context_t *ec = GET_EC();
return thread_profile_frames(ec, start, limit, buff, lines);
}
int
rb_profile_thread_frames(VALUE thread, int start, int limit, VALUE *buff, int *lines)
{
rb_thread_t *th = rb_thread_ptr(thread);
return thread_profile_frames(th->ec, start, limit, buff, lines);
}
static const rb_iseq_t *
frame2iseq(VALUE frame)
{