Skip to content

Commit 7ac16b7

Browse files
committed
src: per-environment time origin value
According to https://html.spec.whatwg.org/#environment-settings-object, the timeOrigin is a per-environment value. Worker's timeOrigin is the time when the worker is created (https://html.spec.whatwg.org/multipage/workers.html#set-up-a-worker-environment-settings-object).
1 parent 02eb10b commit 7ac16b7

7 files changed

+41
-16
lines changed

src/env.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ Environment::Environment(IsolateData* isolate_data,
352352
stream_base_state_(isolate_,
353353
StreamBase::kNumStreamBaseStateFields,
354354
MAYBE_FIELD_PTR(env_info, stream_base_state)),
355-
environment_start_time_(PERFORMANCE_NOW()),
355+
time_origin_(PERFORMANCE_NOW()),
356+
time_origin_timestamp_(GetCurrentTimeInMicroseconds()),
356357
flags_(flags),
357358
thread_id_(thread_id.id == static_cast<uint64_t>(-1)
358359
? AllocateEnvironmentThreadId().id
@@ -455,7 +456,7 @@ void Environment::InitializeMainContext(Local<Context> context,
455456
should_abort_on_uncaught_toggle_[0] = 1;
456457

457458
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT,
458-
environment_start_time_);
459+
time_origin_);
459460
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
460461
per_process::node_start_time);
461462

src/env.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,9 @@ class Environment : public MemoryRetainer {
13801380
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
13811381
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
13821382

1383+
inline uint64_t time_origin() { return time_origin_; }
1384+
inline double time_origin_timestamp() { return time_origin_timestamp_; }
1385+
13831386
inline bool EmitProcessEnvWarning() {
13841387
bool current_value = emit_env_nonstring_warning_;
13851388
emit_env_nonstring_warning_ = false;
@@ -1568,7 +1571,10 @@ class Environment : public MemoryRetainer {
15681571

15691572
AliasedInt32Array stream_base_state_;
15701573

1571-
uint64_t environment_start_time_;
1574+
// https://w3c.github.io/hr-time/#dfn-time-origin
1575+
uint64_t time_origin_;
1576+
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
1577+
double time_origin_timestamp_;
15721578
std::unique_ptr<performance::PerformanceState> performance_state_;
15731579

15741580
bool has_run_bootstrapping_code_ = false;

src/node_http2.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ void Http2Stream::EmitStatistics() {
640640
std::unique_ptr<Http2StreamPerformanceEntry> entry =
641641
std::make_unique<Http2StreamPerformanceEntry>(
642642
"Http2Stream",
643-
start - (node::performance::timeOrigin / 1e6),
643+
start - (env()->time_origin() / 1e6),
644644
duration,
645645
statistics_);
646646

@@ -660,7 +660,7 @@ void Http2Session::EmitStatistics() {
660660
std::unique_ptr<Http2SessionPerformanceEntry> entry =
661661
std::make_unique<Http2SessionPerformanceEntry>(
662662
"Http2Session",
663-
start - (node::performance::timeOrigin / 1e6),
663+
start - (env()->time_origin() / 1e6),
664664
duration,
665665
statistics_);
666666

src/node_perf.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ using v8::Value;
3535

3636
// Microseconds in a millisecond, as a float.
3737
#define MICROS_PER_MILLIS 1e3
38+
// Nanoseconds in a millisecond, as a float.
39+
#define NANOS_PER_MILLIS 1e6
3840

39-
// https://w3c.github.io/hr-time/#dfn-time-origin
40-
const uint64_t timeOrigin = PERFORMANCE_NOW();
41-
// https://w3c.github.io/hr-time/#dfn-time-origin-timestamp
42-
const double timeOriginTimestamp = GetCurrentTimeInMicroseconds();
4341
uint64_t performance_v8_start;
4442

4543
PerformanceState::PerformanceState(Isolate* isolate,
@@ -160,9 +158,9 @@ void MarkGarbageCollectionEnd(
160158
return;
161159

162160
double start_time =
163-
(state->performance_last_gc_start_mark - timeOrigin) / 1e6;
161+
(state->performance_last_gc_start_mark - env->time_origin()) / NANOS_PER_MILLIS;
164162
double duration =
165-
(PERFORMANCE_NOW() / 1e6) - (state->performance_last_gc_start_mark / 1e6);
163+
(PERFORMANCE_NOW() / NANOS_PER_MILLIS) - (state->performance_last_gc_start_mark / NANOS_PER_MILLIS);
166164

167165
std::unique_ptr<GCPerformanceEntry> entry =
168166
std::make_unique<GCPerformanceEntry>(
@@ -257,12 +255,14 @@ void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
257255
}
258256

259257
void GetTimeOrigin(const FunctionCallbackInfo<Value>& args) {
260-
args.GetReturnValue().Set(Number::New(args.GetIsolate(), timeOrigin / 1e6));
258+
Environment* env = Environment::GetCurrent(args);
259+
args.GetReturnValue().Set(Number::New(args.GetIsolate(), env->time_origin() / 1e6));
261260
}
262261

263262
void GetTimeOriginTimeStamp(const FunctionCallbackInfo<Value>& args) {
263+
Environment* env = Environment::GetCurrent(args);
264264
args.GetReturnValue().Set(
265-
Number::New(args.GetIsolate(), timeOriginTimestamp / MICROS_PER_MILLIS));
265+
Number::New(args.GetIsolate(), env->time_origin_timestamp() / MICROS_PER_MILLIS));
266266
}
267267

268268
void Initialize(Local<Object> target,

src/node_perf.h

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class ExternalReferenceRegistry;
2121

2222
namespace performance {
2323

24-
extern const uint64_t timeOrigin;
25-
2624
inline const char* GetPerformanceMilestoneName(
2725
PerformanceMilestone milestone) {
2826
switch (milestone) {

src/node_worker.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ void Worker::LoopStartTime(const FunctionCallbackInfo<Value>& args) {
817817
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START];
818818
CHECK_GE(loop_start_time, 0);
819819
args.GetReturnValue().Set(
820-
(loop_start_time - node::performance::timeOrigin) / 1e6);
820+
(loop_start_time - w->env_->time_origin()) / 1e6);
821821
}
822822

823823
namespace {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { Worker } = require('worker_threads');
6+
7+
const w = new Worker(`
8+
require('worker_threads').parentPort.postMessage(performance.timeOrigin);
9+
`, { eval: true })
10+
11+
w.on('message', common.mustCall(timeOrigin => {
12+
// Worker is created after this main context, it's
13+
// `performance.timeOrigin` must be greater than this
14+
// main context's.
15+
assert.ok(timeOrigin > performance.timeOrigin);
16+
}));
17+
18+
w.on('exit', common.mustCall(code => {
19+
assert.strictEqual(code, 0);
20+
}));

0 commit comments

Comments
 (0)