Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | #ifndef V8_HEAP_OBJECT_STATS_H_ |
| 6 | #define V8_HEAP_OBJECT_STATS_H_ |
| 7 | |
| 8 | #include "src/heap/heap.h" |
| 9 | #include "src/heap/objects-visiting.h" |
| 10 | #include "src/objects.h" |
| 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | class ObjectStats { |
| 16 | public: |
| 17 | explicit ObjectStats(Heap* heap) : heap_(heap) {} |
| 18 | |
| 19 | // ObjectStats are kept in two arrays, counts and sizes. Related stats are |
| 20 | // stored in a contiguous linear buffer. Stats groups are stored one after |
| 21 | // another. |
| 22 | enum { |
| 23 | FIRST_CODE_KIND_SUB_TYPE = LAST_TYPE + 1, |
| 24 | FIRST_FIXED_ARRAY_SUB_TYPE = |
| 25 | FIRST_CODE_KIND_SUB_TYPE + Code::NUMBER_OF_KINDS, |
| 26 | FIRST_CODE_AGE_SUB_TYPE = |
| 27 | FIRST_FIXED_ARRAY_SUB_TYPE + LAST_FIXED_ARRAY_SUB_TYPE + 1, |
| 28 | OBJECT_STATS_COUNT = FIRST_CODE_AGE_SUB_TYPE + Code::kCodeAgeCount + 1 |
| 29 | }; |
| 30 | |
| 31 | void ClearObjectStats(bool clear_last_time_stats = false); |
| 32 | |
| 33 | void TraceObjectStats(); |
| 34 | void TraceObjectStat(const char* name, int count, int size, double time); |
| 35 | void CheckpointObjectStats(); |
| 36 | |
| 37 | void RecordObjectStats(InstanceType type, size_t size) { |
| 38 | DCHECK(type <= LAST_TYPE); |
| 39 | object_counts_[type]++; |
| 40 | object_sizes_[type] += size; |
| 41 | } |
| 42 | |
| 43 | void RecordCodeSubTypeStats(int code_sub_type, int code_age, size_t size) { |
| 44 | int code_sub_type_index = FIRST_CODE_KIND_SUB_TYPE + code_sub_type; |
| 45 | int code_age_index = |
| 46 | FIRST_CODE_AGE_SUB_TYPE + code_age - Code::kFirstCodeAge; |
| 47 | DCHECK(code_sub_type_index >= FIRST_CODE_KIND_SUB_TYPE && |
| 48 | code_sub_type_index < FIRST_CODE_AGE_SUB_TYPE); |
| 49 | DCHECK(code_age_index >= FIRST_CODE_AGE_SUB_TYPE && |
| 50 | code_age_index < OBJECT_STATS_COUNT); |
| 51 | object_counts_[code_sub_type_index]++; |
| 52 | object_sizes_[code_sub_type_index] += size; |
| 53 | object_counts_[code_age_index]++; |
| 54 | object_sizes_[code_age_index] += size; |
| 55 | } |
| 56 | |
| 57 | void RecordFixedArraySubTypeStats(int array_sub_type, size_t size) { |
| 58 | DCHECK(array_sub_type <= LAST_FIXED_ARRAY_SUB_TYPE); |
| 59 | object_counts_[FIRST_FIXED_ARRAY_SUB_TYPE + array_sub_type]++; |
| 60 | object_sizes_[FIRST_FIXED_ARRAY_SUB_TYPE + array_sub_type] += size; |
| 61 | } |
| 62 | |
| 63 | size_t object_count_last_gc(size_t index) { |
| 64 | return object_counts_last_time_[index]; |
| 65 | } |
| 66 | |
| 67 | size_t object_size_last_gc(size_t index) { |
| 68 | return object_sizes_last_time_[index]; |
| 69 | } |
| 70 | |
| 71 | Isolate* isolate(); |
| 72 | Heap* heap() { return heap_; } |
| 73 | |
| 74 | private: |
| 75 | Heap* heap_; |
| 76 | |
| 77 | // Object counts and used memory by InstanceType |
| 78 | size_t object_counts_[OBJECT_STATS_COUNT]; |
| 79 | size_t object_counts_last_time_[OBJECT_STATS_COUNT]; |
| 80 | size_t object_sizes_[OBJECT_STATS_COUNT]; |
| 81 | size_t object_sizes_last_time_[OBJECT_STATS_COUNT]; |
| 82 | }; |
| 83 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 84 | class ObjectStatsCollector { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | public: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 86 | static void CollectStatistics(StaticVisitorBase::VisitorId id, Map* map, |
| 87 | HeapObject* obj); |
| 88 | static void CollectFixedArrayStatistics(HeapObject* obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | |
| 90 | static void CountFixedArray(FixedArrayBase* fixed_array, |
| 91 | FixedArraySubInstanceType fast_type, |
| 92 | FixedArraySubInstanceType dictionary_type); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 93 | static void RecordMapStats(Map* map, HeapObject* obj); |
| 94 | static void RecordCodeStats(Map* map, HeapObject* obj); |
| 95 | static void RecordSharedFunctionInfoStats(Map* map, HeapObject* obj); |
| 96 | static void RecordFixedArrayStats(Map* map, HeapObject* obj); |
| 97 | }; |
| 98 | |
| 99 | class MarkCompactObjectStatsVisitor |
| 100 | : public StaticMarkingVisitor<MarkCompactObjectStatsVisitor> { |
| 101 | public: |
| 102 | static void Initialize(VisitorDispatchTable<Callback>* original); |
| 103 | |
| 104 | template <VisitorId id> |
| 105 | static inline void Visit(Map* map, HeapObject* obj); |
| 106 | }; |
| 107 | |
| 108 | class IncrementalMarkingObjectStatsVisitor |
| 109 | : public StaticMarkingVisitor<IncrementalMarkingObjectStatsVisitor> { |
| 110 | public: |
| 111 | static void Initialize(VisitorDispatchTable<Callback>* original); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | |
| 113 | template <VisitorId id> |
| 114 | static inline void Visit(Map* map, HeapObject* obj); |
| 115 | }; |
| 116 | |
| 117 | } // namespace internal |
| 118 | } // namespace v8 |
| 119 | |
| 120 | #endif // V8_HEAP_OBJECT_STATS_H_ |