Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d415876

Browse files
authored
Do not depend on Dart in FML (#32846)
1 parent c6ea046 commit d415876

File tree

11 files changed

+102
-63
lines changed

11 files changed

+102
-63
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,6 @@ FILE: ../../../flutter/fml/thread_local_unittests.cc
369369
FILE: ../../../flutter/fml/thread_unittests.cc
370370
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.cc
371371
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.h
372-
FILE: ../../../flutter/fml/time/dart_timestamp_provider.cc
373-
FILE: ../../../flutter/fml/time/dart_timestamp_provider.h
374372
FILE: ../../../flutter/fml/time/time_delta.h
375373
FILE: ../../../flutter/fml/time/time_delta_unittest.cc
376374
FILE: ../../../flutter/fml/time/time_point.cc
@@ -1103,6 +1101,8 @@ FILE: ../../../flutter/runtime/dart_service_isolate.h
11031101
FILE: ../../../flutter/runtime/dart_service_isolate_unittests.cc
11041102
FILE: ../../../flutter/runtime/dart_snapshot.cc
11051103
FILE: ../../../flutter/runtime/dart_snapshot.h
1104+
FILE: ../../../flutter/runtime/dart_timestamp_provider.cc
1105+
FILE: ../../../flutter/runtime/dart_timestamp_provider.h
11061106
FILE: ../../../flutter/runtime/dart_vm.cc
11071107
FILE: ../../../flutter/runtime/dart_vm.h
11081108
FILE: ../../../flutter/runtime/dart_vm_data.cc

fml/BUILD.gn

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ source_set("fml") {
8484
"thread.h",
8585
"thread_local.cc",
8686
"thread_local.h",
87-
"time/dart_timestamp_provider.cc",
88-
"time/dart_timestamp_provider.h",
8987
"time/time_delta.h",
9088
"time/time_point.cc",
9189
"time/time_point.h",
@@ -291,7 +289,6 @@ if (enable_unittests) {
291289
deps = [
292290
"//flutter/benchmarking",
293291
"//flutter/fml",
294-
"//flutter/runtime:libdart",
295292
]
296293
}
297294

@@ -349,6 +346,7 @@ if (enable_unittests) {
349346
":fml_fixtures",
350347
"//flutter/fml",
351348
"//flutter/fml/dart",
349+
"//flutter/runtime",
352350
"//flutter/runtime:libdart",
353351
"//flutter/testing",
354352
]

fml/time/time_point.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#include "flutter/fml/time/time_point.h"
66

7+
#include <atomic>
8+
79
#include "flutter/fml/build_config.h"
810
#include "flutter/fml/logging.h"
9-
#include "flutter/fml/time/dart_timestamp_provider.h"
1011

1112
#if defined(OS_FUCHSIA)
1213
#include <zircon/syscalls.h>
@@ -27,17 +28,30 @@ TimePoint TimePoint::CurrentWallTime() {
2728
return Now();
2829
}
2930

31+
void TimePoint::SetClockSource(ClockSource source) {}
3032
#else
3133

34+
namespace {
35+
std::atomic<TimePoint::ClockSource> gSteadyClockSource;
36+
}
37+
3238
template <typename Clock, typename Duration>
3339
static int64_t NanosSinceEpoch(
3440
std::chrono::time_point<Clock, Duration> time_point) {
3541
const auto elapsed = time_point.time_since_epoch();
3642
return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
3743
}
3844

45+
void TimePoint::SetClockSource(ClockSource source) {
46+
gSteadyClockSource = source;
47+
}
48+
3949
TimePoint TimePoint::Now() {
40-
return DartTimelineTicksSinceEpoch();
50+
if (gSteadyClockSource) {
51+
return gSteadyClockSource.load()();
52+
}
53+
const int64_t nanos = NanosSinceEpoch(std::chrono::steady_clock::now());
54+
return TimePoint(nanos);
4155
}
4256

4357
TimePoint TimePoint::CurrentWallTime() {

fml/time/time_point.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FLUTTER_FML_TIME_TIME_POINT_H_
77

88
#include <cstdint>
9+
#include <functional>
910
#include <iosfwd>
1011

1112
#include "flutter/fml/time/time_delta.h"
@@ -20,9 +21,13 @@ namespace fml {
2021
// reboots.
2122
class TimePoint {
2223
public:
24+
using ClockSource = TimePoint (*)();
25+
2326
// Default TimePoint with internal value 0 (epoch).
2427
constexpr TimePoint() = default;
2528

29+
static void SetClockSource(ClockSource source);
30+
2631
static TimePoint Now();
2732

2833
static TimePoint CurrentWallTime();

fml/time/time_point_unittest.cc

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

55
#include "flutter/fml/time/chrono_timestamp_provider.h"
66

7-
#include "flutter/fml/time/dart_timestamp_provider.h"
7+
#include "flutter/runtime/dart_timestamp_provider.h"
88

99
#include <thread>
1010

@@ -20,11 +20,11 @@ TEST(TimePoint, Control) {
2020

2121
TEST(TimePoint, DartClockIsMonotonic) {
2222
using namespace std::chrono_literals;
23-
const auto t1 = DartTimelineTicksSinceEpoch();
23+
const auto t1 = flutter::DartTimelineTicksSinceEpoch();
2424
std::this_thread::sleep_for(1us);
25-
const auto t2 = DartTimelineTicksSinceEpoch();
25+
const auto t2 = flutter::DartTimelineTicksSinceEpoch();
2626
std::this_thread::sleep_for(1us);
27-
const auto t3 = DartTimelineTicksSinceEpoch();
27+
const auto t3 = flutter::DartTimelineTicksSinceEpoch();
2828
EXPECT_LT(TimePoint::Min(), t1);
2929
EXPECT_LE(t1, t2);
3030
EXPECT_LE(t2, t3);

fml/trace_event.cc

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ namespace tracing {
1818
#if FLUTTER_TIMELINE_ENABLED
1919

2020
namespace {
21+
22+
int64_t DefaultMicrosSource() {
23+
return -1;
24+
}
25+
2126
AsciiTrie gAllowlist;
2227
std::atomic<TimelineEventHandler> gTimelineEventHandler;
28+
std::atomic<TimelineMicrosSource> gTimelineMicrosSource = DefaultMicrosSource;
2329

2430
inline void FlutterTimelineEvent(const char* label,
2531
int64_t timestamp0,
@@ -45,6 +51,10 @@ void TraceSetTimelineEventHandler(TimelineEventHandler handler) {
4551
gTimelineEventHandler = handler;
4652
}
4753

54+
void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {
55+
gTimelineMicrosSource = source;
56+
}
57+
4858
size_t TraceNonce() {
4959
static std::atomic_size_t gLastItem;
5060
return ++gLastItem;
@@ -83,19 +93,19 @@ void TraceTimelineEvent(TraceArg category_group,
8393
Dart_Timeline_Event_Type type,
8494
const std::vector<const char*>& c_names,
8595
const std::vector<std::string>& values) {
86-
TraceTimelineEvent(category_group, // group
87-
name, // name
88-
Dart_TimelineGetMicros(), // timestamp_micros
89-
identifier, // identifier
90-
type, // type
91-
c_names, // names
92-
values // values
96+
TraceTimelineEvent(category_group, // group
97+
name, // name
98+
gTimelineMicrosSource.load()(), // timestamp_micros
99+
identifier, // identifier
100+
type, // type
101+
c_names, // names
102+
values // values
93103
);
94104
}
95105

96106
void TraceEvent0(TraceArg category_group, TraceArg name) {
97-
FlutterTimelineEvent(name, // label
98-
Dart_TimelineGetMicros(), // timestamp0
107+
FlutterTimelineEvent(name, // label
108+
gTimelineMicrosSource.load()(), // timestamp0
99109
0, // timestamp1_or_async_id
100110
Dart_Timeline_Event_Begin, // event type
101111
0, // argument_count
@@ -110,8 +120,8 @@ void TraceEvent1(TraceArg category_group,
110120
TraceArg arg1_val) {
111121
const char* arg_names[] = {arg1_name};
112122
const char* arg_values[] = {arg1_val};
113-
FlutterTimelineEvent(name, // label
114-
Dart_TimelineGetMicros(), // timestamp0
123+
FlutterTimelineEvent(name, // label
124+
gTimelineMicrosSource.load()(), // timestamp0
115125
0, // timestamp1_or_async_id
116126
Dart_Timeline_Event_Begin, // event type
117127
1, // argument_count
@@ -128,8 +138,8 @@ void TraceEvent2(TraceArg category_group,
128138
TraceArg arg2_val) {
129139
const char* arg_names[] = {arg1_name, arg2_name};
130140
const char* arg_values[] = {arg1_val, arg2_val};
131-
FlutterTimelineEvent(name, // label
132-
Dart_TimelineGetMicros(), // timestamp0
141+
FlutterTimelineEvent(name, // label
142+
gTimelineMicrosSource.load()(), // timestamp0
133143
0, // timestamp1_or_async_id
134144
Dart_Timeline_Event_Begin, // event type
135145
2, // argument_count
@@ -139,22 +149,22 @@ void TraceEvent2(TraceArg category_group,
139149
}
140150

141151
void TraceEventEnd(TraceArg name) {
142-
FlutterTimelineEvent(name, // label
143-
Dart_TimelineGetMicros(), // timestamp0
144-
0, // timestamp1_or_async_id
145-
Dart_Timeline_Event_End, // event type
146-
0, // argument_count
147-
nullptr, // argument_names
148-
nullptr // argument_values
152+
FlutterTimelineEvent(name, // label
153+
gTimelineMicrosSource.load()(), // timestamp0
154+
0, // timestamp1_or_async_id
155+
Dart_Timeline_Event_End, // event type
156+
0, // argument_count
157+
nullptr, // argument_names
158+
nullptr // argument_values
149159
);
150160
}
151161

152162
void TraceEventAsyncBegin0(TraceArg category_group,
153163
TraceArg name,
154164
TraceIDArg id) {
155-
FlutterTimelineEvent(name, // label
156-
Dart_TimelineGetMicros(), // timestamp0
157-
id, // timestamp1_or_async_id
165+
FlutterTimelineEvent(name, // label
166+
gTimelineMicrosSource.load()(), // timestamp0
167+
id, // timestamp1_or_async_id
158168
Dart_Timeline_Event_Async_Begin, // event type
159169
0, // argument_count
160170
nullptr, // argument_names
@@ -165,8 +175,8 @@ void TraceEventAsyncBegin0(TraceArg category_group,
165175
void TraceEventAsyncEnd0(TraceArg category_group,
166176
TraceArg name,
167177
TraceIDArg id) {
168-
FlutterTimelineEvent(name, // label
169-
Dart_TimelineGetMicros(), // timestamp0
178+
FlutterTimelineEvent(name, // label
179+
gTimelineMicrosSource.load()(), // timestamp0
170180
id, // timestamp1_or_async_id
171181
Dart_Timeline_Event_Async_End, // event type
172182
0, // argument_count
@@ -182,9 +192,9 @@ void TraceEventAsyncBegin1(TraceArg category_group,
182192
TraceArg arg1_val) {
183193
const char* arg_names[] = {arg1_name};
184194
const char* arg_values[] = {arg1_val};
185-
FlutterTimelineEvent(name, // label
186-
Dart_TimelineGetMicros(), // timestamp0
187-
id, // timestamp1_or_async_id
195+
FlutterTimelineEvent(name, // label
196+
gTimelineMicrosSource.load()(), // timestamp0
197+
id, // timestamp1_or_async_id
188198
Dart_Timeline_Event_Async_Begin, // event type
189199
1, // argument_count
190200
arg_names, // argument_names
@@ -199,8 +209,8 @@ void TraceEventAsyncEnd1(TraceArg category_group,
199209
TraceArg arg1_val) {
200210
const char* arg_names[] = {arg1_name};
201211
const char* arg_values[] = {arg1_val};
202-
FlutterTimelineEvent(name, // label
203-
Dart_TimelineGetMicros(), // timestamp0
212+
FlutterTimelineEvent(name, // label
213+
gTimelineMicrosSource.load()(), // timestamp0
204214
id, // timestamp1_or_async_id
205215
Dart_Timeline_Event_Async_End, // event type
206216
1, // argument_count
@@ -210,8 +220,8 @@ void TraceEventAsyncEnd1(TraceArg category_group,
210220
}
211221

212222
void TraceEventInstant0(TraceArg category_group, TraceArg name) {
213-
FlutterTimelineEvent(name, // label
214-
Dart_TimelineGetMicros(), // timestamp0
223+
FlutterTimelineEvent(name, // label
224+
gTimelineMicrosSource.load()(), // timestamp0
215225
0, // timestamp1_or_async_id
216226
Dart_Timeline_Event_Instant, // event type
217227
0, // argument_count
@@ -226,8 +236,8 @@ void TraceEventInstant1(TraceArg category_group,
226236
TraceArg arg1_val) {
227237
const char* arg_names[] = {arg1_name};
228238
const char* arg_values[] = {arg1_val};
229-
FlutterTimelineEvent(name, // label
230-
Dart_TimelineGetMicros(), // timestamp0
239+
FlutterTimelineEvent(name, // label
240+
gTimelineMicrosSource.load()(), // timestamp0
231241
0, // timestamp1_or_async_id
232242
Dart_Timeline_Event_Instant, // event type
233243
1, // argument_count
@@ -244,8 +254,8 @@ void TraceEventInstant2(TraceArg category_group,
244254
TraceArg arg2_val) {
245255
const char* arg_names[] = {arg1_name, arg2_name};
246256
const char* arg_values[] = {arg1_val, arg2_val};
247-
FlutterTimelineEvent(name, // label
248-
Dart_TimelineGetMicros(), // timestamp0
257+
FlutterTimelineEvent(name, // label
258+
gTimelineMicrosSource.load()(), // timestamp0
249259
0, // timestamp1_or_async_id
250260
Dart_Timeline_Event_Instant, // event type
251261
2, // argument_count
@@ -257,9 +267,9 @@ void TraceEventInstant2(TraceArg category_group,
257267
void TraceEventFlowBegin0(TraceArg category_group,
258268
TraceArg name,
259269
TraceIDArg id) {
260-
FlutterTimelineEvent(name, // label
261-
Dart_TimelineGetMicros(), // timestamp0
262-
id, // timestamp1_or_async_id
270+
FlutterTimelineEvent(name, // label
271+
gTimelineMicrosSource.load()(), // timestamp0
272+
id, // timestamp1_or_async_id
263273
Dart_Timeline_Event_Flow_Begin, // event type
264274
0, // argument_count
265275
nullptr, // argument_names
@@ -270,8 +280,8 @@ void TraceEventFlowBegin0(TraceArg category_group,
270280
void TraceEventFlowStep0(TraceArg category_group,
271281
TraceArg name,
272282
TraceIDArg id) {
273-
FlutterTimelineEvent(name, // label
274-
Dart_TimelineGetMicros(), // timestamp0
283+
FlutterTimelineEvent(name, // label
284+
gTimelineMicrosSource.load()(), // timestamp0
275285
id, // timestamp1_or_async_id
276286
Dart_Timeline_Event_Flow_Step, // event type
277287
0, // argument_count
@@ -281,8 +291,8 @@ void TraceEventFlowStep0(TraceArg category_group,
281291
}
282292

283293
void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) {
284-
FlutterTimelineEvent(name, // label
285-
Dart_TimelineGetMicros(), // timestamp0
294+
FlutterTimelineEvent(name, // label
295+
gTimelineMicrosSource.load()(), // timestamp0
286296
id, // timestamp1_or_async_id
287297
Dart_Timeline_Event_Flow_End, // event type
288298
0, // argument_count
@@ -297,6 +307,8 @@ void TraceSetAllowlist(const std::vector<std::string>& allowlist) {}
297307

298308
void TraceSetTimelineEventHandler(TimelineEventHandler handler) {}
299309

310+
void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {}
311+
300312
size_t TraceNonce() {
301313
return 0;
302314
}

fml/trace_event.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,12 @@ typedef void (*TimelineEventHandler)(const char*,
155155
const char**,
156156
const char**);
157157

158+
using TimelineMicrosSource = int64_t (*)();
159+
158160
void TraceSetTimelineEventHandler(TimelineEventHandler handler);
159161

162+
void TraceSetTimelineMicrosSource(TimelineMicrosSource source);
163+
160164
void TraceTimelineEvent(TraceArg category_group,
161165
TraceArg name,
162166
int64_t timestamp_micros,

runtime/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ source_set("runtime") {
5757
"dart_service_isolate.h",
5858
"dart_snapshot.cc",
5959
"dart_snapshot.h",
60+
"dart_timestamp_provider.cc",
61+
"dart_timestamp_provider.h",
6062
"dart_vm.cc",
6163
"dart_vm.h",
6264
"dart_vm_data.cc",
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include "flutter/fml/time/dart_timestamp_provider.h"
5+
#include "dart_timestamp_provider.h"
66

77
#include "dart_tools_api.h"
88

9-
namespace fml {
9+
namespace flutter {
1010

1111
DartTimestampProvider::DartTimestampProvider() = default;
1212

@@ -35,4 +35,4 @@ fml::TimePoint DartTimelineTicksSinceEpoch() {
3535
return DartTimestampProvider::Instance().Now();
3636
}
3737

38-
} // namespace fml
38+
} // namespace flutter

0 commit comments

Comments
 (0)