Skip to content

Commit cce2087

Browse files
Yuriy Vasiyarovtargos
Yuriy Vasiyarov
authored andcommitted
src: export v8.GetHeapCodeAndMetadataStatistics()
Export statistic provided by V8 through HeapCodeStatistics class and and GetHeapCodeAndMetadataStatistics function to v8 Node.js module PR-URL: #27978 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent bbcf9f0 commit cce2087

File tree

7 files changed

+132
-3
lines changed

7 files changed

+132
-3
lines changed

doc/api/v8.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,28 @@ being non-zero indicates a potential memory leak.
166166
}
167167
```
168168

169+
## v8.getHeapCodeStatistics()
170+
<!-- YAML
171+
added: REPLACEME
172+
-->
173+
174+
* Returns: {Object}
175+
176+
Returns an object with the following properties:
177+
178+
* `code_and_metadata_size` {number}
179+
* `bytecode_and_metadata_size` {number}
180+
* `external_script_source_size` {number}
181+
182+
<!-- eslint-skip -->
183+
```js
184+
{
185+
code_and_metadata_size: 212208,
186+
bytecode_and_metadata_size: 161368,
187+
external_script_source_size: 1410794
188+
}
189+
```
190+
169191
## v8.setFlagsFromString(flags)
170192
<!-- YAML
171193
added: v1.0.0

lib/v8.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ const {
9191
setFlagsFromString: _setFlagsFromString,
9292
heapStatisticsArrayBuffer,
9393
heapSpaceStatisticsArrayBuffer,
94+
heapCodeStatisticsArrayBuffer,
9495
updateHeapStatisticsArrayBuffer,
9596
updateHeapSpaceStatisticsArrayBuffer,
97+
updateHeapCodeStatisticsArrayBuffer,
9698

97-
// Properties for heap and heap space statistics buffer extraction.
99+
// Properties for heap statistics buffer extraction.
98100
kTotalHeapSizeIndex,
99101
kTotalHeapSizeExecutableIndex,
100102
kTotalPhysicalSizeIndex,
@@ -104,14 +106,21 @@ const {
104106
kDoesZapGarbageIndex,
105107
kMallocedMemoryIndex,
106108
kPeakMallocedMemoryIndex,
109+
kNumberOfNativeContextsIndex,
110+
kNumberOfDetachedContextsIndex,
111+
112+
// Properties for heap spaces statistics buffer extraction.
107113
kHeapSpaces,
108114
kHeapSpaceStatisticsPropertiesCount,
109115
kSpaceSizeIndex,
110116
kSpaceUsedSizeIndex,
111117
kSpaceAvailableSizeIndex,
112118
kPhysicalSpaceSizeIndex,
113-
kNumberOfNativeContextsIndex,
114-
kNumberOfDetachedContextsIndex
119+
120+
// Properties for heap code statistics buffer extraction.
121+
kCodeAndMetadataSizeIndex,
122+
kBytecodeAndMetadataSizeIndex,
123+
kExternalScriptSourceSizeIndex
115124
} = internalBinding('v8');
116125

117126
const kNumberOfHeapSpaces = kHeapSpaces.length;
@@ -122,6 +131,9 @@ const heapStatisticsBuffer =
122131
const heapSpaceStatisticsBuffer =
123132
new Float64Array(heapSpaceStatisticsArrayBuffer);
124133

134+
const heapCodeStatisticsBuffer =
135+
new Float64Array(heapCodeStatisticsArrayBuffer);
136+
125137
function setFlagsFromString(flags) {
126138
validateString(flags, 'flags');
127139
_setFlagsFromString(flags);
@@ -166,6 +178,17 @@ function getHeapSpaceStatistics() {
166178
return heapSpaceStatistics;
167179
}
168180

181+
function getHeapCodeStatistics() {
182+
const buffer = heapCodeStatisticsBuffer;
183+
184+
updateHeapCodeStatisticsArrayBuffer();
185+
return {
186+
'code_and_metadata_size': buffer[kCodeAndMetadataSizeIndex],
187+
'bytecode_and_metadata_size': buffer[kBytecodeAndMetadataSizeIndex],
188+
'external_script_source_size': buffer[kExternalScriptSourceSizeIndex]
189+
};
190+
}
191+
169192
/* V8 serialization API */
170193

171194
/* JS methods for the base objects */
@@ -272,6 +295,7 @@ module.exports = {
272295
getHeapSnapshot,
273296
getHeapStatistics,
274297
getHeapSpaceStatistics,
298+
getHeapCodeStatistics,
275299
setFlagsFromString,
276300
Serializer,
277301
Deserializer,

src/env-inl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,16 @@ inline void Environment::set_heap_space_statistics_buffer(double* pointer) {
569569
heap_space_statistics_buffer_ = pointer;
570570
}
571571

572+
inline double* Environment::heap_code_statistics_buffer() const {
573+
CHECK_NOT_NULL(heap_code_statistics_buffer_);
574+
return heap_code_statistics_buffer_;
575+
}
576+
577+
inline void Environment::set_heap_code_statistics_buffer(double* pointer) {
578+
CHECK_NULL(heap_code_statistics_buffer_); // Should be set only once.
579+
heap_code_statistics_buffer_ = pointer;
580+
}
581+
572582
inline char* Environment::http_parser_buffer() const {
573583
return http_parser_buffer_;
574584
}

src/env.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ Environment::~Environment() {
414414
delete[] heap_statistics_buffer_;
415415
delete[] heap_space_statistics_buffer_;
416416
delete[] http_parser_buffer_;
417+
delete[] heap_code_statistics_buffer_;
417418

418419
TRACE_EVENT_NESTABLE_ASYNC_END0(
419420
TRACING_CATEGORY_NODE1(environment), "Environment", this);

src/env.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ class Environment : public MemoryRetainer {
10011001
inline double* heap_space_statistics_buffer() const;
10021002
inline void set_heap_space_statistics_buffer(double* pointer);
10031003

1004+
inline double* heap_code_statistics_buffer() const;
1005+
inline void set_heap_code_statistics_buffer(double* pointer);
1006+
10041007
inline char* http_parser_buffer() const;
10051008
inline void set_http_parser_buffer(char* buffer);
10061009
inline bool http_parser_buffer_in_use() const;
@@ -1323,6 +1326,7 @@ class Environment : public MemoryRetainer {
13231326

13241327
double* heap_statistics_buffer_ = nullptr;
13251328
double* heap_space_statistics_buffer_ = nullptr;
1329+
double* heap_code_statistics_buffer_ = nullptr;
13261330

13271331
char* http_parser_buffer_ = nullptr;
13281332
bool http_parser_buffer_in_use_ = false;

src/node_v8.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using v8::Array;
3030
using v8::ArrayBuffer;
3131
using v8::Context;
3232
using v8::FunctionCallbackInfo;
33+
using v8::HeapCodeStatistics;
3334
using v8::HeapSpaceStatistics;
3435
using v8::HeapStatistics;
3536
using v8::Integer;
@@ -43,6 +44,7 @@ using v8::Uint32;
4344
using v8::V8;
4445
using v8::Value;
4546

47+
4648
#define HEAP_STATISTICS_PROPERTIES(V) \
4749
V(0, total_heap_size, kTotalHeapSizeIndex) \
4850
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \
@@ -61,6 +63,7 @@ static const size_t kHeapStatisticsPropertiesCount =
6163
HEAP_STATISTICS_PROPERTIES(V);
6264
#undef V
6365

66+
6467
#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \
6568
V(0, space_size, kSpaceSizeIndex) \
6669
V(1, space_used_size, kSpaceUsedSizeIndex) \
@@ -73,6 +76,16 @@ static const size_t kHeapSpaceStatisticsPropertiesCount =
7376
#undef V
7477

7578

79+
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
80+
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
81+
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \
82+
V(2, external_script_source_size, kExternalScriptSourceSizeIndex)
83+
84+
#define V(a, b, c) +1
85+
static const size_t kHeapCodeStatisticsPropertiesCount =
86+
HEAP_CODE_STATISTICS_PROPERTIES(V);
87+
#undef V
88+
7689
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
7790
Environment* env = Environment::GetCurrent(args);
7891
Local<Integer> result =
@@ -111,6 +124,18 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
111124
}
112125

113126

127+
void UpdateHeapCodeStatisticsArrayBuffer(
128+
const FunctionCallbackInfo<Value>& args) {
129+
Environment* env = Environment::GetCurrent(args);
130+
HeapCodeStatistics s;
131+
env->isolate()->GetHeapCodeAndMetadataStatistics(&s);
132+
double* const buffer = env->heap_code_statistics_buffer();
133+
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
134+
HEAP_CODE_STATISTICS_PROPERTIES(V)
135+
#undef V
136+
}
137+
138+
114139
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
115140
CHECK(args[0]->IsString());
116141
String::Utf8Value flags(args.GetIsolate(), args[0]);
@@ -127,6 +152,7 @@ void Initialize(Local<Object> target,
127152
env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
128153
CachedDataVersionTag);
129154

155+
// Export symbols used by v8.getHeapStatistics()
130156
env->SetMethod(target,
131157
"updateHeapStatisticsArrayBuffer",
132158
UpdateHeapStatisticsArrayBuffer);
@@ -151,6 +177,35 @@ void Initialize(Local<Object> target,
151177
HEAP_STATISTICS_PROPERTIES(V)
152178
#undef V
153179

180+
// Export symbols used by v8.getHeapCodeStatistics()
181+
env->SetMethod(target,
182+
"updateHeapCodeStatisticsArrayBuffer",
183+
UpdateHeapCodeStatisticsArrayBuffer);
184+
185+
env->set_heap_code_statistics_buffer(
186+
new double[kHeapCodeStatisticsPropertiesCount]);
187+
188+
const size_t heap_code_statistics_buffer_byte_length =
189+
sizeof(*env->heap_code_statistics_buffer())
190+
* kHeapCodeStatisticsPropertiesCount;
191+
192+
target->Set(env->context(),
193+
FIXED_ONE_BYTE_STRING(env->isolate(),
194+
"heapCodeStatisticsArrayBuffer"),
195+
ArrayBuffer::New(env->isolate(),
196+
env->heap_code_statistics_buffer(),
197+
heap_code_statistics_buffer_byte_length))
198+
.Check();
199+
200+
#define V(i, _, name) \
201+
target->Set(env->context(), \
202+
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
203+
Uint32::NewFromUnsigned(env->isolate(), i)).Check();
204+
205+
HEAP_CODE_STATISTICS_PROPERTIES(V)
206+
#undef V
207+
208+
// Export symbols used by v8.getHeapSpaceStatistics()
154209
target->Set(env->context(),
155210
FIXED_ONE_BYTE_STRING(env->isolate(),
156211
"kHeapSpaceStatisticsPropertiesCount"),
@@ -205,6 +260,7 @@ void Initialize(Local<Object> target,
205260
HEAP_SPACE_STATISTICS_PROPERTIES(V)
206261
#undef V
207262

263+
// Export symbols used by v8.setFlagsFromString()
208264
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
209265
}
210266

test/parallel/test-v8-stats.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ keys.forEach(function(key) {
2222
});
2323

2424

25+
const heapCodeStatistics = v8.getHeapCodeStatistics();
26+
const heapCodeStatisticsKeys = [
27+
'bytecode_and_metadata_size',
28+
'code_and_metadata_size',
29+
'external_script_source_size'];
30+
assert.deepStrictEqual(Object.keys(heapCodeStatistics).sort(),
31+
heapCodeStatisticsKeys);
32+
heapCodeStatisticsKeys.forEach(function(key) {
33+
assert.strictEqual(typeof heapCodeStatistics[key], 'number');
34+
});
35+
36+
2537
const expectedHeapSpaces = [
2638
'code_large_object_space',
2739
'code_space',

0 commit comments

Comments
 (0)