From 258e02b4044609dab206ab5e464513b8e04e8de3 Mon Sep 17 00:00:00 2001 From: Ivo Anjo Date: Wed, 17 Jul 2024 16:51:49 +0100 Subject: [PATCH] Minor: Simplify time retrieval during worker initialization By getting the timestamp before allocating any memory, we can safely rely on the exception-raising behavior of our helper, rather than needing to handle clearing the state if we get a failure mid-initialization. Having a timestamp that's a few ns before is not expected to impact the discrete dynamic sampler behavior. --- .../collectors_cpu_and_wall_time_worker.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c b/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c index 13532068b48..a610732665c 100644 --- a/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +++ b/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c @@ -316,6 +316,8 @@ static const rb_data_type_t cpu_and_wall_time_worker_typed_data = { }; static VALUE _native_new(VALUE klass) { + long now = monotonic_wall_time_now_ns(RAISE_ON_FAILURE); + struct cpu_and_wall_time_worker_state *state = ruby_xcalloc(1, sizeof(struct cpu_and_wall_time_worker_state)); // Note: Any exceptions raised from this note until the TypedData_Wrap_Struct call will lead to the state memory @@ -340,13 +342,6 @@ static VALUE _native_new(VALUE klass) { state->during_sample = false; reset_stats_not_thread_safe(state); - - long now = monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE); - if (now == 0) { - ruby_xfree(state); - rb_raise(rb_eRuntimeError, ERR_CLOCK_FAIL); - } - discrete_dynamic_sampler_init(&state->allocation_sampler, "allocation", now); // Note: As of this writing, no new Ruby objects get created and stored in the state. If that ever changes, remember