Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add BuildPathList helper
  • Loading branch information
am11 committed Oct 2, 2022
commit f535d327903bf5bbe36242b18f1249c370bb96a3
16 changes: 16 additions & 0 deletions src/coreclr/tools/Common/CommandLineHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public static Dictionary<string, string> BuildPathDictionary(IReadOnlyList<Token
return dictionary;
}

public static List<string> BuildPathList(IReadOnlyList<Token> tokens)
{
List<string> paths = new();
foreach (Token token in tokens)
{
Dictionary<string, string> dictionary = new(StringComparer.OrdinalIgnoreCase);
AppendExpandedPaths(dictionary, token.Value, false);
foreach (string file in dictionary.Values)
{
paths.Add(file);
}
}

return paths;
}

public static TargetOS GetTargetOS(string token)
{
if(string.IsNullOrEmpty(token))
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/tools/dotnet-pgo/PgoRootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Microsoft.Diagnostics.Tools.Pgo
{
internal sealed class PgoRootCommand : RootCommand
{
public Option<Dictionary<string, string>> InputFilesToMerge { get; } =
new(new[] { "--input", "-i" }, result => Helpers.BuildPathDictionary(result.Tokens, false), false, "Input .mibc files to be merged. Multiple input arguments are specified as --input file1.mibc --input file2.mibc") { IsRequired = true, Arity = ArgumentArity.OneOrMore };
public Option<List<string>> InputFilesToMerge { get; } =
new(new[] { "--input", "-i" }, result => Helpers.BuildPathList(result.Tokens), false, "Input .mibc files to be merged. Multiple input arguments are specified as --input file1.mibc --input file2.mibc") { IsRequired = true, Arity = ArgumentArity.OneOrMore };
public Option<string[]> InputFilesToCompare { get; } =
new(new[] { "--input", "-i" }, "The input .mibc files to be compared. Specify as --input file1.mibc --input file2.mibc") { IsRequired = true, Arity = new ArgumentArity(2, 2) /* exactly two */ };
public Option<string> InputFileToDump { get; } =
Expand All @@ -31,8 +31,8 @@ internal sealed class PgoRootCommand : RootCommand
new(new[] { "--pid" }, "The pid within the trace of the process to examine. If this is a multi-process trace, at least one of --pid or --process-name must be specified");
public Option<string> ProcessName { get; } =
new(new[] { "--process-name" }, "The process name within the trace of the process to examine. If this is a multi-process trace, at least one of --pid or --process-name must be specified");
public Option<Dictionary<string, string>> Reference =
new(new[] { "--reference", "-r" }, result => Helpers.BuildPathDictionary(result.Tokens, false), true, "If a reference is not located on disk at the same location as used in the process, it may be specified with a --reference parameter. Multiple --reference parameters may be specified. The wild cards * and ? are supported by this option");
public Option<List<string>> Reference =
new(new[] { "--reference", "-r" }, result => Helpers.BuildPathList(result.Tokens), true, "If a reference is not located on disk at the same location as used in the process, it may be specified with a --reference parameter. Multiple --reference parameters may be specified. The wild cards * and ? are supported by this option");
public Option<int> ClrInstanceId { get; } =
new("--clr-instance-id", "If the process contains multiple .NET runtimes, the instance ID must be specified");
public Option<bool> Spgo { get; } =
Expand Down
34 changes: 18 additions & 16 deletions src/coreclr/tools/dotnet-pgo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,20 @@ public override string ToString()
}
}

class Program
internal sealed class Program
{
private static Logger s_logger = new Logger();

private readonly PgoRootCommand _command;
private List<string> _inputFilesToMerge;
private string[] _inputFilesToCompare;

public Program(PgoRootCommand command)
{
_command = command;

_inputFilesToMerge = Get(command.InputFilesToMerge);
_inputFilesToCompare = Get(command.InputFilesToCompare);
}

private T Get<T>(Option<T> option) => _command.Result.GetValueForOption(option);
Expand Down Expand Up @@ -251,11 +256,11 @@ public int Run()
{
return InnerDumpMain();
}
if (Get(_command.InputFilesToMerge) != null)
if (_inputFilesToMerge != null)
{
return InnerMergeMain();
}
if (Get(_command.InputFilesToCompare) != null)
if (_inputFilesToCompare != null)
{
return InnerCompareMibcMain();
}
Expand Down Expand Up @@ -400,13 +405,12 @@ private int InnerMergeMain()
return -8;
}

var paths = Get(_command.InputFilesToMerge);
var paths = _inputFilesToMerge;
PEReader[] mibcReaders = new PEReader[paths.Count];
for (int i = 0; i < mibcReaders.Length; i++)
{
string path = paths.ElementAt(i).Value;
PrintMessage($"Opening {path}");
mibcReaders[i] = MIbcProfileParser.OpenMibcAsPEReader(path);
PrintMessage($"Opening {paths[i]}");
mibcReaders[i] = MIbcProfileParser.OpenMibcAsPEReader(paths[i]);
}

HashSet<string> assemblyNamesInBubble = null;
Expand All @@ -429,7 +433,7 @@ private int InnerMergeMain()
for (int i = 0; i < mibcReaders.Length; i++)
{
var peReader = mibcReaders[i];
PrintDetailedMessage($"Merging {paths.ElementAt(i).Value}");
PrintDetailedMessage($"Merging {paths[i]}");
ProfileData.MergeProfileData(ref partialNgen, mergedProfileData, MIbcProfileParser.ParseMIbcFile(tsc, peReader, assemblyNamesInBubble, onlyDefinedInAssembly: null));
}

Expand All @@ -438,8 +442,8 @@ private int InnerMergeMain()
int result = MibcEmitter.GenerateMibcFile(mergedConfig, tsc, outputFileInfo, mergedProfileData.Values, _command.ValidateOutputFile, !Get(_command.Compressed));
if (result == 0 && Get(_command.InheritTimestamp))
{
outputFileInfo.CreationTimeUtc = paths.Values.Max(f => new FileInfo(f).CreationTimeUtc);
outputFileInfo.LastWriteTimeUtc = paths.Values.Max(f => new FileInfo(f).LastWriteTimeUtc);
outputFileInfo.CreationTimeUtc = paths.Max(f => new FileInfo(f).CreationTimeUtc);
outputFileInfo.LastWriteTimeUtc = paths.Max(f => new FileInfo(f).LastWriteTimeUtc);
}

return result;
Expand All @@ -455,12 +459,10 @@ private int InnerMergeMain()

private int InnerCompareMibcMain()
{
var paths = Get(_command.InputFilesToCompare);

// Command line parser should require exactly 2 files
Trace.Assert(paths.Length == 2);
string file1 = paths[0];
string file2 = paths[1];
Trace.Assert(_inputFilesToCompare.Length == 2);
string file1 = _inputFilesToCompare[0];
string file2 = _inputFilesToCompare[1];

// Look for the shortest unique names for the input files.
string name1 = Path.GetFileName(file1);
Expand Down Expand Up @@ -1183,7 +1185,7 @@ private int InnerProcessTraceFileMain()

bool filePathError = false;
HashSet<ModuleDesc> modulesLoadedViaReference = new HashSet<ModuleDesc>();
foreach (string file in Get(_command.Reference).Values)
foreach (string file in Get(_command.Reference))
{
try
{
Expand Down