Properly rebuild optimization data when it changes (#56397)
authorJakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
Wed, 28 Jul 2021 08:27:36 +0000 (10:27 +0200)
committerGitHub <noreply@github.com>
Wed, 28 Jul 2021 08:27:36 +0000 (10:27 +0200)
The timestamp of the merged .mibc file was set to when the tool was
invoked, while the inputs have a timestamp from when they were created
in the training scenarios. That means the target to create the merged
.mibc file would not run incrementally until many weeks later.

To fix add an --inherit-timestamp flag to dotnet-pgo that makes the
merged output inherit the max timestamp from the inputs. This is not
ideal as it means incrementally going backwards does not work, but it's
better than the previous behavior.

Fix #53637

src/coreclr/crossgen-corelib.proj
src/coreclr/tools/dotnet-pgo/CommandLineOptions.cs
src/coreclr/tools/dotnet-pgo/Program.cs

index 6d675a2..655fc32 100644 (file)
@@ -66,6 +66,7 @@
       <DotNetPgoCmd>$(DotNetCli) $([MSBuild]::NormalizePath('$(BinDir)', 'dotnet-pgo', 'dotnet-pgo.dll')) merge</DotNetPgoCmd>
       <DotNetPgoCmd>$(DotNetPgoCmd) -o:$(MergedMibcPath)</DotNetPgoCmd>
       <DotNetPgoCmd>$(DotNetPgoCmd) @(OptimizationMibcFiles->'-i:%(Identity)', ' ')</DotNetPgoCmd>
+      <DotNetPgoCmd>$(DotNetPgoCmd) --inherit-timestamp</DotNetPgoCmd> <!-- For incremental builds, otherwise timestamp is too far in the future -->
     </PropertyGroup>
 
     <Message Condition="'$(DotNetBuildFromSource)' != 'true'" Importance="High" Text="$(DotNetPgoCmd)"/>
index 3da9ded..a2e3a82 100644 (file)
@@ -45,6 +45,7 @@ namespace Microsoft.Diagnostics.Tools.Pgo
         public bool DumpMibc = false;
         public FileInfo InputFileToDump;
         public List<FileInfo> CompareMibc;
+        public bool InheritTimestamp;
 
         public string[] HelpArgs = Array.Empty<string>();
 
@@ -265,6 +266,8 @@ namespace Microsoft.Diagnostics.Tools.Pgo
                     }
                 }
 
+                syntax.DefineOption(name: "inherit-timestamp", value: ref InheritTimestamp, help: "If specified, set the output's timestamp to the max timestamp of the input files");
+
                 VerbosityOption();
                 CompressedOption();
                 HelpOption();
index f129fdf..7711337 100644 (file)
@@ -378,7 +378,14 @@ namespace Microsoft.Diagnostics.Tools.Pgo
                     ProfileData.MergeProfileData(ref partialNgen, mergedProfileData, MIbcProfileParser.ParseMIbcFile(tsc, peReader, assemblyNamesInBubble, onlyDefinedInAssembly: null));
                 }
 
-                return MibcEmitter.GenerateMibcFile(tsc, commandLineOptions.OutputFileName, mergedProfileData.Values, commandLineOptions.ValidateOutputFile, commandLineOptions.Uncompressed);
+                int result = MibcEmitter.GenerateMibcFile(tsc, commandLineOptions.OutputFileName, mergedProfileData.Values, commandLineOptions.ValidateOutputFile, commandLineOptions.Uncompressed);
+                if (result == 0 && commandLineOptions.InheritTimestamp)
+                {
+                    commandLineOptions.OutputFileName.CreationTimeUtc = commandLineOptions.InputFilesToMerge.Max(fi => fi.CreationTimeUtc);
+                    commandLineOptions.OutputFileName.LastWriteTimeUtc = commandLineOptions.InputFilesToMerge.Max(fi => fi.LastWriteTimeUtc);
+                }
+
+                return result;
             }
             finally
             {