Skip to content

Commit

Permalink
Merge pull request #2618 from DataDog/ivoanjo/prof-7192-allocation-pr…
Browse files Browse the repository at this point in the history
…ofiling-1

[PROF-7192] Minor: Extend stack collector to be able to record the alloc-samples metric
  • Loading branch information
ivoanjo authored Feb 15, 2023
2 parents cf7cadf + 002d01a commit 849af21
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions ext/ddtrace_profiling_native_extension/collectors_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static VALUE _native_sample(
ENFORCE_TYPE(labels_array, T_ARRAY);
ENFORCE_TYPE(numeric_labels_array, T_ARRAY);

if (RHASH_SIZE(metric_values_hash) != ENABLED_VALUE_TYPES_COUNT) {
if (RHASH_SIZE(metric_values_hash) > ENABLED_VALUE_TYPES_COUNT || RHASH_SIZE(metric_values_hash) == 0) {
rb_raise(
rb_eArgError,
"Mismatched values for metrics; expected %lu values and got %lu instead",
Expand All @@ -92,8 +92,8 @@ static VALUE _native_sample(
);
}

int64_t metric_values[ENABLED_VALUE_TYPES_COUNT];
for (unsigned int i = 0; i < ENABLED_VALUE_TYPES_COUNT; i++) {
int64_t metric_values[ENABLED_VALUE_TYPES_COUNT] = {0};
for (unsigned int i = 0; i < RHASH_SIZE(metric_values_hash); i++) {
VALUE metric_value = rb_hash_fetch(metric_values_hash, rb_str_new_cstr(enabled_value_types[i].type_.ptr));
metric_values[i] = NUM2LONG(metric_value);
}
Expand Down
4 changes: 3 additions & 1 deletion ext/ddtrace_profiling_native_extension/stack_recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ static const ddog_prof_ValueType enabled_value_types[] = {
#define CPU_SAMPLES_VALUE_POS 1
CPU_SAMPLES_VALUE,
#define WALL_TIME_VALUE_POS 2
WALL_TIME_VALUE
WALL_TIME_VALUE,
#define ALLOC_SAMPLES_VALUE_POS 3
ALLOC_SAMPLES_VALUE
};

#define ENABLED_VALUE_TYPES_COUNT (sizeof(enabled_value_types) / sizeof(ddog_prof_ValueType))
Expand Down
6 changes: 4 additions & 2 deletions spec/datadog/profiling/stack_recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def slot_two_mutex_locked?
'cpu-time' => 'nanoseconds',
'cpu-samples' => 'count',
'wall-time' => 'nanoseconds',
'alloc-samples' => 'count',
)
end

Expand All @@ -139,7 +140,7 @@ def sample_types_from(decoded_profile)
end

context 'when profile has a sample' do
let(:metric_values) { { 'cpu-time' => 123, 'cpu-samples' => 456, 'wall-time' => 789 } }
let(:metric_values) { { 'cpu-time' => 123, 'cpu-samples' => 456, 'wall-time' => 789, 'alloc-samples' => 4242 } }
let(:labels) { { 'label_a' => 'value_a', 'label_b' => 'value_b' }.to_a }

let(:samples) { samples_from_pprof(encoded_pprof) }
Expand All @@ -151,7 +152,8 @@ def sample_types_from(decoded_profile)
end

it 'encodes the sample with the metrics provided' do
expect(samples.first.values).to eq(:'cpu-time' => 123, :'cpu-samples' => 456, :'wall-time' => 789)
expect(samples.first.values)
.to eq(:'cpu-time' => 123, :'cpu-samples' => 456, :'wall-time' => 789, :'alloc-samples' => 4242)
end

it 'encodes the sample with the labels provided' do
Expand Down

0 comments on commit 849af21

Please sign in to comment.