Skip to content

AddAnalyzeAndAggregateCommand to GC infra. #4848

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using YamlDotNet.Serialization;

namespace GC.Infrastructure.Core.Configurations.ReliabilityFrameworkTest
{
public sealed class ReliabilityFrameworkTestAnalyzeConfiguration
{
public required string DebuggerPath { get; set; }
public required List<string> StackFrameKeyWords { get; set; }
public required string CoreRoot { get; set; }
public required string WSLInstanceLocation { get; set; }
public required string DumpFolder { get; set; }
public required string AnalyzeOutputFolder { get; set; }
}

public static class ReliabilityFrameworkTestAnalyzeConfigurationParser
{
private static readonly IDeserializer _deserializer =
new DeserializerBuilder().IgnoreUnmatchedProperties().Build();

public static ReliabilityFrameworkTestAnalyzeConfiguration Parse(string path)
{
// Preconditions.
ConfigurationChecker.VerifyFile(path, nameof(ReliabilityFrameworkTestAnalyzeConfigurationParser));

string serializedConfiguration = File.ReadAllText(path);
ReliabilityFrameworkTestAnalyzeConfiguration? configuration = null;

// This try catch is here because the exception from the YamlDotNet isn't helpful and must be imbued with more details.
try
{
configuration = _deserializer.Deserialize<ReliabilityFrameworkTestAnalyzeConfiguration>(serializedConfiguration);
}

catch (Exception ex)
{
throw new ArgumentException($"{nameof(ReliabilityFrameworkTestAnalyzeConfiguration)}: Unable to parse the yaml file because of an error in the syntax. Exception: {ex.Message} \n Call Stack: {ex.StackTrace}");
}

if (String.IsNullOrEmpty(configuration.AnalyzeOutputFolder))
{
throw new ArgumentException($"{nameof(ReliabilityFrameworkTestAnalyzeConfiguration)}: Provide a analyze output folder.");
}

if (!Path.Exists(configuration.DumpFolder))
{
throw new ArgumentException($"{nameof(ReliabilityFrameworkTestAnalyzeConfiguration)}: Dump folder doesn't exist.");
}

// Check Core_Root folder
if (!Path.Exists(configuration.CoreRoot))
{
throw new ArgumentException($"{nameof(ReliabilityFrameworkTestAnalyzeConfiguration)}: Core_Root doesn't exist.");
}
bool hasCoreRun = Directory.GetFiles(configuration.CoreRoot)
.Any(filePath => Path.GetFileNameWithoutExtension(filePath) == "corerun");
if (!hasCoreRun)
{
throw new ArgumentException($"{nameof(ReliabilityFrameworkTestAnalyzeConfiguration)}: Provide a valid Core_Root.");
}

return configuration;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.RegularExpressions;
using GC.Infrastructure.Core.Configurations;
using GC.Infrastructure.Core.Configurations.ReliabilityFrameworkTest;
using Spectre.Console;
using Spectre.Console.Cli;

namespace GC.Infrastructure.Commands.ReliabilityFrameworkTest
{
public class ReliabilityFrameworkTestAggregateCommand :
Command<ReliabilityFrameworkTestAggregateCommand.ReliabilityFrameworkTestAggregateSettings>
{
public class ReliabilityFrameworkTestDumpAnalyzeResult
{
public string? AttributedError { get; set; }
public string? DumpName { get; set; }
public string? CallStackForAllThreadsLogName { get; set; }
public string? CallStackLogName { get; set; }
public string? SourceFilePath { get; set; }
public string? LineNumber { get; set; }
}
public sealed class ReliabilityFrameworkTestAggregateSettings : CommandSettings
{
[Description("Path to Configuration.")]
[CommandOption("-c|--configuration")]
public required string ConfigurationPath { get; init; }
}

public override int Execute([NotNull] CommandContext context,
[NotNull] ReliabilityFrameworkTestAggregateSettings settings)
{
AnsiConsole.Write(new Rule("Aggregate Analysis Results For Reliability Framework Test"));
AnsiConsole.WriteLine();

ConfigurationChecker.VerifyFile(settings.ConfigurationPath,
nameof(ReliabilityFrameworkTestAggregateSettings));
ReliabilityFrameworkTestAnalyzeConfiguration configuration =
ReliabilityFrameworkTestAnalyzeConfigurationParser.Parse(settings.ConfigurationPath);

AggregateResult(configuration);
return 0;
}
public static void AggregateResult(ReliabilityFrameworkTestAnalyzeConfiguration configuration)
{
List<ReliabilityFrameworkTestDumpAnalyzeResult> dumpAnalyzeResultList = new List<ReliabilityFrameworkTestDumpAnalyzeResult>();

foreach (string callStackLogPath in Directory.GetFiles(configuration.AnalyzeOutputFolder, "*_callstack.txt"))
{
Console.WriteLine($"====== Extracting information from {callStackLogPath} ======");

string dumpPath = callStackLogPath.Replace("_callstack.txt", ".dmp");
string callStackForAllThreadsLogPath = callStackLogPath.Replace(
"_callstack.txt", "_callstack_allthreads.txt");

try
{
string callStack = File.ReadAllText(callStackLogPath);

// Search for frame contains keywords.
string? frameInfo = FindFrameByKeyWord(configuration.StackFrameKeyWords, callStack);

// If no line contains given keywords, mark it as unknown error
if (String.IsNullOrEmpty(frameInfo))
{
ReliabilityFrameworkTestDumpAnalyzeResult unknownErrorResult = new()
{
AttributedError = "Unknown error",
DumpName = Path.GetFileName(dumpPath),
CallStackLogName = Path.GetFileName(callStackLogPath),
CallStackForAllThreadsLogName = Path.GetFileName(callStackForAllThreadsLogPath),
SourceFilePath = String.Empty,
LineNumber = String.Empty
};

dumpAnalyzeResultList.Add(unknownErrorResult);
continue;
}

// Extract source file path and line number
(string, int)? SrcFileLineNumTuple =
ExtractSrcFilePathAndLineNumberFromFrameInfo(frameInfo);
if (!SrcFileLineNumTuple.HasValue)
{
continue;
}
(string srcFilePath, int lineNumber) = SrcFileLineNumTuple.Value;

int lineIndex = lineNumber - 1;
string realSrcFilePath = string.Empty;

// Convert source file path if it's in wsl.
if (srcFilePath.StartsWith("/"))
{
if (String.IsNullOrEmpty(configuration.WSLInstanceLocation))
{
Console.WriteLine($"Console.WriteLine Provide wsl instance location to access source file. ");
continue;
}

string srcFilePathWithBackSlash = srcFilePath.Replace("/", "\\");
realSrcFilePath = configuration.WSLInstanceLocation + srcFilePathWithBackSlash;
}
else
{
realSrcFilePath = srcFilePath;
}

// Get source code line that throw error.
var srcLineList = File.ReadAllLines(realSrcFilePath);

string srcLine = srcLineList[lineIndex].Trim();
while (String.IsNullOrEmpty(srcLine) || String.IsNullOrWhiteSpace(srcLine))
{
lineIndex = lineIndex - 1;
srcLine = srcLineList[lineIndex].Trim();
}
string error = srcLine;

ReliabilityFrameworkTestDumpAnalyzeResult dumpAnalyzeResult = new()
{
AttributedError = error,
DumpName = Path.GetFileName(dumpPath),
CallStackLogName = Path.GetFileName(callStackLogPath),
CallStackForAllThreadsLogName = Path.GetFileName(callStackForAllThreadsLogPath),
SourceFilePath = realSrcFilePath,
LineNumber = (lineIndex + 1).ToString()
};

dumpAnalyzeResultList.Add(dumpAnalyzeResult);
}
catch (Exception ex)
{
Console.WriteLine($"Console.WriteLine Fail to analyze {callStackLogPath}: {ex.Message}. ");
}
}

GenerateResultTable(dumpAnalyzeResultList, configuration.AnalyzeOutputFolder);
}
private static void GenerateResultTable(List<ReliabilityFrameworkTestDumpAnalyzeResult> dumpAnalyzeResultList,
string analyzeOutputFolder)
{
var resultListGroup = dumpAnalyzeResultList.GroupBy(dumpAnalyzeResult => dumpAnalyzeResult.AttributedError);

StringBuilder sb = new StringBuilder();
// Write title of table
sb.AppendLine("| Attributed Error | Count/Total(percentage%) | Dump Name | Log Name(Call Stacks of All Threads) | Source File Path | Line Number |");
sb.AppendLine("| :---------- | :---------: | :---------- | :---------- | :---------- | :---------: |");

foreach (IGrouping<string?, ReliabilityFrameworkTestDumpAnalyzeResult>? group in resultListGroup)
{
var resultListWithoutFirstItem = group.ToList();
var firstResult = resultListWithoutFirstItem.FirstOrDefault();
if (firstResult == null)
{
continue;
}
resultListWithoutFirstItem.Remove(firstResult);

string? attributedError = firstResult.AttributedError;
string proportion = $"{group.Count()}/{dumpAnalyzeResultList.Count}";
double proportionInPercentage = Convert.ToDouble(group.Count()) / Convert.ToDouble(dumpAnalyzeResultList.Count);
string? dumpName = firstResult.DumpName;
string? callStackForAllThreadsLogName = firstResult.CallStackForAllThreadsLogName;
string? sourceFilePath = firstResult.SourceFilePath;
string? lineNumber = firstResult.LineNumber;
sb.AppendLine($"| {attributedError} | {proportion}({proportionInPercentage * 100}%) | {dumpName} | {callStackForAllThreadsLogName} | {sourceFilePath} | {lineNumber} |");

foreach (ReliabilityFrameworkTestDumpAnalyzeResult? dumpAnalyzeResult in resultListWithoutFirstItem)
{
dumpName = dumpAnalyzeResult.DumpName;
callStackForAllThreadsLogName = dumpAnalyzeResult.CallStackForAllThreadsLogName;
sourceFilePath = dumpAnalyzeResult.SourceFilePath;
lineNumber = dumpAnalyzeResult.LineNumber;
sb.AppendLine($"| | | {dumpName} | {callStackForAllThreadsLogName} | {sourceFilePath} | {lineNumber} |");
}
}

try
{
string outputPath = Path.Combine(analyzeOutputFolder, "Results.md");
File.WriteAllText(outputPath, sb.ToString());
}
catch (Exception ex)
{
throw new Exception($"Fail to write result to markdown: {ex.Message}");
}

}
private static (string, int)? ExtractSrcFilePathAndLineNumberFromFrameInfo(string frameInfo)
{
string pattern = @"\[(.*?)\]";
Match match = Regex.Match(frameInfo, pattern, RegexOptions.Singleline);

if (!match.Success)
{
Console.WriteLine($"The symbol is not available.");
return null;
}

string fileNameWithLineNumber = match.Groups[1].Value.Trim();
string[] splitOutput = fileNameWithLineNumber.Split("@");

string? fileName = splitOutput.FirstOrDefault(String.Empty);
if (String.IsNullOrEmpty(fileName))
{
Console.WriteLine($"Console.WriteLineFail to extract source file path.");
return null;
}

string? lineNumberstr = splitOutput.LastOrDefault(String.Empty).Trim();
if (String.IsNullOrEmpty(lineNumberstr))
{
Console.WriteLine($"Console.WriteLineFail to extract line number.");
return null;
}

bool success = int.TryParse(lineNumberstr, out int lineNumber);
if (!success)
{
Console.WriteLine($"Console.WriteLineFail to parse line number.");
return null;
}

return (fileName, lineNumber);
}
private static string? FindFrameByKeyWord(List<string> keyWordList, string callStack)
{
string[] lines = callStack.Split("\n");
foreach (string line in lines)
{
foreach (string keyWord in keyWordList)
{
if (line.Contains(keyWord))
{
return line;
}
}
}

Console.WriteLine($"Console.WriteLineFail to find keyword.");
return null;
}
}
}
Loading