diff options
author | Timothy Gu <timothygu99@gmail.com> | 2018-02-25 14:26:22 -0800 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2018-03-06 07:50:01 -0800 |
commit | 9256dbb6115021fb69d5ccc2af0f7e27b0601007 (patch) | |
tree | 9127d1ce78731e3ff76f21517fb5c2b0162b0636 /src/node_perf.cc | |
parent | ad721429c02a086373d841206927718048d6b521 (diff) | |
download | android-node-v8-9256dbb6115021fb69d5ccc2af0f7e27b0601007.tar.gz android-node-v8-9256dbb6115021fb69d5ccc2af0f7e27b0601007.tar.bz2 android-node-v8-9256dbb6115021fb69d5ccc2af0f7e27b0601007.zip |
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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Diffstat (limited to 'src/node_perf.cc')
-rw-r--r-- | src/node_perf.cc | 35 |
1 files changed, 35 insertions, 0 deletions
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 <vector> +#ifdef __POSIX__ +#include <sys/time.h> // 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<uint64_t>(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<Object> obj) { Environment* env = entry.env(); @@ -384,6 +413,12 @@ void Init(Local<Object> 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, |