From b884db5fb12ae0055427b6087cc4a2a7a8397228 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 21:46:21 -0500 Subject: [PATCH] =?UTF-8?q?perf(grpc-data):=20A3=20r2=20L2=20=E2=80=94=20E?= =?UTF-8?q?ventId=20window=20back=20to=20the=20plan's=208;=20measure=20the?= =?UTF-8?q?=20retained=20heap,=20not=20GC.GetTotalMemory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GC.GetTotalMemory on .NET 10 regions reported +92 or +218 MiB (bimodal) for the same state; GCMemoryInfo heap size and a heap dump (136 MB of objects, eeheap allocated 136,318,696 / committed 236,208,128) show the extra was committed-but-free regions. The harness now takes HeapSizeBytes-FragmentedBytes after a full blocking GC and the settled working set, and asserts BOTH <= 200 MiB. Worst case (2 placements, 8 slots, 3 event items, Live+Buffer): heap +106.5 MiB, WS +151.3 MiB max of 3 runs. BytesPerCondition 230,000 -> 160,000 (max WS/condition 158,629, rounded up); MaxImportedConditionBytes default 160,000 x 2,500 = 400,000,000 B (~381 MiB). --- .../Configuration/DataConnectionOptions.cs | 2 +- .../ProjectedConditionRegistry.cs | 8 +++-- .../ProjectedConditionAccess.cs | 10 +++--- ...ImportedConditionMemoryMeasurementTests.cs | 33 +++++++++++++++++-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Host/Configuration/DataConnectionOptions.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Host/Configuration/DataConnectionOptions.cs index ca67a3563..9ffd79ca9 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Host/Configuration/DataConnectionOptions.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Host/Configuration/DataConnectionOptions.cs @@ -21,7 +21,7 @@ public sealed class DataConnectionOptions public long MaxTransportBytes { get; set; } = 64 * 1024 * 1024; /// A3 (§5.5): imported-condition node budget per consumer node (2,500 × 49 worst-case nodes per condition). public int MaxImportedConditionNodes { get; set; } = 122_500; - /// A3 (§5.5): imported-condition memory budget per consumer node (§9.3 L2: worst-case measured 230,000 B/condition × 2,500 ≈ 548 MiB). + /// A3 (§5.5): imported-condition memory budget per consumer node (§9.3 L2: worst-case measured 160,000 B/condition × 2,500 ≈ 381 MiB). public long MaxImportedConditionBytes { get; set; } = ZB.MOM.WW.OtOpcUa.Runtime.DataConnections.ProjectedConditionOptions.DefaultMaxImportedConditionBytes; public long MaxPendingBytes { get; set; } = 1024 * 1024; public TimeSpan MaxSessionLifetime { get; set; } = TimeSpan.FromHours(1); diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs index 201603370..3daba583f 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs @@ -72,8 +72,12 @@ public sealed record ImportedConditionBinding(string ConnectionId, string Condit /// internal sealed class ProjectedConditionRegistry { - /// Recent EventIds kept per condition for N6 re-binding. - internal const int RecentEventIds = 32; + /// + /// Recent EventIds kept per condition (§5.6 step 3: "within its last N (default 8) events"). A3 review round 2: back to + /// the plan's 8 — A3a's 32 had no recorded need (validity is "minted under the current marker", which only the newest + /// few can satisfy; an older EventId already answers BadEventIdUnknown at the origin). + /// + internal const int RecentEventIds = 8; private readonly ConcurrentDictionary _byNode = new(); private readonly ConcurrentDictionary _byInstance = new(ReferenceEqualityComparer.Instance); diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedConditionAccess.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedConditionAccess.cs index 1aaf9b93c..24b4176c2 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedConditionAccess.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedConditionAccess.cs @@ -40,7 +40,7 @@ public sealed class DeployedConditionMappings : IProjectedConditionMappings /// A3 host limits (§5.5 "Budget"). /// How long a connection's condition items stay open after the last event item goes (default 30 s). /// Per consumer node (default 122,500 = 2,500 × 49). -/// Per consumer node (default = L2-measured × 2,500 ≈ 548 MiB). +/// Per consumer node (default = L2-measured × 2,500 ≈ 381 MiB). /// Pause before re-opening an ended condition stream. public sealed record ProjectedConditionOptions(TimeSpan Linger, int MaxImportedConditionNodes, long MaxImportedConditionBytes, TimeSpan ReconnectDelay) @@ -50,10 +50,12 @@ public sealed record ProjectedConditionOptions(TimeSpan Linger, int MaxImportedC /// /// Retained managed bytes per imported condition, measured by the A3 L2 harness (1,000 conditions in one mapping on a /// running SDK 2.0.1 server: node + children + ids + registry/indexes + EventId registry + verdict cache; X0's - /// node-only harness measured 160,997). A3b review S5: the WORST case — two placements, all 32 EventId slots, three - /// event items, the runner's Live + Buffer records — max of three runs 228,353 B (managed heap), rounded up. + /// node-only harness measured 160,997). A3b review S5: the WORST case — two placements, every EventId slot, three + /// event items, the runner's Live + Buffer records. A3 review round 2: measured with the retained heap + /// (GCMemoryInfo, not GC.GetTotalMemory, which counted ~125 MiB of committed-but-free regions) and the + /// settled working set, 8 EventId slots (§5.6): max of three runs = 158,629 B (working set; heap 111,715 B), rounded up. /// - public const long BytesPerCondition = 230_000; + public const long BytesPerCondition = 160_000; /// §9.3 L2: the host byte cap = measured per-condition bytes × 2,500 (the host condition cap). public const long DefaultMaxImportedConditionBytes = BytesPerCondition * 2_500; diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionMemoryMeasurementTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionMemoryMeasurementTests.cs index 4154676d6..daedc50dd 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionMemoryMeasurementTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionMemoryMeasurementTests.cs @@ -62,6 +62,7 @@ public sealed class ImportedConditionMemoryMeasurementTests subscription.MonitoredItems.All(m => ServiceResult.IsGood(m.Status.Error)).ShouldBeTrue("all three event items are admitted"); var (heap0, ws0) = Measure(); + var totalMemory0 = GC.GetTotalMemory(forceFullCollection: true); var keys = Enumerable.Range(0, Conditions).Select(i => KeyOf("l2-" + i)).ToArray(); // Runner state (worst case): the committed Live records AND a full re-snapshot Buffer of the same keys. var live = keys.Select((k, i) => Update(k, i, 3, active: true, acked: false, sequence: 1)).ToList(); @@ -88,6 +89,7 @@ public sealed class ImportedConditionMemoryMeasurementTests nm.AuthorizeImportedCondition(identity, Core.Abstractions.OpcUaOperation.Read, nm.ImportedConditionNodeId(Connection, k)).ShouldBeTrue(); var (heap1, ws1) = Measure(); + var (reportedHeap0, reportedHeap1) = (totalMemory0, GC.GetTotalMemory(forceFullCollection: true)); GC.KeepAlive(live); GC.KeepAlive(buffer); GC.KeepAlive(subscription); @@ -96,21 +98,48 @@ public sealed class ImportedConditionMemoryMeasurementTests var line = $"L2 (worst case: 2 placements, {slots} EventId slots, 3 event items, Live+Buffer records): {Conditions} conditions: " + $"managed heap +{heapGrowth / 1048576.0:F1} MiB ({heapGrowth / Conditions:N0} B/condition; after materialise {(heapMid - heap0) / Conditions:N0}), " + $"working set +{wsGrowth / 1048576.0:F1} MiB ({wsGrowth / Conditions:N0} B/condition); materialise {materialise.TotalMilliseconds:F0} ms, " + - $"{OpcUaServer.ProjectedConditionRegistry.RecentEventIds} transitions {transition.TotalMilliseconds:F0} ms; events reported {nm.ImportedEventsReported}"; + $"{OpcUaServer.ProjectedConditionRegistry.RecentEventIds} transitions {transition.TotalMilliseconds:F0} ms; events reported {nm.ImportedEventsReported}; " + + $"GC.GetTotalMemory delta (not used, see Measure) +{(reportedHeap1 - reportedHeap0) / 1048576.0:F1} MiB"; TestContext.Current.TestOutputHelper?.WriteLine(line); if (Environment.GetEnvironmentVariable("OTOPCUA_A3_MEASURE_OUT") is { Length: > 0 } outPath) File.AppendAllText(outPath, line + Environment.NewLine); wsGrowth.ShouldBeLessThanOrEqualTo(200L * 1024 * 1024); + heapGrowth.ShouldBeLessThanOrEqualTo(200L * 1024 * 1024, "the retained managed heap is under the same bound"); } private static NodeId Local(Opc.Ua.Client.ISession session, NodeId serverId) => new(serverId.IdentifierAsString, (ushort)session.NamespaceUris.GetIndex(ProjectedDataIds.RawNamespaceUri)); + /// + /// Retained managed heap and settled working set after forced compacting GCs. + /// + /// + /// A3 review round 2: GC.GetTotalMemory is NOT the retained heap on this runtime (.NET 10, regions). In the same + /// process state it reported either +92 MiB or +218 MiB (bimodal, run to run) while GC.GetGCMemoryInfo's heap size + /// was 135.5 MB in both, and a heap dump of a "+218 MiB" run held 136 MB of objects in total (eeheap -gc: allocated + /// 136,318,696, committed 236,208,128) — the extra ~125 MiB was committed-but-free region space. The live heap is therefore + /// taken from the last full blocking GC's HeapSizeBytes − FragmentedBytes, and the working set is sampled until the GC + /// has decommitted those free regions (two samples 500 ms apart within 1 MiB, at most 10 s): the instant after the forced + /// GCs it still counts them (+214 MiB seen; +133 MiB two seconds later in the same run). + /// private static (long Heap, long WorkingSet) Measure() { for (var i = 0; i < 3; i++) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true, compacting: true); GC.WaitForPendingFinalizers(); } + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true, compacting: true); + var info = GC.GetGCMemoryInfo(GCKind.FullBlocking); using var process = Process.GetCurrentProcess(); process.Refresh(); - return (GC.GetTotalMemory(forceFullCollection: true), process.WorkingSet64); + var ws = process.WorkingSet64; + var settle = Stopwatch.StartNew(); + while (settle.Elapsed < TimeSpan.FromSeconds(10)) + { + Thread.Sleep(500); + process.Refresh(); + var next = process.WorkingSet64; + var stable = Math.Abs(next - ws) < 1024 * 1024; + ws = next; + if (stable) break; + } + return (info.HeapSizeBytes - info.FragmentedBytes, ws); } private static string KeyOf(string seed) =>