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 : object->Set(isolate->GetCurrentContext(),
31 : v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
32 : .ToLocalChecked(),
33 0 : v8::Number::New(isolate, *counter->GetInternalPointer()))
34 0 : .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 : ->Set(isolate->GetCurrentContext(),
42 : v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
43 : .ToLocalChecked(),
44 0 : v8::Number::New(isolate, value))
45 0 : .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 : object->Set(isolate->GetCurrentContext(),
54 : 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 0 : const v8::FunctionCallbackInfo<v8::Value>& args) {
62 : Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
63 0 : 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 : const StatisticsCounter counter_list[] = {
80 : #define ADD_COUNTER(name, caption) \
81 : { counters->name(), #name } \
82 : ,
83 :
84 0 : STATS_COUNTER_LIST_1(ADD_COUNTER) STATS_COUNTER_LIST_2(ADD_COUNTER)
85 : #undef ADD_COUNTER
86 0 : }; // End counter_list array.
87 :
88 0 : for (size_t i = 0; i < arraysize(counter_list); i++) {
89 : AddCounter(args.GetIsolate(), result, counter_list[i].counter,
90 0 : 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 0 : {heap->new_space()->Size(), "new_space_live_bytes"},
101 0 : {heap->new_space()->Available(), "new_space_available_bytes"},
102 0 : {heap->new_space()->CommittedMemory(), "new_space_commited_bytes"},
103 0 : {heap->old_space()->Size(), "old_space_live_bytes"},
104 0 : {heap->old_space()->Available(), "old_space_available_bytes"},
105 0 : {heap->old_space()->CommittedMemory(), "old_space_commited_bytes"},
106 0 : {heap->code_space()->Size(), "code_space_live_bytes"},
107 0 : {heap->code_space()->Available(), "code_space_available_bytes"},
108 0 : {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
109 0 : {heap->lo_space()->Size(), "lo_space_live_bytes"},
110 0 : {heap->lo_space()->Available(), "lo_space_available_bytes"},
111 0 : {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
112 0 : {heap->code_lo_space()->Size(), "code_lo_space_live_bytes"},
113 0 : {heap->code_lo_space()->Available(), "code_lo_space_available_bytes"},
114 0 : {heap->code_lo_space()->CommittedMemory(),
115 : "code_lo_space_commited_bytes"},
116 0 : };
117 :
118 0 : for (size_t i = 0; i < arraysize(numbers); i++) {
119 0 : AddNumber(args.GetIsolate(), result, numbers[i].number, numbers[i].name);
120 : }
121 :
122 : AddNumber64(args.GetIsolate(), result, heap->external_memory(),
123 0 : "amount_of_external_allocated_memory");
124 : args.GetReturnValue().Set(result);
125 :
126 0 : HeapIterator iterator(reinterpret_cast<Isolate*>(args.GetIsolate())->heap());
127 : int reloc_info_total = 0;
128 : int source_position_table_total = 0;
129 0 : for (HeapObject obj = iterator.next(); !obj.is_null();
130 : obj = iterator.next()) {
131 0 : if (obj->IsCode()) {
132 0 : Code code = Code::cast(obj);
133 0 : reloc_info_total += code->relocation_info()->Size();
134 0 : ByteArray source_position_table = code->SourcePositionTable();
135 0 : if (source_position_table->length() > 0) {
136 0 : source_position_table_total += code->SourcePositionTable()->Size();
137 : }
138 0 : } else if (obj->IsBytecodeArray()) {
139 : source_position_table_total +=
140 0 : BytecodeArray::cast(obj)->SourcePositionTable()->Size();
141 : }
142 : }
143 :
144 : AddNumber(args.GetIsolate(), result, reloc_info_total,
145 0 : "reloc_info_total_size");
146 : AddNumber(args.GetIsolate(), result, source_position_table_total,
147 0 : "source_position_table_total_size");
148 0 : }
149 :
150 : } // namespace internal
151 183867 : } // namespace v8
|