Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] Use timestamp_delta to weight samples #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 20 additions & 20 deletions ext/stackprof/stackprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ static const char *fake_frame_cstrs[] = {
#endif

typedef struct {
size_t total_samples;
size_t caller_samples;
uint64_t total_usec;
uint64_t caller_usec;
size_t seen_at_sample_number;
st_table *edges;
st_table *lines;
Expand All @@ -88,6 +88,7 @@ static struct {

VALUE mode;
VALUE interval;
uint64_t interval_usec;
VALUE out;
VALUE metadata;
int ignore_gc;
Expand Down Expand Up @@ -199,6 +200,12 @@ stackprof_start(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eArgError, "unknown profiler mode");
}

if (RTEST(interval)) {
_stackprof.interval_usec = NUM2ULONG(interval);
} else {
_stackprof.interval_usec = 1;
}

_stackprof.running = 1;
_stackprof.raw = raw;
_stackprof.aggregate = aggregate;
Expand All @@ -208,9 +215,7 @@ stackprof_start(int argc, VALUE *argv, VALUE self)
_stackprof.metadata = metadata;
_stackprof.out = out;

if (raw) {
capture_timestamp(&_stackprof.last_sample_at);
}
capture_timestamp(&_stackprof.last_sample_at);

return Qtrue;
}
Expand Down Expand Up @@ -304,8 +309,8 @@ frame_i(st_data_t key, st_data_t val, st_data_t arg)
rb_hash_aset(details, sym_line, line);
}

rb_hash_aset(details, sym_total_samples, SIZET2NUM(frame_data->total_samples));
rb_hash_aset(details, sym_samples, SIZET2NUM(frame_data->caller_samples));
rb_hash_aset(details, sym_total_samples, ULL2NUM(frame_data->total_usec / _stackprof.interval_usec));
rb_hash_aset(details, sym_samples, ULL2NUM(frame_data->caller_usec / _stackprof.interval_usec));

if (frame_data->edges) {
edges = rb_hash_new();
Expand Down Expand Up @@ -547,12 +552,12 @@ stackprof_record_sample_for_stack(int num, int64_t timestamp_delta)
frame_data_t *frame_data = sample_for(frame);

if (frame_data->seen_at_sample_number != _stackprof.overall_samples) {
frame_data->total_samples++;
frame_data->total_usec += timestamp_delta;
}
frame_data->seen_at_sample_number = _stackprof.overall_samples;

if (i == 0) {
frame_data->caller_samples++;
frame_data->caller_usec += timestamp_delta;
} else if (_stackprof.aggregate) {
if (!frame_data->edges)
frame_data->edges = st_init_numtable();
Expand All @@ -570,21 +575,17 @@ stackprof_record_sample_for_stack(int num, int64_t timestamp_delta)
prev_frame = frame;
}

if (_stackprof.raw) {
capture_timestamp(&_stackprof.last_sample_at);
}
capture_timestamp(&_stackprof.last_sample_at);
}

void
stackprof_record_sample()
{
int64_t timestamp_delta = 0;
int num;
if (_stackprof.raw) {
struct timestamp_t t;
capture_timestamp(&t);
timestamp_delta = delta_usec(&t, &_stackprof.last_sample_at);
}
struct timestamp_t t;
capture_timestamp(&t);
timestamp_delta = delta_usec(&t, &_stackprof.last_sample_at);
num = rb_profile_frames(0, sizeof(_stackprof.frames_buffer) / sizeof(VALUE), _stackprof.frames_buffer, _stackprof.lines_buffer);
stackprof_record_sample_for_stack(num, timestamp_delta);
}
Expand All @@ -594,10 +595,9 @@ stackprof_record_gc_samples()
{
int64_t delta_to_first_unrecorded_gc_sample = 0;
size_t i;
struct timestamp_t t;
capture_timestamp(&t);
if (_stackprof.raw) {
struct timestamp_t t;
capture_timestamp(&t);

// We don't know when the GC samples were actually marked, so let's
// assume that they were marked at a perfectly regular interval.
delta_to_first_unrecorded_gc_sample = delta_usec(&t, &_stackprof.last_sample_at) - (_stackprof.unrecorded_gc_samples - 1) * NUM2LONG(_stackprof.interval);
Expand Down