Skip to content

Commit

Permalink
- Added counters mosa#1233
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed May 18, 2024
1 parent 294893d commit df7d02b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
22 changes: 21 additions & 1 deletion Source/Mosa.Compiler.Framework/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -461,7 +472,9 @@ public void ExecuteCompile(int maxThreads)
threads.ForEach(x => x.Join());
}
else
{
CompilePass();
}

PostEvent(CompilerEvent.CompilingMethodsCompleted);
}
Expand Down Expand Up @@ -490,6 +503,9 @@ private void CompilePass()
/// </remarks>
internal void Finalization()
{
CompileTime.Stop();
GlobalCounters.Set("Elapsed.Total.Milliseconds", (int)CompileTime.ElapsedMilliseconds);

PostEvent(CompilerEvent.FinalizationStart);

// Sum up the counters
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 17 additions & 5 deletions Source/Mosa.Compiler.Framework/Counters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
13 changes: 5 additions & 8 deletions Source/Mosa.Compiler.Framework/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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)");
Expand Down
16 changes: 8 additions & 8 deletions Source/Mosa.Compiler.Framework/MethodScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit df7d02b

Please sign in to comment.