Skip to content
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

Prejit support on FunctionsNetHost #2711

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
91db9cc
FNH-WIP for prejit support
kshyju Jul 18, 2024
60f949a
Install net9 rc1
kshyju Sep 13, 2024
45c6701
Switching back to .NET9 preview 6
kshyju Sep 13, 2024
f66827e
fix YAML to install preview6
kshyju Sep 13, 2024
129ad4a
Changed a trace level log to Info level.
kshyju Sep 16, 2024
40ecea7
Updating build YAML file to include the pre-jit artifacts in the nuge…
kshyju Sep 16, 2024
c246745
PR review fixes
kshyju Sep 19, 2024
972c875
Replaced "AZURE_FUNCTIONS_FUNCTIONSNETHOST" prefix with "AZURE_FUNCTI…
kshyju Sep 19, 2024
0f224fc
Switching to named pipes for communication between FNH and StartupHoo…
kshyju Sep 24, 2024
5fe75d2
Added linux.coldstart jittrace and removed removed R2R from placehold…
kshyju Sep 23, 2024
220777e
Fixed incorrect output path for prelaunch apps
kshyju Sep 23, 2024
0f59756
Using native method to get environment variable.
kshyju Nov 13, 2024
e4f8415
Revert "Using native method to get environment variable."
kshyju Nov 14, 2024
526d6d0
Switching to native interop using DllImport for signaling specializat…
kshyju Nov 15, 2024
4310a3d
Minor cleanup. Upgraded dependencies to GA versions.
kshyju Nov 15, 2024
bd92bc6
Cleanup
kshyju Nov 15, 2024
cf9d6fa
Updated sdk version in global json to net9. Removed duplicate global.…
kshyju Nov 16, 2024
3330553
Minor cleanup
kshyju Nov 18, 2024
21739aa
Install net8 explicitly.
kshyju Nov 18, 2024
b5a5424
Variable rename in YML file based on PR comments.
kshyju Nov 18, 2024
4402eca
Change YAML variable to "tfm"
kshyju Nov 18, 2024
6ad089e
Remove workingdir
kshyju Nov 19, 2024
7bc6174
Minor folder restructuring
kshyju Nov 19, 2024
6478abd
fix project path based on latest folder reorg
kshyju Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/ci/host/official-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extends:

jobs:
- template: /eng/ci/templates/official/jobs/build-host-prelaunch-artifacts.yml@self
- template: /eng/ci/templates/official/jobs/build-host-prejit-artifacts.yml@self
- template: /eng/ci/templates/official/jobs/build-host-artifacts-linux.yml@self
parameters:
PoolName: 1es-pool-azfunc
Expand Down
1 change: 1 addition & 0 deletions eng/ci/host/public-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extends:

jobs:
- template: /eng/ci/templates/official/jobs/build-host-prelaunch-artifacts.yml@self
- template: /eng/ci/templates/official/jobs/build-host-prejit-artifacts.yml@self
- template: /eng/ci/templates/official/jobs/build-host-artifacts-linux.yml@self
parameters:
PoolName: 1es-pool-azfunc-public
Expand Down
29 changes: 29 additions & 0 deletions eng/ci/templates/official/jobs/build-host-prejit-artifacts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
jobs:

- job: BuildPreJitApp
displayName: Build Pre-jit placeholder app artifacts

templateContext:
outputParentDirectory: $(Build.ArtifactStagingDirectory)
outputs:
- output: pipelineArtifact
displayName: Publish Pre-jit placeholder app artifacts
path: $(Build.ArtifactStagingDirectory)/_preJitAppPackages
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider making this a variable

artifact: _preJitAppPackages

variables:
targetFrameworks: 'net8.0,net9.0'

steps:
- template: /eng/ci/templates/steps/install-dotnet.yml@self

- ${{ each tfm in split(variables.targetFrameworks, ',') }}:
- task: DotNetCoreCLI@2
displayName: ${{ tfm }} publish of pre-jit app
inputs:
command: publish
publishWebProjects: false
zipAfterPublish: false
modifyOutputPath: false
arguments: -c Release -o $(Build.ArtifactStagingDirectory)/_preJitAppPackages/${{ replace(tfm, 'net', '') }} -f ${{ tfm }} -p:UseAppHost=false
projects: host/src/FunctionsNetHost.PlaceholderApp/FunctionsNetHost.PlaceholderApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ jobs:
steps:
- template: /eng/ci/templates/steps/install-dotnet.yml@self

- ${{ each version in split(variables.dotnetVersions, ',') }}:
- ${{ each tfm in split(variables.dotnetVersions, ',') }}:
- task: DotNetCoreCLI@2
displayName: Publish prelaunch App.csproj
displayName: ${{ tfm }} publish of prelaunch app
inputs:
command: publish
publishWebProjects: false
zipAfterPublish: false
modifyOutputPath: false
arguments: -c Release -o $(Build.ArtifactStagingDirectory)/_preLaunchAppPackages/${{ version }} -f ${{ version }} -p:UseAppHost=false
arguments: -c Release -o $(Build.ArtifactStagingDirectory)/_preLaunchAppPackages/${{ replace(tfm, 'net', '') }} -f ${{ tfm }} -p:UseAppHost=false
projects: host/src/PrelaunchApp/App.csproj
8 changes: 7 additions & 1 deletion eng/ci/templates/official/jobs/pack-host-artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ jobs:
displayName: Download prelaunch artifacts
inputs:
artifactName: _preLaunchAppPackages
path: $(Build.SourcesDirectory)/host/dist/portable
path: $(Build.SourcesDirectory)/host/dist/portable/prelaunchapps

- task: DownloadPipelineArtifact@2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we supposed to use DownloadPipelineArtifact? 1ES has its own syntax for artifact inputs:

https://eng.ms/docs/cloud-ai-platform/devdiv/one-engineering-system-1es/1es-docs/1es-pipeline-templates/features/inputs/pipeline-artifact

displayName: Download pre-jit artifacts
inputs:
artifactName: _preJitAppPackages
path: $(Build.SourcesDirectory)/host/dist/portable/prejit-placeholder-app

- task: DownloadPipelineArtifact@2
displayName: Download host artifacts - linux
Expand Down
6 changes: 6 additions & 0 deletions eng/ci/templates/steps/install-dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ steps:
packageType: sdk
version: 7.x

- task: UseDotNet@2 # Needed by our test projects
displayName: Install .NET 8
inputs:
packageType: sdk
version: 8.x

- task: UseDotNet@2 # The pinned SDK we use to build
displayName: Install .NET SDK from global.json
inputs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<AssemblyName>Microsoft.Azure.Functions.NetHost.Placeholder</AssemblyName>
<Version>1.0.0</Version>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)/../../../key.snk</AssemblyOriginatorKeyFile>
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<TieredCompilation>false</TieredCompilation>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FunctionsNetHost.Shared\FunctionsNetHost.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="JitTrace\linux.coldstart.jittrace">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="JitTrace\coldstart.jittrace">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35209.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionsNetHost.PlaceholderApp", "FunctionsNetHost.PlaceholderApp.csproj", "{A102AA50-8331-4E2E-B926-F37BA4CA4F83}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionsNetHost.Shared", "..\FunctionsNetHost.Shared\FunctionsNetHost.Shared.csproj", "{298B4E58-F398-4943-9F68-9B9F0DCE0B84}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A102AA50-8331-4E2E-B926-F37BA4CA4F83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A102AA50-8331-4E2E-B926-F37BA4CA4F83}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A102AA50-8331-4E2E-B926-F37BA4CA4F83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A102AA50-8331-4E2E-B926-F37BA4CA4F83}.Release|Any CPU.Build.0 = Release|Any CPU
{298B4E58-F398-4943-9F68-9B9F0DCE0B84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{298B4E58-F398-4943-9F68-9B9F0DCE0B84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{298B4E58-F398-4943-9F68-9B9F0DCE0B84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{298B4E58-F398-4943-9F68-9B9F0DCE0B84}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93CA0BCF-0402-4BDF-86B8-2600B81AFFEB}
EndGlobalSection
EndGlobal
74 changes: 74 additions & 0 deletions host/src/FunctionsNetHost.PlaceholderApp/Interop/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;
using System.Threading;
using FunctionsNetHost.Shared.Interop;

namespace FunctionsNetHost.PlaceholderApp.Interop
{
internal static unsafe class NativeMethods
{
private const string NativeWorkerDll = "FunctionsNetHost.exe";
private static readonly ManualResetEventSlim SpecializationWaitHandle = new(false);
private static SpecializeMessage _specializationMessage;

static NativeMethods()
{
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, ImportResolver);
}

internal static unsafe void RegisterForStartupHookCallback()
{
RegisterStartupHookMessageHandlingCallback(&StartupHookCallbackHandler, nint.Zero);
}

/// <summary>
/// Waits for the specialization message from the native code.
/// This method blocks until the specialization event occurs and the message is received.
/// Once the message is received, it returns the content of the specialization message.
/// </summary>
internal static SpecializeMessage WaitForSpecializationMessage()
{
SpecializationWaitHandle.Wait();

// Don't need _specializationWaitHandle anymore. Dispose it.
SpecializationWaitHandle.Dispose();

return _specializationMessage;
}

private static void RegisterStartupHookMessageHandlingCallback(delegate* unmanaged<byte**, int, nint, nint> requestCallback, nint grpcHandler)
{
_ = register_startuphook_callback(nint.Zero, requestCallback, grpcHandler);
}

[DllImport(NativeWorkerDll)]
private static extern unsafe int register_startuphook_callback(nint pInProcessApplication, delegate* unmanaged<byte**, int, nint, nint> requestCallback, nint grpcHandler);

[UnmanagedCallersOnly]
private static unsafe nint StartupHookCallbackHandler(byte** nativeMessage, int nativeMessageSize, nint grpcHandler)
{
_specializationMessage = SpecializeMessage.FromByteArray(new Span<byte>(*nativeMessage, nativeMessageSize).ToArray());
SpecializationWaitHandle.Set();

return nint.Zero;
}

/// <summary>
/// Custom import resolve callback.
/// When trying to resolve "FunctionsNetHost", we return the handle using GetMainProgramHandle API in this callback.
/// </summary>
private static nint ImportResolver(string libraryName, System.Reflection.Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName == NativeWorkerDll)
{
return NativeLibrary.GetMainProgramHandle();
}

// Return 0 so that built-in resolving code will be executed.
return nint.Zero;
}
}
}
Loading