Skip to content

Commit 9977407

Browse files
committed
Continue introducing ProfileAdapter
1 parent 6ede5b7 commit 9977407

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

ext/datadog_profiling_native_extension/stack_recorder.c

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ typedef struct {
180180
} stats_slot;
181181

182182
typedef struct {
183-
ddog_prof_ProfileHandle profile;
183+
ddog_prof_ProfileAdapter profile;
184184
stats_slot stats;
185185
ddog_Timespec start_timestamp;
186186
} profile_slot;
@@ -196,6 +196,9 @@ typedef struct {
196196
pthread_mutex_t mutex_slot_two;
197197
profile_slot profile_slot_two;
198198

199+
ddog_prof_ProfilesDictionaryHandle dictionary;
200+
ddog_prof_ScratchPadHandle scratchpad;
201+
199202
// ddog_prof_ManagedStringStorage string_storage;
200203
ddog_prof_ManagedStringId label_key_allocation_class;
201204
ddog_prof_ManagedStringId label_key_gc_gen_age;
@@ -273,6 +276,15 @@ static VALUE _native_recorder_after_gc_step(DDTRACE_UNUSED VALUE _self, VALUE re
273276
static VALUE _native_benchmark_intern(DDTRACE_UNUSED VALUE _self, VALUE recorder_instance, VALUE string, VALUE times, VALUE use_all);
274277
static VALUE _native_test_managed_string_storage_produces_valid_profiles(DDTRACE_UNUSED VALUE _self);
275278

279+
ddog_prof_StringId insert_into_profiles_dictionary(stack_recorder_state *state, ddog_CharSlice byte_slice) {
280+
ddog_prof_StringId id;
281+
ddog_prof_Status status = ddog_prof_ProfilesDictionary_insert_str(&id, state->dictionary, byte_slice, DDOG_PROF_UTF8_OPTION_ASSUME);
282+
if (is_ddog_error(status)) {
283+
rb_raise(rb_eRuntimeError, "Failed to run ddog_prof_ProfilesDictionary_insert_str: %"PRIsVALUE, get_status_details_and_drop(&status));
284+
};
285+
return id;
286+
}
287+
276288
void stack_recorder_init(VALUE profiling_module) {
277289
VALUE stack_recorder_class = rb_define_class_under(profiling_module, "StackRecorder", rb_cObject);
278290
// Hosts methods used for testing the native code using RSpec
@@ -357,6 +369,18 @@ static VALUE _native_new(VALUE klass) {
357369
// state->label_key_allocation_class = intern_or_raise(state->string_storage, DDOG_CHARSLICE_C("allocation class"));
358370
// state->label_key_gc_gen_age = intern_or_raise(state->string_storage, DDOG_CHARSLICE_C("gc gen age"));
359371

372+
ddog_prof_ProfilesDictionaryHandle dictionary;
373+
ddog_prof_Status dictionary_status = ddog_prof_ProfilesDictionary_new(&dictionary);
374+
if (is_ddog_error(dictionary_status)) {
375+
rb_raise(rb_eRuntimeError, "Failed to initialize profiles dictionary: %"PRIsVALUE, get_status_details_and_drop(&dictionary_status));
376+
}
377+
378+
ddog_prof_ScratchPadHandle scratchpad;
379+
ddog_prof_Status scratchpad_status = ddog_prof_ScratchPad_new(&scratchpad);
380+
if (is_ddog_error(scratchpad_status)) {
381+
rb_raise(rb_eRuntimeError, "Failed to initialize scratch pad: %"PRIsVALUE, get_status_details_and_drop(&scratchpad_status));
382+
}
383+
360384
initialize_profiles(state, sample_types);
361385

362386
// NOTE: We initialize this because we want a new recorder to be operational even before #initialize runs and our
@@ -380,17 +404,28 @@ static void initialize_slot_concurrency_control(stack_recorder_state *state) {
380404
static void initialize_profiles(stack_recorder_state *state, fixme_ddog_prof_Slice_ValueType sample_types) {
381405
ddog_Timespec start_timestamp = system_epoch_now_timespec();
382406

383-
ddog_prof_ProfileHandle slot_one_profile_handle;
384-
ddog_prof_Status slot_one_profile_status = ddog_prof_Profile_new(&slot_one_profile_handle);
407+
ddog_prof_ValueType converted_types[sample_types.len];
408+
int64_t groupings[sample_types.len];
409+
410+
for (int i = 0; i < sample_types.len; i++) {
411+
converted_types[i] = (ddog_prof_ValueType) {
412+
.type_id = insert_into_profiles_dictionary(state, sample_types.ptr[i].type_),
413+
.unit_id = insert_into_profiles_dictionary(state, sample_types.ptr[i].unit),
414+
};
415+
groupings[i] = i;
416+
}
417+
418+
ddog_prof_ProfileAdapter slot_one_profile_handle;
419+
ddog_prof_Status slot_one_profile_status = ddog_prof_ProfileAdapter_new(&slot_one_profile_handle, state->dictionary, state->scratchpad, (ddog_prof_Slice_ValueType) { .ptr = converted_types, .len = sample_types.len }, (ddog_Slice_I64) { .ptr = groupings, .len = sample_types.len });
385420

386421
if (is_ddog_error(slot_one_profile_status)) {
387422
rb_raise(rb_eRuntimeError, "Failed to initialize slot one profile: %"PRIsVALUE, get_status_details_and_drop(&slot_one_profile_status));
388423
}
389424

390425
state->profile_slot_one = (profile_slot) { .profile = slot_one_profile_handle, .start_timestamp = start_timestamp };
391426

392-
ddog_prof_ProfileHandle slot_two_profile_handle;
393-
ddog_prof_Status slot_two_profile_status = ddog_prof_Profile_new(&slot_two_profile_handle);
427+
ddog_prof_ProfileAdapter slot_two_profile_handle;
428+
ddog_prof_Status slot_two_profile_status = ddog_prof_ProfileAdapter_new(&slot_two_profile_handle, state->dictionary, state->scratchpad, (ddog_prof_Slice_ValueType) { .ptr = converted_types, .len = sample_types.len }, (ddog_Slice_I64) { .ptr = groupings, .len = sample_types.len });
394429

395430
if (is_ddog_error(slot_two_profile_status)) {
396431
// Note: No need to take any special care of slot one, it'll get cleaned up by stack_recorder_typed_data_free
@@ -404,10 +439,10 @@ static void stack_recorder_typed_data_free(void *state_ptr) {
404439
stack_recorder_state *state = (stack_recorder_state *) state_ptr;
405440

406441
pthread_mutex_destroy(&state->mutex_slot_one);
407-
ddog_prof_Profile_drop(&state->profile_slot_one.profile);
442+
ddog_prof_ProfileAdapter_drop(&state->profile_slot_one.profile);
408443

409444
pthread_mutex_destroy(&state->mutex_slot_two);
410-
ddog_prof_Profile_drop(&state->profile_slot_two.profile);
445+
ddog_prof_ProfileAdapter_drop(&state->profile_slot_two.profile);
411446

412447
heap_recorder_free(state->heap_recorder);
413448

@@ -519,8 +554,8 @@ static VALUE _native_initialize(int argc, VALUE *argv, DDTRACE_UNUSED VALUE _sel
519554
state->position_for[TIMELINE_VALUE_ID] = next_disabled_pos++;
520555
}
521556

522-
ddog_prof_Profile_drop(&state->profile_slot_one.profile);
523-
ddog_prof_Profile_drop(&state->profile_slot_two.profile);
557+
ddog_prof_ProfileAdapter_drop(&state->profile_slot_one.profile);
558+
ddog_prof_ProfileAdapter_drop(&state->profile_slot_two.profile);
524559

525560
fixme_ddog_prof_Slice_ValueType sample_types = {.ptr = enabled_value_types, .len = state->enabled_values_count};
526561
initialize_profiles(state, sample_types);
@@ -793,7 +828,17 @@ static void *call_serialize_without_gvl(void *call_args) {
793828

794829
// FIXME TODO: Do we need to reset something??? Not sure the previous comment is still true
795830

796-
args->serialize_status = ddog_prof_ProfileAdapter_build_compressed(&args->encoded_profile, FIXME, &args->slot->start_timestamp, &args->finish_timestamp);
831+
args->serialize_status = ddog_prof_ProfileAdapter_build_compressed(
832+
&args->encoded_profile,
833+
&args->slot->profile,
834+
&args->slot->start_timestamp,
835+
&args->finish_timestamp
836+
);
837+
838+
if (!is_ddog_error(args->serialize_status)) {
839+
ddog_prof_ProfileAdapter_drop(&args->slot->profile);
840+
args->serialize_status = ddog_prof_ProfileAdapter_new(&args->slot->profile);
841+
}
797842

798843
// args->advance_gen_result = ddog_prof_ManagedStringStorage_advance_gen(args->state->string_storage);
799844
args->serialize_ran = true;
@@ -1143,7 +1188,7 @@ static VALUE _native_test_managed_string_storage_produces_valid_profiles(DDTRACE
11431188
11441189
VALUE encoded_pprof_2 = from_ddog_prof_EncodedProfile(serialize_result.ok);
11451190
1146-
ddog_prof_Profile_drop(&profile.ok);
1191+
ddog_prof_ProfileAdapter_drop(&profile.ok);
11471192
ddog_prof_ManagedStringStorage_drop(string_storage.ok);
11481193
11491194
return rb_ary_new_from_args(2, encoded_pprof_1, encoded_pprof_2);

0 commit comments

Comments
 (0)