diff options
author | Timothy Gu <timothygu99@gmail.com> | 2018-02-26 01:26:22 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2018-03-06 18:50:01 +0300 |
commit | 9256dbb6115021fb69d5ccc2af0f7e27b0601007 (patch) | |
tree | 9127d1ce78731e3ff76f21517fb5c2b0162b0636 /src | |
parent | ad721429c02a086373d841206927718048d6b521 (diff) |
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')
-rw-r--r-- | src/node_perf.cc | 35 | ||||
-rw-r--r-- | src/node_perf.h | 8 | ||||
-rw-r--r-- | src/node_perf_common.h | 6 |
3 files changed, 46 insertions, 3 deletions
diff --git a/src/node_perf.cc b/src/node_perf.cc index db4eff53534..bf9746e429d 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, diff --git a/src/node_perf.h b/src/node_perf.h index 82e3f46eb83..0db99acd03e 100644 --- a/src/node_perf.h +++ b/src/node_perf.h @@ -23,6 +23,10 @@ using v8::Local; using v8::Object; using v8::Value; +extern const uint64_t timeOrigin; + +double GetCurrentTimeInMicroseconds(); + static inline PerformanceMilestone ToPerformanceMilestoneEnum(const char* str) { #define V(name, label) \ if (strcmp(str, label) == 0) return NODE_PERFORMANCE_MILESTONE_##name; @@ -78,11 +82,11 @@ class PerformanceEntry { return ToPerformanceEntryTypeEnum(type().c_str()); } - double startTime() const { return startTime_ / 1e6; } + double startTime() const { return startTimeNano() / 1e6; } double duration() const { return durationNano() / 1e6; } - uint64_t startTimeNano() const { return startTime_; } + uint64_t startTimeNano() const { return startTime_ - timeOrigin; } uint64_t durationNano() const { return endTime_ - startTime_; } diff --git a/src/node_perf_common.h b/src/node_perf_common.h index 435a4cffe5a..7ff57359ba5 100644 --- a/src/node_perf_common.h +++ b/src/node_perf_common.h @@ -4,6 +4,7 @@ #include "node.h" #include "v8.h" +#include <algorithm> #include <map> #include <string> @@ -76,7 +77,10 @@ class performance_state { isolate, offsetof(performance_state_internal, observers), NODE_PERFORMANCE_ENTRY_TYPE_INVALID, - root) {} + root) { + for (size_t i = 0; i < milestones.Length(); i++) + milestones[i] = -1.; + } AliasedBuffer<uint8_t, v8::Uint8Array> root; AliasedBuffer<double, v8::Float64Array> milestones; |