Skip to content

add start_time_nsecs to profile info #229

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/stackprof/stackprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static struct {
size_t raw_samples_capa;
size_t raw_sample_index;

struct timespec start_time;
struct timestamp_t last_sample_at;
sample_time_t *raw_sample_times;
size_t raw_sample_times_len;
Expand Down Expand Up @@ -140,6 +141,7 @@ static VALUE sym_aggregate, sym_raw_sample_timestamps, sym_raw_timestamp_deltas,
static VALUE sym_gc_samples, objtracer;
static VALUE gc_hook;
static VALUE rb_mStackProf;
static VALUE sym_start_time_nsecs;

static void stackprof_newobj_handler(VALUE, void*);
static void stackprof_signal_handler(int sig, siginfo_t* sinfo, void* ucontext);
Expand Down Expand Up @@ -217,6 +219,8 @@ stackprof_start(int argc, VALUE *argv, VALUE self)
rb_raise(rb_eArgError, "unknown profiler mode");
}

clock_gettime(CLOCK_REALTIME, &_stackprof.start_time);

_stackprof.running = 1;
_stackprof.raw = raw;
_stackprof.aggregate = aggregate;
Expand Down Expand Up @@ -357,6 +361,7 @@ stackprof_results(int argc, VALUE *argv, VALUE self)
results = rb_hash_new();
rb_hash_aset(results, sym_version, DBL2NUM(1.2));
rb_hash_aset(results, sym_mode, _stackprof.mode);
rb_hash_aset(results, sym_start_time_nsecs, LL2NUM(_stackprof.start_time.tv_sec * NANOSECONDS_IN_SECOND + _stackprof.start_time.tv_nsec));
rb_hash_aset(results, sym_interval, _stackprof.interval);
rb_hash_aset(results, sym_samples, SIZET2NUM(_stackprof.overall_samples));
rb_hash_aset(results, sym_gc_samples, SIZET2NUM(_stackprof.during_gc));
Expand Down Expand Up @@ -946,6 +951,7 @@ Init_stackprof(void)
S(state);
S(marking);
S(sweeping);
S(start_time_nsecs);
#undef S

/* Need to run this to warm the symbol table before we call this during GC */
Expand Down
8 changes: 8 additions & 0 deletions test/test_stackprof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ def test_min_max_interval
end
end

def test_start_time
before_run_realtime = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)

profile = StackProf.run{}
assert profile[:start_time_nsecs] != nil
assert_operator profile[:start_time_nsecs], :>, before_run_realtime
end

def math
250_000.times do
2 ** 10
Expand Down