Compute the total time spent on UI thread for GC (#95692)

This commit is contained in:
Kaushik Iska
2021-12-22 17:04:35 -05:00
committed by GitHub
parent 43732e4597
commit 8e9bca802f
3 changed files with 99 additions and 34 deletions

View File

@@ -0,0 +1,59 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'timeline.dart';
/// GC related timeline events.
///
/// All these events occur only on the UI thread and are non overlapping.
const Set<String> kGCRootEvents = <String>{
'CollectNewGeneration',
'CollectOldGeneration',
'EvacuateNewGeneration',
'StartConcurrentMark',
};
/// Summarizes [TimelineEvents]s corresponding to [kGCRootEvents] category.
///
/// A sample event (some fields have been omitted for brevity):
/// ```
/// {
/// "name": "StartConcurrentMarking",
/// "cat": "GC",
/// "ts": 3240710599608,
/// }
/// ```
/// This class provides methods to compute the total time spend in GC on
/// the UI thread.
class GCSummarizer {
GCSummarizer._(this.totalGCTimeMillis);
/// Creates a [GCSummarizer] given the timeline events.
static GCSummarizer fromEvents(List<TimelineEvent> gcEvents) {
double totalGCTimeMillis = 0;
TimelineEvent? lastGCBeginEvent;
for (final TimelineEvent event in gcEvents) {
if (!kGCRootEvents.contains(event.name)) {
continue;
}
if (event.phase == 'B') {
lastGCBeginEvent = event;
} else if (lastGCBeginEvent != null) {
// These events must not overlap.
assert(event.name == lastGCBeginEvent.name,
'Expected "${lastGCBeginEvent.name}" got "${event.name}"');
final double st = lastGCBeginEvent.timestampMicros!.toDouble();
final double end = event.timestampMicros!.toDouble();
lastGCBeginEvent = null;
totalGCTimeMillis += (end - st) / 1000;
}
}
return GCSummarizer._(totalGCTimeMillis);
}
/// Total time spent doing GC on the UI thread.
final double totalGCTimeMillis;
}

View File

@@ -9,6 +9,7 @@ import 'package:file/file.dart';
import 'package:path/path.dart' as path;
import 'common.dart';
import 'gc_summarizer.dart';
import 'percentile_utils.dart';
import 'profiling_summarizer.dart';
import 'raster_cache_summarizer.dart';
@@ -218,6 +219,7 @@ class TimelineSummary {
final VsyncFrameLagSummarizer vsyncFrameLagSummarizer = _vsyncFrameLagSummarizer();
final Map<String, dynamic> profilingSummary = _profilingSummarizer().summarize();
final RasterCacheSummarizer rasterCacheSummarizer = _rasterCacheSummarizer();
final GCSummarizer gcSummarizer = _gcSummarizer();
final Map<String, dynamic> timelineSummary = <String, dynamic>{
'average_frame_build_time_millis': computeAverageFrameBuildTimeMillis(),
@@ -268,6 +270,7 @@ class TimelineSummary {
'90th_percentile_picture_cache_memory': rasterCacheSummarizer.computePercentilePictureMemory(90.0),
'99th_percentile_picture_cache_memory': rasterCacheSummarizer.computePercentilePictureMemory(99.0),
'worst_picture_cache_memory': rasterCacheSummarizer.computeWorstPictureMemory(),
'total_ui_gc_time': gcSummarizer.totalGCTimeMillis,
};
timelineSummary.addAll(profilingSummary);
@@ -432,4 +435,6 @@ class TimelineSummary {
VsyncFrameLagSummarizer _vsyncFrameLagSummarizer() => VsyncFrameLagSummarizer(_extractEventsWithNames(kVsyncTimelineEventNames));
RasterCacheSummarizer _rasterCacheSummarizer() => RasterCacheSummarizer(_extractNamedEvents(kRasterCacheEvent));
GCSummarizer _gcSummarizer() => GCSummarizer.fromEvents(_extractEventsWithNames(kGCRootEvents));
}