Skip to content

Commit

Permalink
Fix GC profiling timing
Browse files Browse the repository at this point in the history
We were recording GC profile timestamps inside the VM postponed job that
flushes temporary information.  The postponed job could be run much
later than the sample was actually taken, so this commit records a
timestamp in the signal handler when there is a GC event.

It only records one time stamp, so if there are multiple GC events in a
row, we have to assume that the total duration spent in GC starts from
the first time stamp and extends until the timestamp of the next non-GC
sample.  In other words, we don't record a correct timestamp for each GC
sample, only the first one.
  • Loading branch information
tenderlove committed Jul 11, 2023
1 parent 51b71bf commit 329e57b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ext/stackprof/stackprof.c
Expand Up @@ -120,6 +120,8 @@ static struct {
size_t unrecorded_gc_sweeping_samples;
st_table *frames;

timestamp_t gc_start_timestamp;

VALUE fake_frame_names[TOTAL_FAKE_FRAMES];
VALUE empty_string;

Expand Down Expand Up @@ -646,15 +648,15 @@ stackprof_buffer_sample(void)
_stackprof.buffer_time.delta_usec = timestamp_delta;
}

// Postponed job
void
stackprof_record_gc_samples(void)
{
int64_t delta_to_first_unrecorded_gc_sample = 0;
uint64_t start_timestamp = 0;
size_t i;
if (_stackprof.raw) {
struct timestamp_t t;
capture_timestamp(&t);
struct timestamp_t t = _stackprof.gc_start_timestamp;
start_timestamp = timestamp_usec(&t);

// We don't know when the GC samples were actually marked, so let's
Expand Down Expand Up @@ -776,6 +778,10 @@ stackprof_signal_handler(int sig, siginfo_t *sinfo, void *ucontext)
} else if (mode == sym_sweeping) {
_stackprof.unrecorded_gc_sweeping_samples++;
}
if(!_stackprof.unrecorded_gc_samples) {
// record start
capture_timestamp(&_stackprof.gc_start_timestamp);
}
_stackprof.unrecorded_gc_samples++;
rb_postponed_job_register_one(0, stackprof_job_record_gc, (void*)0);
} else {
Expand Down

0 comments on commit 329e57b

Please sign in to comment.