V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
statistics-extension.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/extensions/statistics-extension.h"
6 
7 #include "src/counters.h"
8 #include "src/heap/heap-inl.h" // crbug.com/v8/8499
9 #include "src/isolate.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 const char* const StatisticsExtension::kSource =
15  "native function getV8Statistics();";
16 
17 
18 v8::Local<v8::FunctionTemplate> StatisticsExtension::GetNativeFunctionTemplate(
19  v8::Isolate* isolate, v8::Local<v8::String> str) {
20  DCHECK_EQ(strcmp(*v8::String::Utf8Value(isolate, str), "getV8Statistics"), 0);
21  return v8::FunctionTemplate::New(isolate, StatisticsExtension::GetCounters);
22 }
23 
24 
25 static void AddCounter(v8::Isolate* isolate,
26  v8::Local<v8::Object> object,
27  StatsCounter* counter,
28  const char* name) {
29  if (counter->Enabled()) {
30  object->Set(isolate->GetCurrentContext(),
32  .ToLocalChecked(),
33  v8::Number::New(isolate, *counter->GetInternalPointer()))
34  .FromJust();
35  }
36 }
37 
38 static void AddNumber(v8::Isolate* isolate, v8::Local<v8::Object> object,
39  double value, const char* name) {
40  object
41  ->Set(isolate->GetCurrentContext(),
43  .ToLocalChecked(),
44  v8::Number::New(isolate, value))
45  .FromJust();
46 }
47 
48 
49 static void AddNumber64(v8::Isolate* isolate,
50  v8::Local<v8::Object> object,
51  int64_t value,
52  const char* name) {
53  object->Set(isolate->GetCurrentContext(),
55  .ToLocalChecked(),
56  v8::Number::New(isolate, static_cast<double>(value))).FromJust();
57 }
58 
59 
60 void StatisticsExtension::GetCounters(
62  Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
63  Heap* heap = isolate->heap();
64 
65  if (args.Length() > 0) { // GC if first argument evaluates to true.
66  if (args[0]->IsBoolean() && args[0]->BooleanValue(args.GetIsolate())) {
67  heap->CollectAllGarbage(Heap::kNoGCFlags,
68  GarbageCollectionReason::kCountersExtension);
69  }
70  }
71 
72  Counters* counters = isolate->counters();
73  v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
74 
75  struct StatisticsCounter {
77  const char* name;
78  };
79  const StatisticsCounter counter_list[] = {
80 #define ADD_COUNTER(name, caption) \
81  { counters->name(), #name } \
82  ,
83 
84  STATS_COUNTER_LIST_1(ADD_COUNTER) STATS_COUNTER_LIST_2(ADD_COUNTER)
85 #undef ADD_COUNTER
86  }; // End counter_list array.
87 
88  for (size_t i = 0; i < arraysize(counter_list); i++) {
89  AddCounter(args.GetIsolate(), result, counter_list[i].counter,
90  counter_list[i].name);
91  }
92 
93  struct StatisticNumber {
94  size_t number;
95  const char* name;
96  };
97 
98  const StatisticNumber numbers[] = {
99  {heap->memory_allocator()->Size(), "total_committed_bytes"},
100  {heap->new_space()->Size(), "new_space_live_bytes"},
101  {heap->new_space()->Available(), "new_space_available_bytes"},
102  {heap->new_space()->CommittedMemory(), "new_space_commited_bytes"},
103  {heap->old_space()->Size(), "old_space_live_bytes"},
104  {heap->old_space()->Available(), "old_space_available_bytes"},
105  {heap->old_space()->CommittedMemory(), "old_space_commited_bytes"},
106  {heap->code_space()->Size(), "code_space_live_bytes"},
107  {heap->code_space()->Available(), "code_space_available_bytes"},
108  {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
109  {heap->lo_space()->Size(), "lo_space_live_bytes"},
110  {heap->lo_space()->Available(), "lo_space_available_bytes"},
111  {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
112  {heap->code_lo_space()->Size(), "code_lo_space_live_bytes"},
113  {heap->code_lo_space()->Available(), "code_lo_space_available_bytes"},
114  {heap->code_lo_space()->CommittedMemory(),
115  "code_lo_space_commited_bytes"},
116  };
117 
118  for (size_t i = 0; i < arraysize(numbers); i++) {
119  AddNumber(args.GetIsolate(), result, numbers[i].number, numbers[i].name);
120  }
121 
122  AddNumber64(args.GetIsolate(), result, heap->external_memory(),
123  "amount_of_external_allocated_memory");
124  args.GetReturnValue().Set(result);
125 
126  HeapIterator iterator(reinterpret_cast<Isolate*>(args.GetIsolate())->heap());
127  HeapObject* obj;
128  int reloc_info_total = 0;
129  int source_position_table_total = 0;
130  while ((obj = iterator.next()) != nullptr) {
131  if (obj->IsCode()) {
132  Code code = Code::cast(obj);
133  reloc_info_total += code->relocation_info()->Size();
134  ByteArray source_position_table = code->SourcePositionTable();
135  if (source_position_table->length() > 0) {
136  source_position_table_total += code->SourcePositionTable()->Size();
137  }
138  } else if (obj->IsBytecodeArray()) {
139  source_position_table_total +=
140  BytecodeArray::cast(obj)->SourcePositionTable()->Size();
141  }
142  }
143 
144  AddNumber(args.GetIsolate(), result, reloc_info_total,
145  "reloc_info_total_size");
146  AddNumber(args.GetIsolate(), result, source_position_table_total,
147  "source_position_table_total_size");
148 }
149 
150 } // namespace internal
151 } // namespace v8
Definition: v8.h:85
Definition: libplatform.h:13
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, v8::NewStringType type, int length=-1)
Definition: api.cc:6511
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect)
Definition: api.cc:1417