OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 28 matching lines...) Expand all Loading... |
39 public: | 39 public: |
40 // Write a single heap sample to the log file. | 40 // Write a single heap sample to the log file. |
41 static void WriteSample(); | 41 static void WriteSample(); |
42 | 42 |
43 private: | 43 private: |
44 // Update the array info with stats from obj. | 44 // Update the array info with stats from obj. |
45 static void CollectStats(HeapObject* obj, HistogramInfo* info); | 45 static void CollectStats(HeapObject* obj, HistogramInfo* info); |
46 }; | 46 }; |
47 | 47 |
48 | 48 |
49 // ConstructorHeapProfile is responsible for gathering and logging | |
50 // "constructor profile" of JS objects allocated on heap. | |
51 // It is run during garbage collection cycle, thus it doesn't need | |
52 // to use handles. | |
53 class ConstructorHeapProfile BASE_EMBEDDED { | |
54 public: | |
55 ConstructorHeapProfile(); | |
56 virtual ~ConstructorHeapProfile() {} | |
57 void CollectStats(HeapObject* obj); | |
58 void PrintStats(); | |
59 // Used by ZoneSplayTree::ForEach. Made virtual to allow overriding in tests. | |
60 virtual void Call(String* name, const NumberAndSizeInfo& number_and_size); | |
61 | |
62 private: | |
63 struct TreeConfig { | |
64 typedef String* Key; | |
65 typedef NumberAndSizeInfo Value; | |
66 static const Key kNoKey; | |
67 static const Value kNoValue; | |
68 static int Compare(const Key& a, const Key& b) { | |
69 // Strings are unique, so it is sufficient to compare their pointers. | |
70 return a == b ? 0 : (a < b ? -1 : 1); | |
71 } | |
72 }; | |
73 typedef ZoneSplayTree<TreeConfig> JSObjectsInfoTree; | |
74 | |
75 ZoneScope zscope_; | |
76 JSObjectsInfoTree js_objects_info_tree_; | |
77 }; | |
78 | |
79 | |
80 // JSObjectsCluster describes a group of JS objects that are | 49 // JSObjectsCluster describes a group of JS objects that are |
81 // considered equivalent in terms of retainer profile. | 50 // considered equivalent in terms of a particular profile. |
82 class JSObjectsCluster BASE_EMBEDDED { | 51 class JSObjectsCluster BASE_EMBEDDED { |
83 public: | 52 public: |
| 53 // These special cases are used in retainer profile. |
84 enum SpecialCase { | 54 enum SpecialCase { |
85 ROOTS = 1, | 55 ROOTS = 1, |
86 GLOBAL_PROPERTY = 2 | 56 GLOBAL_PROPERTY = 2 |
87 }; | 57 }; |
88 | 58 |
89 JSObjectsCluster() : constructor_(NULL), instance_(NULL) {} | 59 JSObjectsCluster() : constructor_(NULL), instance_(NULL) {} |
90 explicit JSObjectsCluster(String* constructor) | 60 explicit JSObjectsCluster(String* constructor) |
91 : constructor_(constructor), instance_(NULL) {} | 61 : constructor_(constructor), instance_(NULL) {} |
92 explicit JSObjectsCluster(SpecialCase special) | 62 explicit JSObjectsCluster(SpecialCase special) |
93 : constructor_(FromSpecialCase(special)), instance_(NULL) {} | 63 : constructor_(FromSpecialCase(special)), instance_(NULL) {} |
94 JSObjectsCluster(String* constructor, Object* instance) | 64 JSObjectsCluster(String* constructor, Object* instance) |
95 : constructor_(constructor), instance_(instance) {} | 65 : constructor_(constructor), instance_(instance) {} |
96 | 66 |
97 static int CompareConstructors( | 67 static int CompareConstructors(const JSObjectsCluster& a, |
98 const JSObjectsCluster& a, const JSObjectsCluster& b) { | 68 const JSObjectsCluster& b) { |
99 // Strings are unique, so it is sufficient to compare their pointers. | 69 // Strings are unique, so it is sufficient to compare their pointers. |
100 return a.constructor_ == b.constructor_ ? 0 | 70 return a.constructor_ == b.constructor_ ? 0 |
101 : (a.constructor_ < b.constructor_ ? -1 : 1); | 71 : (a.constructor_ < b.constructor_ ? -1 : 1); |
102 } | 72 } |
103 static int Compare(const JSObjectsCluster& a, const JSObjectsCluster& b) { | 73 static int Compare(const JSObjectsCluster& a, const JSObjectsCluster& b) { |
104 // Strings are unique, so it is sufficient to compare their pointers. | 74 // Strings are unique, so it is sufficient to compare their pointers. |
105 const int cons_cmp = CompareConstructors(a, b); | 75 const int cons_cmp = CompareConstructors(a, b); |
106 return cons_cmp == 0 ? | 76 return cons_cmp == 0 ? |
107 (a.instance_ == b.instance_ ? 0 : (a.instance_ < b.instance_ ? -1 : 1)) | 77 (a.instance_ == b.instance_ ? 0 : (a.instance_ < b.instance_ ? -1 : 1)) |
108 : cons_cmp; | 78 : cons_cmp; |
109 } | 79 } |
110 | 80 |
111 bool is_null() const { return constructor_ == NULL; } | 81 bool is_null() const { return constructor_ == NULL; } |
112 bool can_be_coarsed() const { return instance_ != NULL; } | 82 bool can_be_coarsed() const { return instance_ != NULL; } |
| 83 String* constructor() const { return constructor_; } |
113 | 84 |
114 void Print(StringStream* accumulator) const; | 85 void Print(StringStream* accumulator) const; |
115 // Allows null clusters to be printed. | 86 // Allows null clusters to be printed. |
116 void DebugPrint(StringStream* accumulator) const; | 87 void DebugPrint(StringStream* accumulator) const; |
117 | 88 |
118 private: | 89 private: |
119 static String* FromSpecialCase(SpecialCase special) { | 90 static String* FromSpecialCase(SpecialCase special) { |
120 // We use symbols that are illegal JS identifiers to identify special cases. | 91 // We use symbols that are illegal JS identifiers to identify special cases. |
121 // Their actual value is irrelevant for us. | 92 // Their actual value is irrelevant for us. |
122 switch (special) { | 93 switch (special) { |
123 case ROOTS: return Heap::result_symbol(); | 94 case ROOTS: return Heap::result_symbol(); |
124 case GLOBAL_PROPERTY: return Heap::code_symbol(); | 95 case GLOBAL_PROPERTY: return Heap::code_symbol(); |
125 default: | 96 default: |
126 UNREACHABLE(); | 97 UNREACHABLE(); |
127 return NULL; | 98 return NULL; |
128 } | 99 } |
129 } | 100 } |
130 | 101 |
131 String* constructor_; | 102 String* constructor_; |
132 Object* instance_; | 103 Object* instance_; |
133 }; | 104 }; |
134 | 105 |
135 | 106 |
136 struct JSObjectsClusterTreeConfig; | 107 struct JSObjectsClusterTreeConfig { |
| 108 typedef JSObjectsCluster Key; |
| 109 typedef NumberAndSizeInfo Value; |
| 110 static const Key kNoKey; |
| 111 static const Value kNoValue; |
| 112 static int Compare(const Key& a, const Key& b) { |
| 113 return Key::Compare(a, b); |
| 114 } |
| 115 }; |
137 typedef ZoneSplayTree<JSObjectsClusterTreeConfig> JSObjectsClusterTree; | 116 typedef ZoneSplayTree<JSObjectsClusterTreeConfig> JSObjectsClusterTree; |
138 | 117 |
139 // JSObjectsClusterTree is used to represent retainer graphs using | 118 |
140 // adjacency list form. That is, the first level maps JS object | 119 // ConstructorHeapProfile is responsible for gathering and logging |
141 // clusters to adjacency lists, which in their turn are degenerate | 120 // "constructor profile" of JS objects allocated on heap. |
142 // JSObjectsClusterTrees (their values are NULLs.) | 121 // It is run during garbage collection cycle, thus it doesn't need |
143 struct JSObjectsClusterTreeConfig { | 122 // to use handles. |
| 123 class ConstructorHeapProfile BASE_EMBEDDED { |
| 124 public: |
| 125 ConstructorHeapProfile(); |
| 126 virtual ~ConstructorHeapProfile() {} |
| 127 void CollectStats(HeapObject* obj); |
| 128 void PrintStats(); |
| 129 // Used by ZoneSplayTree::ForEach. Made virtual to allow overriding in tests. |
| 130 virtual void Call(const JSObjectsCluster& cluster, |
| 131 const NumberAndSizeInfo& number_and_size); |
| 132 |
| 133 private: |
| 134 ZoneScope zscope_; |
| 135 JSObjectsClusterTree js_objects_info_tree_; |
| 136 }; |
| 137 |
| 138 |
| 139 // JSObjectsRetainerTree is used to represent retainer graphs using |
| 140 // adjacency list form: |
| 141 // |
| 142 // Cluster -> (Cluster -> NumberAndSizeInfo) |
| 143 // |
| 144 // Subordinate splay trees are stored by pointer. They are zone-allocated, |
| 145 // so it isn't needed to manage their lifetime. |
| 146 // |
| 147 struct JSObjectsRetainerTreeConfig { |
144 typedef JSObjectsCluster Key; | 148 typedef JSObjectsCluster Key; |
145 typedef JSObjectsClusterTree* Value; | 149 typedef JSObjectsClusterTree* Value; |
146 static const Key kNoKey; | 150 static const Key kNoKey; |
147 static const Value kNoValue; | 151 static const Value kNoValue; |
148 static int Compare(const Key& a, const Key& b) { | 152 static int Compare(const Key& a, const Key& b) { |
149 return Key::Compare(a, b); | 153 return Key::Compare(a, b); |
150 } | 154 } |
151 }; | 155 }; |
| 156 typedef ZoneSplayTree<JSObjectsRetainerTreeConfig> JSObjectsRetainerTree; |
152 | 157 |
153 | 158 |
154 class ClustersCoarser BASE_EMBEDDED { | 159 class ClustersCoarser BASE_EMBEDDED { |
155 public: | 160 public: |
156 ClustersCoarser(); | 161 ClustersCoarser(); |
157 | 162 |
158 // Processes a given retainer graph. | 163 // Processes a given retainer graph. |
159 void Process(JSObjectsClusterTree* tree); | 164 void Process(JSObjectsRetainerTree* tree); |
160 | 165 |
161 // Returns an equivalent cluster (can be the cluster itself). | 166 // Returns an equivalent cluster (can be the cluster itself). |
162 // If the given cluster doesn't have an equivalent, returns null cluster. | 167 // If the given cluster doesn't have an equivalent, returns null cluster. |
163 JSObjectsCluster GetCoarseEquivalent(const JSObjectsCluster& cluster); | 168 JSObjectsCluster GetCoarseEquivalent(const JSObjectsCluster& cluster); |
164 // Returns whether a cluster can be substitued with an equivalent and thus, | 169 // Returns whether a cluster can be substitued with an equivalent and thus, |
165 // skipped in some cases. | 170 // skipped in some cases. |
166 bool HasAnEquivalent(const JSObjectsCluster& cluster); | 171 bool HasAnEquivalent(const JSObjectsCluster& cluster); |
167 | 172 |
168 // Used by ZoneSplayTree::ForEach. | 173 // Used by JSObjectsRetainerTree::ForEach. |
169 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); | 174 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 175 void Call(const JSObjectsCluster& cluster, |
| 176 const NumberAndSizeInfo& number_and_size); |
170 | 177 |
171 private: | 178 private: |
172 // Stores a list of back references for a cluster. | 179 // Stores a list of back references for a cluster. |
173 struct ClusterBackRefs { | 180 struct ClusterBackRefs { |
174 explicit ClusterBackRefs(const JSObjectsCluster& cluster_); | 181 explicit ClusterBackRefs(const JSObjectsCluster& cluster_); |
175 ClusterBackRefs(const ClusterBackRefs& src); | 182 ClusterBackRefs(const ClusterBackRefs& src); |
176 ClusterBackRefs& operator=(const ClusterBackRefs& src); | 183 ClusterBackRefs& operator=(const ClusterBackRefs& src); |
177 | 184 |
178 static int Compare(const ClusterBackRefs& a, const ClusterBackRefs& b); | 185 static int Compare(const ClusterBackRefs& a, const ClusterBackRefs& b); |
179 | 186 |
180 JSObjectsCluster cluster; | 187 JSObjectsCluster cluster; |
181 ZoneList<JSObjectsCluster> refs; | 188 ZoneList<JSObjectsCluster> refs; |
182 }; | 189 }; |
183 typedef ZoneList<ClusterBackRefs> SimilarityList; | 190 typedef ZoneList<ClusterBackRefs> SimilarityList; |
184 | 191 |
185 // A tree for storing a list of equivalents for a cluster. | 192 // A tree for storing a list of equivalents for a cluster. |
186 struct ClusterEqualityConfig { | 193 struct ClusterEqualityConfig { |
187 typedef JSObjectsCluster Key; | 194 typedef JSObjectsCluster Key; |
188 typedef JSObjectsCluster Value; | 195 typedef JSObjectsCluster Value; |
189 static const Key kNoKey; | 196 static const Key kNoKey; |
190 static const Value kNoValue; | 197 static const Value kNoValue; |
191 static int Compare(const Key& a, const Key& b) { | 198 static int Compare(const Key& a, const Key& b) { |
192 return Key::Compare(a, b); | 199 return Key::Compare(a, b); |
193 } | 200 } |
194 }; | 201 }; |
195 typedef ZoneSplayTree<ClusterEqualityConfig> EqualityTree; | 202 typedef ZoneSplayTree<ClusterEqualityConfig> EqualityTree; |
196 | 203 |
197 static int ClusterBackRefsCmp( | 204 static int ClusterBackRefsCmp(const ClusterBackRefs* a, |
198 const ClusterBackRefs* a, const ClusterBackRefs* b) { | 205 const ClusterBackRefs* b) { |
199 return ClusterBackRefs::Compare(*a, *b); | 206 return ClusterBackRefs::Compare(*a, *b); |
200 } | 207 } |
201 int DoProcess(JSObjectsClusterTree* tree); | 208 int DoProcess(JSObjectsRetainerTree* tree); |
202 int FillEqualityTree(); | 209 int FillEqualityTree(); |
203 | 210 |
204 static const int kInitialBackrefsListCapacity = 2; | 211 static const int kInitialBackrefsListCapacity = 2; |
205 static const int kInitialSimilarityListCapacity = 2000; | 212 static const int kInitialSimilarityListCapacity = 2000; |
206 // Number of passes for finding equivalents. Limits the length of paths | 213 // Number of passes for finding equivalents. Limits the length of paths |
207 // that can be considered equivalent. | 214 // that can be considered equivalent. |
208 static const int kMaxPassesCount = 10; | 215 static const int kMaxPassesCount = 10; |
209 | 216 |
210 ZoneScope zscope_; | 217 ZoneScope zscope_; |
211 SimilarityList sim_list_; | 218 SimilarityList sim_list_; |
212 EqualityTree eq_tree_; | 219 EqualityTree eq_tree_; |
213 ClusterBackRefs* current_pair_; | 220 ClusterBackRefs* current_pair_; |
214 JSObjectsClusterTree* current_set_; | 221 JSObjectsRetainerTree* current_set_; |
215 }; | 222 }; |
216 | 223 |
217 | 224 |
218 // RetainerHeapProfile is responsible for gathering and logging | 225 // RetainerHeapProfile is responsible for gathering and logging |
219 // "retainer profile" of JS objects allocated on heap. | 226 // "retainer profile" of JS objects allocated on heap. |
220 // It is run during garbage collection cycle, thus it doesn't need | 227 // It is run during garbage collection cycle, thus it doesn't need |
221 // to use handles. | 228 // to use handles. |
222 class RetainerHeapProfile BASE_EMBEDDED { | 229 class RetainerHeapProfile BASE_EMBEDDED { |
223 public: | 230 public: |
224 class Printer { | 231 class Printer { |
225 public: | 232 public: |
226 virtual ~Printer() {} | 233 virtual ~Printer() {} |
227 virtual void PrintRetainers(const StringStream& retainers) = 0; | 234 virtual void PrintRetainers(const JSObjectsCluster& cluster, |
| 235 const StringStream& retainers) = 0; |
228 }; | 236 }; |
229 | 237 |
230 RetainerHeapProfile(); | 238 RetainerHeapProfile(); |
231 void CollectStats(HeapObject* obj); | 239 void CollectStats(HeapObject* obj); |
232 void PrintStats(); | 240 void PrintStats(); |
233 void DebugPrintStats(Printer* printer); | 241 void DebugPrintStats(Printer* printer); |
234 void StoreReference(const JSObjectsCluster& cluster, Object* ref); | 242 void StoreReference(const JSObjectsCluster& cluster, HeapObject* ref); |
235 | 243 |
236 private: | 244 private: |
237 JSObjectsCluster Clusterize(Object* obj); | |
238 | |
239 // Limit on the number of retainers to be printed per cluster. | 245 // Limit on the number of retainers to be printed per cluster. |
240 static const int kMaxRetainersToPrint = 50; | 246 static const int kMaxRetainersToPrint = 50; |
241 ZoneScope zscope_; | 247 ZoneScope zscope_; |
242 JSObjectsClusterTree retainers_tree_; | 248 JSObjectsRetainerTree retainers_tree_; |
243 ClustersCoarser coarser_; | 249 ClustersCoarser coarser_; |
244 // TODO(mnaganov): Use some helper class to hold these state variables. | 250 // TODO(mnaganov): Use some helper class to hold these state variables. |
245 JSObjectsClusterTree* coarse_cluster_tree_; | 251 JSObjectsClusterTree* coarse_cluster_tree_; |
246 int retainers_printed_; | |
247 Printer* current_printer_; | 252 Printer* current_printer_; |
248 StringStream* current_stream_; | 253 StringStream* current_stream_; |
249 public: | 254 public: |
250 // Used by JSObjectsClusterTree::ForEach. | 255 // Used by JSObjectsRetainerTree::ForEach. |
251 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); | 256 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 257 void Call(const JSObjectsCluster& cluster, |
| 258 const NumberAndSizeInfo& number_and_size); |
252 }; | 259 }; |
253 | 260 |
254 | 261 |
255 #endif // ENABLE_LOGGING_AND_PROFILING | 262 #endif // ENABLE_LOGGING_AND_PROFILING |
256 | 263 |
257 } } // namespace v8::internal | 264 } } // namespace v8::internal |
258 | 265 |
259 #endif // V8_HEAP_PROFILER_H_ | 266 #endif // V8_HEAP_PROFILER_H_ |
OLD | NEW |