forked from microsoft/qsharp-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompilationTracker.cs
339 lines (294 loc) · 12.4 KB
/
CompilationTracker.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
namespace Microsoft.Quantum.QsCompiler.CommandLineCompiler
{
/// <summary>
/// Provides an event tracker of the compilation process for the purpose of assessing performance.
/// </summary>
public static class CompilationTracker
{
// Private classes and types.
/// <summary>
/// Represents a task performed by the compiler (eg. source loading, reference loading, syntax tree serialization, etc.).
/// </summary>
private class CompilationTask
{
/// <summary>
/// Represents the name of the parent compilation task.
/// </summary>
public readonly string? ParentName;
/// <summary>
/// Represents the name of the compilation task.
/// </summary>
public readonly string Name;
/// <summary>
/// Contains the UTC datetime when the task started.
/// </summary>
public readonly DateTime UtcStart;
/// <summary>
/// Contains the UTC datetime when the task ended.
/// </summary>
public DateTime? UtcEnd;
/// <summary>
/// Contains the duration of the task in milliseconds.
/// </summary>
public long? DurationInMs;
/// <summary>
/// Stopwatch used to measure the duration of the task.
/// </summary>
private readonly Stopwatch watch;
/// <summary>
/// Generates a key that uniquely identifies a task in the compilation process based on the task's name and its parent's name.
/// </summary>
internal static string GenerateKey(string? parentName, string name)
{
return string.Format("{0}.{1}", parentName ?? "ROOT", name);
}
/// <summary>
/// Creates a compilation task object and starts its stopwatch.
/// </summary>
public CompilationTask(string? parentName, string name)
{
this.ParentName = parentName;
this.Name = name;
this.UtcStart = DateTime.UtcNow;
this.UtcEnd = null;
this.DurationInMs = null;
this.watch = Stopwatch.StartNew();
}
/// <summary>
/// Halts the stopwatch of the compilation task and stores its duration.
/// </summary>
public void End()
{
this.UtcEnd = DateTime.UtcNow;
this.watch.Stop();
this.DurationInMs = this.watch.ElapsedMilliseconds;
}
/// <summary>
/// Returns whether a compilation class is in progress.
/// </summary>
public bool IsInProgress()
{
return this.watch.IsRunning;
}
}
/// <summary>
/// Represents a node in the tree of tasks performed by the compiler.
/// </summary>
private class CompilationTaskNode
{
public readonly CompilationTask Task;
public readonly IDictionary<string, CompilationTaskNode> Children;
public CompilationTaskNode(CompilationTask task)
{
this.Task = task;
this.Children = new Dictionary<string, CompilationTaskNode>();
}
public void WriteToJson(Utf8JsonWriter jsonWriter, string? prefix)
{
var preparedPrefix = "";
if (!string.IsNullOrEmpty(prefix))
{
preparedPrefix = $"{prefix}.";
}
var propertyName = $"{preparedPrefix}{this.Task.Name}";
jsonWriter.WriteNumber(propertyName, this.Task.DurationInMs ?? -1);
foreach (var entry in this.Children.OrderBy(e => e.Key))
{
entry.Value.WriteToJson(jsonWriter, propertyName);
}
}
}
/// <summary>
/// Represents a warning type detected while tracking compilation events.
/// </summary>
private enum WarningType
{
TaskAlreadyExists,
TaskDoesNotExist,
TaskAlreadyEnded,
UknownTaskEventType
}
/// <summary>
/// Represents a warning detected while tracking compilation events.
/// </summary>
private class Warning
{
public readonly WarningType Type;
public readonly DateTime UtcDateTime;
public readonly string Key;
public Warning(WarningType type, string key)
{
this.UtcDateTime = DateTime.UtcNow;
this.Type = type;
this.Key = key;
}
}
/// <summary>
/// Defines a handler for a type of compilation task event.
/// </summary>
private delegate void CompilationTaskEventTypeHandler(CompilationLoader.CompilationTaskEventArgs eventArgs);
// Private members.
/// <summary>
/// Represents the file name where the compilation performance data will be stored.
/// </summary>
private const string CompilationPerfDataFileName = "CompilationPerfData.json";
/// <summary>
/// Represents the file name where the compilation performance warnings will be stored.
/// </summary>
private const string CompilationPerfWarningsFileName = "CompilationPerfWarnings.json";
/// <summary>
/// Provides thread-safe access to the members and methods of this class.
/// </summary>
private static readonly object GlobalLock = new object();
/// <summary>
/// Contains a handler that takes care of each type of task event.
/// Handlers are assumed to be not null.
/// Note that thread-safe access to this member is done through the global lock.
/// </summary>
private static readonly IDictionary<CompilationLoader.CompilationTaskEventType, CompilationTaskEventTypeHandler> CompilationEventTypeHandlers = new Dictionary<CompilationLoader.CompilationTaskEventType, CompilationTaskEventTypeHandler>
{
{ CompilationLoader.CompilationTaskEventType.Start, CompilationEventStartHandler },
{ CompilationLoader.CompilationTaskEventType.End, CompilationEventEndHandler }
};
/// <summary>
/// Contains the compilation tasks tracked through the handled events.
/// Note that thread-safe access to this member is done through the global lock.
/// </summary>
private static readonly IDictionary<string, CompilationTask> CompilationTasks = new Dictionary<string, CompilationTask>();
/// <summary>
/// Contains the warnings generated while handling the compiler tasks events.
/// Note that thread-safe access to this member is done through the global lock.
/// </summary>
private static readonly IList<Warning> Warnings = new List<Warning>();
// Private methods.
/// <summary>
/// Creates a hierarchical structure that contains the compilation tasks.
/// </summary>
private static IList<CompilationTaskNode> BuildCompilationTasksHierarchy()
{
var compilationTasksForest = new List<CompilationTaskNode>();
var toFindChildrenNodes = new Queue<CompilationTaskNode>();
lock (GlobalLock)
{
// First add the roots (top-level tasks) of all trees to the forest.
foreach (var entry in CompilationTasks)
{
if (entry.Value.ParentName == null)
{
var node = new CompilationTaskNode(entry.Value);
compilationTasksForest.Add(node);
toFindChildrenNodes.Enqueue(node);
}
}
// Iterate through the tasks until all of them have been added to the hierarchy.
while (toFindChildrenNodes.Count > 0)
{
var parentNode = toFindChildrenNodes.Dequeue();
foreach (var entry in CompilationTasks)
{
if (parentNode.Task.Name.Equals(entry.Value.ParentName))
{
var childNode = new CompilationTaskNode(entry.Value);
parentNode.Children.Add(childNode.Task.Name, childNode);
toFindChildrenNodes.Enqueue(childNode);
}
}
}
}
return compilationTasksForest;
}
/// <summary>
/// Handles a compilation task start event.
/// </summary>
private static void CompilationEventStartHandler(CompilationLoader.CompilationTaskEventArgs eventArgs)
{
Debug.Assert(Monitor.IsEntered(GlobalLock));
string key = CompilationTask.GenerateKey(eventArgs.ParentTaskName, eventArgs.TaskName);
if (CompilationTasks.ContainsKey(key))
{
Warnings.Add(new Warning(WarningType.TaskAlreadyExists, key));
return;
}
CompilationTasks.Add(key, new CompilationTask(eventArgs.ParentTaskName, eventArgs.TaskName));
}
/// <summary>
/// Handles a compilation task end event.
/// </summary>
private static void CompilationEventEndHandler(CompilationLoader.CompilationTaskEventArgs eventArgs)
{
Debug.Assert(Monitor.IsEntered(GlobalLock));
var key = CompilationTask.GenerateKey(eventArgs.ParentTaskName, eventArgs.TaskName);
if (!CompilationTasks.TryGetValue(key, out var task))
{
Warnings.Add(new Warning(WarningType.TaskDoesNotExist, key));
return;
}
if (!task.IsInProgress())
{
Warnings.Add(new Warning(WarningType.TaskAlreadyEnded, key));
return;
}
task.End();
}
// Public methods.
/// <summary>
/// Handles a compilation task event.
/// </summary>
public static void OnCompilationTaskEvent(object sender, CompilationLoader.CompilationTaskEventArgs args)
{
lock (GlobalLock)
{
if (CompilationEventTypeHandlers.TryGetValue(args.Type, out var hanlder))
{
hanlder(args);
}
else
{
Warnings.Add(new Warning(WarningType.UknownTaskEventType, args.Type.ToString()));
}
}
}
/// <summary>
/// Publishes the results to text files in the specified folder.
/// Throws an IOException if the specified output folder is a file path.
/// Throws a NotSupportedException if the path to the output folder is malformed.
/// </summary>
public static void PublishResults(string outputFolder)
{
var compilationProcessesForest = BuildCompilationTasksHierarchy();
var outputPath = Path.GetFullPath(outputFolder);
Directory.CreateDirectory(outputPath);
using (var file = File.CreateText(Path.Combine(outputPath, CompilationPerfDataFileName)))
{
var jsonWriterOptions = new JsonWriterOptions()
{
Indented = true
};
var jsonWriter = new Utf8JsonWriter(file.BaseStream, jsonWriterOptions);
jsonWriter.WriteStartObject();
foreach (var tree in compilationProcessesForest.OrderBy(t => t.Task.Name))
{
tree.WriteToJson(jsonWriter, null);
}
jsonWriter.WriteEndObject();
jsonWriter.Flush();
}
if (Warnings.Count > 0)
{
using (var file = File.CreateText(Path.Combine(outputPath, CompilationPerfWarningsFileName)))
{
JsonSerializer.SerializeAsync(file.BaseStream, Warnings).Wait();
}
}
}
}
}