-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[IBuildEngine callbacks] Stage 2: RequestCores/ReleaseCores #13306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c05e655
48f88d7
8b5c6ca
0292ea0
89594d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| namespace Microsoft.Build.UnitTests.BackEnd | ||
| { | ||
| /// <summary> | ||
| /// A test task that calls IBuildEngine9.RequestCores and optionally ReleaseCores. | ||
| /// Used by TaskHostCallback_Tests (in-process) and NetTaskHost_E2E_Tests (cross-runtime). | ||
| /// The E2E project includes this file via linked compile to avoid duplication. | ||
| /// </summary> | ||
| public class RequestCoresTask : Task | ||
| { | ||
| /// <summary> | ||
| /// Number of cores to request. Defaults to 1. | ||
| /// </summary> | ||
| public int CoreCount { get; set; } = 1; | ||
|
|
||
| /// <summary> | ||
| /// Whether to release the granted cores after requesting them. | ||
| /// </summary> | ||
| public bool ReleaseAfter { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of cores granted by the build engine. | ||
| /// </summary> | ||
| [Output] | ||
| public int GrantedCores { get; set; } | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| if (BuildEngine is IBuildEngine9 engine9) | ||
| { | ||
| GrantedCores = engine9.RequestCores(CoreCount); | ||
| Log.LogMessage(MessageImportance.High, $"RequestCores({CoreCount}) = {GrantedCores}"); | ||
|
|
||
| if (ReleaseAfter && GrantedCores > 0) | ||
| { | ||
| engine9.ReleaseCores(GrantedCores); | ||
| Log.LogMessage(MessageImportance.High, $"ReleaseCores({GrantedCores}) completed"); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| Log.LogError("BuildEngine does not implement IBuildEngine9"); | ||
| return false; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,6 +162,33 @@ public void NetTaskHost_CallbackIsRunningMultipleNodesTest() | |
| testTaskOutput.ShouldContain("CallbackResult: IsRunningMultipleNodes = False"); | ||
| } | ||
|
|
||
| [WindowsFullFrameworkOnlyFact] | ||
| public void NetTaskHost_CallbackRequestCoresTest() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| env.SetEnvironmentVariable("MSBUILDENABLETASKHOSTCALLBACKS", "1"); | ||
|
|
||
| // Point dotnet resolution to the bootstrap layout so the .NET Core TaskHost | ||
| // uses the locally-built MSBuild.dll (with callback support) instead of the system SDK. | ||
| string bootstrapCorePath = Path.Combine(RunnerUtilities.BootstrapRootPath, "core"); | ||
| string bootstrapDotnet = Path.Combine(bootstrapCorePath, "dotnet.exe"); | ||
| env.SetEnvironmentVariable("DOTNET_HOST_PATH", bootstrapDotnet); | ||
| env.SetEnvironmentVariable("DOTNET_ROOT", bootstrapCorePath); | ||
| env.SetEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR", bootstrapCorePath); | ||
|
Comment on lines
+171
to
+177
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @YuliiaKovalova I feel like you had a less manual mechanism to do this, am I misremembering?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After apphost introduction it will be simplified |
||
|
|
||
| string testProjectPath = Path.Combine(TestAssetsRootPath, "ExampleNetTask", "TestNetTaskResourceCallback", "TestNetTaskResourceCallback.csproj"); | ||
|
|
||
| string testTaskOutput = RunnerUtilities.ExecBootstrapedMSBuild($"{testProjectPath} -v:n -p:LatestDotNetCoreForMSBuild={RunnerUtilities.LatestDotNetCoreForMSBuild} -t:TestTask", out bool successTestTask); | ||
|
|
||
| if (!successTestTask) | ||
| { | ||
| _output.WriteLine(testTaskOutput); | ||
| } | ||
|
|
||
| successTestTask.ShouldBeTrue(); | ||
| testTaskOutput.ShouldContain("CallbackResult: RequestCores(2) ="); | ||
| } | ||
|
|
||
| [WindowsFullFrameworkOnlyFact] | ||
| public void NetTaskWithImplicitHostParamsTest() | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(LatestDotNetCoreForMSBuild)</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <TestProjectFolder>$([System.IO.Path]::GetFullPath('$([System.IO.Path]::Combine('$(MSBuildThisFileDirectory)', '..', '..', '..', '..'))'))</TestProjectFolder> | ||
| <ExampleTaskPath>$([System.IO.Path]::Combine('$(TestProjectFolder)', '$(TargetFramework)', 'ExampleTask.dll'))</ExampleTaskPath> | ||
| </PropertyGroup> | ||
|
|
||
| <UsingTask | ||
| TaskName="Microsoft.Build.UnitTests.BackEnd.RequestCoresTask" | ||
| AssemblyFile="$(ExampleTaskPath)" | ||
| TaskFactory="TaskHostFactory" | ||
| Runtime="NET"/> | ||
|
|
||
| <Target Name="TestTask" BeforeTargets="Build"> | ||
| <Microsoft.Build.UnitTests.BackEnd.RequestCoresTask CoreCount="2" ReleaseAfter="true"> | ||
| <Output PropertyName="Granted" TaskParameter="GrantedCores" /> | ||
| </Microsoft.Build.UnitTests.BackEnd.RequestCoresTask> | ||
| <Message Text="CallbackResult: RequestCores(2) = $(Granted)" Importance="high" /> | ||
| </Target> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,2 @@ | ||||||||||||||||||||
| { | ||||||||||||||||||||
|
||||||||||||||||||||
| { | |
| { | |
| // This global.json exists to prevent walking up the repo tree and | |
| // inheriting the repo-root SDK settings when running the E2E test | |
| // from the bootstrap layout. Use the latest installed SDK instead. | |
| "sdk": { | |
| "allowPrerelease": true, | |
| "rollForward": "latestMajor" | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,8 @@ | |
| <Compile Include="..\Shared\ITaskHostCallbackPacket.cs" /> | ||
| <Compile Include="..\Shared\TaskHostIsRunningMultipleNodesRequest.cs" /> | ||
| <Compile Include="..\Shared\TaskHostIsRunningMultipleNodesResponse.cs" /> | ||
| <Compile Include="..\Shared\TaskHostCoresRequest.cs" /> | ||
| <Compile Include="..\Shared\TaskHostCoresResponse.cs" /> | ||
|
Comment on lines
+123
to
+124
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's making me really sad to see us pile up new shared code. Not blocking but I'd appreciate putting some thought into that. |
||
| <Compile Include="..\Shared\TaskLoader.cs" /> | ||
| <Compile Include="..\Shared\MSBuildLoadContext.cs" Condition="'$(TargetFrameworkIdentifier)'!='.NETFramework'" /> | ||
| <Compile Include="..\Shared\TypeLoader.cs" /> | ||
|
|
||
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.
This test checks that MSB5022 is logged when callbacks are disabled, but it doesn’t assert the expected functional fallback (RequestCores returning 0) or the overall build result (a logged error should typically fail the build). Adding assertions for the returned
GrantedCores/Resultvalue and theBuildResultoutcome would cover the new behavior more completely.