OLD | NEW |
(Empty) | |
| 1 // 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 |
| 31 namespace v8 { |
| 32 namespace 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. |
| 38 class 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 // 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 |
| 81 // considered equivalent in terms of retainer profile. |
| 82 class JSObjectsCluster BASE_EMBEDDED { |
| 83 public: |
| 84 enum SpecialCase { |
| 85 ROOTS = 1, |
| 86 GLOBAL_PROPERTY = 2 |
| 87 }; |
| 88 |
| 89 JSObjectsCluster() : constructor_(NULL), instance_(NULL) {} |
| 90 explicit JSObjectsCluster(String* constructor) |
| 91 : constructor_(constructor), instance_(NULL) {} |
| 92 explicit JSObjectsCluster(SpecialCase special) |
| 93 : constructor_(FromSpecialCase(special)), instance_(NULL) {} |
| 94 JSObjectsCluster(String* constructor, Object* instance) |
| 95 : constructor_(constructor), instance_(instance) {} |
| 96 |
| 97 static int CompareConstructors( |
| 98 const JSObjectsCluster& a, const JSObjectsCluster& b) { |
| 99 // Strings are unique, so it is sufficient to compare their pointers. |
| 100 return a.constructor_ == b.constructor_ ? 0 |
| 101 : (a.constructor_ < b.constructor_ ? -1 : 1); |
| 102 } |
| 103 static int Compare(const JSObjectsCluster& a, const JSObjectsCluster& b) { |
| 104 // Strings are unique, so it is sufficient to compare their pointers. |
| 105 const int cons_cmp = CompareConstructors(a, b); |
| 106 return cons_cmp == 0 ? |
| 107 (a.instance_ == b.instance_ ? 0 : (a.instance_ < b.instance_ ? -1 : 1)) |
| 108 : cons_cmp; |
| 109 } |
| 110 |
| 111 bool is_null() const { return constructor_ == NULL; } |
| 112 bool can_be_coarsed() const { return instance_ != NULL; } |
| 113 |
| 114 void Print(StringStream* accumulator) const; |
| 115 // Allows null clusters to be printed. |
| 116 void DebugPrint(StringStream* accumulator) const; |
| 117 |
| 118 private: |
| 119 static String* FromSpecialCase(SpecialCase special) { |
| 120 // We use symbols that are illegal JS identifiers to identify special cases. |
| 121 // Their actual value is irrelevant for us. |
| 122 switch (special) { |
| 123 case ROOTS: return Heap::result_symbol(); |
| 124 case GLOBAL_PROPERTY: return Heap::code_symbol(); |
| 125 default: |
| 126 UNREACHABLE(); |
| 127 return NULL; |
| 128 } |
| 129 } |
| 130 |
| 131 String* constructor_; |
| 132 Object* instance_; |
| 133 }; |
| 134 |
| 135 |
| 136 struct JSObjectsClusterTreeConfig; |
| 137 typedef ZoneSplayTree<JSObjectsClusterTreeConfig> JSObjectsClusterTree; |
| 138 |
| 139 // JSObjectsClusterTree is used to represent retainer graphs using |
| 140 // adjacency list form. That is, the first level maps JS object |
| 141 // clusters to adjacency lists, which in their turn are degenerate |
| 142 // JSObjectsClusterTrees (their values are NULLs.) |
| 143 struct JSObjectsClusterTreeConfig { |
| 144 typedef JSObjectsCluster Key; |
| 145 typedef JSObjectsClusterTree* Value; |
| 146 static const Key kNoKey; |
| 147 static const Value kNoValue; |
| 148 static int Compare(const Key& a, const Key& b) { |
| 149 return Key::Compare(a, b); |
| 150 } |
| 151 }; |
| 152 |
| 153 |
| 154 class ClustersCoarser BASE_EMBEDDED { |
| 155 public: |
| 156 ClustersCoarser(); |
| 157 |
| 158 // Processes a given retainer graph. |
| 159 void Process(JSObjectsClusterTree* tree); |
| 160 |
| 161 // Returns an equivalent cluster (can be the cluster itself). |
| 162 // If the given cluster doesn't have an equivalent, returns null cluster. |
| 163 JSObjectsCluster GetCoarseEquivalent(const JSObjectsCluster& cluster); |
| 164 // Returns whether a cluster can be substitued with an equivalent and thus, |
| 165 // skipped in some cases. |
| 166 bool HasAnEquivalent(const JSObjectsCluster& cluster); |
| 167 |
| 168 // Used by ZoneSplayTree::ForEach. |
| 169 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 170 |
| 171 private: |
| 172 // Stores a list of back references for a cluster. |
| 173 struct ClusterBackRefs { |
| 174 explicit ClusterBackRefs(const JSObjectsCluster& cluster_); |
| 175 ClusterBackRefs(const ClusterBackRefs& src); |
| 176 ClusterBackRefs& operator=(const ClusterBackRefs& src); |
| 177 |
| 178 static int Compare(const ClusterBackRefs& a, const ClusterBackRefs& b); |
| 179 |
| 180 JSObjectsCluster cluster; |
| 181 ZoneList<JSObjectsCluster> refs; |
| 182 }; |
| 183 typedef ZoneList<ClusterBackRefs> SimilarityList; |
| 184 |
| 185 // A tree for storing a list of equivalents for a cluster. |
| 186 struct ClusterEqualityConfig { |
| 187 typedef JSObjectsCluster Key; |
| 188 typedef JSObjectsCluster Value; |
| 189 static const Key kNoKey; |
| 190 static const Value kNoValue; |
| 191 static int Compare(const Key& a, const Key& b) { |
| 192 return Key::Compare(a, b); |
| 193 } |
| 194 }; |
| 195 typedef ZoneSplayTree<ClusterEqualityConfig> EqualityTree; |
| 196 |
| 197 static int ClusterBackRefsCmp( |
| 198 const ClusterBackRefs* a, const ClusterBackRefs* b) { |
| 199 return ClusterBackRefs::Compare(*a, *b); |
| 200 } |
| 201 int DoProcess(JSObjectsClusterTree* tree); |
| 202 int FillEqualityTree(); |
| 203 |
| 204 static const int INITIAL_BACKREFS_LIST_CAPACITY = 2; |
| 205 static const int INITIAL_SIMILARITY_LIST_CAPACITY = 2000; |
| 206 // Number of passes for finding equivalents. Limits the length of paths |
| 207 // that can be considered equivalent. |
| 208 static const int MAX_PASSES_COUNT = 10; |
| 209 |
| 210 ZoneScope zscope_; |
| 211 SimilarityList simList_; |
| 212 EqualityTree eqTree_; |
| 213 ClusterBackRefs* currentPair_; |
| 214 JSObjectsClusterTree* currentSet_; |
| 215 }; |
| 216 |
| 217 |
| 218 // RetainerHeapProfile is responsible for gathering and logging |
| 219 // "retainer profile" of JS objects allocated on heap. |
| 220 // It is run during garbage collection cycle, thus it doesn't need |
| 221 // to use handles. |
| 222 class RetainerHeapProfile BASE_EMBEDDED { |
| 223 public: |
| 224 class Printer { |
| 225 public: |
| 226 virtual ~Printer() {} |
| 227 virtual void PrintRetainers(const StringStream& retainers) = 0; |
| 228 }; |
| 229 |
| 230 RetainerHeapProfile(); |
| 231 void CollectStats(HeapObject* obj); |
| 232 void PrintStats(); |
| 233 void DebugPrintStats(Printer* printer); |
| 234 void StoreReference(const JSObjectsCluster& cluster, Object* ref); |
| 235 |
| 236 private: |
| 237 JSObjectsCluster Clusterize(Object* obj); |
| 238 |
| 239 // Limit on the number of retainers to be printed per cluster. |
| 240 static const int MAX_RETAINERS_TO_PRINT = 50; |
| 241 ZoneScope zscope_; |
| 242 JSObjectsClusterTree retainers_tree_; |
| 243 ClustersCoarser coarser_; |
| 244 // TODO(mnaganov): Use some helper class to hold these state variables. |
| 245 JSObjectsClusterTree* coarse_cluster_tree_; |
| 246 int retainers_printed_; |
| 247 Printer* current_printer_; |
| 248 StringStream* current_stream_; |
| 249 public: |
| 250 // Used by JSObjectsClusterTree::ForEach. |
| 251 void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 252 }; |
| 253 |
| 254 |
| 255 #endif // ENABLE_LOGGING_AND_PROFILING |
| 256 |
| 257 } } // namespace v8::internal |
| 258 |
| 259 #endif // V8_HEAP_PROFILER_H_ |
OLD | NEW |