Skip to content

Commit

Permalink
Fix for profiler test arm64 (dotnet#102066)
Browse files Browse the repository at this point in the history
  • Loading branch information
davmason authored and Ruihan-Yin committed May 30, 2024
1 parent 8d17b69 commit e207365
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
38 changes: 18 additions & 20 deletions src/tests/profiler/common/ProfilerTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("=");
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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($"Profilee STDOUT: {line}");
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/tests/profiler/multiple/multiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ 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();

[DllImport("Profiler")]
private static extern void PassCallbackToProfiler(ProfilerCallback callback);

public static int RunTest(String[] args)
{
ManualResetEvent _profilerDone = new ManualResetEvent(false);
PassCallbackToProfiler(() => _profilerDone.Set());
ManualResetEvent profilerDone = new ManualResetEvent(false);
ProfilerCallback profilerDoneDelegate = () => profilerDone.Set();
PassCallbackToProfiler(profilerDoneDelegate);

ProfilerControlHelpers.AttachProfilerToSelf(MultipleProfilerGuid, ProfilerPath);

Expand All @@ -35,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;
}

Expand Down
11 changes: 7 additions & 4 deletions src/tests/profiler/native/multiple/multiple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 6 additions & 4 deletions src/tests/profiler/unittest/releaseondetach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ReleaseOnShutdown

[DllImport("Profiler")]
private static extern void PassCallbackToProfiler(ProfilerCallback callback);

public unsafe static int RunTest(string[] args)
{
string profilerName;
Expand All @@ -35,16 +35,18 @@ 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());
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;
}

Expand Down

0 comments on commit e207365

Please sign in to comment.