Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
process: implement process.hrtime.bigint()
  • Loading branch information
joyeecheung committed Jun 16, 2018
commit 30533c7f3c4a06c842fb3dcfb8d1de61bf280280
28 changes: 28 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,33 @@ setTimeout(() => {
}, 1000);
```

## process.hrtime.bigint()
<!-- YAML
added: REPLACEME
-->

* Returns: {bigint}

The `bigint` version of the [`process.hrtime()`][] method returning the
current high-resolution real time in a `bigint`.

Unlike [`process.hrtime()`][], it does not support an additional `time`
argument since the difference can just be computed directly
by substraction of the two `bigint`s.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subtraction


```js
const start = process.hrtime.bigint();
// 191051479007711n

setTimeout(() => {
const end = process.hrtime.bigint();
// 191052633396993n

console.log(`Benchmark took ${end - start} nanoseconds`);
// benchmark took 1154389282 nanoseconds
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benchmark -> Benchmark?

}, 1000);
```

## process.initgroups(user, extraGroup)
<!-- YAML
added: v0.9.4
Expand Down Expand Up @@ -2030,6 +2057,7 @@ cases:
[`process.execPath`]: #process_process_execpath
[`process.exit()`]: #process_process_exit_code
[`process.exitCode`]: #process_process_exitcode
[`process.hrtime()`]: #process_process_hrtime_time
[`process.kill()`]: #process_process_kill_pid_signal
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// object.
{ _setupProcessObject, _setupNextTick,
_setupPromises, _chdir, _cpuUsage,
_hrtime, _memoryUsage, _rawDebug,
_hrtime, _hrtimeBigInt,
_memoryUsage, _rawDebug,
_umask, _initgroups, _setegid, _seteuid,
_setgid, _setuid, _setgroups,
_shouldAbortOnUncaughtToggle },
Expand Down Expand Up @@ -70,7 +71,7 @@
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
} = perf.constants;

_process.setup_hrtime(_hrtime);
_process.setup_hrtime(_hrtime, _hrtimeBigInt);
_process.setup_cpuUsage(_cpuUsage);
_process.setupMemoryUsage(_memoryUsage);
_process.setupKillAndExit();
Expand Down
10 changes: 9 additions & 1 deletion lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function setup_cpuUsage(_cpuUsage) {
// The 3 entries filled in by the original process.hrtime contains
// the upper/lower 32 bits of the second part of the value,
// and the remaining nanoseconds of the value.
function setup_hrtime(_hrtime) {
function setup_hrtime(_hrtime, _hrtimeBigInt) {
const hrValues = new Uint32Array(3);

process.hrtime = function hrtime(time) {
Expand All @@ -115,6 +115,14 @@ function setup_hrtime(_hrtime) {
hrValues[2]
];
};

// Use a BigUint64Array in the closure because V8 does not have an API for
// creating a BigInt out of a uint64_t yet.
const hrBigintValues = new BigUint64Array(1);
process.hrtime.bigint = function() {
_hrtimeBigInt(hrBigintValues);
return hrBigintValues[0];
};
}

function setupMemoryUsage(_memoryUsage) {
Expand Down
1 change: 1 addition & 0 deletions src/bootstrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void SetupBootstrapObject(Environment* env,
BOOTSTRAP_METHOD(_chdir, Chdir);
BOOTSTRAP_METHOD(_cpuUsage, CPUUsage);
BOOTSTRAP_METHOD(_hrtime, Hrtime);
BOOTSTRAP_METHOD(_hrtimeBigInt, HrtimeBigInt);
BOOTSTRAP_METHOD(_memoryUsage, MemoryUsage);
BOOTSTRAP_METHOD(_rawDebug, RawDebug);
BOOTSTRAP_METHOD(_umask, Umask);
Expand Down
1 change: 1 addition & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ void Chdir(const v8::FunctionCallbackInfo<v8::Value>& args);
void CPUUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
void Cwd(const v8::FunctionCallbackInfo<v8::Value>& args);
void Hrtime(const v8::FunctionCallbackInfo<v8::Value>& args);
void HrtimeBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
void Kill(const v8::FunctionCallbackInfo<v8::Value>& args);
void MemoryUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
11 changes: 11 additions & 0 deletions src/node_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::BigUint64Array;
using v8::Float64Array;
using v8::FunctionCallbackInfo;
using v8::HeapStatistics;
Expand Down Expand Up @@ -113,7 +114,11 @@ void Cwd(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(cwd);
}


// Hrtime exposes libuv's uv_hrtime() high-resolution timer.

// This is the legacy version of hrtime before BigInt was introduced in
// JavaScript.
// The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
// so this function instead fills in an Uint32Array with 3 entries,
// to avoid any integer overflow possibility.
Expand All @@ -132,6 +137,12 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) {
fields[2] = t % NANOS_PER_SEC;
}

void HrtimeBigInt(const FunctionCallbackInfo<Value>& args) {
Local<ArrayBuffer> ab = args[0].As<BigUint64Array>()->Buffer();
uint64_t* fields = static_cast<uint64_t*>(ab->GetContents().Data());
fields[0] = uv_hrtime();
}

void Kill(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-process-hrtime-bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

// Tests that process.hrtime.bigint() works.

require('../common');
const assert = require('assert');

const start = process.hrtime.bigint();
assert.strictEqual(typeof start, 'bigint');

const end = process.hrtime.bigint();
assert.strictEqual(typeof end, 'bigint');

assert(end - start >= 0n);