/
BuildThreadMonitor.cs
353 lines (303 loc) · 13.1 KB
/
BuildThreadMonitor.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Xenko.Core.Diagnostics;
using Xenko.Core.MicroThreading;
using System.ServiceModel;
using Xenko.Core.BuildEngine;
namespace Xenko.Core.Assets.CompilerApp
{
internal class BuildThreadMonitor : IBuildThreadMonitor
{
/// <summary>
/// This class stores informations relative to a build step, used to build data to send to the clients. It is immutable.
/// </summary>
private class BuildStepInfo
{
public readonly BuildStep BuildStep;
public readonly long ExecutionId;
public readonly string Description;
public readonly TimestampLocalLogger Logger;
public bool HasBeenSend { get; private set; }
public BuildStepInfo(BuildStep buildStep, long executionId, string description, TimestampLocalLogger logger)
{
BuildStep = buildStep;
ExecutionId = executionId;
Description = description;
Logger = logger;
HasBeenSend = false;
}
public void BuildStepSent()
{
HasBeenSend = true;
}
public override string ToString()
{
return "[" + ExecutionId + "] " + BuildStep;
}
}
private readonly Dictionary<int, List<TimeInterval>> threadExecutionIntervals = new Dictionary<int, List<TimeInterval>>();
private readonly List<BuildStepInfo> buildStepInfos = new List<BuildStepInfo>();
private readonly List<BuildStepInfo> buildStepInfosToSend = new List<BuildStepInfo>();
private readonly List<long> buildStepResultsToSend = new List<long>();
private readonly List<MicrothreadNotification> microthreadNotifications = new List<MicrothreadNotification>();
private readonly Guid builderId;
private readonly string monitorPipeName;
private readonly Stopwatch stopWatch = new Stopwatch();
private DateTime startTime;
// Datetime's ticks are hardware-independent and 100ns long
private static readonly double TickFactor = 10000000.0 / Stopwatch.Frequency;
private IBuildMonitorRemote buildMonitorRemote;
private bool running;
private Thread monitorThread;
public BuildThreadMonitor(Scheduler scheduler, Guid builderId, string monitorPipeName = Builder.MonitorPipeName)
{
this.monitorPipeName = monitorPipeName;
this.builderId = builderId;
scheduler.MicroThreadStarted += MicroThreadStarted;
scheduler.MicroThreadEnded += MicroThreadEnded;
scheduler.MicroThreadCallbackStart += MicroThreadCallbackStart;
scheduler.MicroThreadCallbackEnd += MicroThreadCallbackEnd;
stopWatch.Start();
}
public void RegisterThread(int threadId)
{
lock (threadExecutionIntervals)
{
threadExecutionIntervals.Add(threadId, new List<TimeInterval>());
}
}
public void RegisterBuildStep(BuildStep buildStep, TimestampLocalLogger logger)
{
lock (buildStepInfosToSend)
{
buildStepInfosToSend.Add(new BuildStepInfo(buildStep, buildStep.ExecutionId, buildStep.Description, logger));
}
}
public void Start()
{
startTime = DateTime.Now;
running = true;
monitorThread = new Thread(SafeAction.Wrap(() =>
{
if (TryConnectMonitor())
buildMonitorRemote.StartBuild(builderId, startTime);
int delay = 300;
while (running)
{
Thread.Sleep(delay);
delay = SendThreadUpdate() ? 300 : 1000;
}
SendThreadUpdate();
if (TryConnectMonitor())
buildMonitorRemote.EndBuild(builderId, DateTime.Now);
try
{
// ReSharper disable SuspiciousTypeConversion.Global
var communicationObj = buildMonitorRemote as ICommunicationObject;
// ReSharper restore SuspiciousTypeConversion.Global
if (communicationObj != null)
communicationObj.Close();
}
// We don't know the layer to close under the client channel so it might throw potentially any exception.
// Let's ignore them all because at this step we're just cleaning up things.
// ReSharper disable EmptyGeneralCatchClause
catch
// ReSharper restore EmptyGeneralCatchClause
{ }
}))
{ IsBackground = true, Name = "Monitor Thread" };
monitorThread.Start();
}
public void Finish()
{
running = false;
}
public void Join()
{
if (monitorThread != null)
{
monitorThread.Join();
monitorThread = null;
}
}
private bool TryConnectMonitor()
{
if (buildMonitorRemote == null)
{
try
{
var namedPipeBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { SendTimeout = TimeSpan.FromSeconds(300.0) };
buildMonitorRemote = ChannelFactory<IBuildMonitorRemote>.CreateChannel(namedPipeBinding, new EndpointAddress(monitorPipeName));
buildMonitorRemote.Ping();
}
catch (EndpointNotFoundException)
{
buildMonitorRemote = null;
}
}
return buildMonitorRemote != null;
}
private bool SendThreadUpdate()
{
if (!TryConnectMonitor())
return false;
// Update local info
var localMicroThreadNotifications = new List<MicrothreadNotification>();
var localBuildStepResultsToSend = new List<long>();
lock (microthreadNotifications)
{
localMicroThreadNotifications.AddRange(microthreadNotifications);
microthreadNotifications.Clear();
}
lock (buildStepInfosToSend)
{
buildStepInfos.AddRange(buildStepInfosToSend);
buildStepInfosToSend.Clear();
}
lock (buildStepResultsToSend)
{
localBuildStepResultsToSend.AddRange(buildStepResultsToSend);
buildStepResultsToSend.Clear();
}
try
{
// Sending BuildStep view model
foreach (var buildStepInfo in buildStepInfos.Where(x => !x.HasBeenSend))
{
buildMonitorRemote.SendBuildStepInfo(builderId, buildStepInfo.ExecutionId, buildStepInfo.Description, startTime);
buildStepInfo.BuildStepSent();
}
buildMonitorRemote.SendMicrothreadEvents(builderId, startTime, DateTime.Now, localMicroThreadNotifications);
// Sending log message
foreach (var buildStepInfo in buildStepInfos)
{
if (buildStepInfo.Logger != null)
{
TimestampLocalLogger.Message[] messages = null;
lock (buildStepInfo.Logger)
{
if (buildStepInfo.Logger.Messages.Count > 0)
{
messages = buildStepInfo.Logger.Messages.ToArray();
buildStepInfo.Logger.Messages.Clear();
}
}
if (messages != null)
{
try
{
var serializableMessages = (messages.Select(x => new SerializableTimestampLogMessage(x))).ToList();
buildMonitorRemote.SendCommandLog(builderId, startTime, buildStepInfo.ExecutionId, serializableMessages);
}
catch (Exception)
{
lock (buildStepInfo.Logger)
{
buildStepInfo.Logger.Messages.InsertRange(0, messages);
}
throw;
}
}
}
}
// Sending BuildStep results
for (int i = localBuildStepResultsToSend.Count - 1; i >= 0; --i)
{
long microthreadId = localBuildStepResultsToSend[i];
BuildStepInfo stepInfo = buildStepInfos.SingleOrDefault(x => x.ExecutionId == microthreadId);
if (stepInfo != null && stepInfo.BuildStep != null)
{
buildMonitorRemote.SendBuildStepResult(builderId, startTime, microthreadId, stepInfo.BuildStep.Status);
localBuildStepResultsToSend.RemoveAt(i);
}
}
}
catch (Exception)
{
lock (microthreadNotifications)
{
microthreadNotifications.AddRange(localMicroThreadNotifications);
}
buildMonitorRemote = null;
}
finally
{
if (localBuildStepResultsToSend.Count > 0)
{
lock (buildStepResultsToSend)
{
buildStepResultsToSend.AddRange(localBuildStepResultsToSend);
}
}
}
return buildMonitorRemote != null;
}
private void MicroThreadStarted(object sender, SchedulerThreadEventArgs e)
{
// Not useful anymore? Let's do nothing for the moment
}
private void MicroThreadEnded(object sender, SchedulerThreadEventArgs e)
{
lock (buildStepResultsToSend)
{
buildStepResultsToSend.Add(e.MicroThread.Id);
}
}
private void MicroThreadCallbackStart(object sender, SchedulerThreadEventArgs e)
{
TimeInterval timeInterval;
int intervalCount;
lock (threadExecutionIntervals)
{
List<TimeInterval> intervals = threadExecutionIntervals[e.ThreadId];
if (intervals.Count > 0 && !intervals.Last().HasEnded)
throw new InvalidOperationException("Starting a new microthread on a thread still running another microthread.");
timeInterval = new TimeInterval(GetTicksFromStopwatch());
intervals.Add(timeInterval);
intervalCount = intervals.Count;
}
// Rely on intervals.Count, so must be called after intervals.Add!
long jobId = GetMicrothreadJobIdFromThreadInfo(e.ThreadId, intervalCount);
var jobInfo = new MicrothreadNotification(e.ThreadId, e.MicroThread.Id, jobId, timeInterval.StartTime, MicrothreadNotification.NotificationType.JobStarted);
lock (microthreadNotifications)
{
microthreadNotifications.Add(jobInfo);
}
}
private void MicroThreadCallbackEnd(object sender, SchedulerThreadEventArgs e)
{
long endTime = GetTicksFromStopwatch();
int intervalCount;
lock (threadExecutionIntervals)
{
List<TimeInterval> intervals = threadExecutionIntervals[e.ThreadId];
intervals.Last().End(endTime);
intervalCount = intervals.Count;
}
long jobId = GetMicrothreadJobIdFromThreadInfo(e.ThreadId, intervalCount);
var jobInfo = new MicrothreadNotification(e.ThreadId, e.MicroThread.Id, jobId, endTime, MicrothreadNotification.NotificationType.JobEnded);
lock (microthreadNotifications)
{
microthreadNotifications.Add(jobInfo);
}
}
private long GetMicrothreadJobIdFromThreadInfo(int threadId, int threadIntervalCount)
{
unchecked
{
long result = threadIntervalCount;
result += ((long)threadId) << (sizeof(int) * 8);
return result;
}
}
private long GetTicksFromStopwatch()
{
return (long)(stopWatch.ElapsedTicks * TickFactor);
}
}
}