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