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: 59 59 100.0 %
Date: 2019-07-27 22:37:30 Branches: 6 8 75.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.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::Context;
32
using v8::FunctionCallbackInfo;
33
using v8::HeapSpaceStatistics;
34
using v8::HeapStatistics;
35
using v8::Integer;
36
using v8::Isolate;
37
using v8::Local;
38
using v8::NewStringType;
39
using v8::Object;
40
using v8::ScriptCompiler;
41
using v8::String;
42
using v8::Uint32;
43
using v8::V8;
44
using v8::Value;
45
46
#define HEAP_STATISTICS_PROPERTIES(V)                                         \
47
  V(0, total_heap_size, kTotalHeapSizeIndex)                                  \
48
  V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex)             \
49
  V(2, total_physical_size, kTotalPhysicalSizeIndex)                          \
50
  V(3, total_available_size, kTotalAvailableSize)                             \
51
  V(4, used_heap_size, kUsedHeapSizeIndex)                                    \
52
  V(5, heap_size_limit, kHeapSizeLimitIndex)                                  \
53
  V(6, malloced_memory, kMallocedMemoryIndex)                                 \
54
  V(7, peak_malloced_memory, kPeakMallocedMemoryIndex)                        \
55
  V(8, does_zap_garbage, kDoesZapGarbageIndex)                                \
56
  V(9, number_of_native_contexts, kNumberOfNativeContextsIndex)               \
57
  V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex)
58
59
#define V(a, b, c) +1
60
static const size_t kHeapStatisticsPropertiesCount =
61
    HEAP_STATISTICS_PROPERTIES(V);
62
#undef V
63
64
#define HEAP_SPACE_STATISTICS_PROPERTIES(V)                                   \
65
  V(0, space_size, kSpaceSizeIndex)                                           \
66
  V(1, space_used_size, kSpaceUsedSizeIndex)                                  \
67
  V(2, space_available_size, kSpaceAvailableSizeIndex)                        \
68
  V(3, physical_space_size, kPhysicalSpaceSizeIndex)
69
70
#define V(a, b, c) +1
71
static const size_t kHeapSpaceStatisticsPropertiesCount =
72
    HEAP_SPACE_STATISTICS_PROPERTIES(V);
73
#undef V
74
75
76
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
77
4
  Environment* env = Environment::GetCurrent(args);
78
  Local<Integer> result =
79
      Integer::NewFromUnsigned(env->isolate(),
80
4
                               ScriptCompiler::CachedDataVersionTag());
81
8
  args.GetReturnValue().Set(result);
82
4
}
83
84
85
2
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
86
2
  Environment* env = Environment::GetCurrent(args);
87
2
  HeapStatistics s;
88
2
  env->isolate()->GetHeapStatistics(&s);
89
2
  double* const buffer = env->heap_statistics_buffer();
90
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
91
2
  HEAP_STATISTICS_PROPERTIES(V)
92
#undef V
93
2
}
94
95
96
1
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
97
1
  Environment* env = Environment::GetCurrent(args);
98
1
  HeapSpaceStatistics s;
99
1
  Isolate* const isolate = env->isolate();
100
1
  double* buffer = env->heap_space_statistics_buffer();
101
1
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
102
103
9
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
104
8
    isolate->GetHeapSpaceStatistics(&s, i);
105
8
    size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
106
#define V(index, name, _) buffer[property_offset + index] = \
107
                              static_cast<double>(s.name());
108
8
      HEAP_SPACE_STATISTICS_PROPERTIES(V)
109
#undef V
110
  }
111
1
}
112
113
114
4
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
115
12
  CHECK(args[0]->IsString());
116
4
  String::Utf8Value flags(args.GetIsolate(), args[0]);
117
4
  V8::SetFlagsFromString(*flags, flags.length());
118
4
}
119
120
121
65
void Initialize(Local<Object> target,
122
                Local<Value> unused,
123
                Local<Context> context,
124
                void* priv) {
125
65
  Environment* env = Environment::GetCurrent(context);
126
127
  env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
128
65
                             CachedDataVersionTag);
129
130
  env->SetMethod(target,
131
                 "updateHeapStatisticsArrayBuffer",
132
65
                 UpdateHeapStatisticsArrayBuffer);
133
134
65
  env->set_heap_statistics_buffer(new double[kHeapStatisticsPropertiesCount]);
135
136
  const size_t heap_statistics_buffer_byte_length =
137
65
      sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
138
139
  target->Set(env->context(),
140
              FIXED_ONE_BYTE_STRING(env->isolate(),
141
                                    "heapStatisticsArrayBuffer"),
142
              ArrayBuffer::New(env->isolate(),
143
65
                               env->heap_statistics_buffer(),
144
325
                               heap_statistics_buffer_byte_length)).Check();
145
146
#define V(i, _, name)                                                         \
147
  target->Set(env->context(),                                                 \
148
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
149
              Uint32::NewFromUnsigned(env->isolate(), i)).Check();
150
151
2925
  HEAP_STATISTICS_PROPERTIES(V)
152
#undef V
153
154
  target->Set(env->context(),
155
              FIXED_ONE_BYTE_STRING(env->isolate(),
156
                                    "kHeapSpaceStatisticsPropertiesCount"),
157
              Uint32::NewFromUnsigned(env->isolate(),
158
325
                                      kHeapSpaceStatisticsPropertiesCount))
159
130
              .Check();
160
161
65
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
162
163
  // Heap space names are extracted once and exposed to JavaScript to
164
  // avoid excessive creation of heap space name Strings.
165
65
  HeapSpaceStatistics s;
166
  const Local<Array> heap_spaces = Array::New(env->isolate(),
167
65
                                              number_of_heap_spaces);
168
585
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
169
520
    env->isolate()->GetHeapSpaceStatistics(&s, i);
170
    Local<String> heap_space_name = String::NewFromUtf8(env->isolate(),
171
                                                        s.space_name(),
172
520
                                                        NewStringType::kNormal)
173
1040
                                        .ToLocalChecked();
174
1560
    heap_spaces->Set(env->context(), i, heap_space_name).Check();
175
  }
176
  target->Set(env->context(),
177
              FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
178
260
              heap_spaces).Check();
179
180
  env->SetMethod(target,
181
                 "updateHeapSpaceStatisticsArrayBuffer",
182
65
                 UpdateHeapSpaceStatisticsBuffer);
183
184
  env->set_heap_space_statistics_buffer(
185
65
    new double[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
186
187
  const size_t heap_space_statistics_buffer_byte_length =
188
      sizeof(*env->heap_space_statistics_buffer()) *
189
65
      kHeapSpaceStatisticsPropertiesCount *
190
65
      number_of_heap_spaces;
191
192
  target->Set(env->context(),
193
              FIXED_ONE_BYTE_STRING(env->isolate(),
194
                                    "heapSpaceStatisticsArrayBuffer"),
195
              ArrayBuffer::New(env->isolate(),
196
65
                               env->heap_space_statistics_buffer(),
197
325
                               heap_space_statistics_buffer_byte_length))
198
130
              .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
1105
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
206
#undef V
207
208
65
  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
209
65
}
210
211
}  // namespace node
212
213
4831
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)