Home | History | Annotate | Download | only in extensions
      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 
     37 v8::Handle<v8::FunctionTemplate> StatisticsExtension::GetNativeFunction(
     38     v8::Handle<v8::String> str) {
     39   ASSERT(strcmp(*v8::String::Utf8Value(str), "getV8Statistics") == 0);
     40   return v8::FunctionTemplate::New(StatisticsExtension::GetCounters);
     41 }
     42 
     43 
     44 static void AddCounter(v8::Local<v8::Object> object,
     45                        StatsCounter* counter,
     46                        const char* name) {
     47   if (counter->Enabled()) {
     48     object->Set(v8::String::New(name),
     49                 v8::Number::New(*counter->GetInternalPointer()));
     50   }
     51 }
     52 
     53 static void AddNumber(v8::Local<v8::Object> object,
     54                       intptr_t value,
     55                       const char* name) {
     56   object->Set(v8::String::New(name),
     57               v8::Number::New(static_cast<double>(value)));
     58 }
     59 
     60 
     61 void StatisticsExtension::GetCounters(
     62     const v8::FunctionCallbackInfo<v8::Value>& args) {
     63   Isolate* isolate = Isolate::Current();
     64   Heap* heap = isolate->heap();
     65 
     66   if (args.Length() > 0) {  // GC if first argument evaluates to true.
     67     if (args[0]->IsBoolean() && args[0]->ToBoolean()->Value()) {
     68       heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension");
     69     }
     70   }
     71 
     72   Counters* counters = isolate->counters();
     73   v8::Local<v8::Object> result = v8::Object::New();
     74 
     75 #define ADD_COUNTER(name, caption)                                            \
     76   AddCounter(result, counters->name(), #name);
     77 
     78   STATS_COUNTER_LIST_1(ADD_COUNTER)
     79   STATS_COUNTER_LIST_2(ADD_COUNTER)
     80 #undef ADD_COUNTER
     81 #define ADD_COUNTER(name)                                                     \
     82   AddCounter(result, counters->count_of_##name(), "count_of_" #name);         \
     83   AddCounter(result, counters->size_of_##name(),  "size_of_" #name);
     84 
     85   INSTANCE_TYPE_LIST(ADD_COUNTER)
     86 #undef ADD_COUNTER
     87 #define ADD_COUNTER(name)                                                     \
     88   AddCounter(result, counters->count_of_CODE_TYPE_##name(),                   \
     89              "count_of_CODE_TYPE_" #name);                                    \
     90   AddCounter(result, counters->size_of_CODE_TYPE_##name(),                    \
     91              "size_of_CODE_TYPE_" #name);
     92 
     93   CODE_KIND_LIST(ADD_COUNTER)
     94 #undef ADD_COUNTER
     95 #define ADD_COUNTER(name)                                                     \
     96   AddCounter(result, counters->count_of_FIXED_ARRAY_##name(),                 \
     97              "count_of_FIXED_ARRAY_" #name);                                  \
     98   AddCounter(result, counters->size_of_FIXED_ARRAY_##name(),                  \
     99              "size_of_FIXED_ARRAY_" #name);
    100 
    101   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
    102 #undef ADD_COUNTER
    103 
    104   AddNumber(result, isolate->memory_allocator()->Size(),
    105             "total_committed_bytes");
    106   AddNumber(result, heap->new_space()->Size(),
    107             "new_space_live_bytes");
    108   AddNumber(result, heap->new_space()->Available(),
    109             "new_space_available_bytes");
    110   AddNumber(result, heap->new_space()->CommittedMemory(),
    111             "new_space_commited_bytes");
    112   AddNumber(result, heap->old_pointer_space()->Size(),
    113             "old_pointer_space_live_bytes");
    114   AddNumber(result, heap->old_pointer_space()->Available(),
    115             "old_pointer_space_available_bytes");
    116   AddNumber(result, heap->old_pointer_space()->CommittedMemory(),
    117             "old_pointer_space_commited_bytes");
    118   AddNumber(result, heap->old_data_space()->Size(),
    119             "old_data_space_live_bytes");
    120   AddNumber(result, heap->old_data_space()->Available(),
    121             "old_data_space_available_bytes");
    122   AddNumber(result, heap->old_data_space()->CommittedMemory(),
    123             "old_data_space_commited_bytes");
    124   AddNumber(result, heap->code_space()->Size(),
    125             "code_space_live_bytes");
    126   AddNumber(result, heap->code_space()->Available(),
    127             "code_space_available_bytes");
    128   AddNumber(result, heap->code_space()->CommittedMemory(),
    129             "code_space_commited_bytes");
    130   AddNumber(result, heap->cell_space()->Size(),
    131             "cell_space_live_bytes");
    132   AddNumber(result, heap->cell_space()->Available(),
    133             "cell_space_available_bytes");
    134   AddNumber(result, heap->cell_space()->CommittedMemory(),
    135             "cell_space_commited_bytes");
    136   AddNumber(result, heap->property_cell_space()->Size(),
    137             "property_cell_space_live_bytes");
    138   AddNumber(result, heap->property_cell_space()->Available(),
    139             "property_cell_space_available_bytes");
    140   AddNumber(result, heap->property_cell_space()->CommittedMemory(),
    141             "property_cell_space_commited_bytes");
    142   AddNumber(result, heap->lo_space()->Size(),
    143             "lo_space_live_bytes");
    144   AddNumber(result, heap->lo_space()->Available(),
    145             "lo_space_available_bytes");
    146   AddNumber(result, heap->lo_space()->CommittedMemory(),
    147             "lo_space_commited_bytes");
    148   AddNumber(result, heap->amount_of_external_allocated_memory(),
    149             "amount_of_external_allocated_memory");
    150   args.GetReturnValue().Set(result);
    151 }
    152 
    153 
    154 void StatisticsExtension::Register() {
    155   static StatisticsExtension statistics_extension;
    156   static v8::DeclareExtension declaration(&statistics_extension);
    157 }
    158 
    159 } }  // namespace v8::internal
    160