From 8006b64708e7754966edd4ea8539d51cac661287 Mon Sep 17 00:00:00 2001 From: David Mason Date: Thu, 9 May 2024 00:32:31 -0700 Subject: [PATCH 1/6] Fix delegate that can be collected --- src/tests/profiler/multiple/multiple.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/tests/profiler/multiple/multiple.cs b/src/tests/profiler/multiple/multiple.cs index a0952687021bf0..754f781873fbd9 100644 --- a/src/tests/profiler/multiple/multiple.cs +++ b/src/tests/profiler/multiple/multiple.cs @@ -13,13 +13,19 @@ class MultiplyLoaded static readonly Guid MultipleProfilerGuid = new Guid("BFA8EF13-E144-49B9-B95C-FC1C150C7651"); static readonly string ProfilerPath = ProfilerTestRunner.GetProfilerPath(); + static ManualResetEvent _profilerDone = new ManualResetEvent(false); + [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); + private static void ProfilerDone() + { + _profilerDone.Set(); + } + public static int RunTest(String[] args) { - ManualResetEvent _profilerDone = new ManualResetEvent(false); - PassCallbackToProfiler(() => _profilerDone.Set()); + PassCallbackToProfiler(ProfilerDone); ProfilerControlHelpers.AttachProfilerToSelf(MultipleProfilerGuid, ProfilerPath); From 6c6e2282cb7671076db1eb39d4ec44eea3248551 Mon Sep 17 00:00:00 2001 From: David Mason Date: Thu, 9 May 2024 23:13:00 -0700 Subject: [PATCH 2/6] Update multiple.cpp --- src/tests/profiler/native/multiple/multiple.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/tests/profiler/native/multiple/multiple.cpp b/src/tests/profiler/native/multiple/multiple.cpp index c36e4a8efa491f..054cb3e1e42656 100644 --- a/src/tests/profiler/native/multiple/multiple.cpp +++ b/src/tests/profiler/native/multiple/multiple.cpp @@ -57,11 +57,14 @@ HRESULT MultiplyLoaded::ProfilerDetachSucceeded() ++_detachCount; printf("ProfilerDetachSucceeded _detachCount=%d\n", _detachCount.load()); - if (_detachCount == MAX_PROFILERS - && _exceptionThrownSeenCount >= MAX_PROFILERS - && _failures == 0) + if (_detachCount == MAX_PROFILERS) { - printf("PROFILER TEST PASSES\n"); + if (_exceptionThrownSeenCount >= MAX_PROFILERS && _failures == 0) + { + printf("PROFILER TEST PASSES\n"); + fflush(stdout); + } + NotifyManagedCodeViaCallback(pCorProfilerInfo); } From d3f34db6f1053e875dfb4b04b89c3099bed95f76 Mon Sep 17 00:00:00 2001 From: David Mason Date: Thu, 9 May 2024 23:23:59 -0700 Subject: [PATCH 3/6] Update ProfilerTestRunner.cs --- .../profiler/common/ProfilerTestRunner.cs | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/tests/profiler/common/ProfilerTestRunner.cs b/src/tests/profiler/common/ProfilerTestRunner.cs index 2dfc2530c59d65..2e1165d1169dbf 100644 --- a/src/tests/profiler/common/ProfilerTestRunner.cs +++ b/src/tests/profiler/common/ProfilerTestRunner.cs @@ -52,7 +52,7 @@ public static int Run(string profileePath, if (loadAsNotification) { StringBuilder builder = new StringBuilder(); - for(int i = 0; i < notificationCopies; ++i) + for (int i = 0; i < notificationCopies; ++i) { builder.Append(profilerPath); builder.Append("="); @@ -94,13 +94,11 @@ public static int Run(string profileePath, envVars.Add("Profiler_Test_Name", testName); - if(!File.Exists(profilerPath)) + if (!File.Exists(profilerPath)) { FailFastWithMessage("Profiler library not found at expected path: " + profilerPath); } - ProfileeOutputVerifier verifier = new ProfileeOutputVerifier(); - Process process = new Process(); process.StartInfo.FileName = program; process.StartInfo.Arguments = arguments; @@ -117,15 +115,10 @@ public static int Run(string profileePath, process.StartInfo.EnvironmentVariables[key] = envVars[key]; } - process.OutputDataReceived += (sender, args) => - { - Console.WriteLine(args.Data); - verifier.WriteLine(args.Data); - }; process.Start(); + ProfileeOutputVerifier verifier = new ProfileeOutputVerifier(process.StandardOutput); - process.BeginOutputReadLine(); - + verifier.VerifyOutput(); process.WaitForExit(); // There are two conditions for profiler tests to pass, the output of the profiled program @@ -198,21 +191,26 @@ class ProfileeOutputVerifier private volatile bool _hasPassingOutput; public string SuccessPhrase = "PROFILER TEST PASSES"; - public bool HasPassingOutput => _hasPassingOutput; + private StreamReader standardOutput; - public void WriteLine(string message) + public ProfileeOutputVerifier(StreamReader standardOutput) { - if (message != null && message.Contains(SuccessPhrase)) - { - _hasPassingOutput = true; - } + this.standardOutput = standardOutput; } - public void WriteLine(string format, params object[] args) + public bool HasPassingOutput => _hasPassingOutput; + + internal void VerifyOutput() { - if (string.Format(format,args).Contains(SuccessPhrase)) + string line; + while ((line = standardOutput.ReadLine()) != null) { - _hasPassingOutput = true; + if (line.Contains(SuccessPhrase)) + { + _hasPassingOutput = true; + } + + Console.WriteLine($"Profiler STDOUT: {line}"); } } } From 379cfaf49f9dc32ba72590c70a12d3488d7d71a5 Mon Sep 17 00:00:00 2001 From: David Mason Date: Thu, 9 May 2024 23:30:17 -0700 Subject: [PATCH 4/6] Fix same issue in releaseondetach test --- src/tests/profiler/multiple/multiple.cs | 6 +++--- src/tests/profiler/unittest/releaseondetach.cs | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/tests/profiler/multiple/multiple.cs b/src/tests/profiler/multiple/multiple.cs index 754f781873fbd9..16fffc434ef872 100644 --- a/src/tests/profiler/multiple/multiple.cs +++ b/src/tests/profiler/multiple/multiple.cs @@ -10,10 +10,10 @@ namespace Profiler.Tests { class MultiplyLoaded { - static readonly Guid MultipleProfilerGuid = new Guid("BFA8EF13-E144-49B9-B95C-FC1C150C7651"); - static readonly string ProfilerPath = ProfilerTestRunner.GetProfilerPath(); + private static readonly Guid MultipleProfilerGuid = new Guid("BFA8EF13-E144-49B9-B95C-FC1C150C7651"); + private static readonly string ProfilerPath = ProfilerTestRunner.GetProfilerPath(); - static ManualResetEvent _profilerDone = new ManualResetEvent(false); + private static ManualResetEvent _profilerDone = new ManualResetEvent(false); [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); diff --git a/src/tests/profiler/unittest/releaseondetach.cs b/src/tests/profiler/unittest/releaseondetach.cs index e50190b1872dfe..bf1a77717ff1ca 100644 --- a/src/tests/profiler/unittest/releaseondetach.cs +++ b/src/tests/profiler/unittest/releaseondetach.cs @@ -12,10 +12,16 @@ namespace Profiler.Tests class ReleaseOnShutdown { private static readonly Guid ReleaseOnShutdownGuid = new Guid("B8C47A29-9C1D-4EEA-ABA0-8E8B3E3B792E"); + private static ManualResetEvent _profilerDone = new ManualResetEvent(false); [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); - + + private static void ProfilerDone() + { + _profilerDone.Set(); + } + public unsafe static int RunTest(string[] args) { string profilerName; @@ -35,11 +41,10 @@ public unsafe static int RunTest(string[] args) string rootPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string profilerPath = Path.Combine(rootPath, profilerName); - ManualResetEvent _profilerDone = new ManualResetEvent(false); Console.WriteLine($"Attaching profiler {profilerPath} to self."); ProfilerControlHelpers.AttachProfilerToSelf(ReleaseOnShutdownGuid, profilerPath); - PassCallbackToProfiler(() => _profilerDone.Set()); + PassCallbackToProfiler(ProfilerDone); if (!_profilerDone.WaitOne(TimeSpan.FromMinutes(5))) { Console.WriteLine("Profiler did not set the callback, test will fail."); From 5856592691b89a29c542b8618fa165055ae56325 Mon Sep 17 00:00:00 2001 From: David Mason Date: Thu, 9 May 2024 23:31:57 -0700 Subject: [PATCH 5/6] Typo --- src/tests/profiler/common/ProfilerTestRunner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/profiler/common/ProfilerTestRunner.cs b/src/tests/profiler/common/ProfilerTestRunner.cs index 2e1165d1169dbf..e95817116c90e7 100644 --- a/src/tests/profiler/common/ProfilerTestRunner.cs +++ b/src/tests/profiler/common/ProfilerTestRunner.cs @@ -210,7 +210,7 @@ internal void VerifyOutput() _hasPassingOutput = true; } - Console.WriteLine($"Profiler STDOUT: {line}"); + Console.WriteLine($"Profilee STDOUT: {line}"); } } } From c6d94bdc25078efa7d8a30bf16ca0c28336aae4e Mon Sep 17 00:00:00 2001 From: David Mason Date: Fri, 10 May 2024 03:20:11 -0700 Subject: [PATCH 6/6] Changes --- src/tests/profiler/multiple/multiple.cs | 14 +++++--------- src/tests/profiler/unittest/releaseondetach.cs | 13 +++++-------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/tests/profiler/multiple/multiple.cs b/src/tests/profiler/multiple/multiple.cs index 16fffc434ef872..7f2a5a5494929f 100644 --- a/src/tests/profiler/multiple/multiple.cs +++ b/src/tests/profiler/multiple/multiple.cs @@ -13,19 +13,14 @@ class MultiplyLoaded private static readonly Guid MultipleProfilerGuid = new Guid("BFA8EF13-E144-49B9-B95C-FC1C150C7651"); private static readonly string ProfilerPath = ProfilerTestRunner.GetProfilerPath(); - private static ManualResetEvent _profilerDone = new ManualResetEvent(false); - [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); - private static void ProfilerDone() - { - _profilerDone.Set(); - } - public static int RunTest(String[] args) { - PassCallbackToProfiler(ProfilerDone); + ManualResetEvent profilerDone = new ManualResetEvent(false); + ProfilerCallback profilerDoneDelegate = () => profilerDone.Set(); + PassCallbackToProfiler(profilerDoneDelegate); ProfilerControlHelpers.AttachProfilerToSelf(MultipleProfilerGuid, ProfilerPath); @@ -41,11 +36,12 @@ public static int RunTest(String[] args) } Console.WriteLine("Waiting for profilers to all detach"); - if (!_profilerDone.WaitOne(TimeSpan.FromMinutes(5))) + if (!profilerDone.WaitOne(TimeSpan.FromMinutes(5))) { throw new Exception("Test timed out waiting for the profilers to set the callback, test will fail."); } + GC.KeepAlive(profilerDoneDelegate); return 100; } diff --git a/src/tests/profiler/unittest/releaseondetach.cs b/src/tests/profiler/unittest/releaseondetach.cs index bf1a77717ff1ca..0e5ec490984cce 100644 --- a/src/tests/profiler/unittest/releaseondetach.cs +++ b/src/tests/profiler/unittest/releaseondetach.cs @@ -12,16 +12,10 @@ namespace Profiler.Tests class ReleaseOnShutdown { private static readonly Guid ReleaseOnShutdownGuid = new Guid("B8C47A29-9C1D-4EEA-ABA0-8E8B3E3B792E"); - private static ManualResetEvent _profilerDone = new ManualResetEvent(false); [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); - private static void ProfilerDone() - { - _profilerDone.Set(); - } - public unsafe static int RunTest(string[] args) { string profilerName; @@ -44,12 +38,15 @@ public unsafe static int RunTest(string[] args) Console.WriteLine($"Attaching profiler {profilerPath} to self."); ProfilerControlHelpers.AttachProfilerToSelf(ReleaseOnShutdownGuid, profilerPath); - PassCallbackToProfiler(ProfilerDone); - if (!_profilerDone.WaitOne(TimeSpan.FromMinutes(5))) + ManualResetEvent profilerDone = new ManualResetEvent(false); + ProfilerCallback profilerDoneDelegate = () => profilerDone.Set(); + PassCallbackToProfiler(profilerDoneDelegate); + if (!profilerDone.WaitOne(TimeSpan.FromMinutes(5))) { Console.WriteLine("Profiler did not set the callback, test will fail."); } + GC.KeepAlive(profilerDoneDelegate); return 100; }