GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_v8.cc Lines: 103 103 100.0 %
Date: 2022-10-23 04:21:34 Branches: 11 20 55.0 %

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
  V(3, cpu_profiler_metadata_size, kCPUProfilerMetaDataSizeIndex)
87
88
#define V(a, b, c) +1
89
static const size_t kHeapCodeStatisticsPropertiesCount =
90
    HEAP_CODE_STATISTICS_PROPERTIES(V);
91
#undef V
92
93
6354
BindingData::BindingData(Environment* env, Local<Object> obj)
94
    : SnapshotableObject(env, obj, type_int),
95
      heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
96
      heap_space_statistics_buffer(env->isolate(),
97
                                   kHeapSpaceStatisticsPropertiesCount),
98
      heap_code_statistics_buffer(env->isolate(),
99
6354
                                  kHeapCodeStatisticsPropertiesCount) {
100
6354
  obj->Set(env->context(),
101
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
102
25416
           heap_statistics_buffer.GetJSArray())
103
      .Check();
104
6354
  obj->Set(env->context(),
105
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
106
25416
           heap_code_statistics_buffer.GetJSArray())
107
      .Check();
108
6354
  obj->Set(env->context(),
109
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsBuffer"),
110
19062
           heap_space_statistics_buffer.GetJSArray())
111
      .Check();
112
6354
}
113
114
7
bool BindingData::PrepareForSerialization(Local<Context> context,
115
                                          v8::SnapshotCreator* creator) {
116
  // We'll just re-initialize the buffers in the constructor since their
117
  // contents can be thrown away once consumed in the previous call.
118
7
  heap_statistics_buffer.Release();
119
7
  heap_space_statistics_buffer.Release();
120
7
  heap_code_statistics_buffer.Release();
121
  // Return true because we need to maintain the reference to the binding from
122
  // JS land.
123
7
  return true;
124
}
125
126
5566
void BindingData::Deserialize(Local<Context> context,
127
                              Local<Object> holder,
128
                              int index,
129
                              InternalFieldInfoBase* info) {
130
  DCHECK_EQ(index, BaseObject::kEmbedderType);
131
11132
  HandleScope scope(context->GetIsolate());
132
5566
  Environment* env = Environment::GetCurrent(context);
133
5566
  BindingData* binding = env->AddBindingData<BindingData>(context, holder);
134
5566
  CHECK_NOT_NULL(binding);
135
5566
}
136
137
7
InternalFieldInfoBase* BindingData::Serialize(int index) {
138
  DCHECK_EQ(index, BaseObject::kEmbedderType);
139
  InternalFieldInfo* info =
140
7
      InternalFieldInfoBase::New<InternalFieldInfo>(type());
141
7
  return info;
142
}
143
144
25
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
145
25
  tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
146
25
  tracker->TrackField("heap_space_statistics_buffer",
147
25
                      heap_space_statistics_buffer);
148
25
  tracker->TrackField("heap_code_statistics_buffer",
149
25
                      heap_code_statistics_buffer);
150
25
}
151
152
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
153
4
  Environment* env = Environment::GetCurrent(args);
154
  Local<Integer> result =
155
      Integer::NewFromUnsigned(env->isolate(),
156
4
                               ScriptCompiler::CachedDataVersionTag());
157
4
  args.GetReturnValue().Set(result);
158
4
}
159
160
1
void SetHeapSnapshotNearHeapLimit(const FunctionCallbackInfo<Value>& args) {
161
1
  CHECK(args[0]->IsUint32());
162
1
  Environment* env = Environment::GetCurrent(args);
163
2
  uint32_t limit = args[0].As<v8::Uint32>()->Value();
164
1
  CHECK_GT(limit, 0);
165
1
  env->AddHeapSnapshotNearHeapLimitCallback();
166
1
  env->set_heap_snapshot_near_heap_limit(limit);
167
1
}
168
169
1484
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
170
1484
  BindingData* data = Environment::GetBindingData<BindingData>(args);
171
1484
  HeapStatistics s;
172
1484
  args.GetIsolate()->GetHeapStatistics(&s);
173
1484
  AliasedFloat64Array& buffer = data->heap_statistics_buffer;
174
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
175
1484
  HEAP_STATISTICS_PROPERTIES(V)
176
#undef V
177
1484
}
178
179
180
11872
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
181
11872
  BindingData* data = Environment::GetBindingData<BindingData>(args);
182
11872
  HeapSpaceStatistics s;
183
11872
  Isolate* const isolate = args.GetIsolate();
184
11872
  CHECK(args[0]->IsUint32());
185
23744
  size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
186
11872
  isolate->GetHeapSpaceStatistics(&s, space_index);
187
188
11872
  AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;
189
190
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
191
11872
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
192
#undef V
193
11872
}
194
195
1
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
196
1
  BindingData* data = Environment::GetBindingData<BindingData>(args);
197
1
  HeapCodeStatistics s;
198
1
  args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
199
1
  AliasedFloat64Array& buffer = data->heap_code_statistics_buffer;
200
201
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
202
1
  HEAP_CODE_STATISTICS_PROPERTIES(V)
203
#undef V
204
1
}
205
206
207
8
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
208
16
  CHECK(args[0]->IsString());
209
16
  String::Utf8Value flags(args.GetIsolate(), args[0]);
210
8
  V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
211
8
}
212
213
788
void Initialize(Local<Object> target,
214
                Local<Value> unused,
215
                Local<Context> context,
216
                void* priv) {
217
788
  Environment* env = Environment::GetCurrent(context);
218
  BindingData* const binding_data =
219
788
      env->AddBindingData<BindingData>(context, target);
220
788
  if (binding_data == nullptr) return;
221
222
788
  SetMethodNoSideEffect(
223
      context, target, "cachedDataVersionTag", CachedDataVersionTag);
224
788
  SetMethodNoSideEffect(context,
225
                        target,
226
                        "setHeapSnapshotNearHeapLimit",
227
                        SetHeapSnapshotNearHeapLimit);
228
788
  SetMethod(context,
229
            target,
230
            "updateHeapStatisticsBuffer",
231
            UpdateHeapStatisticsBuffer);
232
233
788
  SetMethod(context,
234
            target,
235
            "updateHeapCodeStatisticsBuffer",
236
            UpdateHeapCodeStatisticsBuffer);
237
238
788
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
239
240
  // Heap space names are extracted once and exposed to JavaScript to
241
  // avoid excessive creation of heap space name Strings.
242
788
  HeapSpaceStatistics s;
243
1576
  MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces);
244
7092
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
245
6304
    env->isolate()->GetHeapSpaceStatistics(&s, i);
246
12608
    heap_spaces[i] = String::NewFromUtf8(env->isolate(), s.space_name())
247
                                             .ToLocalChecked();
248
  }
249
  target
250
788
      ->Set(
251
          context,
252
          FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
253
2364
          Array::New(env->isolate(), heap_spaces.out(), number_of_heap_spaces))
254
      .Check();
255
256
788
  SetMethod(context,
257
            target,
258
            "updateHeapSpaceStatisticsBuffer",
259
            UpdateHeapSpaceStatisticsBuffer);
260
261
#define V(i, _, name)                                                          \
262
  target                                                                       \
263
      ->Set(context,                                                           \
264
            FIXED_ONE_BYTE_STRING(env->isolate(), #name),                      \
265
            Uint32::NewFromUnsigned(env->isolate(), i))                        \
266
      .Check();
267
268
33884
  HEAP_STATISTICS_PROPERTIES(V)
269
10244
  HEAP_CODE_STATISTICS_PROPERTIES(V)
270
9456
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
271
#undef V
272
273
  // Export symbols used by v8.setFlagsFromString()
274
788
  SetMethod(context, target, "setFlagsFromString", SetFlagsFromString);
275
}
276
277
5574
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
278
5574
  registry->Register(CachedDataVersionTag);
279
5574
  registry->Register(UpdateHeapStatisticsBuffer);
280
5574
  registry->Register(UpdateHeapCodeStatisticsBuffer);
281
5574
  registry->Register(UpdateHeapSpaceStatisticsBuffer);
282
5574
  registry->Register(SetFlagsFromString);
283
5574
  registry->Register(SetHeapSnapshotNearHeapLimit);
284
5574
}
285
286
}  // namespace v8_utils
287
}  // namespace node
288
289
5645
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize)
290
5574
NODE_MODULE_EXTERNAL_REFERENCE(v8, node::v8_utils::RegisterExternalReferences)