Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 28a3cd2

Browse files
mralephcommit-bot@chromium.org
authored andcommitted
[vm_snapshot_analysis] Add buildComparisonTreemap
This helper method compares two size profiles and returns result as a treemap. Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-linux-release-try,pkg-win-release-try,pkg-mac-release-try Change-Id: I82e5617bc367a2b89d3685ce7f7babc01492b531 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152908 Reviewed-by: Vyacheslav Egorov <[email protected]> Commit-Queue: Vyacheslav Egorov <[email protected]>
1 parent f6acbcd commit 28a3cd2

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

pkg/vm_snapshot_analysis/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.4.0
4+
5+
- Add `buildComparisonTreemap` for constructing treemap representing the diff
6+
between two size profiles.
7+
38
## 0.3.0
49

510
- Extract treemap construction code into a separate library, to make it

pkg/vm_snapshot_analysis/lib/treemap.dart

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,65 @@ Map<String, dynamic> treemapFromJson(Object inputJson,
9393
collapseSingleChildPathNodes: collapseSingleChildPathNodes);
9494
}
9595

96+
/// Convert the given [ProgramInfo] object into a treemap in either
97+
/// [TreemapFormat.collapsed] or [TreemapFormat.simplified] format.
98+
///
99+
/// See [treemapFromJson] for the schema of the returned map object.
100+
Map<String, dynamic> treemapFromInfo(ProgramInfo info,
101+
{TreemapFormat format = TreemapFormat.collapsed,
102+
bool collapseSingleChildPathNodes = true}) {
103+
final root = {'n': '', 'children': {}, 'k': kindPath, 'maxDepth': 0};
104+
_treemapFromInfo(root, info, format: format);
105+
return _flatten(root,
106+
collapseSingleChildPathNodes: collapseSingleChildPathNodes);
107+
}
108+
109+
void _treemapFromInfo(Map<String, dynamic> root, ProgramInfo info,
110+
{TreemapFormat format = TreemapFormat.simplified}) {
111+
if (format != TreemapFormat.collapsed && format != TreemapFormat.simplified) {
112+
throw ArgumentError(
113+
'can only build simplified or collapsed formats from the program info',
114+
);
115+
}
116+
117+
int cumulativeSize(ProgramInfoNode node) {
118+
return (node.size ?? 0) +
119+
node.children.values
120+
.fold<int>(0, (sum, child) => sum + cumulativeSize(child));
121+
}
122+
123+
void recurse(ProgramInfoNode node, String path, Map<String, dynamic> root,
124+
TreemapFormat format) {
125+
if (node.children.isEmpty ||
126+
(node.type == NodeType.functionNode &&
127+
format == TreemapFormat.simplified)) {
128+
// For simple format we remove information about nested functions from
129+
// the output.
130+
_addSymbol(root, path, node.name, cumulativeSize(node));
131+
return;
132+
}
133+
134+
path = path != '' ? '$path/${node.name}' : node.name;
135+
_addSymbol(root, path, '<self>', node.size);
136+
for (var child in node.children.values) {
137+
recurse(child, path, root, format);
138+
}
139+
}
140+
141+
_addSymbol(root, '', info.root.name, info.root.size);
142+
for (var child in info.root.children.values) {
143+
recurse(child, '', root, format);
144+
}
145+
}
146+
96147
void _treemapFromSnapshot(Map<String, dynamic> root, v8_profile.Snapshot snap,
97148
{TreemapFormat format = TreemapFormat.objectType}) {
98149
final info = v8_profile.toProgramInfo(snap);
99150

100151
// For collapsed and simple formats there is no need to traverse snapshot
101152
// nodes. Just recurse into [ProgramInfo] structure instead.
102153
if (format == TreemapFormat.collapsed || format == TreemapFormat.simplified) {
103-
int cumulativeSize(ProgramInfoNode node) {
104-
return (node.size ?? 0) +
105-
node.children.values
106-
.fold<int>(0, (sum, child) => sum + cumulativeSize(child));
107-
}
108-
109-
void recurse(ProgramInfoNode node, String path) {
110-
if (node.children.isEmpty ||
111-
(node.type == NodeType.functionNode &&
112-
format == TreemapFormat.simplified)) {
113-
// For simple format we remove information about nested functions from
114-
// the output.
115-
_addSymbol(root, path, node.name, cumulativeSize(node));
116-
return;
117-
}
118-
119-
path = path != '' ? '$path/${node.name}' : node.name;
120-
_addSymbol(root, path, '<self>', node.size);
121-
for (var child in node.children.values) {
122-
recurse(child, path);
123-
}
124-
}
125-
126-
_addSymbol(root, '', info.root.name, info.root.size);
127-
for (var child in info.root.children.values) {
128-
recurse(child, '');
129-
}
154+
_treemapFromInfo(root, info, format: format);
130155
return;
131156
}
132157

pkg/vm_snapshot_analysis/lib/utils.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:vm_snapshot_analysis/ascii_table.dart';
1010
import 'package:vm_snapshot_analysis/program_info.dart';
1111
import 'package:vm_snapshot_analysis/instruction_sizes.dart'
1212
as instruction_sizes;
13+
import 'package:vm_snapshot_analysis/treemap.dart';
1314
import 'package:vm_snapshot_analysis/v8_profile.dart' as v8_profile;
1415

1516
Future<Object> loadJson(File input) async {
@@ -32,6 +33,20 @@ Future<ProgramInfo> loadProgramInfo(File input,
3233
}
3334
}
3435

36+
/// Compare two size profiles and return result of the comparison as a treemap.
37+
Future<Map<String, dynamic>> buildComparisonTreemap(File oldJson, File newJson,
38+
{TreemapFormat format = TreemapFormat.collapsed,
39+
bool collapseAnonymousClosures = false}) async {
40+
final oldSizes = await loadProgramInfo(oldJson,
41+
collapseAnonymousClosures: collapseAnonymousClosures);
42+
final newSizes = await loadProgramInfo(newJson,
43+
collapseAnonymousClosures: collapseAnonymousClosures);
44+
45+
final diff = computeDiff(oldSizes, newSizes);
46+
47+
return treemapFromInfo(diff, format: format);
48+
}
49+
3550
void printHistogram(ProgramInfo info, Histogram histogram,
3651
{Iterable<String> prefix = const [],
3752
Iterable<String> suffix = const [],

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 working with non-symbolic stack traces.
3-
version: 0.3.0
3+
version: 0.4.0
44

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

0 commit comments

Comments
 (0)