yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "statistics-extension.h" |
| 29 | |
| 30 | namespace v8 { |
| 31 | namespace internal { |
| 32 | |
| 33 | const char* const StatisticsExtension::kSource = |
| 34 | "native function getV8Statistics();"; |
| 35 | |
| 36 | |
machenbach@chromium.org | 9f18d91 | 2013-11-28 13:42:41 +0000 | [diff] [blame] | 37 | v8::Handle<v8::FunctionTemplate> StatisticsExtension::GetNativeFunctionTemplate( |
| 38 | v8::Isolate* isolate, |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 39 | v8::Handle<v8::String> str) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 40 | ASSERT(strcmp(*v8::String::Utf8Value(str), "getV8Statistics") == 0); |
hpayer@chromium.org | 4f99be9 | 2013-12-18 16:23:55 +0000 | [diff] [blame] | 41 | return v8::FunctionTemplate::New(isolate, StatisticsExtension::GetCounters); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 45 | static void AddCounter(v8::Isolate* isolate, |
| 46 | v8::Local<v8::Object> object, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 47 | StatsCounter* counter, |
| 48 | const char* name) { |
| 49 | if (counter->Enabled()) { |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 50 | object->Set(v8::String::NewFromUtf8(isolate, name), |
ulan@chromium.org | 0f13e74 | 2014-01-03 15:51:11 +0000 | [diff] [blame] | 51 | v8::Number::New(isolate, *counter->GetInternalPointer())); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 55 | static void AddNumber(v8::Isolate* isolate, |
| 56 | v8::Local<v8::Object> object, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 57 | intptr_t value, |
| 58 | const char* name) { |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 59 | object->Set(v8::String::NewFromUtf8(isolate, name), |
ulan@chromium.org | 0f13e74 | 2014-01-03 15:51:11 +0000 | [diff] [blame] | 60 | v8::Number::New(isolate, static_cast<double>(value))); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 64 | static void AddNumber64(v8::Isolate* isolate, |
| 65 | v8::Local<v8::Object> object, |
machenbach@chromium.org | 7ff7607 | 2013-11-21 09:47:43 +0000 | [diff] [blame] | 66 | int64_t value, |
| 67 | const char* name) { |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 68 | object->Set(v8::String::NewFromUtf8(isolate, name), |
ulan@chromium.org | 0f13e74 | 2014-01-03 15:51:11 +0000 | [diff] [blame] | 69 | v8::Number::New(isolate, static_cast<double>(value))); |
machenbach@chromium.org | 7ff7607 | 2013-11-21 09:47:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 73 | void StatisticsExtension::GetCounters( |
| 74 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 75 | Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 76 | Heap* heap = isolate->heap(); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 77 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 78 | if (args.Length() > 0) { // GC if first argument evaluates to true. |
| 79 | if (args[0]->IsBoolean() && args[0]->ToBoolean()->Value()) { |
| 80 | heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Counters* counters = isolate->counters(); |
ulan@chromium.org | 0f13e74 | 2014-01-03 15:51:11 +0000 | [diff] [blame] | 85 | v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 86 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 87 | #define ADD_COUNTER(name, caption) \ |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 88 | AddCounter(args.GetIsolate(), result, counters->name(), #name); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 89 | |
| 90 | STATS_COUNTER_LIST_1(ADD_COUNTER) |
| 91 | STATS_COUNTER_LIST_2(ADD_COUNTER) |
| 92 | #undef ADD_COUNTER |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 93 | #define ADD_COUNTER(name) \ |
| 94 | AddCounter(args.GetIsolate(), result, counters->count_of_##name(), \ |
| 95 | "count_of_" #name); \ |
| 96 | AddCounter(args.GetIsolate(), result, counters->size_of_##name(), \ |
| 97 | "size_of_" #name); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 98 | |
| 99 | INSTANCE_TYPE_LIST(ADD_COUNTER) |
| 100 | #undef ADD_COUNTER |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 101 | #define ADD_COUNTER(name) \ |
| 102 | AddCounter(args.GetIsolate(), result, counters->count_of_CODE_TYPE_##name(), \ |
| 103 | "count_of_CODE_TYPE_" #name); \ |
| 104 | AddCounter(args.GetIsolate(), result, counters->size_of_CODE_TYPE_##name(), \ |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 105 | "size_of_CODE_TYPE_" #name); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 106 | |
| 107 | CODE_KIND_LIST(ADD_COUNTER) |
| 108 | #undef ADD_COUNTER |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 109 | #define ADD_COUNTER(name) \ |
| 110 | AddCounter(args.GetIsolate(), result, \ |
| 111 | counters->count_of_FIXED_ARRAY_##name(), \ |
| 112 | "count_of_FIXED_ARRAY_" #name); \ |
| 113 | AddCounter(args.GetIsolate(), result, \ |
| 114 | counters->size_of_FIXED_ARRAY_##name(), \ |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 115 | "size_of_FIXED_ARRAY_" #name); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 116 | |
| 117 | FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER) |
| 118 | #undef ADD_COUNTER |
| 119 | |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 120 | AddNumber(args.GetIsolate(), result, isolate->memory_allocator()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 121 | "total_committed_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 122 | AddNumber(args.GetIsolate(), result, heap->new_space()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 123 | "new_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 124 | AddNumber(args.GetIsolate(), result, heap->new_space()->Available(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 125 | "new_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 126 | AddNumber(args.GetIsolate(), result, heap->new_space()->CommittedMemory(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 127 | "new_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 128 | AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 129 | "old_pointer_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 130 | AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Available(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 131 | "old_pointer_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 132 | AddNumber(args.GetIsolate(), result, |
| 133 | heap->old_pointer_space()->CommittedMemory(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 134 | "old_pointer_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 135 | AddNumber(args.GetIsolate(), result, heap->old_data_space()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 136 | "old_data_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 137 | AddNumber(args.GetIsolate(), result, heap->old_data_space()->Available(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 138 | "old_data_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 139 | AddNumber(args.GetIsolate(), result, |
| 140 | heap->old_data_space()->CommittedMemory(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 141 | "old_data_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 142 | AddNumber(args.GetIsolate(), result, heap->code_space()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 143 | "code_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 144 | AddNumber(args.GetIsolate(), result, heap->code_space()->Available(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 145 | "code_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 146 | AddNumber(args.GetIsolate(), result, heap->code_space()->CommittedMemory(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 147 | "code_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 148 | AddNumber(args.GetIsolate(), result, heap->cell_space()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 149 | "cell_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 150 | AddNumber(args.GetIsolate(), result, heap->cell_space()->Available(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 151 | "cell_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 152 | AddNumber(args.GetIsolate(), result, heap->cell_space()->CommittedMemory(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 153 | "cell_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 154 | AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Size(), |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 155 | "property_cell_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 156 | AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Available(), |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 157 | "property_cell_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 158 | AddNumber(args.GetIsolate(), result, |
| 159 | heap->property_cell_space()->CommittedMemory(), |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 160 | "property_cell_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 161 | AddNumber(args.GetIsolate(), result, heap->lo_space()->Size(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 162 | "lo_space_live_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 163 | AddNumber(args.GetIsolate(), result, heap->lo_space()->Available(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 164 | "lo_space_available_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 165 | AddNumber(args.GetIsolate(), result, heap->lo_space()->CommittedMemory(), |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 166 | "lo_space_commited_bytes"); |
machenbach@chromium.org | f984189 | 2013-11-25 12:01:13 +0000 | [diff] [blame] | 167 | AddNumber64(args.GetIsolate(), result, |
| 168 | heap->amount_of_external_allocated_memory(), |
machenbach@chromium.org | 7ff7607 | 2013-11-21 09:47:43 +0000 | [diff] [blame] | 169 | "amount_of_external_allocated_memory"); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 170 | args.GetReturnValue().Set(result); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 171 | } |
| 172 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 173 | } } // namespace v8::internal |