statistics-extension.cc 5.48 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
#include "src/counters.h"
8
#include "src/heap/heap-inl.h"  // crbug.com/v8/8499
9
#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_EQ(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() && args[0]->BooleanValue(args.GetIsolate())) {
67 68
      heap->CollectAllGarbage(Heap::kNoGCFlags,
                              GarbageCollectionReason::kCountersExtension);
69 70 71 72
    }
  }

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

75 76 77 78 79 80 81 82 83 84
  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)
85
#undef ADD_COUNTER
86 87 88 89 90 91 92 93
  };  // 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 {
94
    size_t number;
95 96 97 98
    const char* name;
  };

  const StatisticNumber numbers[] = {
99
      {heap->memory_allocator()->Size(), "total_committed_bytes"},
100 101
      {heap->new_space()->Size(), "new_space_live_bytes"},
      {heap->new_space()->Available(), "new_space_available_bytes"},
102
      {heap->new_space()->CommittedMemory(), "new_space_commited_bytes"},
103 104
      {heap->old_space()->Size(), "old_space_live_bytes"},
      {heap->old_space()->Available(), "old_space_available_bytes"},
105
      {heap->old_space()->CommittedMemory(), "old_space_commited_bytes"},
106 107
      {heap->code_space()->Size(), "code_space_live_bytes"},
      {heap->code_space()->Available(), "code_space_available_bytes"},
108
      {heap->code_space()->CommittedMemory(), "code_space_commited_bytes"},
109 110
      {heap->lo_space()->Size(), "lo_space_live_bytes"},
      {heap->lo_space()->Available(), "lo_space_available_bytes"},
111
      {heap->lo_space()->CommittedMemory(), "lo_space_commited_bytes"},
112 113 114 115
      {heap->code_lo_space()->Size(), "code_lo_space_live_bytes"},
      {heap->code_lo_space()->Available(), "code_lo_space_available_bytes"},
      {heap->code_lo_space()->CommittedMemory(),
       "code_lo_space_commited_bytes"},
116 117 118 119 120
  };

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

122
  AddNumber64(args.GetIsolate(), result, heap->external_memory(),
123
              "amount_of_external_allocated_memory");
124
  args.GetReturnValue().Set(result);
125 126 127 128

  HeapIterator iterator(reinterpret_cast<Isolate*>(args.GetIsolate())->heap());
  int reloc_info_total = 0;
  int source_position_table_total = 0;
129 130
  for (HeapObject obj = iterator.next(); !obj.is_null();
       obj = iterator.next()) {
131
    if (obj->IsCode()) {
132
      Code code = Code::cast(obj);
133
      reloc_info_total += code->relocation_info()->Size();
134
      ByteArray source_position_table = code->SourcePositionTable();
135
      if (source_position_table->length() > 0) {
136
        source_position_table_total += code->SourcePositionTable()->Size();
137 138 139
      }
    } else if (obj->IsBytecodeArray()) {
      source_position_table_total +=
140
          BytecodeArray::cast(obj)->SourcePositionTable()->Size();
141 142 143 144 145 146 147
    }
  }

  AddNumber(args.GetIsolate(), result, reloc_info_total,
            "reloc_info_total_size");
  AddNumber(args.GetIsolate(), result, source_position_table_total,
            "source_position_table_total_size");
148 149
}

150 151
}  // namespace internal
}  // namespace v8