2018-04-04 01:05:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-23 09:09:05 +00:00
|
|
|
const {
|
2020-11-17 23:31:30 +00:00
|
|
|
ArrayPrototypeJoin,
|
|
|
|
SafeSet,
|
2019-11-23 09:09:05 +00:00
|
|
|
} = primordials;
|
|
|
|
|
2018-10-08 23:10:07 +00:00
|
|
|
const { hasTracing } = internalBinding('config');
|
2018-04-04 01:05:33 +00:00
|
|
|
|
|
|
|
const kMaxTracingCount = 10;
|
|
|
|
|
|
|
|
const {
|
|
|
|
ERR_TRACE_EVENTS_CATEGORY_REQUIRED,
|
|
|
|
ERR_TRACE_EVENTS_UNAVAILABLE,
|
|
|
|
} = require('internal/errors').codes;
|
|
|
|
|
2019-02-01 22:47:38 +00:00
|
|
|
const { ownsProcessState } = require('internal/worker');
|
|
|
|
if (!hasTracing || !ownsProcessState)
|
2018-04-04 01:05:33 +00:00
|
|
|
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
|
|
|
|
|
2018-08-06 19:28:33 +00:00
|
|
|
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
|
2018-04-04 01:05:33 +00:00
|
|
|
const { customInspectSymbol } = require('internal/util');
|
2019-03-20 15:06:14 +00:00
|
|
|
const { format } = require('internal/util/inspect');
|
2021-01-21 11:03:00 +00:00
|
|
|
const {
|
|
|
|
validateObject,
|
2023-01-19 09:08:38 +00:00
|
|
|
validateStringArray,
|
2021-01-21 11:03:00 +00:00
|
|
|
} = require('internal/validators');
|
2018-04-04 01:05:33 +00:00
|
|
|
|
2020-11-17 23:31:30 +00:00
|
|
|
const enabledTracingObjects = new SafeSet();
|
2018-04-04 01:05:33 +00:00
|
|
|
|
|
|
|
class Tracing {
|
2023-12-28 23:20:22 +00:00
|
|
|
#handle;
|
|
|
|
#categories;
|
|
|
|
#enabled = false;
|
|
|
|
|
2018-04-04 01:05:33 +00:00
|
|
|
constructor(categories) {
|
2023-12-28 23:20:22 +00:00
|
|
|
this.#handle = new CategorySet(categories);
|
|
|
|
this.#categories = categories;
|
2018-04-04 01:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
2023-12-28 23:20:22 +00:00
|
|
|
if (!this.#enabled) {
|
|
|
|
this.#enabled = true;
|
|
|
|
this.#handle.enable();
|
2018-04-04 01:05:33 +00:00
|
|
|
enabledTracingObjects.add(this);
|
|
|
|
if (enabledTracingObjects.size > kMaxTracingCount) {
|
|
|
|
process.emitWarning(
|
|
|
|
'Possible trace_events memory leak detected. There are more than ' +
|
2023-02-14 17:45:16 +00:00
|
|
|
`${kMaxTracingCount} enabled Tracing objects.`,
|
2018-04-04 01:05:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
2023-12-28 23:20:22 +00:00
|
|
|
if (this.#enabled) {
|
|
|
|
this.#enabled = false;
|
|
|
|
this.#handle.disable();
|
2018-04-04 01:05:33 +00:00
|
|
|
enabledTracingObjects.delete(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get enabled() {
|
2023-12-28 23:20:22 +00:00
|
|
|
return this.#enabled;
|
2018-04-04 01:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get categories() {
|
2023-12-28 23:20:22 +00:00
|
|
|
return ArrayPrototypeJoin(this.#categories, ',');
|
2018-04-04 01:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[customInspectSymbol](depth, opts) {
|
2019-06-03 02:33:23 +00:00
|
|
|
if (typeof depth === 'number' && depth < 0)
|
|
|
|
return this;
|
|
|
|
|
2018-04-04 01:05:33 +00:00
|
|
|
const obj = {
|
|
|
|
enabled: this.enabled,
|
2023-02-12 18:26:21 +00:00
|
|
|
categories: this.categories,
|
2018-04-04 01:05:33 +00:00
|
|
|
};
|
|
|
|
return `Tracing ${format(obj)}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTracing(options) {
|
2021-01-21 11:03:00 +00:00
|
|
|
validateObject(options, 'options');
|
2023-01-19 09:08:38 +00:00
|
|
|
validateStringArray(options.categories, 'options.categories');
|
2018-04-04 01:05:33 +00:00
|
|
|
|
|
|
|
if (options.categories.length <= 0)
|
|
|
|
throw new ERR_TRACE_EVENTS_CATEGORY_REQUIRED();
|
|
|
|
|
|
|
|
return new Tracing(options.categories);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createTracing,
|
2023-02-12 18:26:21 +00:00
|
|
|
getEnabledCategories,
|
2018-04-04 01:05:33 +00:00
|
|
|
};
|