cb672ea060
The X0 node-only harness used a GC.GetTotalMemory(true) delta, which on .NET 10 counts committed-but-free GC regions. Switched it to the L2 Measure() method (full blocking GC, HeapSizeBytes - FragmentedBytes, settled working set): 79,085 B heap / ~90 KB WS per OffNormal condition (GetTotalMemory still reads 158,169 B, reproducing the old 160,997). Plan 5.5, X0/A3/A4-docs evidence and the BytesPerCondition comment corrected; the default (160,000 B from the L2 in-server worst case) is unchanged.
56 lines
4.4 KiB
C#
56 lines
4.4 KiB
C#
using Opc.Ua;
|
|
sealed class Ids : INodeIdFactory { int n; public NodeId New(ISystemContext c, NodeState node) => new NodeId("n" + (++n), 2); }
|
|
static class P {
|
|
static int Count(ISystemContext ctx, NodeState n, Dictionary<string,int> kinds) {
|
|
kinds[n.NodeClass.ToString()] = kinds.GetValueOrDefault(n.NodeClass.ToString()) + 1;
|
|
var children = new List<BaseInstanceState>(); n.GetChildren(ctx, children);
|
|
int c = 1; foreach (var ch in children) c += Count(ctx, ch, kinds); return c;
|
|
}
|
|
static void Run(string name, Func<NodeState, AlarmConditionState> make, ISystemContext ctx) {
|
|
var parent = new FolderState(null);
|
|
var alarm = make(parent);
|
|
alarm.ConfirmedState = new TwoStateVariableState(alarm);
|
|
alarm.Confirm = new AddCommentMethodState(alarm);
|
|
alarm.ShelvingState = new ShelvedStateMachineState(alarm);
|
|
alarm.MaxTimeShelved = new PropertyState<double>.Implementation<VariantBuilder>(alarm);
|
|
alarm.Create(ctx, new NodeId("cond", 2), new QualifiedName("cond", 2), new LocalizedText("cond"), true);
|
|
var kinds = new Dictionary<string,int>();
|
|
int total = Count(ctx, alarm, kinds);
|
|
Console.WriteLine($"{name}: total={total} ({string.Join(", ", kinds.Select(k => k.Key + "=" + k.Value))}); Quality={(alarm.Quality!=null)} BranchId={(alarm.BranchId!=null)} Comment={(alarm.Comment!=null)}");
|
|
}
|
|
// #929: GC.GetTotalMemory counts committed-but-free regions on .NET 10. Use the last full blocking GC's
|
|
// HeapSizeBytes - FragmentedBytes plus a settled working set: the same method as the L2 harness's Measure()
|
|
// (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionMemoryMeasurementTests.cs).
|
|
static (long Heap, long WorkingSet) Measure() {
|
|
for (int i = 0; i < 3; i++) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); GC.WaitForPendingFinalizers(); }
|
|
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
|
|
var info = GC.GetGCMemoryInfo(GCKind.FullBlocking);
|
|
using var p = System.Diagnostics.Process.GetCurrentProcess(); p.Refresh(); long ws = p.WorkingSet64;
|
|
var settle = System.Diagnostics.Stopwatch.StartNew();
|
|
while (settle.Elapsed < TimeSpan.FromSeconds(10)) {
|
|
Thread.Sleep(500); p.Refresh(); long next = p.WorkingSet64; bool stable = Math.Abs(next - ws) < 1024 * 1024; ws = next; if (stable) break; }
|
|
return (info.HeapSizeBytes - info.FragmentedBytes, ws);
|
|
}
|
|
static void Main() {
|
|
var ctx = new SystemContext((ITelemetryContext)null) { NodeIdFactory = new Ids(), NamespaceUris = new NamespaceTable() };
|
|
ctx.NamespaceUris.Append("urn:a"); ctx.NamespaceUris.Append("urn:b");
|
|
Run("AlarmConditionState", p => new AlarmConditionState(p), ctx);
|
|
Run("OffNormalAlarmState", p => new OffNormalAlarmState(p), ctx);
|
|
Run("DiscreteAlarmState", p => new DiscreteAlarmState(p), ctx);
|
|
var keep = new List<object>(); var (heap0, ws0) = Measure(); long before = GC.GetTotalMemory(true);
|
|
for (int i = 0; i < 2000; i++) { var parent = new FolderState(null); var a = new OffNormalAlarmState(parent);
|
|
a.ConfirmedState = new TwoStateVariableState(a); a.Confirm = new AddCommentMethodState(a); a.ShelvingState = new ShelvedStateMachineState(a);
|
|
a.MaxTimeShelved = new PropertyState<double>.Implementation<VariantBuilder>(a);
|
|
a.Create(ctx, new NodeId("c"+i, 2), new QualifiedName("c"+i, 2), new LocalizedText("c"+i), true); keep.Add(a); }
|
|
var (heap1, ws1) = Measure(); long after = GC.GetTotalMemory(true);
|
|
Console.WriteLine($"retained bytes/condition (2000, OffNormal, no address-space indexes): heap {(heap1-heap0)/2000} (GCMemoryInfo), " +
|
|
$"working set {(ws1-ws0)/2000} (settled); GC.GetTotalMemory delta {(after-before)/2000} (not used, #929)"); GC.KeepAlive(keep);
|
|
var conds = keep.Cast<OffNormalAlarmState>().ToArray(); var recs = new object[conds.Length];
|
|
for (int w = 0; w < 3; w++) { var sw = System.Diagnostics.Stopwatch.StartNew();
|
|
for (int i = 0; i < conds.Length; i++) { var c = conds[i]; recs[i] = (c.NodeId, c.ActiveState?.Id?.Value, c.AckedState?.Id?.Value, c.ConfirmedState?.Id?.Value,
|
|
c.EnabledState?.Id?.Value, c.ShelvingState?.CurrentState?.Value, c.Severity?.Value, c.Message?.Value, c.Quality?.Value, c.Retain?.Value,
|
|
c.Time?.Value, c.Comment?.Value, c.ClientUserId?.Value, c.ConditionClassId?.Value, c.ActiveState?.TransitionTime?.Value); }
|
|
sw.Stop(); Console.WriteLine($"snapshot copy of {conds.Length} conditions: {sw.Elapsed.TotalMilliseconds:F2} ms"); }
|
|
}
|
|
}
|