Skip to content

Commit 086eb16

Browse files
committed
[Test][UserEvents] Add Multithreaded scenario
1 parent 79ab950 commit 086eb16

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Diagnostics.Tracing;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Tracing.UserEvents.Tests.Common;
11+
using Microsoft.Diagnostics.Tracing;
12+
13+
namespace Tracing.UserEvents.Tests.MultiThread
14+
{
15+
[EventSource(Name = "DemoMultiThread")]
16+
public sealed class MultiThreadEventSource : EventSource
17+
{
18+
public static readonly MultiThreadEventSource Log = new MultiThreadEventSource();
19+
20+
private MultiThreadEventSource() {}
21+
22+
[Event(1, Level = EventLevel.Informational)]
23+
public void WorkerEvent(int workerId)
24+
{
25+
WriteEvent(1, workerId);
26+
}
27+
}
28+
29+
public static class MultiThread
30+
{
31+
private const int WorkerCount = 4;
32+
33+
public static void MultiThreadTracee()
34+
{
35+
Task[] tasks = new Task[WorkerCount];
36+
37+
for (int i = 0; i < WorkerCount; i++)
38+
{
39+
int workerId = i;
40+
tasks[i] = Task.Run(() =>
41+
{
42+
MultiThreadEventSource.Log.WorkerEvent(workerId);
43+
});
44+
}
45+
46+
Task.WaitAll(tasks);
47+
}
48+
49+
private static readonly Func<EventPipeEventSource, bool> s_traceValidator = source =>
50+
{
51+
HashSet<int> seenWorkers = new HashSet<int>();
52+
53+
source.Dynamic.All += (TraceEvent e) =>
54+
{
55+
if (!string.Equals(e.ProviderName, "DemoMultiThread", StringComparison.Ordinal))
56+
{
57+
return;
58+
}
59+
60+
if (e.EventName is not "WorkerEvent")
61+
{
62+
return;
63+
}
64+
65+
int workerId = -1;
66+
try
67+
{
68+
workerId = (int)(e.PayloadByName("workerId") ?? -1);
69+
}
70+
catch (Exception ex)
71+
{
72+
Console.Error.WriteLine($"Exception while reading workerId payload: {ex}");
73+
}
74+
75+
if (workerId >= 0)
76+
{
77+
seenWorkers.Add(workerId);
78+
}
79+
};
80+
81+
source.Process();
82+
83+
for (int i = 0; i < WorkerCount; i++)
84+
{
85+
if (!seenWorkers.Contains(i))
86+
{
87+
Console.Error.WriteLine($"Did not observe event for worker {i}.");
88+
return false;
89+
}
90+
}
91+
92+
return true;
93+
};
94+
95+
public static int Main(string[] args)
96+
{
97+
if (args.Length > 0 && args[0].Equals("tracee", StringComparison.OrdinalIgnoreCase))
98+
{
99+
MultiThreadTracee();
100+
return 0;
101+
}
102+
103+
return UserEventsTestRunner.Run("multithread", typeof(MultiThread).Assembly.Location, s_traceValidator);
104+
}
105+
}
106+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<CLRTestTargetUnsupported Condition="'$(TargetOS)' != 'linux' or ('$(TargetArchitecture)' != 'x64' and '$(TargetArchitecture)' != 'arm64')">true</CLRTestTargetUnsupported>
4+
<RequiresProcessIsolation>true</RequiresProcessIsolation>
5+
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="$(MSBuildProjectName).cs" />
10+
<ProjectReference Include="../common/userevents_common.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<Content Include="multithread.script">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
<TargetPath>multithread.script</TargetPath>
17+
</Content>
18+
</ItemGroup>
19+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let DemoMultiThread_flags = new_dotnet_provider_flags();
2+
record_dotnet_provider("DemoMultiThread", 0x0, 5, DemoMultiThread_flags);

0 commit comments

Comments
 (0)