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 "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 ASSERT(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() && args[0]->ToBoolean()->Value()) {
57 heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension");
61 Counters* counters = isolate->counters();
62 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
64 #define ADD_COUNTER(name, caption) \
65 AddCounter(args.GetIsolate(), result, counters->name(), #name);
67 STATS_COUNTER_LIST_1(ADD_COUNTER)
68 STATS_COUNTER_LIST_2(ADD_COUNTER)
70 #define ADD_COUNTER(name) \
71 AddCounter(args.GetIsolate(), result, counters->count_of_##name(), \
73 AddCounter(args.GetIsolate(), result, counters->size_of_##name(), \
76 INSTANCE_TYPE_LIST(ADD_COUNTER)
78 #define ADD_COUNTER(name) \
79 AddCounter(args.GetIsolate(), result, counters->count_of_CODE_TYPE_##name(), \
80 "count_of_CODE_TYPE_" #name); \
81 AddCounter(args.GetIsolate(), result, counters->size_of_CODE_TYPE_##name(), \
82 "size_of_CODE_TYPE_" #name);
84 CODE_KIND_LIST(ADD_COUNTER)
86 #define ADD_COUNTER(name) \
87 AddCounter(args.GetIsolate(), result, \
88 counters->count_of_FIXED_ARRAY_##name(), \
89 "count_of_FIXED_ARRAY_" #name); \
90 AddCounter(args.GetIsolate(), result, \
91 counters->size_of_FIXED_ARRAY_##name(), \
92 "size_of_FIXED_ARRAY_" #name);
94 FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
97 AddNumber(args.GetIsolate(), result, isolate->memory_allocator()->Size(),
98 "total_committed_bytes");
99 AddNumber(args.GetIsolate(), result, heap->new_space()->Size(),
100 "new_space_live_bytes");
101 AddNumber(args.GetIsolate(), result, heap->new_space()->Available(),
102 "new_space_available_bytes");
103 AddNumber(args.GetIsolate(), result, heap->new_space()->CommittedMemory(),
104 "new_space_commited_bytes");
105 AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Size(),
106 "old_pointer_space_live_bytes");
107 AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Available(),
108 "old_pointer_space_available_bytes");
109 AddNumber(args.GetIsolate(), result,
110 heap->old_pointer_space()->CommittedMemory(),
111 "old_pointer_space_commited_bytes");
112 AddNumber(args.GetIsolate(), result, heap->old_data_space()->Size(),
113 "old_data_space_live_bytes");
114 AddNumber(args.GetIsolate(), result, heap->old_data_space()->Available(),
115 "old_data_space_available_bytes");
116 AddNumber(args.GetIsolate(), result,
117 heap->old_data_space()->CommittedMemory(),
118 "old_data_space_commited_bytes");
119 AddNumber(args.GetIsolate(), result, heap->code_space()->Size(),
120 "code_space_live_bytes");
121 AddNumber(args.GetIsolate(), result, heap->code_space()->Available(),
122 "code_space_available_bytes");
123 AddNumber(args.GetIsolate(), result, heap->code_space()->CommittedMemory(),
124 "code_space_commited_bytes");
125 AddNumber(args.GetIsolate(), result, heap->cell_space()->Size(),
126 "cell_space_live_bytes");
127 AddNumber(args.GetIsolate(), result, heap->cell_space()->Available(),
128 "cell_space_available_bytes");
129 AddNumber(args.GetIsolate(), result, heap->cell_space()->CommittedMemory(),
130 "cell_space_commited_bytes");
131 AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Size(),
132 "property_cell_space_live_bytes");
133 AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Available(),
134 "property_cell_space_available_bytes");
135 AddNumber(args.GetIsolate(), result,
136 heap->property_cell_space()->CommittedMemory(),
137 "property_cell_space_commited_bytes");
138 AddNumber(args.GetIsolate(), result, heap->lo_space()->Size(),
139 "lo_space_live_bytes");
140 AddNumber(args.GetIsolate(), result, heap->lo_space()->Available(),
141 "lo_space_available_bytes");
142 AddNumber(args.GetIsolate(), result, heap->lo_space()->CommittedMemory(),
143 "lo_space_commited_bytes");
144 AddNumber64(args.GetIsolate(), result,
145 heap->amount_of_external_allocated_memory(),
146 "amount_of_external_allocated_memory");
147 args.GetReturnValue().Set(result);
150 } } // namespace v8::internal