2014-12-09 21:57:48 +00:00
|
|
|
// Copyright (c) 2014, StrongLoop Inc.
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2019-11-23 09:09:05 +00:00
|
|
|
Array,
|
2022-07-15 07:56:04 +00:00
|
|
|
BigInt64Array,
|
|
|
|
BigUint64Array,
|
2020-09-29 18:29:40 +00:00
|
|
|
DataView,
|
2020-01-02 18:19:02 +00:00
|
|
|
Error,
|
2019-11-30 19:13:55 +00:00
|
|
|
Float32Array,
|
|
|
|
Float64Array,
|
|
|
|
Int16Array,
|
|
|
|
Int32Array,
|
|
|
|
Int8Array,
|
2024-04-21 16:53:08 +00:00
|
|
|
JSONParse,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectPrototypeToString,
|
2019-11-30 19:13:55 +00:00
|
|
|
Uint16Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint8Array,
|
|
|
|
Uint8ClampedArray,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2017-04-26 23:48:05 +00:00
|
|
|
const { Buffer } = require('buffer');
|
2022-09-08 09:33:20 +00:00
|
|
|
const { validateString, validateUint32 } = require('internal/validators');
|
2017-06-08 08:17:02 +00:00
|
|
|
const {
|
2020-12-17 11:10:00 +00:00
|
|
|
Serializer,
|
2023-02-12 18:26:21 +00:00
|
|
|
Deserializer,
|
2018-08-06 20:59:11 +00:00
|
|
|
} = internalBinding('serdes');
|
2022-04-18 04:36:36 +00:00
|
|
|
const {
|
2023-02-12 18:26:21 +00:00
|
|
|
namespace: startupSnapshot,
|
2022-04-18 04:36:36 +00:00
|
|
|
} = require('internal/v8/startup_snapshot');
|
2020-06-09 12:46:52 +00:00
|
|
|
|
|
|
|
let profiler = {};
|
|
|
|
if (internalBinding('config').hasInspector) {
|
|
|
|
profiler = internalBinding('profiler');
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:51:36 +00:00
|
|
|
const assert = require('internal/assert');
|
2019-10-29 20:24:23 +00:00
|
|
|
const { inspect } = require('internal/util/inspect');
|
2017-01-27 05:38:11 +00:00
|
|
|
const { FastBuffer } = require('internal/buffer');
|
2019-05-12 12:30:29 +00:00
|
|
|
const { getValidatedPath } = require('internal/fs/utils');
|
2019-03-07 16:51:36 +00:00
|
|
|
const {
|
|
|
|
createHeapSnapshotStream,
|
2023-02-12 18:26:21 +00:00
|
|
|
triggerHeapSnapshot,
|
2019-03-07 16:51:36 +00:00
|
|
|
} = internalBinding('heap_utils');
|
2022-10-13 12:37:22 +00:00
|
|
|
const {
|
|
|
|
HeapSnapshotStream,
|
2023-02-12 18:26:21 +00:00
|
|
|
getHeapSnapshotOptions,
|
2024-03-02 22:11:30 +00:00
|
|
|
queryObjects,
|
2022-10-13 12:37:22 +00:00
|
|
|
} = require('internal/heap_utils');
|
2021-07-05 00:43:26 +00:00
|
|
|
const promiseHooks = require('internal/promise_hooks');
|
2022-09-08 09:33:20 +00:00
|
|
|
const { getOptionValue } = require('internal/options');
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Generates a snapshot of the current V8 heap
|
|
|
|
* and writes it to a JSON file.
|
|
|
|
* @param {string} [filename]
|
2022-10-13 12:37:22 +00:00
|
|
|
* @param {{
|
|
|
|
* exposeInternals?: boolean,
|
|
|
|
* exposeNumericValues?: boolean
|
|
|
|
* }} [options]
|
2021-06-05 21:16:27 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-10-13 12:37:22 +00:00
|
|
|
function writeHeapSnapshot(filename, options) {
|
2019-03-07 16:51:36 +00:00
|
|
|
if (filename !== undefined) {
|
2019-05-12 12:30:29 +00:00
|
|
|
filename = getValidatedPath(filename);
|
2019-03-07 16:51:36 +00:00
|
|
|
}
|
2022-10-13 12:37:22 +00:00
|
|
|
const optionArray = getHeapSnapshotOptions(options);
|
|
|
|
return triggerHeapSnapshot(filename, optionArray);
|
2019-03-07 16:51:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Generates a snapshot of the current V8 heap
|
|
|
|
* and returns a Readable Stream.
|
2022-10-13 12:37:22 +00:00
|
|
|
* @param {{
|
|
|
|
* exposeInternals?: boolean,
|
|
|
|
* exposeNumericValues?: boolean
|
|
|
|
* }} [options]
|
2021-06-05 21:16:27 +00:00
|
|
|
* @returns {import('./stream.js').Readable}
|
|
|
|
*/
|
2022-10-13 12:37:22 +00:00
|
|
|
function getHeapSnapshot(options) {
|
|
|
|
const optionArray = getHeapSnapshotOptions(options);
|
|
|
|
const handle = createHeapSnapshotStream(optionArray);
|
2019-03-07 16:51:36 +00:00
|
|
|
assert(handle);
|
|
|
|
return new HeapSnapshotStream(handle);
|
|
|
|
}
|
2015-01-21 16:36:59 +00:00
|
|
|
|
2021-02-08 15:19:37 +00:00
|
|
|
// We need to get the buffer from the binding at the callsite since
|
|
|
|
// it's re-initialized after deserialization.
|
|
|
|
const binding = internalBinding('v8');
|
|
|
|
|
2017-04-26 23:48:05 +00:00
|
|
|
const {
|
|
|
|
cachedDataVersionTag,
|
2017-10-26 23:31:23 +00:00
|
|
|
setFlagsFromString: _setFlagsFromString,
|
2020-04-19 11:59:04 +00:00
|
|
|
updateHeapStatisticsBuffer,
|
|
|
|
updateHeapSpaceStatisticsBuffer,
|
|
|
|
updateHeapCodeStatisticsBuffer,
|
2022-09-08 09:33:20 +00:00
|
|
|
setHeapSnapshotNearHeapLimit: _setHeapSnapshotNearHeapLimit,
|
2017-04-26 23:48:05 +00:00
|
|
|
|
2019-05-30 13:41:03 +00:00
|
|
|
// Properties for heap statistics buffer extraction.
|
2017-04-26 23:48:05 +00:00
|
|
|
kTotalHeapSizeIndex,
|
|
|
|
kTotalHeapSizeExecutableIndex,
|
|
|
|
kTotalPhysicalSizeIndex,
|
|
|
|
kTotalAvailableSize,
|
|
|
|
kUsedHeapSizeIndex,
|
|
|
|
kHeapSizeLimitIndex,
|
|
|
|
kDoesZapGarbageIndex,
|
|
|
|
kMallocedMemoryIndex,
|
|
|
|
kPeakMallocedMemoryIndex,
|
2019-05-30 13:41:03 +00:00
|
|
|
kNumberOfNativeContextsIndex,
|
|
|
|
kNumberOfDetachedContextsIndex,
|
2022-04-23 15:35:36 +00:00
|
|
|
kTotalGlobalHandlesSizeIndex,
|
|
|
|
kUsedGlobalHandlesSizeIndex,
|
|
|
|
kExternalMemoryIndex,
|
2019-05-30 13:41:03 +00:00
|
|
|
|
|
|
|
// Properties for heap spaces statistics buffer extraction.
|
2017-04-26 23:48:05 +00:00
|
|
|
kHeapSpaces,
|
|
|
|
kSpaceSizeIndex,
|
|
|
|
kSpaceUsedSizeIndex,
|
|
|
|
kSpaceAvailableSizeIndex,
|
2019-05-28 10:41:12 +00:00
|
|
|
kPhysicalSpaceSizeIndex,
|
2019-05-30 13:41:03 +00:00
|
|
|
|
|
|
|
// Properties for heap code statistics buffer extraction.
|
|
|
|
kCodeAndMetadataSizeIndex,
|
|
|
|
kBytecodeAndMetadataSizeIndex,
|
2022-04-29 15:21:41 +00:00
|
|
|
kExternalScriptSourceSizeIndex,
|
|
|
|
kCPUProfilerMetaDataSizeIndex,
|
2023-04-20 03:28:35 +00:00
|
|
|
|
|
|
|
heapStatisticsBuffer,
|
|
|
|
heapCodeStatisticsBuffer,
|
|
|
|
heapSpaceStatisticsBuffer,
|
2021-02-08 15:19:37 +00:00
|
|
|
} = binding;
|
2017-04-26 23:48:05 +00:00
|
|
|
|
|
|
|
const kNumberOfHeapSpaces = kHeapSpaces.length;
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Sets V8 command-line flags.
|
|
|
|
* @param {string} flags
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-10-26 23:31:23 +00:00
|
|
|
function setFlagsFromString(flags) {
|
2018-12-11 15:24:22 +00:00
|
|
|
validateString(flags, 'flags');
|
2017-10-26 23:31:23 +00:00
|
|
|
_setFlagsFromString(flags);
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Gets the current V8 heap statistics.
|
|
|
|
* @returns {{
|
|
|
|
* total_heap_size: number;
|
|
|
|
* total_heap_size_executable: number;
|
|
|
|
* total_physical_size: number;
|
|
|
|
* total_available_size: number;
|
|
|
|
* used_heap_size: number;
|
|
|
|
* heap_size_limit: number;
|
|
|
|
* malloced_memory: number;
|
|
|
|
* peak_malloced_memory: number;
|
|
|
|
* does_zap_garbage: number;
|
|
|
|
* number_of_native_contexts: number;
|
|
|
|
* number_of_detached_contexts: number;
|
|
|
|
* }}
|
|
|
|
*/
|
2017-04-26 23:48:05 +00:00
|
|
|
function getHeapStatistics() {
|
2023-04-20 03:28:35 +00:00
|
|
|
const buffer = heapStatisticsBuffer;
|
2015-01-16 16:15:17 +00:00
|
|
|
|
2020-04-19 11:59:04 +00:00
|
|
|
updateHeapStatisticsBuffer();
|
2015-01-16 16:15:17 +00:00
|
|
|
|
|
|
|
return {
|
2021-09-03 00:48:58 +00:00
|
|
|
total_heap_size: buffer[kTotalHeapSizeIndex],
|
|
|
|
total_heap_size_executable: buffer[kTotalHeapSizeExecutableIndex],
|
|
|
|
total_physical_size: buffer[kTotalPhysicalSizeIndex],
|
|
|
|
total_available_size: buffer[kTotalAvailableSize],
|
|
|
|
used_heap_size: buffer[kUsedHeapSizeIndex],
|
|
|
|
heap_size_limit: buffer[kHeapSizeLimitIndex],
|
|
|
|
malloced_memory: buffer[kMallocedMemoryIndex],
|
|
|
|
peak_malloced_memory: buffer[kPeakMallocedMemoryIndex],
|
|
|
|
does_zap_garbage: buffer[kDoesZapGarbageIndex],
|
|
|
|
number_of_native_contexts: buffer[kNumberOfNativeContextsIndex],
|
2022-04-23 15:35:36 +00:00
|
|
|
number_of_detached_contexts: buffer[kNumberOfDetachedContextsIndex],
|
|
|
|
total_global_handles_size: buffer[kTotalGlobalHandlesSizeIndex],
|
|
|
|
used_global_handles_size: buffer[kUsedGlobalHandlesSizeIndex],
|
2023-02-12 18:26:21 +00:00
|
|
|
external_memory: buffer[kExternalMemoryIndex],
|
2015-01-16 16:15:17 +00:00
|
|
|
};
|
2017-04-26 23:48:05 +00:00
|
|
|
}
|
2015-12-29 10:54:35 +00:00
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Gets the current V8 heap space statistics.
|
|
|
|
* @returns {{
|
|
|
|
* space_name: string;
|
|
|
|
* space_size: number;
|
|
|
|
* space_used_size: number;
|
|
|
|
* space_available_size: number;
|
|
|
|
* physical_space_size: number;
|
|
|
|
* }[]}
|
|
|
|
*/
|
2017-04-26 23:48:05 +00:00
|
|
|
function getHeapSpaceStatistics() {
|
2015-12-29 10:54:35 +00:00
|
|
|
const heapSpaceStatistics = new Array(kNumberOfHeapSpaces);
|
2023-04-20 03:28:35 +00:00
|
|
|
const buffer = heapSpaceStatisticsBuffer;
|
2015-12-29 10:54:35 +00:00
|
|
|
|
2019-11-06 13:46:24 +00:00
|
|
|
for (let i = 0; i < kNumberOfHeapSpaces; i++) {
|
2020-04-19 11:59:04 +00:00
|
|
|
updateHeapSpaceStatisticsBuffer(i);
|
2015-12-29 10:54:35 +00:00
|
|
|
heapSpaceStatistics[i] = {
|
|
|
|
space_name: kHeapSpaces[i],
|
2020-04-19 11:59:04 +00:00
|
|
|
space_size: buffer[kSpaceSizeIndex],
|
|
|
|
space_used_size: buffer[kSpaceUsedSizeIndex],
|
|
|
|
space_available_size: buffer[kSpaceAvailableSizeIndex],
|
2023-02-12 18:26:21 +00:00
|
|
|
physical_space_size: buffer[kPhysicalSpaceSizeIndex],
|
2015-12-29 10:54:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return heapSpaceStatistics;
|
2017-04-26 23:48:05 +00:00
|
|
|
}
|
2017-01-27 05:38:11 +00:00
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Gets the current V8 heap code statistics.
|
|
|
|
* @returns {{
|
|
|
|
* code_and_metadata_size: number;
|
|
|
|
* bytecode_and_metadata_size: number;
|
|
|
|
* external_script_source_size: number;
|
2022-04-29 15:21:41 +00:00
|
|
|
* cpu_profiler_metadata_size: number;
|
2021-06-05 21:16:27 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2019-05-30 13:41:03 +00:00
|
|
|
function getHeapCodeStatistics() {
|
2023-04-20 03:28:35 +00:00
|
|
|
const buffer = heapCodeStatisticsBuffer;
|
2019-05-30 13:41:03 +00:00
|
|
|
|
2020-04-19 11:59:04 +00:00
|
|
|
updateHeapCodeStatisticsBuffer();
|
2019-05-30 13:41:03 +00:00
|
|
|
return {
|
2021-09-03 00:48:58 +00:00
|
|
|
code_and_metadata_size: buffer[kCodeAndMetadataSizeIndex],
|
|
|
|
bytecode_and_metadata_size: buffer[kBytecodeAndMetadataSizeIndex],
|
2022-04-29 15:21:41 +00:00
|
|
|
external_script_source_size: buffer[kExternalScriptSourceSizeIndex],
|
|
|
|
cpu_profiler_metadata_size: buffer[kCPUProfilerMetaDataSizeIndex],
|
2019-05-30 13:41:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-08 09:33:20 +00:00
|
|
|
let heapSnapshotNearHeapLimitCallbackAdded = false;
|
|
|
|
function setHeapSnapshotNearHeapLimit(limit) {
|
2024-06-05 22:59:05 +00:00
|
|
|
validateUint32(limit, 'limit', true);
|
2022-09-08 09:33:20 +00:00
|
|
|
if (heapSnapshotNearHeapLimitCallbackAdded ||
|
|
|
|
getOptionValue('--heapsnapshot-near-heap-limit') > 0
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
heapSnapshotNearHeapLimitCallbackAdded = true;
|
|
|
|
_setHeapSnapshotNearHeapLimit(limit);
|
|
|
|
}
|
|
|
|
|
2017-01-27 05:38:11 +00:00
|
|
|
/* V8 serialization API */
|
|
|
|
|
|
|
|
/* JS methods for the base objects */
|
|
|
|
Serializer.prototype._getDataCloneError = Error;
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Reads raw bytes from the deserializer's internal buffer.
|
|
|
|
* @param {number} length
|
|
|
|
* @returns {Buffer}
|
|
|
|
*/
|
2017-04-26 23:48:05 +00:00
|
|
|
Deserializer.prototype.readRawBytes = function readRawBytes(length) {
|
2017-01-27 05:38:11 +00:00
|
|
|
const offset = this._readRawBytes(length);
|
|
|
|
// `this.buffer` can be a Buffer or a plain Uint8Array, so just calling
|
|
|
|
// `.slice()` doesn't work.
|
|
|
|
return new FastBuffer(this.buffer.buffer,
|
|
|
|
this.buffer.byteOffset + offset,
|
|
|
|
length);
|
|
|
|
};
|
|
|
|
|
2022-07-15 07:56:04 +00:00
|
|
|
function arrayBufferViewTypeToIndex(abView) {
|
|
|
|
const type = ObjectPrototypeToString(abView);
|
|
|
|
if (type === '[object Int8Array]') return 0;
|
|
|
|
if (type === '[object Uint8Array]') return 1;
|
|
|
|
if (type === '[object Uint8ClampedArray]') return 2;
|
|
|
|
if (type === '[object Int16Array]') return 3;
|
|
|
|
if (type === '[object Uint16Array]') return 4;
|
|
|
|
if (type === '[object Int32Array]') return 5;
|
|
|
|
if (type === '[object Uint32Array]') return 6;
|
|
|
|
if (type === '[object Float32Array]') return 7;
|
|
|
|
if (type === '[object Float64Array]') return 8;
|
|
|
|
if (type === '[object DataView]') return 9;
|
|
|
|
// Index 10 is FastBuffer.
|
|
|
|
if (type === '[object BigInt64Array]') return 11;
|
|
|
|
if (type === '[object BigUint64Array]') return 12;
|
|
|
|
return -1;
|
2017-01-27 05:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 07:56:04 +00:00
|
|
|
function arrayBufferViewIndexToType(index) {
|
|
|
|
if (index === 0) return Int8Array;
|
|
|
|
if (index === 1) return Uint8Array;
|
|
|
|
if (index === 2) return Uint8ClampedArray;
|
|
|
|
if (index === 3) return Int16Array;
|
|
|
|
if (index === 4) return Uint16Array;
|
|
|
|
if (index === 5) return Int32Array;
|
|
|
|
if (index === 6) return Uint32Array;
|
|
|
|
if (index === 7) return Float32Array;
|
|
|
|
if (index === 8) return Float64Array;
|
|
|
|
if (index === 9) return DataView;
|
|
|
|
if (index === 10) return FastBuffer;
|
|
|
|
if (index === 11) return BigInt64Array;
|
|
|
|
if (index === 12) return BigUint64Array;
|
|
|
|
return undefined;
|
|
|
|
}
|
2017-01-27 05:38:11 +00:00
|
|
|
|
|
|
|
class DefaultSerializer extends Serializer {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this._setTreatArrayBufferViewsAsHostObjects(true);
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Used to write some kind of host object, i.e. an
|
|
|
|
* object that is created by native C++ bindings.
|
2021-11-29 06:48:00 +00:00
|
|
|
* @param {object} abView
|
2021-06-05 21:16:27 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-27 05:38:11 +00:00
|
|
|
_writeHostObject(abView) {
|
2022-07-15 07:56:04 +00:00
|
|
|
// Keep track of how to handle different ArrayBufferViews. The default
|
|
|
|
// Serializer for Node does not use the V8 methods for serializing those
|
|
|
|
// objects because Node's `Buffer` objects use pooled allocation in many
|
|
|
|
// cases, and their underlying `ArrayBuffer`s would show up in the
|
|
|
|
// serialization. Because a) those may contain sensitive data and the user
|
|
|
|
// may not be aware of that and b) they are often much larger than the
|
|
|
|
// `Buffer` itself, custom serialization is applied.
|
|
|
|
let i = 10; // FastBuffer
|
|
|
|
if (abView.constructor !== Buffer) {
|
|
|
|
i = arrayBufferViewTypeToIndex(abView);
|
|
|
|
if (i === -1) {
|
2019-10-29 20:24:23 +00:00
|
|
|
throw new this._getDataCloneError(
|
|
|
|
`Unserializable host object: ${inspect(abView)}`);
|
2017-01-27 05:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.writeUint32(i);
|
|
|
|
this.writeUint32(abView.byteLength);
|
|
|
|
this.writeRawBytes(new Uint8Array(abView.buffer,
|
|
|
|
abView.byteOffset,
|
|
|
|
abView.byteLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DefaultDeserializer extends Deserializer {
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Used to read some kind of host object, i.e. an
|
|
|
|
* object that is created by native C++ bindings.
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2017-01-27 05:38:11 +00:00
|
|
|
_readHostObject() {
|
|
|
|
const typeIndex = this.readUint32();
|
2022-07-15 07:56:04 +00:00
|
|
|
const ctor = arrayBufferViewIndexToType(typeIndex);
|
2017-01-27 05:38:11 +00:00
|
|
|
const byteLength = this.readUint32();
|
|
|
|
const byteOffset = this._readRawBytes(byteLength);
|
|
|
|
const BYTES_PER_ELEMENT = ctor.BYTES_PER_ELEMENT || 1;
|
|
|
|
|
|
|
|
const offset = this.buffer.byteOffset + byteOffset;
|
|
|
|
if (offset % BYTES_PER_ELEMENT === 0) {
|
|
|
|
return new ctor(this.buffer.buffer,
|
|
|
|
offset,
|
|
|
|
byteLength / BYTES_PER_ELEMENT);
|
|
|
|
}
|
2020-04-08 16:58:03 +00:00
|
|
|
// Copy to an aligned buffer first.
|
|
|
|
const buffer_copy = Buffer.allocUnsafe(byteLength);
|
2024-10-07 11:11:29 +00:00
|
|
|
buffer_copy.set(new Uint8Array(this.buffer.buffer, this.buffer.byteOffset + byteOffset, byteLength));
|
2020-04-08 16:58:03 +00:00
|
|
|
return new ctor(buffer_copy.buffer,
|
|
|
|
buffer_copy.byteOffset,
|
|
|
|
byteLength / BYTES_PER_ELEMENT);
|
2017-01-27 05:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Uses a `DefaultSerializer` to serialize `value`
|
|
|
|
* into a buffer.
|
|
|
|
* @param {any} value
|
|
|
|
* @returns {Buffer}
|
|
|
|
*/
|
2017-04-26 23:48:05 +00:00
|
|
|
function serialize(value) {
|
2017-01-27 05:38:11 +00:00
|
|
|
const ser = new DefaultSerializer();
|
|
|
|
ser.writeHeader();
|
|
|
|
ser.writeValue(value);
|
|
|
|
return ser.releaseBuffer();
|
2017-04-26 23:48:05 +00:00
|
|
|
}
|
2017-01-27 05:38:11 +00:00
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Uses a `DefaultDeserializer` with default options
|
|
|
|
* to read a JavaScript value from a buffer.
|
|
|
|
* @param {Buffer | TypedArray | DataView} buffer
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2017-04-26 23:48:05 +00:00
|
|
|
function deserialize(buffer) {
|
2017-01-27 05:38:11 +00:00
|
|
|
const der = new DefaultDeserializer(buffer);
|
|
|
|
der.readHeader();
|
|
|
|
return der.readValue();
|
2017-04-26 23:48:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 12:02:09 +00:00
|
|
|
class GCProfiler {
|
|
|
|
#profiler = null;
|
|
|
|
|
|
|
|
start() {
|
|
|
|
if (!this.#profiler) {
|
|
|
|
this.#profiler = new binding.GCProfiler();
|
|
|
|
this.#profiler.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if (this.#profiler) {
|
|
|
|
const data = this.#profiler.stop();
|
|
|
|
this.#profiler = null;
|
|
|
|
return JSONParse(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 08:45:25 +00:00
|
|
|
module.exports = {
|
2017-04-26 23:48:05 +00:00
|
|
|
cachedDataVersionTag,
|
2019-03-07 16:51:36 +00:00
|
|
|
getHeapSnapshot,
|
2017-04-26 23:48:05 +00:00
|
|
|
getHeapStatistics,
|
|
|
|
getHeapSpaceStatistics,
|
2019-05-30 13:41:03 +00:00
|
|
|
getHeapCodeStatistics,
|
2017-04-26 23:48:05 +00:00
|
|
|
setFlagsFromString,
|
|
|
|
Serializer,
|
|
|
|
Deserializer,
|
|
|
|
DefaultSerializer,
|
|
|
|
DefaultDeserializer,
|
|
|
|
deserialize,
|
2020-06-09 12:46:52 +00:00
|
|
|
takeCoverage: profiler.takeCoverage,
|
2020-06-09 13:18:25 +00:00
|
|
|
stopCoverage: profiler.stopCoverage,
|
2019-03-07 16:51:36 +00:00
|
|
|
serialize,
|
2020-01-29 16:41:26 +00:00
|
|
|
writeHeapSnapshot,
|
2021-07-05 00:43:26 +00:00
|
|
|
promiseHooks,
|
2024-03-02 22:11:30 +00:00
|
|
|
queryObjects,
|
2022-09-08 09:33:20 +00:00
|
|
|
startupSnapshot,
|
|
|
|
setHeapSnapshotNearHeapLimit,
|
2023-01-25 12:02:09 +00:00
|
|
|
GCProfiler,
|
2017-01-27 05:38:11 +00:00
|
|
|
};
|