File size: 1,852 Bytes
4e917e6 346a42c 4e917e6 346a42c 4e917e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // 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 [TimelineEvent]s corresponding to [kGCRootEvents] category.
///
/// A sample event (some fields have been omitted for brevity):
/// ```json
/// {
/// "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;
}
|