deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / extensions / statistics-extension.cc
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 namespace v8 {
8 namespace internal {
9
10 const char* const StatisticsExtension::kSource =
11     "native function getV8Statistics();";
12
13
14 v8::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
22 static 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
32 static 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
41 static 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
50 void 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.
56     if (args[0]->IsBoolean() &&
57         args[0]->ToBoolean(args.GetIsolate())->Value()) {
58       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   struct StatisticsCounter {
66     v8::internal::StatsCounter* counter;
67     const char* name;
68   };
69   const StatisticsCounter counter_list[] = {
70 #define ADD_COUNTER(name, caption) \
71   { counters->name(), #name }      \
72   ,
73
74       STATS_COUNTER_LIST_1(ADD_COUNTER) STATS_COUNTER_LIST_2(ADD_COUNTER)
75 #undef ADD_COUNTER
76 #define ADD_COUNTER(name)                            \
77   { counters->count_of_##name(), "count_of_" #name } \
78   , {counters->size_of_##name(), "size_of_" #name},
79
80           INSTANCE_TYPE_LIST(ADD_COUNTER)
81 #undef 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},
85
86               CODE_KIND_LIST(ADD_COUNTER)
87 #undef 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},
91
92                   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
93 #undef ADD_COUNTER
94   };  // End counter_list array.
95
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);
99   }
100
101   struct StatisticNumber {
102     intptr_t number;
103     const char* name;
104   };
105
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"},
129   };
130
131   for (size_t i = 0; i < arraysize(numbers); i++) {
132     AddNumber(args.GetIsolate(), result, numbers[i].number, numbers[i].name);
133   }
134
135   AddNumber64(args.GetIsolate(), result,
136               heap->amount_of_external_allocated_memory(),
137               "amount_of_external_allocated_memory");
138   args.GetReturnValue().Set(result);
139 }
140
141 } }  // namespace v8::internal