Skip to content

Commit

Permalink
Detect perfview failures (dotnet#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Nov 21, 2023
1 parent fcf71c7 commit e3e4139
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/Microsoft.Crank.Agent/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Based on the target framework
private static readonly string DefaultChannel = "current";
private const int CommitHashLength = 12;

private const string PerfViewVersion = "v3.1.5";
private const string PerfViewVersion = "v3.1.6";

private static readonly HttpClient _httpClient;
private static readonly HttpClientHandler _httpClientHandler;
Expand Down Expand Up @@ -1320,7 +1320,8 @@ private static async Task ProcessJobs(string hostname, string dockerHostname, Ca
{
if (OperatingSystem == OperatingSystem.Windows)
{
RunPerfview($"stop /AcceptEula /NoNGenRundown /NoView {_startPerfviewArguments}", Path.Combine(tempDir, benchmarksDir));
var logFilename = Path.Combine(workingDirectory, "perfview.log");
RunPerfview($"stop /AcceptEula /NoNGenRundown /NoView /LogFile:\"{logFilename}\" {_startPerfviewArguments}", Path.Combine(tempDir, benchmarksDir));
}
else if (OperatingSystem == OperatingSystem.Linux)
{
Expand Down Expand Up @@ -1547,7 +1548,8 @@ async Task StopJobAsync(bool abortCollection = false)
// Abort all Perfview processes
if (OperatingSystem == OperatingSystem.Windows)
{
var perfViewProcess = RunPerfview("abort", Path.GetPathRoot(_perfviewPath));
var logFilename = Path.Combine(workingDirectory, "perfview.log");
RunPerfview($"abort /LogFile:\"{logFilename}\"", Path.GetPathRoot(_perfviewPath));
}
else if (OperatingSystem == OperatingSystem.Linux)
{
Expand Down Expand Up @@ -1799,12 +1801,12 @@ private static void EnsureSourceFolderExists(string sourceTempDir, Source source
}
}

private static string RunPerfview(string arguments, string workingDirectory)
private static bool RunPerfview(string arguments, string workingDirectory)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Log.Info($"PerfView is only supported on Windows");
return null;
return false;
}

Log.Info($"Starting process '{_perfviewPath} {arguments}' in '{workingDirectory}'");
Expand All @@ -1822,35 +1824,13 @@ private static string RunPerfview(string arguments, string workingDirectory)
EnableRaisingEvents = true
};

var perfviewDoneEvent = new ManualResetEvent(false);
var output = new StringBuilder();

process.OutputDataReceived += (_, e) =>
{
if (e != null && e.Data != null)
{
Log.Info(e.Data);

if (e.Data.Contains("Press enter to close window"))
{
perfviewDoneEvent.Set();
}

output.Append(e.Data);
}
};

process.Start();
process.BeginOutputReadLine();

// Wait until PerfView is done
perfviewDoneEvent.WaitOne();

// Perfview is waiting for a keystroke to stop
process.StandardInput.WriteLine();

process.WaitForExit();

var success = process.ExitCode == 0;

process.Close();
return output.ToString();
return success;
}

private static Process RunPerfcollect(string arguments, string workingDirectory)
Expand Down Expand Up @@ -5084,9 +5064,28 @@ private static void StartCollection(string workingDirectory, Job job)
_startPerfviewArguments += $" /{customArg.Key}{value}";
}

RunPerfview($"start /AcceptEula /NoGui {_startPerfviewArguments} \"{job.PerfViewTraceFile}\"", workingDirectory);
var logFilename = Path.Combine(workingDirectory, "perfview.log");

var success = RunPerfview($"start /AcceptEula /NoGui /LogFile:\"{logFilename}\" {_startPerfviewArguments} \"{job.PerfViewTraceFile}\"", workingDirectory);
Log.Info($"Starting PerfView {_startPerfviewArguments}");

if (!success)
{
// PerfView could not start
Log.Info($"PerfView failed.");
Log.Info($"{job.State} -> Failed ({job.Service}:{job.Id})");

if (File.Exists(logFilename))
{
var perfviewLog = File.ReadAllText(logFilename);

Log.Info(perfviewLog);
job.Error = perfviewLog;
}

job.State = JobState.Failed;
}

// PerfView adds ".etl.zip" to the requested filename
job.PerfViewTraceFile = job.PerfViewTraceFile + ".etl.zip";
}
Expand Down

0 comments on commit e3e4139

Please sign in to comment.