Skip to content

Commit 077a790

Browse files
sigmundchCommit Bot
authored andcommitted
[dart2js] Include memory usage in dart2js's output.
This adds a small text on the output of dart2js to indicate how much memory it consumed during compilation. It's implemented via a service protocol API that collects the total heap capacity at the time dart2js is practically done with compilation. Once this is in place, we could change our benchmarks to start using this metric. Change-Id: Iaf4425ef713ce0195474ef4e818149d46ab55e9b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244802 Commit-Queue: Sigmund Cherem <[email protected]> Reviewed-by: Ben Konyi <[email protected]> Reviewed-by: Joshua Litt <[email protected]>
1 parent aae9610 commit 077a790

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// This library contains logic to fetch RAM utilization information.
6+
/// It is implemented as an RPC that connects to the VM's vm_service isolate and
7+
/// all necessary details.
8+
///
9+
/// This is similar to the information that the VM prints when provided the
10+
/// `--print_metrics` flag. However, this API allows us to obtain the data
11+
/// directly while the process is running and embedded in the compiler output
12+
/// (and in the future in dump-info).
13+
///
14+
/// Note that one could alternatively use Process.maxRss instead, however that
15+
/// number may have a lot more variability depending on system conditions.
16+
/// Our goal with this number is not so much to be exact, but to have a good
17+
/// metric we can track overtime and use to detect improvements and regressions.
18+
import 'dart:developer';
19+
import 'package:vm_service/vm_service_io.dart' as vm_service_io;
20+
21+
Future<int> _currentHeapCapacity() async {
22+
final info =
23+
await Service.controlWebServer(enable: true, silenceOutput: true);
24+
final observatoryUri = info.serverUri!;
25+
final wsUri = 'ws://${observatoryUri.authority}${observatoryUri.path}ws';
26+
final vmService = await vm_service_io.vmServiceConnectUri(wsUri);
27+
int sum = 0;
28+
for (final group in (await vmService.getVM()).isolateGroups!) {
29+
final usage = await vmService.getIsolateGroupMemoryUsage(group.id!);
30+
sum += usage.heapCapacity!;
31+
}
32+
vmService.dispose();
33+
return sum;
34+
}
35+
36+
Future<String> currentHeapCapacityInMb() async {
37+
final capacity = await _currentHeapCapacity();
38+
return "${(capacity / (1024 * 1024)).toStringAsFixed(3)} MB";
39+
}

pkg/compiler/lib/src/dart2js.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:front_end/src/api_unstable/dart2js.dart' as fe;
1616
import '../compiler_api.dart' as api;
1717
import '../compiler_api_unmigrated.dart' as api_unmigrated;
1818
import 'commandline_options.dart';
19+
import 'common/ram_usage.dart';
1920
import 'options.dart' show CompilerOptions, FeatureOptions;
2021
import 'source_file_provider.dart';
2122
import 'util/command_line.dart';
@@ -933,7 +934,8 @@ Future<api.CompilationResult> compile(List<String> argv,
933934
RandomAccessFileOutputProvider(out, sourceMapOut,
934935
onInfo: diagnosticHandler.info, onFailure: fail);
935936

936-
api.CompilationResult compilationDone(api.CompilationResult result) {
937+
Future<api.CompilationResult> compilationDone(
938+
api.CompilationResult result) async {
937939
if (!result.isSuccess) {
938940
fail('Compilation failed.');
939941
}
@@ -1060,7 +1062,8 @@ Future<api.CompilationResult> compile(List<String> argv,
10601062
print('$processName '
10611063
'${_formatCharacterCount(inputSize)} $inputName to '
10621064
'${_formatCharacterCount(outputSize)} $outputName in '
1063-
'${_formatDurationAsSeconds(wallclock.elapsed)} seconds');
1065+
'${_formatDurationAsSeconds(wallclock.elapsed)} seconds using '
1066+
'${await currentHeapCapacityInMb()} of memory');
10641067
if (primaryOutputSize != null) {
10651068
diagnosticHandler
10661069
.info('${_formatCharacterCount(primaryOutputSize)} $outputName '

pkg/compiler/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies:
1919
js_runtime: any
2020
js_shared: any
2121
kernel: any
22+
vm_service: any
2223

2324
dev_dependencies:
2425
args: any

0 commit comments

Comments
 (0)