This repository was archived by the owner on Nov 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 503
SuperIlc improvements, part #1 #7190
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ff6dfb
SuperIlc improvements, part #1
trylek 51d834c
Added missing copyright header on ParallelRunner.cs
trylek f260c78
Rename ProcessInfo.Data to strongly typed InputFileName
trylek 2916a95
Additional minor fixes to improve captured log quality
trylek de4c250
Fix incorrect process duration reporting in ParallelRunner
trylek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// Execute a given number of mutually independent build subprocesses represented by an array of | ||
| /// command lines with a given degree of parallelization. | ||
| /// </summary> | ||
| public sealed class ParallelRunner | ||
| { | ||
| /// <summary> | ||
| /// Helper class for launching mutually independent build subprocesses in parallel. | ||
| /// It supports launching the processes and optionally redirecting their standard and | ||
| /// error output streams to prevent them from interleaving in the final build output log. | ||
| /// Multiple instances of this class representing the individual running processes | ||
| /// can exist at the same time. | ||
| /// </summmary> | ||
| class ProcessSlot | ||
| { | ||
| /// <summary> | ||
| /// Process slot index (used for diagnostic purposes) | ||
| /// </summary> | ||
| readonly int _slotIndex; | ||
|
|
||
| /// <summary> | ||
| /// Event used to report that a process has exited | ||
| /// </summary> | ||
| readonly AutoResetEvent _processExitEvent; | ||
|
|
||
| /// <summary> | ||
| /// Process object | ||
| /// </summary> | ||
| ProcessRunner _processRunner; | ||
|
|
||
| /// <summary> | ||
| /// Constructor stores global slot parameters and initializes the slot state machine | ||
| /// </summary> | ||
| /// <param name="slotIndex">Process slot index used for diagnostic purposes</param> | ||
| /// <param name="processExitEvent">Event used to report process exit</param> | ||
| public ProcessSlot(int slotIndex, AutoResetEvent processExitEvent) | ||
| { | ||
| _slotIndex = slotIndex; | ||
| _processExitEvent = processExitEvent; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Launch a new process. | ||
| /// </summary> | ||
| /// <param name="processInfo">application to execute</param> | ||
| /// <param name="processIndex">Numeric index used to prefix messages pertaining to this process in the console output</param> | ||
| public void Launch(ProcessInfo processInfo, int processIndex) | ||
| { | ||
| Debug.Assert(_processRunner == null); | ||
| Console.WriteLine($"{processIndex}: launching: {processInfo.ProcessPath} {processInfo.Arguments}"); | ||
|
|
||
| _processRunner = new ProcessRunner(processInfo, processIndex, _processExitEvent); | ||
| } | ||
|
|
||
| public bool IsAvailable() | ||
| { | ||
| if (_processRunner == null) | ||
| { | ||
| return true; | ||
| } | ||
| if (!_processRunner.IsAvailable()) | ||
| { | ||
| return false; | ||
| } | ||
| _processRunner.Dispose(); | ||
| _processRunner = null; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Execute a given set of mutually independent build commands with the default | ||
| /// degree of parallelism. | ||
| /// </summary> | ||
| /// <param name="processesToRun">Processes to execute in parallel</param> | ||
| public static void Run(IEnumerable<ProcessInfo> processesToRun) | ||
| { | ||
| Run(processesToRun, degreeOfParallelism: Environment.ProcessorCount); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Execute a given set of mutually independent build commands with given degree of | ||
| /// parallelism. | ||
| /// </summary> | ||
| /// <param name="processesToRun">Processes to execute in parallel</param> | ||
| /// <param name="degreeOfParallelism">Maximum number of processes to execute in parallel</param> | ||
| public static void Run(IEnumerable<ProcessInfo> processesToRun, int degreeOfParallelism) | ||
| { | ||
| int processCount = processesToRun.Count(); | ||
| if (processCount < degreeOfParallelism) | ||
| { | ||
| // We never need a higher DOP than the number of process to execute | ||
| degreeOfParallelism = processCount; | ||
| } | ||
|
|
||
| using (AutoResetEvent processExitEvent = new AutoResetEvent(initialState: false)) | ||
| { | ||
| ProcessSlot[] processSlots = new ProcessSlot[degreeOfParallelism]; | ||
| for (int index = 0; index < degreeOfParallelism; index++) | ||
| { | ||
| processSlots[index] = new ProcessSlot(index, processExitEvent); | ||
| } | ||
|
|
||
| int processIndex = 0; | ||
| foreach (ProcessInfo processInfo in processesToRun) | ||
| { | ||
| // Allocate a process slot, potentially waiting on the exit event | ||
| // when all slots are busy (running) | ||
| ProcessSlot freeSlot = null; | ||
| do | ||
| { | ||
| foreach (ProcessSlot slot in processSlots) | ||
| { | ||
| if (slot.IsAvailable()) | ||
| { | ||
| freeSlot = slot; | ||
| break; | ||
| } | ||
| } | ||
| if (freeSlot == null) | ||
| { | ||
| // All slots are busy - wait for a process to finish | ||
| processExitEvent.WaitOne(); | ||
| } | ||
| } | ||
| while (freeSlot == null); | ||
|
|
||
| freeSlot.Launch(processInfo, ++processIndex); | ||
| } | ||
|
|
||
| // We have launched all the commands, now wait for all processes to finish | ||
| bool activeProcessesExist; | ||
| do | ||
| { | ||
| activeProcessesExist = false; | ||
| foreach (ProcessSlot slot in processSlots) | ||
| { | ||
| if (!slot.IsAvailable()) | ||
| { | ||
| activeProcessesExist = true; | ||
| } | ||
| } | ||
| if (activeProcessesExist) | ||
| { | ||
| processExitEvent.WaitOne(); | ||
| } | ||
| } | ||
| while (activeProcessesExist); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Could you update the error message in the catch to refer to
runnerOutputPathinstead of the now potentially incorrectoutputDirectory?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will do!