Skip to content

Commit 516634a

Browse files
authored
Add --dart-aot-perf and --verbose-durations. (#4394)
1 parent 7d35eb6 commit 516634a

16 files changed

+138
-37
lines changed

build_runner/CHANGELOG.md

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

3+
- Add `--dart-aot-perf` flag for profiling on Linux. Use it with `--force-aot`.
4+
It runs the builders under the `perf` profiling tool which writes to
5+
`perf.data`.
6+
- Add `--verbose-durations` flag that logs durations with greater precision.
37
- Improved debugging instructions in README.md.
8+
- Bug fix: fix line wrapping in `build_runner` args usage output.
49

510
## 2.12.2
611

build_runner/lib/src/bootstrap/bootstrapper.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Bootstrapper {
5555
Future<int> run(
5656
BuiltList<String> arguments, {
5757
required Iterable<String> jitVmArgs,
58+
required bool dartAotPerf,
5859
Iterable<String>? experiments,
5960
bool retryCompileFailures = false,
6061
}) async {
@@ -121,6 +122,7 @@ class Bootstrapper {
121122
aotSnapshot: entrypointAotPath,
122123
arguments: arguments,
123124
message: buildProcessState.serialize(),
125+
runUnderPerf: dartAotPerf,
124126
)
125127
: await ParentProcess.runAndSend(
126128
script: entrypointDillPath,

build_runner/lib/src/bootstrap/processes.dart

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,31 @@ class ParentProcess {
5050
required String aotSnapshot,
5151
required Iterable<String> arguments,
5252
required String message,
53+
required bool runUnderPerf,
5354
}) async {
54-
return await _runAndSend(
55-
executable: p.join(
56-
p.dirname(Platform.resolvedExecutable),
57-
'dartaotruntime',
58-
),
59-
arguments: [aotSnapshot, ...arguments],
60-
message: message,
55+
final dartAotRuntime = p.join(
56+
p.dirname(Platform.resolvedExecutable),
57+
'dartaotruntime',
6158
);
59+
return runUnderPerf
60+
? await _runAndSend(
61+
executable: 'perf',
62+
arguments: [
63+
'record',
64+
'-g',
65+
'--output',
66+
'perf.data',
67+
dartAotRuntime,
68+
aotSnapshot,
69+
...arguments,
70+
],
71+
message: message,
72+
)
73+
: await _runAndSend(
74+
executable: dartAotRuntime,
75+
arguments: [aotSnapshot, ...arguments],
76+
message: message,
77+
);
6278
}
6379

6480
static Future<RunAndSendResult> _runAndSend({

build_runner/lib/src/build_plan/build_options.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ class BuildOptions {
2323
final String? configKey;
2424
final BuiltList<String> enableExperiments;
2525
final bool enableLowResourcesMode;
26+
final bool dartAotPerf;
2627
final bool forceAot;
2728
final bool forceJit;
2829
final bool isReleaseBuild;
2930
final String? logPerformanceDir;
3031
final bool outputSymlinksOnly;
3132
final bool trackPerformance;
3233
final bool verbose;
34+
final bool verboseDurations;
3335
final bool workspace;
3436

3537
late final bool anyMergedOutputDirectory = buildDirs.any(
@@ -41,6 +43,7 @@ class BuildOptions {
4143
required this.builderConfigOverrides,
4244
required this.buildFilters,
4345
required this.configKey,
46+
required this.dartAotPerf,
4447
required this.enableExperiments,
4548
required this.enableLowResourcesMode,
4649
required this.forceAot,
@@ -50,6 +53,7 @@ class BuildOptions {
5053
required this.outputSymlinksOnly,
5154
required this.trackPerformance,
5255
required this.verbose,
56+
required this.verboseDurations,
5357
required this.workspace,
5458
});
5559

@@ -59,25 +63,28 @@ class BuildOptions {
5963
/// command line arg parsing configuration.
6064
@visibleForTesting
6165
factory BuildOptions.forTests({
62-
bool? forceAot,
63-
bool? forceJit,
6466
BuiltMap<String, BuiltMap<String, Object?>>? builderConfigOverrides,
6567
BuiltSet<BuildDirectory>? buildDirs,
6668
BuiltSet<BuildFilter>? buildFilters,
6769
String? configKey,
70+
bool? dartAotPerf,
6871
BuiltList<String>? enableExperiments,
6972
bool? enableLowResourcesMode,
73+
bool? forceAot,
74+
bool? forceJit,
7075
bool? isReleaseBuild,
7176
String? logPerformanceDir,
7277
bool? outputSymlinksOnly,
7378
bool? trackPerformance,
7479
bool? verbose,
80+
bool? verboseDurations,
7581
bool? workspace,
7682
}) => BuildOptions(
7783
builderConfigOverrides: builderConfigOverrides ?? BuiltMap(),
7884
buildDirs: buildDirs ?? BuiltSet(),
7985
buildFilters: buildFilters ?? BuiltSet(),
8086
configKey: configKey,
87+
dartAotPerf: dartAotPerf ?? false,
8188
enableExperiments: enableExperiments ?? BuiltList(),
8289
enableLowResourcesMode: enableLowResourcesMode ?? false,
8390
forceAot: forceAot ?? false,
@@ -87,6 +94,7 @@ class BuildOptions {
8794
outputSymlinksOnly: outputSymlinksOnly ?? false,
8895
trackPerformance: trackPerformance ?? false,
8996
verbose: verbose ?? false,
97+
verboseDurations: verboseDurations ?? false,
9098
workspace: workspace ?? false,
9199
);
92100

@@ -115,6 +123,8 @@ class BuildOptions {
115123
),
116124
buildFilters: _parseBuildFilters(commandLine, rootPackage: rootPackage),
117125
configKey: commandLine.config,
126+
// Only available on Linux.
127+
dartAotPerf: commandLine.dartAotPerf ?? false,
118128
enableExperiments: commandLine.enableExperiments!,
119129
enableLowResourcesMode: commandLine.lowResourcesMode!,
120130
forceAot: commandLine.forceAot!,
@@ -125,6 +135,7 @@ class BuildOptions {
125135
trackPerformance:
126136
commandLine.trackPerformance! || commandLine.logPerformance != null,
127137
verbose: commandLine.verbose!,
138+
verboseDurations: commandLine.verboseDurations!,
128139
workspace: commandLine.workspace!,
129140
);
130141

@@ -146,6 +157,7 @@ class BuildOptions {
146157
builderConfigOverrides: builderConfigOverrides,
147158
buildFilters: buildFilters ?? this.buildFilters,
148159
configKey: configKey,
160+
dartAotPerf: dartAotPerf,
149161
enableExperiments: enableExperiments,
150162
enableLowResourcesMode: enableLowResourcesMode,
151163
forceAot: forceAot,
@@ -155,6 +167,7 @@ class BuildOptions {
155167
outputSymlinksOnly: outputSymlinksOnly,
156168
trackPerformance: trackPerformance,
157169
verbose: verbose,
170+
verboseDurations: verboseDurations,
158171
workspace: workspace,
159172
);
160173
}

build_runner/lib/src/build_runner.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ class BuildRunner {
174174
}) async {
175175
buildLog.configuration = buildLog.configuration.rebuild((b) {
176176
b.mode = commandLine.type.buildLogMode;
177+
b.verbose = commandLine.verbose;
178+
b.verboseDurations = commandLine.verboseDurations;
177179
});
178180

179181
final bootstrapper = Bootstrapper(
@@ -182,6 +184,7 @@ class BuildRunner {
182184
);
183185
return await bootstrapper.run(
184186
arguments,
187+
dartAotPerf: commandLine.dartAotPerf ?? false,
185188
jitVmArgs: commandLine.jitVmArgs ?? const Iterable.empty(),
186189
experiments: commandLine.enableExperiments,
187190
retryCompileFailures:

build_runner/lib/src/build_runner_command_line.dart

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,23 @@ class BuildRunnerCommandLine {
4343
final BuiltList<String>? buildFilter;
4444
final String? buildMode;
4545
final String? config;
46+
final bool? dartAotPerf;
4647
final BuiltList<String>? defines;
4748
final BuiltList<String>? enableExperiments;
4849
final bool? forceAot;
4950
final bool? forceJit;
50-
final BuiltList<String>? jitVmArgs;
5151
final String? hostname;
52+
final BuiltList<String>? jitVmArgs;
5253
final bool? liveReload;
5354
final String? logPerformance;
5455
final bool? logRequests;
5556
final bool? lowResourcesMode;
5657
final BuiltList<String>? outputs;
5758
final bool? release;
58-
final bool? trackPerformance;
5959
final bool? symlink;
60+
final bool? trackPerformance;
6061
final bool? verbose;
62+
final bool? verboseDurations;
6163
final bool? workspace;
6264

6365
static Future<BuildRunnerCommandLine?> parse(Iterable<String> arguments) =>
@@ -69,21 +71,23 @@ class BuildRunnerCommandLine {
6971
buildFilter = argResults.listNamed(buildFilterOption),
7072
buildMode = argResults.stringNamed(buildModeFlag),
7173
config = argResults.stringNamed(configOption),
74+
dartAotPerf = argResults.boolNamed(dartAotPerfOption),
7275
defines = argResults.listNamed(defineOption),
7376
enableExperiments = argResults.listNamed(enableExperimentOption),
7477
forceAot = argResults.boolNamed(forceAotOption),
7578
forceJit = argResults.boolNamed(forceJitOption),
76-
jitVmArgs = argResults.listNamed(dartJitVmArgOption),
7779
hostname = argResults.stringNamed(hostnameOption),
80+
jitVmArgs = argResults.listNamed(dartJitVmArgOption),
7881
liveReload = argResults.boolNamed(liveReloadOption),
7982
logPerformance = argResults.stringNamed(logPerformanceOption),
8083
logRequests = argResults.boolNamed(logRequestsOption),
8184
lowResourcesMode = argResults.boolNamed(lowResourcesModeOption),
8285
outputs = argResults.listNamed(outputOption),
8386
release = argResults.boolNamed(releaseOption),
84-
trackPerformance = argResults.boolNamed(trackPerformanceOption),
8587
symlink = argResults.boolNamed(symlinkOption),
88+
trackPerformance = argResults.boolNamed(trackPerformanceOption),
8689
verbose = argResults.boolNamed(verboseOption),
90+
verboseDurations = argResults.boolNamed(verboseDurationsOption),
8791
// Only "build" and "watch" support --workspace, default to false for
8892
// other commands.
8993
workspace = argResults.boolNamed(workspaceOption) ?? false;
@@ -133,6 +137,7 @@ const deleteFilesByDefaultOption = 'delete-conflicting-outputs';
133137
const enableExperimentOption = 'enable-experiment';
134138
const forceAotOption = 'force-aot';
135139
const forceJitOption = 'force-jit';
140+
const dartAotPerfOption = 'dart-aot-perf';
136141
const dartJitVmArgOption = 'dart-jit-vm-arg';
137142
const hostnameOption = 'hostname';
138143
const liveReloadOption = 'live-reload';
@@ -141,8 +146,9 @@ const logRequestsOption = 'log-requests';
141146
const lowResourcesModeOption = 'low-resources-mode';
142147
const outputOption = 'output';
143148
const releaseOption = 'release';
144-
const trackPerformanceOption = 'track-performance';
145149
const symlinkOption = 'symlink';
150+
const trackPerformanceOption = 'track-performance';
151+
const verboseDurationsOption = 'verbose-durations';
146152
const verboseOption = 'verbose';
147153
const workspaceOption = 'workspace';
148154

@@ -153,10 +159,7 @@ class _CommandRunner extends CommandRunner<BuildRunnerCommandLine> {
153159
: super(
154160
'build_runner',
155161
'Dart build tool.',
156-
usageLineLength:
157-
buildProcessState.stdio.hasTerminal
158-
? buildProcessState.stdio.terminalColumns
159-
: 80,
162+
usageLineLength: buildProcessState.stdio.terminalColumns,
160163
) {
161164
addCommand(_build);
162165
addCommand(_clean);
@@ -170,7 +173,15 @@ class _CommandRunner extends CommandRunner<BuildRunnerCommandLine> {
170173

171174
final _build = _Build();
172175

173-
class _Build extends Command<BuildRunnerCommandLine> {
176+
/// [Command] with `ArgParser.usageLineLength` set.
177+
abstract class _Command<T> extends Command<T> {
178+
@override
179+
final ArgParser argParser = ArgParser(
180+
usageLineLength: buildProcessState.stdio.terminalColumns,
181+
);
182+
}
183+
184+
class _Build extends _Command<BuildRunnerCommandLine> {
174185
_Build() {
175186
addBuildArgs(argParser, symlinksDefault: false, supportWorkspace: true);
176187
}
@@ -228,6 +239,11 @@ class _Build extends Command<BuildRunnerCommandLine> {
228239
negatable: true,
229240
defaultsTo: false,
230241
)
242+
..addFlag(
243+
verboseDurationsOption,
244+
negatable: false,
245+
help: 'Logs durations with greater precision.',
246+
)
231247
..addOption(
232248
logPerformanceOption,
233249
help:
@@ -272,7 +288,7 @@ class _Build extends Command<BuildRunnerCommandLine> {
272288
help:
273289
'An explicit filter of files to build. Relative paths and '
274290
'`package:` uris are supported, including glob syntax for paths '
275-
'portions (but not package names).\n\n'
291+
'portions (but not package names). '
276292
'If multiple filters are applied then outputs matching any '
277293
'filter will be built (they do not need to match all filters).',
278294
)
@@ -284,16 +300,24 @@ class _Build extends Command<BuildRunnerCommandLine> {
284300
dartJitVmArgOption,
285301
help:
286302
'Flags to pass to `dart run` when launching the inner build '
287-
'script\n.'
303+
'script. '
288304
'For example, `--dart-jit-vm-arg "--observe" '
289305
'--dart-jit-vm-arg "--pause-isolates-on-start"` would start the '
290306
'build script with a debugger attached to it.',
291307
);
308+
if (Platform.isLinux) {
309+
argParser.addFlag(
310+
dartAotPerfOption,
311+
negatable: false,
312+
help: 'Run AOT-compiled builders under the `perf` profiling tool.',
313+
);
314+
}
315+
292316
if (supportWorkspace) {
293317
argParser.addFlag(
294318
workspaceOption,
295319
defaultsTo: false,
296-
negatable: true,
320+
negatable: false,
297321
help: 'Build all packages in the current workspace.',
298322
);
299323
}
@@ -315,7 +339,7 @@ class _Build extends Command<BuildRunnerCommandLine> {
315339

316340
final _clean = _Clean();
317341

318-
class _Clean extends Command<BuildRunnerCommandLine> {
342+
class _Clean extends _Command<BuildRunnerCommandLine> {
319343
@override
320344
String get name => 'clean';
321345

@@ -330,7 +354,7 @@ class _Clean extends Command<BuildRunnerCommandLine> {
330354

331355
final _daemon = _Daemon();
332356

333-
class _Daemon extends Command<BuildRunnerCommandLine> {
357+
class _Daemon extends _Command<BuildRunnerCommandLine> {
334358
@override
335359
String get description => 'Starts the build daemon.';
336360

@@ -367,7 +391,7 @@ class _Daemon extends Command<BuildRunnerCommandLine> {
367391

368392
final _run = _Run();
369393

370-
class _Run extends Command<BuildRunnerCommandLine> {
394+
class _Run extends _Command<BuildRunnerCommandLine> {
371395
_Run() {
372396
_Build.addBuildArgs(
373397
argParser,
@@ -394,7 +418,7 @@ class _Run extends Command<BuildRunnerCommandLine> {
394418

395419
final _serve = _Serve();
396420

397-
class _Serve extends Command<BuildRunnerCommandLine> {
421+
class _Serve extends _Command<BuildRunnerCommandLine> {
398422
_Serve() {
399423
_Build.addBuildArgs(
400424
argParser,
@@ -438,7 +462,7 @@ class _Serve extends Command<BuildRunnerCommandLine> {
438462

439463
final _test = _Test();
440464

441-
class _Test extends Command<BuildRunnerCommandLine> {
465+
class _Test extends _Command<BuildRunnerCommandLine> {
442466
_Test() {
443467
_Build.addBuildArgs(
444468
argParser,
@@ -465,7 +489,7 @@ class _Test extends Command<BuildRunnerCommandLine> {
465489

466490
final _watch = _Watch();
467491

468-
class _Watch extends Command<BuildRunnerCommandLine> {
492+
class _Watch extends _Command<BuildRunnerCommandLine> {
469493
_Watch() {
470494
_Build.addBuildArgs(
471495
argParser,

build_runner/lib/src/commands/build_command.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BuildCommand implements BuildRunnerCommand {
4343
buildLog.configuration = buildLog.configuration.rebuild((b) {
4444
b.mode = BuildLogMode.build;
4545
b.verbose = buildOptions.verbose;
46+
b.verboseDurations = buildOptions.verboseDurations;
4647
b.onLog = testingOverrides.onLog;
4748
});
4849

0 commit comments

Comments
 (0)