Skip to content

Commit afc8cc0

Browse files
authored
[Profiler] Allow lock contention provider to upscale by duration instead of by count (#4263)
* Allow lock contention provider to upscale by value instead of by count * Raw lock contention count/duration labels are sent as numeric label for timeline
1 parent dcbcd3e commit afc8cc0

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native/ContentionProvider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void ContentionProvider::OnContention(double contentionDurationNs)
8181
{
8282
std::lock_guard lock(_contentionsLock);
8383

84-
if (!_sampler.Sample(bucket))
84+
if (!_sampler.Sample(bucket, static_cast<uint64_t>(contentionDurationNs)))
8585
{
8686
return;
8787
}

profiler/src/ProfilerEngine/Datadog.Profiler.Native/GroupSampler.h

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct UpscaleGroupInfo
1616
TGroup Group;
1717
uint64_t RealCount;
1818
uint64_t SampledCount;
19+
uint64_t RealValue;
20+
uint64_t SampledValue;
1921
};
2022

2123
// Template class that support "sampling by group".
@@ -34,24 +36,27 @@ class GroupSampler : public GenericSampler
3436
public:
3537
struct GroupInfo
3638
{
37-
uint64_t Real;
38-
uint64_t Sampled;
39+
uint64_t RealCount;
40+
uint64_t SampledCount;
41+
uint64_t RealValue;
42+
uint64_t SampledValue;
3943
};
4044

4145
public:
42-
bool Sample(TGroup group)
46+
bool Sample(TGroup group, uint64_t value = 0)
4347
{
4448
std::unique_lock lock(_groupsMutex);
4549

46-
// increment the real count for the given group
50+
// increment the real count and value for the given group
4751
GroupInfo* pInfo;
48-
AddInGroup(group, pInfo);
52+
AddInGroup(group, pInfo, value);
4953

5054
auto [it, inserted] = _knownGroups.insert(std::move(group));
5155
if (inserted && _keepAtLeastOne)
5256
{
53-
// increment the sampled count for the given group
54-
pInfo->Sampled++;
57+
// increment the sampled count and value for the given group
58+
pInfo->SampledCount++;
59+
pInfo->SampledValue += value;
5560

5661
// This is the first time we see this group in this time window,
5762
// so force the sampling decision
@@ -61,29 +66,32 @@ class GroupSampler : public GenericSampler
6166
auto sampled = _sampler.Sample();
6267
if (sampled)
6368
{
64-
// increment the sampled count for the given group
65-
pInfo->Sampled++;
69+
// increment the sampled count and value for the given group
70+
pInfo->SampledCount++;
71+
pInfo->SampledValue += value;
6672
}
6773

6874
return sampled;
6975
}
7076

7177
// MUST be called under the lock
72-
void AddInGroup(TGroup group, GroupInfo*& groupInfo)
78+
void AddInGroup(TGroup group, GroupInfo*& groupInfo, uint64_t value = 0)
7379
{
7480
auto info = _groups.find(group);
7581
if (info != _groups.end())
7682
{
7783
groupInfo = &(info->second);
78-
info->second.Real++;
79-
84+
info->second.RealCount++;
85+
info->second.RealValue += value;
8086
return;
8187
}
8288

8389
// need to add the info of this new group
8490
GroupInfo gi;
85-
gi.Real = 1;
86-
gi.Sampled = 0;
91+
gi.RealCount = 1;
92+
gi.SampledCount = 0;
93+
gi.RealValue = value;
94+
gi.SampledValue = 0;
8795
auto slot = _groups.insert_or_assign(group, gi);
8896
groupInfo = &((*slot.first).second);
8997
}
@@ -95,17 +103,19 @@ class GroupSampler : public GenericSampler
95103

96104
upscaleGroups.reserve(_groups.size());
97105

98-
for (auto& [name, counts] : _groups)
106+
for (auto& [name, info] : _groups)
99107
{
100-
auto sampledCount = counts.Sampled;
108+
auto sampledCount = info.SampledCount;
101109
if (sampledCount > 0)
102110
{
103-
upscaleGroups.push_back(UpscaleGroupInfo<TGroup>{name, counts.Real, sampledCount});
111+
upscaleGroups.push_back(UpscaleGroupInfo<TGroup>{name, info.RealCount, sampledCount, info.RealValue, info.SampledValue});
104112
}
105113

106114
// reset groups count
107-
counts.Real = 0;
108-
counts.Sampled = 0;
115+
info.RealCount = 0;
116+
info.SampledCount = 0;
117+
info.RealValue = 0;
118+
info.SampledValue = 0;
109119
}
110120

111121
return upscaleGroups;

profiler/src/ProfilerEngine/Datadog.Profiler.Native/LibddprofExporter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,20 @@ void LibddprofExporter::AddUpscalingRules(ddog_prof_Profile* profile, std::vecto
480480
ddog_CharSlice labelName = FfiHelper::StringToCharSlice(upscalingInfo.LabelName);
481481
ddog_CharSlice groupName = FfiHelper::StringToCharSlice(group.Group);
482482

483-
auto upscalingRuleAdd = ddog_prof_Profile_add_upscaling_rule_proportional(profile, offsets_slice, labelName, groupName, group.SampledCount, group.RealCount);
483+
// upscaling could be based on count (exceptions) or value (lock contention)
484+
uint64_t sampled = group.SampledCount;
485+
if (group.SampledValue != 0)
486+
{
487+
sampled = group.SampledValue;
488+
}
489+
490+
uint64_t real = group.RealCount;
491+
if (group.RealValue != 0)
492+
{
493+
real = group.RealValue;
494+
}
495+
496+
auto upscalingRuleAdd = ddog_prof_Profile_add_upscaling_rule_proportional(profile, offsets_slice, labelName, groupName, sampled, real);
484497
if (upscalingRuleAdd.tag == DDOG_PROF_PROFILE_UPSCALING_RULE_ADD_RESULT_ERR)
485498
{
486499
auto errorMessage = ddog_Error_message(&upscalingRuleAdd.err);

profiler/src/ProfilerEngine/Datadog.Profiler.Native/RawContentionSample.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class RawContentionSample : public RawSample
4242

4343
sample->AddLabel(Label{BucketLabelName, std::move(Bucket)});
4444
sample->AddValue(1, contentionCountIndex);
45-
sample->AddLabel(Label{RawCountLabelName, std::to_string(1)});
46-
sample->AddLabel(Label{RawDurationLabelName, std::to_string(static_cast<uint64_t>(ContentionDuration))});
45+
sample->AddNumericLabel(NumericLabel{RawCountLabelName, 1});
46+
sample->AddNumericLabel(NumericLabel{RawDurationLabelName, static_cast<uint64_t>(ContentionDuration)});
4747
sample->AddValue(static_cast<std::int64_t>(ContentionDuration), contentionDurationIndex);
4848
}
4949

0 commit comments

Comments
 (0)