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 138 void ObjectStatsVisitor::CountFixedArray( 139 FixedArrayBase* fixed_array, FixedArraySubInstanceType fast_type, 140 FixedArraySubInstanceType dictionary_type) { 141 Heap* heap = fixed_array->map()->GetHeap(); 142 if (fixed_array->map() != heap->fixed_cow_array_map() && 143 fixed_array->map() != heap->fixed_double_array_map() && 144 fixed_array != heap->empty_fixed_array()) { 145 if (fixed_array->IsDictionary()) { 146 heap->object_stats_->RecordFixedArraySubTypeStats(dictionary_type, 147 fixed_array->Size()); 148 } else { 149 heap->object_stats_->RecordFixedArraySubTypeStats(fast_type, 150 fixed_array->Size()); 151 } 152 } 153 } 154 155 156 void ObjectStatsVisitor::VisitBase(VisitorId id, Map* map, HeapObject* obj) { 157 Heap* heap = map->GetHeap(); 158 int object_size = obj->Size(); 159 heap->object_stats_->RecordObjectStats(map->instance_type(), object_size); 160 table_.GetVisitorById(id)(map, obj); 161 if (obj->IsJSObject()) { 162 JSObject* object = JSObject::cast(obj); 163 CountFixedArray(object->elements(), DICTIONARY_ELEMENTS_SUB_TYPE, 164 FAST_ELEMENTS_SUB_TYPE); 165 CountFixedArray(object->properties(), DICTIONARY_PROPERTIES_SUB_TYPE, 166 FAST_PROPERTIES_SUB_TYPE); 167 } 168 } 169 170 171 template <ObjectStatsVisitor::VisitorId id> 172 void ObjectStatsVisitor::Visit(Map* map, HeapObject* obj) { 173 VisitBase(id, map, obj); 174 } 175 176 177 template <> 178 void ObjectStatsVisitor::Visit<ObjectStatsVisitor::kVisitMap>(Map* map, 179 HeapObject* obj) { 180 Heap* heap = map->GetHeap(); 181 Map* map_obj = Map::cast(obj); 182 DCHECK(map->instance_type() == MAP_TYPE); 183 DescriptorArray* array = map_obj->instance_descriptors(); 184 if (map_obj->owns_descriptors() && array != heap->empty_descriptor_array()) { 185 int fixed_array_size = array->Size(); 186 heap->object_stats_->RecordFixedArraySubTypeStats(DESCRIPTOR_ARRAY_SUB_TYPE, 187 fixed_array_size); 188 } 189 if (map_obj->has_code_cache()) { 190 CodeCache* cache = CodeCache::cast(map_obj->code_cache()); 191 heap->object_stats_->RecordFixedArraySubTypeStats( 192 MAP_CODE_CACHE_SUB_TYPE, cache->default_cache()->Size()); 193 if (!cache->normal_type_cache()->IsUndefined()) { 194 heap->object_stats_->RecordFixedArraySubTypeStats( 195 MAP_CODE_CACHE_SUB_TYPE, 196 FixedArray::cast(cache->normal_type_cache())->Size()); 197 } 198 } 199 VisitBase(kVisitMap, map, obj); 200 } 201 202 203 template <> 204 void ObjectStatsVisitor::Visit<ObjectStatsVisitor::kVisitCode>( 205 Map* map, HeapObject* obj) { 206 Heap* heap = map->GetHeap(); 207 int object_size = obj->Size(); 208 DCHECK(map->instance_type() == CODE_TYPE); 209 Code* code_obj = Code::cast(obj); 210 heap->object_stats_->RecordCodeSubTypeStats(code_obj->kind(), 211 code_obj->GetAge(), object_size); 212 VisitBase(kVisitCode, map, obj); 213 } 214 215 216 template <> 217 void ObjectStatsVisitor::Visit<ObjectStatsVisitor::kVisitSharedFunctionInfo>( 218 Map* map, HeapObject* obj) { 219 Heap* heap = map->GetHeap(); 220 SharedFunctionInfo* sfi = SharedFunctionInfo::cast(obj); 221 if (sfi->scope_info() != heap->empty_fixed_array()) { 222 heap->object_stats_->RecordFixedArraySubTypeStats( 223 SCOPE_INFO_SUB_TYPE, FixedArray::cast(sfi->scope_info())->Size()); 224 } 225 VisitBase(kVisitSharedFunctionInfo, map, obj); 226 } 227 228 229 template <> 230 void ObjectStatsVisitor::Visit<ObjectStatsVisitor::kVisitFixedArray>( 231 Map* map, HeapObject* obj) { 232 Heap* heap = map->GetHeap(); 233 FixedArray* fixed_array = FixedArray::cast(obj); 234 if (fixed_array == heap->string_table()) { 235 heap->object_stats_->RecordFixedArraySubTypeStats(STRING_TABLE_SUB_TYPE, 236 fixed_array->Size()); 237 } 238 VisitBase(kVisitFixedArray, map, obj); 239 } 240 241 242 void ObjectStatsVisitor::Initialize(VisitorDispatchTable<Callback>* original) { 243 // Copy the original visitor table to make call-through possible. After we 244 // preserved a copy locally, we patch the original table to call us. 245 table_.CopyFrom(original); 246 #define COUNT_FUNCTION(id) original->Register(kVisit##id, Visit<kVisit##id>); 247 VISITOR_ID_LIST(COUNT_FUNCTION) 248 #undef COUNT_FUNCTION 249 } 250 251 } // namespace internal 252 } // namespace v8 253