Skip to content

Commit

Permalink
Support filtering heapdomstats by types, to support attribution of ta…
Browse files Browse the repository at this point in the history
…rgeted sets of objects

Summary:
Now that we want to push higher attribution for specific features, it's helpful to be able to list unattributed objects that float to the top of the dominator tree, and that need more reference classifiers.

Note that this is not sufficient to ensure complete attribution, as matching objects could still be descendants of other top-level objects, or float to non-meaningful intermediate nodes.

Differential Revision: D54563606

fbshipit-source-id: 26d3ae58017fdcc70947ef29083903731e5c8d9b
  • Loading branch information
elliekorn authored and facebook-github-bot committed Mar 6, 2024
1 parent 028c9de commit c710246
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Commands/HeapDomStatsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -28,6 +35,16 @@ public override void Run()

void DumpStats()
{
TypeSet? typeSet;
if (TypeIndexOrPattern != null)
{
typeSet = TypeIndexOrPattern.ResolveTypeIndexOrPattern(Context, IncludeDerived);
}
else
{
typeSet = null;
}

List<int>? children = CurrentHeapDom.GetChildren(CurrentHeapDom.RootNodeIndex);

var stats = new Dictionary<int, (int Count, long Size)>();
Expand All @@ -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));
Expand Down Expand Up @@ -101,6 +123,6 @@ void DumpStats()
Output.EndArray();
}

public override string HelpText => "heapdomstats ['bysize]";
public override string HelpText => "heapdomstats ['bysize] ['type [<type index or pattern>] ['includederived]]";
}
}

0 comments on commit c710246

Please sign in to comment.