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