Skip to content

Commit

Permalink
[Profiler] Add tag GC CPU samples (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
gleocadie authored Mar 7, 2025
1 parent 999a3aa commit cd6af0e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ std::vector<std::shared_ptr<IThreadInfo>> const& GCThreadsCpuProvider::GetThread
return _gcThreads;
}

Labels GCThreadsCpuProvider::GetLabels()
{
return Labels{Label{"gc_cpu_sample", "true"}};
}

std::vector<FrameInfoView> GCThreadsCpuProvider::GetFrames()
{
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GCThreadsCpuProvider : public NativeThreadsCpuProviderBase
private:
bool IsGcThread(std::shared_ptr<IThreadInfo> const& thread);
std::vector<std::shared_ptr<IThreadInfo>> const& GetThreads() override;
Labels GetLabels() override;
std::vector<FrameInfoView> GetFrames() override;

std::vector<std::shared_ptr<IThreadInfo>> _gcThreads;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ std::unique_ptr<SamplesEnumerator> NativeThreadsCpuProviderBase::GetSamples()
sample->AddFrame(frame);
}

for (auto const& label : GetLabels())
{
sample->AddLabel(Label{label.first,label.second});
}

enumerator->Set(sample);

return enumerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "IFrameStore.h"
#include "ISamplesProvider.h"
#include "IThreadInfo.h"
#include "Sample.h"

#include <vector>

Expand All @@ -26,6 +27,7 @@ class NativeThreadsCpuProviderBase : public ISamplesProvider
std::unique_ptr<SamplesEnumerator> GetSamples() override;
virtual std::vector<FrameInfoView> GetFrames() = 0;
virtual std::vector<std::shared_ptr<IThreadInfo>> const& GetThreads() = 0;
virtual Labels GetLabels() = 0;

CpuTimeProvider* _cpuTimeProvider;
std::chrono::milliseconds _previousTotalCpuTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
// </copyright>

using System;
using System.Linq;
using Datadog.Profiler.IntegrationTests.Helpers;
using FluentAssertions;
using Xunit;
Expand All @@ -19,7 +21,7 @@ public class GarbageCollectorCpuTimeTest
private static readonly StackFrame GcFrame = new("|lm:[native] GC |ns: |ct: |cg: |fn:Garbage Collector |fg: |sg:");
private static readonly StackFrame ClrFrame = new("|lm:[native] CLR |ns: |ct: |cg: |fn:.NET |fg: |sg:");

private readonly StackTrace gcStack = new(GcFrame, ClrFrame);
private static readonly StackTrace GcStack = new(GcFrame, ClrFrame);

private readonly ITestOutputHelper _output;

Expand Down Expand Up @@ -47,7 +49,7 @@ public void CheckCpuTimeForGcThreadsIsReported(string appName, string framework,

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should()
// match the GC stacktrace and check that the waltime value is 0 and the cpu value is not 0
.Contain(sample => sample.StackTrace.Equals(gcStack) && sample.Values[0] == 0 && sample.Values[1] != 0);
.Contain(sample => IsGcCpuSample(sample) && sample.Values[0] == 0 && sample.Values[1] != 0);
}

[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0" })]
Expand All @@ -68,7 +70,7 @@ public void CheckNoGcSampleIfCpuProfilingIsDisabled(string appName, string frame
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(gcStack));
SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(GcStack));
}

[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0" })]
Expand All @@ -87,7 +89,7 @@ public void CheckNoGcSampleIfNotGcServerSetToOne(string appName, string framewor
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(gcStack));
SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => IsGcCpuSample(sample));
}

[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0" })]
Expand All @@ -106,7 +108,7 @@ public void CheckNoGcSampleIfFeatureDeactivated(string appName, string framework
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(gcStack));
SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => IsGcCpuSample(sample));
}

[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0" })]
Expand All @@ -125,7 +127,7 @@ public void CheckNoGcSampleIfFeatureDeactivatedByDefault(string appName, string
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(gcStack));
SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(GcStack) && sample.Labels.Any(x => x.Name == "" && x.Value == "") );
}

[TestAppFact("Samples.Computer01", new[] { "netcoreapp3.1" })]
Expand All @@ -144,7 +146,7 @@ public void CheckFeatureIsDisabledIfNetCoreVersionIsLessThan_5(string appName, s
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(gcStack));
SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => IsGcCpuSample(sample));
}

[TestAppFact("Samples.Computer01", new[] { "net462" })]
Expand All @@ -163,7 +165,12 @@ public void CheckFeatureIsDisabledIfDotNetFramework(string appName, string frame
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);

SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => sample.StackTrace.Equals(gcStack));
SamplesHelper.GetSamples(runner.Environment.PprofDir).Should().NotContain(sample => IsGcCpuSample(sample));
}

private static bool IsGcCpuSample((StackTrace StackTrace, PprofHelper.Label[] Labels, long[] Values) sample)
{
return sample.StackTrace.Equals(GcStack) && sample.Labels.Any(label => label.Name == "gc_cpu_sample" && label.Value == "true");
}
}
}

0 comments on commit cd6af0e

Please sign in to comment.