Skip to content

Commit 2f4b0e9

Browse files
committedJan 26, 2016
Merge pull request #313 from tgiphil/master
Added per method counter statistics
2 parents 94463b1 + 159eb53 commit 2f4b0e9

18 files changed

+379
-266
lines changed
 

‎Source/Mosa.Compiler.Framework/BaseCompiler.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public abstract class BaseCompiler
6565
/// <summary>
6666
/// Gets the counters.
6767
/// </summary>
68-
public Counters Counters { get; private set; }
68+
public Counters GlobalCounters { get; private set; }
6969

7070
/// <summary>
7171
/// Gets the scheduler.
@@ -126,9 +126,9 @@ public void Initialize(MosaCompiler compiler)
126126

127127
PreCompilePipeline = new CompilerPipeline();
128128
PostCompilePipeline = new CompilerPipeline();
129-
Counters = new Counters();
129+
GlobalCounters = new Counters();
130130
PlugSystem = new PlugSystem();
131-
CompilerData = new CompilerData(this);
131+
CompilerData = new CompilerData();
132132

133133
// Create new dictionary
134134
IntrinsicTypes = new Dictionary<string, Type>();
@@ -350,14 +350,22 @@ internal void PostCompile()
350350
NewCompilerTraceEvent(CompilerEvent.CompilerStageEnd, stage.Name);
351351
}
352352

353+
// TODO: Add compiler option
354+
355+
// Sum up the counters
356+
foreach (var methodData in CompilerData.MethodData)
357+
{
358+
GlobalCounters.Merge(methodData.Counters);
359+
}
360+
353361
ExportCounters();
354362
}
355363

356364
#endregion Methods
357365

358366
protected void ExportCounters()
359367
{
360-
foreach (var counter in Counters.Export())
368+
foreach (var counter in GlobalCounters.Export())
361369
{
362370
NewCompilerTraceEvent(CompilerEvent.Counter, counter);
363371
}
@@ -392,7 +400,7 @@ protected void NewCompilerTraceEvent(CompilerEvent compilerEvent, string message
392400
/// <param name="count">The count.</param>
393401
protected void UpdateCounter(string name, int count)
394402
{
395-
Counters.UpdateCounter(name, count);
403+
GlobalCounters.Update(name, count);
396404
}
397405

398406
protected MosaType GetPlatformInternalRuntimeType()

‎Source/Mosa.Compiler.Framework/BaseInstruction.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public string ToString(InstructionNode node)
160160
{
161161
var op = node.GetResult(i);
162162
sb.Append(" ");
163-
sb.Append(op == null ? "[NULL]" : op.ToString());
163+
sb.Append(op == null ? "[NULL]" : op.ToString(true));
164164
sb.Append(",");
165165
}
166166

@@ -178,7 +178,7 @@ public string ToString(InstructionNode node)
178178
{
179179
var op = node.GetOperand(i);
180180
sb.Append(" ");
181-
sb.Append(op == null ? "[NULL]" : op.ToString());
181+
sb.Append(op == null ? "[NULL]" : op.ToString(true));
182182
sb.Append(",");
183183
}
184184

@@ -246,7 +246,7 @@ protected virtual string GetModifier(InstructionNode node)
246246
/// <summary>
247247
/// Gets the condition string.
248248
/// </summary>
249-
/// <param name="conditioncode">The conditioncode.</param>
249+
/// <param name="conditioncode">The condition code.</param>
250250
/// <returns></returns>
251251
protected string GetConditionString(ConditionCode conditioncode)
252252
{

‎Source/Mosa.Compiler.Framework/BaseMethodCompiler.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ public class BaseMethodCompiler
156156
/// </value>
157157
public int ThreadID { get; private set; }
158158

159+
/// <summary>
160+
/// Gets the method data.
161+
/// </summary>
162+
/// <value>
163+
/// The method data.
164+
/// </value>
165+
public CompilerMethodData MethodData { get; private set; }
166+
159167
#endregion Properties
160168

161169
#region Construction
@@ -175,8 +183,8 @@ protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBloc
175183
Scheduler = compiler.CompilationScheduler;
176184
Architecture = compiler.Architecture;
177185
TypeSystem = compiler.TypeSystem;
178-
TypeLayout = Compiler.TypeLayout;
179-
Trace = Compiler.CompilerTrace;
186+
TypeLayout = compiler.TypeLayout;
187+
Trace = compiler.CompilerTrace;
180188
Linker = compiler.Linker;
181189
BasicBlocks = basicBlocks ?? new BasicBlocks();
182190
Pipeline = new CompilerPipeline();
@@ -185,9 +193,12 @@ protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBloc
185193
LocalVariables = emptyOperandList;
186194
ThreadID = threadID;
187195
DominanceAnalysis = new Dominance(Compiler.CompilerOptions.DominanceAnalysisFactory, BasicBlocks);
188-
PluggedMethod = Compiler.PlugSystem.GetPlugMethod(Method);
196+
PluggedMethod = compiler.PlugSystem.GetPlugMethod(Method);
189197
stop = false;
190198

199+
MethodData = compiler.CompilerData.GetCompilerMethodData(Method);
200+
MethodData.Counters.Clear();
201+
191202
EvaluateParameterOperands();
192203
}
193204

@@ -202,7 +213,7 @@ protected void EvaluateParameterOperands()
202213
{
203214
int index = 0;
204215

205-
//FIXME! Note: displacement is recalculated later
216+
// Note: displacement is recalculated later
206217
int displacement = 4;
207218

208219
if (Method.HasThis || Method.HasExplicitThis)
@@ -249,6 +260,10 @@ public void Compile()
249260

250261
InitializeType();
251262

263+
var log = new TraceLog(TraceType.Counters, this.Method, string.Empty, Trace.TraceFilter.Active);
264+
log.Log(MethodData.Counters.Export());
265+
Trace.TraceListener.OnNewTraceLog(log);
266+
252267
EndCompile();
253268
}
254269

‎Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public abstract class BaseMethodCompilerStage : IMethodCompilerStage, ITraceFact
8686
/// </value>
8787
protected InstructionSize NativeInstructionSize { get; private set; }
8888

89+
/// <summary>
90+
/// Gets the method data.
91+
/// </summary>
92+
/// <value>
93+
/// The method data.
94+
/// </value>
95+
protected CompilerMethodData MethodData { get; private set; }
96+
8997
#endregion Properties
9098

9199
#region IPipelineStage Members
@@ -112,11 +120,12 @@ void IMethodCompilerStage.Initialize(BaseMethodCompiler compiler)
112120
TypeSystem = compiler.TypeSystem;
113121
TypeLayout = compiler.TypeLayout;
114122
CallingConvention = Architecture.CallingConvention;
115-
116123
NativePointerSize = Architecture.NativePointerSize;
117124
NativeAlignment = Architecture.NativeAlignment;
118125
NativeInstructionSize = Architecture.NativeInstructionSize;
119126

127+
MethodData = MethodCompiler.MethodData;
128+
120129
traceLogs = new List<TraceLog>();
121130

122131
Setup();
@@ -565,7 +574,7 @@ protected void NewCompilerTraceEvent(CompilerEvent compileEvent, string message)
565574
/// <param name="count">The count.</param>
566575
public void UpdateCounter(string name, int count)
567576
{
568-
MethodCompiler.Compiler.Counters.UpdateCounter(name, count);
577+
MethodData.Counters.Update(name, count);
569578
}
570579

571580
/// <summary>

‎Source/Mosa.Compiler.Framework/CompilerData.cs

+18-21
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,35 @@ public sealed class CompilerData
1313
{
1414
#region Data Members
1515

16-
private Dictionary<MosaType, CompilerTypeData> compilerTypes = new Dictionary<MosaType, CompilerTypeData>();
16+
private Dictionary<MosaType, CompilerTypeData> types = new Dictionary<MosaType, CompilerTypeData>();
1717

18-
private Dictionary<MosaMethod, CompilerMethodData> compilerMethods = new Dictionary<MosaMethod, CompilerMethodData>();
18+
private Dictionary<MosaMethod, CompilerMethodData> methods = new Dictionary<MosaMethod, CompilerMethodData>();
1919

2020
#endregion Data Members
2121

22-
#region Properties
23-
24-
public BaseCompiler Compiler { get; private set; }
25-
26-
#endregion Properties
27-
28-
#region Methods
29-
30-
public CompilerData(BaseCompiler compiler)
22+
public IEnumerable<CompilerMethodData> MethodData
3123
{
32-
if (compiler == null)
33-
throw new ArgumentNullException("compiler");
34-
35-
Compiler = compiler;
24+
get
25+
{
26+
foreach (var method in methods)
27+
{
28+
yield return method.Value;
29+
}
30+
}
3631
}
3732

33+
#region Methods
34+
3835
public CompilerTypeData GetCompilerTypeData(MosaType type)
3936
{
40-
lock (compilerTypes)
37+
lock (types)
4138
{
4239
CompilerTypeData compilerType;
4340

44-
if (!compilerTypes.TryGetValue(type, out compilerType))
41+
if (!types.TryGetValue(type, out compilerType))
4542
{
4643
compilerType = new CompilerTypeData(type);
47-
compilerTypes.Add(type, compilerType);
44+
types.Add(type, compilerType);
4845
}
4946

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

5451
public CompilerMethodData GetCompilerMethodData(MosaMethod method)
5552
{
56-
lock (compilerMethods)
53+
lock (methods)
5754
{
5855
CompilerMethodData compilerMethod;
5956

60-
if (!compilerMethods.TryGetValue(method, out compilerMethod))
57+
if (!methods.TryGetValue(method, out compilerMethod))
6158
{
6259
compilerMethod = new CompilerMethodData(method);
63-
compilerMethods.Add(method, compilerMethod);
60+
methods.Add(method, compilerMethod);
6461
}
6562

6663
return compilerMethod;

‎Source/Mosa.Compiler.Framework/CompilerMethodData.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Mosa.Compiler.Framework
1212
/// </summary>
1313
public sealed class CompilerMethodData
1414
{
15+
public Counters Counters { get; private set; }
16+
1517
#region Properties
1618

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

5355
#endregion Properties
5456

55-
#region Methods
56-
5757
public CompilerMethodData(MosaMethod mosaMethod)
5858
{
5959
if (mosaMethod == null)
@@ -63,9 +63,12 @@ public CompilerMethodData(MosaMethod mosaMethod)
6363

6464
Calls = new List<MosaMethod>();
6565
CalledBy = new List<MosaMethod>();
66+
Counters = new Counters();
6667
CompileCount = 0;
6768
}
6869

70+
#region Methods
71+
6972
public void AddCalledBy(MosaMethod method)
7073
{
7174
lock (this)

‎Source/Mosa.Compiler.Framework/Counters.cs

+29-9
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ namespace Mosa.Compiler.Framework
77
/// <summary>
88
///
99
/// </summary>
10-
public class Counters
10+
public sealed class Counters
1111
{
12-
protected Dictionary<string, int> counters = new Dictionary<string, int>();
12+
private Dictionary<string, int> counters;
1313

14-
public Counters()
15-
{ }
14+
public void Clear()
15+
{
16+
if (counters != null)
17+
counters.Clear();
18+
}
1619

17-
public void UpdateCounter(string name, int count)
20+
public void Update(string name, int count)
1821
{
19-
lock (counters)
22+
lock (this)
2023
{
24+
if (counters == null)
25+
counters = new Dictionary<string, int>();
26+
2127
if (counters.ContainsKey(name))
2228
counters[name] = counters[name] + count;
2329
else
@@ -27,14 +33,28 @@ public void UpdateCounter(string name, int count)
2733

2834
public IList<string> Export()
2935
{
30-
List<string> counts = new List<string>();
36+
var counts = new List<string>();
3137

32-
foreach (var item in counters)
38+
if (counters != null)
3339
{
34-
counts.Add(item.Key + ": " + item.Value.ToString());
40+
foreach (var item in counters)
41+
{
42+
counts.Add(item.Key + ": " + item.Value.ToString());
43+
}
3544
}
3645

3746
return counts;
3847
}
48+
49+
public void Merge(Counters counters)
50+
{
51+
if (counters.counters != null)
52+
{
53+
foreach (var item in counters.counters)
54+
{
55+
Update(item.Key, item.Value);
56+
}
57+
}
58+
}
3959
}
4060
}

‎Source/Mosa.Compiler.Framework/Operand.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,10 @@ public string ToString(bool full)
10221022
{
10231023
sb.AppendFormat(" [{0}]", Type.FullName);
10241024
}
1025+
else
1026+
{
1027+
//sb.AppendFormat(" [{0}]", Type.FullName);
1028+
}
10251029

10261030
return sb.ToString().Replace(" ", " ").Trim();
10271031
}

0 commit comments

Comments
 (0)
Please sign in to comment.