Home | History | Annotate | Download | only in heap
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/heap/object-stats.h"
      6 
      7 #include "src/counters.h"
      8 #include "src/heap/heap-inl.h"
      9 #include "src/isolate.h"
     10 #include "src/utils.h"
     11 
     12 namespace v8 {
     13 namespace internal {
     14 
     15 static base::LazyMutex object_stats_mutex = LAZY_MUTEX_INITIALIZER;
     16 
     17 
     18 void ObjectStats::ClearObjectStats(bool clear_last_time_stats) {
     19   memset(object_counts_, 0, sizeof(object_counts_));
     20   memset(object_sizes_, 0, sizeof(object_sizes_));
     21   if (clear_last_time_stats) {
     22     memset(object_counts_last_time_, 0, sizeof(object_counts_last_time_));
     23     memset(object_sizes_last_time_, 0, sizeof(object_sizes_last_time_));
     24   }
     25 }
     26 
     27 
     28 void ObjectStats::TraceObjectStat(const char* name, int count, int size,
     29                                   double time) {
     30   int ms_count = heap()->ms_count();
     31   PrintIsolate(isolate(),
     32                "heap:%p, time:%f, gc:%d, type:%s, count:%d, size:%d\n",
     33                static_cast<void*>(heap()), time, ms_count, name, count, size);
     34 }
     35 
     36 
     37 void ObjectStats::TraceObjectStats() {
     38   base::LockGuard<base::Mutex> lock_guard(object_stats_mutex.Pointer());
     39   int index;
     40   int count;
     41   int size;
     42   int total_size = 0;
     43   double time = isolate()->time_millis_since_init();
     44 #define TRACE_OBJECT_COUNT(name)                     \
     45   count = static_cast<int>(object_counts_[name]);    \
     46   size = static_cast<int>(object_sizes_[name]) / KB; \
     47   total_size += size;                                \
     48   TraceObjectStat(#name, count, size, time);
     49   INSTANCE_TYPE_LIST(TRACE_OBJECT_COUNT)
     50 #undef TRACE_OBJECT_COUNT
     51 #define TRACE_OBJECT_COUNT(name)                      \
     52   index = FIRST_CODE_KIND_SUB_TYPE + Code::name;      \
     53   count = static_cast<int>(object_counts_[index]);    \
     54   size = static_cast<int>(object_sizes_[index]) / KB; \
     55   TraceObjectStat("*CODE_" #name, count, size, time);
     56   CODE_KIND_LIST(TRACE_OBJECT_COUNT)
     57 #undef TRACE_OBJECT_COUNT
     58 #define TRACE_OBJECT_COUNT(name)                      \
     59   index = FIRST_FIXED_ARRAY_SUB_TYPE + name;          \
     60   count = static_cast<int>(object_counts_[index]);    \
     61   size = static_cast<int>(object_sizes_[index]) / KB; \
     62   TraceObjectStat("*FIXED_ARRAY_" #name, count, size, time);
     63   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(TRACE_OBJECT_COUNT)
     64 #undef TRACE_OBJECT_COUNT
     65 #define TRACE_OBJECT_COUNT(name)                                              \
     66   index =                                                                     \
     67       FIRST_CODE_AGE_SUB_TYPE + Code::k##name##CodeAge - Code::kFirstCodeAge; \
     68   count = static_cast<int>(object_counts_[index]);                            \
     69   size = static_cast<int>(object_sizes_[index]) / KB;                         \
     70   TraceObjectStat("*CODE_AGE_" #name, count, size, time);
     71   CODE_AGE_LIST_COMPLETE(TRACE_OBJECT_COUNT)
     72 #undef TRACE_OBJECT_COUNT
     73 }
     74 
     75 
     76 void ObjectStats::CheckpointObjectStats() {
     77   base::LockGuard<base::Mutex> lock_guard(object_stats_mutex.Pointer());
     78   Counters* counters = isolate()->counters();
     79 #define ADJUST_LAST_TIME_OBJECT_COUNT(name)              \
     80   counters->count_of_##name()->Increment(                \
     81       static_cast<int>(object_counts_[name]));           \
     82   counters->count_of_##name()->Decrement(                \
     83       static_cast<int>(object_counts_last_time_[name])); \
     84   counters->size_of_##name()->Increment(                 \
     85       static_cast<int>(object_sizes_[name]));            \
     86   counters->size_of_##name()->Decrement(                 \
     87       static_cast<int>(object_sizes_last_time_[name]));
     88   INSTANCE_TYPE_LIST(ADJUST_LAST_TIME_OBJECT_COUNT)
     89 #undef ADJUST_LAST_TIME_OBJECT_COUNT
     90   int index;
     91 #define ADJUST_LAST_TIME_OBJECT_COUNT(name)               \
     92   index = FIRST_CODE_KIND_SUB_TYPE + Code::name;          \
     93   counters->count_of_CODE_TYPE_##name()->Increment(       \
     94       static_cast<int>(object_counts_[index]));           \
     95   counters->count_of_CODE_TYPE_##name()->Decrement(       \
     96       static_cast<int>(object_counts_last_time_[index])); \
     97   counters->size_of_CODE_TYPE_##name()->Increment(        \
     98       static_cast<int>(object_sizes_[index]));            \
     99   counters->size_of_CODE_TYPE_##name()->Decrement(        \
    100       static_cast<int>(object_sizes_last_time_[index]));
    101   CODE_KIND_LIST(ADJUST_LAST_TIME_OBJECT_COUNT)
    102 #undef ADJUST_LAST_TIME_OBJECT_COUNT
    103 #define ADJUST_LAST_TIME_OBJECT_COUNT(name)               \
    104   index = FIRST_FIXED_ARRAY_SUB_TYPE + name;              \
    105   counters->count_of_FIXED_ARRAY_##name()->Increment(     \
    106       static_cast<int>(object_counts_[index]));           \
    107   counters->count_of_FIXED_ARRAY_##name()->Decrement(     \
    108       static_cast<int>(object_counts_last_time_[index])); \
    109   counters->size_of_FIXED_ARRAY_##name()->Increment(      \
    110       static_cast<int>(object_sizes_[index]));            \
    111   counters->size_of_FIXED_ARRAY_##name()->Decrement(      \
    112       static_cast<int>(object_sizes_last_time_[index]));
    113   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(ADJUST_LAST_TIME_OBJECT_COUNT)
    114 #undef ADJUST_LAST_TIME_OBJECT_COUNT
    115 #define ADJUST_LAST_TIME_OBJECT_COUNT(name)                                   \
    116   index =                                                                     \
    117       FIRST_CODE_AGE_SUB_TYPE + Code::k##name##CodeAge - Code::kFirstCodeAge; \
    118   counters->count_of_CODE_AGE_##name()->Increment(                            \
    119       static_cast<int>(object_counts_[index]));                               \
    120   counters->count_of_CODE_AGE_##name()->Decrement(                            \
    121       static_cast<int>(object_counts_last_time_[index]));                     \
    122   counters->size_of_CODE_AGE_##name()->Increment(                             \
    123       static_cast<int>(object_sizes_[index]));                                \
    124   counters->size_of_CODE_AGE_##name()->Decrement(                             \
    125       static_cast<int>(object_sizes_last_time_[index]));
    126   CODE_AGE_LIST_COMPLETE(ADJUST_LAST_TIME_OBJECT_COUNT)
    127 #undef ADJUST_LAST_TIME_OBJECT_COUNT
    128 
    129   MemCopy(object_counts_last_time_, object_counts_, sizeof(object_counts_));
    130   MemCopy(object_sizes_last_time_, object_sizes_, sizeof(object_sizes_));
    131   ClearObjectStats();
    132 }
    133 
    134 
    135 Isolate* ObjectStats::isolate() { return heap()->isolate(); }
    136 
    137 void ObjectStatsCollector::CountFixedArray(
    138     FixedArrayBase* fixed_array, FixedArraySubInstanceType fast_type,
    139     FixedArraySubInstanceType dictionary_type) {
    140   Heap* heap = fixed_array->map()->GetHeap();
    141   if (fixed_array->map() != heap->fixed_cow_array_map() &&
    142       fixed_array->map() != heap->fixed_double_array_map() &&
    143       fixed_array != heap->empty_fixed_array()) {
    144     if (fixed_array->IsDictionary()) {
    145       heap->object_stats_->RecordFixedArraySubTypeStats(dictionary_type,
    146                                                         fixed_array->Size());
    147     } else {
    148       heap->object_stats_->RecordFixedArraySubTypeStats(fast_type,
    149                                                         fixed_array->Size());
    150     }
    151   }
    152 }
    153 
    154 void ObjectStatsCollector::CollectStatistics(StaticVisitorBase::VisitorId id,
    155                                              Map* map, HeapObject* obj) {
    156   // Record any type specific statistics here.
    157   switch (id) {
    158     case StaticVisitorBase::kVisitMap:
    159       RecordMapStats(map, obj);
    160       break;
    161     case StaticVisitorBase::kVisitCode:
    162       RecordCodeStats(map, obj);
    163       break;
    164     case StaticVisitorBase::kVisitSharedFunctionInfo:
    165       RecordSharedFunctionInfoStats(map, obj);
    166       break;
    167     case StaticVisitorBase::kVisitFixedArray:
    168       RecordFixedArrayStats(map, obj);
    169       break;
    170     default:
    171       break;
    172   }
    173 
    174   Heap* heap = map->GetHeap();
    175   int object_size = obj->Size();
    176   heap->object_stats_->RecordObjectStats(map->instance_type(), object_size);
    177 }
    178 
    179 void ObjectStatsCollector::CollectFixedArrayStatistics(HeapObject* obj) {
    180   if (obj->IsJSObject()) {
    181     JSObject* object = JSObject::cast(obj);
    182     CountFixedArray(object->elements(), DICTIONARY_ELEMENTS_SUB_TYPE,
    183                     FAST_ELEMENTS_SUB_TYPE);
    184     CountFixedArray(object->properties(), DICTIONARY_PROPERTIES_SUB_TYPE,
    185                     FAST_PROPERTIES_SUB_TYPE);
    186   }
    187 }
    188 
    189 void ObjectStatsCollector::RecordMapStats(Map* map, HeapObject* obj) {
    190   Heap* heap = map->GetHeap();
    191   Map* map_obj = Map::cast(obj);
    192   DCHECK(map->instance_type() == MAP_TYPE);
    193   DescriptorArray* array = map_obj->instance_descriptors();
    194   if (map_obj->owns_descriptors() && array != heap->empty_descriptor_array()) {
    195     int fixed_array_size = array->Size();
    196     heap->object_stats_->RecordFixedArraySubTypeStats(DESCRIPTOR_ARRAY_SUB_TYPE,
    197                                                       fixed_array_size);
    198   }
    199   if (map_obj->has_code_cache()) {
    200     FixedArray* cache = map_obj->code_cache();
    201     heap->object_stats_->RecordFixedArraySubTypeStats(MAP_CODE_CACHE_SUB_TYPE,
    202                                                       cache->Size());
    203   }
    204 }
    205 
    206 void ObjectStatsCollector::RecordCodeStats(Map* map, HeapObject* obj) {
    207   Heap* heap = map->GetHeap();
    208   int object_size = obj->Size();
    209   DCHECK(map->instance_type() == CODE_TYPE);
    210   Code* code_obj = Code::cast(obj);
    211   heap->object_stats_->RecordCodeSubTypeStats(code_obj->kind(),
    212                                               code_obj->GetAge(), object_size);
    213 }
    214 
    215 void ObjectStatsCollector::RecordSharedFunctionInfoStats(Map* map,
    216                                                          HeapObject* obj) {
    217   Heap* heap = map->GetHeap();
    218   SharedFunctionInfo* sfi = SharedFunctionInfo::cast(obj);
    219   if (sfi->scope_info() != heap->empty_fixed_array()) {
    220     heap->object_stats_->RecordFixedArraySubTypeStats(
    221         SCOPE_INFO_SUB_TYPE, FixedArray::cast(sfi->scope_info())->Size());
    222   }
    223 }
    224 
    225 void ObjectStatsCollector::RecordFixedArrayStats(Map* map, HeapObject* obj) {
    226   Heap* heap = map->GetHeap();
    227   FixedArray* fixed_array = FixedArray::cast(obj);
    228   if (fixed_array == heap->string_table()) {
    229     heap->object_stats_->RecordFixedArraySubTypeStats(STRING_TABLE_SUB_TYPE,
    230                                                       fixed_array->Size());
    231   }
    232 }
    233 
    234 void MarkCompactObjectStatsVisitor::Initialize(
    235     VisitorDispatchTable<Callback>* original) {
    236   // Copy the original visitor table to make call-through possible. After we
    237   // preserved a copy locally, we patch the original table to call us.
    238   table_.CopyFrom(original);
    239 #define COUNT_FUNCTION(id) original->Register(kVisit##id, Visit<kVisit##id>);
    240   VISITOR_ID_LIST(COUNT_FUNCTION)
    241 #undef COUNT_FUNCTION
    242 }
    243 
    244 template <MarkCompactObjectStatsVisitor::VisitorId id>
    245 void MarkCompactObjectStatsVisitor::Visit(Map* map, HeapObject* obj) {
    246   ObjectStatsCollector::CollectStatistics(id, map, obj);
    247   table_.GetVisitorById(id)(map, obj);
    248   ObjectStatsCollector::CollectFixedArrayStatistics(obj);
    249 }
    250 
    251 void IncrementalMarkingObjectStatsVisitor::Initialize(
    252     VisitorDispatchTable<Callback>* original) {
    253   // Copy the original visitor table to make call-through possible. After we
    254   // preserved a copy locally, we patch the original table to call us.
    255   table_.CopyFrom(original);
    256 #define COUNT_FUNCTION(id) original->Register(kVisit##id, Visit<kVisit##id>);
    257   VISITOR_ID_LIST(COUNT_FUNCTION)
    258 #undef COUNT_FUNCTION
    259 }
    260 
    261 template <IncrementalMarkingObjectStatsVisitor::VisitorId id>
    262 void IncrementalMarkingObjectStatsVisitor::Visit(Map* map, HeapObject* obj) {
    263   ObjectStatsCollector::CollectStatistics(id, map, obj);
    264   table_.GetVisitorById(id)(map, obj);
    265   ObjectStatsCollector::CollectFixedArrayStatistics(obj);
    266 }
    267 
    268 }  // namespace internal
    269 }  // namespace v8
    270