From cc78aee56aac3b1cb91ecdefb3bcc9e96caaeef2 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sat, 1 Aug 2020 21:29:29 -0700 Subject: [PATCH] Fabric: Telemetry time point serialization functions Summary: Fabric's Telemetry uses `std::chrono::steady_clock::time_point`s to represent the exact moments in time when some events happen. This is the same clock that pretty much any performance tracker use (and should use), QPL included. However, different trackers use different serializable representations of that timepoints. This diff adds functions that convert the timepoint value to UNIX Timestamp and to a number of seconds from the `steady_clock` epoch start. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D22887632 fbshipit-source-id: 2b51acddccee9af8071a34797b5015d6fd008394 --- ReactCommon/utils/Telemetry.h | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/ReactCommon/utils/Telemetry.h b/ReactCommon/utils/Telemetry.h index 4fbe7f912ca..10a82e07f04 100644 --- a/ReactCommon/utils/Telemetry.h +++ b/ReactCommon/utils/Telemetry.h @@ -8,6 +8,7 @@ #pragma once #include +#include namespace facebook { namespace react { @@ -52,6 +53,55 @@ static inline int64_t telemetryTimePointToMilliseconds( .count(); } +/* + * Returns a number of seconds that passed from "Steady Clock" epoch starting + * time point to a given time point. + */ +static inline double telemetryTimePointToSteadyClockSeconds( + TelemetryTimePoint timePoint) { + static_assert( + std::is_same:: + value, + "`TelemetryClock` must be `std::chrono::steady_clock` to make the " + "following implementation work correctly."); + + auto nanoseconds = std::chrono::duration_cast( + timePoint.time_since_epoch()) + .count(); + return nanoseconds / 1.0e9; +} + +/* + * Converts a time point on one clock to a time point on a different clock. + */ +template < + typename DestinationTimePointT, + typename SourceTimePointT, + typename DestnationClockT = typename DestinationTimePointT::clock, + typename SourceClockT = typename SourceTimePointT::clock> +DestinationTimePointT clockCast(SourceTimePointT timePoint) { + auto sourseClockNow = SourceClockT::now(); + auto destinationClockNow = DestnationClockT::now(); + return std::chrono::time_point_cast( + timePoint - sourseClockNow + destinationClockNow); +} + +/* + * Returns a number of seconds that passed from the UNIX Epoch starting time + * point to a given time point. + * Also known as POSIX time or UNIX Timestamp. + */ +static inline double telemetryTimePointToSecondsSinceEpoch( + TelemetryTimePoint timePoint) { + auto systemClockTimePoint = + clockCast( + timePoint); + return std::chrono::duration_cast( + systemClockTimePoint.time_since_epoch()) + .count() / + 1000000.0; +} + /* * Returns a number of milliseconds that represents the given duration object. */