Skip to content

Commit bad53ff

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.
1 parent 02eb10b commit bad53ff

7 files changed

+49
-19
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

+11-1
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,13 @@ 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() {
1384+
return time_origin_;
1385+
}
1386+
inline double time_origin_timestamp() {
1387+
return time_origin_timestamp_;
1388+
}
1389+
13831390
inline bool EmitProcessEnvWarning() {
13841391
bool current_value = emit_env_nonstring_warning_;
13851392
emit_env_nonstring_warning_ = false;
@@ -1568,7 +1575,10 @@ class Environment : public MemoryRetainer {
15681575

15691576
AliasedInt32Array stream_base_state_;
15701577

1571-
uint64_t environment_start_time_;
1578+
// https://w3c.github.io/hr-time/#dfn-time-origin
1579+
uint64_t time_origin_;
1580+
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
1581+
double time_origin_timestamp_;
15721582
std::unique_ptr<performance::PerformanceState> performance_state_;
15731583

15741584
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

+12-10
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,10 @@ void MarkGarbageCollectionEnd(
160158
return;
161159

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

167166
std::unique_ptr<GCPerformanceEntry> entry =
168167
std::make_unique<GCPerformanceEntry>(
@@ -257,12 +256,15 @@ void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
257256
}
258257

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

263264
void GetTimeOriginTimeStamp(const FunctionCallbackInfo<Value>& args) {
264-
args.GetReturnValue().Set(
265-
Number::New(args.GetIsolate(), timeOriginTimestamp / MICROS_PER_MILLIS));
265+
Environment* env = Environment::GetCurrent(args);
266+
args.GetReturnValue().Set(Number::New(
267+
args.GetIsolate(), env->time_origin_timestamp() / MICROS_PER_MILLIS));
266268
}
267269

268270
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-2
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,7 @@ void Worker::LoopStartTime(const FunctionCallbackInfo<Value>& args) {
816816
double loop_start_time = w->env_->performance_state()->milestones[
817817
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START];
818818
CHECK_GE(loop_start_time, 0);
819-
args.GetReturnValue().Set(
820-
(loop_start_time - node::performance::timeOrigin) / 1e6);
819+
args.GetReturnValue().Set((loop_start_time - w->env_->time_origin()) / 1e6);
821820
}
822821

823822
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)