|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | +using System; |
| 4 | +using System.Runtime; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.WebJobs.Script.WebHost.Middleware |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A middleware responsible for Optimizing CLR settings like GC to help with cold start |
| 14 | + /// </summary> |
| 15 | + internal class ClrOptimizationMiddleware |
| 16 | + { |
| 17 | + // This is double the amount of memory allocated during cold start specialization. |
| 18 | + // This value is calculated based on prod profiles across all languages observed for an extended period of time. |
| 19 | + // This value is just a best effort and if for any reason CLR needs to allocate more memory then it will ignore this value. |
| 20 | + private const long AllocationBudgetForGCDuringSpecialization = 16 * 1024 * 1024; |
| 21 | + private readonly ILogger _logger; |
| 22 | + private readonly RequestDelegate _next; |
| 23 | + private readonly IScriptWebHostEnvironment _webHostEnvironment; |
| 24 | + private RequestDelegate _invoke; |
| 25 | + private double _specialized = 0; |
| 26 | + |
| 27 | + public ClrOptimizationMiddleware(RequestDelegate next, IScriptWebHostEnvironment webHostEnvironment, ILogger<SystemTraceMiddleware> logger) |
| 28 | + { |
| 29 | + _webHostEnvironment = webHostEnvironment; |
| 30 | + _logger = logger; |
| 31 | + _next = next; |
| 32 | + _invoke = InvokeClrOptimizationCheck; |
| 33 | + } |
| 34 | + |
| 35 | + public Task Invoke(HttpContext context) |
| 36 | + { |
| 37 | + return _invoke(context); |
| 38 | + } |
| 39 | + |
| 40 | + private Task InvokeClrOptimizationCheck(HttpContext context) |
| 41 | + { |
| 42 | + var task = _next.Invoke(context).ContinueWith(task => |
| 43 | + { |
| 44 | + // We are tweaking GC behavior in ClrOptimizationMiddleware as this is one of the last call stacks that get executed during standby mode as well as function exection. |
| 45 | + // We force a GC and enter no GC region in standby mode and exit no GC region after first function execution during specialization. |
| 46 | + StartStopGCAsBestEffort(); |
| 47 | + }, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion); |
| 48 | + |
| 49 | + return task; |
| 50 | + } |
| 51 | + |
| 52 | + private void StartStopGCAsBestEffort() |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + if (_webHostEnvironment.InStandbyMode) |
| 57 | + { |
| 58 | + // This is just to make sure we do not enter NoGCRegion multiple times during standby mode. |
| 59 | + if (GCSettings.LatencyMode != GCLatencyMode.NoGCRegion) |
| 60 | + { |
| 61 | + // In standby mode, we enforce a GC then enter NoGCRegion mode as best effort. |
| 62 | + // This is to try to avoid GC during cold start specialization. |
| 63 | + GC.Collect(); |
| 64 | + if (!GC.TryStartNoGCRegion(AllocationBudgetForGCDuringSpecialization, disallowFullBlockingGC: false)) |
| 65 | + { |
| 66 | + _logger.LogError($"CLR runtime failed to commit the requested amount of memory: {AllocationBudgetForGCDuringSpecialization}"); |
| 67 | + } |
| 68 | + _logger.LogInformation($"Collection count for gen 0: {GC.CollectionCount(0)}, gen 1: {GC.CollectionCount(1)}, gen 2: {GC.CollectionCount(2)}"); |
| 69 | + } |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + // if not in standby mode and we are in NoGCRegion then we end NoGCRegion. |
| 74 | + if (GCSettings.LatencyMode == GCLatencyMode.NoGCRegion) |
| 75 | + { |
| 76 | + GC.EndNoGCRegion(); |
| 77 | + _logger.LogInformation($"Collection count for gen 0: {GC.CollectionCount(0)}, gen 1: {GC.CollectionCount(1)}, gen 2: {GC.CollectionCount(2)}"); |
| 78 | + } |
| 79 | + |
| 80 | + // This logic needs to run only once during specialization, so replacing the RequestDelegate after specialization |
| 81 | + if (Interlocked.CompareExchange(ref _specialized, 1, 0) == 0) |
| 82 | + { |
| 83 | + Interlocked.Exchange(ref _invoke, _next); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + catch (Exception ex) |
| 88 | + { |
| 89 | + _logger.LogError(ex.Message); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments