blob: dac488e9fe3fb4981c062daa6f612ccf557574a2 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_HEAP_PROFILER_H_
29#define V8_HEAP_PROFILER_H_
30
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010031#include "zone-inl.h"
Steve Block6ded16b2010-05-10 14:33:55 +010032
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
36#ifdef ENABLE_LOGGING_AND_PROFILING
37
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010038class HeapSnapshot;
39class HeapSnapshotsCollection;
40
Ben Murdoch3bec4d22010-07-22 14:51:16 +010041#define HEAP_PROFILE(Call) \
42 do { \
43 if (v8::internal::HeapProfiler::is_profiling()) { \
44 v8::internal::HeapProfiler::Call; \
45 } \
46 } while (false)
47#else
48#define HEAP_PROFILE(Call) ((void) 0)
49#endif // ENABLE_LOGGING_AND_PROFILING
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010050
Steve Blocka7e24c12009-10-30 11:49:00 +000051// The HeapProfiler writes data to the log files, which can be postprocessed
52// to generate .hp files for use by the GHC/Valgrind tool hp2ps.
53class HeapProfiler {
54 public:
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010055 static void Setup();
56 static void TearDown();
57
58#ifdef ENABLE_LOGGING_AND_PROFILING
59 static HeapSnapshot* TakeSnapshot(const char* name);
60 static HeapSnapshot* TakeSnapshot(String* name);
61 static int GetSnapshotsCount();
62 static HeapSnapshot* GetSnapshot(int index);
63 static HeapSnapshot* FindSnapshot(unsigned uid);
64
Ben Murdoch3bec4d22010-07-22 14:51:16 +010065 static void ObjectMoveEvent(Address from, Address to);
66
67 static INLINE(bool is_profiling()) {
68 return singleton_ != NULL && singleton_->snapshots_->is_tracking_objects();
69 }
70
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010071 // Obsolete interface.
Steve Blocka7e24c12009-10-30 11:49:00 +000072 // Write a single heap sample to the log file.
73 static void WriteSample();
74
75 private:
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010076 HeapProfiler();
77 ~HeapProfiler();
78 HeapSnapshot* TakeSnapshotImpl(const char* name);
79 HeapSnapshot* TakeSnapshotImpl(String* name);
80
81 // Obsolete interface.
Steve Blocka7e24c12009-10-30 11:49:00 +000082 // Update the array info with stats from obj.
83 static void CollectStats(HeapObject* obj, HistogramInfo* info);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010084
85 HeapSnapshotsCollection* snapshots_;
86 unsigned next_snapshot_uid_;
87
88 static HeapProfiler* singleton_;
89#endif // ENABLE_LOGGING_AND_PROFILING
Steve Blocka7e24c12009-10-30 11:49:00 +000090};
91
92
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010093#ifdef ENABLE_LOGGING_AND_PROFILING
94
Steve Blocka7e24c12009-10-30 11:49:00 +000095// JSObjectsCluster describes a group of JS objects that are
96// considered equivalent in terms of a particular profile.
97class JSObjectsCluster BASE_EMBEDDED {
98 public:
99 // These special cases are used in retainer profile.
100 enum SpecialCase {
101 ROOTS = 1,
102 GLOBAL_PROPERTY = 2,
Steve Blockd0582a62009-12-15 09:54:21 +0000103 CODE = 3,
104 SELF = 100 // This case is used in ClustersCoarser only.
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 };
106
107 JSObjectsCluster() : constructor_(NULL), instance_(NULL) {}
108 explicit JSObjectsCluster(String* constructor)
109 : constructor_(constructor), instance_(NULL) {}
110 explicit JSObjectsCluster(SpecialCase special)
111 : constructor_(FromSpecialCase(special)), instance_(NULL) {}
112 JSObjectsCluster(String* constructor, Object* instance)
113 : constructor_(constructor), instance_(instance) {}
114
115 static int CompareConstructors(const JSObjectsCluster& a,
116 const JSObjectsCluster& b) {
117 // Strings are unique, so it is sufficient to compare their pointers.
118 return a.constructor_ == b.constructor_ ? 0
119 : (a.constructor_ < b.constructor_ ? -1 : 1);
120 }
121 static int Compare(const JSObjectsCluster& a, const JSObjectsCluster& b) {
122 // Strings are unique, so it is sufficient to compare their pointers.
123 const int cons_cmp = CompareConstructors(a, b);
124 return cons_cmp == 0 ?
125 (a.instance_ == b.instance_ ? 0 : (a.instance_ < b.instance_ ? -1 : 1))
126 : cons_cmp;
127 }
128 static int Compare(const JSObjectsCluster* a, const JSObjectsCluster* b) {
129 return Compare(*a, *b);
130 }
131
132 bool is_null() const { return constructor_ == NULL; }
133 bool can_be_coarsed() const { return instance_ != NULL; }
134 String* constructor() const { return constructor_; }
135
136 void Print(StringStream* accumulator) const;
137 // Allows null clusters to be printed.
138 void DebugPrint(StringStream* accumulator) const;
139
140 private:
141 static String* FromSpecialCase(SpecialCase special) {
142 // We use symbols that are illegal JS identifiers to identify special cases.
143 // Their actual value is irrelevant for us.
144 switch (special) {
145 case ROOTS: return Heap::result_symbol();
146 case GLOBAL_PROPERTY: return Heap::code_symbol();
Steve Blockd0582a62009-12-15 09:54:21 +0000147 case CODE: return Heap::arguments_shadow_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 case SELF: return Heap::catch_var_symbol();
149 default:
150 UNREACHABLE();
151 return NULL;
152 }
153 }
154
155 String* constructor_;
156 Object* instance_;
157};
158
159
160struct JSObjectsClusterTreeConfig {
161 typedef JSObjectsCluster Key;
162 typedef NumberAndSizeInfo Value;
163 static const Key kNoKey;
164 static const Value kNoValue;
165 static int Compare(const Key& a, const Key& b) {
166 return Key::Compare(a, b);
167 }
168};
169typedef ZoneSplayTree<JSObjectsClusterTreeConfig> JSObjectsClusterTree;
170
171
172// ConstructorHeapProfile is responsible for gathering and logging
173// "constructor profile" of JS objects allocated on heap.
174// It is run during garbage collection cycle, thus it doesn't need
175// to use handles.
176class ConstructorHeapProfile BASE_EMBEDDED {
177 public:
178 ConstructorHeapProfile();
179 virtual ~ConstructorHeapProfile() {}
180 void CollectStats(HeapObject* obj);
181 void PrintStats();
182 // Used by ZoneSplayTree::ForEach. Made virtual to allow overriding in tests.
183 virtual void Call(const JSObjectsCluster& cluster,
184 const NumberAndSizeInfo& number_and_size);
185
186 private:
187 ZoneScope zscope_;
188 JSObjectsClusterTree js_objects_info_tree_;
189};
190
191
192// JSObjectsRetainerTree is used to represent retainer graphs using
193// adjacency list form:
194//
195// Cluster -> (Cluster -> NumberAndSizeInfo)
196//
197// Subordinate splay trees are stored by pointer. They are zone-allocated,
198// so it isn't needed to manage their lifetime.
199//
200struct JSObjectsRetainerTreeConfig {
201 typedef JSObjectsCluster Key;
202 typedef JSObjectsClusterTree* Value;
203 static const Key kNoKey;
204 static const Value kNoValue;
205 static int Compare(const Key& a, const Key& b) {
206 return Key::Compare(a, b);
207 }
208};
209typedef ZoneSplayTree<JSObjectsRetainerTreeConfig> JSObjectsRetainerTree;
210
211
212class ClustersCoarser BASE_EMBEDDED {
213 public:
214 ClustersCoarser();
215
216 // Processes a given retainer graph.
217 void Process(JSObjectsRetainerTree* tree);
218
219 // Returns an equivalent cluster (can be the cluster itself).
220 // If the given cluster doesn't have an equivalent, returns null cluster.
221 JSObjectsCluster GetCoarseEquivalent(const JSObjectsCluster& cluster);
222 // Returns whether a cluster can be substitued with an equivalent and thus,
223 // skipped in some cases.
224 bool HasAnEquivalent(const JSObjectsCluster& cluster);
225
226 // Used by JSObjectsRetainerTree::ForEach.
227 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree);
228 void Call(const JSObjectsCluster& cluster,
229 const NumberAndSizeInfo& number_and_size);
230
231 private:
232 // Stores a list of back references for a cluster.
233 struct ClusterBackRefs {
234 explicit ClusterBackRefs(const JSObjectsCluster& cluster_);
235 ClusterBackRefs(const ClusterBackRefs& src);
236 ClusterBackRefs& operator=(const ClusterBackRefs& src);
237
238 static int Compare(const ClusterBackRefs& a, const ClusterBackRefs& b);
239 void SortRefs() { refs.Sort(JSObjectsCluster::Compare); }
240 static void SortRefsIterator(ClusterBackRefs* ref) { ref->SortRefs(); }
241
242 JSObjectsCluster cluster;
243 ZoneList<JSObjectsCluster> refs;
244 };
245 typedef ZoneList<ClusterBackRefs> SimilarityList;
246
247 // A tree for storing a list of equivalents for a cluster.
248 struct ClusterEqualityConfig {
249 typedef JSObjectsCluster Key;
250 typedef JSObjectsCluster Value;
251 static const Key kNoKey;
252 static const Value kNoValue;
253 static int Compare(const Key& a, const Key& b) {
254 return Key::Compare(a, b);
255 }
256 };
257 typedef ZoneSplayTree<ClusterEqualityConfig> EqualityTree;
258
259 static int ClusterBackRefsCmp(const ClusterBackRefs* a,
260 const ClusterBackRefs* b) {
261 return ClusterBackRefs::Compare(*a, *b);
262 }
263 int DoProcess(JSObjectsRetainerTree* tree);
264 int FillEqualityTree();
265
266 static const int kInitialBackrefsListCapacity = 2;
267 static const int kInitialSimilarityListCapacity = 2000;
268 // Number of passes for finding equivalents. Limits the length of paths
269 // that can be considered equivalent.
270 static const int kMaxPassesCount = 10;
271
272 ZoneScope zscope_;
273 SimilarityList sim_list_;
274 EqualityTree eq_tree_;
275 ClusterBackRefs* current_pair_;
276 JSObjectsRetainerTree* current_set_;
277 const JSObjectsCluster* self_;
278};
279
280
281// RetainerHeapProfile is responsible for gathering and logging
282// "retainer profile" of JS objects allocated on heap.
283// It is run during garbage collection cycle, thus it doesn't need
284// to use handles.
285class RetainerHeapProfile BASE_EMBEDDED {
286 public:
287 class Printer {
288 public:
289 virtual ~Printer() {}
290 virtual void PrintRetainers(const JSObjectsCluster& cluster,
291 const StringStream& retainers) = 0;
292 };
293
294 RetainerHeapProfile();
295 void CollectStats(HeapObject* obj);
296 void PrintStats();
297 void DebugPrintStats(Printer* printer);
298 void StoreReference(const JSObjectsCluster& cluster, HeapObject* ref);
299
300 private:
301 ZoneScope zscope_;
302 JSObjectsRetainerTree retainers_tree_;
303 ClustersCoarser coarser_;
304};
305
306
Steve Block3ce2e202009-11-05 08:53:23 +0000307class ProducerHeapProfile : public AllStatic {
308 public:
309 static void Setup();
Leon Clarkee46be812010-01-19 14:06:41 +0000310 static void RecordJSObjectAllocation(Object* obj) {
311 if (FLAG_log_producers) DoRecordJSObjectAllocation(obj);
312 }
313
Steve Block3ce2e202009-11-05 08:53:23 +0000314 private:
Leon Clarkee46be812010-01-19 14:06:41 +0000315 static void DoRecordJSObjectAllocation(Object* obj);
Steve Block3ce2e202009-11-05 08:53:23 +0000316 static bool can_log_;
317};
318
Steve Blocka7e24c12009-10-30 11:49:00 +0000319#endif // ENABLE_LOGGING_AND_PROFILING
320
321} } // namespace v8::internal
322
323#endif // V8_HEAP_PROFILER_H_