-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSchedulingPlan.cs
627 lines (540 loc) · 23.4 KB
/
SchedulingPlan.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
#nullable disable
namespace Microsoft.Build.BackEnd
{
/// <summary>
/// A SchedulingPlan contains timing and relationship information for a build which has already occurred. This data can then be
/// used by subsequent builds to determine how best to distribute work among several nodes.
/// </summary>
internal class SchedulingPlan
{
/// <summary>
/// The configuration cache.
/// </summary>
private IConfigCache _configCache;
/// <summary>
/// The active scheduling data.
/// </summary>
private SchedulingData _schedulingData;
/// <summary>
/// Mapping of project full paths to plan configuration data.
/// </summary>
private Dictionary<string, PlanConfigData> _configPathToData = new Dictionary<string, PlanConfigData>();
/// <summary>
/// Mapping of configuration ids to plan configuration data.
/// </summary>
private Dictionary<int, PlanConfigData> _configIdToData = new Dictionary<int, PlanConfigData>();
/// <summary>
/// Mapping of configuration ids to the set of configurations which were traversed to get to this configuration.
/// </summary>
private Dictionary<int, List<Stack<PlanConfigData>>> _configIdToPaths = new Dictionary<int, List<Stack<PlanConfigData>>>();
/// <summary>
/// Constructor.
/// </summary>
public SchedulingPlan(IConfigCache configCache, SchedulingData schedulingData)
{
_configCache = configCache;
_schedulingData = schedulingData;
this.MaximumConfigurationId = BuildRequestConfiguration.InvalidConfigurationId;
}
public PlanConfigData GetConfiguration(int configId)
{
_configPathToData.TryGetValue(_configCache[configId].ProjectFullPath, out PlanConfigData data);
return data;
}
/// <summary>
/// Returns true if a valid plan was read, false otherwise.
/// </summary>
public bool IsPlanValid
{
get;
private set;
}
/// <summary>
/// Returns the largest configuration id known to the plan.
/// </summary>
public int MaximumConfigurationId
{
get;
private set;
}
/// <summary>
/// Writes a plan for the specified submission id.
/// </summary>
public void WritePlan(int submissionId, ILoggingService loggingService, BuildEventContext buildEventContext)
{
if (!BuildParameters.EnableBuildPlan)
{
return;
}
SchedulableRequest rootRequest = GetRootRequest(submissionId);
if (rootRequest == null)
{
return;
}
string planName = GetPlanName(rootRequest);
if (String.IsNullOrEmpty(planName))
{
return;
}
try
{
using (StreamWriter file = new StreamWriter(File.Open(planName, FileMode.Create)))
{
// Write the accumulated configuration times.
Dictionary<int, double> accumulatedTimeByConfiguration = new Dictionary<int, double>();
RecursiveAccumulateConfigurationTimes(rootRequest, accumulatedTimeByConfiguration);
List<KeyValuePair<int, double>> configurationsInOrder = new(accumulatedTimeByConfiguration);
configurationsInOrder.Sort((l, r) => Comparer<int>.Default.Compare(l.Key, r.Key));
foreach (KeyValuePair<int, double> configuration in configurationsInOrder)
{
file.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0} {1} {2}", configuration.Key, configuration.Value, _configCache[configuration.Key].ProjectFullPath));
}
file.WriteLine();
// Write out the dependency information.
RecursiveWriteDependencies(file, rootRequest);
}
}
catch (IOException)
{
loggingService.LogCommentFromText(buildEventContext, MessageImportance.Low, ResourceUtilities.FormatResourceStringStripCodeAndKeyword("CantWriteBuildPlan", planName));
}
}
/// <summary>
/// Reads a plan for the specified submission Id.
/// </summary>
public void ReadPlan(int submissionId, ILoggingService loggingService, BuildEventContext buildEventContext)
{
if (!BuildParameters.EnableBuildPlan)
{
return;
}
SchedulableRequest rootRequest = GetRootRequest(submissionId);
if (rootRequest == null)
{
return;
}
string planName = GetPlanName(rootRequest);
if (String.IsNullOrEmpty(planName))
{
return;
}
if (!FileSystems.Default.FileExists(planName))
{
return;
}
try
{
using (StreamReader file = new StreamReader(File.Open(planName, FileMode.Open)))
{
ReadTimes(file);
ReadHierarchy(file);
}
if (_configIdToData.Count > 0)
{
AnalyzeData();
}
}
catch (IOException)
{
loggingService.LogCommentFromText(buildEventContext, MessageImportance.Low, ResourceUtilities.FormatResourceStringStripCodeAndKeyword("CantReadBuildPlan", planName));
}
catch (InvalidDataException)
{
loggingService.LogCommentFromText(buildEventContext, MessageImportance.Low, ResourceUtilities.FormatResourceStringStripCodeAndKeyword("BuildPlanCorrupt", planName));
}
catch (FormatException)
{
loggingService.LogCommentFromText(buildEventContext, MessageImportance.Low, ResourceUtilities.FormatResourceStringStripCodeAndKeyword("BuildPlanCorrupt", planName));
}
}
/// <summary>
/// Returns the config id for the config specified by the path, if any.
/// </summary>
/// <returns>The config id if one exists, otherwise BuildRequestConfiguration.InvalidConfigurationId</returns>
public int GetConfigIdForPath(string configPath)
{
PlanConfigData config;
if (!_configPathToData.TryGetValue(configPath, out config))
{
return BuildRequestConfiguration.InvalidConfigurationId;
}
return config.ConfigId;
}
/// <summary>
/// Gets the name of the plan file for a specified submission.
/// </summary>
private string GetPlanName(SchedulableRequest rootRequest)
{
if (rootRequest == null)
{
return null;
}
return _configCache[rootRequest.BuildRequest.ConfigurationId].ProjectFullPath + ".buildplan";
}
/// <summary>
/// Analyzes the plan data which has been read.
/// </summary>
private void AnalyzeData()
{
DoRecursiveAnalysis();
if (Traits.Instance.DebugScheduler)
{
DetermineExpensiveConfigs();
DetermineConfigsByNumberOfOccurrences();
DetermineConfigsWithTheMostImmediateReferences();
DetermineConfigsWithGreatestPlanTime();
}
IsPlanValid = true;
}
/// <summary>
/// Writes out configuration in order of the greatest total plan time.
/// </summary>
private void DetermineConfigsWithGreatestPlanTime()
{
List<KeyValuePair<int, PlanConfigData>> projectsInOrderOfTotalPlanTime = new(_configIdToData);
projectsInOrderOfTotalPlanTime.Sort((left, right) => Comparer<double>.Default.Compare(right.Value.TotalPlanTime, left.Value.TotalPlanTime));
foreach (KeyValuePair<int, PlanConfigData> configuration in projectsInOrderOfTotalPlanTime)
{
PlanConfigData config = configuration.Value;
Console.WriteLine("{0}: {1} ({2} referrers) {3}", configuration.Key, config.TotalPlanTime, config.ReferrerCount, config.ConfigFullPath);
foreach (PlanConfigData referrer in config.Referrers)
{
Console.WriteLine(" {0} {1}", referrer.ConfigId, referrer.ConfigFullPath);
}
Console.WriteLine();
}
Console.WriteLine();
}
/// <summary>
/// Writes out configs in order of most immediate references.
/// </summary>
private void DetermineConfigsWithTheMostImmediateReferences()
{
Console.WriteLine("Projects with the most immediate children:");
List<KeyValuePair<int, PlanConfigData>> projectsInOrderOfImmediateChildCount = new(_configIdToData);
projectsInOrderOfImmediateChildCount.Sort((left, right) => Comparer<int>.Default.Compare(right.Value.ReferencesCount, left.Value.ReferencesCount));
foreach (KeyValuePair<int, PlanConfigData> configuration in projectsInOrderOfImmediateChildCount)
{
Console.WriteLine("{0}: {1} {2}", configuration.Key, configuration.Value.ReferencesCount, configuration.Value.ConfigFullPath);
}
Console.WriteLine();
}
/// <summary>
/// Writes out configs in order of how often they are seen in the hierarchy.
/// </summary>
private void DetermineConfigsByNumberOfOccurrences()
{
Console.WriteLine("Configs in hierarchy by number of occurrences:");
List<int> projectsInOrderOfReference = new List<int>(_configIdToData.Keys);
projectsInOrderOfReference.Sort(delegate (int left, int right) { return -Comparer<int>.Default.Compare(_configIdToPaths[left].Count, _configIdToPaths[right].Count); });
foreach (int configId in projectsInOrderOfReference)
{
Console.WriteLine("{0}: {1} {2}", configId, _configIdToPaths[configId].Count, _configIdToData[configId].ConfigFullPath);
}
Console.WriteLine();
}
/// <summary>
/// This method finds all of the paths which lead to any given project
/// </summary>
private void DoRecursiveAnalysis()
{
Stack<PlanConfigData> currentPath = new Stack<PlanConfigData>();
PlanConfigData root = _configIdToData[1];
RecursiveVisitNodes(root, currentPath);
}
/// <summary>
/// Recursively visits all nodes in the hierarchy.
/// </summary>
private void RecursiveVisitNodes(PlanConfigData root, Stack<PlanConfigData> currentPath)
{
// Store the current path as a new path for this config
List<Stack<PlanConfigData>> pathsForConfig;
if (!_configIdToPaths.TryGetValue(root.ConfigId, out pathsForConfig))
{
pathsForConfig = new List<Stack<PlanConfigData>>();
_configIdToPaths[root.ConfigId] = pathsForConfig;
}
// Reverse the stack so we get a path from the root to this node.
Stack<PlanConfigData> pathToAdd = new Stack<PlanConfigData>(currentPath);
// And add it to the list of paths.
pathsForConfig.Add(pathToAdd);
// Now add ourselves to the current path
currentPath.Push(root);
// Visit our children
foreach (PlanConfigData child in root.References)
{
RecursiveVisitNodes(child, currentPath);
}
// Remove ourselves from the current path
currentPath.Pop();
}
/// <summary>
/// Finds projects in order of expense and displays the paths leading to them.
/// </summary>
private void DetermineExpensiveConfigs()
{
Console.WriteLine("Projects by expense:");
List<PlanConfigData> projectsByExpense = new List<PlanConfigData>(_configIdToData.Values);
// Sort most expensive to least expensive.
projectsByExpense.Sort(delegate (PlanConfigData left, PlanConfigData right) { return -Comparer<double>.Default.Compare(left.AccumulatedTime, right.AccumulatedTime); });
foreach (PlanConfigData config in projectsByExpense)
{
Console.WriteLine("{0}: {1} {2}", config.ConfigId, config.AccumulatedTime, config.ConfigFullPath);
List<Stack<PlanConfigData>> pathsByLength = _configIdToPaths[config.ConfigId];
// Sort the paths from shortest to longest.
pathsByLength.Sort(delegate (Stack<PlanConfigData> left, Stack<PlanConfigData> right) { return Comparer<int>.Default.Compare(left.Count, right.Count); });
foreach (Stack<PlanConfigData> path in pathsByLength)
{
Console.Write(" ");
foreach (PlanConfigData pathEntry in path)
{
Console.Write(" {0}", pathEntry.ConfigId);
}
Console.WriteLine();
}
Console.WriteLine();
}
}
/// <summary>
/// Reads the hierarchy from a plan file.
/// </summary>
private void ReadHierarchy(StreamReader file)
{
while (file.ReadLine() is string line)
{
if (line.Length == 0)
{
return;
}
string[] values = line.Split(MSBuildConstants.SpaceChar);
if (values.Length < 1)
{
throw new InvalidDataException("Too few values in hierarchy");
}
int configId = Convert.ToInt32(values[0], CultureInfo.InvariantCulture);
PlanConfigData parent = _configIdToData[configId];
for (int i = 1; i < values.Length; i++)
{
int childId = Convert.ToInt32(values[i], CultureInfo.InvariantCulture);
PlanConfigData child = _configIdToData[childId];
parent.AddReference(child);
}
}
}
/// <summary>
/// Reads the accumulated time and path information for each configuration from the plan file.
/// </summary>
private void ReadTimes(StreamReader file)
{
while (file.ReadLine() is string line)
{
if (line.Length == 0)
{
return;
}
string[] values = line.Split(MSBuildConstants.SpaceChar);
if (values.Length < 3)
{
throw new InvalidDataException("Too few values in build plan.");
}
int configId = Convert.ToInt32(values[0], CultureInfo.InvariantCulture);
double accumulatedTime = Convert.ToDouble(values[1], CultureInfo.InvariantCulture);
string configFullPath = values[2];
PlanConfigData data = new PlanConfigData(configId, configFullPath, accumulatedTime);
_configIdToData[configId] = data;
_configPathToData[configFullPath] = data;
MaximumConfigurationId = Math.Max(MaximumConfigurationId, configId);
}
}
/// <summary>
/// Retrieves the root request for the specified submission id.
/// </summary>
/// <returns>The request if one exists, otherwise null.</returns>
private SchedulableRequest GetRootRequest(int submissionId)
{
foreach (SchedulableRequest request in _schedulingData.GetRequestsByHierarchy(null))
{
if (request.BuildRequest.SubmissionId == submissionId)
{
return request;
}
}
return null;
}
/// <summary>
/// Writes out all of the dependencies for a specified request, recursively.
/// </summary>
private void RecursiveWriteDependencies(StreamWriter file, SchedulableRequest request)
{
file.Write(request.BuildRequest.ConfigurationId);
foreach (SchedulableRequest child in _schedulingData.GetRequestsByHierarchy(request))
{
file.Write(" {0}", child.BuildRequest.ConfigurationId);
}
file.WriteLine();
foreach (SchedulableRequest child in _schedulingData.GetRequestsByHierarchy(request))
{
RecursiveWriteDependencies(file, child);
}
}
/// <summary>
/// Recursively accumulates the amount of time spent in each configuration.
/// </summary>
private void RecursiveAccumulateConfigurationTimes(SchedulableRequest request, Dictionary<int, double> accumulatedTimeByConfiguration)
{
double accumulatedTime;
// NOTE: Do we want to count it each time the config appears in the hierarchy? This will inflate the
// cost of frequently referenced configurations.
accumulatedTimeByConfiguration.TryGetValue(request.BuildRequest.ConfigurationId, out accumulatedTime);
accumulatedTimeByConfiguration[request.BuildRequest.ConfigurationId] = accumulatedTime + request.GetTimeSpentInState(SchedulableRequestState.Executing).TotalMilliseconds;
foreach (SchedulableRequest childRequest in _schedulingData.GetRequestsByHierarchy(request))
{
RecursiveAccumulateConfigurationTimes(childRequest, accumulatedTimeByConfiguration);
}
}
/// <summary>
/// The data associated with a config as read from a build plan.
/// </summary>
internal class PlanConfigData
{
/// <summary>
/// The configuration id.
/// </summary>
private int _configId;
/// <summary>
/// The full path to the project.
/// </summary>
private string _configFullPath;
/// <summary>
/// The amount of time spent in the configuration.
/// </summary>
private double _accumulatedTime;
/// <summary>
/// The total time of all of the references.
/// </summary>
private double _accumulatedTimeOfReferences;
/// <summary>
/// The set of references.
/// </summary>
private HashSet<PlanConfigData> _references = new HashSet<PlanConfigData>();
/// <summary>
/// The set of referrers.
/// </summary>
private HashSet<PlanConfigData> _referrers = new HashSet<PlanConfigData>();
/// <summary>
/// Constructor.
/// </summary>
public PlanConfigData(int configId, string configFullPath, double accumulatedTime)
{
_configId = configId;
_configFullPath = configFullPath;
_accumulatedTime = accumulatedTime;
}
/// <summary>
/// Gets the configuration id.
/// </summary>
public int ConfigId
{
get { return _configId; }
}
/// <summary>
/// Gets the configuration's full path.
/// </summary>
public string ConfigFullPath
{
get { return _configFullPath; }
}
/// <summary>
/// Gets the configuration's accumulated time.
/// </summary>
public double AccumulatedTime
{
get { return _accumulatedTime; }
set { _accumulatedTime = value; }
}
/// <summary>
/// Gets the configuration's accumulated time for all of its references.
/// </summary>
public double AccumulatedTimeOfReferences
{
get { return _accumulatedTimeOfReferences; }
}
/// <summary>
/// Retrieves the total time for this configuration, which includes the time spent on its references.
/// </summary>
public double TotalPlanTime
{
// Count our time, plus the amount of time all of our children take. Multiply this by the total number
// of referrers to weight us higher the more configurations depend on us.
get { return AccumulatedTime + AccumulatedTimeOfReferences; }
}
/// <summary>
/// Retrieves the number of references this configuration has.
/// </summary>
public int ReferencesCount
{
get { return _references.Count; }
}
/// <summary>
/// Retrieves the references from this configuration.
/// </summary>
public IEnumerable<PlanConfigData> References
{
get { return _references; }
}
/// <summary>
/// Retrieves the number of configurations which refer to this one.
/// </summary>
public int ReferrerCount
{
get { return _referrers.Count; }
}
/// <summary>
/// Retrieves the configurations which refer to this one.
/// </summary>
public IEnumerable<PlanConfigData> Referrers
{
get { return _referrers; }
}
/// <summary>
/// Adds the specified configuration as a reference.
/// </summary>
public void AddReference(PlanConfigData reference)
{
if (!_references.Contains(reference))
{
_references.Add(reference);
// My own accumulated reference time and that of all of my referrers increases as well
if (!reference._referrers.Contains(this))
{
reference._referrers.Add(this);
}
_accumulatedTimeOfReferences += reference.AccumulatedTime;
RecursivelyApplyReferenceTimeToReferrers(reference.AccumulatedTime);
}
}
/// <summary>
/// Applies the specified duration offset to the configurations which refer to this one.
/// </summary>
public void RecursivelyApplyReferenceTimeToReferrers(double duration)
{
foreach (PlanConfigData referrer in _referrers)
{
referrer._accumulatedTimeOfReferences += duration;
referrer.RecursivelyApplyReferenceTimeToReferrers(duration);
}
}
}
}
}