v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
statistics-extension.cc
Go to the documentation of this file.
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
6
10#include "src/heap/heap-inl.h" // crbug.com/v8/8499
12#include "src/objects/tagged.h"
13#include "src/roots/roots.h"
14
15namespace v8 {
16namespace internal {
17
18const char* const StatisticsExtension::kSource =
19 "native function getV8Statistics();";
20
21
27
28
29static void AddCounter(v8::Isolate* isolate,
31 StatsCounter* counter,
32 const char* name) {
33 if (counter->Enabled()) {
34 object
35 ->Set(isolate->GetCurrentContext(),
36 v8::String::NewFromUtf8(isolate, name).ToLocalChecked(),
37 v8::Number::New(isolate, *counter->GetInternalPointer()))
38 .FromJust();
39 }
40}
41
42static void AddNumber(v8::Isolate* isolate, v8::Local<v8::Object> object,
43 double value, const char* name) {
44 object
45 ->Set(isolate->GetCurrentContext(),
46 v8::String::NewFromUtf8(isolate, name).ToLocalChecked(),
47 v8::Number::New(isolate, value))
48 .FromJust();
49}
50
51
52static void AddNumber64(v8::Isolate* isolate,
54 int64_t value,
55 const char* name) {
56 object
57 ->Set(isolate->GetCurrentContext(),
58 v8::String::NewFromUtf8(isolate, name).ToLocalChecked(),
59 v8::Number::New(isolate, static_cast<double>(value)))
60 .FromJust();
61}
62
66 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
67 Heap* heap = isolate->heap();
68
69 if (info.Length() > 0) { // GC if first argument evaluates to true.
70 if (info[0]->IsBoolean() && info[0]->BooleanValue(info.GetIsolate())) {
71 heap->CollectAllGarbage(GCFlag::kNoFlags,
73 }
74 }
75
76 Counters* counters = isolate->counters();
77 v8::Local<v8::Object> result = v8::Object::New(info.GetIsolate());
78
79 heap->FreeMainThreadLinearAllocationAreas();
80
81 struct StatisticsCounter {
83 const char* name;
84 };
85 // clang-format off
86 const StatisticsCounter counter_list[] = {
87#define ADD_COUNTER(name, caption) {counters->name(), #name},
90#undef ADD_COUNTER
91 }; // End counter_list array.
92 // clang-format on
93
94 for (size_t i = 0; i < arraysize(counter_list); i++) {
95 AddCounter(info.GetIsolate(), result, counter_list[i].counter,
96 counter_list[i].name);
97 }
98
99 struct StatisticNumber {
100 size_t number;
101 const char* name;
102 };
103
104 size_t new_space_size = 0;
105 size_t new_space_available = 0;
106 size_t new_space_committed_memory = 0;
107
108 if (heap->new_space()) {
109 new_space_size = heap->new_space()->Size();
110 new_space_available = heap->new_space()->Available();
111 new_space_committed_memory = heap->new_space()->CommittedMemory();
112 }
113
114 const StatisticNumber numbers[] = {
115 {heap->memory_allocator()->Size(), "total_committed_bytes"},
116 {new_space_size, "new_space_live_bytes"},
117 {new_space_available, "new_space_available_bytes"},
118 {new_space_committed_memory, "new_space_commited_bytes"},
119 {heap->old_space()->Size(), "old_space_live_bytes"},
120 {heap->old_space()->Available(), "old_space_available_bytes"},
121 {heap->old_space()->CommittedMemory(), "old_space_commited_bytes"},
122 {heap->code_space()->Size(), "code_space_live_bytes"},
123 {heap->code_space()->Available(), "code_space_available_bytes"},
124 {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
125 {heap->lo_space()->Size(), "lo_space_live_bytes"},
126 {heap->lo_space()->Available(), "lo_space_available_bytes"},
127 {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
128 {heap->code_lo_space()->Size(), "code_lo_space_live_bytes"},
129 {heap->code_lo_space()->Available(), "code_lo_space_available_bytes"},
130 {heap->code_lo_space()->CommittedMemory(),
131 "code_lo_space_commited_bytes"},
132 {heap->trusted_space()->Size(), "trusted_space_live_bytes"},
133 {heap->trusted_space()->Available(), "trusted_space_available_bytes"},
134 {heap->trusted_space()->CommittedMemory(),
135 "trusted_space_commited_bytes"},
136 {heap->trusted_lo_space()->Size(), "trusted_lo_space_live_bytes"},
137 {heap->trusted_lo_space()->Available(),
138 "trusted_lo_space_available_bytes"},
139 {heap->trusted_lo_space()->CommittedMemory(),
140 "trusted_lo_space_commited_bytes"},
141 };
142
143 for (size_t i = 0; i < arraysize(numbers); i++) {
144 AddNumber(info.GetIsolate(), result, numbers[i].number, numbers[i].name);
145 }
146
147 AddNumber64(info.GetIsolate(), result, heap->external_memory(),
148 "amount_of_external_allocated_memory");
149
150 int reloc_info_total = 0;
151 int source_position_table_total = 0;
152 {
153 HeapObjectIterator iterator(
154 reinterpret_cast<Isolate*>(info.GetIsolate())->heap());
155 DCHECK(!AllowGarbageCollection::IsAllowed());
156 for (Tagged<HeapObject> obj = iterator.Next(); !obj.is_null();
157 obj = iterator.Next()) {
158 Tagged<Object> maybe_source_positions;
159 if (IsCode(obj)) {
160 Tagged<Code> code = Cast<Code>(obj);
161 reloc_info_total += code->relocation_size();
162 if (!code->has_source_position_table()) continue;
163 maybe_source_positions = code->source_position_table();
164 } else if (IsBytecodeArray(obj)) {
165 maybe_source_positions =
166 Cast<BytecodeArray>(obj)->raw_source_position_table(kAcquireLoad);
167 } else {
168 continue;
169 }
170 if (!IsTrustedByteArray(maybe_source_positions)) continue;
172 Cast<TrustedByteArray>(maybe_source_positions);
173 if (source_positions->length() == 0) continue;
174 source_position_table_total += source_positions->AllocatedSize();
175 }
176 }
177
178 AddNumber(info.GetIsolate(), result, reloc_info_total,
179 "reloc_info_total_size");
180 AddNumber(info.GetIsolate(), result, source_position_table_total,
181 "source_position_table_total_size");
182 info.GetReturnValue().Set(result);
183}
184
185} // namespace internal
186} // namespace v8
const char * name
Definition builtins.cc:39
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, const CFunction *c_function=nullptr, uint16_t instance_type=0, uint16_t allowed_receiver_instance_type_range_start=0, uint16_t allowed_receiver_instance_type_range_end=0)
Definition api.cc:1101
static Local< Number > New(Isolate *isolate, double value)
Definition api.cc:9557
static Local< Object > New(Isolate *isolate)
Definition api.cc:7756
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=NewStringType::kNormal, int length=-1)
Definition api.cc:7593
Tagged< HeapObject > Next()
Definition heap.cc:6658
static void GetCounters(const v8::FunctionCallbackInfo< v8::Value > &info)
v8::Local< v8::FunctionTemplate > GetNativeFunctionTemplate(v8::Isolate *isolate, v8::Local< v8::String > name) override
V8_EXPORT_PRIVATE bool Enabled()
Definition counters.cc:32
std::atomic< int > * GetInternalPointer()
Definition counters.h:120
V8_INLINE constexpr bool is_null() const
Definition tagged.h:502
#define STATS_COUNTER_LIST(SC)
#define STATS_COUNTER_NATIVE_CODE_LIST(SC)
SourcePositionTable * source_positions
ZoneVector< RpoNumber > & result
static void AddCounter(v8::Isolate *isolate, v8::Local< v8::Object > object, StatsCounter *counter, const char *name)
static void AddNumber64(v8::Isolate *isolate, v8::Local< v8::Object > object, int64_t value, const char *name)
bool V8_EXPORT ValidateCallbackInfo(const FunctionCallbackInfo< void > &info)
Definition api.cc:12301
static void AddNumber(v8::Isolate *isolate, v8::Local< v8::Object > object, double value, const char *name)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
static constexpr AcquireLoadTag kAcquireLoad
Definition globals.h:2908
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define arraysize(array)
Definition macros.h:67
#define ADD_COUNTER(name, caption)