GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_v8.cc Lines: 76 76 100.0 %
Date: 2020-06-24 22:13:30 Branches: 5 8 62.5 %

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.h"
23
#include "base_object-inl.h"
24
#include "env-inl.h"
25
#include "memory_tracker-inl.h"
26
#include "util-inl.h"
27
#include "v8.h"
28
29
namespace node {
30
31
using v8::Array;
32
using v8::Context;
33
using v8::FunctionCallbackInfo;
34
using v8::HeapCodeStatistics;
35
using v8::HeapSpaceStatistics;
36
using v8::HeapStatistics;
37
using v8::Integer;
38
using v8::Isolate;
39
using v8::Local;
40
using v8::NewStringType;
41
using v8::Object;
42
using v8::ScriptCompiler;
43
using v8::String;
44
using v8::Uint32;
45
using v8::V8;
46
using v8::Value;
47
48
49
#define HEAP_STATISTICS_PROPERTIES(V)                                         \
50
  V(0, total_heap_size, kTotalHeapSizeIndex)                                  \
51
  V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex)             \
52
  V(2, total_physical_size, kTotalPhysicalSizeIndex)                          \
53
  V(3, total_available_size, kTotalAvailableSize)                             \
54
  V(4, used_heap_size, kUsedHeapSizeIndex)                                    \
55
  V(5, heap_size_limit, kHeapSizeLimitIndex)                                  \
56
  V(6, malloced_memory, kMallocedMemoryIndex)                                 \
57
  V(7, peak_malloced_memory, kPeakMallocedMemoryIndex)                        \
58
  V(8, does_zap_garbage, kDoesZapGarbageIndex)                                \
59
  V(9, number_of_native_contexts, kNumberOfNativeContextsIndex)               \
60
  V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex)
61
62
#define V(a, b, c) +1
63
static constexpr size_t kHeapStatisticsPropertiesCount =
64
    HEAP_STATISTICS_PROPERTIES(V);
65
#undef V
66
67
68
#define HEAP_SPACE_STATISTICS_PROPERTIES(V)                                   \
69
  V(0, space_size, kSpaceSizeIndex)                                           \
70
  V(1, space_used_size, kSpaceUsedSizeIndex)                                  \
71
  V(2, space_available_size, kSpaceAvailableSizeIndex)                        \
72
  V(3, physical_space_size, kPhysicalSpaceSizeIndex)
73
74
#define V(a, b, c) +1
75
static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
76
    HEAP_SPACE_STATISTICS_PROPERTIES(V);
77
#undef V
78
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
89
846
class BindingData : public BaseObject {
90
 public:
91
525
  BindingData(Environment* env, Local<Object> obj)
92
525
      : BaseObject(env, obj),
93
        heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
94
        heap_space_statistics_buffer(env->isolate(),
95
                                     kHeapSpaceStatisticsPropertiesCount),
96
        heap_code_statistics_buffer(env->isolate(),
97
525
                                    kHeapCodeStatisticsPropertiesCount) {}
98
99
  static constexpr FastStringKey binding_data_name { "v8" };
100
101
  AliasedFloat64Array heap_statistics_buffer;
102
  AliasedFloat64Array heap_space_statistics_buffer;
103
  AliasedFloat64Array heap_code_statistics_buffer;
104
105
16
  void MemoryInfo(MemoryTracker* tracker) const override {
106
16
    tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
107
16
    tracker->TrackField("heap_space_statistics_buffer",
108
16
                        heap_space_statistics_buffer);
109
16
    tracker->TrackField("heap_code_statistics_buffer",
110
16
                        heap_code_statistics_buffer);
111
16
  }
112
16
  SET_SELF_SIZE(BindingData)
113
16
  SET_MEMORY_INFO_NAME(BindingData)
114
};
115
116
// TODO(addaleax): Remove once we're on C++17.
117
constexpr FastStringKey BindingData::binding_data_name;
118
119
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
120
4
  Environment* env = Environment::GetCurrent(args);
121
  Local<Integer> result =
122
      Integer::NewFromUnsigned(env->isolate(),
123
4
                               ScriptCompiler::CachedDataVersionTag());
124
8
  args.GetReturnValue().Set(result);
125
4
}
126
127
1568
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
128
1568
  BindingData* data = Environment::GetBindingData<BindingData>(args);
129
1568
  HeapStatistics s;
130
1568
  args.GetIsolate()->GetHeapStatistics(&s);
131
1568
  AliasedFloat64Array& buffer = data->heap_statistics_buffer;
132
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
133
1568
  HEAP_STATISTICS_PROPERTIES(V)
134
#undef V
135
1568
}
136
137
138
12544
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
139
12544
  BindingData* data = Environment::GetBindingData<BindingData>(args);
140
12544
  HeapSpaceStatistics s;
141
12544
  Isolate* const isolate = args.GetIsolate();
142
25088
  CHECK(args[0]->IsUint32());
143
37632
  size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
144
12544
  isolate->GetHeapSpaceStatistics(&s, space_index);
145
146
12544
  AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;
147
148
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
149
12544
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
150
#undef V
151
12544
}
152
153
1
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
154
1
  BindingData* data = Environment::GetBindingData<BindingData>(args);
155
1
  HeapCodeStatistics s;
156
1
  args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
157
1
  AliasedFloat64Array& buffer = data->heap_code_statistics_buffer;
158
159
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
160
1
  HEAP_CODE_STATISTICS_PROPERTIES(V)
161
#undef V
162
1
}
163
164
165
8
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
166
24
  CHECK(args[0]->IsString());
167
16
  String::Utf8Value flags(args.GetIsolate(), args[0]);
168
8
  V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
169
8
}
170
171
172
525
void Initialize(Local<Object> target,
173
                Local<Value> unused,
174
                Local<Context> context,
175
                void* priv) {
176
525
  Environment* env = Environment::GetCurrent(context);
177
  BindingData* const binding_data =
178
525
      env->AddBindingData<BindingData>(context, target);
179
525
  if (binding_data == nullptr) return;
180
181
  env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
182
525
                             CachedDataVersionTag);
183
184
  // Export symbols used by v8.getHeapStatistics()
185
  env->SetMethod(
186
525
      target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer);
187
188
  target
189
1050
      ->Set(env->context(),
190
            FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
191
1575
            binding_data->heap_statistics_buffer.GetJSArray())
192
      .Check();
193
194
#define V(i, _, name)                                                         \
195
  target->Set(env->context(),                                                 \
196
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
197
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
198
199
23625
  HEAP_STATISTICS_PROPERTIES(V)
200
#undef V
201
202
  // Export symbols used by v8.getHeapCodeStatistics()
203
  env->SetMethod(
204
525
      target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer);
205
206
  target
207
1050
      ->Set(env->context(),
208
            FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
209
2625
            binding_data->heap_code_statistics_buffer.GetJSArray())
210
      .Check();
211
212
#define V(i, _, name)                                                         \
213
  target->Set(env->context(),                                                 \
214
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
215
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
216
217
6825
  HEAP_CODE_STATISTICS_PROPERTIES(V)
218
#undef V
219
220
525
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
221
222
  // Heap space names are extracted once and exposed to JavaScript to
223
  // avoid excessive creation of heap space name Strings.
224
525
  HeapSpaceStatistics s;
225
1050
  MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces);
226
4725
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
227
4200
    env->isolate()->GetHeapSpaceStatistics(&s, i);
228
16800
    heap_spaces[i] = String::NewFromUtf8(env->isolate(),
229
                                         s.space_name(),
230
4200
                                         NewStringType::kNormal)
231
4200
                                             .ToLocalChecked();
232
  }
233
1050
  target->Set(env->context(),
234
              FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
235
              Array::New(env->isolate(),
236
                         heap_spaces.out(),
237
2625
                         number_of_heap_spaces)).Check();
238
239
  env->SetMethod(target,
240
                 "updateHeapSpaceStatisticsBuffer",
241
525
                 UpdateHeapSpaceStatisticsBuffer);
242
243
  target
244
1050
      ->Set(env->context(),
245
            FIXED_ONE_BYTE_STRING(env->isolate(),
246
                                  "heapSpaceStatisticsBuffer"),
247
2625
            binding_data->heap_space_statistics_buffer.GetJSArray())
248
      .Check();
249
250
#define V(i, _, name)                                                         \
251
  target->Set(env->context(),                                                 \
252
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
253
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
254
255
8925
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
256
#undef V
257
258
  // Export symbols used by v8.setFlagsFromString()
259
525
  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
260
}
261
262
}  // namespace node
263
264
4398
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)