Line data Source code
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/objects.h"
9 : #include "src/objects/code.h"
10 :
11 : // These instance types do not exist for actual use but are merely introduced
12 : // for object stats tracing. In contrast to Code and FixedArray sub types
13 : // these types are not known to other counters outside of object stats
14 : // tracing.
15 : //
16 : // Update LAST_VIRTUAL_TYPE below when changing this macro.
17 : #define VIRTUAL_INSTANCE_TYPE_LIST(V) \
18 : CODE_KIND_LIST(V) \
19 : V(ARRAY_BOILERPLATE_DESCRIPTION_ELEMENTS_TYPE) \
20 : V(BOILERPLATE_ELEMENTS_TYPE) \
21 : V(BOILERPLATE_PROPERTY_ARRAY_TYPE) \
22 : V(BOILERPLATE_PROPERTY_DICTIONARY_TYPE) \
23 : V(BYTECODE_ARRAY_CONSTANT_POOL_TYPE) \
24 : V(BYTECODE_ARRAY_HANDLER_TABLE_TYPE) \
25 : V(COW_ARRAY_TYPE) \
26 : V(DEOPTIMIZATION_DATA_TYPE) \
27 : V(DEPENDENT_CODE_TYPE) \
28 : V(ELEMENTS_TYPE) \
29 : V(EMBEDDED_OBJECT_TYPE) \
30 : V(ENUM_CACHE_TYPE) \
31 : V(ENUM_INDICES_CACHE_TYPE) \
32 : V(FEEDBACK_VECTOR_ENTRY_TYPE) \
33 : V(FEEDBACK_VECTOR_HEADER_TYPE) \
34 : V(FEEDBACK_VECTOR_SLOT_CALL_TYPE) \
35 : V(FEEDBACK_VECTOR_SLOT_CALL_UNUSED_TYPE) \
36 : V(FEEDBACK_VECTOR_SLOT_ENUM_TYPE) \
37 : V(FEEDBACK_VECTOR_SLOT_LOAD_TYPE) \
38 : V(FEEDBACK_VECTOR_SLOT_LOAD_UNUSED_TYPE) \
39 : V(FEEDBACK_VECTOR_SLOT_OTHER_TYPE) \
40 : V(FEEDBACK_VECTOR_SLOT_STORE_TYPE) \
41 : V(FEEDBACK_VECTOR_SLOT_STORE_UNUSED_TYPE) \
42 : V(FUNCTION_TEMPLATE_INFO_ENTRIES_TYPE) \
43 : V(GLOBAL_ELEMENTS_TYPE) \
44 : V(GLOBAL_PROPERTIES_TYPE) \
45 : V(JS_ARRAY_BOILERPLATE_TYPE) \
46 : V(JS_COLLECTION_TABLE_TYPE) \
47 : V(JS_OBJECT_BOILERPLATE_TYPE) \
48 : V(NOSCRIPT_SHARED_FUNCTION_INFOS_TYPE) \
49 : V(NUMBER_STRING_CACHE_TYPE) \
50 : V(OBJECT_PROPERTY_DICTIONARY_TYPE) \
51 : V(OBJECT_TO_CODE_TYPE) \
52 : V(OPTIMIZED_CODE_LITERALS_TYPE) \
53 : V(OTHER_CONTEXT_TYPE) \
54 : V(PROTOTYPE_USERS_TYPE) \
55 : V(REGEXP_MULTIPLE_CACHE_TYPE) \
56 : V(RELOC_INFO_TYPE) \
57 : V(RETAINED_MAPS_TYPE) \
58 : V(SCRIPT_LIST_TYPE) \
59 : V(SCRIPT_SHARED_FUNCTION_INFOS_TYPE) \
60 : V(SCRIPT_SOURCE_EXTERNAL_ONE_BYTE_TYPE) \
61 : V(SCRIPT_SOURCE_EXTERNAL_TWO_BYTE_TYPE) \
62 : V(SCRIPT_SOURCE_NON_EXTERNAL_ONE_BYTE_TYPE) \
63 : V(SCRIPT_SOURCE_NON_EXTERNAL_TWO_BYTE_TYPE) \
64 : V(SERIALIZED_OBJECTS_TYPE) \
65 : V(SINGLE_CHARACTER_STRING_CACHE_TYPE) \
66 : V(STRING_SPLIT_CACHE_TYPE) \
67 : V(STRING_EXTERNAL_RESOURCE_ONE_BYTE_TYPE) \
68 : V(STRING_EXTERNAL_RESOURCE_TWO_BYTE_TYPE) \
69 : V(SOURCE_POSITION_TABLE_TYPE) \
70 : V(UNCOMPILED_JS_FUNCTION_TYPE) \
71 : V(UNCOMPILED_SHARED_FUNCTION_INFO_TYPE) \
72 : V(WEAK_NEW_SPACE_OBJECT_TO_CODE_TYPE)
73 :
74 : namespace v8 {
75 : namespace internal {
76 :
77 : class Heap;
78 : class Isolate;
79 :
80 : class ObjectStats {
81 : public:
82 : static const size_t kNoOverAllocation = 0;
83 :
84 0 : explicit ObjectStats(Heap* heap) : heap_(heap) { ClearObjectStats(); }
85 :
86 : // See description on VIRTUAL_INSTANCE_TYPE_LIST.
87 : enum VirtualInstanceType {
88 : #define DEFINE_VIRTUAL_INSTANCE_TYPE(type) type,
89 : VIRTUAL_INSTANCE_TYPE_LIST(DEFINE_VIRTUAL_INSTANCE_TYPE)
90 : #undef DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE
91 : LAST_VIRTUAL_TYPE = WEAK_NEW_SPACE_OBJECT_TO_CODE_TYPE,
92 : };
93 :
94 : // ObjectStats are kept in two arrays, counts and sizes. Related stats are
95 : // stored in a contiguous linear buffer. Stats groups are stored one after
96 : // another.
97 : enum {
98 : FIRST_VIRTUAL_TYPE = LAST_TYPE + 1,
99 : OBJECT_STATS_COUNT = FIRST_VIRTUAL_TYPE + LAST_VIRTUAL_TYPE + 1,
100 : };
101 :
102 : void ClearObjectStats(bool clear_last_time_stats = false);
103 :
104 : void PrintJSON(const char* key);
105 : void Dump(std::stringstream& stream);
106 :
107 : void CheckpointObjectStats();
108 : void RecordObjectStats(InstanceType type, size_t size);
109 : void RecordVirtualObjectStats(VirtualInstanceType type, size_t size,
110 : size_t over_allocated);
111 :
112 : size_t object_count_last_gc(size_t index) {
113 0 : return object_counts_last_time_[index];
114 : }
115 :
116 : size_t object_size_last_gc(size_t index) {
117 0 : return object_sizes_last_time_[index];
118 : }
119 :
120 : Isolate* isolate();
121 : Heap* heap() { return heap_; }
122 :
123 : private:
124 : static const int kFirstBucketShift = 5; // <32
125 : static const int kLastBucketShift = 20; // >=1M
126 : static const int kFirstBucket = 1 << kFirstBucketShift;
127 : static const int kLastBucket = 1 << kLastBucketShift;
128 : static const int kNumberOfBuckets = kLastBucketShift - kFirstBucketShift + 1;
129 : static const int kLastValueBucketIndex = kLastBucketShift - kFirstBucketShift;
130 :
131 : void PrintKeyAndId(const char* key, int gc_count);
132 : // The following functions are excluded from inline to reduce the overall
133 : // binary size of VB. On x64 this save around 80KB.
134 : V8_NOINLINE void PrintInstanceTypeJSON(const char* key, int gc_count,
135 : const char* name, int index);
136 : V8_NOINLINE void DumpInstanceTypeData(std::stringstream& stream,
137 : const char* name, int index);
138 :
139 : int HistogramIndexFromSize(size_t size);
140 :
141 : Heap* heap_;
142 : // Object counts and used memory by InstanceType.
143 : size_t object_counts_[OBJECT_STATS_COUNT];
144 : size_t object_counts_last_time_[OBJECT_STATS_COUNT];
145 : size_t object_sizes_[OBJECT_STATS_COUNT];
146 : size_t object_sizes_last_time_[OBJECT_STATS_COUNT];
147 : // Approximation of overallocated memory by InstanceType.
148 : size_t over_allocated_[OBJECT_STATS_COUNT];
149 : // Detailed histograms by InstanceType.
150 : size_t size_histogram_[OBJECT_STATS_COUNT][kNumberOfBuckets];
151 : size_t over_allocated_histogram_[OBJECT_STATS_COUNT][kNumberOfBuckets];
152 :
153 : size_t tagged_fields_count_;
154 : size_t embedder_fields_count_;
155 : size_t unboxed_double_fields_count_;
156 : size_t raw_fields_count_;
157 :
158 : friend class ObjectStatsCollectorImpl;
159 : };
160 :
161 : class ObjectStatsCollector {
162 : public:
163 : ObjectStatsCollector(Heap* heap, ObjectStats* live, ObjectStats* dead)
164 0 : : heap_(heap), live_(live), dead_(dead) {
165 : DCHECK_NOT_NULL(heap_);
166 : DCHECK_NOT_NULL(live_);
167 : DCHECK_NOT_NULL(dead_);
168 : }
169 :
170 : // Collects type information of live and dead objects. Requires mark bits to
171 : // be present.
172 : void Collect();
173 :
174 : private:
175 : Heap* const heap_;
176 : ObjectStats* const live_;
177 : ObjectStats* const dead_;
178 : };
179 :
180 : } // namespace internal
181 : } // namespace v8
182 :
183 : #endif // V8_HEAP_OBJECT_STATS_H_
|