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: 74 74 100.0 %
Date: 2020-02-19 22:14:06 Branches: 5 6 83.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 "env-inl.h"
24
#include "util-inl.h"
25
#include "v8.h"
26
27
namespace node {
28
29
using v8::Array;
30
using v8::ArrayBuffer;
31
using v8::BackingStore;
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 const 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 const size_t kHeapSpaceStatisticsPropertiesCount =
76
    HEAP_SPACE_STATISTICS_PROPERTIES(V);
77
#undef V
78
79
80
#define HEAP_CODE_STATISTICS_PROPERTIES(V)                                    \
81
  V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex)                    \
82
  V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex)             \
83
  V(2, external_script_source_size, kExternalScriptSourceSizeIndex)
84
85
#define V(a, b, c) +1
86
static const size_t kHeapCodeStatisticsPropertiesCount =
87
    HEAP_CODE_STATISTICS_PROPERTIES(V);
88
#undef V
89
90
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
91
4
  Environment* env = Environment::GetCurrent(args);
92
  Local<Integer> result =
93
      Integer::NewFromUnsigned(env->isolate(),
94
4
                               ScriptCompiler::CachedDataVersionTag());
95
8
  args.GetReturnValue().Set(result);
96
4
}
97
98
99
1570
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
100
1570
  Environment* env = Environment::GetCurrent(args);
101
1570
  HeapStatistics s;
102
1570
  env->isolate()->GetHeapStatistics(&s);
103
1570
  double* const buffer = env->heap_statistics_buffer();
104
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
105
1570
  HEAP_STATISTICS_PROPERTIES(V)
106
#undef V
107
1570
}
108
109
110
1570
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
111
1570
  Environment* env = Environment::GetCurrent(args);
112
1570
  HeapSpaceStatistics s;
113
1570
  Isolate* const isolate = env->isolate();
114
1570
  double* buffer = env->heap_space_statistics_buffer();
115
1570
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
116
117
14130
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
118
12560
    isolate->GetHeapSpaceStatistics(&s, i);
119
12560
    size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
120
#define V(index, name, _) buffer[property_offset + index] = \
121
                              static_cast<double>(s.name());
122
12560
      HEAP_SPACE_STATISTICS_PROPERTIES(V)
123
#undef V
124
  }
125
1570
}
126
127
128
1
void UpdateHeapCodeStatisticsArrayBuffer(
129
    const FunctionCallbackInfo<Value>& args) {
130
1
  Environment* env = Environment::GetCurrent(args);
131
1
  HeapCodeStatistics s;
132
1
  env->isolate()->GetHeapCodeAndMetadataStatistics(&s);
133
1
  double* const buffer = env->heap_code_statistics_buffer();
134
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
135
1
  HEAP_CODE_STATISTICS_PROPERTIES(V)
136
#undef V
137
1
}
138
139
140
6
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
141
18
  CHECK(args[0]->IsString());
142
12
  String::Utf8Value flags(args.GetIsolate(), args[0]);
143
6
  V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
144
6
}
145
146
147
499
void Initialize(Local<Object> target,
148
                Local<Value> unused,
149
                Local<Context> context,
150
                void* priv) {
151
499
  Environment* env = Environment::GetCurrent(context);
152
153
  env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
154
499
                             CachedDataVersionTag);
155
156
  // Export symbols used by v8.getHeapStatistics()
157
  env->SetMethod(target,
158
                 "updateHeapStatisticsArrayBuffer",
159
499
                 UpdateHeapStatisticsArrayBuffer);
160
161
  const size_t heap_statistics_buffer_byte_length =
162
499
      sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
163
164
  Local<ArrayBuffer> heap_statistics_ab =
165
499
      ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length);
166
499
  env->set_heap_statistics_buffer(heap_statistics_ab->GetBackingStore());
167
998
  target->Set(env->context(),
168
              FIXED_ONE_BYTE_STRING(env->isolate(),
169
                                    "heapStatisticsArrayBuffer"),
170
1996
              heap_statistics_ab).Check();
171
172
#define V(i, _, name)                                                         \
173
  target->Set(env->context(),                                                 \
174
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
175
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
176
177
22455
  HEAP_STATISTICS_PROPERTIES(V)
178
#undef V
179
180
  // Export symbols used by v8.getHeapCodeStatistics()
181
  env->SetMethod(target,
182
                 "updateHeapCodeStatisticsArrayBuffer",
183
499
                 UpdateHeapCodeStatisticsArrayBuffer);
184
185
  const size_t heap_code_statistics_buffer_byte_length =
186
      sizeof(*env->heap_code_statistics_buffer())
187
499
      * kHeapCodeStatisticsPropertiesCount;
188
189
  Local<ArrayBuffer> heap_code_statistics_ab =
190
      ArrayBuffer::New(env->isolate(),
191
499
                       heap_code_statistics_buffer_byte_length);
192
  env->set_heap_code_statistics_buffer(
193
499
      heap_code_statistics_ab->GetBackingStore());
194
998
  target->Set(env->context(),
195
              FIXED_ONE_BYTE_STRING(env->isolate(),
196
                                    "heapCodeStatisticsArrayBuffer"),
197
1996
              heap_code_statistics_ab)
198
  .Check();
199
200
#define V(i, _, name)                                                         \
201
  target->Set(env->context(),                                                 \
202
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
203
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
204
205
6487
  HEAP_CODE_STATISTICS_PROPERTIES(V)
206
#undef V
207
208
  // Export symbols used by v8.getHeapSpaceStatistics()
209
998
  target->Set(env->context(),
210
              FIXED_ONE_BYTE_STRING(env->isolate(),
211
                                    "kHeapSpaceStatisticsPropertiesCount"),
212
              Uint32::NewFromUnsigned(env->isolate(),
213
2495
                                      kHeapSpaceStatisticsPropertiesCount))
214
              .Check();
215
216
499
  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
499
  HeapSpaceStatistics s;
221
998
  MaybeStackBuffer<Local<Value>, 16> heap_spaces(number_of_heap_spaces);
222
4491
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
223
3992
    env->isolate()->GetHeapSpaceStatistics(&s, i);
224
15968
    heap_spaces[i] = String::NewFromUtf8(env->isolate(),
225
                                         s.space_name(),
226
3992
                                         NewStringType::kNormal)
227
3992
                                             .ToLocalChecked();
228
  }
229
998
  target->Set(env->context(),
230
              FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
231
              Array::New(env->isolate(),
232
                         heap_spaces.out(),
233
2495
                         number_of_heap_spaces)).Check();
234
235
  env->SetMethod(target,
236
                 "updateHeapSpaceStatisticsArrayBuffer",
237
499
                 UpdateHeapSpaceStatisticsBuffer);
238
239
  const size_t heap_space_statistics_buffer_byte_length =
240
      sizeof(*env->heap_space_statistics_buffer()) *
241
      kHeapSpaceStatisticsPropertiesCount *
242
499
      number_of_heap_spaces;
243
244
  Local<ArrayBuffer> heap_space_statistics_ab =
245
      ArrayBuffer::New(env->isolate(),
246
499
                       heap_space_statistics_buffer_byte_length);
247
  env->set_heap_space_statistics_buffer(
248
499
      heap_space_statistics_ab->GetBackingStore());
249
998
  target->Set(env->context(),
250
              FIXED_ONE_BYTE_STRING(env->isolate(),
251
                                    "heapSpaceStatisticsArrayBuffer"),
252
1996
              heap_space_statistics_ab)
253
              .Check();
254
255
#define V(i, _, name)                                                         \
256
  target->Set(env->context(),                                                 \
257
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
258
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
259
260
8483
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
261
#undef V
262
263
  // Export symbols used by v8.setFlagsFromString()
264
499
  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
265
499
}
266
267
}  // namespace node
268
269
4185
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)