diff --git a/Commands/HeapDomStatsCommand.cs b/Commands/HeapDomStatsCommand.cs index 9e82209..0cf4497 100644 --- a/Commands/HeapDomStatsCommand.cs +++ b/Commands/HeapDomStatsCommand.cs @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +using MemorySnapshotAnalyzer.AbstractMemorySnapshot; using MemorySnapshotAnalyzer.CommandInfrastructure; using System; using System.Collections.Generic; @@ -19,6 +20,12 @@ public HeapDomStatsCommand(Repl repl) : base(repl) {} #pragma warning disable CS0649 // Field '...' is never assigned to, and will always have its default value [FlagArgument("bysize")] public bool OrderBySize; + + [NamedArgument("type")] + public CommandLineArgument? TypeIndexOrPattern; + + [FlagArgument("includederived")] + public bool IncludeDerived; #pragma warning restore CS0649 // Field '...' is never assigned to, and will always have its default value public override void Run() @@ -28,6 +35,16 @@ public override void Run() void DumpStats() { + TypeSet? typeSet; + if (TypeIndexOrPattern != null) + { + typeSet = TypeIndexOrPattern.ResolveTypeIndexOrPattern(Context, IncludeDerived); + } + else + { + typeSet = null; + } + List? children = CurrentHeapDom.GetChildren(CurrentHeapDom.RootNodeIndex); var stats = new Dictionary(); @@ -43,12 +60,17 @@ void DumpStats() int nodeIndex = children[i]; if (CurrentBacktracer.IsLiveObjectNode(nodeIndex)) { + int postorderIndex = CurrentBacktracer.NodeIndexToPostorderIndex(nodeIndex); + int typeIndex = CurrentTracedHeap.PostorderTypeIndexOrSentinel(postorderIndex); + if (typeSet != null && !typeSet.Contains(typeIndex)) + { + continue; + } + long treeSize = CurrentHeapDom.TreeSize(nodeIndex); totalSize += treeSize; totalObjectCount++; - int postorderIndex = CurrentBacktracer.NodeIndexToPostorderIndex(nodeIndex); - int typeIndex = CurrentTracedHeap.PostorderTypeIndexOrSentinel(postorderIndex); if (stats.TryGetValue(typeIndex, out (int Count, long Size) data)) { stats[typeIndex] = (data.Count + 1, data.Size + CurrentHeapDom.TreeSize(nodeIndex)); @@ -101,6 +123,6 @@ void DumpStats() Output.EndArray(); } - public override string HelpText => "heapdomstats ['bysize]"; + public override string HelpText => "heapdomstats ['bysize] ['type [] ['includederived]]"; } }