-
Notifications
You must be signed in to change notification settings - Fork 6k
[fuchsia] Use FFI to get System clockMonotonic #27353
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| part of zircon; | ||
|
|
||
| final _kLibZirconDartPath = '/pkg/lib/libzircon_ffi.so'; | ||
|
|
||
| class _Bindings { | ||
| static ZirconFFIBindings? _bindings; | ||
|
|
||
| @pragma('vm:entry-point') | ||
| static ZirconFFIBindings? get() { | ||
| // For soft-transition until libzircon_ffi.so rolls into GI. | ||
| if (!File(_kLibZirconDartPath).existsSync()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (_bindings == null) { | ||
| final _dylib = DynamicLibrary.open(_kLibZirconDartPath); | ||
| _bindings = ZirconFFIBindings(_dylib); | ||
| } | ||
| return _bindings; | ||
| } | ||
| } | ||
|
|
||
| final ZirconFFIBindings? zirconFFIBindings = _Bindings.get(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,7 +236,15 @@ class System extends NativeFieldWrapperClass1 { | |
| static MapResult vmoMap(Handle vmo) native 'System_VmoMap'; | ||
|
|
||
| // Time operations. | ||
| static int clockGetMonotonic() native 'System_ClockGetMonotonic'; | ||
| static int clockGetMonotonic() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw... I don't think we currently have dart unittest/integration test coverage for this method: https://cs.opensource.google/search?q=%22clockGetMonotonic%22&sq=&ss=fuchsia%2Ffuchsia:sdk%2Fdart%2Fzircon%2Ftest%2F Just wanted to make sure if you've manually checked if this worked since our existing tests don't cover this. 🙂
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed it, I was testing using https://fuchsia-review.googlesource.com/c/fuchsia/+/554605 |
||
| if (zirconFFIBindings == null) { | ||
| return _nativeClockGetMonotonic(); | ||
| } else { | ||
| return zirconFFIBindings!.zircon_dart_clock_get_monotonic(); | ||
| } | ||
| } | ||
|
|
||
| static int _nativeClockGetMonotonic() native 'System_ClockGetMonotonic'; | ||
|
|
||
| // TODO(edcoyne): Remove this, it is required to safely do an API transition across repos. | ||
| static int reboot() { return -2; /*ZX_ERR_NOT_SUPPORTED*/ } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright 2013 The Flutter Authors. All rights reserved. | ||
| # Use of this source code is governed by a BSD-style license that can be | ||
| # found in the LICENSE file. | ||
|
|
||
| import("//build/fuchsia/sdk.gni") | ||
|
|
||
| config("zircon_ffi_config") { | ||
| include_dirs = [ "." ] | ||
| } | ||
|
|
||
| shared_library("zircon_ffi") { | ||
| public_configs = [ ":zircon_ffi_config" ] | ||
|
|
||
| sources = [ | ||
| "clock.cc", | ||
| "clock.h", | ||
| ] | ||
|
|
||
| deps = [ | ||
| "$fuchsia_sdk_root/pkg:zx", | ||
| "//third_party/dart/runtime:dart_api", | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "clock.h" | ||
|
|
||
| #include <zircon/syscalls.h> | ||
|
|
||
| uint64_t zircon_dart_clock_get_monotonic() { | ||
| return zx_clock_get_monotonic(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_FFI_CLOCK_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_FFI_CLOCK_H_ | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #define ZIRCON_FFI_EXPORT __attribute__((visibility("default"))) | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| ZIRCON_FFI_EXPORT uint64_t zircon_dart_clock_get_monotonic(); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_SDK_FFI_CLOCK_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // AUTO GENERATED FILE, DO NOT EDIT. | ||
| // | ||
| // Generated by `package:ffigen`. | ||
| import 'dart:ffi' as ffi; | ||
|
|
||
| /// Bindings for `dart:zircon_ffi`. | ||
| class ZirconFFIBindings { | ||
| /// Holds the symbol lookup function. | ||
| final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName) | ||
| _lookup; | ||
|
|
||
| /// The symbols are looked up in [dynamicLibrary]. | ||
| ZirconFFIBindings(ffi.DynamicLibrary dynamicLibrary) | ||
| : _lookup = dynamicLibrary.lookup; | ||
|
|
||
| /// The symbols are looked up with [lookup]. | ||
| ZirconFFIBindings.fromLookup( | ||
| ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName) | ||
| lookup) | ||
| : _lookup = lookup; | ||
|
|
||
| int zircon_dart_clock_get_monotonic() { | ||
| return _zircon_dart_clock_get_monotonic(); | ||
| } | ||
|
|
||
| late final _zircon_dart_clock_get_monotonic_ptr = | ||
| _lookup<ffi.NativeFunction<_c_zircon_dart_clock_get_monotonic>>( | ||
| 'zircon_dart_clock_get_monotonic'); | ||
| late final _dart_zircon_dart_clock_get_monotonic | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chinmaygarde, yup, I'm planning on building on top of this. Choosing a simple method like clock monotonic was the easiest way for me to test the combination of:
After this change, I wish to move
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like chinmay's question still has merit here. We will need a more complex trampoline as time goes on for sure, but why put the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You run into symbol visibility issues and the linker or LTO thinking it can get rid of symbols with default visibility because they are not used. Kaushik and I were experimenting with it this morning and having a tough time making resolving symbols that way.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is already merged but I have experimented with this before and didn't need to do any extra trampolining by adding this method to the |
||
| _zircon_dart_clock_get_monotonic = _zircon_dart_clock_get_monotonic_ptr | ||
| .asFunction<_dart_zircon_dart_clock_get_monotonic>(); | ||
| } | ||
|
|
||
| typedef _c_zircon_dart_clock_get_monotonic = ffi.Uint64 Function(); | ||
|
|
||
| typedef _dart_zircon_dart_clock_get_monotonic = int Function(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| name: zircon_ffi | ||
|
|
||
| environment: | ||
| sdk: '>=2.12.0 <3.0.0' | ||
|
|
||
| dependencies: | ||
| ffi: ^1.0.0 | ||
|
|
||
| dev_dependencies: | ||
| ffigen: ^3.0.0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! I didn't know this existed. 😀 |
||
| lints: ^1.0.1 | ||
|
|
||
| ffigen: | ||
| name: ZirconFFIBindings | ||
| description: Bindings for `dart:zircon_ffi`. | ||
| output: 'lib/zircon_ffi.dart' | ||
| headers: | ||
| entry-points: | ||
| - 'clock.h' | ||
| functions: | ||
| include: | ||
| - 'zircon_dart_.*' | ||
| macros: | ||
| include: | ||
| - nothing | ||
| enums: | ||
| include: | ||
| - nothing | ||
| unnamed-enums: | ||
| include: | ||
| - nothing | ||
| globals: | ||
| include: | ||
| - nothing | ||
| structs: | ||
| include: | ||
| - nothing | ||
| dependency-only: opaque | ||
| compiler-opts: | ||
| - '-I../../../../../../../third_party/dart/runtime' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put this logic directly into ZirconFFIBindings and expose an instance getter, then have
systen.dartcall ZirconFFIBindings.instance? This is how the framework does things and its niceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ZirconFFIBindingsis autogenerated usingffigenand I wanted to keep it that way.