Skip to content

Commit

Permalink
Merge pull request #313 from tgiphil/master
Browse files Browse the repository at this point in the history
Added per method counter statistics
  • Loading branch information
tgiphil committed Jan 26, 2016
2 parents 94463b1 + 159eb53 commit 2f4b0e9
Show file tree
Hide file tree
Showing 18 changed files with 379 additions and 266 deletions.
18 changes: 13 additions & 5 deletions Source/Mosa.Compiler.Framework/BaseCompiler.cs
Expand Up @@ -65,7 +65,7 @@ public abstract class BaseCompiler
/// <summary>
/// Gets the counters.
/// </summary>
public Counters Counters { get; private set; }
public Counters GlobalCounters { get; private set; }

/// <summary>
/// Gets the scheduler.
Expand Down Expand Up @@ -126,9 +126,9 @@ public void Initialize(MosaCompiler compiler)

PreCompilePipeline = new CompilerPipeline();
PostCompilePipeline = new CompilerPipeline();
Counters = new Counters();
GlobalCounters = new Counters();
PlugSystem = new PlugSystem();
CompilerData = new CompilerData(this);
CompilerData = new CompilerData();

// Create new dictionary
IntrinsicTypes = new Dictionary<string, Type>();
Expand Down Expand Up @@ -350,14 +350,22 @@ internal void PostCompile()
NewCompilerTraceEvent(CompilerEvent.CompilerStageEnd, stage.Name);
}

// TODO: Add compiler option

// Sum up the counters
foreach (var methodData in CompilerData.MethodData)
{
GlobalCounters.Merge(methodData.Counters);
}

ExportCounters();
}

#endregion Methods

protected void ExportCounters()
{
foreach (var counter in Counters.Export())
foreach (var counter in GlobalCounters.Export())
{
NewCompilerTraceEvent(CompilerEvent.Counter, counter);
}
Expand Down Expand Up @@ -392,7 +400,7 @@ protected void NewCompilerTraceEvent(CompilerEvent compilerEvent, string message
/// <param name="count">The count.</param>
protected void UpdateCounter(string name, int count)
{
Counters.UpdateCounter(name, count);
GlobalCounters.Update(name, count);
}

protected MosaType GetPlatformInternalRuntimeType()
Expand Down
6 changes: 3 additions & 3 deletions Source/Mosa.Compiler.Framework/BaseInstruction.cs
Expand Up @@ -160,7 +160,7 @@ public string ToString(InstructionNode node)
{
var op = node.GetResult(i);
sb.Append(" ");
sb.Append(op == null ? "[NULL]" : op.ToString());
sb.Append(op == null ? "[NULL]" : op.ToString(true));
sb.Append(",");
}

Expand All @@ -178,7 +178,7 @@ public string ToString(InstructionNode node)
{
var op = node.GetOperand(i);
sb.Append(" ");
sb.Append(op == null ? "[NULL]" : op.ToString());
sb.Append(op == null ? "[NULL]" : op.ToString(true));
sb.Append(",");
}

Expand Down Expand Up @@ -246,7 +246,7 @@ protected virtual string GetModifier(InstructionNode node)
/// <summary>
/// Gets the condition string.
/// </summary>
/// <param name="conditioncode">The conditioncode.</param>
/// <param name="conditioncode">The condition code.</param>
/// <returns></returns>
protected string GetConditionString(ConditionCode conditioncode)
{
Expand Down
23 changes: 19 additions & 4 deletions Source/Mosa.Compiler.Framework/BaseMethodCompiler.cs
Expand Up @@ -156,6 +156,14 @@ public class BaseMethodCompiler
/// </value>
public int ThreadID { get; private set; }

/// <summary>
/// Gets the method data.
/// </summary>
/// <value>
/// The method data.
/// </value>
public CompilerMethodData MethodData { get; private set; }

#endregion Properties

#region Construction
Expand All @@ -175,8 +183,8 @@ protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBloc
Scheduler = compiler.CompilationScheduler;
Architecture = compiler.Architecture;
TypeSystem = compiler.TypeSystem;
TypeLayout = Compiler.TypeLayout;
Trace = Compiler.CompilerTrace;
TypeLayout = compiler.TypeLayout;
Trace = compiler.CompilerTrace;
Linker = compiler.Linker;
BasicBlocks = basicBlocks ?? new BasicBlocks();
Pipeline = new CompilerPipeline();
Expand All @@ -185,9 +193,12 @@ protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBloc
LocalVariables = emptyOperandList;
ThreadID = threadID;
DominanceAnalysis = new Dominance(Compiler.CompilerOptions.DominanceAnalysisFactory, BasicBlocks);
PluggedMethod = Compiler.PlugSystem.GetPlugMethod(Method);
PluggedMethod = compiler.PlugSystem.GetPlugMethod(Method);
stop = false;

MethodData = compiler.CompilerData.GetCompilerMethodData(Method);
MethodData.Counters.Clear();

EvaluateParameterOperands();
}

Expand All @@ -202,7 +213,7 @@ protected void EvaluateParameterOperands()
{
int index = 0;

//FIXME! Note: displacement is recalculated later
// Note: displacement is recalculated later
int displacement = 4;

if (Method.HasThis || Method.HasExplicitThis)
Expand Down Expand Up @@ -249,6 +260,10 @@ public void Compile()

InitializeType();

var log = new TraceLog(TraceType.Counters, this.Method, string.Empty, Trace.TraceFilter.Active);
log.Log(MethodData.Counters.Export());
Trace.TraceListener.OnNewTraceLog(log);

EndCompile();
}

Expand Down
13 changes: 11 additions & 2 deletions Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Expand Up @@ -86,6 +86,14 @@ public abstract class BaseMethodCompilerStage : IMethodCompilerStage, ITraceFact
/// </value>
protected InstructionSize NativeInstructionSize { get; private set; }

/// <summary>
/// Gets the method data.
/// </summary>
/// <value>
/// The method data.
/// </value>
protected CompilerMethodData MethodData { get; private set; }

#endregion Properties

#region IPipelineStage Members
Expand All @@ -112,11 +120,12 @@ void IMethodCompilerStage.Initialize(BaseMethodCompiler compiler)
TypeSystem = compiler.TypeSystem;
TypeLayout = compiler.TypeLayout;
CallingConvention = Architecture.CallingConvention;

NativePointerSize = Architecture.NativePointerSize;
NativeAlignment = Architecture.NativeAlignment;
NativeInstructionSize = Architecture.NativeInstructionSize;

MethodData = MethodCompiler.MethodData;

traceLogs = new List<TraceLog>();

Setup();
Expand Down Expand Up @@ -565,7 +574,7 @@ protected void NewCompilerTraceEvent(CompilerEvent compileEvent, string message)
/// <param name="count">The count.</param>
public void UpdateCounter(string name, int count)
{
MethodCompiler.Compiler.Counters.UpdateCounter(name, count);
MethodData.Counters.Update(name, count);
}

/// <summary>
Expand Down
39 changes: 18 additions & 21 deletions Source/Mosa.Compiler.Framework/CompilerData.cs
Expand Up @@ -13,38 +13,35 @@ public sealed class CompilerData
{
#region Data Members

private Dictionary<MosaType, CompilerTypeData> compilerTypes = new Dictionary<MosaType, CompilerTypeData>();
private Dictionary<MosaType, CompilerTypeData> types = new Dictionary<MosaType, CompilerTypeData>();

private Dictionary<MosaMethod, CompilerMethodData> compilerMethods = new Dictionary<MosaMethod, CompilerMethodData>();
private Dictionary<MosaMethod, CompilerMethodData> methods = new Dictionary<MosaMethod, CompilerMethodData>();

#endregion Data Members

#region Properties

public BaseCompiler Compiler { get; private set; }

#endregion Properties

#region Methods

public CompilerData(BaseCompiler compiler)
public IEnumerable<CompilerMethodData> MethodData
{
if (compiler == null)
throw new ArgumentNullException("compiler");

Compiler = compiler;
get
{
foreach (var method in methods)
{
yield return method.Value;
}
}
}

#region Methods

public CompilerTypeData GetCompilerTypeData(MosaType type)
{
lock (compilerTypes)
lock (types)
{
CompilerTypeData compilerType;

if (!compilerTypes.TryGetValue(type, out compilerType))
if (!types.TryGetValue(type, out compilerType))
{
compilerType = new CompilerTypeData(type);
compilerTypes.Add(type, compilerType);
types.Add(type, compilerType);
}

return compilerType;
Expand All @@ -53,14 +50,14 @@ public CompilerTypeData GetCompilerTypeData(MosaType type)

public CompilerMethodData GetCompilerMethodData(MosaMethod method)
{
lock (compilerMethods)
lock (methods)
{
CompilerMethodData compilerMethod;

if (!compilerMethods.TryGetValue(method, out compilerMethod))
if (!methods.TryGetValue(method, out compilerMethod))
{
compilerMethod = new CompilerMethodData(method);
compilerMethods.Add(method, compilerMethod);
methods.Add(method, compilerMethod);
}

return compilerMethod;
Expand Down
7 changes: 5 additions & 2 deletions Source/Mosa.Compiler.Framework/CompilerMethodData.cs
Expand Up @@ -12,6 +12,8 @@ namespace Mosa.Compiler.Framework
/// </summary>
public sealed class CompilerMethodData
{
public Counters Counters { get; private set; }

#region Properties

public MosaMethod Method { get; private set; }
Expand Down Expand Up @@ -52,8 +54,6 @@ public sealed class CompilerMethodData

#endregion Properties

#region Methods

public CompilerMethodData(MosaMethod mosaMethod)
{
if (mosaMethod == null)
Expand All @@ -63,9 +63,12 @@ public CompilerMethodData(MosaMethod mosaMethod)

Calls = new List<MosaMethod>();
CalledBy = new List<MosaMethod>();
Counters = new Counters();
CompileCount = 0;
}

#region Methods

public void AddCalledBy(MosaMethod method)
{
lock (this)
Expand Down
38 changes: 29 additions & 9 deletions Source/Mosa.Compiler.Framework/Counters.cs
Expand Up @@ -7,17 +7,23 @@ namespace Mosa.Compiler.Framework
/// <summary>
///
/// </summary>
public class Counters
public sealed class Counters
{
protected Dictionary<string, int> counters = new Dictionary<string, int>();
private Dictionary<string, int> counters;

public Counters()
{ }
public void Clear()
{
if (counters != null)
counters.Clear();
}

public void UpdateCounter(string name, int count)
public void Update(string name, int count)
{
lock (counters)
lock (this)
{
if (counters == null)
counters = new Dictionary<string, int>();

if (counters.ContainsKey(name))
counters[name] = counters[name] + count;
else
Expand All @@ -27,14 +33,28 @@ public void UpdateCounter(string name, int count)

public IList<string> Export()
{
List<string> counts = new List<string>();
var counts = new List<string>();

foreach (var item in counters)
if (counters != null)
{
counts.Add(item.Key + ": " + item.Value.ToString());
foreach (var item in counters)
{
counts.Add(item.Key + ": " + item.Value.ToString());
}
}

return counts;
}

public void Merge(Counters counters)
{
if (counters.counters != null)
{
foreach (var item in counters.counters)
{
Update(item.Key, item.Value);
}
}
}
}
}
4 changes: 4 additions & 0 deletions Source/Mosa.Compiler.Framework/Operand.cs
Expand Up @@ -1022,6 +1022,10 @@ public string ToString(bool full)
{
sb.AppendFormat(" [{0}]", Type.FullName);
}
else
{
//sb.AppendFormat(" [{0}]", Type.FullName);
}

return sb.ToString().Replace(" ", " ").Trim();
}
Expand Down

0 comments on commit 2f4b0e9

Please sign in to comment.