Flutter Engine
The Flutter Engine
frame_timings.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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#ifndef FLUTTER_FLOW_FRAME_TIMINGS_H_
6#define FLUTTER_FLOW_FRAME_TIMINGS_H_
7
8#include <mutex>
9
10#include "flutter/common/settings.h"
11#include "flutter/flow/raster_cache.h"
12#include "flutter/fml/macros.h"
13#include "flutter/fml/status.h"
14#include "flutter/fml/time/time_delta.h"
15#include "flutter/fml/time/time_point.h"
16
17#define TRACE_EVENT_WITH_FRAME_NUMBER(recorder, category_group, name, \
18 flow_id_count, flow_ids) \
19 TRACE_EVENT1_WITH_FLOW_IDS(category_group, name, flow_id_count, flow_ids, \
20 "frame_number", \
21 recorder->GetFrameNumberTraceArg())
22
23namespace flutter {
24
25/// Records timestamps for various phases of a frame rendering process.
26///
27/// Recorder is created on vsync and destroyed after the rasterization of the
28/// frame. This class is thread safe and doesn't require additional
29/// synchronization.
31 public:
32 /// Various states that the recorder can be in. When created the recorder is
33 /// in an unitialized state and transtions in sequential order of the states.
34 // After adding an item to this enum, modify StateToString accordingly.
35 enum class State : uint32_t {
36 kUninitialized,
37 kVsync,
38 kBuildStart,
39 kBuildEnd,
40 kRasterStart,
41 kRasterEnd,
42 };
43
44 /// Default constructor, initializes the recorder with State::kUninitialized.
46
47 /// Constructor with a pre-populated frame number.
48 explicit FrameTimingsRecorder(uint64_t frame_number);
49
51
52 /// Timestamp of the vsync signal.
54
55 /// Timestamp of when the frame was targeted to be presented.
56 ///
57 /// This is typically the next vsync signal timestamp.
59
60 /// Timestamp of when the frame building started.
62
63 /// Timestamp of when the frame was finished building.
65
66 /// Timestamp of when the frame rasterization started.
68
69 /// Timestamp of when the frame rasterization finished.
71
72 /// Timestamp of when the frame rasterization is complete in wall-time.
74
75 /// Duration of the frame build time.
77
78 /// Count of the layer cache entries
79 size_t GetLayerCacheCount() const;
80
81 /// Total Bytes in all layer cache entries
82 size_t GetLayerCacheBytes() const;
83
84 /// Count of the picture cache entries
85 size_t GetPictureCacheCount() const;
86
87 /// Total Bytes in all picture cache entries
88 size_t GetPictureCacheBytes() const;
89
90 /// Records a vsync event.
91 void RecordVsync(fml::TimePoint vsync_start, fml::TimePoint vsync_target);
92
93 /// Records a build start event.
94 void RecordBuildStart(fml::TimePoint build_start);
95
96 /// Records a build end event.
97 void RecordBuildEnd(fml::TimePoint build_end);
98
99 /// Records a raster start event.
100 void RecordRasterStart(fml::TimePoint raster_start);
101
102 /// Clones the recorder until (and including) the specified state.
103 std::unique_ptr<FrameTimingsRecorder> CloneUntil(State state);
104
105 /// Records a raster end event, and builds a `FrameTiming` that summarizes all
106 /// the events. This summary is sent to the framework.
108
109 /// Returns the frame number. Frame number is unique per frame and a frame
110 /// built earlier will have a frame number less than a frame that has been
111 /// built at a later point of time.
112 uint64_t GetFrameNumber() const;
113
114 /// Returns the frame number in a fml tracing friendly format.
115 const char* GetFrameNumberTraceArg() const;
116
117 /// Returns the recorded time from when `RecordRasterEnd` is called.
119
120 /// Asserts in unopt builds that the recorder is current at the specified
121 /// state.
122 ///
123 /// Instead of adding a `GetState` method and asserting on the result, this
124 /// method prevents other logic from relying on the state.
125 ///
126 /// In release builds, this call is a no-op.
127 void AssertInState(State state) const;
128
129 private:
130 FML_FRIEND_TEST(FrameTimingsRecorderTest, ThrowWhenRecordBuildBeforeVsync);
131 FML_FRIEND_TEST(FrameTimingsRecorderTest,
132 ThrowWhenRecordRasterBeforeBuildEnd);
133
134 [[nodiscard]] fml::Status RecordVsyncImpl(fml::TimePoint vsync_start,
135 fml::TimePoint vsync_target);
136 [[nodiscard]] fml::Status RecordBuildStartImpl(fml::TimePoint build_start);
137 [[nodiscard]] fml::Status RecordBuildEndImpl(fml::TimePoint build_end);
138 [[nodiscard]] fml::Status RecordRasterStartImpl(fml::TimePoint raster_start);
139
140 static std::atomic<uint64_t> frame_number_gen_;
141
142 mutable std::mutex state_mutex_;
144
145 const uint64_t frame_number_;
146 const std::string frame_number_trace_arg_val_;
147
148 fml::TimePoint vsync_start_;
149 fml::TimePoint vsync_target_;
150 fml::TimePoint build_start_;
151 fml::TimePoint build_end_;
152 fml::TimePoint raster_start_;
153 fml::TimePoint raster_end_;
154 fml::TimePoint raster_end_wall_time_;
155
156 size_t layer_cache_count_;
157 size_t layer_cache_bytes_;
158 size_t picture_cache_count_;
159 size_t picture_cache_bytes_;
160
161 // Set when `RecordRasterEnd` is called. Cannot be reset once set.
162 FrameTiming timing_;
163
164 FML_DISALLOW_COPY_ASSIGN_AND_MOVE(FrameTimingsRecorder);
165};
166
167} // namespace flutter
168
169#endif // FLUTTER_FLOW_FRAME_TIMINGS_H_
void RecordBuildStart(fml::TimePoint build_start)
Records a build start event.
FrameTiming RecordRasterEnd(const RasterCache *cache=nullptr)
fml::TimePoint GetVsyncStartTime() const
Timestamp of the vsync signal.
fml::TimePoint GetBuildEndTime() const
Timestamp of when the frame was finished building.
fml::TimePoint GetRasterEndTime() const
Timestamp of when the frame rasterization finished.
void RecordRasterStart(fml::TimePoint raster_start)
Records a raster start event.
std::unique_ptr< FrameTimingsRecorder > CloneUntil(State state)
Clones the recorder until (and including) the specified state.
void AssertInState(State state) const
size_t GetPictureCacheCount() const
Count of the picture cache entries.
fml::TimePoint GetRasterEndWallTime() const
Timestamp of when the frame rasterization is complete in wall-time.
size_t GetLayerCacheBytes() const
Total Bytes in all layer cache entries.
void RecordVsync(fml::TimePoint vsync_start, fml::TimePoint vsync_target)
Records a vsync event.
fml::TimePoint GetVsyncTargetTime() const
size_t GetPictureCacheBytes() const
Total Bytes in all picture cache entries.
size_t GetLayerCacheCount() const
Count of the layer cache entries.
void RecordBuildEnd(fml::TimePoint build_end)
Records a build end event.
FrameTimingsRecorder()
Default constructor, initializes the recorder with State::kUninitialized.
fml::TimePoint GetBuildStartTime() const
Timestamp of when the frame building started.
const char * GetFrameNumberTraceArg() const
Returns the frame number in a fml tracing friendly format.
fml::TimePoint GetRasterStartTime() const
Timestamp of when the frame rasterization started.
FrameTiming GetRecordedTime() const
Returns the recorded time from when RecordRasterEnd is called.
fml::TimeDelta GetBuildDuration() const
Duration of the frame build time.
AtkStateType state
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191