statistics-extension.cc 6.09 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/extensions/statistics-extension.h"
6

7 8 9
#include "src/counters.h"
#include "src/heap/heap-inl.h"
#include "src/isolate.h"
10

11 12 13 14 15 16 17
namespace v8 {
namespace internal {

const char* const StatisticsExtension::kSource =
    "native function getV8Statistics();";


18 19
v8::Local<v8::FunctionTemplate> StatisticsExtension::GetNativeFunctionTemplate(
    v8::Isolate* isolate, v8::Local<v8::String> str) {
20
  DCHECK(strcmp(*v8::String::Utf8Value(isolate, str), "getV8Statistics") == 0);
21
  return v8::FunctionTemplate::New(isolate, StatisticsExtension::GetCounters);
22 23 24
}


25 26
static void AddCounter(v8::Isolate* isolate,
                       v8::Local<v8::Object> object,
27 28
                       StatsCounter* counter,
                       const char* name) {
29
  if (counter->Enabled()) {
30 31 32 33 34
    object->Set(isolate->GetCurrentContext(),
                v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
                    .ToLocalChecked(),
                v8::Number::New(isolate, *counter->GetInternalPointer()))
        .FromJust();
35 36 37
  }
}

38 39 40 41 42 43 44 45
static void AddNumber(v8::Isolate* isolate, v8::Local<v8::Object> object,
                      double value, const char* name) {
  object
      ->Set(isolate->GetCurrentContext(),
            v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
                .ToLocalChecked(),
            v8::Number::New(isolate, value))
      .FromJust();
46 47
}

48

49 50
static void AddNumber64(v8::Isolate* isolate,
                        v8::Local<v8::Object> object,
51 52
                        int64_t value,
                        const char* name) {
53 54 55 56
  object->Set(isolate->GetCurrentContext(),
              v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
                  .ToLocalChecked(),
              v8::Number::New(isolate, static_cast<double>(value))).FromJust();
57 58 59
}


60 61
void StatisticsExtension::GetCounters(
    const v8::FunctionCallbackInfo<v8::Value>& args) {
62
  Isolate* isolate = reinterpret_cast<Isolate*>(args.GetIsolate());
63
  Heap* heap = isolate->heap();
64

65
  if (args.Length() > 0) {  // GC if first argument evaluates to true.
66
    if (args[0]->IsBoolean() &&
67 68 69
        args[0]
            ->BooleanValue(args.GetIsolate()->GetCurrentContext())
            .FromMaybe(false)) {
70 71
      heap->CollectAllGarbage(Heap::kNoGCFlags,
                              GarbageCollectionReason::kCountersExtension);
72 73 74 75
    }
  }

  Counters* counters = isolate->counters();
76
  v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate());
77

78 79 80 81 82 83 84 85 86 87
  struct StatisticsCounter {
    v8::internal::StatsCounter* counter;
    const char* name;
  };
  const StatisticsCounter counter_list[] = {
#define ADD_COUNTER(name, caption) \
  { counters->name(), #name }      \
  ,

      STATS_COUNTER_LIST_1(ADD_COUNTER) STATS_COUNTER_LIST_2(ADD_COUNTER)
88
#undef ADD_COUNTER
89 90 91
#define ADD_COUNTER(name)                            \
  { counters->count_of_##name(), "count_of_" #name } \
  , {counters->size_of_##name(), "size_of_" #name},
92

93
          INSTANCE_TYPE_LIST(ADD_COUNTER)
94
#undef ADD_COUNTER
95 96 97
#define ADD_COUNTER(name)                                                \
  { counters->count_of_CODE_TYPE_##name(), "count_of_CODE_TYPE_" #name } \
  , {counters->size_of_CODE_TYPE_##name(), "size_of_CODE_TYPE_" #name},
98

99
              CODE_KIND_LIST(ADD_COUNTER)
100
#undef ADD_COUNTER
101 102 103 104 105
#define ADD_COUNTER(name)                                                    \
  { counters->count_of_FIXED_ARRAY_##name(), "count_of_FIXED_ARRAY_" #name } \
  , {counters->size_of_FIXED_ARRAY_##name(), "size_of_FIXED_ARRAY_" #name},

                  FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADD_COUNTER)
106
#undef ADD_COUNTER
107 108 109 110 111 112 113 114
  };  // End counter_list array.

  for (size_t i = 0; i < arraysize(counter_list); i++) {
    AddCounter(args.GetIsolate(), result, counter_list[i].counter,
               counter_list[i].name);
  }

  struct StatisticNumber {
115
    size_t number;
116 117 118 119
    const char* name;
  };

  const StatisticNumber numbers[] = {
120
      {heap->memory_allocator()->Size(), "total_committed_bytes"},
121 122
      {heap->new_space()->Size(), "new_space_live_bytes"},
      {heap->new_space()->Available(), "new_space_available_bytes"},
123
      {heap->new_space()->CommittedMemory(), "new_space_commited_bytes"},
124 125
      {heap->old_space()->Size(), "old_space_live_bytes"},
      {heap->old_space()->Available(), "old_space_available_bytes"},
126
      {heap->old_space()->CommittedMemory(), "old_space_commited_bytes"},
127 128
      {heap->code_space()->Size(), "code_space_live_bytes"},
      {heap->code_space()->Available(), "code_space_available_bytes"},
129
      {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
130 131
      {heap->lo_space()->Size(), "lo_space_live_bytes"},
      {heap->lo_space()->Available(), "lo_space_available_bytes"},
132
      {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
133 134 135 136 137
  };

  for (size_t i = 0; i < arraysize(numbers); i++) {
    AddNumber(args.GetIsolate(), result, numbers[i].number, numbers[i].name);
  }
138

139
  AddNumber64(args.GetIsolate(), result, heap->external_memory(),
140
              "amount_of_external_allocated_memory");
141
  args.GetReturnValue().Set(result);
142 143 144 145 146

  HeapIterator iterator(reinterpret_cast<Isolate*>(args.GetIsolate())->heap());
  HeapObject* obj;
  int reloc_info_total = 0;
  int source_position_table_total = 0;
147
  while ((obj = iterator.next()) != nullptr) {
148 149 150
    if (obj->IsCode()) {
      Code* code = Code::cast(obj);
      reloc_info_total += code->relocation_info()->Size();
151
      ByteArray* source_position_table = code->SourcePositionTable();
152
      if (source_position_table->length() > 0) {
153
        source_position_table_total += code->SourcePositionTable()->Size();
154 155 156
      }
    } else if (obj->IsBytecodeArray()) {
      source_position_table_total +=
157
          BytecodeArray::cast(obj)->SourcePositionTable()->Size();
158 159 160 161 162 163 164
    }
  }

  AddNumber(args.GetIsolate(), result, reloc_info_total,
            "reloc_info_total_size");
  AddNumber(args.GetIsolate(), result, source_position_table_total,
            "source_position_table_total_size");
165 166
}

167 168
}  // namespace internal
}  // namespace v8