GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_v8.cc Lines: 93 93 100.0 %
Date: 2022-04-25 04:18:27 Branches: 8 14 57.1 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "node_v8.h"
23
#include "base_object-inl.h"
24
#include "env-inl.h"
25
#include "memory_tracker-inl.h"
26
#include "node.h"
27
#include "node_external_reference.h"
28
#include "util-inl.h"
29
#include "v8.h"
30
31
namespace node {
32
namespace v8_utils {
33
using v8::Array;
34
using v8::Context;
35
using v8::FunctionCallbackInfo;
36
using v8::HandleScope;
37
using v8::HeapCodeStatistics;
38
using v8::HeapSpaceStatistics;
39
using v8::HeapStatistics;
40
using v8::Integer;
41
using v8::Isolate;
42
using v8::Local;
43
using v8::Object;
44
using v8::ScriptCompiler;
45
using v8::String;
46
using v8::Uint32;
47
using v8::V8;
48
using v8::Value;
49
50
#define HEAP_STATISTICS_PROPERTIES(V)                                          \
51
  V(0, total_heap_size, kTotalHeapSizeIndex)                                   \
52
  V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex)              \
53
  V(2, total_physical_size, kTotalPhysicalSizeIndex)                           \
54
  V(3, total_available_size, kTotalAvailableSize)                              \
55
  V(4, used_heap_size, kUsedHeapSizeIndex)                                     \
56
  V(5, heap_size_limit, kHeapSizeLimitIndex)                                   \
57
  V(6, malloced_memory, kMallocedMemoryIndex)                                  \
58
  V(7, peak_malloced_memory, kPeakMallocedMemoryIndex)                         \
59
  V(8, does_zap_garbage, kDoesZapGarbageIndex)                                 \
60
  V(9, number_of_native_contexts, kNumberOfNativeContextsIndex)                \
61
  V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex)           \
62
  V(11, total_global_handles_size, kTotalGlobalHandlesSizeIndex)               \
63
  V(12, used_global_handles_size, kUsedGlobalHandlesSizeIndex)                 \
64
  V(13, external_memory, kExternalMemoryIndex)
65
66
#define V(a, b, c) +1
67
static constexpr size_t kHeapStatisticsPropertiesCount =
68
    HEAP_STATISTICS_PROPERTIES(V);
69
#undef V
70
71
#define HEAP_SPACE_STATISTICS_PROPERTIES(V)                                   \
72
  V(0, space_size, kSpaceSizeIndex)                                           \
73
  V(1, space_used_size, kSpaceUsedSizeIndex)                                  \
74
  V(2, space_available_size, kSpaceAvailableSizeIndex)                        \
75
  V(3, physical_space_size, kPhysicalSpaceSizeIndex)
76
77
#define V(a, b, c) +1
78
static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
79
    HEAP_SPACE_STATISTICS_PROPERTIES(V);
80
#undef V
81
82
#define HEAP_CODE_STATISTICS_PROPERTIES(V)                                     \
83
  V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex)                      \
84
  V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex)              \
85
  V(2, external_script_source_size, kExternalScriptSourceSizeIndex)
86
87
#define V(a, b, c) +1
88
static const size_t kHeapCodeStatisticsPropertiesCount =
89
    HEAP_CODE_STATISTICS_PROPERTIES(V);
90
#undef V
91
92
5647
BindingData::BindingData(Environment* env, Local<Object> obj)
93
    : SnapshotableObject(env, obj, type_int),
94
      heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
95
      heap_space_statistics_buffer(env->isolate(),
96
                                   kHeapSpaceStatisticsPropertiesCount),
97
      heap_code_statistics_buffer(env->isolate(),
98
5647
                                  kHeapCodeStatisticsPropertiesCount) {
99
5647
  obj->Set(env->context(),
100
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
101
22588
           heap_statistics_buffer.GetJSArray())
102
      .Check();
103
5647
  obj->Set(env->context(),
104
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
105
22588
           heap_code_statistics_buffer.GetJSArray())
106
      .Check();
107
5647
  obj->Set(env->context(),
108
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsBuffer"),
109
16941
           heap_space_statistics_buffer.GetJSArray())
110
      .Check();
111
5647
}
112
113
6
void BindingData::PrepareForSerialization(Local<Context> context,
114
                                          v8::SnapshotCreator* creator) {
115
  // We'll just re-initialize the buffers in the constructor since their
116
  // contents can be thrown away once consumed in the previous call.
117
6
  heap_statistics_buffer.Release();
118
6
  heap_space_statistics_buffer.Release();
119
6
  heap_code_statistics_buffer.Release();
120
6
}
121
122
5008
void BindingData::Deserialize(Local<Context> context,
123
                              Local<Object> holder,
124
                              int index,
125
                              InternalFieldInfo* info) {
126
  DCHECK_EQ(index, BaseObject::kSlot);
127
10016
  HandleScope scope(context->GetIsolate());
128
5008
  Environment* env = Environment::GetCurrent(context);
129
5008
  BindingData* binding = env->AddBindingData<BindingData>(context, holder);
130
5008
  CHECK_NOT_NULL(binding);
131
5008
}
132
133
6
InternalFieldInfo* BindingData::Serialize(int index) {
134
  DCHECK_EQ(index, BaseObject::kSlot);
135
6
  InternalFieldInfo* info = InternalFieldInfo::New(type());
136
6
  return info;
137
}
138
139
23
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
140
23
  tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
141
23
  tracker->TrackField("heap_space_statistics_buffer",
142
23
                      heap_space_statistics_buffer);
143
23
  tracker->TrackField("heap_code_statistics_buffer",
144
23
                      heap_code_statistics_buffer);
145
23
}
146
147
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
148
4
  Environment* env = Environment::GetCurrent(args);
149
  Local<Integer> result =
150
      Integer::NewFromUnsigned(env->isolate(),
151
4
                               ScriptCompiler::CachedDataVersionTag());
152
4
  args.GetReturnValue().Set(result);
153
4
}
154
155
1588
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
156
1588
  BindingData* data = Environment::GetBindingData<BindingData>(args);
157
1588
  HeapStatistics s;
158
1588
  args.GetIsolate()->GetHeapStatistics(&s);
159
1588
  AliasedFloat64Array& buffer = data->heap_statistics_buffer;
160
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
161
1588
  HEAP_STATISTICS_PROPERTIES(V)
162
#undef V
163
1588
}
164
165
166
12704
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
167
12704
  BindingData* data = Environment::GetBindingData<BindingData>(args);
168
12704
  HeapSpaceStatistics s;
169
12704
  Isolate* const isolate = args.GetIsolate();
170
12704
  CHECK(args[0]->IsUint32());
171
25408
  size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
172
12704
  isolate->GetHeapSpaceStatistics(&s, space_index);
173
174
12704
  AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;
175
176
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
177
12704
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
178
#undef V
179
12704
}
180
181
1
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
182
1
  BindingData* data = Environment::GetBindingData<BindingData>(args);
183
1
  HeapCodeStatistics s;
184
1
  args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
185
1
  AliasedFloat64Array& buffer = data->heap_code_statistics_buffer;
186
187
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
188
1
  HEAP_CODE_STATISTICS_PROPERTIES(V)
189
#undef V
190
1
}
191
192
193
8
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
194
16
  CHECK(args[0]->IsString());
195
16
  String::Utf8Value flags(args.GetIsolate(), args[0]);
196
8
  V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
197
8
}
198
199
639
void Initialize(Local<Object> target,
200
                Local<Value> unused,
201
                Local<Context> context,
202
                void* priv) {
203
639
  Environment* env = Environment::GetCurrent(context);
204
  BindingData* const binding_data =
205
639
      env->AddBindingData<BindingData>(context, target);
206
639
  if (binding_data == nullptr) return;
207
208
639
  env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
209
                             CachedDataVersionTag);
210
639
  env->SetMethod(
211
      target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer);
212
213
639
  env->SetMethod(
214
      target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer);
215
216
639
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
217
218
  // Heap space names are extracted once and exposed to JavaScript to
219
  // avoid excessive creation of heap space name Strings.
220
639
  HeapSpaceStatistics s;
221
1278
  MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces);
222
5751
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
223
5112
    env->isolate()->GetHeapSpaceStatistics(&s, i);
224
10224
    heap_spaces[i] = String::NewFromUtf8(env->isolate(), s.space_name())
225
                                             .ToLocalChecked();
226
  }
227
639
  target->Set(env->context(),
228
              FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
229
              Array::New(env->isolate(),
230
                         heap_spaces.out(),
231
1917
                         number_of_heap_spaces)).Check();
232
233
639
  env->SetMethod(target,
234
                 "updateHeapSpaceStatisticsBuffer",
235
                 UpdateHeapSpaceStatisticsBuffer);
236
237
#define V(i, _, name)                                                          \
238
  target                                                                       \
239
      ->Set(env->context(),                                                    \
240
            FIXED_ONE_BYTE_STRING(env->isolate(), #name),                      \
241
            Uint32::NewFromUnsigned(env->isolate(), i))                        \
242
      .Check();
243
244
27477
  HEAP_STATISTICS_PROPERTIES(V)
245
6390
  HEAP_CODE_STATISTICS_PROPERTIES(V)
246
7668
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
247
#undef V
248
249
  // Export symbols used by v8.setFlagsFromString()
250
639
  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
251
}
252
253
5014
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
254
5014
  registry->Register(CachedDataVersionTag);
255
5014
  registry->Register(UpdateHeapStatisticsBuffer);
256
5014
  registry->Register(UpdateHeapCodeStatisticsBuffer);
257
5014
  registry->Register(UpdateHeapSpaceStatisticsBuffer);
258
5014
  registry->Register(SetFlagsFromString);
259
5014
}
260
261
}  // namespace v8_utils
262
}  // namespace node
263
264
5082
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize)
265
5014
NODE_MODULE_EXTERNAL_REFERENCE(v8, node::v8_utils::RegisterExternalReferences)