Line data Source code
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 0 : 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 0 : return v8::FunctionTemplate::New(isolate, StatisticsExtension::GetCounters);
22 : }
23 :
24 :
25 0 : static void AddCounter(v8::Isolate* isolate,
26 : v8::Local<v8::Object> object,
27 : StatsCounter* counter,
28 : const char* name) {
29 0 : if (counter->Enabled()) {
30 0 : object->Set(isolate->GetCurrentContext(),
31 0 : v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
32 : .ToLocalChecked(),
33 0 : v8::Number::New(isolate, *counter->GetInternalPointer()))
34 : .FromJust();
35 : }
36 0 : }
37 :
38 0 : static void AddNumber(v8::Isolate* isolate, v8::Local<v8::Object> object,
39 : double value, const char* name) {
40 : object
41 0 : ->Set(isolate->GetCurrentContext(),
42 0 : v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
43 : .ToLocalChecked(),
44 0 : v8::Number::New(isolate, value))
45 : .FromJust();
46 0 : }
47 :
48 :
49 0 : static void AddNumber64(v8::Isolate* isolate,
50 : v8::Local<v8::Object> object,
51 : int64_t value,
52 : const char* name) {
53 0 : object->Set(isolate->GetCurrentContext(),
54 0 : v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
55 : .ToLocalChecked(),
56 0 : v8::Number::New(isolate, static_cast<double>(value))).FromJust();
57 0 : }
58 :
59 :
60 0 : void StatisticsExtension::GetCounters(
61 : const v8::FunctionCallbackInfo<v8::Value>& args) {
62 : Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
63 : Heap* heap = isolate->heap();
64 :
65 0 : if (args.Length() > 0) { // GC if first argument evaluates to true.
66 0 : if (args[0]->IsBoolean() && args[0]->BooleanValue(args.GetIsolate())) {
67 : heap->CollectAllGarbage(Heap::kNoGCFlags,
68 0 : GarbageCollectionReason::kCountersExtension);
69 : }
70 : }
71 :
72 : Counters* counters = isolate->counters();
73 0 : v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
74 :
75 : struct StatisticsCounter {
76 : v8::internal::StatsCounter* counter;
77 : const char* name;
78 : };
79 : // clang-format off
80 : const StatisticsCounter counter_list[] = {
81 : #define ADD_COUNTER(name, caption) {counters->name(), #name},
82 : STATS_COUNTER_LIST_1(ADD_COUNTER)
83 : STATS_COUNTER_LIST_2(ADD_COUNTER)
84 : STATS_COUNTER_NATIVE_CODE_LIST(ADD_COUNTER)
85 : #undef ADD_COUNTER
86 0 : }; // End counter_list array.
87 : // clang-format on
88 :
89 0 : for (size_t i = 0; i < arraysize(counter_list); i++) {
90 0 : AddCounter(args.GetIsolate(), result, counter_list[i].counter,
91 0 : counter_list[i].name);
92 : }
93 :
94 : struct StatisticNumber {
95 : size_t number;
96 : const char* name;
97 : };
98 :
99 : const StatisticNumber numbers[] = {
100 : {heap->memory_allocator()->Size(), "total_committed_bytes"},
101 0 : {heap->new_space()->Size(), "new_space_live_bytes"},
102 0 : {heap->new_space()->Available(), "new_space_available_bytes"},
103 0 : {heap->new_space()->CommittedMemory(), "new_space_commited_bytes"},
104 0 : {heap->old_space()->Size(), "old_space_live_bytes"},
105 0 : {heap->old_space()->Available(), "old_space_available_bytes"},
106 0 : {heap->old_space()->CommittedMemory(), "old_space_commited_bytes"},
107 0 : {heap->code_space()->Size(), "code_space_live_bytes"},
108 0 : {heap->code_space()->Available(), "code_space_available_bytes"},
109 0 : {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
110 0 : {heap->lo_space()->Size(), "lo_space_live_bytes"},
111 0 : {heap->lo_space()->Available(), "lo_space_available_bytes"},
112 0 : {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
113 0 : {heap->code_lo_space()->Size(), "code_lo_space_live_bytes"},
114 0 : {heap->code_lo_space()->Available(), "code_lo_space_available_bytes"},
115 0 : {heap->code_lo_space()->CommittedMemory(),
116 : "code_lo_space_commited_bytes"},
117 0 : };
118 :
119 0 : for (size_t i = 0; i < arraysize(numbers); i++) {
120 0 : AddNumber(args.GetIsolate(), result, numbers[i].number, numbers[i].name);
121 : }
122 :
123 : AddNumber64(args.GetIsolate(), result, heap->external_memory(),
124 0 : "amount_of_external_allocated_memory");
125 : args.GetReturnValue().Set(result);
126 :
127 0 : HeapIterator iterator(reinterpret_cast<Isolate*>(args.GetIsolate())->heap());
128 : int reloc_info_total = 0;
129 : int source_position_table_total = 0;
130 0 : for (HeapObject obj = iterator.next(); !obj.is_null();
131 : obj = iterator.next()) {
132 0 : if (obj->IsCode()) {
133 0 : Code code = Code::cast(obj);
134 0 : reloc_info_total += code->relocation_info()->Size();
135 0 : ByteArray source_position_table = code->SourcePositionTable();
136 0 : if (source_position_table->length() > 0) {
137 0 : source_position_table_total += code->SourcePositionTable()->Size();
138 : }
139 0 : } else if (obj->IsBytecodeArray()) {
140 : source_position_table_total +=
141 0 : BytecodeArray::cast(obj)->SourcePositionTable()->Size();
142 : }
143 : }
144 :
145 0 : AddNumber(args.GetIsolate(), result, reloc_info_total,
146 0 : "reloc_info_total_size");
147 0 : AddNumber(args.GetIsolate(), result, source_position_table_total,
148 0 : "source_position_table_total_size");
149 0 : }
150 :
151 : } // namespace internal
152 121996 : } // namespace v8
|