Skip to content

Commit

Permalink
Do not depend on Dart in FML (flutter#32846)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield authored Apr 22, 2022
1 parent c6ea046 commit d415876
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 63 deletions.
4 changes: 2 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -291,7 +289,6 @@ if (enable_unittests) {
deps = [
"//flutter/benchmarking",
"//flutter/fml",
"//flutter/runtime:libdart",
]
}

Expand Down Expand Up @@ -349,6 +346,7 @@ if (enable_unittests) {
":fml_fixtures",
"//flutter/fml",
"//flutter/fml/dart",
"//flutter/runtime",
"//flutter/runtime:libdart",
"//flutter/testing",
]
Expand Down
18 changes: 16 additions & 2 deletions fml/time/time_point.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -27,17 +28,30 @@ 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) {
const auto elapsed = time_point.time_since_epoch();
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() {
Expand Down
5 changes: 5 additions & 0 deletions fml/time/time_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FLUTTER_FML_TIME_TIME_POINT_H_

#include <cstdint>
#include <functional>
#include <iosfwd>

#include "flutter/fml/time/time_delta.h"
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions fml/time/time_point_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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);
Expand Down
98 changes: 55 additions & 43 deletions fml/trace_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions fml/trace_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,4 +35,4 @@ fml::TimePoint DartTimelineTicksSinceEpoch() {
return DartTimestampProvider::Instance().Now();
}

} // namespace fml
} // namespace flutter
Loading

0 comments on commit d415876

Please sign in to comment.