Skip to content

Grow the logging queue instead of blocking producers when memory allows - #15281

Merged
AlesProkop merged 4 commits into
mainfrom
alesprokop-rar-logging-experiment
Oct 16, 2026
Merged

AlesProkop merged 4 commits into
mainfrom
alesprokop-rar-logging-experiment

Conversation

@AlesProkop

@AlesProkop AlesProkop commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Related issue(s): #15224

Context

In short: when the logging queue is full, it now grows if the machine has the memory, instead of blocking every thread that logs.

All threads of a build log into one LoggingService. Events wait in a queue of at most 200,000 entries until a single thread has handed them to the loggers. When a logger is slow, typically the binary logger on a large solution, the queue fills up and every thread that logs blocks until the consumer catches up. ResolveAssemblyReference logs a lot of messages, so with -mt and a binlog it spends much of its time waiting for queue space: in an incremental OrchardCore build its summed task time is about 75 s instead of about 20 s (#15224).

Changes Made

When a producer finds the queue full, it doubles the capacity (200K → 400K → 800K …) instead of waiting, as long as the process has the memory for the added events. If it does not, the producer waits for the logger as before, but wakes up every second to look at the capacity and the memory again, so the queue grows once the GC reports that the memory is available.

  • Memory check. GC.GetGCMemoryInfo() reports HighMemoryLoadThresholdBytes - MemoryLoadBytes, the memory the process can still use before the GC considers the machine to be under high memory load, and TotalAvailableMemoryBytes - TotalCommittedBytes, the room left below the GC heap hard limit (DOTNET_GCHeapHardLimit, which containers get by default). The smaller of the two is used. The GC measures them when it last ran, so a change shows after the next GC. Events that were already queued then are part of what it measured, so only the added capacity is priced against it.
  • Why 2 KB per event. An event takes about 144 bytes plus 2 bytes per character of its message (368 B for 100 characters, 2,168 B for 1,000), and the events of real builds measure 0.3 to 0.4 KB. 2 KB corresponds to an average message of about 900 characters, five times what is typical. That over-prices events, so growth is refused early rather than late, and it is still cheap enough that the first doubling needs only 0.41 GB of headroom (then 0.82, 1.64 and 3.3 GB). Every doubling reads the memory again, so a wrong guess overshoots by one step at most.
  • No fixed upper bound. Each doubling is priced against the headroom that is left, so the growth stops where the machine does. A fixed 2,000,000 events is just as fast (see below) but commits the memory on every machine, whether it is needed or not.
  • The capacity never shrinks, but the memory is only used while events are backlogged and is freed as they drain.

The decision is made in TryGrowQueueCapacity, called from the producer loop of LoggingService.ProcessLoggingEvent. Producers that find the queue full together take it one at a time (under a lock), so only one of them looks at the memory and the others see its result.

Compatibility

  • Builds whose loggers keep up never fill the queue and are not affected.
  • .NET only. .NET Framework (Visual Studio, msbuild.exe) keeps the fixed capacity, because it has no API for the memory load, and its producers keep waiting without a time limit, exactly as before.
  • Opt-outs: setting MSBUILDLOGGINGQUEUECAPACITY keeps the capacity fixed at that value, as today, and MSBUILDDISABLEFEATURESFROMVERSION=18.13 turns the feature off (change wave 18.13, listed in ChangeWaves.md).
  • No new warnings, errors, APIs or protocol changes. With MSBUILDDEBUGCOMM=1 the communication trace shows The logging queue grew from 200000 to 400000 events.

Testing

Unit tests. 19 test cases in LoggingQueueCapacity_Tests cover growth when the headroom allows it, the exact headroom boundary, refusal, waiting producers noticing that memory recovered while nothing is dequeued, producers that find the queue full together sharing one memory evaluation, a second producer adopting the growth of the first, the headroom being capped by the GC heap hard limit, and the opt-outs. They pass on net11.0 and net472, as does the whole Microsoft.Build.UnitTests.Logging namespace (185 tests; one skip on net11.0 that was there before). With DOTNET_GCHeapHardLimit=0x40000000 on a machine with 64 GiB the real provider reports 0.96 GiB of headroom instead of 28 GiB (checked once with a scratch test that is not part of the PR).

PerfStar (binlog runs, Orchard Core incremental build with -mt; main is the median of the last six scheduled main builds, this PR is one run of this branch):

main this PR change
Linux 39.9 s 29.7 s −25.6%
Windows 30.6 s 27.0 s −11.6%

The binlog overhead (the build with a binlog compared with the same build without one) drops from 64.5% to 27.5% on Linux and from 57.8% to 22.3% on Windows. The other scenarios of the run (Orchard Core rebuild with -mt, the non-MT incremental build on Linux, net8 console) change by 3% or less, and builds without a binlog are flat. An earlier experiment with a fixed 2,000,000 events (perf/janpro/binlog-queue-attermpt2) gave the same speedup (Linux 39.4 → 30.3 s, Windows 30.6 → 26.9 s).

The comparison on the dashboard (Binlog overhead → Comparison; candidate: this branch's run 20261014.12.s, reference: the scheduled main run 20261010.2.s, which is close to the median of the six):

Build time with binlog Binlog overhead
Windows comparison comparison
Linux comparison comparison

The dashboard's default reference is the mean of 30 days of main, which is noisy, so the links pin one main run.

Local OrchardCore (ea88aa0e, -mt with a binlog, warm incremental build, one Windows machine, 5 runs per column, 4 with the GC limit; same binary, only MSBUILDLOGGINGQUEUECAPACITY differs):

200K fixed (today) 2M fixed this PR
wall time 33.2 s 30.6 s 29.9 s
summed ResolveAssemblyReference time 75.2 s 20.8 s 20.2 s
peak private memory 1.64 GiB 1.75 GiB 1.73 GiB
wall time with a 4 GiB GC heap limit 34.5 s 30.5 s 30.7 s

The PerfStar and local numbers were measured before the review fixes, which only change what happens when the queue is full and how the headroom is computed (at most once a second); the lines that run for every event are unchanged. A micro-benchmark of the producer path (16 producers, Debug build) before and after the fixes shows no measurable difference: +1% when the queue never fills (noise), −2% to 0% while it grows under contention, +0.3% to +0.7% in the memory-refusal state.

The logging service queue holds at most 200,000 events. When a slow logger such as the binary logger falls behind, the queue fills and every thread that logs blocks, which stalls ResolveAssemblyReference and other chatty tasks in -mt builds (#15224).

On .NET, a producer that finds the queue full now doubles its capacity instead of blocking when GC.GetGCMemoryInfo reports enough headroom below the high memory load threshold for the added events, priced at an assumed 2 KB per event. Otherwise it waits as before and the memory is evaluated again after a second. Setting MSBUILDLOGGINGQUEUECAPACITY or disabling change wave 18.13 keeps the fixed capacity, and .NET Framework is unchanged.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 11:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Timed re-evaluation, concurrent probing, and stale GC memory accounting leave correctness and memory-pressure risks.

2 open findings
What changed in this PR

Adds adaptive growth to the asynchronous logging queue to reduce producer contention while respecting memory pressure.

Changes:

  • Doubles queue capacity when GC-reported headroom permits.
  • Adds Change Wave and environment-variable opt-outs.
  • Adds focused tests and documentation.
File Description
LoggingService.cs Implements adaptive queue growth and memory checks.
LoggingQueueCapacity_Tests.cs Tests growth, refusal, and opt-outs.
Logging-Internals.md Documents adaptive capacity behavior.
ChangeWaves.md Registers the 18.13 behavior change.

🧠 Review effort: Balanced

Comment thread documentation/wiki/Logging-Internals.md Resolved

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The adaptive-capacity path can strand producers that were already waiting on the dequeue signal; with a logger callback that depends on those logging calls, this forms a deadlock. The retry coordination also permits a concurrent GC-memory-query herd, and the new tests/docs do not currently prove or describe the actual retry behavior deterministically.

# Dimension Verdict
3 Performance & Allocation Awareness 🟡 1 MAJOR
4 Test Coverage & Completeness 🟡 1 MAJOR
13 Concurrency & Thread Safety 🔴 1 BLOCKING
18 Documentation Accuracy 🟡 1 MODERATE

✅ 20/24 dimensions clean.

  • Concurrency — wake producers when capacity growth creates queue space
  • Performance — serialize the once-per-second memory evaluation
  • Tests — prove the producer reached the no-headroom path
  • Documentation — align retry wording with implemented wake/retry semantics

Warning

Firewall blocked 3 domains

The following domains were blocked by the firewall during workflow execution:

  • api.github.com
  • github.com
  • raw.githubusercontent.com

[!TIP]
api.github.com is blocked because GitHub API access uses the built-in GitHub tools by default. Instead of adding api.github.com to network.allowed, use tools.github.mode: gh-proxy for direct pre-authenticated GitHub CLI access without requiring network access to api.github.com:

tools:
  github:
    mode: gh-proxy

See GitHub Tools for more information on gh-proxy mode.

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.github.com"
    - "github.com"
    - "raw.githubusercontent.com"

See Network Configuration for more information.

Generated by Expert Code Review (on open) for #15281 · copilot · gpt56 · 907 AIC · ⌖ 0.527 AIC · ⊞ 24.9K

Comment thread documentation/wiki/Logging-Internals.md Resolved
AlesProkop and others added 2 commits October 14, 2026 14:11
A producer that found the queue full and could not grow it waited for a dequeue without a time limit. While the logger is stuck, nothing is dequeued, so memory that became available again or growth done by another producer went unnoticed. With a growing queue, producers now wake up after the retry interval of one second and look at the capacity and the memory again. A queue with a fixed capacity still waits without a limit, and so does .NET Framework, where the memory cannot be measured and the behavior stays as it was.

Producers that find the queue full together now share one memory evaluation: the decision is taken under a lock, and the producers that waited for it see the result instead of querying the GC again.

The test of the refusal path now waits until the producer has reached the full queue. New tests cover the retry while nothing is dequeued, the shared evaluation and the missing memory provider.

Co-authored-by: Copilot App <[email protected]>
The memory load and the high-memory-load threshold that the GC reports are computed from the physical memory of the
machine and ignore a GC heap hard limit (DOTNET_GCHeapHardLimit, or the default limit of a container), so with a
low limit on a large machine the queue could be approved to grow far beyond what the heap can hold. Take the room
left below the hard limit (TotalAvailableMemoryBytes - TotalCommittedBytes) into account as well.

The GC reports all of these values as of its last collection, so correct the documentation: growth that was refused
is allowed again once a GC has measured that memory is available, not as soon as it becomes available.

Co-authored-by: Copilot App <[email protected]>
@AlesProkop
AlesProkop merged commit ff8a546 into main Oct 16, 2026
14 checks passed
@AlesProkop
AlesProkop deleted the alesprokop-rar-logging-experiment branch October 16, 2026 13:36
@AlesProkop AlesProkop mentioned this pull request Nov 19, 2026
5 of 16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants