-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathInnerLoopMsBuildParser.cs
172 lines (154 loc) · 7.75 KB
/
InnerLoopMsBuildParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using Microsoft.Diagnostics.Tracing;
using Reporting;
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
namespace ScenarioMeasurement
{
public class InnerLoopMsBuildParser : IParser
{
public void EnableKernelProvider(ITraceSession kernel)
{
kernel.EnableKernelProvider(TraceSessionManager.KernelKeyword.Process, TraceSessionManager.KernelKeyword.Thread, TraceSessionManager.KernelKeyword.ContextSwitch);
}
public void EnableUserProviders(ITraceSession user)
{
user.EnableUserProvider("InnerLoopMarkerEventSource", TraceEventLevel.Verbose);
user.EnableUserProvider("Microsoft-Build", TraceEventLevel.Verbose);
}
public IEnumerable<Counter> Parse(string mergeTraceFile, string processName, IList<int> pids, string commandLine)
{
var results = new List<double>();
var threadTimes = new List<double>();
var buildEvalTime = new List<double>();
double threadTime = 0;
var ins = new Dictionary<int, double>();
double start = -1;
double buildEvalStart = -1;
int? pid = null;
Dictionary<string, List<double>> firstRun = new Dictionary<string, List<double>>();
Dictionary<string, List<double>> secondRun = new Dictionary<string, List<double>>();
Dictionary<string, List<double>> currentRun = firstRun;
using (var source = new TraceSourceManager(mergeTraceFile))
{
source.Kernel.ProcessStart += evt =>
{
if (!pid.HasValue && ParserUtility.MatchProcessStart(evt, source, processName, pids, commandLine))
{
pid = evt.ProcessID;
start = evt.TimeStampRelativeMSec;
}
};
if (source.IsWindows)
{
((ETWTraceEventSource)source.Source).Kernel.ThreadCSwitch += evt =>
{
if (!pid.HasValue) // we're currently in a measurement interval
return;
if (evt.NewProcessID != pid && evt.OldProcessID != pid)
return; // but this isn't our process
if (evt.OldProcessID == pid) // this is a switch out from our process
{
if (ins.TryGetValue(evt.OldThreadID, out var value)) // had we ever recorded a switch in for this thread?
{
threadTime += evt.TimeStampRelativeMSec - value;
ins.Remove(evt.OldThreadID);
}
}
else // this is a switch in to our process
{
ins[evt.NewThreadID] = evt.TimeStampRelativeMSec;
}
};
}
source.Kernel.ProcessStop += evt =>
{
if (pid.HasValue && ParserUtility.MatchSingleProcessID(evt, source, (int)pid))
{
if(!currentRun.ContainsKey(evt.EventName))
{
currentRun.Add(evt.EventName, new List<double>());
currentRun[evt.EventName].Add(evt.TimeStampRelativeMSec - start);
}
else
{
currentRun[evt.EventName].Add(evt.TimeStampRelativeMSec - start);
}
results.Add(evt.TimeStampRelativeMSec - start);
pid = null;
start = 0;
if (source.IsWindows)
{
if(!currentRun.ContainsKey("ThreadCSwitch"))
{
currentRun.Add("ThreadCSwitch", new List<double>());
currentRun["ThreadCSwitch"].Add(threadTime);
}
else
{
currentRun["ThreadCSwitch"].Add(threadTime);
}
threadTimes.Add(threadTime);
threadTime = 0;
}
}
};
source.Source.Dynamic.AddCallbackForProviderEvent("Microsoft-Build", "Evaluate/Start", evt =>
{
if(pid.HasValue && evt.ProcessID == pid.Value)
{
buildEvalStart = evt.TimeStampRelativeMSec;
}
});
source.Source.Dynamic.AddCallbackForProviderEvent("Microsoft-Build", "EvaluateStop/Stop", evt =>
{
if(pid.HasValue && evt.ProcessID == pid.Value)
{
if(!currentRun.ContainsKey(evt.EventName))
{
currentRun.Add(evt.EventName, new List<double>());
currentRun[evt.EventName].Add(evt.TimeStampRelativeMSec - buildEvalStart);
}
else
{
currentRun[evt.EventName].Add(evt.TimeStampRelativeMSec - buildEvalStart);
}
}
});
source.Source.Dynamic.AddCallbackForProviderEvent("InnerLoopMarkerEventSource", "Split", evt =>
{
currentRun = secondRun;
});
source.Source.Dynamic.AddCallbackForProviderEvent("InnerLoopMarkerEventSource", "EndIteration", evt =>
{
currentRun = firstRun;
});
source.Process();
}
List<double> diffGS = new List<double>();
List<double> diffTOT = new List<double>();
List<double> diffEBT = new List<double>();
for(int i = 0; i < firstRun["Process/Stop"].Count; i++)
{
diffGS.Add(firstRun["Process/Stop"][i] - secondRun["Process/Stop"][i]);
}
for(int i = 0; i < firstRun["ThreadCSwitch"].Count; i++)
{
diffTOT.Add(firstRun["ThreadCSwitch"][i] - secondRun["ThreadCSwitch"][i]);
}
for(int i = 0; i < firstRun["EvaluateStop/Stop"].Count; i++)
{
diffEBT.Add(firstRun["EvaluateStop/Stop"][i] - secondRun["EvaluateStop/Stop"][i]);
}
return new[] {
new Counter() { Name = "Generic Startup First Run", MetricName = "ms", TopCounter=true, Results = firstRun["Process/Stop"].ToArray() },
new Counter() { Name = "Generic Startup Second Run", MetricName = "ms", TopCounter=true, Results = secondRun["Process/Stop"].ToArray() },
new Counter() { Name = "Generic Startup Diff", MetricName = "ms", DefaultCounter=true, TopCounter=true, Results = diffGS.ToArray() },
new Counter() { Name = "Time on Thread Diff", MetricName = "ms", TopCounter=true, Results = diffTOT.ToArray() },
new Counter() { Name = "Build Evaluate Time First Run", MetricName = "ms", TopCounter=true, Results = firstRun["EvaluateStop/Stop"].ToArray() },
new Counter() { Name = "Build Evaluate Time Second Run", MetricName = "ms", TopCounter=true, Results = secondRun["EvaluateStop/Stop"].ToArray() },
new Counter() { Name = "Build Evaluate Time Diff", MetricName = "ms", TopCounter=true, Results = diffEBT.ToArray() }
};
}
}
}