GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_v8.cc Lines: 93 93 100.0 %
Date: 2022-08-12 04:19:25 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
  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
6101
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
6101
                                  kHeapCodeStatisticsPropertiesCount) {
100
6101
  obj->Set(env->context(),
101
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
102
24404
           heap_statistics_buffer.GetJSArray())
103
      .Check();
104
6101
  obj->Set(env->context(),
105
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
106
24404
           heap_code_statistics_buffer.GetJSArray())
107
      .Check();
108
6101
  obj->Set(env->context(),
109
           FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsBuffer"),
110
18303
           heap_space_statistics_buffer.GetJSArray())
111
      .Check();
112
6101
}
113
114
6
void 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
6
  heap_statistics_buffer.Release();
119
6
  heap_space_statistics_buffer.Release();
120
6
  heap_code_statistics_buffer.Release();
121
6
}
122
123
5330
void BindingData::Deserialize(Local<Context> context,
124
                              Local<Object> holder,
125
                              int index,
126
                              InternalFieldInfo* info) {
127
  DCHECK_EQ(index, BaseObject::kEmbedderType);
128
10660
  HandleScope scope(context->GetIsolate());
129
5330
  Environment* env = Environment::GetCurrent(context);
130
5330
  BindingData* binding = env->AddBindingData<BindingData>(context, holder);
131
5330
  CHECK_NOT_NULL(binding);
132
5330
}
133
134
6
InternalFieldInfo* BindingData::Serialize(int index) {
135
  DCHECK_EQ(index, BaseObject::kEmbedderType);
136
6
  InternalFieldInfo* info = InternalFieldInfo::New(type());
137
6
  return info;
138
}
139
140
24
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
141
24
  tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
142
24
  tracker->TrackField("heap_space_statistics_buffer",
143
24
                      heap_space_statistics_buffer);
144
24
  tracker->TrackField("heap_code_statistics_buffer",
145
24
                      heap_code_statistics_buffer);
146
24
}
147
148
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
149
4
  Environment* env = Environment::GetCurrent(args);
150
  Local<Integer> result =
151
      Integer::NewFromUnsigned(env->isolate(),
152
4
                               ScriptCompiler::CachedDataVersionTag());
153
4
  args.GetReturnValue().Set(result);
154
4
}
155
156
1518
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
157
1518
  BindingData* data = Environment::GetBindingData<BindingData>(args);
158
1518
  HeapStatistics s;
159
1518
  args.GetIsolate()->GetHeapStatistics(&s);
160
1518
  AliasedFloat64Array& buffer = data->heap_statistics_buffer;
161
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
162
1518
  HEAP_STATISTICS_PROPERTIES(V)
163
#undef V
164
1518
}
165
166
167
12144
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
168
12144
  BindingData* data = Environment::GetBindingData<BindingData>(args);
169
12144
  HeapSpaceStatistics s;
170
12144
  Isolate* const isolate = args.GetIsolate();
171
12144
  CHECK(args[0]->IsUint32());
172
24288
  size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
173
12144
  isolate->GetHeapSpaceStatistics(&s, space_index);
174
175
12144
  AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;
176
177
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
178
12144
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
179
#undef V
180
12144
}
181
182
1
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
183
1
  BindingData* data = Environment::GetBindingData<BindingData>(args);
184
1
  HeapCodeStatistics s;
185
1
  args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
186
1
  AliasedFloat64Array& buffer = data->heap_code_statistics_buffer;
187
188
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
189
1
  HEAP_CODE_STATISTICS_PROPERTIES(V)
190
#undef V
191
1
}
192
193
194
8
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
195
16
  CHECK(args[0]->IsString());
196
16
  String::Utf8Value flags(args.GetIsolate(), args[0]);
197
8
  V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
198
8
}
199
200
771
void Initialize(Local<Object> target,
201
                Local<Value> unused,
202
                Local<Context> context,
203
                void* priv) {
204
771
  Environment* env = Environment::GetCurrent(context);
205
  BindingData* const binding_data =
206
771
      env->AddBindingData<BindingData>(context, target);
207
771
  if (binding_data == nullptr) return;
208
209
771
  SetMethodNoSideEffect(
210
      context, target, "cachedDataVersionTag", CachedDataVersionTag);
211
771
  SetMethod(context,
212
            target,
213
            "updateHeapStatisticsBuffer",
214
            UpdateHeapStatisticsBuffer);
215
216
771
  SetMethod(context,
217
            target,
218
            "updateHeapCodeStatisticsBuffer",
219
            UpdateHeapCodeStatisticsBuffer);
220
221
771
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
222
223
  // Heap space names are extracted once and exposed to JavaScript to
224
  // avoid excessive creation of heap space name Strings.
225
771
  HeapSpaceStatistics s;
226
1542
  MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces);
227
6939
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
228
6168
    env->isolate()->GetHeapSpaceStatistics(&s, i);
229
12336
    heap_spaces[i] = String::NewFromUtf8(env->isolate(), s.space_name())
230
                                             .ToLocalChecked();
231
  }
232
  target
233
771
      ->Set(
234
          context,
235
          FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
236
2313
          Array::New(env->isolate(), heap_spaces.out(), number_of_heap_spaces))
237
      .Check();
238
239
771
  SetMethod(context,
240
            target,
241
            "updateHeapSpaceStatisticsBuffer",
242
            UpdateHeapSpaceStatisticsBuffer);
243
244
#define V(i, _, name)                                                          \
245
  target                                                                       \
246
      ->Set(context,                                                           \
247
            FIXED_ONE_BYTE_STRING(env->isolate(), #name),                      \
248
            Uint32::NewFromUnsigned(env->isolate(), i))                        \
249
      .Check();
250
251
33153
  HEAP_STATISTICS_PROPERTIES(V)
252
10023
  HEAP_CODE_STATISTICS_PROPERTIES(V)
253
9252
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
254
#undef V
255
256
  // Export symbols used by v8.setFlagsFromString()
257
771
  SetMethod(context, target, "setFlagsFromString", SetFlagsFromString);
258
}
259
260
5337
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
261
5337
  registry->Register(CachedDataVersionTag);
262
5337
  registry->Register(UpdateHeapStatisticsBuffer);
263
5337
  registry->Register(UpdateHeapCodeStatisticsBuffer);
264
5337
  registry->Register(UpdateHeapSpaceStatisticsBuffer);
265
5337
  registry->Register(SetFlagsFromString);
266
5337
}
267
268
}  // namespace v8_utils
269
}  // namespace node
270
271
5409
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize)
272
5337
NODE_MODULE_EXTERNAL_REFERENCE(v8, node::v8_utils::RegisterExternalReferences)