Do not depend on Dart in FML (flutter/engine#32846)

This commit is contained in:
Dan Field 2022-04-21 19:22:45 -07:00 committed by GitHub
parent 3ff6b2afa7
commit f51504259a
11 changed files with 102 additions and 63 deletions

View File

@ -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

View File

@ -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",
]

View File

@ -4,9 +4,10 @@
#include "flutter/fml/time/time_point.h"
#include <atomic>
#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/time/dart_timestamp_provider.h"
#if defined(OS_FUCHSIA)
#include <zircon/syscalls.h>
@ -27,8 +28,13 @@ TimePoint TimePoint::CurrentWallTime() {
return Now();
}
void TimePoint::SetClockSource(ClockSource source) {}
#else
namespace {
std::atomic<TimePoint::ClockSource> gSteadyClockSource;
}
template <typename Clock, typename Duration>
static int64_t NanosSinceEpoch(
std::chrono::time_point<Clock, Duration> time_point) {
@ -36,8 +42,16 @@ static int64_t NanosSinceEpoch(
return std::chrono::duration_cast<std::chrono::nanoseconds>(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() {

View File

@ -6,6 +6,7 @@
#define FLUTTER_FML_TIME_TIME_POINT_H_
#include <cstdint>
#include <functional>
#include <iosfwd>
#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();

View File

@ -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 <thread>
@ -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);

View File

@ -18,8 +18,14 @@ namespace tracing {
#if FLUTTER_TIMELINE_ENABLED
namespace {
int64_t DefaultMicrosSource() {
return -1;
}
AsciiTrie gAllowlist;
std::atomic<TimelineEventHandler> gTimelineEventHandler;
std::atomic<TimelineMicrosSource> 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<const char*>& c_names,
const std::vector<std::string>& 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<std::string>& allowlist) {}
void TraceSetTimelineEventHandler(TimelineEventHandler handler) {}
void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {}
size_t TraceNonce() {
return 0;
}

View File

@ -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,

View File

@ -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",

View File

@ -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

View File

@ -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_

View File

@ -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);
}