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.
5 #include "src/extensions/statistics-extension.h"
10 const char* const StatisticsExtension::kSource =
11 "native function getV8Statistics();";
14 v8::Handle<v8::FunctionTemplate> StatisticsExtension::GetNativeFunctionTemplate(
16 v8::Handle<v8::String> str) {
17 DCHECK(strcmp(*v8::String::Utf8Value(str), "getV8Statistics") == 0);
18 return v8::FunctionTemplate::New(isolate, StatisticsExtension::GetCounters);
22 static void AddCounter(v8::Isolate* isolate,
23 v8::Local<v8::Object> object,
24 StatsCounter* counter,
26 if (counter->Enabled()) {
27 object->Set(v8::String::NewFromUtf8(isolate, name),
28 v8::Number::New(isolate, *counter->GetInternalPointer()));
32 static void AddNumber(v8::Isolate* isolate,
33 v8::Local<v8::Object> object,
36 object->Set(v8::String::NewFromUtf8(isolate, name),
37 v8::Number::New(isolate, static_cast<double>(value)));
41 static void AddNumber64(v8::Isolate* isolate,
42 v8::Local<v8::Object> object,
45 object->Set(v8::String::NewFromUtf8(isolate, name),
46 v8::Number::New(isolate, static_cast<double>(value)));
50 void StatisticsExtension::GetCounters(
51 const v8::FunctionCallbackInfo<v8::Value>& args) {
52 Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
53 Heap* heap = isolate->heap();
55 if (args.Length() > 0) { // GC if first argument evaluates to true.
56 if (args[0]->IsBoolean() &&
57 args[0]->ToBoolean(args.GetIsolate())->Value()) {
58 heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension");
62 Counters* counters = isolate->counters();
63 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
65 struct StatisticsCounter {
66 v8::internal::StatsCounter* counter;
69 const StatisticsCounter counter_list[] = {
70 #define ADD_COUNTER(name, caption) \
71 { counters->name(), #name } \
74 STATS_COUNTER_LIST_1(ADD_COUNTER) STATS_COUNTER_LIST_2(ADD_COUNTER)
76 #define ADD_COUNTER(name) \
77 { counters->count_of_##name(), "count_of_" #name } \
78 , {counters->size_of_##name(), "size_of_" #name},
80 INSTANCE_TYPE_LIST(ADD_COUNTER)
82 #define ADD_COUNTER(name) \
83 { counters->count_of_CODE_TYPE_##name(), "count_of_CODE_TYPE_" #name } \
84 , {counters->size_of_CODE_TYPE_##name(), "size_of_CODE_TYPE_" #name},
86 CODE_KIND_LIST(ADD_COUNTER)
88 #define ADD_COUNTER(name) \
89 { counters->count_of_FIXED_ARRAY_##name(), "count_of_FIXED_ARRAY_" #name } \
90 , {counters->size_of_FIXED_ARRAY_##name(), "size_of_FIXED_ARRAY_" #name},
92 FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
94 }; // End counter_list array.
96 for (size_t i = 0; i < arraysize(counter_list); i++) {
97 AddCounter(args.GetIsolate(), result, counter_list[i].counter,
98 counter_list[i].name);
101 struct StatisticNumber {
106 const StatisticNumber numbers[] = {
107 {isolate->memory_allocator()->Size(), "total_committed_bytes"},
108 {heap->new_space()->Size(), "new_space_live_bytes"},
109 {heap->new_space()->Available(), "new_space_available_bytes"},
110 {heap->new_space()->CommittedMemory(), "new_space_commited_bytes"},
111 {heap->old_pointer_space()->Size(), "old_pointer_space_live_bytes"},
112 {heap->old_pointer_space()->Available(),
113 "old_pointer_space_available_bytes"},
114 {heap->old_pointer_space()->CommittedMemory(),
115 "old_pointer_space_commited_bytes"},
116 {heap->old_data_space()->Size(), "old_data_space_live_bytes"},
117 {heap->old_data_space()->Available(), "old_data_space_available_bytes"},
118 {heap->old_data_space()->CommittedMemory(),
119 "old_data_space_commited_bytes"},
120 {heap->code_space()->Size(), "code_space_live_bytes"},
121 {heap->code_space()->Available(), "code_space_available_bytes"},
122 {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
123 {heap->cell_space()->Size(), "cell_space_live_bytes"},
124 {heap->cell_space()->Available(), "cell_space_available_bytes"},
125 {heap->cell_space()->CommittedMemory(), "cell_space_commited_bytes"},
126 {heap->lo_space()->Size(), "lo_space_live_bytes"},
127 {heap->lo_space()->Available(), "lo_space_available_bytes"},
128 {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
131 for (size_t i = 0; i < arraysize(numbers); i++) {
132 AddNumber(args.GetIsolate(), result, numbers[i].number, numbers[i].name);
135 AddNumber64(args.GetIsolate(), result,
136 heap->amount_of_external_allocated_memory(),
137 "amount_of_external_allocated_memory");
138 args.GetReturnValue().Set(result);
141 } } // namespace v8::internal