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::GetNativeFunctionTemplate(
     38     v8::Isolate* isolate,
     39     v8::Handle<v8::String> str) {
     40   ASSERT(strcmp(*v8::String::Utf8Value(str), "getV8Statistics") == 0);
     41   return v8::FunctionTemplate::New(StatisticsExtension::GetCounters);
     42 }
     43 
     44 
     45 static void AddCounter(v8::Isolate* isolate,
     46                        v8::Local<v8::Object> object,
     47                        StatsCounter* counter,
     48                        const char* name) {
     49   if (counter->Enabled()) {
     50     object->Set(v8::String::NewFromUtf8(isolate, name),
     51                 v8::Number::New(*counter->GetInternalPointer()));
     52   }
     53 }
     54 
     55 static void AddNumber(v8::Isolate* isolate,
     56                       v8::Local<v8::Object> object,
     57                       intptr_t value,
     58                       const char* name) {
     59   object->Set(v8::String::NewFromUtf8(isolate, name),
     60               v8::Number::New(static_cast<double>(value)));
     61 }
     62 
     63 
     64 static void AddNumber64(v8::Isolate* isolate,
     65                         v8::Local<v8::Object> object,
     66                         int64_t value,
     67                         const char* name) {
     68   object->Set(v8::String::NewFromUtf8(isolate, name),
     69               v8::Number::New(static_cast<double>(value)));
     70 }
     71 
     72 
     73 void StatisticsExtension::GetCounters(
     74     const v8::FunctionCallbackInfo<v8::Value>& args) {
     75   Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
     76   Heap* heap = isolate->heap();
     77 
     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();
     85   v8::Local<v8::Object> result = v8::Object::New();
     86 
     87 #define ADD_COUNTER(name, caption)                                            \
     88   AddCounter(args.GetIsolate(), result, counters->name(), #name);
     89 
     90   STATS_COUNTER_LIST_1(ADD_COUNTER)
     91   STATS_COUNTER_LIST_2(ADD_COUNTER)
     92 #undef ADD_COUNTER
     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);
     98 
     99   INSTANCE_TYPE_LIST(ADD_COUNTER)
    100 #undef ADD_COUNTER
    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(),  \
    105              "size_of_CODE_TYPE_" #name);
    106 
    107   CODE_KIND_LIST(ADD_COUNTER)
    108 #undef ADD_COUNTER
    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(),                           \
    115              "size_of_FIXED_ARRAY_" #name);
    116 
    117   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
    118 #undef ADD_COUNTER
    119 
    120   AddNumber(args.GetIsolate(), result, isolate->memory_allocator()->Size(),
    121             "total_committed_bytes");
    122   AddNumber(args.GetIsolate(), result, heap->new_space()->Size(),
    123             "new_space_live_bytes");
    124   AddNumber(args.GetIsolate(), result, heap->new_space()->Available(),
    125             "new_space_available_bytes");
    126   AddNumber(args.GetIsolate(), result, heap->new_space()->CommittedMemory(),
    127             "new_space_commited_bytes");
    128   AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Size(),
    129             "old_pointer_space_live_bytes");
    130   AddNumber(args.GetIsolate(), result, heap->old_pointer_space()->Available(),
    131             "old_pointer_space_available_bytes");
    132   AddNumber(args.GetIsolate(), result,
    133             heap->old_pointer_space()->CommittedMemory(),
    134             "old_pointer_space_commited_bytes");
    135   AddNumber(args.GetIsolate(), result, heap->old_data_space()->Size(),
    136             "old_data_space_live_bytes");
    137   AddNumber(args.GetIsolate(), result, heap->old_data_space()->Available(),
    138             "old_data_space_available_bytes");
    139   AddNumber(args.GetIsolate(), result,
    140             heap->old_data_space()->CommittedMemory(),
    141             "old_data_space_commited_bytes");
    142   AddNumber(args.GetIsolate(), result, heap->code_space()->Size(),
    143             "code_space_live_bytes");
    144   AddNumber(args.GetIsolate(), result, heap->code_space()->Available(),
    145             "code_space_available_bytes");
    146   AddNumber(args.GetIsolate(), result, heap->code_space()->CommittedMemory(),
    147             "code_space_commited_bytes");
    148   AddNumber(args.GetIsolate(), result, heap->cell_space()->Size(),
    149             "cell_space_live_bytes");
    150   AddNumber(args.GetIsolate(), result, heap->cell_space()->Available(),
    151             "cell_space_available_bytes");
    152   AddNumber(args.GetIsolate(), result, heap->cell_space()->CommittedMemory(),
    153             "cell_space_commited_bytes");
    154   AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Size(),
    155             "property_cell_space_live_bytes");
    156   AddNumber(args.GetIsolate(), result, heap->property_cell_space()->Available(),
    157             "property_cell_space_available_bytes");
    158   AddNumber(args.GetIsolate(), result,
    159             heap->property_cell_space()->CommittedMemory(),
    160             "property_cell_space_commited_bytes");
    161   AddNumber(args.GetIsolate(), result, heap->lo_space()->Size(),
    162             "lo_space_live_bytes");
    163   AddNumber(args.GetIsolate(), result, heap->lo_space()->Available(),
    164             "lo_space_available_bytes");
    165   AddNumber(args.GetIsolate(), result, heap->lo_space()->CommittedMemory(),
    166             "lo_space_commited_bytes");
    167   AddNumber64(args.GetIsolate(), result,
    168               heap->amount_of_external_allocated_memory(),
    169               "amount_of_external_allocated_memory");
    170   args.GetReturnValue().Set(result);
    171 }
    172 
    173 
    174 void StatisticsExtension::Register() {
    175   static StatisticsExtension statistics_extension;
    176   static v8::DeclareExtension declaration(&statistics_extension);
    177 }
    178 
    179 } }  // namespace v8::internal
    180