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-02-13 22:28:58 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
57
#define V(a, b, c) +1
58
static const size_t kHeapStatisticsPropertiesCount =
59
    HEAP_STATISTICS_PROPERTIES(V);
60
#undef V
61
62
#define HEAP_SPACE_STATISTICS_PROPERTIES(V)                                   \
63
  V(0, space_size, kSpaceSizeIndex)                                           \
64
  V(1, space_used_size, kSpaceUsedSizeIndex)                                  \
65
  V(2, space_available_size, kSpaceAvailableSizeIndex)                        \
66
  V(3, physical_space_size, kPhysicalSpaceSizeIndex)
67
68
#define V(a, b, c) +1
69
static const size_t kHeapSpaceStatisticsPropertiesCount =
70
    HEAP_SPACE_STATISTICS_PROPERTIES(V);
71
#undef V
72
73
74
4
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
75
4
  Environment* env = Environment::GetCurrent(args);
76
  Local<Integer> result =
77
      Integer::NewFromUnsigned(env->isolate(),
78
4
                               ScriptCompiler::CachedDataVersionTag());
79
8
  args.GetReturnValue().Set(result);
80
4
}
81
82
83
2
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
84
2
  Environment* env = Environment::GetCurrent(args);
85
2
  HeapStatistics s;
86
2
  env->isolate()->GetHeapStatistics(&s);
87
2
  double* const buffer = env->heap_statistics_buffer();
88
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
89
2
  HEAP_STATISTICS_PROPERTIES(V)
90
#undef V
91
2
}
92
93
94
1
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
95
1
  Environment* env = Environment::GetCurrent(args);
96
1
  HeapSpaceStatistics s;
97
1
  Isolate* const isolate = env->isolate();
98
1
  double* buffer = env->heap_space_statistics_buffer();
99
1
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
100
101
8
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
102
7
    isolate->GetHeapSpaceStatistics(&s, i);
103
7
    size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
104
#define V(index, name, _) buffer[property_offset + index] = \
105
                              static_cast<double>(s.name());
106
7
      HEAP_SPACE_STATISTICS_PROPERTIES(V)
107
#undef V
108
  }
109
1
}
110
111
112
4
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
113
12
  CHECK(args[0]->IsString());
114
4
  String::Utf8Value flags(args.GetIsolate(), args[0]);
115
4
  V8::SetFlagsFromString(*flags, flags.length());
116
4
}
117
118
119
4402
void Initialize(Local<Object> target,
120
                Local<Value> unused,
121
                Local<Context> context,
122
                void* priv) {
123
4402
  Environment* env = Environment::GetCurrent(context);
124
125
  env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
126
4402
                             CachedDataVersionTag);
127
128
  env->SetMethod(target,
129
                 "updateHeapStatisticsArrayBuffer",
130
4402
                 UpdateHeapStatisticsArrayBuffer);
131
132
4402
  env->set_heap_statistics_buffer(new double[kHeapStatisticsPropertiesCount]);
133
134
  const size_t heap_statistics_buffer_byte_length =
135
4402
      sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
136
137
  target->Set(env->context(),
138
              FIXED_ONE_BYTE_STRING(env->isolate(),
139
                                    "heapStatisticsArrayBuffer"),
140
              ArrayBuffer::New(env->isolate(),
141
4402
                               env->heap_statistics_buffer(),
142
22010
                               heap_statistics_buffer_byte_length)).FromJust();
143
144
#define V(i, _, name)                                                         \
145
  target->Set(env->context(),                                                 \
146
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
147
              Uint32::NewFromUnsigned(env->isolate(), i)).FromJust();
148
149
162874
  HEAP_STATISTICS_PROPERTIES(V)
150
#undef V
151
152
  target->Set(env->context(),
153
              FIXED_ONE_BYTE_STRING(env->isolate(),
154
                                    "kHeapSpaceStatisticsPropertiesCount"),
155
              Uint32::NewFromUnsigned(env->isolate(),
156
22010
                                      kHeapSpaceStatisticsPropertiesCount))
157
8804
              .FromJust();
158
159
4402
  size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
160
161
  // Heap space names are extracted once and exposed to JavaScript to
162
  // avoid excessive creation of heap space name Strings.
163
4402
  HeapSpaceStatistics s;
164
  const Local<Array> heap_spaces = Array::New(env->isolate(),
165
4402
                                              number_of_heap_spaces);
166
35216
  for (size_t i = 0; i < number_of_heap_spaces; i++) {
167
30814
    env->isolate()->GetHeapSpaceStatistics(&s, i);
168
    Local<String> heap_space_name = String::NewFromUtf8(env->isolate(),
169
                                                        s.space_name(),
170
30814
                                                        NewStringType::kNormal)
171
61628
                                        .ToLocalChecked();
172
92442
    heap_spaces->Set(env->context(), i, heap_space_name).FromJust();
173
  }
174
  target->Set(env->context(),
175
              FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
176
17608
              heap_spaces).FromJust();
177
178
  env->SetMethod(target,
179
                 "updateHeapSpaceStatisticsArrayBuffer",
180
4402
                 UpdateHeapSpaceStatisticsBuffer);
181
182
  env->set_heap_space_statistics_buffer(
183
4402
    new double[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
184
185
  const size_t heap_space_statistics_buffer_byte_length =
186
      sizeof(*env->heap_space_statistics_buffer()) *
187
4402
      kHeapSpaceStatisticsPropertiesCount *
188
4402
      number_of_heap_spaces;
189
190
  target->Set(env->context(),
191
              FIXED_ONE_BYTE_STRING(env->isolate(),
192
                                    "heapSpaceStatisticsArrayBuffer"),
193
              ArrayBuffer::New(env->isolate(),
194
4402
                               env->heap_space_statistics_buffer(),
195
22010
                               heap_space_statistics_buffer_byte_length))
196
8804
              .FromJust();
197
198
#define V(i, _, name)                                                         \
199
  target->Set(env->context(),                                                 \
200
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
201
              Uint32::NewFromUnsigned(env->isolate(), i)).FromJust();
202
203
74834
  HEAP_SPACE_STATISTICS_PROPERTIES(V)
204
#undef V
205
206
4402
  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
207
4402
}
208
209
}  // namespace node
210
211
4314
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)