From f51504259abffaf577f788902ea1f585a3b79804 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 21 Apr 2022 19:22:45 -0700 Subject: [PATCH] Do not depend on Dart in FML (flutter/engine#32846) --- .../ci/licenses_golden/licenses_flutter | 4 +- engine/src/flutter/fml/BUILD.gn | 4 +- engine/src/flutter/fml/time/time_point.cc | 18 +++- engine/src/flutter/fml/time/time_point.h | 5 + .../flutter/fml/time/time_point_unittest.cc | 8 +- engine/src/flutter/fml/trace_event.cc | 98 +++++++++++-------- engine/src/flutter/fml/trace_event.h | 4 + engine/src/flutter/runtime/BUILD.gn | 2 + .../dart_timestamp_provider.cc | 6 +- .../dart_timestamp_provider.h | 12 +-- .../flutter/runtime/dart_vm_initializer.cc | 4 + 11 files changed, 102 insertions(+), 63 deletions(-) rename engine/src/flutter/{fml/time => runtime}/dart_timestamp_provider.cc (92%) rename engine/src/flutter/{fml/time => runtime}/dart_timestamp_provider.h (76%) diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 2ed46278fec..02d16d077bc 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -369,8 +369,6 @@ FILE: ../../../flutter/fml/thread_local_unittests.cc FILE: ../../../flutter/fml/thread_unittests.cc FILE: ../../../flutter/fml/time/chrono_timestamp_provider.cc FILE: ../../../flutter/fml/time/chrono_timestamp_provider.h -FILE: ../../../flutter/fml/time/dart_timestamp_provider.cc -FILE: ../../../flutter/fml/time/dart_timestamp_provider.h FILE: ../../../flutter/fml/time/time_delta.h FILE: ../../../flutter/fml/time/time_delta_unittest.cc FILE: ../../../flutter/fml/time/time_point.cc @@ -1103,6 +1101,8 @@ FILE: ../../../flutter/runtime/dart_service_isolate.h FILE: ../../../flutter/runtime/dart_service_isolate_unittests.cc FILE: ../../../flutter/runtime/dart_snapshot.cc FILE: ../../../flutter/runtime/dart_snapshot.h +FILE: ../../../flutter/runtime/dart_timestamp_provider.cc +FILE: ../../../flutter/runtime/dart_timestamp_provider.h FILE: ../../../flutter/runtime/dart_vm.cc FILE: ../../../flutter/runtime/dart_vm.h FILE: ../../../flutter/runtime/dart_vm_data.cc diff --git a/engine/src/flutter/fml/BUILD.gn b/engine/src/flutter/fml/BUILD.gn index 71f3f929a54..7446042f98d 100644 --- a/engine/src/flutter/fml/BUILD.gn +++ b/engine/src/flutter/fml/BUILD.gn @@ -84,8 +84,6 @@ source_set("fml") { "thread.h", "thread_local.cc", "thread_local.h", - "time/dart_timestamp_provider.cc", - "time/dart_timestamp_provider.h", "time/time_delta.h", "time/time_point.cc", "time/time_point.h", @@ -291,7 +289,6 @@ if (enable_unittests) { deps = [ "//flutter/benchmarking", "//flutter/fml", - "//flutter/runtime:libdart", ] } @@ -349,6 +346,7 @@ if (enable_unittests) { ":fml_fixtures", "//flutter/fml", "//flutter/fml/dart", + "//flutter/runtime", "//flutter/runtime:libdart", "//flutter/testing", ] diff --git a/engine/src/flutter/fml/time/time_point.cc b/engine/src/flutter/fml/time/time_point.cc index e3bd652ce01..75735dbe6e6 100644 --- a/engine/src/flutter/fml/time/time_point.cc +++ b/engine/src/flutter/fml/time/time_point.cc @@ -4,9 +4,10 @@ #include "flutter/fml/time/time_point.h" +#include + #include "flutter/fml/build_config.h" #include "flutter/fml/logging.h" -#include "flutter/fml/time/dart_timestamp_provider.h" #if defined(OS_FUCHSIA) #include @@ -27,8 +28,13 @@ TimePoint TimePoint::CurrentWallTime() { return Now(); } +void TimePoint::SetClockSource(ClockSource source) {} #else +namespace { +std::atomic gSteadyClockSource; +} + template static int64_t NanosSinceEpoch( std::chrono::time_point time_point) { @@ -36,8 +42,16 @@ static int64_t NanosSinceEpoch( return std::chrono::duration_cast(elapsed).count(); } +void TimePoint::SetClockSource(ClockSource source) { + gSteadyClockSource = source; +} + TimePoint TimePoint::Now() { - return DartTimelineTicksSinceEpoch(); + if (gSteadyClockSource) { + return gSteadyClockSource.load()(); + } + const int64_t nanos = NanosSinceEpoch(std::chrono::steady_clock::now()); + return TimePoint(nanos); } TimePoint TimePoint::CurrentWallTime() { diff --git a/engine/src/flutter/fml/time/time_point.h b/engine/src/flutter/fml/time/time_point.h index 1960234f7c9..812fcdfa4c4 100644 --- a/engine/src/flutter/fml/time/time_point.h +++ b/engine/src/flutter/fml/time/time_point.h @@ -6,6 +6,7 @@ #define FLUTTER_FML_TIME_TIME_POINT_H_ #include +#include #include #include "flutter/fml/time/time_delta.h" @@ -20,9 +21,13 @@ namespace fml { // reboots. class TimePoint { public: + using ClockSource = TimePoint (*)(); + // Default TimePoint with internal value 0 (epoch). constexpr TimePoint() = default; + static void SetClockSource(ClockSource source); + static TimePoint Now(); static TimePoint CurrentWallTime(); diff --git a/engine/src/flutter/fml/time/time_point_unittest.cc b/engine/src/flutter/fml/time/time_point_unittest.cc index 32b35ed287f..01e9a00fea3 100644 --- a/engine/src/flutter/fml/time/time_point_unittest.cc +++ b/engine/src/flutter/fml/time/time_point_unittest.cc @@ -4,7 +4,7 @@ #include "flutter/fml/time/chrono_timestamp_provider.h" -#include "flutter/fml/time/dart_timestamp_provider.h" +#include "flutter/runtime/dart_timestamp_provider.h" #include @@ -20,11 +20,11 @@ TEST(TimePoint, Control) { TEST(TimePoint, DartClockIsMonotonic) { using namespace std::chrono_literals; - const auto t1 = DartTimelineTicksSinceEpoch(); + const auto t1 = flutter::DartTimelineTicksSinceEpoch(); std::this_thread::sleep_for(1us); - const auto t2 = DartTimelineTicksSinceEpoch(); + const auto t2 = flutter::DartTimelineTicksSinceEpoch(); std::this_thread::sleep_for(1us); - const auto t3 = DartTimelineTicksSinceEpoch(); + const auto t3 = flutter::DartTimelineTicksSinceEpoch(); EXPECT_LT(TimePoint::Min(), t1); EXPECT_LE(t1, t2); EXPECT_LE(t2, t3); diff --git a/engine/src/flutter/fml/trace_event.cc b/engine/src/flutter/fml/trace_event.cc index 20acb36306a..4c27bbee176 100644 --- a/engine/src/flutter/fml/trace_event.cc +++ b/engine/src/flutter/fml/trace_event.cc @@ -18,8 +18,14 @@ namespace tracing { #if FLUTTER_TIMELINE_ENABLED namespace { + +int64_t DefaultMicrosSource() { + return -1; +} + AsciiTrie gAllowlist; std::atomic gTimelineEventHandler; +std::atomic gTimelineMicrosSource = DefaultMicrosSource; inline void FlutterTimelineEvent(const char* label, int64_t timestamp0, @@ -45,6 +51,10 @@ void TraceSetTimelineEventHandler(TimelineEventHandler handler) { gTimelineEventHandler = handler; } +void TraceSetTimelineMicrosSource(TimelineMicrosSource source) { + gTimelineMicrosSource = source; +} + size_t TraceNonce() { static std::atomic_size_t gLastItem; return ++gLastItem; @@ -83,19 +93,19 @@ void TraceTimelineEvent(TraceArg category_group, Dart_Timeline_Event_Type type, const std::vector& c_names, const std::vector& values) { - TraceTimelineEvent(category_group, // group - name, // name - Dart_TimelineGetMicros(), // timestamp_micros - identifier, // identifier - type, // type - c_names, // names - values // values + TraceTimelineEvent(category_group, // group + name, // name + gTimelineMicrosSource.load()(), // timestamp_micros + identifier, // identifier + type, // type + c_names, // names + values // values ); } void TraceEvent0(TraceArg category_group, TraceArg name) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 0, // timestamp1_or_async_id Dart_Timeline_Event_Begin, // event type 0, // argument_count @@ -110,8 +120,8 @@ void TraceEvent1(TraceArg category_group, TraceArg arg1_val) { const char* arg_names[] = {arg1_name}; const char* arg_values[] = {arg1_val}; - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 0, // timestamp1_or_async_id Dart_Timeline_Event_Begin, // event type 1, // argument_count @@ -128,8 +138,8 @@ void TraceEvent2(TraceArg category_group, TraceArg arg2_val) { const char* arg_names[] = {arg1_name, arg2_name}; const char* arg_values[] = {arg1_val, arg2_val}; - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 0, // timestamp1_or_async_id Dart_Timeline_Event_Begin, // event type 2, // argument_count @@ -139,22 +149,22 @@ void TraceEvent2(TraceArg category_group, } void TraceEventEnd(TraceArg name) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 - 0, // timestamp1_or_async_id - Dart_Timeline_Event_End, // event type - 0, // argument_count - nullptr, // argument_names - nullptr // argument_values + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 + 0, // timestamp1_or_async_id + Dart_Timeline_Event_End, // event type + 0, // argument_count + nullptr, // argument_names + nullptr // argument_values ); } void TraceEventAsyncBegin0(TraceArg category_group, TraceArg name, TraceIDArg id) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 - id, // timestamp1_or_async_id + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 + id, // timestamp1_or_async_id Dart_Timeline_Event_Async_Begin, // event type 0, // argument_count nullptr, // argument_names @@ -165,8 +175,8 @@ void TraceEventAsyncBegin0(TraceArg category_group, void TraceEventAsyncEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 id, // timestamp1_or_async_id Dart_Timeline_Event_Async_End, // event type 0, // argument_count @@ -182,9 +192,9 @@ void TraceEventAsyncBegin1(TraceArg category_group, TraceArg arg1_val) { const char* arg_names[] = {arg1_name}; const char* arg_values[] = {arg1_val}; - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 - id, // timestamp1_or_async_id + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 + id, // timestamp1_or_async_id Dart_Timeline_Event_Async_Begin, // event type 1, // argument_count arg_names, // argument_names @@ -199,8 +209,8 @@ void TraceEventAsyncEnd1(TraceArg category_group, TraceArg arg1_val) { const char* arg_names[] = {arg1_name}; const char* arg_values[] = {arg1_val}; - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 id, // timestamp1_or_async_id Dart_Timeline_Event_Async_End, // event type 1, // argument_count @@ -210,8 +220,8 @@ void TraceEventAsyncEnd1(TraceArg category_group, } void TraceEventInstant0(TraceArg category_group, TraceArg name) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 0, // timestamp1_or_async_id Dart_Timeline_Event_Instant, // event type 0, // argument_count @@ -226,8 +236,8 @@ void TraceEventInstant1(TraceArg category_group, TraceArg arg1_val) { const char* arg_names[] = {arg1_name}; const char* arg_values[] = {arg1_val}; - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 0, // timestamp1_or_async_id Dart_Timeline_Event_Instant, // event type 1, // argument_count @@ -244,8 +254,8 @@ void TraceEventInstant2(TraceArg category_group, TraceArg arg2_val) { const char* arg_names[] = {arg1_name, arg2_name}; const char* arg_values[] = {arg1_val, arg2_val}; - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 0, // timestamp1_or_async_id Dart_Timeline_Event_Instant, // event type 2, // argument_count @@ -257,9 +267,9 @@ void TraceEventInstant2(TraceArg category_group, void TraceEventFlowBegin0(TraceArg category_group, TraceArg name, TraceIDArg id) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 - id, // timestamp1_or_async_id + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 + id, // timestamp1_or_async_id Dart_Timeline_Event_Flow_Begin, // event type 0, // argument_count nullptr, // argument_names @@ -270,8 +280,8 @@ void TraceEventFlowBegin0(TraceArg category_group, void TraceEventFlowStep0(TraceArg category_group, TraceArg name, TraceIDArg id) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 id, // timestamp1_or_async_id Dart_Timeline_Event_Flow_Step, // event type 0, // argument_count @@ -281,8 +291,8 @@ void TraceEventFlowStep0(TraceArg category_group, } void TraceEventFlowEnd0(TraceArg category_group, TraceArg name, TraceIDArg id) { - FlutterTimelineEvent(name, // label - Dart_TimelineGetMicros(), // timestamp0 + FlutterTimelineEvent(name, // label + gTimelineMicrosSource.load()(), // timestamp0 id, // timestamp1_or_async_id Dart_Timeline_Event_Flow_End, // event type 0, // argument_count @@ -297,6 +307,8 @@ void TraceSetAllowlist(const std::vector& allowlist) {} void TraceSetTimelineEventHandler(TimelineEventHandler handler) {} +void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {} + size_t TraceNonce() { return 0; } diff --git a/engine/src/flutter/fml/trace_event.h b/engine/src/flutter/fml/trace_event.h index 2c41ce45c3a..de12d5cc91d 100644 --- a/engine/src/flutter/fml/trace_event.h +++ b/engine/src/flutter/fml/trace_event.h @@ -155,8 +155,12 @@ typedef void (*TimelineEventHandler)(const char*, const char**, const char**); +using TimelineMicrosSource = int64_t (*)(); + void TraceSetTimelineEventHandler(TimelineEventHandler handler); +void TraceSetTimelineMicrosSource(TimelineMicrosSource source); + void TraceTimelineEvent(TraceArg category_group, TraceArg name, int64_t timestamp_micros, diff --git a/engine/src/flutter/runtime/BUILD.gn b/engine/src/flutter/runtime/BUILD.gn index 2db3f12dbd3..1d88b2340c1 100644 --- a/engine/src/flutter/runtime/BUILD.gn +++ b/engine/src/flutter/runtime/BUILD.gn @@ -57,6 +57,8 @@ source_set("runtime") { "dart_service_isolate.h", "dart_snapshot.cc", "dart_snapshot.h", + "dart_timestamp_provider.cc", + "dart_timestamp_provider.h", "dart_vm.cc", "dart_vm.h", "dart_vm_data.cc", diff --git a/engine/src/flutter/fml/time/dart_timestamp_provider.cc b/engine/src/flutter/runtime/dart_timestamp_provider.cc similarity index 92% rename from engine/src/flutter/fml/time/dart_timestamp_provider.cc rename to engine/src/flutter/runtime/dart_timestamp_provider.cc index 2c5de9d4d8f..238c40dfa42 100644 --- a/engine/src/flutter/fml/time/dart_timestamp_provider.cc +++ b/engine/src/flutter/runtime/dart_timestamp_provider.cc @@ -2,11 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/fml/time/dart_timestamp_provider.h" +#include "dart_timestamp_provider.h" #include "dart_tools_api.h" -namespace fml { +namespace flutter { DartTimestampProvider::DartTimestampProvider() = default; @@ -35,4 +35,4 @@ fml::TimePoint DartTimelineTicksSinceEpoch() { return DartTimestampProvider::Instance().Now(); } -} // namespace fml +} // namespace flutter diff --git a/engine/src/flutter/fml/time/dart_timestamp_provider.h b/engine/src/flutter/runtime/dart_timestamp_provider.h similarity index 76% rename from engine/src/flutter/fml/time/dart_timestamp_provider.h rename to engine/src/flutter/runtime/dart_timestamp_provider.h index 97ab6b1a8ef..98fa47fafc2 100644 --- a/engine/src/flutter/fml/time/dart_timestamp_provider.h +++ b/engine/src/flutter/runtime/dart_timestamp_provider.h @@ -2,20 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_ -#define FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_ +#ifndef FLUTTER_RUNTIME_DART_TIMESTAMP_PROVIDER_H_ +#define FLUTTER_RUNTIME_DART_TIMESTAMP_PROVIDER_H_ #include "flutter/fml/time/timestamp_provider.h" #include "flutter/fml/macros.h" #include "flutter/fml/time/time_point.h" -namespace fml { +namespace flutter { fml::TimePoint DartTimelineTicksSinceEpoch(); /// TimestampProvider implementation that is backed by Dart_TimelineGetTicks -class DartTimestampProvider : TimestampProvider { +class DartTimestampProvider : fml::TimestampProvider { public: static DartTimestampProvider& Instance() { static DartTimestampProvider instance; @@ -36,6 +36,6 @@ class DartTimestampProvider : TimestampProvider { FML_DISALLOW_COPY_AND_ASSIGN(DartTimestampProvider); }; -} // namespace fml +} // namespace flutter -#endif // FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_ +#endif // FLUTTER_RUNTIME_DART_TIMESTAMP_PROVIDER_H_ diff --git a/engine/src/flutter/runtime/dart_vm_initializer.cc b/engine/src/flutter/runtime/dart_vm_initializer.cc index 28148dcbbaf..8c1c44f87fc 100644 --- a/engine/src/flutter/runtime/dart_vm_initializer.cc +++ b/engine/src/flutter/runtime/dart_vm_initializer.cc @@ -14,6 +14,8 @@ #include "third_party/tonic/converter/dart_converter.h" #include "third_party/tonic/logging/dart_error.h" +#include "dart_timestamp_provider.h" + namespace { // Tracks whether Dart has been initialized and if it is safe to call Dart // APIs. @@ -88,7 +90,9 @@ void DartVMInitializer::Initialize(Dart_InitializeParams* params) { gDartInitialized = true; } + fml::TimePoint::SetClockSource(flutter::DartTimelineTicksSinceEpoch); fml::tracing::TraceSetTimelineEventHandler(LogDartTimelineEvent); + fml::tracing::TraceSetTimelineMicrosSource(Dart_TimelineGetMicros); tonic::SetUnhandledExceptionReporter(&ReportUnhandledException); }