forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleruntimeeventvalidation.cs
180 lines (157 loc) · 7.79 KB
/
simpleruntimeeventvalidation.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Diagnostics.Tracing;
using Tracing.Tests.Common;
using Microsoft.Diagnostics.NETCore.Client;
using Xunit;
namespace Tracing.Tests.SimpleRuntimeEventValidation
{
public class RuntimeEventValidation
{
[Fact]
public static int TestEntryPoint()
{
// This test validates GC and Exception events in the runtime
var ret = IpcTraceTest.RunAndValidateEventCounts(
// Validation is done with _DoesTraceContainEvents
new Dictionary<string, ExpectedEventCount>(){{ "Microsoft-Windows-DotNETRuntime", -1 }},
_eventGeneratingActionForGC,
// GCKeyword (0x1): 0b1, GCAllocationTick requries Verbose level
new List<EventPipeProvider>(){new EventPipeProvider("Microsoft-Windows-DotNETRuntime", EventLevel.Verbose, 0b1)},
1024, _DoesTraceContainGCEvents, enableRundownProvider:false);
// Run the 2nd test scenario only if the first one passes
if(ret== 100)
{
ret = IpcTraceTest.RunAndValidateEventCounts(
new Dictionary<string, ExpectedEventCount>(){{ "Microsoft-DotNETCore-EventPipe", 1 }},
_eventGeneratingActionForExceptions,
// ExceptionKeyword (0x8000): 0b1000_0000_0000_0000
new List<EventPipeProvider>(){new EventPipeProvider("Microsoft-Windows-DotNETRuntime", EventLevel.Warning, 0b1000_0000_0000_0000)},
1024, _DoesTraceContainExceptionEvents, enableRundownProvider:false);
if(ret == 100)
{
ret = IpcTraceTest.RunAndValidateEventCounts(
new Dictionary<string, ExpectedEventCount>(){{ "Microsoft-Windows-DotNETRuntime", -1}},
_eventGeneratingActionForFinalizers,
new List<EventPipeProvider>(){new EventPipeProvider("Microsoft-Windows-DotNETRuntime", EventLevel.Informational, 0b1)},
1024, _DoesTraceContainFinalizerEvents, enableRundownProvider:false);
}
}
if (ret < 0)
return ret;
else
return 100;
}
private static Action _eventGeneratingActionForGC = () =>
{
for (int i = 0; i < 50; i++)
{
if (i % 10 == 0)
Logger.logger.Log($"Called GC.Collect() {i} times...");
RuntimeEventValidation eventValidation = new RuntimeEventValidation();
eventValidation = null;
GC.Collect();
// Explicitly targeting AllocationTick event
GC.KeepAlive(new int[1000]);
}
};
private static Action _eventGeneratingActionForExceptions = () =>
{
for (int i = 0; i < 10; i++)
{
if (i % 5 == 0)
Logger.logger.Log($"Thrown an exception {i} times...");
try
{
throw new ArgumentNullException("Throw ArgumentNullException");
}
catch (Exception e)
{
//Do nothing
}
}
};
private static Action _eventGeneratingActionForFinalizers = () =>
{
for (int i = 0; i < 50; i++)
{
if (i % 10 == 0)
Logger.logger.Log($"Called GC.WaitForPendingFinalizers() {i} times...");
GC.WaitForPendingFinalizers();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
};
private static Func<EventPipeEventSource, Func<int>> _DoesTraceContainGCEvents = (source) =>
{
int GCStartEvents = 0;
int GCEndEvents = 0;
source.Clr.GCStart += (eventData) => GCStartEvents += 1;
source.Clr.GCStop += (eventData) => GCEndEvents += 1;
int GCRestartEEStartEvents = 0;
int GCRestartEEStopEvents = 0;
source.Clr.GCRestartEEStart += (eventData) => GCRestartEEStartEvents += 1;
source.Clr.GCRestartEEStop += (eventData) => GCRestartEEStopEvents += 1;
int GCSuspendEEEvents = 0;
int GCSuspendEEEndEvents = 0;
source.Clr.GCSuspendEEStart += (eventData) => GCSuspendEEEvents += 1;
source.Clr.GCSuspendEEStop += (eventData) => GCSuspendEEEndEvents += 1;
int GCAllocationTickEvents = 0;
source.Clr.GCAllocationTick += (eventData) => GCAllocationTickEvents += 1;
return () => {
Logger.logger.Log("Event counts validation");
Logger.logger.Log("GCStartEvents: " + GCStartEvents);
Logger.logger.Log("GCEndEvents: " + GCEndEvents);
bool GCStartStopResult = GCStartEvents >= 50 && GCEndEvents >= 50 && Math.Abs(GCStartEvents - GCEndEvents) <=2;
Logger.logger.Log("GCStartStopResult check: " + GCStartStopResult);
Logger.logger.Log("GCRestartEEStartEvents: " + GCRestartEEStartEvents);
Logger.logger.Log("GCRestartEEStopEvents: " + GCRestartEEStopEvents);
bool GCRestartEEStartStopResult = GCRestartEEStartEvents >= 50 && GCRestartEEStopEvents >= 50;
Logger.logger.Log("GCRestartEEStartStopResult check: " + GCRestartEEStartStopResult);
Logger.logger.Log("GCSuspendEEEvents: " + GCSuspendEEEvents);
Logger.logger.Log("GCSuspendEEEndEvents: " + GCSuspendEEEndEvents);
bool GCSuspendEEStartStopResult = GCSuspendEEEvents >= 50 && GCSuspendEEEndEvents >= 50;
Logger.logger.Log("GCSuspendEEStartStopResult check: " + GCSuspendEEStartStopResult);
Logger.logger.Log("GCAllocationTickEvents: " + GCAllocationTickEvents);
bool GCAllocationTickResult = GCAllocationTickEvents > 0;
return GCStartStopResult && GCRestartEEStartStopResult && GCSuspendEEStartStopResult && GCAllocationTickResult ? 100 : -1;
};
};
private static Func<EventPipeEventSource, Func<int>> _DoesTraceContainExceptionEvents = (source) =>
{
int ExStartEvents = 0;
source.Clr.ExceptionStart += (eventData) =>
{
if(eventData.ToString().IndexOf("System.ArgumentNullException")>=0)
ExStartEvents += 1;
};
return () => {
Logger.logger.Log("Exception Event counts validation");
Logger.logger.Log("ExStartEvents: " + ExStartEvents);
bool ExStartResult = ExStartEvents >= 10;
return ExStartResult ? 100 : -1;
};
};
private static Func<EventPipeEventSource, Func<int>> _DoesTraceContainFinalizerEvents = (source) =>
{
int GCFinalizersEndEvents = 0;
source.Clr.GCFinalizersStop += (eventData) => GCFinalizersEndEvents += 1;
int GCFinalizersStartEvents = 0;
source.Clr.GCFinalizersStart += (eventData) => GCFinalizersStartEvents += 1;
return () => {
Logger.logger.Log("Event counts validation");
Logger.logger.Log("GCFinalizersEndEvents: " + GCFinalizersEndEvents);
Logger.logger.Log("GCFinalizersStartEvents: " + GCFinalizersStartEvents);
return GCFinalizersEndEvents >= 50 && GCFinalizersStartEvents >= 50 ? 100 : -1;
};
};
}
}