Fabric: Introducing `TelemetryController` as part of `MountingManager`

Summary:
This diff implements TelemetryController, a small tool that can be used to instrument mounting transactions. It abstracts the logic of merging telemetry data of multiple transactions in a thread-safe manner.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D22490580

fbshipit-source-id: 3f3425b88d38fddb555c1390fd8f1ff3ef1c475a
This commit is contained in:
Valentin Shergin 2020-07-15 23:28:27 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 242c3c8343
Коммит 35f2cd26ae
4 изменённых файлов: 127 добавлений и 1 удалений

Просмотреть файл

@ -24,7 +24,8 @@ MountingCoordinator::MountingCoordinator(
std::weak_ptr<MountingOverrideDelegate const> delegate)
: surfaceId_(baseRevision.getRootShadowNode().getSurfaceId()),
baseRevision_(baseRevision),
mountingOverrideDelegate_(delegate) {
mountingOverrideDelegate_(delegate),
telemetryController_(*this) {
#ifdef RN_SHADOW_TREE_INTROSPECTION
stubViewTree_ = stubViewTreeFromShadowNode(baseRevision_.getRootShadowNode());
#endif
@ -172,5 +173,9 @@ better::optional<MountingTransaction> MountingCoordinator::pullTransaction()
return transaction;
}
TelemetryController const &MountingCoordinator::getTelemetryController() const {
return telemetryController_;
}
} // namespace react
} // namespace facebook

Просмотреть файл

@ -14,6 +14,7 @@
#include <react/mounting/MountingOverrideDelegate.h>
#include <react/mounting/MountingTransaction.h>
#include <react/mounting/ShadowTreeRevision.h>
#include <react/mounting/TelemetryController.h>
#include "ShadowTreeRevision.h"
#ifdef RN_SHADOW_TREE_INTROSPECTION
@ -69,6 +70,8 @@ class MountingCoordinator final {
*/
bool waitForTransaction(std::chrono::duration<double> timeout) const;
TelemetryController const &getTelemetryController() const;
/*
* Methods from this section are meant to be used by
* `MountingOverrideDelegate` only.
@ -104,6 +107,8 @@ class MountingCoordinator final {
mutable std::condition_variable signal_;
std::weak_ptr<MountingOverrideDelegate const> mountingOverrideDelegate_;
TelemetryController telemetryController_;
#ifdef RN_SHADOW_TREE_INTROSPECTION
void validateTransactionAgainstStubViewTree(
ShadowViewMutationList const &mutations,

Просмотреть файл

@ -0,0 +1,58 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TelemetryController.h"
#include <react/mounting/MountingCoordinator.h>
namespace facebook {
namespace react {
TelemetryController::TelemetryController(
MountingCoordinator const &mountingCoordinator) noexcept
: mountingCoordinator_(mountingCoordinator) {}
bool TelemetryController::pullTransaction(
std::function<void(MountingTransactionMetadata metadata)> willMount,
std::function<void(ShadowViewMutationList const &mutations)> doMount,
std::function<void(MountingTransactionMetadata metadata)> didMount) const
noexcept {
auto optional = mountingCoordinator_.pullTransaction();
if (!optional.has_value()) {
return false;
}
auto transaction = std::move(*optional);
auto surfaceId = transaction.getSurfaceId();
auto number = transaction.getNumber();
auto telemetry = transaction.getTelemetry();
auto numberOfMutations = transaction.getMutations().size();
mutex_.lock();
auto compoundTelemetry = compoundTelemetry_;
mutex_.unlock();
willMount({surfaceId, number, telemetry, compoundTelemetry});
telemetry.willMount();
doMount(std::move(transaction.getMutations()));
telemetry.didMount();
compoundTelemetry.incorporate(telemetry, numberOfMutations);
didMount({surfaceId, number, telemetry, compoundTelemetry});
mutex_.lock();
compoundTelemetry_ = compoundTelemetry;
mutex_.unlock();
return true;
}
} // namespace react
} // namespace facebook

Просмотреть файл

@ -0,0 +1,58 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <mutex>
#include <react/mounting/MountingTelemetry.h>
#include <react/mounting/MountingTransaction.h>
#include <react/mounting/MountingTransactionMetadata.h>
namespace facebook {
namespace react {
class MountingCoordinator;
/*
* Provides convenient tools for aggregating and accessing telemetry data
* associated with running Surface.
*/
class TelemetryController final {
friend class MountingCoordinator;
/*
* To be used by `MountingCoordinator`.
*/
TelemetryController(MountingCoordinator const &mountingCoordinator) noexcept;
/*
* Not copyable.
*/
TelemetryController(TelemetryController const &other) noexcept = delete;
TelemetryController &operator=(TelemetryController const &other) noexcept =
delete;
public:
/*
* Calls `MountingCoordinator::pullTransaction()` and aggregates telemetry.
*/
bool pullTransaction(
std::function<void(MountingTransactionMetadata metadata)> willMount,
std::function<void(ShadowViewMutationList const &mutations)> doMount,
std::function<void(MountingTransactionMetadata metadata)> didMount) const
noexcept;
private:
MountingCoordinator const &mountingCoordinator_;
mutable SurfaceTelemetry compoundTelemetry_{};
mutable std::mutex mutex_;
};
} // namespace react
} // namespace facebook