blob: bd875df236809a1cb6e7ade710960eaa97d9de48 [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
31namespace v8 {
32namespace internal {
33
34#ifdef ENABLE_LOGGING_AND_PROFILING
35
36// The HeapProfiler writes data to the log files, which can be postprocessed
37// to generate .hp files for use by the GHC/Valgrind tool hp2ps.
38class HeapProfiler {
39 public:
40 // Write a single heap sample to the log file.
41 static void WriteSample();
42
43 private:
44 // Update the array info with stats from obj.
45 static void CollectStats(HeapObject* obj, HistogramInfo* info);
46};
47
48
49// JSObjectsCluster describes a group of JS objects that are
50// considered equivalent in terms of a particular profile.
51class JSObjectsCluster BASE_EMBEDDED {
52 public:
53 // These special cases are used in retainer profile.
54 enum SpecialCase {
55 ROOTS = 1,
56 GLOBAL_PROPERTY = 2,
57 SELF = 3 // This case is used in ClustersCoarser only.
58 };
59
60 JSObjectsCluster() : constructor_(NULL), instance_(NULL) {}
61 explicit JSObjectsCluster(String* constructor)
62 : constructor_(constructor), instance_(NULL) {}
63 explicit JSObjectsCluster(SpecialCase special)
64 : constructor_(FromSpecialCase(special)), instance_(NULL) {}
65 JSObjectsCluster(String* constructor, Object* instance)
66 : constructor_(constructor), instance_(instance) {}
67
68 static int CompareConstructors(const JSObjectsCluster& a,
69 const JSObjectsCluster& b) {
70 // Strings are unique, so it is sufficient to compare their pointers.
71 return a.constructor_ == b.constructor_ ? 0
72 : (a.constructor_ < b.constructor_ ? -1 : 1);
73 }
74 static int Compare(const JSObjectsCluster& a, const JSObjectsCluster& b) {
75 // Strings are unique, so it is sufficient to compare their pointers.
76 const int cons_cmp = CompareConstructors(a, b);
77 return cons_cmp == 0 ?
78 (a.instance_ == b.instance_ ? 0 : (a.instance_ < b.instance_ ? -1 : 1))
79 : cons_cmp;
80 }
81 static int Compare(const JSObjectsCluster* a, const JSObjectsCluster* b) {
82 return Compare(*a, *b);
83 }
84
85 bool is_null() const { return constructor_ == NULL; }
86 bool can_be_coarsed() const { return instance_ != NULL; }
87 String* constructor() const { return constructor_; }
88
89 void Print(StringStream* accumulator) const;
90 // Allows null clusters to be printed.
91 void DebugPrint(StringStream* accumulator) const;
92
93 private:
94 static String* FromSpecialCase(SpecialCase special) {
95 // We use symbols that are illegal JS identifiers to identify special cases.
96 // Their actual value is irrelevant for us.
97 switch (special) {
98 case ROOTS: return Heap::result_symbol();
99 case GLOBAL_PROPERTY: return Heap::code_symbol();
100 case SELF: return Heap::catch_var_symbol();
101 default:
102 UNREACHABLE();
103 return NULL;
104 }
105 }
106
107 String* constructor_;
108 Object* instance_;
109};
110
111
112struct JSObjectsClusterTreeConfig {
113 typedef JSObjectsCluster Key;
114 typedef NumberAndSizeInfo Value;
115 static const Key kNoKey;
116 static const Value kNoValue;
117 static int Compare(const Key& a, const Key& b) {
118 return Key::Compare(a, b);
119 }
120};
121typedef ZoneSplayTree<JSObjectsClusterTreeConfig> JSObjectsClusterTree;
122
123
124// ConstructorHeapProfile is responsible for gathering and logging
125// "constructor profile" of JS objects allocated on heap.
126// It is run during garbage collection cycle, thus it doesn't need
127// to use handles.
128class ConstructorHeapProfile BASE_EMBEDDED {
129 public:
130 ConstructorHeapProfile();
131 virtual ~ConstructorHeapProfile() {}
132 void CollectStats(HeapObject* obj);
133 void PrintStats();
134 // Used by ZoneSplayTree::ForEach. Made virtual to allow overriding in tests.
135 virtual void Call(const JSObjectsCluster& cluster,
136 const NumberAndSizeInfo& number_and_size);
137
138 private:
139 ZoneScope zscope_;
140 JSObjectsClusterTree js_objects_info_tree_;
141};
142
143
144// JSObjectsRetainerTree is used to represent retainer graphs using
145// adjacency list form:
146//
147// Cluster -> (Cluster -> NumberAndSizeInfo)
148//
149// Subordinate splay trees are stored by pointer. They are zone-allocated,
150// so it isn't needed to manage their lifetime.
151//
152struct JSObjectsRetainerTreeConfig {
153 typedef JSObjectsCluster Key;
154 typedef JSObjectsClusterTree* Value;
155 static const Key kNoKey;
156 static const Value kNoValue;
157 static int Compare(const Key& a, const Key& b) {
158 return Key::Compare(a, b);
159 }
160};
161typedef ZoneSplayTree<JSObjectsRetainerTreeConfig> JSObjectsRetainerTree;
162
163
164class ClustersCoarser BASE_EMBEDDED {
165 public:
166 ClustersCoarser();
167
168 // Processes a given retainer graph.
169 void Process(JSObjectsRetainerTree* tree);
170
171 // Returns an equivalent cluster (can be the cluster itself).
172 // If the given cluster doesn't have an equivalent, returns null cluster.
173 JSObjectsCluster GetCoarseEquivalent(const JSObjectsCluster& cluster);
174 // Returns whether a cluster can be substitued with an equivalent and thus,
175 // skipped in some cases.
176 bool HasAnEquivalent(const JSObjectsCluster& cluster);
177
178 // Used by JSObjectsRetainerTree::ForEach.
179 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree);
180 void Call(const JSObjectsCluster& cluster,
181 const NumberAndSizeInfo& number_and_size);
182
183 private:
184 // Stores a list of back references for a cluster.
185 struct ClusterBackRefs {
186 explicit ClusterBackRefs(const JSObjectsCluster& cluster_);
187 ClusterBackRefs(const ClusterBackRefs& src);
188 ClusterBackRefs& operator=(const ClusterBackRefs& src);
189
190 static int Compare(const ClusterBackRefs& a, const ClusterBackRefs& b);
191 void SortRefs() { refs.Sort(JSObjectsCluster::Compare); }
192 static void SortRefsIterator(ClusterBackRefs* ref) { ref->SortRefs(); }
193
194 JSObjectsCluster cluster;
195 ZoneList<JSObjectsCluster> refs;
196 };
197 typedef ZoneList<ClusterBackRefs> SimilarityList;
198
199 // A tree for storing a list of equivalents for a cluster.
200 struct ClusterEqualityConfig {
201 typedef JSObjectsCluster Key;
202 typedef JSObjectsCluster 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 };
209 typedef ZoneSplayTree<ClusterEqualityConfig> EqualityTree;
210
211 static int ClusterBackRefsCmp(const ClusterBackRefs* a,
212 const ClusterBackRefs* b) {
213 return ClusterBackRefs::Compare(*a, *b);
214 }
215 int DoProcess(JSObjectsRetainerTree* tree);
216 int FillEqualityTree();
217
218 static const int kInitialBackrefsListCapacity = 2;
219 static const int kInitialSimilarityListCapacity = 2000;
220 // Number of passes for finding equivalents. Limits the length of paths
221 // that can be considered equivalent.
222 static const int kMaxPassesCount = 10;
223
224 ZoneScope zscope_;
225 SimilarityList sim_list_;
226 EqualityTree eq_tree_;
227 ClusterBackRefs* current_pair_;
228 JSObjectsRetainerTree* current_set_;
229 const JSObjectsCluster* self_;
230};
231
232
233// RetainerHeapProfile is responsible for gathering and logging
234// "retainer profile" of JS objects allocated on heap.
235// It is run during garbage collection cycle, thus it doesn't need
236// to use handles.
237class RetainerHeapProfile BASE_EMBEDDED {
238 public:
239 class Printer {
240 public:
241 virtual ~Printer() {}
242 virtual void PrintRetainers(const JSObjectsCluster& cluster,
243 const StringStream& retainers) = 0;
244 };
245
246 RetainerHeapProfile();
247 void CollectStats(HeapObject* obj);
248 void PrintStats();
249 void DebugPrintStats(Printer* printer);
250 void StoreReference(const JSObjectsCluster& cluster, HeapObject* ref);
251
252 private:
253 ZoneScope zscope_;
254 JSObjectsRetainerTree retainers_tree_;
255 ClustersCoarser coarser_;
256};
257
258
Steve Block3ce2e202009-11-05 08:53:23 +0000259class ProducerHeapProfile : public AllStatic {
260 public:
261 static void Setup();
262 static void RecordJSObjectAllocation(Object* obj);
263 private:
264 static bool can_log_;
265};
266
Steve Blocka7e24c12009-10-30 11:49:00 +0000267#endif // ENABLE_LOGGING_AND_PROFILING
268
269} } // namespace v8::internal
270
271#endif // V8_HEAP_PROFILER_H_