From 9256dbb6115021fb69d5ccc2af0f7e27b0601007 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Sun, 25 Feb 2018 14:26:22 -0800 Subject: perf_hooks: fix timing Fixes: https://github.com/nodejs/node/issues/17892 Fixes: https://github.com/nodejs/node/issues/17893 Fixes: https://github.com/nodejs/node/issues/18992 PR-URL: https://github.com/nodejs/node/pull/18993 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Anatoli Papirovski --- src/node_perf.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/node_perf.cc') diff --git a/src/node_perf.cc b/src/node_perf.cc index db4eff5353..bf9746e429 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -3,6 +3,10 @@ #include +#ifdef __POSIX__ +#include // gettimeofday +#endif + namespace node { namespace performance { @@ -21,13 +25,38 @@ using v8::Object; using v8::String; using v8::Value; +// Microseconds in a second, as a float. +#define MICROS_PER_SEC 1e6 +// Microseconds in a millisecond, as a float. +#define MICROS_PER_MILLIS 1e3 + +// https://w3c.github.io/hr-time/#dfn-time-origin const uint64_t timeOrigin = PERFORMANCE_NOW(); +// https://w3c.github.io/hr-time/#dfn-time-origin-timestamp +const double timeOriginTimestamp = GetCurrentTimeInMicroseconds(); uint64_t performance_node_start; uint64_t performance_v8_start; uint64_t performance_last_gc_start_mark_ = 0; v8::GCType performance_last_gc_type_ = v8::GCType::kGCTypeAll; +double GetCurrentTimeInMicroseconds() { +#ifdef _WIN32 +// The difference between the Unix Epoch and the Windows Epoch in 100-ns ticks. +#define TICKS_TO_UNIX_EPOCH 116444736000000000LL + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + uint64_t filetime_int = static_cast(ft.dwHighDateTime) << 32 | + ft.dwLowDateTime; + // FILETIME is measured in terms of 100 ns. Convert that to 1 us (1000 ns). + return (filetime_int - TICKS_TO_UNIX_EPOCH) / 10.; +#else + struct timeval tp; + gettimeofday(&tp, nullptr); + return MICROS_PER_SEC * tp.tv_sec + tp.tv_usec; +#endif +} + // Initialize the performance entry object properties inline void InitObject(const PerformanceEntry& entry, Local obj) { Environment* env = entry.env(); @@ -384,6 +413,12 @@ void Init(Local target, v8::Number::New(isolate, timeOrigin / 1e6), attr).ToChecked(); + target->DefineOwnProperty( + context, + FIXED_ONE_BYTE_STRING(isolate, "timeOriginTimestamp"), + v8::Number::New(isolate, timeOriginTimestamp / MICROS_PER_MILLIS), + attr).ToChecked(); + target->DefineOwnProperty(context, env->constants_string(), constants, -- cgit v1.2.3