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