From 9904414f48ee5f69b4e1d289bf25bc0e5df48a11 Mon Sep 17 00:00:00 2001 From: Phil Garcia Date: Fri, 17 May 2024 18:47:52 -0700 Subject: [PATCH] - Added counters #1233 (#1235) --- Source/Mosa.Compiler.Framework/Compiler.cs | 22 ++++++++++++++++++- .../CompilerStages/LinkerEmitStage.cs | 6 +++-- .../CompilerStages/LinkerLayoutStage.cs | 10 +++++---- Source/Mosa.Compiler.Framework/Counters.cs | 22 ++++++++++++++----- .../Mosa.Compiler.Framework/MethodCompiler.cs | 13 +++++------ .../Mosa.Compiler.Framework/MethodScanner.cs | 16 +++++++------- 6 files changed, 61 insertions(+), 28 deletions(-) diff --git a/Source/Mosa.Compiler.Framework/Compiler.cs b/Source/Mosa.Compiler.Framework/Compiler.cs index 20be2d0537..0afdf88c98 100644 --- a/Source/Mosa.Compiler.Framework/Compiler.cs +++ b/Source/Mosa.Compiler.Framework/Compiler.cs @@ -113,6 +113,12 @@ public sealed class Compiler public bool HasError { get; private set; } + public Stopwatch CompileTime { get; } = new Stopwatch(); + + public Stopwatch TotalCompileTime { get; } = new Stopwatch(); + + public Stopwatch LinkerTime { get; } = new Stopwatch(); + #endregion Properties #region Static Methods @@ -131,8 +137,8 @@ public sealed class Compiler !string.IsNullOrEmpty(mosaSettings.PreLinkHashFile) ? new PreLinkHashFileStage() : null, new LinkerLayoutStage(), !string.IsNullOrEmpty(mosaSettings.PostLinkHashFile) ? new PostLinkHashFileStage() : null, - !string.IsNullOrEmpty(mosaSettings.CompileTimeFile) ? new MethodCompileTimeStage() : null, !string.IsNullOrEmpty(mosaSettings.OutputFile) && mosaSettings.EmitBinary ? new LinkerEmitStage() : null, + !string.IsNullOrEmpty(mosaSettings.CompileTimeFile) ? new MethodCompileTimeStage() : null, !string.IsNullOrEmpty(mosaSettings.MapFile) ? new MapFileStage() : null, !string.IsNullOrEmpty(mosaSettings.CounterFile) ? new CounterFileStage() : null, !string.IsNullOrEmpty(mosaSettings.DebugFile) ? new DebugFileStage() : null, @@ -448,8 +454,13 @@ private MosaMethod CompileMethod(MosaMethod method, int threadID) public void ExecuteCompile(int maxThreads) { + GlobalCounters.Set("Compiler.MaxThreads", maxThreads); + PostEvent(CompilerEvent.CompilingMethodsStart); + CompileTime.Start(); + TotalCompileTime.Start(); + if (maxThreads > 0) { var threads = Enumerable @@ -461,7 +472,9 @@ public void ExecuteCompile(int maxThreads) threads.ForEach(x => x.Join()); } else + { CompilePass(); + } PostEvent(CompilerEvent.CompilingMethodsCompleted); } @@ -490,6 +503,9 @@ private void CompilePass() /// internal void Finalization() { + CompileTime.Stop(); + GlobalCounters.Set("Elapsed.Total.Milliseconds", (int)CompileTime.ElapsedMilliseconds); + PostEvent(CompilerEvent.FinalizationStart); // Sum up the counters @@ -520,6 +536,10 @@ internal void Finalization() PostEvent(CompilerEvent.FinalizationStageEnd, stage.Name); } + TotalCompileTime.Stop(); + GlobalCounters.Set("Elapsed.TotalCompile.Milliseconds", (int)TotalCompileTime.ElapsedMilliseconds); + GlobalCounters.Set("Compiler.TotalMethods", MethodScheduler.TotalMethods); + MethodScanner.Complete(); EmitCounters(); diff --git a/Source/Mosa.Compiler.Framework/CompilerStages/LinkerEmitStage.cs b/Source/Mosa.Compiler.Framework/CompilerStages/LinkerEmitStage.cs index 4d0a76433c..6abb3c6945 100644 --- a/Source/Mosa.Compiler.Framework/CompilerStages/LinkerEmitStage.cs +++ b/Source/Mosa.Compiler.Framework/CompilerStages/LinkerEmitStage.cs @@ -16,15 +16,17 @@ protected override void Finalization() if (string.IsNullOrEmpty(MosaSettings.OutputFile)) return; - Compiler.PostEvent(CompilerEvent.LinkingStart); - File.Delete(MosaSettings.OutputFile); + Compiler.PostEvent(CompilerEvent.LinkingStart); + using (var file = new FileStream(MosaSettings.OutputFile, FileMode.Create)) { Linker.Emit(file); } + Compiler.LinkerTime.Stop(); + Compiler.PostEvent(CompilerEvent.LinkingEnd); } } diff --git a/Source/Mosa.Compiler.Framework/CompilerStages/LinkerLayoutStage.cs b/Source/Mosa.Compiler.Framework/CompilerStages/LinkerLayoutStage.cs index 1692a3fbbf..3e1eefb1fe 100644 --- a/Source/Mosa.Compiler.Framework/CompilerStages/LinkerLayoutStage.cs +++ b/Source/Mosa.Compiler.Framework/CompilerStages/LinkerLayoutStage.cs @@ -17,9 +17,11 @@ protected override void Finalization() Linker.FinalizeLayout(); - Compiler.GlobalCounters.Update("Linker.Text", (int)Linker.Sections[(int)SectionKind.Text].Size); - Compiler.GlobalCounters.Update("Linker.Data", (int)Linker.Sections[(int)SectionKind.Data].Size); - Compiler.GlobalCounters.Update("Linker.ROData", (int)Linker.Sections[(int)SectionKind.ROData].Size); - Compiler.GlobalCounters.Update("Linker.BSS", (int)Linker.Sections[(int)SectionKind.BSS].Size); + Compiler.LinkerTime.Reset(); + + Compiler.GlobalCounters.Set("Linker.Text", (int)Linker.Sections[(int)SectionKind.Text].Size); + Compiler.GlobalCounters.Set("Linker.Data", (int)Linker.Sections[(int)SectionKind.Data].Size); + Compiler.GlobalCounters.Set("Linker.ROData", (int)Linker.Sections[(int)SectionKind.ROData].Size); + Compiler.GlobalCounters.Set("Linker.BSS", (int)Linker.Sections[(int)SectionKind.BSS].Size); } } diff --git a/Source/Mosa.Compiler.Framework/Counters.cs b/Source/Mosa.Compiler.Framework/Counters.cs index 6578e49eae..81b3ea658a 100644 --- a/Source/Mosa.Compiler.Framework/Counters.cs +++ b/Source/Mosa.Compiler.Framework/Counters.cs @@ -17,29 +17,41 @@ public void Reset() public void Update(string name, int count) { - UpdateCounter(name, count); + UpdateCounter(name, count, false); + } + + public void Set(string name, int count) + { + UpdateCounter(name, count, true); } public void Update(Counter counter) { - UpdateCounter(counter.Name, counter.Count); + UpdateCounter(counter.Name, counter.Count, false); } public void Update(Counters counters) { foreach (var counter in counters.Entries.Values) { - UpdateCounter(counter.Name, counter.Count); + UpdateCounter(counter.Name, counter.Count, false); } } - private void UpdateCounter(string name, int count) + private void UpdateCounter(string name, int count, bool reset = false) { lock (_lock) { if (Entries.TryGetValue(name, out var counter)) { - counter.Increment(count); + if (reset) + { + counter.Set(count); + } + else + { + counter.Increment(count); + } } else { diff --git a/Source/Mosa.Compiler.Framework/MethodCompiler.cs b/Source/Mosa.Compiler.Framework/MethodCompiler.cs index b11e71fb7e..90f005f748 100644 --- a/Source/Mosa.Compiler.Framework/MethodCompiler.cs +++ b/Source/Mosa.Compiler.Framework/MethodCompiler.cs @@ -320,8 +320,7 @@ public MethodCompiler(Compiler compiler, MosaMethod method, BasicBlocks basicBlo MethodData.ReturnSize = methodInfo.ReturnSize; MethodData.ReturnInRegister = methodInfo.ReturnInRegister; - //MethodData.Counters.Update("ExecutionTime.Setup.Ticks", (int)Stopwatch.ElapsedTicks); - MethodData.Counters.Update("ExecutionTime.Setup.Milliseconds", (int)(Stopwatch.Elapsed.Ticks / TimeSpan.TicksPerMillisecond)); + MethodData.Counters.Set("ExecutionTime.Setup.Milliseconds", (int)(Stopwatch.Elapsed.Ticks / TimeSpan.TicksPerMillisecond)); Transform.SetCompiler(compiler); Transform.SetMethodCompiler(this); @@ -453,11 +452,9 @@ private void ExecutePipeline() MethodData.ElapsedTicks = lastTick; - //MethodData.Counters.Update("ExecutionTime.StageStart.Ticks", (int)startTick); - //MethodData.Counters.Update("ExecutionTime.Total.Ticks", (int)lastTick); - MethodData.Counters.Update("ExecutionTime.Setup.Milliseconds", (int)startMS); - MethodData.Counters.Update("ExecutionTime.Execution.Milliseconds", (int)(lastMS - startMS)); - MethodData.Counters.Update("ExecutionTime.Total.Milliseconds", (int)lastMS); + MethodData.Counters.Set("ExecutionTime.Setup.Milliseconds", (int)startMS); + MethodData.Counters.Set("ExecutionTime.Execution.Milliseconds", (int)(lastMS - startMS)); + MethodData.Counters.Set("ExecutionTime.Total.Milliseconds", (int)lastMS); var executionTimeLog = new TraceLog(TraceType.MethodDebug, Method, "Execution Time/Ticks", MethodData.Version); @@ -478,7 +475,7 @@ private void ExecutePipeline() executionTimeLog.Log(entry); //MethodData.Counters.Update($"ExecutionTime.{i:00}.{Pipeline[i].Name}.Ticks", (int)ticks); - MethodData.Counters.Update($"ExecutionTime.{i:00}.{Pipeline[i].Name}.Milliseconds", (int)(ticks / TimeSpan.TicksPerMillisecond)); + MethodData.Counters.Set($"ExecutionTime.{i:00}.{Pipeline[i].Name}.Milliseconds", (int)(ticks / TimeSpan.TicksPerMillisecond)); } executionTimeLog.Log($"{"****Total Time",-57}({lastTick} Ticks)"); diff --git a/Source/Mosa.Compiler.Framework/MethodScanner.cs b/Source/Mosa.Compiler.Framework/MethodScanner.cs index 66d350c8b2..dfe41973a3 100644 --- a/Source/Mosa.Compiler.Framework/MethodScanner.cs +++ b/Source/Mosa.Compiler.Framework/MethodScanner.cs @@ -79,14 +79,14 @@ public void Complete() } } - Compiler.GlobalCounters.Update("MethodScanner.TotalTypes", totalTypes); - Compiler.GlobalCounters.Update("MethodScanner.TotalMethods", totalMethods); - - Compiler.GlobalCounters.Update("MethodScanner.AllocatedTypes", allocatedTypes.Count); - Compiler.GlobalCounters.Update("MethodScanner.InvokedMethods", invokedMethods.Count); - Compiler.GlobalCounters.Update("MethodScanner.ScheduledMethods", scheduledMethods.Count); - Compiler.GlobalCounters.Update("MethodScanner.AccessedFields", accessedFields.Count); - Compiler.GlobalCounters.Update("MethodScanner.InvokedInterfaceType", invokedInteraceTypes.Count); + Compiler.GlobalCounters.Set("MethodScanner.TotalTypes", totalTypes); + Compiler.GlobalCounters.Set("MethodScanner.TotalMethods", totalMethods); + + Compiler.GlobalCounters.Set("MethodScanner.AllocatedTypes", allocatedTypes.Count); + Compiler.GlobalCounters.Set("MethodScanner.InvokedMethods", invokedMethods.Count); + Compiler.GlobalCounters.Set("MethodScanner.ScheduledMethods", scheduledMethods.Count); + Compiler.GlobalCounters.Set("MethodScanner.AccessedFields", accessedFields.Count); + Compiler.GlobalCounters.Set("MethodScanner.InvokedInterfaceType", invokedInteraceTypes.Count); Compiler.PostTraceLog(trace); }