-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathGarbageCollectionsProfilerTest.cs
150 lines (125 loc) · 6.37 KB
/
GarbageCollectionsProfilerTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// <copyright file="GarbageCollectionsProfilerTest.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// 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 Xunit;
using Xunit.Abstractions;
namespace Datadog.Profiler.IntegrationTests.GarbageCollections
{
public class GarbageCollectionsProfilerTest
{
private const string ScenarioGenerics = "--scenario 12";
private const string ScenarioWithoutGC = "--scenario 6 --threads 1";
private const string GcRootFrame = "|lm: |ns: |ct: |cg: |fn:Garbage Collector |fg: |sg:";
private readonly ITestOutputHelper _output;
public GarbageCollectionsProfilerTest(ITestOutputHelper output)
{
_output = output;
}
[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0" })]
public void ShouldGetGarbageCollectionSamples(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioGenerics);
EnvironmentHelper.DisableDefaultProfilers(runner);
// enable GC profiler
runner.Environment.SetVariable(EnvironmentVariables.GarbageCollectionProfilerEnabled, "1");
// only garbage collection profiler enabled so should only see the 1 related value per sample + Generation label
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);
Assert.True(CheckSamplesAreGC(runner.Environment.PprofDir));
}
[TestAppFact("Samples.Computer01", new[] { "net6.0", "net7.0" })]
public void ShouldGetGarbageCollectionSamplesByDefault(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioGenerics);
// disable default profilers except GC
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ContentionProfilerEnabled, "0");
// only garbage collection profiler enabled so should only see the 1 related value per sample + Generation label
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);
Assert.True(CheckSamplesAreGC(runner.Environment.PprofDir));
}
[TestAppFact("Samples.Computer01", new[] { "net462" })]
public void ShouldGetGarbageCollectionSamplesViaEtw(string appName, string framework, string appAssembly)
{
var runner = new TestApplicationRunner(appName, framework, appAssembly, _output, commandLine: ScenarioWithoutGC);
// allow agent proxy to send the recorded events
runner.TestDurationInSeconds = 30;
// disable default profilers except GC
runner.Environment.SetVariable(EnvironmentVariables.WallTimeProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.CpuProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ExceptionProfilerEnabled, "0");
runner.Environment.SetVariable(EnvironmentVariables.ContentionProfilerEnabled, "0");
Guid guid = Guid.NewGuid();
runner.Environment.SetVariable(EnvironmentVariables.EtwReplayEndpoint, "\\\\.\\pipe\\DD_ETW_TEST_AGENT-" + guid);
using var agent = MockDatadogAgent.CreateHttpAgent(runner.XUnitLogger);
if (IntPtr.Size == 4)
{
// 32-bit
agent.StartEtwProxy(runner.XUnitLogger, "DD_ETW_TEST_AGENT-" + guid, "GarbageCollections\\3x3GCs-32.bevents");
}
else
{
// 64-bit
agent.StartEtwProxy(runner.XUnitLogger, "DD_ETW_TEST_AGENT-" + guid, "GarbageCollections\\3x3GCs-64.bevents");
}
int eventsCount = 0;
agent.EventsSent += (sender, e) => eventsCount = e.Value;
runner.Run(agent);
Assert.True(agent.NbCallsOnProfilingEndpoint > 0);
Assert.True(eventsCount > 0);
Assert.True(CheckSamplesAreGC(runner.Environment.PprofDir));
// TODO: it should be possible to ensure that 3 GCs per generations are seen in the .pprof files
}
private bool CheckSamplesAreGC(string directory)
{
var rootFrame = new StackFrame(GcRootFrame);
int profileCount = 0;
foreach (var profile in SamplesHelper.GetProfiles(directory))
{
int sampleCount = 0;
foreach (var sample in profile.Sample)
{
if (!sample.Labels(profile).Any(l => l.Name == "gc generation"))
{
return false;
}
// check fake call stack
var frames = sample.StackTrace(profile);
if (frames.FramesCount != 2)
{
return false;
}
if (!frames[0].Equals(rootFrame))
{
return false;
}
var generation = frames[1].Function;
if (!generation.StartsWith("gen"))
{
return false;
}
if (!(generation.EndsWith("0") || generation.EndsWith("1") || generation.EndsWith("2")))
{
return false;
}
sampleCount++;
}
if (sampleCount == 0)
{
return false;
}
profileCount++;
}
return (profileCount > 0);
}
}
}