perf_hooks: introduce createHistogram

Adds a new `perf_hooks.createHistogram()` API for creating histogram
instances that allow user recording.

Makes Histogram instances cloneable via MessagePort. This allows, for
instance, an event loop delay monitor to be running on the main thread
while the histogram data can be monitored actively from a worker thread.

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/37155
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
James M Snell 2021-01-30 09:26:15 -08:00
parent ce4798b555
commit 4a19cc8947
No known key found for this signature in database
GPG key ID: 7341B15C070877AC
15 changed files with 781 additions and 320 deletions

View file

@ -16,6 +16,7 @@ const {
ObjectKeys,
SafeSet,
Symbol,
TypeError,
} = primordials;
const {
@ -65,6 +66,7 @@ const {
const {
Histogram,
createHistogram,
kHandle,
} = require('internal/histogram');
@ -86,6 +88,7 @@ const kInsertEntry = Symbol('insert-entry');
const kGetEntries = Symbol('get-entries');
const kIndex = Symbol('index');
const kMarks = Symbol('marks');
const kEnabled = Symbol('kEnabled');
const observers = {};
const observerableTypes = [
@ -630,9 +633,26 @@ function sortedInsert(list, entry) {
}
class ELDHistogram extends Histogram {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
enable() { return this[kHandle].enable(); }
disable() { return this[kHandle].disable(); }
constructor(i) {
if (!(i instanceof _ELDHistogram)) {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('illegal constructor');
}
super(i);
this[kEnabled] = false;
}
enable() {
if (this[kEnabled]) return false;
this[kEnabled] = true;
this[kHandle].start();
return true;
}
disable() {
if (!this[kEnabled]) return false;
this[kEnabled] = false;
this[kHandle].stop();
return true;
}
}
function monitorEventLoopDelay(options = {}) {
@ -651,7 +671,8 @@ function monitorEventLoopDelay(options = {}) {
module.exports = {
performance,
PerformanceObserver,
monitorEventLoopDelay
monitorEventLoopDelay,
createHistogram,
};
ObjectDefineProperty(module.exports, 'constants', {