Skip to content

Commit f92884c

Browse files
authored
Rename debugProfilePlatformChannels to a constant that works in release mode (#134922)
When it comes to startup profiling, it is very helpful to look at platform channels. `debugProfilePlatformChannels` today only works in debug and profile mode. Unfortunately, using profile mode is less accurate for startup profiling, because of the service isolate introducing additional overhead. This PR allows this toggle to work in release mode. Note that there are two parts to `debugProfilePlatformChannels`: - Adding timeline events - Logging statistics about platform channels I also considered adding a separate toggle to limit the scope of this change to the former, but that seems like complexity that we might not need at this time. Towards #102189
1 parent a0406cc commit f92884c

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

packages/flutter/lib/src/services/debug.dart

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ export 'hardware_keyboard.dart' show KeyDataTransitMode;
1515
/// of their extent of support for keyboard API.
1616
KeyDataTransitMode? debugKeyEventSimulatorTransitModeOverride;
1717

18-
/// Profile and print statistics on Platform Channel usage.
19-
///
20-
/// When this is true statistics about the usage of Platform Channels will be
21-
/// printed out periodically to the console and Timeline events will show the
22-
/// time between sending and receiving a message (encoding and decoding time
23-
/// excluded).
24-
///
25-
/// The statistics include the total bytes transmitted and the average number of
26-
/// bytes per invocation in the last quantum. "Up" means in the direction of
27-
/// Flutter to the host platform, "down" is the host platform to flutter.
28-
bool debugProfilePlatformChannels = false;
29-
3018
/// Setting to true will cause extensive logging to occur when key events are
3119
/// received.
3220
///
@@ -46,7 +34,7 @@ bool debugAssertAllServicesVarsUnset(String reason) {
4634
if (debugKeyEventSimulatorTransitModeOverride != null) {
4735
throw FlutterError(reason);
4836
}
49-
if (debugProfilePlatformChannels || debugPrintKeyboardEvents) {
37+
if (debugPrintKeyboardEvents) {
5038
throw FlutterError(reason);
5139
}
5240
return true;

packages/flutter/lib/src/services/platform_channel.dart

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import '_background_isolate_binary_messenger_io.dart'
1212

1313
import 'binary_messenger.dart';
1414
import 'binding.dart';
15-
import 'debug.dart' show debugProfilePlatformChannels;
1615
import 'message_codec.dart';
1716
import 'message_codecs.dart';
1817

@@ -23,9 +22,21 @@ export 'binary_messenger.dart' show BinaryMessenger;
2322
export 'binding.dart' show RootIsolateToken;
2423
export 'message_codec.dart' show MessageCodec, MethodCall, MethodCodec;
2524

26-
bool _debugProfilePlatformChannelsIsRunning = false;
27-
const Duration _debugProfilePlatformChannelsRate = Duration(seconds: 1);
28-
final Expando<BinaryMessenger> _debugBinaryMessengers = Expando<BinaryMessenger>();
25+
/// Profile and print statistics on Platform Channel usage.
26+
///
27+
/// When this is true statistics about the usage of Platform Channels will be
28+
/// printed out periodically to the console and Timeline events will show the
29+
/// time between sending and receiving a message (encoding and decoding time
30+
/// excluded).
31+
///
32+
/// The statistics include the total bytes transmitted and the average number of
33+
/// bytes per invocation in the last quantum. "Up" means in the direction of
34+
/// Flutter to the host platform, "down" is the host platform to flutter.
35+
const bool kProfilePlatformChannels = false;
36+
37+
bool _profilePlatformChannelsIsRunning = false;
38+
const Duration _profilePlatformChannelsRate = Duration(seconds: 1);
39+
final Expando<BinaryMessenger> _profiledBinaryMessengers = Expando<BinaryMessenger>();
2940

3041
class _ProfiledBinaryMessenger implements BinaryMessenger {
3142
const _ProfiledBinaryMessenger(this.proxy, this.channelTypeName, this.codecTypeName);
@@ -40,17 +51,12 @@ class _ProfiledBinaryMessenger implements BinaryMessenger {
4051

4152
Future<ByteData?>? sendWithPostfix(String channel, String postfix, ByteData? message) async {
4253
_debugRecordUpStream(channelTypeName, '$channel$postfix', codecTypeName, message);
43-
TimelineTask? debugTimelineTask;
44-
if (!kReleaseMode) {
45-
debugTimelineTask = TimelineTask()..start('Platform Channel send $channel$postfix');
46-
}
54+
final TimelineTask timelineTask = TimelineTask()..start('Platform Channel send $channel$postfix');
4755
final ByteData? result;
4856
try {
4957
result = await proxy.send(channel, message);
5058
} finally {
51-
if (!kReleaseMode) {
52-
debugTimelineTask!.finish();
53-
}
59+
timelineTask.finish();
5460
}
5561
_debugRecordDownStream(channelTypeName, '$channel$postfix', codecTypeName, result);
5662
return result;
@@ -93,17 +99,17 @@ class _PlatformChannelStats {
9399
double get averageDownPayload => _downBytes / _downCount;
94100
}
95101

96-
final Map<String, _PlatformChannelStats> _debugProfilePlatformChannelsStats = <String, _PlatformChannelStats>{};
102+
final Map<String, _PlatformChannelStats> _profilePlatformChannelsStats = <String, _PlatformChannelStats>{};
97103

98104
Future<void> _debugLaunchProfilePlatformChannels() async {
99-
if (!_debugProfilePlatformChannelsIsRunning) {
100-
_debugProfilePlatformChannelsIsRunning = true;
101-
await Future<dynamic>.delayed(_debugProfilePlatformChannelsRate);
102-
_debugProfilePlatformChannelsIsRunning = false;
105+
if (!_profilePlatformChannelsIsRunning) {
106+
_profilePlatformChannelsIsRunning = true;
107+
await Future<dynamic>.delayed(_profilePlatformChannelsRate);
108+
_profilePlatformChannelsIsRunning = false;
103109
final StringBuffer log = StringBuffer();
104110
log.writeln('Platform Channel Stats:');
105111
final List<_PlatformChannelStats> allStats =
106-
_debugProfilePlatformChannelsStats.values.toList();
112+
_profilePlatformChannelsStats.values.toList();
107113
// Sort highest combined bandwidth first.
108114
allStats.sort((_PlatformChannelStats x, _PlatformChannelStats y) =>
109115
(y.upBytes + y.downBytes) - (x.upBytes + x.downBytes));
@@ -112,14 +118,14 @@ Future<void> _debugLaunchProfilePlatformChannels() async {
112118
' (name:"${stats.channel}" type:"${stats.type}" codec:"${stats.codec}" upBytes:${stats.upBytes} upBytes_avg:${stats.averageUpPayload.toStringAsFixed(1)} downBytes:${stats.downBytes} downBytes_avg:${stats.averageDownPayload.toStringAsFixed(1)})');
113119
}
114120
debugPrint(log.toString());
115-
_debugProfilePlatformChannelsStats.clear();
121+
_profilePlatformChannelsStats.clear();
116122
}
117123
}
118124

119125
void _debugRecordUpStream(String channelTypeName, String name,
120126
String codecTypeName, ByteData? bytes) {
121127
final _PlatformChannelStats stats =
122-
_debugProfilePlatformChannelsStats[name] ??=
128+
_profilePlatformChannelsStats[name] ??=
123129
_PlatformChannelStats(name, codecTypeName, channelTypeName);
124130
stats.addUpStream(bytes?.lengthInBytes ?? 0);
125131
_debugLaunchProfilePlatformChannels();
@@ -128,7 +134,7 @@ void _debugRecordUpStream(String channelTypeName, String name,
128134
void _debugRecordDownStream(String channelTypeName, String name,
129135
String codecTypeName, ByteData? bytes) {
130136
final _PlatformChannelStats stats =
131-
_debugProfilePlatformChannelsStats[name] ??=
137+
_profilePlatformChannelsStats[name] ??=
132138
_PlatformChannelStats(name, codecTypeName, channelTypeName);
133139
stats.addDownStream(bytes?.lengthInBytes ?? 0);
134140
_debugLaunchProfilePlatformChannels();
@@ -184,8 +190,8 @@ class BasicMessageChannel<T> {
184190
/// [BackgroundIsolateBinaryMessenger.ensureInitialized].
185191
BinaryMessenger get binaryMessenger {
186192
final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger();
187-
return !kReleaseMode && debugProfilePlatformChannels
188-
? _debugBinaryMessengers[this] ??= _ProfiledBinaryMessenger(
193+
return kProfilePlatformChannels
194+
? _profiledBinaryMessengers[this] ??= _ProfiledBinaryMessenger(
189195
// ignore: no_runtimetype_tostring
190196
result, runtimeType.toString(), codec.runtimeType.toString())
191197
: result;
@@ -273,8 +279,8 @@ class MethodChannel {
273279
/// [BackgroundIsolateBinaryMessenger.ensureInitialized].
274280
BinaryMessenger get binaryMessenger {
275281
final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger();
276-
return !kReleaseMode && debugProfilePlatformChannels
277-
? _debugBinaryMessengers[this] ??= _ProfiledBinaryMessenger(
282+
return kProfilePlatformChannels
283+
? _profiledBinaryMessengers[this] ??= _ProfiledBinaryMessenger(
278284
// ignore: no_runtimetype_tostring
279285
result, runtimeType.toString(), codec.runtimeType.toString())
280286
: result;
@@ -304,7 +310,7 @@ class MethodChannel {
304310
Future<T?> _invokeMethod<T>(String method, { required bool missingOk, dynamic arguments }) async {
305311
final ByteData input = codec.encodeMethodCall(MethodCall(method, arguments));
306312
final ByteData? result =
307-
!kReleaseMode && debugProfilePlatformChannels ?
313+
kProfilePlatformChannels ?
308314
await (binaryMessenger as _ProfiledBinaryMessenger).sendWithPostfix(name, '#$method', input) :
309315
await binaryMessenger.send(name, input);
310316
if (result == null) {

0 commit comments

Comments
 (0)