blob: bb5ee333f67ccbab5998b222a7c6a257b76c1588 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
7namespace v8 {
8namespace internal {
9
10const char* const StatisticsExtension::kSource =
11 "native function getV8Statistics();";
12
13
14v8::Handle<v8::FunctionTemplate> StatisticsExtension::GetNativeFunctionTemplate(
15 v8::Isolate* isolate,
16 v8::Handle<v8::String> str) {
17 DCHECK(strcmp(*v8::String::Utf8Value(str), "getV8Statistics") == 0);
18 return v8::FunctionTemplate::New(isolate, StatisticsExtension::GetCounters);
19}
20
21
22static void AddCounter(v8::Isolate* isolate,
23 v8::Local<v8::Object> object,
24 StatsCounter* counter,
25 const char* name) {
26 if (counter->Enabled()) {
27 object->Set(v8::String::NewFromUtf8(isolate, name),
28 v8::Number::New(isolate, *counter->GetInternalPointer()));
29 }
30}
31
32static void AddNumber(v8::Isolate* isolate,
33 v8::Local<v8::Object> object,
34 intptr_t value,
35 const char* name) {
36 object->Set(v8::String::NewFromUtf8(isolate, name),
37 v8::Number::New(isolate, static_cast<double>(value)));
38}
39
40
41static void AddNumber64(v8::Isolate* isolate,
42 v8::Local<v8::Object> object,
43 int64_t value,
44 const char* name) {
45 object->Set(v8::String::NewFromUtf8(isolate, name),
46 v8::Number::New(isolate, static_cast<double>(value)));
47}
48
49
50void StatisticsExtension::GetCounters(
51 const v8::FunctionCallbackInfo<v8::Value>& args) {
52 Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
53 Heap* heap = isolate->heap();
54
55 if (args.Length() > 0) { // GC if first argument evaluates to true.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 if (args[0]->IsBoolean() &&
57 args[0]->ToBoolean(args.GetIsolate())->Value()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension");
59 }
60 }
61
62 Counters* counters = isolate->counters();
63 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
64
65#define ADD_COUNTER(name, caption) \
66 AddCounter(args.GetIsolate(), result, counters->name(), #name);
67
68 STATS_COUNTER_LIST_1(ADD_COUNTER)
69 STATS_COUNTER_LIST_2(ADD_COUNTER)
70#undef ADD_COUNTER
71#define ADD_COUNTER(name) \
72 AddCounter(args.GetIsolate(), result, counters->count_of_##name(), \
73 "count_of_" #name); \
74 AddCounter(args.GetIsolate(), result, counters->size_of_##name(), \
75 "size_of_" #name);
76
77 INSTANCE_TYPE_LIST(ADD_COUNTER)
78#undef ADD_COUNTER
79#define ADD_COUNTER(name) \
80 AddCounter(args.GetIsolate(), result, counters->count_of_CODE_TYPE_##name(), \
81 "count_of_CODE_TYPE_" #name); \
82 AddCounter(args.GetIsolate(), result, counters->size_of_CODE_TYPE_##name(), \
83 "size_of_CODE_TYPE_" #name);
84
85 CODE_KIND_LIST(ADD_COUNTER)
86#undef ADD_COUNTER
87#define ADD_COUNTER(name) \
88 AddCounter(args.GetIsolate(), result, \
89 counters->count_of_FIXED_ARRAY_##name(), \
90 "count_of_FIXED_ARRAY_" #name); \
91 AddCounter(args.GetIsolate(), result, \
92 counters->size_of_FIXED_ARRAY_##name(), \
93 "size_of_FIXED_ARRAY_" #name);
94
95 FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
96#undef ADD_COUNTER
97
98 AddNumber(args.GetIsolate(), result, isolate->memory_allocator()->Size(),
99 "total_committed_bytes");
100 AddNumber(args.GetIsolate(), result, heap->new_space()->Size(),
101 "new_space_live_bytes");
102 AddNumber(args.GetIsolate(), result, heap->new_space()->Available(),
103 "new_space_available_bytes");
104 AddNumber(args.GetIsolate(), result, heap->new_space()->CommittedMemory(),
105 "new_space_commited_bytes");
106 AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Size(),
107 "old_pointer_space_live_bytes");
108 AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Available(),
109 "old_pointer_space_available_bytes");
110 AddNumber(args.GetIsolate(), result,
111 heap->old_pointer_space()->CommittedMemory(),
112 "old_pointer_space_commited_bytes");
113 AddNumber(args.GetIsolate(), result, heap->old_data_space()->Size(),
114 "old_data_space_live_bytes");
115 AddNumber(args.GetIsolate(), result, heap->old_data_space()->Available(),
116 "old_data_space_available_bytes");
117 AddNumber(args.GetIsolate(), result,
118 heap->old_data_space()->CommittedMemory(),
119 "old_data_space_commited_bytes");
120 AddNumber(args.GetIsolate(), result, heap->code_space()->Size(),
121 "code_space_live_bytes");
122 AddNumber(args.GetIsolate(), result, heap->code_space()->Available(),
123 "code_space_available_bytes");
124 AddNumber(args.GetIsolate(), result, heap->code_space()->CommittedMemory(),
125 "code_space_commited_bytes");
126 AddNumber(args.GetIsolate(), result, heap->cell_space()->Size(),
127 "cell_space_live_bytes");
128 AddNumber(args.GetIsolate(), result, heap->cell_space()->Available(),
129 "cell_space_available_bytes");
130 AddNumber(args.GetIsolate(), result, heap->cell_space()->CommittedMemory(),
131 "cell_space_commited_bytes");
132 AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Size(),
133 "property_cell_space_live_bytes");
134 AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Available(),
135 "property_cell_space_available_bytes");
136 AddNumber(args.GetIsolate(), result,
137 heap->property_cell_space()->CommittedMemory(),
138 "property_cell_space_commited_bytes");
139 AddNumber(args.GetIsolate(), result, heap->lo_space()->Size(),
140 "lo_space_live_bytes");
141 AddNumber(args.GetIsolate(), result, heap->lo_space()->Available(),
142 "lo_space_available_bytes");
143 AddNumber(args.GetIsolate(), result, heap->lo_space()->CommittedMemory(),
144 "lo_space_commited_bytes");
145 AddNumber64(args.GetIsolate(), result,
146 heap->amount_of_external_allocated_memory(),
147 "amount_of_external_allocated_memory");
148 args.GetReturnValue().Set(result);
149}
150
151} } // namespace v8::internal