Skip to content

Commit 42dfb9d

Browse files
kenzieschmollcommit-bot@chromium.org
authored andcommitted
Add generateCallGraphWithDominators method to generate a CallGraph from a precompiler trace.
Exposing this functionality makes it possible to use this logic in Dart DevTools. This CL also includes some renames that make the code more readable. When there are both `ProgramInfoNode`s and `CallGraphNode`s in scope, the name "node" is ambiguous. Bug: #43169 Change-Id: Ic8ef04e10c48db011cd28e1786bee34223766e47 Cq-Include-Trybots: luci.dart.try:pkg-linux-release-try,pkg-win-release-try,pkg-mac-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/160342 Commit-Queue: Kenzie Schmoll <[email protected]> Reviewed-by: Vyacheslav Egorov <[email protected]>
1 parent 01024df commit 42dfb9d

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

pkg/vm_snapshot_analysis/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
depth of outputted dependency trees configurable.
66
- Rename `deps-collapse-depth` (formerly `-d`) flag for `summary` command to
77
`deps-start-depth` (now `-s`).
8+
- Add `generateCallGraphWithDominators` method that generates a `CallGraph`
9+
object from precompiler trace.
810

911
## 0.5.4
1012
- Fix bug causing name clash for Type class.

pkg/vm_snapshot_analysis/lib/precompiler_trace.dart

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ class CallGraph {
105105

106106
// Mapping from [ProgramInfoNode] to a corresponding [CallGraphNode] (if any)
107107
// via [ProgramInfoNode.id].
108-
final List<CallGraphNode> _nodeByEntityId;
108+
final List<CallGraphNode> _graphNodeByEntityId;
109109

110-
CallGraph._(this.program, this.nodes, this._nodeByEntityId);
110+
CallGraph._(this.program, this.nodes, this._graphNodeByEntityId);
111111

112112
CallGraphNode get root => nodes.first;
113113

114-
CallGraphNode lookup(ProgramInfoNode node) => _nodeByEntityId[node.id];
114+
CallGraphNode lookup(ProgramInfoNode node) => _graphNodeByEntityId[node.id];
115115

116116
Iterable<CallGraphNode> get dynamicCalls =>
117117
nodes.where((n) => n.isDynamicCallNode);
118118

119119
/// Compute a collapsed version of the call-graph, where
120120
CallGraph collapse(NodeType type, {bool dropCallNodes = false}) {
121-
final nodesByData = <Object, CallGraphNode>{};
122-
final nodeByEntityId = <CallGraphNode>[];
121+
final graphNodesByData = <Object, CallGraphNode>{};
122+
final graphNodeByEntityId = <CallGraphNode>[];
123123

124124
ProgramInfoNode collapsed(ProgramInfoNode nn) {
125125
// Root always collapses onto itself.
@@ -138,24 +138,24 @@ class CallGraph {
138138
return n;
139139
}
140140

141-
CallGraphNode nodeFor(Object data) {
142-
return nodesByData.putIfAbsent(data, () {
143-
final n = CallGraphNode(nodesByData.length, data: data);
141+
CallGraphNode callGraphNodeFor(Object data) {
142+
return graphNodesByData.putIfAbsent(data, () {
143+
final n = CallGraphNode(graphNodesByData.length, data: data);
144144
if (data is ProgramInfoNode) {
145-
if (nodeByEntityId.length <= data.id) {
146-
nodeByEntityId.length = data.id * 2 + 1;
145+
if (graphNodeByEntityId.length <= data.id) {
146+
graphNodeByEntityId.length = data.id * 2 + 1;
147147
}
148-
nodeByEntityId[data.id] = n;
148+
graphNodeByEntityId[data.id] = n;
149149
}
150150
return n;
151151
});
152152
}
153153

154154
final newNodes = nodes.map((n) {
155155
if (n.data is ProgramInfoNode) {
156-
return nodeFor(collapsed(n.data));
156+
return callGraphNodeFor(collapsed(n.data));
157157
} else if (!dropCallNodes) {
158-
return nodeFor(n.data);
158+
return callGraphNodeFor(n.data);
159159
}
160160
}).toList(growable: false);
161161

@@ -170,8 +170,8 @@ class CallGraph {
170170
}
171171
}
172172

173-
return CallGraph._(
174-
program, nodesByData.values.toList(growable: false), nodeByEntityId);
173+
return CallGraph._(program, graphNodesByData.values.toList(growable: false),
174+
graphNodeByEntityId);
175175
}
176176

177177
/// Compute dominator tree of the call-graph.
@@ -529,3 +529,21 @@ class _TraceReader {
529529
name: libraryUri, parent: node, type: NodeType.libraryNode);
530530
}
531531
}
532+
533+
/// Generates a [CallGraph] from the given [precompilerTrace], which is produced
534+
/// by `--trace-precompiler-to`, then collapses it down to the granularity
535+
/// specified by [nodeType], and computes dominators of the resulting graph.
536+
CallGraph generateCallGraphWithDominators(
537+
Object precompilerTrace,
538+
NodeType nodeType,
539+
) {
540+
var callGraph = loadTrace(precompilerTrace);
541+
542+
// Convert call graph into the approximate dependency graph, dropping any
543+
// dynamic and dispatch table based dependencies from the graph and only
544+
// following the static call, field access and allocation edges.
545+
callGraph = callGraph.collapse(nodeType, dropCallNodes: true)
546+
..computeDominators();
547+
548+
return callGraph;
549+
}

pkg/vm_snapshot_analysis/lib/src/commands/summary.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,13 @@ void outputSummary(File input,
187187
(granularity == HistogramType.byLibrary ||
188188
granularity == HistogramType.byPackage)) {
189189
final traceJsonRaw = await loadJsonFromFile(traceJson);
190-
var callGraph = loadTrace(traceJsonRaw);
191-
192-
// Convert call graph into the approximate dependency graph, dropping any
193-
// dynamic and dispatch table based dependencies from the graph and only
194-
// following the static call, field access and allocation edges.
195-
callGraph = callGraph.collapse(
196-
granularity == HistogramType.byLibrary
197-
? NodeType.libraryNode
198-
: NodeType.packageNode,
199-
dropCallNodes: true);
200-
callGraph.computeDominators();
190+
191+
final callGraph = generateCallGraphWithDominators(
192+
traceJsonRaw,
193+
granularity == HistogramType.byLibrary
194+
? NodeType.libraryNode
195+
: NodeType.packageNode,
196+
);
201197

202198
// Compute name mapping from histogram buckets to new coarser buckets, by
203199
// collapsing dependency tree at [depsStartDepth] level: node 'Foo' with

pkg/vm_snapshot_analysis/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: vm_snapshot_analysis
22
description: Utilities for analysing AOT snapshot size.
3-
version: 0.5.5-dev.1
3+
version: 0.5.5
44

55
homepage: https://github.com/dart-lang/sdk/tree/master/pkg/vm_snapshot_analysis
66

0 commit comments

Comments
 (0)