Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : //
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 :
6 : #include "src/heap/object-stats.h"
7 :
8 : #include <unordered_set>
9 :
10 : #include "src/assembler-inl.h"
11 : #include "src/base/bits.h"
12 : #include "src/compilation-cache.h"
13 : #include "src/counters.h"
14 : #include "src/globals.h"
15 : #include "src/heap/heap-inl.h"
16 : #include "src/heap/mark-compact.h"
17 : #include "src/isolate.h"
18 : #include "src/memcopy.h"
19 : #include "src/objects/compilation-cache-inl.h"
20 : #include "src/objects/heap-object.h"
21 : #include "src/objects/js-array-inl.h"
22 : #include "src/objects/js-collection-inl.h"
23 : #include "src/objects/literal-objects-inl.h"
24 : #include "src/objects/slots.h"
25 : #include "src/objects/templates.h"
26 : #include "src/ostreams.h"
27 :
28 : namespace v8 {
29 : namespace internal {
30 :
31 : static base::LazyMutex object_stats_mutex = LAZY_MUTEX_INITIALIZER;
32 :
33 0 : class FieldStatsCollector : public ObjectVisitor {
34 : public:
35 : FieldStatsCollector(size_t* tagged_fields_count,
36 : size_t* embedder_fields_count,
37 : size_t* unboxed_double_fields_count,
38 : size_t* raw_fields_count)
39 : : tagged_fields_count_(tagged_fields_count),
40 : embedder_fields_count_(embedder_fields_count),
41 : unboxed_double_fields_count_(unboxed_double_fields_count),
42 0 : raw_fields_count_(raw_fields_count) {}
43 :
44 0 : void RecordStats(HeapObject host) {
45 0 : size_t old_pointer_fields_count = *tagged_fields_count_;
46 0 : host->Iterate(this);
47 : size_t tagged_fields_count_in_object =
48 0 : *tagged_fields_count_ - old_pointer_fields_count;
49 :
50 0 : int object_size_in_words = host->Size() / kTaggedSize;
51 : DCHECK_LE(tagged_fields_count_in_object, object_size_in_words);
52 : size_t raw_fields_count_in_object =
53 0 : object_size_in_words - tagged_fields_count_in_object;
54 :
55 0 : if (host->IsJSObject()) {
56 0 : JSObjectFieldStats field_stats = GetInobjectFieldStats(host->map());
57 : // Embedder fields are already included into pointer words.
58 : DCHECK_LE(field_stats.embedded_fields_count_,
59 : tagged_fields_count_in_object);
60 0 : tagged_fields_count_in_object -= field_stats.embedded_fields_count_;
61 0 : *tagged_fields_count_ -= field_stats.embedded_fields_count_;
62 0 : *embedder_fields_count_ += field_stats.embedded_fields_count_;
63 :
64 : // The rest are data words.
65 : DCHECK_LE(field_stats.unboxed_double_fields_count_,
66 : raw_fields_count_in_object);
67 0 : raw_fields_count_in_object -= field_stats.unboxed_double_fields_count_;
68 0 : *unboxed_double_fields_count_ += field_stats.unboxed_double_fields_count_;
69 : }
70 0 : *raw_fields_count_ += raw_fields_count_in_object;
71 0 : }
72 :
73 0 : void VisitPointers(HeapObject host, ObjectSlot start,
74 : ObjectSlot end) override {
75 0 : *tagged_fields_count_ += (end - start);
76 0 : }
77 0 : void VisitPointers(HeapObject host, MaybeObjectSlot start,
78 : MaybeObjectSlot end) override {
79 0 : *tagged_fields_count_ += (end - start);
80 0 : }
81 :
82 0 : void VisitCodeTarget(Code host, RelocInfo* rinfo) override {
83 : // Code target is most likely encoded as a relative 32-bit offset and not
84 : // as a full tagged value, so there's nothing to count.
85 0 : }
86 :
87 0 : void VisitEmbeddedPointer(Code host, RelocInfo* rinfo) override {
88 0 : *tagged_fields_count_ += 1;
89 0 : }
90 :
91 : private:
92 : struct JSObjectFieldStats {
93 : JSObjectFieldStats()
94 : : embedded_fields_count_(0), unboxed_double_fields_count_(0) {}
95 :
96 : unsigned embedded_fields_count_ : kDescriptorIndexBitCount;
97 : unsigned unboxed_double_fields_count_ : kDescriptorIndexBitCount;
98 : };
99 : std::unordered_map<Map, JSObjectFieldStats, Object::Hasher>
100 : field_stats_cache_;
101 :
102 : JSObjectFieldStats GetInobjectFieldStats(Map map);
103 :
104 : size_t* const tagged_fields_count_;
105 : size_t* const embedder_fields_count_;
106 : size_t* const unboxed_double_fields_count_;
107 : size_t* const raw_fields_count_;
108 : };
109 :
110 : FieldStatsCollector::JSObjectFieldStats
111 0 : FieldStatsCollector::GetInobjectFieldStats(Map map) {
112 : auto iter = field_stats_cache_.find(map);
113 0 : if (iter != field_stats_cache_.end()) {
114 0 : return iter->second;
115 : }
116 : // Iterate descriptor array and calculate stats.
117 : JSObjectFieldStats stats;
118 0 : stats.embedded_fields_count_ = JSObject::GetEmbedderFieldCount(map);
119 0 : if (!map->is_dictionary_map()) {
120 : int nof = map->NumberOfOwnDescriptors();
121 0 : DescriptorArray descriptors = map->instance_descriptors();
122 0 : for (int descriptor = 0; descriptor < nof; descriptor++) {
123 0 : PropertyDetails details = descriptors->GetDetails(descriptor);
124 0 : if (details.location() == kField) {
125 0 : FieldIndex index = FieldIndex::ForDescriptor(map, descriptor);
126 : // Stop on first out-of-object field.
127 0 : if (!index.is_inobject()) break;
128 0 : if (details.representation().IsDouble() &&
129 0 : map->IsUnboxedDoubleField(index)) {
130 0 : ++stats.unboxed_double_fields_count_;
131 : }
132 : }
133 : }
134 : }
135 0 : field_stats_cache_.insert(std::make_pair(map, stats));
136 0 : return stats;
137 : }
138 :
139 0 : void ObjectStats::ClearObjectStats(bool clear_last_time_stats) {
140 0 : memset(object_counts_, 0, sizeof(object_counts_));
141 0 : memset(object_sizes_, 0, sizeof(object_sizes_));
142 0 : memset(over_allocated_, 0, sizeof(over_allocated_));
143 0 : memset(size_histogram_, 0, sizeof(size_histogram_));
144 0 : memset(over_allocated_histogram_, 0, sizeof(over_allocated_histogram_));
145 0 : if (clear_last_time_stats) {
146 0 : memset(object_counts_last_time_, 0, sizeof(object_counts_last_time_));
147 0 : memset(object_sizes_last_time_, 0, sizeof(object_sizes_last_time_));
148 : }
149 0 : tagged_fields_count_ = 0;
150 0 : embedder_fields_count_ = 0;
151 0 : unboxed_double_fields_count_ = 0;
152 0 : raw_fields_count_ = 0;
153 0 : }
154 :
155 : // Tell the compiler to never inline this: occasionally, the optimizer will
156 : // decide to inline this and unroll the loop, making the compiled code more than
157 : // 100KB larger.
158 0 : V8_NOINLINE static void PrintJSONArray(size_t* array, const int len) {
159 0 : PrintF("[ ");
160 0 : for (int i = 0; i < len; i++) {
161 0 : PrintF("%zu", array[i]);
162 0 : if (i != (len - 1)) PrintF(", ");
163 : }
164 0 : PrintF(" ]");
165 0 : }
166 :
167 0 : V8_NOINLINE static void DumpJSONArray(std::stringstream& stream, size_t* array,
168 : const int len) {
169 0 : stream << PrintCollection(Vector<size_t>(array, len));
170 0 : }
171 :
172 0 : void ObjectStats::PrintKeyAndId(const char* key, int gc_count) {
173 : PrintF("\"isolate\": \"%p\", \"id\": %d, \"key\": \"%s\", ",
174 0 : reinterpret_cast<void*>(isolate()), gc_count, key);
175 0 : }
176 :
177 0 : void ObjectStats::PrintInstanceTypeJSON(const char* key, int gc_count,
178 : const char* name, int index) {
179 0 : PrintF("{ ");
180 : PrintKeyAndId(key, gc_count);
181 0 : PrintF("\"type\": \"instance_type_data\", ");
182 0 : PrintF("\"instance_type\": %d, ", index);
183 0 : PrintF("\"instance_type_name\": \"%s\", ", name);
184 0 : PrintF("\"overall\": %zu, ", object_sizes_[index]);
185 0 : PrintF("\"count\": %zu, ", object_counts_[index]);
186 0 : PrintF("\"over_allocated\": %zu, ", over_allocated_[index]);
187 0 : PrintF("\"histogram\": ");
188 0 : PrintJSONArray(size_histogram_[index], kNumberOfBuckets);
189 0 : PrintF(",");
190 0 : PrintF("\"over_allocated_histogram\": ");
191 0 : PrintJSONArray(over_allocated_histogram_[index], kNumberOfBuckets);
192 0 : PrintF(" }\n");
193 0 : }
194 :
195 0 : void ObjectStats::PrintJSON(const char* key) {
196 : double time = isolate()->time_millis_since_init();
197 : int gc_count = heap()->gc_count();
198 :
199 : // gc_descriptor
200 0 : PrintF("{ ");
201 : PrintKeyAndId(key, gc_count);
202 0 : PrintF("\"type\": \"gc_descriptor\", \"time\": %f }\n", time);
203 : // field_data
204 0 : PrintF("{ ");
205 : PrintKeyAndId(key, gc_count);
206 0 : PrintF("\"type\": \"field_data\"");
207 0 : PrintF(", \"tagged_fields\": %zu", tagged_fields_count_ * kTaggedSize);
208 0 : PrintF(", \"embedder_fields\": %zu",
209 0 : embedder_fields_count_ * kEmbedderDataSlotSize);
210 0 : PrintF(", \"unboxed_double_fields\": %zu",
211 0 : unboxed_double_fields_count_ * kDoubleSize);
212 0 : PrintF(", \"other_raw_fields\": %zu", raw_fields_count_ * kSystemPointerSize);
213 0 : PrintF(" }\n");
214 : // bucket_sizes
215 0 : PrintF("{ ");
216 : PrintKeyAndId(key, gc_count);
217 0 : PrintF("\"type\": \"bucket_sizes\", \"sizes\": [ ");
218 0 : for (int i = 0; i < kNumberOfBuckets; i++) {
219 0 : PrintF("%d", 1 << (kFirstBucketShift + i));
220 0 : if (i != (kNumberOfBuckets - 1)) PrintF(", ");
221 : }
222 0 : PrintF(" ] }\n");
223 :
224 : #define INSTANCE_TYPE_WRAPPER(name) \
225 : PrintInstanceTypeJSON(key, gc_count, #name, name);
226 :
227 : #define VIRTUAL_INSTANCE_TYPE_WRAPPER(name) \
228 : PrintInstanceTypeJSON(key, gc_count, #name, FIRST_VIRTUAL_TYPE + name);
229 :
230 0 : INSTANCE_TYPE_LIST(INSTANCE_TYPE_WRAPPER)
231 0 : VIRTUAL_INSTANCE_TYPE_LIST(VIRTUAL_INSTANCE_TYPE_WRAPPER)
232 :
233 : #undef INSTANCE_TYPE_WRAPPER
234 : #undef VIRTUAL_INSTANCE_TYPE_WRAPPER
235 0 : }
236 :
237 0 : void ObjectStats::DumpInstanceTypeData(std::stringstream& stream,
238 : const char* name, int index) {
239 0 : stream << "\"" << name << "\":{";
240 0 : stream << "\"type\":" << static_cast<int>(index) << ",";
241 0 : stream << "\"overall\":" << object_sizes_[index] << ",";
242 0 : stream << "\"count\":" << object_counts_[index] << ",";
243 0 : stream << "\"over_allocated\":" << over_allocated_[index] << ",";
244 0 : stream << "\"histogram\":";
245 0 : DumpJSONArray(stream, size_histogram_[index], kNumberOfBuckets);
246 0 : stream << ",\"over_allocated_histogram\":";
247 0 : DumpJSONArray(stream, over_allocated_histogram_[index], kNumberOfBuckets);
248 0 : stream << "},";
249 0 : }
250 :
251 0 : void ObjectStats::Dump(std::stringstream& stream) {
252 : double time = isolate()->time_millis_since_init();
253 : int gc_count = heap()->gc_count();
254 :
255 0 : stream << "{";
256 0 : stream << "\"isolate\":\"" << reinterpret_cast<void*>(isolate()) << "\",";
257 0 : stream << "\"id\":" << gc_count << ",";
258 0 : stream << "\"time\":" << time << ",";
259 :
260 : // field_data
261 0 : stream << "\"field_data\":{";
262 0 : stream << "\"tagged_fields\":" << (tagged_fields_count_ * kTaggedSize);
263 : stream << ",\"embedder_fields\":"
264 0 : << (embedder_fields_count_ * kEmbedderDataSlotSize);
265 : stream << ",\"unboxed_double_fields\": "
266 0 : << (unboxed_double_fields_count_ * kDoubleSize);
267 : stream << ",\"other_raw_fields\":"
268 0 : << (raw_fields_count_ * kSystemPointerSize);
269 0 : stream << "}, ";
270 :
271 0 : stream << "\"bucket_sizes\":[";
272 0 : for (int i = 0; i < kNumberOfBuckets; i++) {
273 0 : stream << (1 << (kFirstBucketShift + i));
274 0 : if (i != (kNumberOfBuckets - 1)) stream << ",";
275 : }
276 0 : stream << "],";
277 0 : stream << "\"type_data\":{";
278 :
279 : #define INSTANCE_TYPE_WRAPPER(name) DumpInstanceTypeData(stream, #name, name);
280 :
281 : #define VIRTUAL_INSTANCE_TYPE_WRAPPER(name) \
282 : DumpInstanceTypeData(stream, #name, FIRST_VIRTUAL_TYPE + name);
283 :
284 0 : INSTANCE_TYPE_LIST(INSTANCE_TYPE_WRAPPER);
285 0 : VIRTUAL_INSTANCE_TYPE_LIST(VIRTUAL_INSTANCE_TYPE_WRAPPER)
286 0 : stream << "\"END\":{}}}";
287 :
288 : #undef INSTANCE_TYPE_WRAPPER
289 : #undef VIRTUAL_INSTANCE_TYPE_WRAPPER
290 0 : }
291 :
292 0 : void ObjectStats::CheckpointObjectStats() {
293 : base::MutexGuard lock_guard(object_stats_mutex.Pointer());
294 0 : MemCopy(object_counts_last_time_, object_counts_, sizeof(object_counts_));
295 0 : MemCopy(object_sizes_last_time_, object_sizes_, sizeof(object_sizes_));
296 0 : ClearObjectStats();
297 0 : }
298 :
299 : namespace {
300 :
301 : int Log2ForSize(size_t size) {
302 : DCHECK_GT(size, 0);
303 0 : return kSizetSize * 8 - 1 - base::bits::CountLeadingZeros(size);
304 : }
305 :
306 : } // namespace
307 :
308 0 : int ObjectStats::HistogramIndexFromSize(size_t size) {
309 0 : if (size == 0) return 0;
310 0 : return Min(Max(Log2ForSize(size) + 1 - kFirstBucketShift, 0),
311 0 : kLastValueBucketIndex);
312 : }
313 :
314 0 : void ObjectStats::RecordObjectStats(InstanceType type, size_t size,
315 : size_t over_allocated) {
316 : DCHECK_LE(type, LAST_TYPE);
317 0 : object_counts_[type]++;
318 0 : object_sizes_[type] += size;
319 0 : size_histogram_[type][HistogramIndexFromSize(size)]++;
320 0 : over_allocated_[type] += over_allocated;
321 0 : over_allocated_histogram_[type][HistogramIndexFromSize(size)]++;
322 0 : }
323 :
324 0 : void ObjectStats::RecordVirtualObjectStats(VirtualInstanceType type,
325 : size_t size, size_t over_allocated) {
326 : DCHECK_LE(type, LAST_VIRTUAL_TYPE);
327 0 : object_counts_[FIRST_VIRTUAL_TYPE + type]++;
328 0 : object_sizes_[FIRST_VIRTUAL_TYPE + type] += size;
329 0 : size_histogram_[FIRST_VIRTUAL_TYPE + type][HistogramIndexFromSize(size)]++;
330 0 : over_allocated_[FIRST_VIRTUAL_TYPE + type] += over_allocated;
331 : over_allocated_histogram_[FIRST_VIRTUAL_TYPE + type]
332 0 : [HistogramIndexFromSize(size)]++;
333 0 : }
334 :
335 0 : Isolate* ObjectStats::isolate() { return heap()->isolate(); }
336 :
337 0 : class ObjectStatsCollectorImpl {
338 : public:
339 : enum Phase {
340 : kPhase1,
341 : kPhase2,
342 : };
343 : static const int kNumberOfPhases = kPhase2 + 1;
344 :
345 : ObjectStatsCollectorImpl(Heap* heap, ObjectStats* stats);
346 :
347 : void CollectGlobalStatistics();
348 :
349 : enum class CollectFieldStats { kNo, kYes };
350 : void CollectStatistics(HeapObject obj, Phase phase,
351 : CollectFieldStats collect_field_stats);
352 :
353 : private:
354 : enum CowMode {
355 : kCheckCow,
356 : kIgnoreCow,
357 : };
358 :
359 : Isolate* isolate() { return heap_->isolate(); }
360 :
361 : bool RecordVirtualObjectStats(HeapObject parent, HeapObject obj,
362 : ObjectStats::VirtualInstanceType type,
363 : size_t size, size_t over_allocated,
364 : CowMode check_cow_array = kCheckCow);
365 : void RecordExternalResourceStats(Address resource,
366 : ObjectStats::VirtualInstanceType type,
367 : size_t size);
368 : // Gets size from |ob| and assumes no over allocating.
369 : bool RecordSimpleVirtualObjectStats(HeapObject parent, HeapObject obj,
370 : ObjectStats::VirtualInstanceType type);
371 : // For HashTable it is possible to compute over allocated memory.
372 : template <typename Derived, typename Shape>
373 : void RecordHashTableVirtualObjectStats(HeapObject parent,
374 : HashTable<Derived, Shape> hash_table,
375 : ObjectStats::VirtualInstanceType type);
376 :
377 : bool SameLiveness(HeapObject obj1, HeapObject obj2);
378 : bool CanRecordFixedArray(FixedArrayBase array);
379 : bool IsCowArray(FixedArrayBase array);
380 :
381 : // Blacklist for objects that should not be recorded using
382 : // VirtualObjectStats and RecordSimpleVirtualObjectStats. For recording those
383 : // objects dispatch to the low level ObjectStats::RecordObjectStats manually.
384 : bool ShouldRecordObject(HeapObject object, CowMode check_cow_array);
385 :
386 : void RecordObjectStats(
387 : HeapObject obj, InstanceType type, size_t size,
388 : size_t over_allocated = ObjectStats::kNoOverAllocation);
389 :
390 : // Specific recursion into constant pool or embedded code objects. Records
391 : // FixedArrays and Tuple2.
392 : void RecordVirtualObjectsForConstantPoolOrEmbeddedObjects(
393 : HeapObject parent, HeapObject object,
394 : ObjectStats::VirtualInstanceType type);
395 :
396 : // Details.
397 : void RecordVirtualAllocationSiteDetails(AllocationSite site);
398 : void RecordVirtualBytecodeArrayDetails(BytecodeArray bytecode);
399 : void RecordVirtualCodeDetails(Code code);
400 : void RecordVirtualContext(Context context);
401 : void RecordVirtualFeedbackVectorDetails(FeedbackVector vector);
402 : void RecordVirtualFixedArrayDetails(FixedArray array);
403 : void RecordVirtualFunctionTemplateInfoDetails(FunctionTemplateInfo fti);
404 : void RecordVirtualJSGlobalObjectDetails(JSGlobalObject object);
405 : void RecordVirtualJSObjectDetails(JSObject object);
406 : void RecordVirtualMapDetails(Map map);
407 : void RecordVirtualScriptDetails(Script script);
408 : void RecordVirtualExternalStringDetails(ExternalString script);
409 : void RecordVirtualSharedFunctionInfoDetails(SharedFunctionInfo info);
410 :
411 : void RecordVirtualArrayBoilerplateDescription(
412 : ArrayBoilerplateDescription description);
413 : Heap* heap_;
414 : ObjectStats* stats_;
415 : MarkCompactCollector::NonAtomicMarkingState* marking_state_;
416 : std::unordered_set<HeapObject, Object::Hasher> virtual_objects_;
417 : std::unordered_set<Address> external_resources_;
418 : FieldStatsCollector field_stats_collector_;
419 : };
420 :
421 0 : ObjectStatsCollectorImpl::ObjectStatsCollectorImpl(Heap* heap,
422 : ObjectStats* stats)
423 : : heap_(heap),
424 : stats_(stats),
425 : marking_state_(
426 : heap->mark_compact_collector()->non_atomic_marking_state()),
427 : field_stats_collector_(
428 : &stats->tagged_fields_count_, &stats->embedder_fields_count_,
429 0 : &stats->unboxed_double_fields_count_, &stats->raw_fields_count_) {}
430 :
431 0 : bool ObjectStatsCollectorImpl::ShouldRecordObject(HeapObject obj,
432 : CowMode check_cow_array) {
433 0 : if (obj->IsFixedArrayExact()) {
434 : FixedArray fixed_array = FixedArray::cast(obj);
435 0 : bool cow_check = check_cow_array == kIgnoreCow || !IsCowArray(fixed_array);
436 0 : return CanRecordFixedArray(fixed_array) && cow_check;
437 : }
438 0 : if (obj == ReadOnlyRoots(heap_).empty_property_array()) return false;
439 0 : return true;
440 : }
441 :
442 : template <typename Derived, typename Shape>
443 0 : void ObjectStatsCollectorImpl::RecordHashTableVirtualObjectStats(
444 : HeapObject parent, HashTable<Derived, Shape> hash_table,
445 : ObjectStats::VirtualInstanceType type) {
446 : size_t over_allocated =
447 : (hash_table->Capacity() - (hash_table->NumberOfElements() +
448 : hash_table->NumberOfDeletedElements())) *
449 0 : HashTable<Derived, Shape>::kEntrySize * kTaggedSize;
450 0 : RecordVirtualObjectStats(parent, hash_table, type, hash_table->Size(),
451 : over_allocated);
452 0 : }
453 :
454 0 : bool ObjectStatsCollectorImpl::RecordSimpleVirtualObjectStats(
455 : HeapObject parent, HeapObject obj, ObjectStats::VirtualInstanceType type) {
456 0 : return RecordVirtualObjectStats(parent, obj, type, obj->Size(),
457 0 : ObjectStats::kNoOverAllocation, kCheckCow);
458 : }
459 :
460 0 : bool ObjectStatsCollectorImpl::RecordVirtualObjectStats(
461 : HeapObject parent, HeapObject obj, ObjectStats::VirtualInstanceType type,
462 : size_t size, size_t over_allocated, CowMode check_cow_array) {
463 0 : CHECK_LT(over_allocated, size);
464 0 : if (!SameLiveness(parent, obj) || !ShouldRecordObject(obj, check_cow_array)) {
465 : return false;
466 : }
467 :
468 0 : if (virtual_objects_.find(obj) == virtual_objects_.end()) {
469 : virtual_objects_.insert(obj);
470 0 : stats_->RecordVirtualObjectStats(type, size, over_allocated);
471 0 : return true;
472 : }
473 : return false;
474 : }
475 :
476 0 : void ObjectStatsCollectorImpl::RecordExternalResourceStats(
477 : Address resource, ObjectStats::VirtualInstanceType type, size_t size) {
478 0 : if (external_resources_.find(resource) == external_resources_.end()) {
479 : external_resources_.insert(resource);
480 0 : stats_->RecordVirtualObjectStats(type, size, 0);
481 : }
482 0 : }
483 :
484 0 : void ObjectStatsCollectorImpl::RecordVirtualAllocationSiteDetails(
485 : AllocationSite site) {
486 0 : if (!site->PointsToLiteral()) return;
487 0 : JSObject boilerplate = site->boilerplate();
488 0 : if (boilerplate->IsJSArray()) {
489 : RecordSimpleVirtualObjectStats(site, boilerplate,
490 0 : ObjectStats::JS_ARRAY_BOILERPLATE_TYPE);
491 : // Array boilerplates cannot have properties.
492 : } else {
493 0 : RecordVirtualObjectStats(
494 : site, boilerplate, ObjectStats::JS_OBJECT_BOILERPLATE_TYPE,
495 0 : boilerplate->Size(), ObjectStats::kNoOverAllocation);
496 0 : if (boilerplate->HasFastProperties()) {
497 : // We'll mis-classify the empty_property_array here. Given that there is a
498 : // single instance, this is negligible.
499 0 : PropertyArray properties = boilerplate->property_array();
500 : RecordSimpleVirtualObjectStats(
501 0 : site, properties, ObjectStats::BOILERPLATE_PROPERTY_ARRAY_TYPE);
502 : } else {
503 0 : NameDictionary properties = boilerplate->property_dictionary();
504 : RecordSimpleVirtualObjectStats(
505 0 : site, properties, ObjectStats::BOILERPLATE_PROPERTY_DICTIONARY_TYPE);
506 : }
507 : }
508 0 : FixedArrayBase elements = boilerplate->elements();
509 : RecordSimpleVirtualObjectStats(site, elements,
510 0 : ObjectStats::BOILERPLATE_ELEMENTS_TYPE);
511 : }
512 :
513 0 : void ObjectStatsCollectorImpl::RecordVirtualFunctionTemplateInfoDetails(
514 : FunctionTemplateInfo fti) {
515 : // named_property_handler and indexed_property_handler are recorded as
516 : // INTERCEPTOR_INFO_TYPE.
517 0 : if (!fti->call_code()->IsUndefined(isolate())) {
518 : RecordSimpleVirtualObjectStats(
519 0 : fti, CallHandlerInfo::cast(fti->call_code()),
520 0 : ObjectStats::FUNCTION_TEMPLATE_INFO_ENTRIES_TYPE);
521 : }
522 0 : if (!fti->GetInstanceCallHandler()->IsUndefined(isolate())) {
523 : RecordSimpleVirtualObjectStats(
524 0 : fti, CallHandlerInfo::cast(fti->GetInstanceCallHandler()),
525 0 : ObjectStats::FUNCTION_TEMPLATE_INFO_ENTRIES_TYPE);
526 : }
527 0 : }
528 :
529 0 : void ObjectStatsCollectorImpl::RecordVirtualJSGlobalObjectDetails(
530 : JSGlobalObject object) {
531 : // Properties.
532 0 : GlobalDictionary properties = object->global_dictionary();
533 : RecordHashTableVirtualObjectStats(object, properties,
534 0 : ObjectStats::GLOBAL_PROPERTIES_TYPE);
535 : // Elements.
536 0 : FixedArrayBase elements = object->elements();
537 : RecordSimpleVirtualObjectStats(object, elements,
538 0 : ObjectStats::GLOBAL_ELEMENTS_TYPE);
539 0 : }
540 :
541 0 : void ObjectStatsCollectorImpl::RecordVirtualJSObjectDetails(JSObject object) {
542 : // JSGlobalObject is recorded separately.
543 0 : if (object->IsJSGlobalObject()) return;
544 :
545 : // Uncompiled JSFunction has a separate type.
546 0 : if (object->IsJSFunction() && !JSFunction::cast(object)->is_compiled()) {
547 0 : RecordSimpleVirtualObjectStats(HeapObject(), object,
548 : ObjectStats::JS_UNCOMPILED_FUNCTION_TYPE);
549 : }
550 :
551 : // Properties.
552 0 : if (object->HasFastProperties()) {
553 0 : PropertyArray properties = object->property_array();
554 0 : if (properties != ReadOnlyRoots(heap_).empty_property_array()) {
555 : size_t over_allocated =
556 0 : object->map()->UnusedPropertyFields() * kTaggedSize;
557 0 : RecordVirtualObjectStats(object, properties,
558 : object->map()->is_prototype_map()
559 : ? ObjectStats::PROTOTYPE_PROPERTY_ARRAY_TYPE
560 : : ObjectStats::OBJECT_PROPERTY_ARRAY_TYPE,
561 0 : properties->Size(), over_allocated);
562 : }
563 : } else {
564 0 : NameDictionary properties = object->property_dictionary();
565 0 : RecordHashTableVirtualObjectStats(
566 : object, properties,
567 : object->map()->is_prototype_map()
568 : ? ObjectStats::PROTOTYPE_PROPERTY_DICTIONARY_TYPE
569 0 : : ObjectStats::OBJECT_PROPERTY_DICTIONARY_TYPE);
570 : }
571 :
572 : // Elements.
573 0 : FixedArrayBase elements = object->elements();
574 0 : if (object->HasDictionaryElements()) {
575 0 : RecordHashTableVirtualObjectStats(
576 0 : object, NumberDictionary::cast(elements),
577 : object->IsJSArray() ? ObjectStats::ARRAY_DICTIONARY_ELEMENTS_TYPE
578 0 : : ObjectStats::OBJECT_DICTIONARY_ELEMENTS_TYPE);
579 0 : } else if (object->IsJSArray()) {
580 0 : if (elements != ReadOnlyRoots(heap_).empty_fixed_array()) {
581 : size_t element_size =
582 0 : (elements->Size() - FixedArrayBase::kHeaderSize) / elements->length();
583 0 : uint32_t length = JSArray::cast(object)->length()->Number();
584 0 : size_t over_allocated = (elements->length() - length) * element_size;
585 0 : RecordVirtualObjectStats(object, elements,
586 : ObjectStats::ARRAY_ELEMENTS_TYPE,
587 0 : elements->Size(), over_allocated);
588 : }
589 : } else {
590 : RecordSimpleVirtualObjectStats(object, elements,
591 0 : ObjectStats::OBJECT_ELEMENTS_TYPE);
592 : }
593 :
594 : // JSCollections.
595 0 : if (object->IsJSCollection()) {
596 : // TODO(bmeurer): Properly compute over-allocation here.
597 : RecordSimpleVirtualObjectStats(
598 0 : object, FixedArray::cast(JSCollection::cast(object)->table()),
599 0 : ObjectStats::JS_COLLECTION_TABLE_TYPE);
600 : }
601 : }
602 :
603 0 : static ObjectStats::VirtualInstanceType GetFeedbackSlotType(
604 : MaybeObject maybe_obj, FeedbackSlotKind kind, Isolate* isolate) {
605 0 : if (maybe_obj->IsCleared())
606 : return ObjectStats::FEEDBACK_VECTOR_SLOT_OTHER_TYPE;
607 : Object obj = maybe_obj->GetHeapObjectOrSmi();
608 0 : switch (kind) {
609 : case FeedbackSlotKind::kCall:
610 0 : if (obj == *isolate->factory()->uninitialized_symbol() ||
611 : obj == *isolate->factory()->premonomorphic_symbol()) {
612 : return ObjectStats::FEEDBACK_VECTOR_SLOT_CALL_UNUSED_TYPE;
613 : }
614 0 : return ObjectStats::FEEDBACK_VECTOR_SLOT_CALL_TYPE;
615 :
616 : case FeedbackSlotKind::kLoadProperty:
617 : case FeedbackSlotKind::kLoadGlobalInsideTypeof:
618 : case FeedbackSlotKind::kLoadGlobalNotInsideTypeof:
619 : case FeedbackSlotKind::kLoadKeyed:
620 : case FeedbackSlotKind::kHasKeyed:
621 0 : if (obj == *isolate->factory()->uninitialized_symbol() ||
622 : obj == *isolate->factory()->premonomorphic_symbol()) {
623 : return ObjectStats::FEEDBACK_VECTOR_SLOT_LOAD_UNUSED_TYPE;
624 : }
625 0 : return ObjectStats::FEEDBACK_VECTOR_SLOT_LOAD_TYPE;
626 :
627 : case FeedbackSlotKind::kStoreNamedSloppy:
628 : case FeedbackSlotKind::kStoreNamedStrict:
629 : case FeedbackSlotKind::kStoreOwnNamed:
630 : case FeedbackSlotKind::kStoreGlobalSloppy:
631 : case FeedbackSlotKind::kStoreGlobalStrict:
632 : case FeedbackSlotKind::kStoreKeyedSloppy:
633 : case FeedbackSlotKind::kStoreKeyedStrict:
634 0 : if (obj == *isolate->factory()->uninitialized_symbol() ||
635 : obj == *isolate->factory()->premonomorphic_symbol()) {
636 : return ObjectStats::FEEDBACK_VECTOR_SLOT_STORE_UNUSED_TYPE;
637 : }
638 0 : return ObjectStats::FEEDBACK_VECTOR_SLOT_STORE_TYPE;
639 :
640 : case FeedbackSlotKind::kBinaryOp:
641 : case FeedbackSlotKind::kCompareOp:
642 : return ObjectStats::FEEDBACK_VECTOR_SLOT_ENUM_TYPE;
643 :
644 : default:
645 0 : return ObjectStats::FEEDBACK_VECTOR_SLOT_OTHER_TYPE;
646 : }
647 : }
648 :
649 0 : void ObjectStatsCollectorImpl::RecordVirtualFeedbackVectorDetails(
650 : FeedbackVector vector) {
651 0 : if (virtual_objects_.find(vector) != virtual_objects_.end()) return;
652 : // Manually insert the feedback vector into the virtual object list, since
653 : // we're logging its component parts separately.
654 : virtual_objects_.insert(vector);
655 :
656 : size_t calculated_size = 0;
657 :
658 : // Log the feedback vector's header (fixed fields).
659 : size_t header_size = vector->slots_start().address() - vector->address();
660 0 : stats_->RecordVirtualObjectStats(ObjectStats::FEEDBACK_VECTOR_HEADER_TYPE,
661 0 : header_size, ObjectStats::kNoOverAllocation);
662 : calculated_size += header_size;
663 :
664 : // Iterate over the feedback slots and log each one.
665 0 : if (!vector->shared_function_info()->HasFeedbackMetadata()) return;
666 :
667 : FeedbackMetadataIterator it(vector->metadata());
668 0 : while (it.HasNext()) {
669 0 : FeedbackSlot slot = it.Next();
670 : // Log the entry (or entries) taken up by this slot.
671 0 : size_t slot_size = it.entry_size() * kTaggedSize;
672 0 : stats_->RecordVirtualObjectStats(
673 0 : GetFeedbackSlotType(vector->Get(slot), it.kind(), heap_->isolate()),
674 0 : slot_size, ObjectStats::kNoOverAllocation);
675 0 : calculated_size += slot_size;
676 :
677 : // Log the monomorphic/polymorphic helper objects that this slot owns.
678 0 : for (int i = 0; i < it.entry_size(); i++) {
679 0 : MaybeObject raw_object = vector->get(slot.ToInt() + i);
680 : HeapObject object;
681 0 : if (raw_object->GetHeapObject(&object)) {
682 0 : if (object->IsCell() || object->IsWeakFixedArray()) {
683 : RecordSimpleVirtualObjectStats(
684 0 : vector, object, ObjectStats::FEEDBACK_VECTOR_ENTRY_TYPE);
685 : }
686 : }
687 : }
688 : }
689 :
690 0 : CHECK_EQ(calculated_size, vector->Size());
691 : }
692 :
693 0 : void ObjectStatsCollectorImpl::RecordVirtualFixedArrayDetails(
694 : FixedArray array) {
695 0 : if (IsCowArray(array)) {
696 0 : RecordVirtualObjectStats(HeapObject(), array, ObjectStats::COW_ARRAY_TYPE,
697 0 : array->Size(), ObjectStats::kNoOverAllocation,
698 0 : kIgnoreCow);
699 : }
700 0 : }
701 :
702 0 : void ObjectStatsCollectorImpl::CollectStatistics(
703 : HeapObject obj, Phase phase, CollectFieldStats collect_field_stats) {
704 0 : Map map = obj->map();
705 0 : switch (phase) {
706 : case kPhase1:
707 0 : if (obj->IsFeedbackVector()) {
708 0 : RecordVirtualFeedbackVectorDetails(FeedbackVector::cast(obj));
709 0 : } else if (obj->IsMap()) {
710 0 : RecordVirtualMapDetails(Map::cast(obj));
711 0 : } else if (obj->IsBytecodeArray()) {
712 0 : RecordVirtualBytecodeArrayDetails(BytecodeArray::cast(obj));
713 0 : } else if (obj->IsCode()) {
714 0 : RecordVirtualCodeDetails(Code::cast(obj));
715 0 : } else if (obj->IsFunctionTemplateInfo()) {
716 : RecordVirtualFunctionTemplateInfoDetails(
717 0 : FunctionTemplateInfo::cast(obj));
718 0 : } else if (obj->IsJSGlobalObject()) {
719 0 : RecordVirtualJSGlobalObjectDetails(JSGlobalObject::cast(obj));
720 0 : } else if (obj->IsJSObject()) {
721 : // This phase needs to come after RecordVirtualAllocationSiteDetails
722 : // to properly split among boilerplates.
723 0 : RecordVirtualJSObjectDetails(JSObject::cast(obj));
724 0 : } else if (obj->IsSharedFunctionInfo()) {
725 0 : RecordVirtualSharedFunctionInfoDetails(SharedFunctionInfo::cast(obj));
726 0 : } else if (obj->IsContext()) {
727 0 : RecordVirtualContext(Context::cast(obj));
728 0 : } else if (obj->IsScript()) {
729 0 : RecordVirtualScriptDetails(Script::cast(obj));
730 0 : } else if (obj->IsArrayBoilerplateDescription()) {
731 : RecordVirtualArrayBoilerplateDescription(
732 : ArrayBoilerplateDescription::cast(obj));
733 0 : } else if (obj->IsFixedArrayExact()) {
734 : // Has to go last as it triggers too eagerly.
735 0 : RecordVirtualFixedArrayDetails(FixedArray::cast(obj));
736 : }
737 : break;
738 : case kPhase2:
739 0 : if (obj->IsExternalString()) {
740 : // This has to be in Phase2 to avoid conflicting with recording Script
741 : // sources. We still want to run RecordObjectStats after though.
742 0 : RecordVirtualExternalStringDetails(ExternalString::cast(obj));
743 : }
744 : size_t over_allocated = ObjectStats::kNoOverAllocation;
745 0 : if (obj->IsJSObject()) {
746 0 : over_allocated = map->instance_size() - map->UsedInstanceSize();
747 : }
748 0 : RecordObjectStats(obj, map->instance_type(), obj->Size(), over_allocated);
749 0 : if (collect_field_stats == CollectFieldStats::kYes) {
750 0 : field_stats_collector_.RecordStats(obj);
751 : }
752 : break;
753 : }
754 0 : }
755 :
756 0 : void ObjectStatsCollectorImpl::CollectGlobalStatistics() {
757 : // Iterate boilerplates first to disambiguate them from regular JS objects.
758 0 : Object list = heap_->allocation_sites_list();
759 0 : while (list->IsAllocationSite()) {
760 : AllocationSite site = AllocationSite::cast(list);
761 0 : RecordVirtualAllocationSiteDetails(site);
762 : list = site->weak_next();
763 : }
764 :
765 : // FixedArray.
766 0 : RecordSimpleVirtualObjectStats(HeapObject(), heap_->serialized_objects(),
767 0 : ObjectStats::SERIALIZED_OBJECTS_TYPE);
768 0 : RecordSimpleVirtualObjectStats(HeapObject(), heap_->number_string_cache(),
769 0 : ObjectStats::NUMBER_STRING_CACHE_TYPE);
770 0 : RecordSimpleVirtualObjectStats(
771 0 : HeapObject(), heap_->single_character_string_cache(),
772 0 : ObjectStats::SINGLE_CHARACTER_STRING_CACHE_TYPE);
773 0 : RecordSimpleVirtualObjectStats(HeapObject(), heap_->string_split_cache(),
774 0 : ObjectStats::STRING_SPLIT_CACHE_TYPE);
775 0 : RecordSimpleVirtualObjectStats(HeapObject(), heap_->regexp_multiple_cache(),
776 0 : ObjectStats::REGEXP_MULTIPLE_CACHE_TYPE);
777 0 : RecordSimpleVirtualObjectStats(HeapObject(), heap_->retained_maps(),
778 0 : ObjectStats::RETAINED_MAPS_TYPE);
779 :
780 : // WeakArrayList.
781 0 : RecordSimpleVirtualObjectStats(
782 : HeapObject(),
783 0 : WeakArrayList::cast(heap_->noscript_shared_function_infos()),
784 0 : ObjectStats::NOSCRIPT_SHARED_FUNCTION_INFOS_TYPE);
785 0 : RecordSimpleVirtualObjectStats(HeapObject(),
786 0 : WeakArrayList::cast(heap_->script_list()),
787 0 : ObjectStats::SCRIPT_LIST_TYPE);
788 0 : }
789 :
790 0 : void ObjectStatsCollectorImpl::RecordObjectStats(HeapObject obj,
791 : InstanceType type, size_t size,
792 : size_t over_allocated) {
793 0 : if (virtual_objects_.find(obj) == virtual_objects_.end()) {
794 0 : stats_->RecordObjectStats(type, size, over_allocated);
795 : }
796 0 : }
797 :
798 0 : bool ObjectStatsCollectorImpl::CanRecordFixedArray(FixedArrayBase array) {
799 0 : ReadOnlyRoots roots(heap_);
800 0 : return array != roots.empty_fixed_array() &&
801 0 : array != roots.empty_sloppy_arguments_elements() &&
802 0 : array != roots.empty_slow_element_dictionary() &&
803 0 : array != roots.empty_property_dictionary();
804 : }
805 :
806 0 : bool ObjectStatsCollectorImpl::IsCowArray(FixedArrayBase array) {
807 0 : return array->map() == ReadOnlyRoots(heap_).fixed_cow_array_map();
808 : }
809 :
810 0 : bool ObjectStatsCollectorImpl::SameLiveness(HeapObject obj1, HeapObject obj2) {
811 0 : return obj1.is_null() || obj2.is_null() ||
812 0 : marking_state_->Color(obj1) == marking_state_->Color(obj2);
813 : }
814 :
815 0 : void ObjectStatsCollectorImpl::RecordVirtualMapDetails(Map map) {
816 : // TODO(mlippautz): map->dependent_code(): DEPENDENT_CODE_TYPE.
817 :
818 : // For Map we want to distinguish between various different states
819 : // to get a better picture of what's going on in MapSpace. This
820 : // method computes the virtual instance type to use for a given map,
821 : // using MAP_TYPE for regular maps that aren't special in any way.
822 0 : if (map->is_prototype_map()) {
823 0 : if (map->is_dictionary_map()) {
824 0 : RecordSimpleVirtualObjectStats(
825 0 : HeapObject(), map, ObjectStats::MAP_PROTOTYPE_DICTIONARY_TYPE);
826 0 : } else if (map->is_abandoned_prototype_map()) {
827 0 : RecordSimpleVirtualObjectStats(HeapObject(), map,
828 0 : ObjectStats::MAP_ABANDONED_PROTOTYPE_TYPE);
829 : } else {
830 0 : RecordSimpleVirtualObjectStats(HeapObject(), map,
831 0 : ObjectStats::MAP_PROTOTYPE_TYPE);
832 : }
833 0 : } else if (map->is_deprecated()) {
834 0 : RecordSimpleVirtualObjectStats(HeapObject(), map,
835 0 : ObjectStats::MAP_DEPRECATED_TYPE);
836 0 : } else if (map->is_dictionary_map()) {
837 0 : RecordSimpleVirtualObjectStats(HeapObject(), map,
838 0 : ObjectStats::MAP_DICTIONARY_TYPE);
839 0 : } else if (map->is_stable()) {
840 0 : RecordSimpleVirtualObjectStats(HeapObject(), map,
841 0 : ObjectStats::MAP_STABLE_TYPE);
842 : } else {
843 : // This will be logged as MAP_TYPE in Phase2.
844 : }
845 :
846 : DescriptorArray array = map->instance_descriptors();
847 0 : if (map->owns_descriptors() &&
848 0 : array != ReadOnlyRoots(heap_).empty_descriptor_array()) {
849 : // Generally DescriptorArrays have their own instance type already
850 : // (DESCRIPTOR_ARRAY_TYPE), but we'd like to be able to tell which
851 : // of those are for (abandoned) prototypes, and which of those are
852 : // owned by deprecated maps.
853 0 : if (map->is_prototype_map()) {
854 : RecordSimpleVirtualObjectStats(
855 0 : map, array, ObjectStats::PROTOTYPE_DESCRIPTOR_ARRAY_TYPE);
856 0 : } else if (map->is_deprecated()) {
857 : RecordSimpleVirtualObjectStats(
858 0 : map, array, ObjectStats::DEPRECATED_DESCRIPTOR_ARRAY_TYPE);
859 : }
860 :
861 : EnumCache enum_cache = array->enum_cache();
862 0 : RecordSimpleVirtualObjectStats(array, enum_cache->keys(),
863 0 : ObjectStats::ENUM_KEYS_CACHE_TYPE);
864 0 : RecordSimpleVirtualObjectStats(array, enum_cache->indices(),
865 0 : ObjectStats::ENUM_INDICES_CACHE_TYPE);
866 : }
867 :
868 0 : if (map->is_prototype_map()) {
869 0 : if (map->prototype_info()->IsPrototypeInfo()) {
870 : PrototypeInfo info = PrototypeInfo::cast(map->prototype_info());
871 : Object users = info->prototype_users();
872 0 : if (users->IsWeakFixedArray()) {
873 0 : RecordSimpleVirtualObjectStats(map, WeakArrayList::cast(users),
874 0 : ObjectStats::PROTOTYPE_USERS_TYPE);
875 : }
876 : }
877 : }
878 0 : }
879 :
880 0 : void ObjectStatsCollectorImpl::RecordVirtualScriptDetails(Script script) {
881 : RecordSimpleVirtualObjectStats(
882 0 : script, script->shared_function_infos(),
883 0 : ObjectStats::SCRIPT_SHARED_FUNCTION_INFOS_TYPE);
884 :
885 : // Log the size of external source code.
886 : Object raw_source = script->source();
887 0 : if (raw_source->IsExternalString()) {
888 : // The contents of external strings aren't on the heap, so we have to record
889 : // them manually. The on-heap String object is recorded indepentendely in
890 : // the normal pass.
891 0 : ExternalString string = ExternalString::cast(raw_source);
892 : Address resource = string->resource_as_address();
893 0 : size_t off_heap_size = string->ExternalPayloadSize();
894 0 : RecordExternalResourceStats(
895 : resource,
896 : string->IsOneByteRepresentation()
897 : ? ObjectStats::SCRIPT_SOURCE_EXTERNAL_ONE_BYTE_TYPE
898 : : ObjectStats::SCRIPT_SOURCE_EXTERNAL_TWO_BYTE_TYPE,
899 0 : off_heap_size);
900 0 : } else if (raw_source->IsString()) {
901 : String source = String::cast(raw_source);
902 0 : RecordSimpleVirtualObjectStats(
903 : script, source,
904 : source->IsOneByteRepresentation()
905 : ? ObjectStats::SCRIPT_SOURCE_NON_EXTERNAL_ONE_BYTE_TYPE
906 0 : : ObjectStats::SCRIPT_SOURCE_NON_EXTERNAL_TWO_BYTE_TYPE);
907 : }
908 0 : }
909 :
910 0 : void ObjectStatsCollectorImpl::RecordVirtualExternalStringDetails(
911 : ExternalString string) {
912 : // Track the external string resource size in a separate category.
913 :
914 : Address resource = string->resource_as_address();
915 0 : size_t off_heap_size = string->ExternalPayloadSize();
916 0 : RecordExternalResourceStats(
917 : resource,
918 : string->IsOneByteRepresentation()
919 : ? ObjectStats::STRING_EXTERNAL_RESOURCE_ONE_BYTE_TYPE
920 : : ObjectStats::STRING_EXTERNAL_RESOURCE_TWO_BYTE_TYPE,
921 0 : off_heap_size);
922 0 : }
923 :
924 0 : void ObjectStatsCollectorImpl::RecordVirtualSharedFunctionInfoDetails(
925 : SharedFunctionInfo info) {
926 : // Uncompiled SharedFunctionInfo gets its own category.
927 0 : if (!info->is_compiled()) {
928 0 : RecordSimpleVirtualObjectStats(
929 0 : HeapObject(), info, ObjectStats::UNCOMPILED_SHARED_FUNCTION_INFO_TYPE);
930 : }
931 0 : }
932 :
933 0 : void ObjectStatsCollectorImpl::RecordVirtualArrayBoilerplateDescription(
934 : ArrayBoilerplateDescription description) {
935 : RecordVirtualObjectsForConstantPoolOrEmbeddedObjects(
936 0 : description, description->constant_elements(),
937 0 : ObjectStats::ARRAY_BOILERPLATE_DESCRIPTION_ELEMENTS_TYPE);
938 0 : }
939 :
940 0 : void ObjectStatsCollectorImpl::
941 : RecordVirtualObjectsForConstantPoolOrEmbeddedObjects(
942 : HeapObject parent, HeapObject object,
943 : ObjectStats::VirtualInstanceType type) {
944 0 : if (!RecordSimpleVirtualObjectStats(parent, object, type)) return;
945 0 : if (object->IsFixedArrayExact()) {
946 : FixedArray array = FixedArray::cast(object);
947 0 : for (int i = 0; i < array->length(); i++) {
948 : Object entry = array->get(i);
949 0 : if (!entry->IsHeapObject()) continue;
950 : RecordVirtualObjectsForConstantPoolOrEmbeddedObjects(
951 0 : array, HeapObject::cast(entry), type);
952 : }
953 : }
954 : }
955 :
956 0 : void ObjectStatsCollectorImpl::RecordVirtualBytecodeArrayDetails(
957 : BytecodeArray bytecode) {
958 : RecordSimpleVirtualObjectStats(
959 0 : bytecode, bytecode->constant_pool(),
960 0 : ObjectStats::BYTECODE_ARRAY_CONSTANT_POOL_TYPE);
961 : // FixedArrays on constant pool are used for holding descriptor information.
962 : // They are shared with optimized code.
963 : FixedArray constant_pool = FixedArray::cast(bytecode->constant_pool());
964 0 : for (int i = 0; i < constant_pool->length(); i++) {
965 : Object entry = constant_pool->get(i);
966 0 : if (entry->IsFixedArrayExact()) {
967 : RecordVirtualObjectsForConstantPoolOrEmbeddedObjects(
968 : constant_pool, HeapObject::cast(entry),
969 0 : ObjectStats::EMBEDDED_OBJECT_TYPE);
970 : }
971 : }
972 : RecordSimpleVirtualObjectStats(
973 0 : bytecode, bytecode->handler_table(),
974 0 : ObjectStats::BYTECODE_ARRAY_HANDLER_TABLE_TYPE);
975 0 : if (bytecode->HasSourcePositionTable()) {
976 0 : RecordSimpleVirtualObjectStats(bytecode, bytecode->SourcePositionTable(),
977 0 : ObjectStats::SOURCE_POSITION_TABLE_TYPE);
978 : }
979 0 : }
980 :
981 : namespace {
982 :
983 0 : ObjectStats::VirtualInstanceType CodeKindToVirtualInstanceType(
984 : Code::Kind kind) {
985 0 : switch (kind) {
986 : #define CODE_KIND_CASE(type) \
987 : case Code::type: \
988 : return ObjectStats::type;
989 0 : CODE_KIND_LIST(CODE_KIND_CASE)
990 : #undef CODE_KIND_CASE
991 : default:
992 0 : UNREACHABLE();
993 : }
994 : UNREACHABLE();
995 : }
996 :
997 : } // namespace
998 :
999 0 : void ObjectStatsCollectorImpl::RecordVirtualCodeDetails(Code code) {
1000 0 : RecordSimpleVirtualObjectStats(HeapObject(), code,
1001 0 : CodeKindToVirtualInstanceType(code->kind()));
1002 0 : RecordSimpleVirtualObjectStats(code, code->deoptimization_data(),
1003 0 : ObjectStats::DEOPTIMIZATION_DATA_TYPE);
1004 0 : RecordSimpleVirtualObjectStats(code, code->relocation_info(),
1005 0 : ObjectStats::RELOC_INFO_TYPE);
1006 : Object source_position_table = code->source_position_table();
1007 0 : if (source_position_table->IsSourcePositionTableWithFrameCache()) {
1008 : RecordSimpleVirtualObjectStats(
1009 : code,
1010 : SourcePositionTableWithFrameCache::cast(source_position_table)
1011 0 : ->source_position_table(),
1012 0 : ObjectStats::SOURCE_POSITION_TABLE_TYPE);
1013 0 : } else if (source_position_table->IsHeapObject()) {
1014 : RecordSimpleVirtualObjectStats(code,
1015 : HeapObject::cast(source_position_table),
1016 0 : ObjectStats::SOURCE_POSITION_TABLE_TYPE);
1017 : }
1018 0 : if (code->kind() == Code::Kind::OPTIMIZED_FUNCTION) {
1019 : DeoptimizationData input_data =
1020 : DeoptimizationData::cast(code->deoptimization_data());
1021 0 : if (input_data->length() > 0) {
1022 0 : RecordSimpleVirtualObjectStats(code->deoptimization_data(),
1023 0 : input_data->LiteralArray(),
1024 0 : ObjectStats::OPTIMIZED_CODE_LITERALS_TYPE);
1025 : }
1026 : }
1027 : int const mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
1028 0 : for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
1029 : RelocInfo::Mode mode = it.rinfo()->rmode();
1030 0 : if (mode == RelocInfo::EMBEDDED_OBJECT) {
1031 : Object target = it.rinfo()->target_object();
1032 0 : if (target->IsFixedArrayExact()) {
1033 : RecordVirtualObjectsForConstantPoolOrEmbeddedObjects(
1034 0 : code, HeapObject::cast(target), ObjectStats::EMBEDDED_OBJECT_TYPE);
1035 : }
1036 : }
1037 : }
1038 0 : }
1039 :
1040 0 : void ObjectStatsCollectorImpl::RecordVirtualContext(Context context) {
1041 0 : if (context->IsNativeContext()) {
1042 0 : RecordObjectStats(context, NATIVE_CONTEXT_TYPE, context->Size());
1043 0 : } else if (context->IsFunctionContext()) {
1044 0 : RecordObjectStats(context, FUNCTION_CONTEXT_TYPE, context->Size());
1045 : } else {
1046 0 : RecordSimpleVirtualObjectStats(HeapObject(), context,
1047 0 : ObjectStats::OTHER_CONTEXT_TYPE);
1048 : }
1049 0 : }
1050 :
1051 : class ObjectStatsVisitor {
1052 : public:
1053 : ObjectStatsVisitor(Heap* heap, ObjectStatsCollectorImpl* live_collector,
1054 : ObjectStatsCollectorImpl* dead_collector,
1055 : ObjectStatsCollectorImpl::Phase phase)
1056 : : live_collector_(live_collector),
1057 : dead_collector_(dead_collector),
1058 : marking_state_(
1059 : heap->mark_compact_collector()->non_atomic_marking_state()),
1060 0 : phase_(phase) {}
1061 :
1062 0 : bool Visit(HeapObject obj, int size) {
1063 0 : if (marking_state_->IsBlack(obj)) {
1064 0 : live_collector_->CollectStatistics(
1065 0 : obj, phase_, ObjectStatsCollectorImpl::CollectFieldStats::kYes);
1066 : } else {
1067 : DCHECK(!marking_state_->IsGrey(obj));
1068 0 : dead_collector_->CollectStatistics(
1069 0 : obj, phase_, ObjectStatsCollectorImpl::CollectFieldStats::kNo);
1070 : }
1071 0 : return true;
1072 : }
1073 :
1074 : private:
1075 : ObjectStatsCollectorImpl* live_collector_;
1076 : ObjectStatsCollectorImpl* dead_collector_;
1077 : MarkCompactCollector::NonAtomicMarkingState* marking_state_;
1078 : ObjectStatsCollectorImpl::Phase phase_;
1079 : };
1080 :
1081 : namespace {
1082 :
1083 0 : void IterateHeap(Heap* heap, ObjectStatsVisitor* visitor) {
1084 0 : SpaceIterator space_it(heap);
1085 0 : HeapObject obj;
1086 0 : while (space_it.has_next()) {
1087 0 : std::unique_ptr<ObjectIterator> it(space_it.next()->GetObjectIterator());
1088 : ObjectIterator* obj_it = it.get();
1089 0 : for (obj = obj_it->Next(); !obj.is_null(); obj = obj_it->Next()) {
1090 0 : visitor->Visit(obj, obj->Size());
1091 : }
1092 : }
1093 0 : }
1094 :
1095 : } // namespace
1096 :
1097 0 : void ObjectStatsCollector::Collect() {
1098 0 : ObjectStatsCollectorImpl live_collector(heap_, live_);
1099 0 : ObjectStatsCollectorImpl dead_collector(heap_, dead_);
1100 0 : live_collector.CollectGlobalStatistics();
1101 0 : for (int i = 0; i < ObjectStatsCollectorImpl::kNumberOfPhases; i++) {
1102 0 : ObjectStatsVisitor visitor(heap_, &live_collector, &dead_collector,
1103 0 : static_cast<ObjectStatsCollectorImpl::Phase>(i));
1104 0 : IterateHeap(heap_, &visitor);
1105 : }
1106 0 : }
1107 :
1108 : } // namespace internal
1109 122036 : } // namespace v8
|