Skip to content

Commit 9f0f5f9

Browse files
committed
src: move v8 stats buffers out of Environment
Moves state that is specific to the `v8` binding into the `v8` binding implementation as a cleanup.
1 parent 824f84e commit 9f0f5f9

3 files changed

Lines changed: 54 additions & 78 deletions

File tree

src/env-inl.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -580,39 +580,6 @@ inline double Environment::get_default_trigger_async_id() {
580580
return default_trigger_async_id;
581581
}
582582

583-
inline double* Environment::heap_statistics_buffer() const {
584-
CHECK_NOT_NULL(heap_statistics_buffer_);
585-
return static_cast<double*>(heap_statistics_buffer_->Data());
586-
}
587-
588-
inline void Environment::set_heap_statistics_buffer(
589-
std::shared_ptr<v8::BackingStore> backing_store) {
590-
CHECK(!heap_statistics_buffer_); // Should be set only once.
591-
heap_statistics_buffer_ = std::move(backing_store);
592-
}
593-
594-
inline double* Environment::heap_space_statistics_buffer() const {
595-
CHECK(heap_space_statistics_buffer_);
596-
return static_cast<double*>(heap_space_statistics_buffer_->Data());
597-
}
598-
599-
inline void Environment::set_heap_space_statistics_buffer(
600-
std::shared_ptr<v8::BackingStore> backing_store) {
601-
CHECK(!heap_space_statistics_buffer_); // Should be set only once.
602-
heap_space_statistics_buffer_ = std::move(backing_store);
603-
}
604-
605-
inline double* Environment::heap_code_statistics_buffer() const {
606-
CHECK(heap_code_statistics_buffer_);
607-
return static_cast<double*>(heap_code_statistics_buffer_->Data());
608-
}
609-
610-
inline void Environment::set_heap_code_statistics_buffer(
611-
std::shared_ptr<v8::BackingStore> backing_store) {
612-
CHECK(!heap_code_statistics_buffer_); // Should be set only once.
613-
heap_code_statistics_buffer_ = std::move(backing_store);
614-
}
615-
616583
inline char* Environment::http_parser_buffer() const {
617584
return http_parser_buffer_;
618585
}

src/env.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,18 +1004,6 @@ class Environment : public MemoryRetainer {
10041004
inline uint32_t get_next_script_id();
10051005
inline uint32_t get_next_function_id();
10061006

1007-
inline double* heap_statistics_buffer() const;
1008-
inline void set_heap_statistics_buffer(
1009-
std::shared_ptr<v8::BackingStore> backing_store);
1010-
1011-
inline double* heap_space_statistics_buffer() const;
1012-
inline void set_heap_space_statistics_buffer(
1013-
std::shared_ptr<v8::BackingStore> backing_store);
1014-
1015-
inline double* heap_code_statistics_buffer() const;
1016-
inline void set_heap_code_statistics_buffer(
1017-
std::shared_ptr<v8::BackingStore> backing_store);
1018-
10191007
inline char* http_parser_buffer() const;
10201008
inline void set_http_parser_buffer(char* buffer);
10211009
inline bool http_parser_buffer_in_use() const;
@@ -1377,14 +1365,10 @@ class Environment : public MemoryRetainer {
13771365
int handle_cleanup_waiting_ = 0;
13781366
int request_waiting_ = 0;
13791367

1380-
std::shared_ptr<v8::BackingStore> heap_statistics_buffer_;
1381-
std::shared_ptr<v8::BackingStore> heap_space_statistics_buffer_;
1382-
std::shared_ptr<v8::BackingStore> heap_code_statistics_buffer_;
1383-
13841368
char* http_parser_buffer_ = nullptr;
13851369
bool http_parser_buffer_in_use_ = false;
1386-
13871370
EnabledDebugList enabled_debug_list_;
1371+
13881372
AliasedFloat64Array fs_stats_field_array_;
13891373
AliasedBigUint64Array fs_stats_field_bigint_array_;
13901374

src/node_v8.cc

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
#include "node.h"
23+
#include "base_object-inl.h"
2324
#include "env-inl.h"
25+
#include "memory_tracker-inl.h"
2426
#include "util-inl.h"
2527
#include "v8.h"
2628

@@ -59,7 +61,7 @@ using v8::Value;
5961
V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex)
6062

6163
#define V(a, b, c) +1
62-
static const size_t kHeapStatisticsPropertiesCount =
64+
static constexpr size_t kHeapStatisticsPropertiesCount =
6365
HEAP_STATISTICS_PROPERTIES(V);
6466
#undef V
6567

@@ -71,10 +73,28 @@ static const size_t kHeapStatisticsPropertiesCount =
7173
V(3, physical_space_size, kPhysicalSpaceSizeIndex)
7274

7375
#define V(a, b, c) +1
74-
static const size_t kHeapSpaceStatisticsPropertiesCount =
76+
static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
7577
HEAP_SPACE_STATISTICS_PROPERTIES(V);
7678
#undef V
7779

80+
class BindingData : public BaseObject {
81+
public:
82+
BindingData(Environment* env, Local<Object> obj) : BaseObject(env, obj) {}
83+
84+
std::shared_ptr<BackingStore> heap_statistics_buffer;
85+
std::shared_ptr<BackingStore> heap_space_statistics_buffer;
86+
std::shared_ptr<BackingStore> heap_code_statistics_buffer;
87+
88+
void MemoryInfo(MemoryTracker* tracker) const override {
89+
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
90+
tracker->TrackField("heap_space_statistics_buffer",
91+
heap_space_statistics_buffer);
92+
tracker->TrackField("heap_code_statistics_buffer",
93+
heap_code_statistics_buffer);
94+
}
95+
SET_SELF_SIZE(BindingData)
96+
SET_MEMORY_INFO_NAME(BindingData)
97+
};
7898

7999
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
80100
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
@@ -96,40 +116,44 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
96116

97117

98118
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
99-
Environment* env = Environment::GetCurrent(args);
119+
BindingData* data = Unwrap<BindingData>(args.Data());
100120
HeapStatistics s;
101-
env->isolate()->GetHeapStatistics(&s);
102-
double* const buffer = env->heap_statistics_buffer();
121+
args.GetIsolate()->GetHeapStatistics(&s);
122+
double* const buffer =
123+
static_cast<double*>(data->heap_statistics_buffer->Data());
103124
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
104125
HEAP_STATISTICS_PROPERTIES(V)
105126
#undef V
106127
}
107128

108129

109130
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
110-
Environment* env = Environment::GetCurrent(args);
131+
BindingData* data = Unwrap<BindingData>(args.Data());
111132
HeapSpaceStatistics s;
112-
Isolate* const isolate = env->isolate();
113-
double* buffer = env->heap_space_statistics_buffer();
114-
size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
133+
Isolate* const isolate = args.GetIsolate();
134+
size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces();
135+
136+
double* const buffer =
137+
static_cast<double*>(data->heap_space_statistics_buffer->Data());
115138

116139
for (size_t i = 0; i < number_of_heap_spaces; i++) {
117140
isolate->GetHeapSpaceStatistics(&s, i);
118141
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
119-
#define V(index, name, _) buffer[property_offset + index] = \
120-
static_cast<double>(s.name());
121-
HEAP_SPACE_STATISTICS_PROPERTIES(V)
142+
#define V(index, name, _) \
143+
buffer[property_offset + index] = static_cast<double>(s.name());
144+
HEAP_SPACE_STATISTICS_PROPERTIES(V)
122145
#undef V
123146
}
124147
}
125148

126149

127150
void UpdateHeapCodeStatisticsArrayBuffer(
128151
const FunctionCallbackInfo<Value>& args) {
129-
Environment* env = Environment::GetCurrent(args);
152+
BindingData* data = Unwrap<BindingData>(args.Data());
130153
HeapCodeStatistics s;
131-
env->isolate()->GetHeapCodeAndMetadataStatistics(&s);
132-
double* const buffer = env->heap_code_statistics_buffer();
154+
args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
155+
double* const buffer =
156+
static_cast<double*>(data->heap_code_statistics_buffer->Data());
133157
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
134158
HEAP_CODE_STATISTICS_PROPERTIES(V)
135159
#undef V
@@ -148,6 +172,9 @@ void Initialize(Local<Object> target,
148172
Local<Context> context,
149173
void* priv) {
150174
Environment* env = Environment::GetCurrent(context);
175+
Environment::BindingScope<BindingData> binding_scope(env);
176+
BindingData* binding_data = binding_scope.data;
177+
if (binding_data == nullptr) return;
151178

152179
env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
153180
CachedDataVersionTag);
@@ -158,11 +185,11 @@ void Initialize(Local<Object> target,
158185
UpdateHeapStatisticsArrayBuffer);
159186

160187
const size_t heap_statistics_buffer_byte_length =
161-
sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
188+
sizeof(double) * kHeapStatisticsPropertiesCount;
162189

163190
Local<ArrayBuffer> heap_statistics_ab =
164191
ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length);
165-
env->set_heap_statistics_buffer(heap_statistics_ab->GetBackingStore());
192+
binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore();
166193
target->Set(env->context(),
167194
FIXED_ONE_BYTE_STRING(env->isolate(),
168195
"heapStatisticsArrayBuffer"),
@@ -182,19 +209,17 @@ void Initialize(Local<Object> target,
182209
UpdateHeapCodeStatisticsArrayBuffer);
183210

184211
const size_t heap_code_statistics_buffer_byte_length =
185-
sizeof(*env->heap_code_statistics_buffer())
186-
* kHeapCodeStatisticsPropertiesCount;
212+
sizeof(double) * kHeapCodeStatisticsPropertiesCount;
187213

188214
Local<ArrayBuffer> heap_code_statistics_ab =
189215
ArrayBuffer::New(env->isolate(),
190216
heap_code_statistics_buffer_byte_length);
191-
env->set_heap_code_statistics_buffer(
192-
heap_code_statistics_ab->GetBackingStore());
217+
binding_data->heap_code_statistics_buffer =
218+
heap_code_statistics_ab->GetBackingStore();
193219
target->Set(env->context(),
194220
FIXED_ONE_BYTE_STRING(env->isolate(),
195221
"heapCodeStatisticsArrayBuffer"),
196-
heap_code_statistics_ab)
197-
.Check();
222+
heap_code_statistics_ab).Check();
198223

199224
#define V(i, _, name) \
200225
target->Set(env->context(), \
@@ -236,20 +261,20 @@ void Initialize(Local<Object> target,
236261
UpdateHeapSpaceStatisticsBuffer);
237262

238263
const size_t heap_space_statistics_buffer_byte_length =
239-
sizeof(*env->heap_space_statistics_buffer()) *
264+
sizeof(double) *
240265
kHeapSpaceStatisticsPropertiesCount *
241266
number_of_heap_spaces;
242267

243268
Local<ArrayBuffer> heap_space_statistics_ab =
244269
ArrayBuffer::New(env->isolate(),
245270
heap_space_statistics_buffer_byte_length);
246-
env->set_heap_space_statistics_buffer(
247-
heap_space_statistics_ab->GetBackingStore());
271+
binding_data->heap_space_statistics_buffer =
272+
heap_space_statistics_ab->GetBackingStore();
273+
248274
target->Set(env->context(),
249275
FIXED_ONE_BYTE_STRING(env->isolate(),
250276
"heapSpaceStatisticsArrayBuffer"),
251-
heap_space_statistics_ab)
252-
.Check();
277+
heap_space_statistics_ab).Check();
253278

254279
#define V(i, _, name) \
255280
target->Set(env->context(), \

0 commit comments

Comments
 (0)