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: 75 75 100.0 %
Date: 2020-08-07 22:13:19 Branches: 7 12 58.3 %

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

17763
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)