Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_v8.cc
Go to the documentation of this file.
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.h"
24 #include "env-inl.h"
25 #include "util.h"
26 #include "util-inl.h"
27 #include "v8.h"
28 
29 namespace node {
30 
31 using v8::Array;
32 using v8::ArrayBuffer;
33 using v8::Context;
34 using v8::FunctionCallbackInfo;
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 #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 
59 #define V(a, b, c) +1
60 static const size_t kHeapStatisticsPropertiesCount =
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 =
73 #undef V
74 
75 // Will be populated in InitializeV8Bindings.
76 static size_t number_of_heap_spaces = 0;
77 
78 
79 void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
80  Environment* env = Environment::GetCurrent(args);
81  Local<Integer> result =
82  Integer::NewFromUnsigned(env->isolate(),
84  args.GetReturnValue().Set(result);
85 }
86 
87 
88 void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
89  Environment* env = Environment::GetCurrent(args);
90  HeapStatistics s;
91  env->isolate()->GetHeapStatistics(&s);
92  double* const buffer = env->heap_statistics_buffer();
93 #define V(index, name, _) buffer[index] = static_cast<double>(s.name());
95 #undef V
96 }
97 
98 
99 void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
100  Environment* env = Environment::GetCurrent(args);
101  HeapSpaceStatistics s;
102  Isolate* const isolate = env->isolate();
103  double* buffer = env->heap_space_statistics_buffer();
104 
105  for (size_t i = 0; i < number_of_heap_spaces; i++) {
106  isolate->GetHeapSpaceStatistics(&s, i);
107  size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
108 #define V(index, name, _) buffer[property_offset + index] = \
109  static_cast<double>(s.name());
111 #undef V
112  }
113 }
114 
115 
116 void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
117  Environment* env = Environment::GetCurrent(args);
118 
119  if (args.Length() < 1)
120  return env->ThrowTypeError("v8 flag is required");
121  if (!args[0]->IsString())
122  return env->ThrowTypeError("v8 flag must be a string");
123 
124  String::Utf8Value flags(args[0]);
125  V8::SetFlagsFromString(*flags, flags.length());
126 }
127 
128 
129 void InitializeV8Bindings(Local<Object> target,
130  Local<Value> unused,
131  Local<Context> context) {
132  Environment* env = Environment::GetCurrent(context);
133 
134  env->SetMethod(target, "cachedDataVersionTag", CachedDataVersionTag);
135 
136  env->SetMethod(target,
137  "updateHeapStatisticsArrayBuffer",
139 
140  env->set_heap_statistics_buffer(new double[kHeapStatisticsPropertiesCount]);
141 
142  const size_t heap_statistics_buffer_byte_length =
143  sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
144 
145  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
146  "heapStatisticsArrayBuffer"),
147  ArrayBuffer::New(env->isolate(),
148  env->heap_statistics_buffer(),
149  heap_statistics_buffer_byte_length));
150 
151 #define V(i, _, name) \
152  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
153  Uint32::NewFromUnsigned(env->isolate(), i));
154 
156 #undef V
157 
158  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
159  "kHeapSpaceStatisticsPropertiesCount"),
160  Uint32::NewFromUnsigned(env->isolate(),
161  kHeapSpaceStatisticsPropertiesCount));
162 
163  number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
164 
165  // Heap space names are extracted once and exposed to JavaScript to
166  // avoid excessive creation of heap space name Strings.
167  HeapSpaceStatistics s;
168  const Local<Array> heap_spaces = Array::New(env->isolate(),
169  number_of_heap_spaces);
170  for (size_t i = 0; i < number_of_heap_spaces; i++) {
171  env->isolate()->GetHeapSpaceStatistics(&s, i);
172  Local<String> heap_space_name = String::NewFromUtf8(env->isolate(),
173  s.space_name(),
174  NewStringType::kNormal)
175  .ToLocalChecked();
176  heap_spaces->Set(i, heap_space_name);
177  }
178  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kHeapSpaces"),
179  heap_spaces);
180 
181  env->SetMethod(target,
182  "updateHeapSpaceStatisticsArrayBuffer",
184 
185  env->set_heap_space_statistics_buffer(
186  new double[kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces]);
187 
188  const size_t heap_space_statistics_buffer_byte_length =
189  sizeof(*env->heap_space_statistics_buffer()) *
190  kHeapSpaceStatisticsPropertiesCount *
191  number_of_heap_spaces;
192 
193  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
194  "heapSpaceStatisticsArrayBuffer"),
195  ArrayBuffer::New(env->isolate(),
196  env->heap_space_statistics_buffer(),
197  heap_space_statistics_buffer_byte_length));
198 
199 #define V(i, _, name) \
200  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
201  Uint32::NewFromUnsigned(env->isolate(), i));
202 
204 #undef V
205 
206  env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
207 }
208 
209 } // namespace node
210 
void SetFlagsFromString(const FunctionCallbackInfo< Value > &args)
Definition: node_v8.cc:116
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
void CachedDataVersionTag(const FunctionCallbackInfo< Value > &args)
Definition: node_v8.cc:79
#define HEAP_STATISTICS_PROPERTIES(V)
Definition: node_v8.cc:48
void InitializeV8Bindings(Local< Object > target, Local< Value > unused, Local< Context > context)
Definition: node_v8.cc:129
#define HEAP_SPACE_STATISTICS_PROPERTIES(V)
Definition: node_v8.cc:64
#define V(a, b, c)
Definition: node_v8.cc:70
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo< Value > &args)
Definition: node_v8.cc:99
dtrace s
Definition: v8ustack.d:615
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo< Value > &args)
Definition: node_v8.cc:88