Skip to content

Commit 4b4cabb

Browse files
authored
fire service protocol extension events for frames (flutter#10966)
* fire service protocol extension events for frames * start time in micros * introduce a profile() function; only send frame events when in profile (or debug) modes * moved the profile() function to foundation/profile.dart * refactor to make the change more testable; test the change * fire service protocol events by listening to onFrameInfo * remove the frame event stream; add a devicelab test * remove a todo * final
1 parent 22ccb74 commit 4b4cabb

File tree

6 files changed

+155
-9
lines changed

6 files changed

+155
-9
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:convert';
7+
import 'dart:io';
8+
9+
import 'package:path/path.dart' as path;
10+
import 'package:vm_service_client/vm_service_client.dart';
11+
12+
import 'package:flutter_devicelab/framework/adb.dart';
13+
import 'package:flutter_devicelab/framework/framework.dart';
14+
import 'package:flutter_devicelab/framework/utils.dart';
15+
16+
const int kObservatoryPort = 8888;
17+
18+
void main() {
19+
task(() async {
20+
final Device device = await devices.workingDevice;
21+
await device.unlock();
22+
final Directory appDir = dir(path.join(flutterDirectory.path, 'dev/integration_tests/ui'));
23+
await inDirectory(appDir, () async {
24+
final Completer<Null> ready = new Completer<Null>();
25+
bool ok;
26+
print('run: starting...');
27+
final Process run = await startProcess(
28+
path.join(flutterDirectory.path, 'bin', 'flutter'),
29+
<String>['run', '--verbose', '--observatory-port=$kObservatoryPort', '-d', device.deviceId, 'lib/main.dart'],
30+
);
31+
run.stdout
32+
.transform(UTF8.decoder)
33+
.transform(const LineSplitter())
34+
.listen((String line) {
35+
print('run:stdout: $line');
36+
if (line.contains(new RegExp(r'^\[\s+\] For a more detailed help message, press "h"\. To quit, press "q"\.'))) {
37+
print('run: ready!');
38+
ready.complete();
39+
ok ??= true;
40+
}
41+
});
42+
run.stderr
43+
.transform(UTF8.decoder)
44+
.transform(const LineSplitter())
45+
.listen((String line) {
46+
stderr.writeln('run:stderr: $line');
47+
});
48+
run.exitCode.then((int exitCode) { ok = false; });
49+
await Future.any<dynamic>(<Future<dynamic>>[ ready.future, run.exitCode ]);
50+
if (!ok)
51+
throw 'Failed to run test app.';
52+
53+
final VMServiceClient client = new VMServiceClient.connect('ws://localhost:$kObservatoryPort/ws');
54+
final VM vm = await client.getVM();
55+
final VMIsolateRef isolate = vm.isolates.first;
56+
final Stream<VMExtensionEvent> frameEvents = isolate.onExtensionEvent.where(
57+
(VMExtensionEvent e) => e.kind == 'Flutter.Frame');
58+
59+
print('reassembling app...');
60+
final Future<VMExtensionEvent> frameFuture = frameEvents.first;
61+
await isolate.invokeExtension('ext.flutter.reassemble');
62+
63+
// ensure we get an event
64+
final VMExtensionEvent event = await frameFuture;
65+
print('${event.kind}: ${event.data}');
66+
67+
// validate the fields
68+
// {number: 8, startTime: 0, elapsed: 1437}
69+
expect(event.data['number'] is int);
70+
expect(event.data['number'] >= 0);
71+
expect(event.data['startTime'] is int);
72+
expect(event.data['startTime'] >= 0);
73+
expect(event.data['elapsed'] is int);
74+
expect(event.data['elapsed'] >= 0);
75+
76+
run.stdin.write('q');
77+
final int result = await run.exitCode;
78+
if (result != 0)
79+
throw 'Received unexpected exit code $result from run process.';
80+
});
81+
return new TaskResult.success(null);
82+
});
83+
}
84+
85+
void expect(bool value) {
86+
if (!value)
87+
throw 'failed assertion in service extensions test';
88+
}

dev/devicelab/manifest.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ tasks:
121121
required_agent_capabilities: ["has-android-device"]
122122
flaky: true
123123

124+
service_extensions_test:
125+
description: >
126+
Validates our service protocol extensions.
127+
stage: devicelab
128+
required_agent_capabilities: ["has-android-device"]
129+
flaky: true
130+
124131
android_sample_catalog_generator:
125132
description: >
126133
Builds sample catalog markdown pages and Android screenshots

packages/flutter/lib/foundation.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export 'src/foundation/licenses.dart';
4040
export 'src/foundation/observer_list.dart';
4141
export 'src/foundation/platform.dart';
4242
export 'src/foundation/print.dart';
43+
export 'src/foundation/profile.dart';
4344
export 'src/foundation/serialization.dart';
4445
export 'src/foundation/synchronous_future.dart';
4546
export 'src/foundation/tree_diagnostics_mixin.dart';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:ui';
6+
7+
/// Whether we've been built in release mode.
8+
const bool _kReleaseMode = const bool.fromEnvironment("dart.vm.product");
9+
10+
/// When running in profile mode (or debug mode), invoke the given function.
11+
///
12+
/// In release mode, the function is not invoked.
13+
// TODO(devoncarew): Going forward, we'll want the call to profile() to be tree-shaken out.
14+
void profile(VoidCallback function) {
15+
if (_kReleaseMode)
16+
return;
17+
function();
18+
}

packages/flutter/lib/src/scheduler/binding.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:collection';
7-
import 'dart:developer';
7+
import 'dart:developer' as developer;
88
import 'dart:ui' as ui show window;
99
import 'dart:ui' show VoidCallback;
1010

@@ -550,7 +550,8 @@ abstract class SchedulerBinding extends BindingBase {
550550
}
551551
Duration _currentFrameTimeStamp;
552552

553-
int _debugFrameNumber = 0;
553+
int _profileFrameNumber = 0;
554+
final Stopwatch _profileFrameStopwatch = new Stopwatch();
554555
String _debugBanner;
555556

556557
/// Called by the engine to prepare the framework to produce a new frame.
@@ -577,22 +578,27 @@ abstract class SchedulerBinding extends BindingBase {
577578
/// statements printed during a frame from those printed between frames (e.g.
578579
/// in response to events or timers).
579580
void handleBeginFrame(Duration rawTimeStamp) {
580-
Timeline.startSync('Frame');
581+
developer.Timeline.startSync('Frame');
581582
_firstRawTimeStampInEpoch ??= rawTimeStamp;
582583
_currentFrameTimeStamp = _adjustForEpoch(rawTimeStamp ?? _lastRawTimeStamp);
583584
if (rawTimeStamp != null)
584585
_lastRawTimeStamp = rawTimeStamp;
585586

587+
profile(() {
588+
_profileFrameNumber += 1;
589+
_profileFrameStopwatch.reset();
590+
_profileFrameStopwatch.start();
591+
});
592+
586593
assert(() {
587-
_debugFrameNumber += 1;
588594
if (debugPrintBeginFrameBanner || debugPrintEndFrameBanner) {
589595
final StringBuffer frameTimeStampDescription = new StringBuffer();
590596
if (rawTimeStamp != null) {
591597
_debugDescribeTimeStamp(_currentFrameTimeStamp, frameTimeStampDescription);
592598
} else {
593599
frameTimeStampDescription.write('(warm-up frame)');
594600
}
595-
_debugBanner = '▄▄▄▄▄▄▄▄ Frame ${_debugFrameNumber.toString().padRight(7)} ${frameTimeStampDescription.toString().padLeft(18)} ▄▄▄▄▄▄▄▄';
601+
_debugBanner = '▄▄▄▄▄▄▄▄ Frame ${_profileFrameNumber.toString().padRight(7)} ${frameTimeStampDescription.toString().padLeft(18)} ▄▄▄▄▄▄▄▄';
596602
if (debugPrintBeginFrameBanner)
597603
debugPrint(_debugBanner);
598604
}
@@ -603,7 +609,7 @@ abstract class SchedulerBinding extends BindingBase {
603609
_hasScheduledFrame = false;
604610
try {
605611
// TRANSIENT FRAME CALLBACKS
606-
Timeline.startSync('Animate');
612+
developer.Timeline.startSync('Animate');
607613
_schedulerPhase = SchedulerPhase.transientCallbacks;
608614
final Map<int, _FrameCallbackEntry> callbacks = _transientCallbacks;
609615
_transientCallbacks = <int, _FrameCallbackEntry>{};
@@ -628,7 +634,7 @@ abstract class SchedulerBinding extends BindingBase {
628634
/// useful when working with frame callbacks.
629635
void handleDrawFrame() {
630636
assert(_schedulerPhase == SchedulerPhase.midFrameMicrotasks);
631-
Timeline.finishSync(); // end the "Animate" phase
637+
developer.Timeline.finishSync(); // end the "Animate" phase
632638
try {
633639
// PERSISTENT FRAME CALLBACKS
634640
_schedulerPhase = SchedulerPhase.persistentCallbacks;
@@ -644,14 +650,22 @@ abstract class SchedulerBinding extends BindingBase {
644650
_invokeFrameCallback(callback, _currentFrameTimeStamp);
645651
} finally {
646652
_schedulerPhase = SchedulerPhase.idle;
647-
_currentFrameTimeStamp = null;
648-
Timeline.finishSync();
653+
developer.Timeline.finishSync(); // end the Frame
654+
profile(() {
655+
_profileFrameStopwatch.stop();
656+
developer.postEvent('Flutter.Frame', <String, dynamic>{
657+
'number': _profileFrameNumber,
658+
'startTime': _currentFrameTimeStamp.inMicroseconds,
659+
'elapsed': _profileFrameStopwatch.elapsedMicroseconds
660+
});
661+
});
649662
assert(() {
650663
if (debugPrintEndFrameBanner)
651664
debugPrint('▀' * _debugBanner.length);
652665
_debugBanner = null;
653666
return true;
654667
});
668+
_currentFrameTimeStamp = null;
655669
}
656670

657671
// All frame-related callbacks have been executed. Run lower-priority tasks.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:test/test.dart';
7+
8+
const bool isReleaseMode = const bool.fromEnvironment("dart.vm.product");
9+
10+
void main() {
11+
test("profile invokes its closure in debug or profile mode", () {
12+
int count = 0;
13+
profile(() {
14+
count++;
15+
});
16+
expect(count, isReleaseMode ? 0 : 1);
17+
});
18+
}

0 commit comments

Comments
 (0)