This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathCompilationTracker.cs
364 lines (317 loc) · 13.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) Microsoft Corporation.
// 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;
using Microsoft.Quantum.QsCompiler.Diagnostics;
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
{
/// <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 string? ParentName { get; }
/// <summary>
/// Represents the name of the compilation task.
/// </summary>
public string Name { get; }
/// <summary>
/// Identifier of the task.
/// </summary>
public string Id => GenerateKey(this.ParentName, this.Name);
/// <summary>
/// List of tuples in which each item represents the duration measured per thread.
/// </summary>
public List<(string Id, long DurationInMs)> ItemizedDurations
{
get
{
if (this.watches.Count == 0)
{
throw new InvalidOperationException($"Attempt to get task '{this.Id}' duration when no interval has been measured");
}
else if (this.IsInProgress())
{
throw new InvalidOperationException($"Attempt to get task '{this.Id}' duration when measurement is in progress");
}
// For tasks whose performance was only measured in one thread, do not include a thread number in the ID.
var itemizedDurations = new List<(string TaskItem, long DurationInMs)>();
if (this.watches.Count == 1)
{
var key = this.watches.Keys.First();
var watch = this.watches[key];
itemizedDurations.Add((this.Name, watch.ElapsedMilliseconds));
}
else
{
foreach (var item in this.watches)
{
itemizedDurations.Add(($"{this.Name}[{item.Key}]", item.Value.ElapsedMilliseconds));
}
}
return itemizedDurations;
}
}
/// <summary>
/// Number of intervals (start/stop cycles) measured.
/// </summary>
public int IntervalCount { get; private set; }
/// <summary>
/// Stopwatches used to measure the duration of the task on each thread.
/// </summary>
private readonly IDictionary<int, Stopwatch> watches;
/// <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.watches = new Dictionary<int, Stopwatch>();
}
/// <summary>
/// Returns whether a compilation class is in progress.
/// </summary>
public bool IsInProgress()
{
foreach (var item in this.watches)
{
if (item.Value.IsRunning)
{
return true;
}
}
return false;
}
/// <summary>
/// Starts/resumes time accounting for this task.
/// </summary>
public void Start()
{
var threadId = Thread.CurrentThread.ManagedThreadId;
if (!this.watches.TryGetValue(threadId, out var watch))
{
watch = new Stopwatch();
this.watches.Add(threadId, watch);
}
if (watch.IsRunning)
{
throw new InvalidOperationException($"Attempt to start task '{this.Id}' when it is already in progress in the current thread");
}
watch.Start();
}
/// <summary>
/// Stops/pauses time accounting for this task.
/// </summary>
public void Stop()
{
var threadId = Thread.CurrentThread.ManagedThreadId;
if (!this.watches.TryGetValue(threadId, out var watch))
{
throw new InvalidOperationException($"Attempt to stop task in a thread that did not start it");
}
if (!watch.IsRunning)
{
throw new InvalidOperationException($"Attempt to stop task '{this.Id}' when it is not in progress in the current thread");
}
watch.Stop();
this.IntervalCount++;
}
}
/// <summary>
/// Represents a node in the tree of tasks performed by the compiler.
/// </summary>
private class CompilationTaskNode
{
public CompilationTask Task { get; }
public IDictionary<string, CompilationTaskNode> Children { get; }
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}.";
}
// Write the itemized durations for this task.
foreach (var item in this.Task.ItemizedDurations)
{
var propertyName = $"{preparedPrefix}{item.Id}";
jsonWriter.WriteNumber(propertyName, item.DurationInMs);
}
// Write the child tasks.
var fullTaskName = $"{preparedPrefix}{this.Task.Name}";
foreach (var entry in this.Children.OrderBy(e => e.Key))
{
entry.Value.WriteToJson(jsonWriter, fullTaskName);
}
}
}
// Public members.
/// <summary>
/// Represents the file name where the compilation performance data will be stored.
/// </summary>
public const string CompilationPerfDataFileName = "CompilationPerfData.json";
/// <summary>
/// Defines a handler for a type of compilation task event.
/// </summary>
private delegate void CompilationTaskEventTypeHandler(string? parentTaskName, string taskName);
// Private members.
/// <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<CompilationTaskEventType, CompilationTaskEventTypeHandler> CompilationEventTypeHandlers = new Dictionary<CompilationTaskEventType, CompilationTaskEventTypeHandler>
{
{ CompilationTaskEventType.Start, CompilationEventStartHandler },
{ 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>();
// 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(string? parentTaskName, string taskName)
{
Debug.Assert(Monitor.IsEntered(GlobalLock));
var key = CompilationTask.GenerateKey(parentTaskName, taskName);
if (!CompilationTasks.TryGetValue(key, out var task))
{
task = new CompilationTask(parentTaskName, taskName);
CompilationTasks.Add(key, task);
}
task.Start();
}
/// <summary>
/// Handles a compilation task end event.
/// </summary>
private static void CompilationEventEndHandler(string? parentTaskName, string taskName)
{
Debug.Assert(Monitor.IsEntered(GlobalLock));
var key = CompilationTask.GenerateKey(parentTaskName, taskName);
if (!CompilationTasks.TryGetValue(key, out var task))
{
throw new InvalidOperationException($"Attempt to stop task '{key}' which does not exist");
}
task.Stop();
}
// Public methods.
/// <summary>
/// Clears tracked data.
/// </summary>
public static void ClearData()
{
lock (GlobalLock)
{
CompilationTasks.Clear();
}
}
/// <summary>
/// Handles a compilation task event.
/// </summary>
public static void OnCompilationTaskEvent(CompilationTaskEventType type, string? parentTaskName, string taskName)
{
lock (GlobalLock)
{
if (!CompilationEventTypeHandlers.TryGetValue(type, out var handler))
{
throw new ArgumentException($"No handler for compilation task event type '{type}' exists");
}
handler(parentTaskName, taskName);
}
}
/// <summary>
/// Publishes the results to text files in the specified folder.
/// </summary>
/// <exception cref="NotSupportedException"><paramref name="outputFolder"/> is malformed.</exception>
/// <exception cref="IOException"><paramref name="outputFolder"/> is a file path.</exception>
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,
};
using 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();
}
}
}