Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for profiler test arm64 #102066

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
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
14 changes: 10 additions & 4 deletions src/tests/profiler/multiple/multiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ 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();

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)
{
ManualResetEvent _profilerDone = new ManualResetEvent(false);
PassCallbackToProfiler(() => _profilerDone.Set());
PassCallbackToProfiler(ProfilerDone);
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 explicitly cache this delegate given the correctness of the test depends on it. Right now we are relying on the C# compiler to do that caching implicitly. Some versions of the C# compiler (C# 11) do cache it in practice but this is not considered guaranteed behavior by the spec.

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 will push changes to explicitly cache the delegate because explicit is better, but isn't that link just talking about whether the C# compiler caches and reuses the same delegate in multiple callsites? It could still keep the delegate rooted even if it never uses the same one twice in old versions.


ProfilerControlHelpers.AttachProfilerToSelf(MultipleProfilerGuid, ProfilerPath);

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
11 changes: 8 additions & 3 deletions src/tests/profiler/unittest/releaseondetach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
Expand Down