From 5f57005ec9eacbfc7855ec7c9e246f61da5a75ae Mon Sep 17 00:00:00 2001 From: Ben Ripkens Date: Tue, 29 Dec 2015 11:54:35 +0100 Subject: v8,src: expose statistics about heap spaces Provide means to inspect information about the separate heap spaces via a callable API. This is helpful to analyze memory issues. Fixes: https://github.com/nodejs/node/issues/2079 PR-URL: https://github.com/nodejs/node/pull/4463 Reviewed-By: Colin Ihrig Reviewed-By: Trevor Norris Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- lib/v8.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'lib/v8.js') diff --git a/lib/v8.js b/lib/v8.js index acadfa64e0..551b2ada98 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -16,9 +16,9 @@ const v8binding = process.binding('v8'); +// Properties for heap statistics buffer extraction. const heapStatisticsBuffer = new Uint32Array(v8binding.heapStatisticsArrayBuffer); - const kTotalHeapSizeIndex = v8binding.kTotalHeapSizeIndex; const kTotalHeapSizeExecutableIndex = v8binding.kTotalHeapSizeExecutableIndex; const kTotalPhysicalSizeIndex = v8binding.kTotalPhysicalSizeIndex; @@ -26,6 +26,18 @@ const kTotalAvailableSize = v8binding.kTotalAvailableSize; const kUsedHeapSizeIndex = v8binding.kUsedHeapSizeIndex; const kHeapSizeLimitIndex = v8binding.kHeapSizeLimitIndex; +// Properties for heap space statistics buffer extraction. +const heapSpaceStatisticsBuffer = + new Uint32Array(v8binding.heapSpaceStatisticsArrayBuffer); +const kHeapSpaces = v8binding.kHeapSpaces; +const kNumberOfHeapSpaces = kHeapSpaces.length; +const kHeapSpaceStatisticsPropertiesCount = + v8binding.kHeapSpaceStatisticsPropertiesCount; +const kSpaceSizeIndex = v8binding.kSpaceSizeIndex; +const kSpaceUsedSizeIndex = v8binding.kSpaceUsedSizeIndex; +const kSpaceAvailableSizeIndex = v8binding.kSpaceAvailableSizeIndex; +const kPhysicalSpaceSizeIndex = v8binding.kPhysicalSpaceSizeIndex; + exports.getHeapStatistics = function() { const buffer = heapStatisticsBuffer; @@ -42,3 +54,22 @@ exports.getHeapStatistics = function() { }; exports.setFlagsFromString = v8binding.setFlagsFromString; + +exports.getHeapSpaceStatistics = function() { + const heapSpaceStatistics = new Array(kNumberOfHeapSpaces); + const buffer = heapSpaceStatisticsBuffer; + v8binding.updateHeapSpaceStatisticsArrayBuffer(); + + for (let i = 0; i < kNumberOfHeapSpaces; i++) { + const propertyOffset = i * kHeapSpaceStatisticsPropertiesCount; + heapSpaceStatistics[i] = { + space_name: kHeapSpaces[i], + space_size: buffer[propertyOffset + kSpaceSizeIndex], + space_used_size: buffer[propertyOffset + kSpaceUsedSizeIndex], + space_available_size: buffer[propertyOffset + kSpaceAvailableSizeIndex], + physical_space_size: buffer[propertyOffset + kPhysicalSpaceSizeIndex] + }; + } + + return heapSpaceStatistics; +}; -- cgit v1.2.1