[fuchsia] allow injecting the inspect node

This CL allows users to inject the inspect node used inside the
os_fuchsia.cc file instead of having it get created automatically.
This allows us to use inspect in other areas of the flutter/dart
runners. This code needs to be soft transitioned so there will be
follow up CLs after the runners are migrated.

TEST=This was tested against the current version of the runner that does
not use these new codepaths and against a version that does.

Change-Id: I2fff36223aa4021da9cd2051daf6312d2b115492
Bug: fxbug.dev/69558
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/190960
Commit-Queue: Chase Latta <chaselatta@google.com>
Reviewed-by: Zach Anderson <zra@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Chase Latta 2021-03-22 17:38:24 +00:00 committed by commit-bot@chromium.org
parent dd91769e90
commit dfb009d70a
4 changed files with 68 additions and 28 deletions

View file

@ -18,11 +18,20 @@ library_for_all_configs("libdart_platform") {
if (is_fuchsia) {
if (using_fuchsia_gn_sdk) {
extra_deps += [ "$fuchsia_sdk_root/pkg/sys_cpp" ]
extra_deps += [
"$fuchsia_sdk_root/pkg/sys_cpp",
"$fuchsia_sdk_root/pkg/sys_inspect_cpp",
]
} else if (using_fuchsia_sdk) {
extra_deps += [ "$fuchsia_sdk_root/pkg:sys_cpp" ]
extra_deps += [
"$fuchsia_sdk_root/pkg:sys_cpp",
"$fuchsia_sdk_root/pkg:sys_inspect_cpp",
]
} else {
extra_deps += [ "//sdk/lib/sys/cpp" ]
extra_deps += [
"//sdk/lib/sys/cpp",
"//sdk/lib/sys/inspect/cpp",
]
}
}
}

View file

@ -6,8 +6,10 @@
#if defined(HOST_OS_FUCHSIA)
#include <memory>
#include <utility>
#include "lib/sys/cpp/component_context.h"
#include "lib/sys/inspect/cpp/component.h"
#include "platform/utils.h"
#include "platform/utils_fuchsia.h"
@ -57,6 +59,23 @@ sys::ComponentContext* ComponentContext() {
return context.get();
}
std::unique_ptr<inspect::Node> vm_node;
void SetDartVmNode(std::unique_ptr<inspect::Node> node) {
vm_node = std::move(node);
}
std::unique_ptr<inspect::Node> TakeDartVmNode() {
// TODO(fxbug.dev/69558) Remove the creation of the node_ from this call
// after the runners have been migrated to injecting this object.
if (vm_node == nullptr) {
static std::unique_ptr<sys::ComponentInspector> component_inspector =
std::make_unique<sys::ComponentInspector>(dart::ComponentContext());
inspect::Node& root = component_inspector->inspector()->GetRoot();
vm_node = std::make_unique<inspect::Node>(root.CreateChild("vm"));
}
return std::move(vm_node);
}
} // namespace dart
#endif // defined(HOST_OS_FUCHSIA)

View file

@ -6,6 +6,7 @@
#define RUNTIME_PLATFORM_UTILS_FUCHSIA_H_
#include <endian.h>
#include <memory>
namespace sys {
@ -14,6 +15,13 @@ class ComponentContext;
} // namespace sys
namespace inspect {
// From Fuchsia SDK.
class Node;
} // namespace inspect
namespace dart {
inline uint16_t Utils::HostToBigEndian16(uint16_t value) {
@ -57,6 +65,18 @@ inline char* Utils::StrError(int err, char* buffer, size_t bufsize) {
// call sys::ComponentContext::Create().
sys::ComponentContext* ComponentContext();
// Sets the inspect node set to be used in the dart vm
//
// This method will take ownership of the node
void SetDartVmNode(std::unique_ptr<inspect::Node> node);
// Returns the inspect node set in SetDartVmNode().
//
// The caller should take ownership of the returned node because
// the value will be set to null after this call.
// This call may return null if no node is provided.
std::unique_ptr<inspect::Node> TakeDartVmNode();
} // namespace dart
#endif // RUNTIME_PLATFORM_UTILS_FUCHSIA_H_

View file

@ -80,23 +80,21 @@ enum class TZDataStatus {
// Under normal operation, all metric values below should be zero.
class InspectMetrics {
public:
// Does not take ownership of inspector.
explicit InspectMetrics(inspect::Inspector* inspector)
: inspector_(inspector),
root_(inspector_->GetRoot()),
metrics_(root_.CreateChild("os")),
dst_status_(metrics_.CreateInt("dst_status", kUninitialized)),
tz_data_status_(metrics_.CreateInt("tz_data_status", kUninitialized)),
// Takes ownership of the vm_node.
explicit InspectMetrics(std::unique_ptr<inspect::Node> vm_node)
: vm_node_(std::move(vm_node)),
dst_status_(vm_node_->CreateInt("dst_status", kUninitialized)),
tz_data_status_(vm_node_->CreateInt("tz_data_status", kUninitialized)),
tz_data_close_status_(
metrics_.CreateInt("tz_data_close_status", kUninitialized)),
vm_node_->CreateInt("tz_data_close_status", kUninitialized)),
get_profile_status_(
metrics_.CreateInt("get_profile_status", kUninitialized)),
vm_node_->CreateInt("get_profile_status", kUninitialized)),
profiles_timezone_content_status_(
metrics_.CreateInt("timezone_content_status", kOk)),
num_get_profile_calls_(metrics_.CreateInt("num_get_profile_calls", 0)),
num_on_change_calls_(metrics_.CreateInt("num_on_change_calls", 0)),
vm_node_->CreateInt("timezone_content_status", kOk)),
num_get_profile_calls_(vm_node_->CreateInt("num_get_profile_calls", 0)),
num_on_change_calls_(vm_node_->CreateInt("num_on_change_calls", 0)),
num_intl_provider_errors_(
metrics_.CreateInt("num_intl_provider_errors", 0)) {}
vm_node_->CreateInt("num_intl_provider_errors", 0)) {}
// Registers a single call to GetProfile callback.
void RegisterGetProfileCall() { num_get_profile_calls_.Add(1); }
@ -131,14 +129,8 @@ class InspectMetrics {
}
private:
// The inspector that all metrics are being reported into.
inspect::Inspector* inspector_;
// References inspector_ state.
inspect::Node& root_;
// The OS metrics node.
inspect::Node metrics_;
std::unique_ptr<inspect::Node> vm_node_;
// The status of the last GetTimeZoneOffset call.
inspect::IntProperty dst_status_;
@ -305,7 +297,6 @@ class TimezoneName final {
std::set<const std::string> timezone_names;
// Initialized on OS:Init(), deinitialized on OS::Cleanup.
std::unique_ptr<sys::ComponentInspector> component_inspector;
std::shared_ptr<InspectMetrics> metrics;
std::shared_ptr<TimezoneName> timezone_name;
async_loop_t* message_loop = nullptr;
@ -603,9 +594,11 @@ void OS::Init() {
async_loop_start_thread(message_loop, "Fuchsia async loop", nullptr);
}
sys::ComponentContext* context = dart::ComponentContext();
component_inspector = std::make_unique<sys::ComponentInspector>(context);
metrics = std::make_shared<InspectMetrics>(component_inspector->inspector());
auto vm_node = dart::TakeDartVmNode();
// TODO(fxbug.dev/69558) allow vm_node to be null and not crash
ASSERT(vm_node != nullptr);
metrics = std::make_shared<InspectMetrics>(std::move(vm_node));
InitializeTZData();
auto services = sys::ServiceDirectory::CreateFromNamespace();
@ -620,7 +613,6 @@ void OS::Cleanup() {
}
timezone_name.reset();
metrics.reset();
component_inspector.reset();
if (message_loop != nullptr) {
// Check message_loop is still the default dispatcher before clearing it.