Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<!-- Not auto-updated. -->
<MicrosoftDiaSymReaderVersion>2.0.0</MicrosoftDiaSymReaderVersion>
<MicrosoftDiaSymReaderNativeVersion>17.10.0-beta1.24272.1</MicrosoftDiaSymReaderNativeVersion>
<TraceEventVersion>3.1.16</TraceEventVersion>
<TraceEventVersion>3.1.28</TraceEventVersion>
<NETStandardLibraryRefVersion>2.1.0</NETStandardLibraryRefVersion>
<NetStandardLibraryVersion>2.0.3</NetStandardLibraryVersion>
<MicrosoftDiagnosticsToolsRuntimeClientVersion>1.0.4-preview6.19326.1</MicrosoftDiagnosticsToolsRuntimeClientVersion>
Expand Down
2 changes: 2 additions & 0 deletions src/tests/tracing/eventpipe/userevents/dotnet-common.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let Microsoft_Windows_DotNETRuntime_flags = new_dotnet_provider_flags();
record_dotnet_provider("Microsoft-Windows-DotNETRuntime", 0x100003801D, 4, Microsoft_Windows_DotNETRuntime_flags);
129 changes: 129 additions & 0 deletions src/tests/tracing/eventpipe/userevents/userevents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 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;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Etlx;

namespace Tracing.Tests.UserEvents
{
public class UserEventsTest
{
private static readonly string trace = "trace.nettrace";
private const int SIGINT = 2;

[DllImport("libc", SetLastError = true)]
private static extern int kill(int pid, int sig);

public static int Main(string[] args)
{
if (args.Length > 0 && args[0] == "tracee")
{
UserEventsTracee.Run();
return 0;
}

return TestEntryPoint();
}

public static int TestEntryPoint()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add checks for:

  1. process is elevated
  2. OS is Linux
  3. user_events are supported

Its likely at some point this test will be run in the wrong environment and the logs should make it trivial to diagnose.

Copy link
Member

@lateralusX lateralusX Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should not even build the test on none linux platforms. CLRTestTargetUnsupported msbuild property could be used to exclude a test on specific platforms.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLRTestTargetUnsupported is in the csproj, so it should hopefully prevent this test from running on non linux-x64/linux-arm64 platforms. Then again, I think more logic is needed to check for Alpine.

Added checks for geteuid and checking if sys/kernel/tracing/user_events_data exists

string appBaseDir = AppContext.BaseDirectory;
string recordTracePath = Path.Combine(appBaseDir, "record-trace");
string scriptFilePath = Path.Combine(appBaseDir, "dotnet-common.script");
string traceFilePath = Path.Combine(appBaseDir, trace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to create a path in some randomly generated temp file (Path.GetTempFileName()). This helps avoid issues if the test runs multiple times and doesn't correctly clean up or if there is ever a scenario where the directory holding the test binary isn't writable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to Path.GetTempFileName(),


if (!File.Exists(recordTracePath) || !File.Exists(scriptFilePath))
{
Console.WriteLine("record-trace or dotnet-common.script not found. Test cannot run.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its helpful to print the exact path we didn't find. (In general its helpful for the test logging to err towards being overly verbose to make failure diagnosis easier)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, split into separate if blocks to have a more specific error message.

return -1;
}

Process traceeProcess = new();
traceeProcess.StartInfo.FileName = Process.GetCurrentProcess().MainModule.FileName;
traceeProcess.StartInfo.Arguments = $"{typeof(UserEventsTest).Assembly.Location} tracee";
traceeProcess.StartInfo.WorkingDirectory = appBaseDir;
traceeProcess.Start();
int traceePid = traceeProcess.Id;

Process recordTraceProcess = new();
recordTraceProcess.StartInfo.FileName = recordTracePath;
recordTraceProcess.StartInfo.Arguments = $"--script-file {scriptFilePath} --pid {traceePid}";
recordTraceProcess.StartInfo.WorkingDirectory = appBaseDir;
recordTraceProcess.StartInfo.RedirectStandardOutput = true;
recordTraceProcess.StartInfo.RedirectStandardError = true;
recordTraceProcess.OutputDataReceived += (_, args) => Console.WriteLine($"[record-trace] {args.Data}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should redirect the tracee output as well to ensure we aren't losing useful error diagnostics that it might print.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

recordTraceProcess.ErrorDataReceived += (_, args) => Console.Error.WriteLine($"[record-trace] {args.Data}");
recordTraceProcess.Start();
recordTraceProcess.BeginOutputReadLine();
recordTraceProcess.BeginErrorReadLine();

if (!recordTraceProcess.HasExited && !traceeProcess.WaitForExit(15000))
{
traceeProcess.Kill();
}

// Until record-trace supports duration, the only way to stop it is to send SIGINT (ctrl+c)
kill(recordTraceProcess.Id, SIGINT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more logging that we sent SIGINT, waiting for exit, and if we killed the process after timeout.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, thanks!

if (!recordTraceProcess.HasExited && !recordTraceProcess.WaitForExit(20000))
{
// record-trace needs to stop gracefully to generate the trace file
recordTraceProcess.Kill();
}

if (!File.Exists(traceFilePath))
{
Console.Error.WriteLine($"Expected trace file not found at `{traceFilePath}`");
return -1;
}

if (!ValidateTraceeEvents(traceFilePath))
{
Console.Error.WriteLine($"Trace file `{traceFilePath}` does not contain expected events.");
return -1;
}

if (File.Exists(traceFilePath))
{
try
{
File.Delete(traceFilePath);
}
catch {}
}

return 100;
}

private static bool ValidateTraceeEvents(string traceFilePath)
{
string etlxPath = TraceLog.CreateFromEventPipeDataFile(traceFilePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can parse the .nettrace file directly using EventPipeEventSource in TraceEvent. This avoids creating a 2nd file that the test also needs to clean up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason when I tried it last, it didn't work (pilot error), switched to using EventPipeEventSource. Right now the events are "unknown" with id. Maybe its cause I'm using the Dynamic parser? I'll look into TraceEvent more closely

using TraceLog log = new(etlxPath);
using TraceLogEventSource source = log.Events.GetSource();
bool startEventFound = false;
bool stopEventFound = false;

source.AllEvents += (TraceEvent e) =>
Copy link
Member

@lateralusX lateralusX Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to add more tests here that checks for metadata/fields, rundown events, callstacks etc or should we add some basic verification to this test or plan to extend existing EventPipe tests to also work over UserEvents?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was mainly to add a basic verification that the end to end runtime side (from accepting the ipc message to writing to the tracepoints worked). Since User_events is built on EventPipe, my initial thought is that duplicating the existing eventpipe tests for user events wouldn't be adding anything. I think we can add more tests later on, but not sure what coverage is good and reasonable. I'm not even sure yet if our CI machines have user_events, or if they run with elevated privileges, so this was mainly to see if we can have a basic E2E test going.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely not duplicate but maybe look on extending the existing event pipe tests to run over user events + additional validate logic but agree that is something we could look at later.

So, if this is mainly a smoke test then maybe we should make sure we at least hit things we know is handled in the one-collect library, during the work we hit a number of things that needed special attention, like activity id's, custom metadata and potential stack traces. Right now, these only tests one runtime start/stop event fired under very unique circumstances. Maybe we should do some short multi-threading scenario as well, making sure we won't hit any races in the code path unique to user events?

{
if (e.ProviderName == "Microsoft-Windows-DotNETRuntime")
{
if (e.EventName == "GC/Start")
{
startEventFound = true;
}
else if (e.EventName == "GC/Stop")
{
stopEventFound = true;
}
}
};

source.Process();
return startEventFound && stopEventFound;
}
}
}
36 changes: 36 additions & 0 deletions src/tests/tracing/eventpipe/userevents/userevents.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<CLRTestTargetUnsupported Condition="'$(TargetOS)' != 'linux' or ('$(TargetArchitecture)' != 'x64' and '$(TargetArchitecture)' != 'arm64')">true</CLRTestTargetUnsupported>
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is this line needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mostly following convention of other runtime tests. I'll remove it for this test, but when would it be appropriate to add?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to be the default for all projects under src\tests

<IlasmRoundTripIncompatible>true</IlasmRoundTripIncompatible>
<MicrosoftOneCollectRecordTraceVersion>0.1.32221</MicrosoftOneCollectRecordTraceVersion>
Copy link
Member

@jkotas jkotas Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in eng\Versions.props where we keep track of versions of all dependencies?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured that since this test acquires Microsoft.OneCollect.RecordTrace through dotnet-diagnostics-tests feed via RestoreAdditionalProjectSources I felt that it was more appropriate to scope the version only to the test. Nothing else in the repo uses that feed, and nothing else uses Microsoft.OneCollect.RecordTrace, so eng/Versions.props didn't feel right. But I can move it to eng/Versions.props if that's preferred. It just seemed misleading since I ddidnt think other projects in the repo could even acquire the package without adding the dotnet-diagnostics-tests as a nuget source.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many versions in Versions.props are one-offs. For example, GrpcAuthVersion is only used by src\tests\FunctionalTests\Android\Device_Emulator\gRPC\Android.Device_Emulator.gRPC.Test.csproj

Nothing else in the repo uses that feed,

I am not sure whether adding the extra field is going to be compatible with the eng system infrastructure. I do not see any other place that adds feeds this way in the repo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to using a test local NuGet.Config to add dotnet-diagnostics-tests as a source.

</PropertyGroup>

<PropertyGroup>
<RestoreAdditionalProjectSources>$(RestoreAdditionalProjectSources);https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-diagnostics-tests/nuget/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.OneCollect.RecordTrace" Version="$(MicrosoftOneCollectRecordTraceVersion)" PrivateAssets="All" Condition="'$(TargetOS)' == 'linux'" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
<Compile Include="usereventstracee.cs" />
</ItemGroup>

<ItemGroup>
<None Include="dotnet-common.script" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<Target Name="CopyRecordTraceBinary" AfterTargets="Build" Condition="'$(TargetOS)' == 'linux'">
<PropertyGroup>
<RecordTracePath>$(NuGetPackageRoot)microsoft.onecollect.recordtrace/$(MicrosoftOneCollectRecordTraceVersion)/runtimes/$(TargetOS)-$(TargetArchitecture)/native/record-trace</RecordTracePath>
</PropertyGroup>

<Copy SourceFiles="$(RecordTracePath)" DestinationFolder="$(OutputPath)" Condition="Exists('$(RecordTracePath)')" />
<Error Text="record-trace not found at $(RecordTracePath)" Condition="!Exists('$(RecordTracePath)')" />
</Target>
</Project>
31 changes: 31 additions & 0 deletions src/tests/tracing/eventpipe/userevents/usereventstracee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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;
using System.Threading;

namespace Tracing.Tests.UserEvents
{
public class UserEventsTracee
{
private static byte[] s_array;

public static void Run()
{
long startTimestamp = Stopwatch.GetTimestamp();
long targetTicks = Stopwatch.Frequency * 10; // 10s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 1 second instead? Ideally we want tests to run quickly whenever possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed


while (Stopwatch.GetTimestamp() - startTimestamp < targetTicks)
{
for (int i = 0; i < 100; i++)
{
s_array = new byte[1024 * 10];
}

GC.Collect();
Thread.Sleep(100);
}
}
}
}
Loading