5 #include "src/extensions/statistics-extension.h" 7 #include "src/counters.h" 8 #include "src/heap/heap-inl.h" 9 #include "src/isolate.h" 14 const char*
const StatisticsExtension::kSource =
15 "native function getV8Statistics();";
25 static void AddCounter(v8::Isolate* isolate,
27 StatsCounter* counter,
29 if (counter->Enabled()) {
30 object->Set(isolate->GetCurrentContext(),
33 v8::Number::New(isolate, *counter->GetInternalPointer()))
39 double value,
const char* name) {
41 ->Set(isolate->GetCurrentContext(),
44 v8::Number::New(isolate, value))
49 static void AddNumber64(v8::Isolate* isolate,
53 object->Set(isolate->GetCurrentContext(),
56 v8::Number::New(isolate, static_cast<double>(value))).FromJust();
60 void StatisticsExtension::GetCounters(
62 Isolate* isolate =
reinterpret_cast<Isolate*
>(args.GetIsolate());
63 Heap* heap = isolate->heap();
65 if (args.Length() > 0) {
66 if (args[0]->IsBoolean() && args[0]->BooleanValue(args.GetIsolate())) {
67 heap->CollectAllGarbage(Heap::kNoGCFlags,
68 GarbageCollectionReason::kCountersExtension);
72 Counters* counters = isolate->counters();
75 struct StatisticsCounter {
79 const StatisticsCounter counter_list[] = {
80 #define ADD_COUNTER(name, caption) \ 81 { counters->name(), #name } \ 84 STATS_COUNTER_LIST_1(ADD_COUNTER) STATS_COUNTER_LIST_2(ADD_COUNTER)
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);
93 struct StatisticNumber {
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"},
118 for (
size_t i = 0;
i < arraysize(numbers);
i++) {
119 AddNumber(args.GetIsolate(), result, numbers[
i].number, numbers[
i].name);
122 AddNumber64(args.GetIsolate(), result, heap->external_memory(),
123 "amount_of_external_allocated_memory");
124 args.GetReturnValue().Set(result);
126 HeapIterator iterator(reinterpret_cast<Isolate*>(args.GetIsolate())->heap());
128 int reloc_info_total = 0;
129 int source_position_table_total = 0;
130 while ((obj = iterator.next()) !=
nullptr) {
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();
138 }
else if (obj->IsBytecodeArray()) {
139 source_position_table_total +=
140 BytecodeArray::cast(obj)->SourcePositionTable()->Size();
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");
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, v8::NewStringType type, int length=-1)
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)