From a25783f47ee5d28017f8af054ebbbe3787ea4ab9 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Tue, 16 Jan 2024 06:23:51 -0800 Subject: [PATCH] Reduce process count for ETW manifest test (#96950) * Reduce process count for ETW manifest test Remove the launch process and instead run within the test host. Launch the ETW session prior to launching the process - removes the race window between ETW session spin up and process start. --- .../TestsManifestGeneration.Etw.cs | 151 +++++++++--------- 1 file changed, 73 insertions(+), 78 deletions(-) diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs index 214f75b625a88..d3001cc369032 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs @@ -33,117 +33,112 @@ public partial class TestsManifestGeneration [SkipOnCoreClr("Test should only be run in non-stress modes", ~RuntimeTestModes.RegularRun)] public void Test_EventSource_EtwManifestGeneration() { - RemoteInvokeOptions options = new RemoteInvokeOptions { TimeOut = 300_000 /* ms */ }; - RemoteExecutor.Invoke(() => - { - RemoteInvokeOptions localOptions = new RemoteInvokeOptions { TimeOut = 300_000 /* ms */ }; - using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() => - { - var es = new SimpleEventSource(); - for (var i = 0; i < 100; i++) - { - es.WriteSimpleInt(i); - Thread.Sleep(100); - } - }, localOptions)) - { - var etlFileName = @"file.etl"; - var tracesession = new TraceEventSession("testname", etlFileName); + var pid = Process.GetCurrentProcess().Id; + var etlFileName = $"file.{pid}.etl"; + var tracesession = new TraceEventSession("testname", etlFileName); - tracesession.EnableProvider("SimpleEventSource"); + tracesession.EnableProvider(nameof(SimpleEventSource)); - Thread.Sleep(TimeSpan.FromSeconds(5)); + RemoteInvokeOptions localOptions = new RemoteInvokeOptions { TimeOut = 300_000 /* ms */ }; + using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() => + { + var es = new SimpleEventSource(); + for (var i = 0; i < 100; i++) + { + es.WriteSimpleInt(i); + Thread.Sleep(100); + } + }, localOptions)) + { + Thread.Sleep(TimeSpan.FromSeconds(5)); - tracesession.Flush(); + tracesession.Flush(); - tracesession.DisableProvider("SimpleEventSource"); - tracesession.Dispose(); + tracesession.DisableProvider(nameof(SimpleEventSource)); + tracesession.Dispose(); - var manifestExists = false; - var max_retries = 50; + var manifestExists = false; + var max_retries = 50; - for (int i = 0; i < max_retries; i++) + for (int i = 0; i < max_retries; i++) + { + if (VerifyManifestAndRemoveFile(etlFileName)) { - if (VerifyManifestAndRemoveFile(etlFileName)) - { - manifestExists = true; - break; - } - Thread.Sleep(1000); + manifestExists = true; + break; } - Assert.True(manifestExists); + Thread.Sleep(1000); } - }, options).Dispose(); + Assert.True(manifestExists); + } } [ConditionalFact(nameof(IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported))] [SkipOnCoreClr("Test should only be run in non-stress modes", ~RuntimeTestModes.RegularRun)] public void Test_EventSource_EtwManifestGenerationRollover() { - RemoteExecutor.Invoke(() => - { - using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() => - { - var es = new SimpleEventSource(); - for (var i = 0; i < 100; i++) - { - es.WriteSimpleInt(i); - Thread.Sleep(100); - } - })) - { - var initialFileName = @"initialFile.etl"; - var rolloverFileName = @"rolloverFile.etl"; - var tracesession = new TraceEventSession("testname", initialFileName); - var max_retries = 50; + var pid = Process.GetCurrentProcess().Id; + var initialFileName = $"initialFile.{pid}.etl"; + var rolloverFileName = $"rolloverFile.{pid}.etl"; + var tracesession = new TraceEventSession("testname", initialFileName); - tracesession.EnableProvider("SimpleEventSource"); + tracesession.EnableProvider(nameof(SimpleEventSource)); - Thread.Sleep(TimeSpan.FromSeconds(5)); + using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() => + { + var es = new SimpleEventSource(); + for (var i = 0; i < 100; i++) + { + es.WriteSimpleInt(i); + Thread.Sleep(100); + } + })) + { + Thread.Sleep(TimeSpan.FromSeconds(5)); - tracesession.Flush(); + tracesession.Flush(); - tracesession.SetFileName(rolloverFileName); + tracesession.SetFileName(rolloverFileName); - Thread.Sleep(TimeSpan.FromSeconds(5)); + Thread.Sleep(TimeSpan.FromSeconds(5)); - tracesession.Flush(); + tracesession.Flush(); - tracesession.DisableProvider("SimpleEventSource"); - tracesession.Dispose(); + tracesession.DisableProvider(nameof(SimpleEventSource)); + tracesession.Dispose(); - bool initialFileHasManifest = false; - bool rollOverFileHasManifest = false; + bool initialFileHasManifest = false; + bool rollOverFileHasManifest = false; - for (int i = 0; i < max_retries; i++) + var max_retries = 50; + for (int i = 0; i < max_retries; i++) + { + if (VerifyManifestAndRemoveFile(initialFileName)) { - if (VerifyManifestAndRemoveFile(initialFileName)) - { - initialFileHasManifest = true; - break; - } - Thread.Sleep(1000); + initialFileHasManifest = true; + break; } - for (int i = 0; i < max_retries; i++) + Thread.Sleep(1000); + } + for (int i = 0; i < max_retries; i++) + { + if (VerifyManifestAndRemoveFile(rolloverFileName)) { - if (VerifyManifestAndRemoveFile(rolloverFileName)) - { - rollOverFileHasManifest = true; - break; - } - Thread.Sleep(1000); + rollOverFileHasManifest = true; + break; } - Assert.True(initialFileHasManifest); - Assert.True(rollOverFileHasManifest); + Thread.Sleep(1000); } - }).Dispose(); + Assert.True(initialFileHasManifest); + Assert.True(rollOverFileHasManifest); + } } private bool VerifyManifestAndRemoveFile(string fileName) { Assert.True(File.Exists(fileName)); - ETWTraceEventSource source = new ETWTraceEventSource(fileName); + using var source = new ETWTraceEventSource(fileName); Dictionary providers = new Dictionary(); int eventCount = 0; @@ -157,7 +152,7 @@ private bool VerifyManifestAndRemoveFile(string fileName) } providers[eventData.ProviderName]++; - if (eventData.ProviderName.Equals("SimpleEventSource") && eventData.EventName.Equals("ManifestData")) + if (eventData.ProviderName.Equals(nameof(SimpleEventSource)) && eventData.EventName.Equals("ManifestData")) { sawManifestData = true; } @@ -167,7 +162,7 @@ private bool VerifyManifestAndRemoveFile(string fileName) if (!sawManifestData) { - Console.WriteLine("Did not see ManifestData event from SimpleEventSource, test will fail. Additional info:"); + Console.WriteLine($"Did not see ManifestData event from {nameof(SimpleEventSource)}, test will fail. Additional info:"); Console.WriteLine($" file name {fileName}"); Console.WriteLine($" total event count {eventCount}"); Console.WriteLine($" total providers {providers.Count}");