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,
|
2019-11-30 19:13:55 +00:00
|
|
|
ArrayBuffer,
|
2020-12-05 10:56:34 +00:00
|
|
|
ArrayPrototypeForEach,
|
|
|
|
ArrayPrototypePush,
|
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,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectPrototypeToString,
|
2020-11-20 14:48:18 +00:00
|
|
|
SafeMap,
|
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');
|
2018-12-11 15:24:22 +00:00
|
|
|
const { validateString } = require('internal/validators');
|
2017-06-08 08:17:02 +00:00
|
|
|
const {
|
2020-12-17 11:10:00 +00:00
|
|
|
Serializer,
|
|
|
|
Deserializer
|
2018-08-06 20:59:11 +00:00
|
|
|
} = internalBinding('serdes');
|
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');
|
2018-08-17 08:33:45 +00:00
|
|
|
const { copy } = internalBinding('buffer');
|
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 { toNamespacedPath } = require('path');
|
|
|
|
const {
|
|
|
|
createHeapSnapshotStream,
|
|
|
|
triggerHeapSnapshot
|
|
|
|
} = internalBinding('heap_utils');
|
2020-01-29 16:41:26 +00:00
|
|
|
const { HeapSnapshotStream } = require('internal/heap_utils');
|
2021-07-05 00:43:26 +00:00
|
|
|
const promiseHooks = require('internal/promise_hooks');
|
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 writes it to a JSON file.
|
|
|
|
* @param {string} [filename]
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2019-03-07 16:51:36 +00:00
|
|
|
function writeHeapSnapshot(filename) {
|
|
|
|
if (filename !== undefined) {
|
2019-05-12 12:30:29 +00:00
|
|
|
filename = getValidatedPath(filename);
|
2019-03-07 16:51:36 +00:00
|
|
|
filename = toNamespacedPath(filename);
|
|
|
|
}
|
|
|
|
return triggerHeapSnapshot(filename);
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:16:27 +00:00
|
|
|
/**
|
|
|
|
* Generates a snapshot of the current V8 heap
|
|
|
|
* and returns a Readable Stream.
|
|
|
|
* @returns {import('./stream.js').Readable}
|
|
|
|
*/
|
2019-03-07 16:51:36 +00:00
|
|
|
function getHeapSnapshot() {
|
|
|
|
const handle = createHeapSnapshotStream();
|
|
|
|
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,
|
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,
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
kExternalScriptSourceSizeIndex
|
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() {
|
2021-02-08 15:19:37 +00:00
|
|
|
const buffer = binding.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],
|
|
|
|
number_of_detached_contexts: buffer[kNumberOfDetachedContextsIndex]
|
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);
|
2021-02-08 15:19:37 +00:00
|
|
|
const buffer = binding.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],
|
|
|
|
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;
|
|
|
|
* }}
|
|
|
|
*/
|
2019-05-30 13:41:03 +00:00
|
|
|
function getHeapCodeStatistics() {
|
2021-02-08 15:19:37 +00:00
|
|
|
const buffer = binding.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],
|
|
|
|
external_script_source_size: buffer[kExternalScriptSourceSizeIndex]
|
2019-05-30 13:41:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
const arrayBufferViewTypes = [Int8Array, Uint8Array, Uint8ClampedArray,
|
|
|
|
Int16Array, Uint16Array, Int32Array, Uint32Array,
|
|
|
|
Float32Array, Float64Array, DataView];
|
|
|
|
|
2020-11-20 14:48:18 +00:00
|
|
|
const arrayBufferViewTypeToIndex = new SafeMap();
|
2017-01-27 05:38:11 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const dummy = new ArrayBuffer();
|
2020-12-05 10:56:34 +00:00
|
|
|
ArrayPrototypeForEach(arrayBufferViewTypes, (ctor, i) => {
|
2019-11-22 17:04:46 +00:00
|
|
|
const tag = ObjectPrototypeToString(new ctor(dummy));
|
2017-01-27 05:38:11 +00:00
|
|
|
arrayBufferViewTypeToIndex.set(tag, i);
|
2020-12-05 10:56:34 +00:00
|
|
|
});
|
2017-01-27 05:38:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 10:56:34 +00:00
|
|
|
const bufferConstructorIndex =
|
|
|
|
ArrayPrototypePush(arrayBufferViewTypes, FastBuffer) - 1;
|
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.
|
|
|
|
* @param {Object} abView
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-27 05:38:11 +00:00
|
|
|
_writeHostObject(abView) {
|
|
|
|
let i = 0;
|
|
|
|
if (abView.constructor === Buffer) {
|
|
|
|
i = bufferConstructorIndex;
|
|
|
|
} else {
|
2019-11-22 17:04:46 +00:00
|
|
|
const tag = ObjectPrototypeToString(abView);
|
2017-01-27 05:38:11 +00:00
|
|
|
i = arrayBufferViewTypeToIndex.get(tag);
|
|
|
|
|
|
|
|
if (i === undefined) {
|
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();
|
|
|
|
const ctor = arrayBufferViewTypes[typeIndex];
|
|
|
|
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);
|
|
|
|
copy(this.buffer, buffer_copy, 0, byteOffset, byteOffset + byteLength);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
2017-01-27 05:38:11 +00:00
|
|
|
};
|