| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1 | // Copyright 2009-2010 the V8 project authors. All rights reserved. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | #include "v8.h" |
| 29 | |
| 30 | #include "heap-profiler.h" |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 31 | #include "frames-inl.h" |
| 32 | #include "global-handles.h" |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 33 | #include "profile-generator.h" |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "string-stream.h" |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | |
| 40 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 41 | namespace { |
| 42 | |
| 43 | // Clusterizer is a set of helper functions for converting |
| 44 | // object references into clusters. |
| 45 | class Clusterizer : public AllStatic { |
| 46 | public: |
| 47 | static JSObjectsCluster Clusterize(HeapObject* obj) { |
| 48 | return Clusterize(obj, true); |
| 49 | } |
| 50 | static void InsertIntoTree(JSObjectsClusterTree* tree, |
| 51 | HeapObject* obj, bool fine_grain); |
| 52 | static void InsertReferenceIntoTree(JSObjectsClusterTree* tree, |
| 53 | const JSObjectsCluster& cluster) { |
| 54 | InsertIntoTree(tree, cluster, 0); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | static JSObjectsCluster Clusterize(HeapObject* obj, bool fine_grain); |
| 59 | static int CalculateNetworkSize(JSObject* obj); |
| 60 | static int GetObjectSize(HeapObject* obj) { |
| 61 | return obj->IsJSObject() ? |
| 62 | CalculateNetworkSize(JSObject::cast(obj)) : obj->Size(); |
| 63 | } |
| 64 | static void InsertIntoTree(JSObjectsClusterTree* tree, |
| 65 | const JSObjectsCluster& cluster, int size); |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | JSObjectsCluster Clusterizer::Clusterize(HeapObject* obj, bool fine_grain) { |
| 70 | if (obj->IsJSObject()) { |
| 71 | JSObject* js_obj = JSObject::cast(obj); |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 72 | String* constructor = GetConstructorNameForHeapProfile( |
| 73 | JSObject::cast(js_obj)); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | // Differentiate Object and Array instances. |
| 75 | if (fine_grain && (constructor == Heap::Object_symbol() || |
| 76 | constructor == Heap::Array_symbol())) { |
| 77 | return JSObjectsCluster(constructor, obj); |
| 78 | } else { |
| 79 | return JSObjectsCluster(constructor); |
| 80 | } |
| 81 | } else if (obj->IsString()) { |
| 82 | return JSObjectsCluster(Heap::String_symbol()); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 83 | } else if (obj->IsJSGlobalPropertyCell()) { |
| 84 | return JSObjectsCluster(JSObjectsCluster::GLOBAL_PROPERTY); |
| 85 | } else if (obj->IsCode() || obj->IsSharedFunctionInfo() || obj->IsScript()) { |
| 86 | return JSObjectsCluster(JSObjectsCluster::CODE); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | } |
| 88 | return JSObjectsCluster(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void Clusterizer::InsertIntoTree(JSObjectsClusterTree* tree, |
| 93 | HeapObject* obj, bool fine_grain) { |
| 94 | JSObjectsCluster cluster = Clusterize(obj, fine_grain); |
| 95 | if (cluster.is_null()) return; |
| 96 | InsertIntoTree(tree, cluster, GetObjectSize(obj)); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void Clusterizer::InsertIntoTree(JSObjectsClusterTree* tree, |
| 101 | const JSObjectsCluster& cluster, int size) { |
| 102 | JSObjectsClusterTree::Locator loc; |
| 103 | tree->Insert(cluster, &loc); |
| 104 | NumberAndSizeInfo number_and_size = loc.value(); |
| 105 | number_and_size.increment_number(1); |
| 106 | number_and_size.increment_bytes(size); |
| 107 | loc.set_value(number_and_size); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | int Clusterizer::CalculateNetworkSize(JSObject* obj) { |
| 112 | int size = obj->Size(); |
| 113 | // If 'properties' and 'elements' are non-empty (thus, non-shared), |
| 114 | // take their size into account. |
| Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 115 | if (obj->properties() != Heap::empty_fixed_array()) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | size += obj->properties()->Size(); |
| 117 | } |
| Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 118 | if (obj->elements() != Heap::empty_fixed_array()) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | size += obj->elements()->Size(); |
| 120 | } |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 121 | // For functions, also account non-empty context and literals sizes. |
| 122 | if (obj->IsJSFunction()) { |
| 123 | JSFunction* f = JSFunction::cast(obj); |
| 124 | if (f->unchecked_context()->IsContext()) { |
| 125 | size += f->context()->Size(); |
| 126 | } |
| 127 | if (f->literals()->length() != 0) { |
| 128 | size += f->literals()->Size(); |
| 129 | } |
| 130 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | return size; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // A helper class for recording back references. |
| 136 | class ReferencesExtractor : public ObjectVisitor { |
| 137 | public: |
| 138 | ReferencesExtractor(const JSObjectsCluster& cluster, |
| 139 | RetainerHeapProfile* profile) |
| 140 | : cluster_(cluster), |
| 141 | profile_(profile), |
| 142 | inside_array_(false) { |
| 143 | } |
| 144 | |
| 145 | void VisitPointer(Object** o) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 146 | if ((*o)->IsFixedArray() && !inside_array_) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | // Traverse one level deep for data members that are fixed arrays. |
| 148 | // This covers the case of 'elements' and 'properties' of JSObject, |
| 149 | // and function contexts. |
| 150 | inside_array_ = true; |
| 151 | FixedArray::cast(*o)->Iterate(this); |
| 152 | inside_array_ = false; |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 153 | } else if ((*o)->IsHeapObject()) { |
| 154 | profile_->StoreReference(cluster_, HeapObject::cast(*o)); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | void VisitPointers(Object** start, Object** end) { |
| 159 | for (Object** p = start; p < end; p++) VisitPointer(p); |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | const JSObjectsCluster& cluster_; |
| 164 | RetainerHeapProfile* profile_; |
| 165 | bool inside_array_; |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | // A printer interface implementation for the Retainers profile. |
| 170 | class RetainersPrinter : public RetainerHeapProfile::Printer { |
| 171 | public: |
| 172 | void PrintRetainers(const JSObjectsCluster& cluster, |
| 173 | const StringStream& retainers) { |
| 174 | HeapStringAllocator allocator; |
| 175 | StringStream stream(&allocator); |
| 176 | cluster.Print(&stream); |
| 177 | LOG(HeapSampleJSRetainersEvent( |
| 178 | *(stream.ToCString()), *(retainers.ToCString()))); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | // Visitor for printing a cluster tree. |
| 184 | class ClusterTreePrinter BASE_EMBEDDED { |
| 185 | public: |
| 186 | explicit ClusterTreePrinter(StringStream* stream) : stream_(stream) {} |
| 187 | void Call(const JSObjectsCluster& cluster, |
| 188 | const NumberAndSizeInfo& number_and_size) { |
| 189 | Print(stream_, cluster, number_and_size); |
| 190 | } |
| 191 | static void Print(StringStream* stream, |
| 192 | const JSObjectsCluster& cluster, |
| 193 | const NumberAndSizeInfo& number_and_size); |
| 194 | |
| 195 | private: |
| 196 | StringStream* stream_; |
| 197 | }; |
| 198 | |
| 199 | |
| 200 | void ClusterTreePrinter::Print(StringStream* stream, |
| 201 | const JSObjectsCluster& cluster, |
| 202 | const NumberAndSizeInfo& number_and_size) { |
| 203 | stream->Put(','); |
| 204 | cluster.Print(stream); |
| 205 | stream->Add(";%d", number_and_size.number()); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | // Visitor for printing a retainer tree. |
| 210 | class SimpleRetainerTreePrinter BASE_EMBEDDED { |
| 211 | public: |
| 212 | explicit SimpleRetainerTreePrinter(RetainerHeapProfile::Printer* printer) |
| 213 | : printer_(printer) {} |
| 214 | void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 215 | |
| 216 | private: |
| 217 | RetainerHeapProfile::Printer* printer_; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | void SimpleRetainerTreePrinter::Call(const JSObjectsCluster& cluster, |
| 222 | JSObjectsClusterTree* tree) { |
| 223 | HeapStringAllocator allocator; |
| 224 | StringStream stream(&allocator); |
| 225 | ClusterTreePrinter retainers_printer(&stream); |
| 226 | tree->ForEach(&retainers_printer); |
| 227 | printer_->PrintRetainers(cluster, stream); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | // Visitor for aggregating references count of equivalent clusters. |
| 232 | class RetainersAggregator BASE_EMBEDDED { |
| 233 | public: |
| 234 | RetainersAggregator(ClustersCoarser* coarser, JSObjectsClusterTree* dest_tree) |
| 235 | : coarser_(coarser), dest_tree_(dest_tree) {} |
| 236 | void Call(const JSObjectsCluster& cluster, |
| 237 | const NumberAndSizeInfo& number_and_size); |
| 238 | |
| 239 | private: |
| 240 | ClustersCoarser* coarser_; |
| 241 | JSObjectsClusterTree* dest_tree_; |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | void RetainersAggregator::Call(const JSObjectsCluster& cluster, |
| 246 | const NumberAndSizeInfo& number_and_size) { |
| 247 | JSObjectsCluster eq = coarser_->GetCoarseEquivalent(cluster); |
| 248 | if (eq.is_null()) eq = cluster; |
| 249 | JSObjectsClusterTree::Locator loc; |
| 250 | dest_tree_->Insert(eq, &loc); |
| 251 | NumberAndSizeInfo aggregated_number = loc.value(); |
| 252 | aggregated_number.increment_number(number_and_size.number()); |
| 253 | loc.set_value(aggregated_number); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | // Visitor for printing retainers tree. Aggregates equivalent retainer clusters. |
| 258 | class AggregatingRetainerTreePrinter BASE_EMBEDDED { |
| 259 | public: |
| 260 | AggregatingRetainerTreePrinter(ClustersCoarser* coarser, |
| 261 | RetainerHeapProfile::Printer* printer) |
| 262 | : coarser_(coarser), printer_(printer) {} |
| 263 | void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 264 | |
| 265 | private: |
| 266 | ClustersCoarser* coarser_; |
| 267 | RetainerHeapProfile::Printer* printer_; |
| 268 | }; |
| 269 | |
| 270 | |
| 271 | void AggregatingRetainerTreePrinter::Call(const JSObjectsCluster& cluster, |
| 272 | JSObjectsClusterTree* tree) { |
| 273 | if (!coarser_->GetCoarseEquivalent(cluster).is_null()) return; |
| 274 | JSObjectsClusterTree dest_tree_; |
| 275 | RetainersAggregator retainers_aggregator(coarser_, &dest_tree_); |
| 276 | tree->ForEach(&retainers_aggregator); |
| 277 | HeapStringAllocator allocator; |
| 278 | StringStream stream(&allocator); |
| 279 | ClusterTreePrinter retainers_printer(&stream); |
| 280 | dest_tree_.ForEach(&retainers_printer); |
| 281 | printer_->PrintRetainers(cluster, stream); |
| 282 | } |
| 283 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 284 | } // namespace |
| 285 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | |
| 287 | // A helper class for building a retainers tree, that aggregates |
| 288 | // all equivalent clusters. |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 289 | class RetainerTreeAggregator { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | public: |
| 291 | explicit RetainerTreeAggregator(ClustersCoarser* coarser) |
| 292 | : coarser_(coarser) {} |
| 293 | void Process(JSObjectsRetainerTree* input_tree) { |
| 294 | input_tree->ForEach(this); |
| 295 | } |
| 296 | void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree); |
| 297 | JSObjectsRetainerTree& output_tree() { return output_tree_; } |
| 298 | |
| 299 | private: |
| 300 | ClustersCoarser* coarser_; |
| 301 | JSObjectsRetainerTree output_tree_; |
| 302 | }; |
| 303 | |
| 304 | |
| 305 | void RetainerTreeAggregator::Call(const JSObjectsCluster& cluster, |
| 306 | JSObjectsClusterTree* tree) { |
| 307 | JSObjectsCluster eq = coarser_->GetCoarseEquivalent(cluster); |
| 308 | if (eq.is_null()) return; |
| 309 | JSObjectsRetainerTree::Locator loc; |
| 310 | if (output_tree_.Insert(eq, &loc)) { |
| 311 | loc.set_value(new JSObjectsClusterTree()); |
| 312 | } |
| 313 | RetainersAggregator retainers_aggregator(coarser_, loc.value()); |
| 314 | tree->ForEach(&retainers_aggregator); |
| 315 | } |
| 316 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 318 | HeapProfiler* HeapProfiler::singleton_ = NULL; |
| 319 | |
| 320 | HeapProfiler::HeapProfiler() |
| 321 | : snapshots_(new HeapSnapshotsCollection()), |
| 322 | next_snapshot_uid_(1) { |
| 323 | } |
| 324 | |
| 325 | |
| 326 | HeapProfiler::~HeapProfiler() { |
| 327 | delete snapshots_; |
| 328 | } |
| 329 | |
| 330 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 331 | |
| 332 | void HeapProfiler::Setup() { |
| 333 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 334 | if (singleton_ == NULL) { |
| 335 | singleton_ = new HeapProfiler(); |
| 336 | } |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | |
| 341 | void HeapProfiler::TearDown() { |
| 342 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 343 | delete singleton_; |
| 344 | singleton_ = NULL; |
| 345 | #endif |
| 346 | } |
| 347 | |
| 348 | |
| 349 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 350 | |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 351 | HeapSnapshot* HeapProfiler::TakeSnapshot(const char* name, |
| 352 | int type, |
| 353 | v8::ActivityControl* control) { |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 354 | ASSERT(singleton_ != NULL); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 355 | return singleton_->TakeSnapshotImpl(name, type, control); |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 359 | HeapSnapshot* HeapProfiler::TakeSnapshot(String* name, |
| 360 | int type, |
| 361 | v8::ActivityControl* control) { |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 362 | ASSERT(singleton_ != NULL); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 363 | return singleton_->TakeSnapshotImpl(name, type, control); |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 367 | HeapSnapshot* HeapProfiler::TakeSnapshotImpl(const char* name, |
| 368 | int type, |
| 369 | v8::ActivityControl* control) { |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 370 | HeapSnapshot::Type s_type = static_cast<HeapSnapshot::Type>(type); |
| 371 | HeapSnapshot* result = |
| 372 | snapshots_->NewSnapshot(s_type, name, next_snapshot_uid_++); |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 373 | bool generation_completed = true; |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 374 | switch (s_type) { |
| 375 | case HeapSnapshot::kFull: { |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 376 | HeapSnapshotGenerator generator(result, control); |
| 377 | generation_completed = generator.GenerateSnapshot(); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | case HeapSnapshot::kAggregated: { |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 381 | Heap::CollectAllGarbage(true); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 382 | AggregatedHeapSnapshot agg_snapshot; |
| 383 | AggregatedHeapSnapshotGenerator generator(&agg_snapshot); |
| 384 | generator.GenerateSnapshot(); |
| 385 | generator.FillHeapSnapshot(result); |
| 386 | break; |
| 387 | } |
| 388 | default: |
| 389 | UNREACHABLE(); |
| 390 | } |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 391 | if (!generation_completed) { |
| 392 | delete result; |
| 393 | result = NULL; |
| 394 | } |
| 395 | snapshots_->SnapshotGenerationFinished(result); |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 396 | return result; |
| 397 | } |
| 398 | |
| 399 | |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 400 | HeapSnapshot* HeapProfiler::TakeSnapshotImpl(String* name, |
| 401 | int type, |
| 402 | v8::ActivityControl* control) { |
| 403 | return TakeSnapshotImpl(snapshots_->GetName(name), type, control); |
| Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
| 407 | int HeapProfiler::GetSnapshotsCount() { |
| 408 | ASSERT(singleton_ != NULL); |
| 409 | return singleton_->snapshots_->snapshots()->length(); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | HeapSnapshot* HeapProfiler::GetSnapshot(int index) { |
| 414 | ASSERT(singleton_ != NULL); |
| 415 | return singleton_->snapshots_->snapshots()->at(index); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) { |
| 420 | ASSERT(singleton_ != NULL); |
| 421 | return singleton_->snapshots_->GetSnapshot(uid); |
| 422 | } |
| 423 | |
| 424 | |
| Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 425 | void HeapProfiler::ObjectMoveEvent(Address from, Address to) { |
| 426 | ASSERT(singleton_ != NULL); |
| 427 | singleton_->snapshots_->ObjectMoveEvent(from, to); |
| 428 | } |
| 429 | |
| 430 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | const JSObjectsClusterTreeConfig::Key JSObjectsClusterTreeConfig::kNoKey; |
| 432 | const JSObjectsClusterTreeConfig::Value JSObjectsClusterTreeConfig::kNoValue; |
| 433 | |
| 434 | |
| 435 | ConstructorHeapProfile::ConstructorHeapProfile() |
| 436 | : zscope_(DELETE_ON_EXIT) { |
| 437 | } |
| 438 | |
| 439 | |
| 440 | void ConstructorHeapProfile::Call(const JSObjectsCluster& cluster, |
| 441 | const NumberAndSizeInfo& number_and_size) { |
| 442 | HeapStringAllocator allocator; |
| 443 | StringStream stream(&allocator); |
| 444 | cluster.Print(&stream); |
| 445 | LOG(HeapSampleJSConstructorEvent(*(stream.ToCString()), |
| 446 | number_and_size.number(), |
| 447 | number_and_size.bytes())); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void ConstructorHeapProfile::CollectStats(HeapObject* obj) { |
| 452 | Clusterizer::InsertIntoTree(&js_objects_info_tree_, obj, false); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | void ConstructorHeapProfile::PrintStats() { |
| 457 | js_objects_info_tree_.ForEach(this); |
| 458 | } |
| 459 | |
| 460 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 461 | static const char* GetConstructorName(const char* name) { |
| 462 | return name[0] != '\0' ? name : "(anonymous)"; |
| 463 | } |
| 464 | |
| 465 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 466 | const char* JSObjectsCluster::GetSpecialCaseName() const { |
| 467 | if (constructor_ == FromSpecialCase(ROOTS)) { |
| 468 | return "(roots)"; |
| 469 | } else if (constructor_ == FromSpecialCase(GLOBAL_PROPERTY)) { |
| 470 | return "(global property)"; |
| 471 | } else if (constructor_ == FromSpecialCase(CODE)) { |
| 472 | return "(code)"; |
| 473 | } else if (constructor_ == FromSpecialCase(SELF)) { |
| 474 | return "(self)"; |
| 475 | } |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | void JSObjectsCluster::Print(StringStream* accumulator) const { |
| 481 | ASSERT(!is_null()); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 482 | const char* special_case_name = GetSpecialCaseName(); |
| 483 | if (special_case_name != NULL) { |
| 484 | accumulator->Add(special_case_name); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 485 | } else { |
| 486 | SmartPointer<char> s_name( |
| 487 | constructor_->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL)); |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 488 | accumulator->Add("%s", GetConstructorName(*s_name)); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 489 | if (instance_ != NULL) { |
| 490 | accumulator->Add(":%p", static_cast<void*>(instance_)); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | |
| 496 | void JSObjectsCluster::DebugPrint(StringStream* accumulator) const { |
| 497 | if (!is_null()) { |
| 498 | Print(accumulator); |
| 499 | } else { |
| 500 | accumulator->Add("(null cluster)"); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | inline ClustersCoarser::ClusterBackRefs::ClusterBackRefs( |
| 506 | const JSObjectsCluster& cluster_) |
| 507 | : cluster(cluster_), refs(kInitialBackrefsListCapacity) { |
| 508 | } |
| 509 | |
| 510 | |
| 511 | inline ClustersCoarser::ClusterBackRefs::ClusterBackRefs( |
| 512 | const ClustersCoarser::ClusterBackRefs& src) |
| 513 | : cluster(src.cluster), refs(src.refs.capacity()) { |
| 514 | refs.AddAll(src.refs); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | inline ClustersCoarser::ClusterBackRefs& |
| 519 | ClustersCoarser::ClusterBackRefs::operator=( |
| 520 | const ClustersCoarser::ClusterBackRefs& src) { |
| 521 | if (this == &src) return *this; |
| 522 | cluster = src.cluster; |
| 523 | refs.Clear(); |
| 524 | refs.AddAll(src.refs); |
| 525 | return *this; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | inline int ClustersCoarser::ClusterBackRefs::Compare( |
| 530 | const ClustersCoarser::ClusterBackRefs& a, |
| 531 | const ClustersCoarser::ClusterBackRefs& b) { |
| 532 | int cmp = JSObjectsCluster::CompareConstructors(a.cluster, b.cluster); |
| 533 | if (cmp != 0) return cmp; |
| 534 | if (a.refs.length() < b.refs.length()) return -1; |
| 535 | if (a.refs.length() > b.refs.length()) return 1; |
| 536 | for (int i = 0; i < a.refs.length(); ++i) { |
| 537 | int cmp = JSObjectsCluster::Compare(a.refs[i], b.refs[i]); |
| 538 | if (cmp != 0) return cmp; |
| 539 | } |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | ClustersCoarser::ClustersCoarser() |
| 545 | : zscope_(DELETE_ON_EXIT), |
| 546 | sim_list_(ClustersCoarser::kInitialSimilarityListCapacity), |
| 547 | current_pair_(NULL), |
| 548 | current_set_(NULL), |
| 549 | self_(NULL) { |
| 550 | } |
| 551 | |
| 552 | |
| 553 | void ClustersCoarser::Call(const JSObjectsCluster& cluster, |
| 554 | JSObjectsClusterTree* tree) { |
| 555 | if (!cluster.can_be_coarsed()) return; |
| 556 | ClusterBackRefs pair(cluster); |
| 557 | ASSERT(current_pair_ == NULL); |
| 558 | current_pair_ = &pair; |
| 559 | current_set_ = new JSObjectsRetainerTree(); |
| 560 | self_ = &cluster; |
| 561 | tree->ForEach(this); |
| 562 | sim_list_.Add(pair); |
| 563 | current_pair_ = NULL; |
| 564 | current_set_ = NULL; |
| 565 | self_ = NULL; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | void ClustersCoarser::Call(const JSObjectsCluster& cluster, |
| 570 | const NumberAndSizeInfo& number_and_size) { |
| 571 | ASSERT(current_pair_ != NULL); |
| 572 | ASSERT(current_set_ != NULL); |
| 573 | ASSERT(self_ != NULL); |
| 574 | JSObjectsRetainerTree::Locator loc; |
| 575 | if (JSObjectsCluster::Compare(*self_, cluster) == 0) { |
| 576 | current_pair_->refs.Add(JSObjectsCluster(JSObjectsCluster::SELF)); |
| 577 | return; |
| 578 | } |
| 579 | JSObjectsCluster eq = GetCoarseEquivalent(cluster); |
| 580 | if (!eq.is_null()) { |
| 581 | if (current_set_->Find(eq, &loc)) return; |
| 582 | current_pair_->refs.Add(eq); |
| 583 | current_set_->Insert(eq, &loc); |
| 584 | } else { |
| 585 | current_pair_->refs.Add(cluster); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | |
| 590 | void ClustersCoarser::Process(JSObjectsRetainerTree* tree) { |
| 591 | int last_eq_clusters = -1; |
| 592 | for (int i = 0; i < kMaxPassesCount; ++i) { |
| 593 | sim_list_.Clear(); |
| 594 | const int curr_eq_clusters = DoProcess(tree); |
| 595 | // If no new cluster equivalents discovered, abort processing. |
| 596 | if (last_eq_clusters == curr_eq_clusters) break; |
| 597 | last_eq_clusters = curr_eq_clusters; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
| 602 | int ClustersCoarser::DoProcess(JSObjectsRetainerTree* tree) { |
| 603 | tree->ForEach(this); |
| 604 | sim_list_.Iterate(ClusterBackRefs::SortRefsIterator); |
| 605 | sim_list_.Sort(ClusterBackRefsCmp); |
| 606 | return FillEqualityTree(); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | JSObjectsCluster ClustersCoarser::GetCoarseEquivalent( |
| 611 | const JSObjectsCluster& cluster) { |
| 612 | if (!cluster.can_be_coarsed()) return JSObjectsCluster(); |
| 613 | EqualityTree::Locator loc; |
| 614 | return eq_tree_.Find(cluster, &loc) ? loc.value() : JSObjectsCluster(); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | bool ClustersCoarser::HasAnEquivalent(const JSObjectsCluster& cluster) { |
| 619 | // Return true for coarsible clusters that have a non-identical equivalent. |
| 620 | if (!cluster.can_be_coarsed()) return false; |
| 621 | JSObjectsCluster eq = GetCoarseEquivalent(cluster); |
| 622 | return !eq.is_null() && JSObjectsCluster::Compare(cluster, eq) != 0; |
| 623 | } |
| 624 | |
| 625 | |
| 626 | int ClustersCoarser::FillEqualityTree() { |
| 627 | int eq_clusters_count = 0; |
| 628 | int eq_to = 0; |
| 629 | bool first_added = false; |
| 630 | for (int i = 1; i < sim_list_.length(); ++i) { |
| 631 | if (ClusterBackRefs::Compare(sim_list_[i], sim_list_[eq_to]) == 0) { |
| 632 | EqualityTree::Locator loc; |
| 633 | if (!first_added) { |
| 634 | // Add self-equivalence, if we have more than one item in this |
| 635 | // equivalence class. |
| 636 | eq_tree_.Insert(sim_list_[eq_to].cluster, &loc); |
| 637 | loc.set_value(sim_list_[eq_to].cluster); |
| 638 | first_added = true; |
| 639 | } |
| 640 | eq_tree_.Insert(sim_list_[i].cluster, &loc); |
| 641 | loc.set_value(sim_list_[eq_to].cluster); |
| 642 | ++eq_clusters_count; |
| 643 | } else { |
| 644 | eq_to = i; |
| 645 | first_added = false; |
| 646 | } |
| 647 | } |
| 648 | return eq_clusters_count; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | const JSObjectsCluster ClustersCoarser::ClusterEqualityConfig::kNoKey; |
| 653 | const JSObjectsCluster ClustersCoarser::ClusterEqualityConfig::kNoValue; |
| 654 | const JSObjectsRetainerTreeConfig::Key JSObjectsRetainerTreeConfig::kNoKey; |
| 655 | const JSObjectsRetainerTreeConfig::Value JSObjectsRetainerTreeConfig::kNoValue = |
| 656 | NULL; |
| 657 | |
| 658 | |
| 659 | RetainerHeapProfile::RetainerHeapProfile() |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 660 | : zscope_(DELETE_ON_EXIT), |
| 661 | aggregator_(NULL) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 662 | JSObjectsCluster roots(JSObjectsCluster::ROOTS); |
| 663 | ReferencesExtractor extractor(roots, this); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 664 | Heap::IterateRoots(&extractor, VISIT_ONLY_STRONG); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 668 | RetainerHeapProfile::~RetainerHeapProfile() { |
| 669 | delete aggregator_; |
| 670 | } |
| 671 | |
| 672 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 673 | void RetainerHeapProfile::StoreReference(const JSObjectsCluster& cluster, |
| 674 | HeapObject* ref) { |
| 675 | JSObjectsCluster ref_cluster = Clusterizer::Clusterize(ref); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 676 | if (ref_cluster.is_null()) return; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 677 | JSObjectsRetainerTree::Locator ref_loc; |
| 678 | if (retainers_tree_.Insert(ref_cluster, &ref_loc)) { |
| 679 | ref_loc.set_value(new JSObjectsClusterTree()); |
| 680 | } |
| 681 | JSObjectsClusterTree* referenced_by = ref_loc.value(); |
| 682 | Clusterizer::InsertReferenceIntoTree(referenced_by, cluster); |
| 683 | } |
| 684 | |
| 685 | |
| 686 | void RetainerHeapProfile::CollectStats(HeapObject* obj) { |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 687 | const JSObjectsCluster cluster = Clusterizer::Clusterize(obj); |
| 688 | if (cluster.is_null()) return; |
| 689 | ReferencesExtractor extractor(cluster, this); |
| 690 | obj->Iterate(&extractor); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 694 | void RetainerHeapProfile::CoarseAndAggregate() { |
| 695 | coarser_.Process(&retainers_tree_); |
| 696 | ASSERT(aggregator_ == NULL); |
| 697 | aggregator_ = new RetainerTreeAggregator(&coarser_); |
| 698 | aggregator_->Process(&retainers_tree_); |
| 699 | } |
| 700 | |
| 701 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 702 | void RetainerHeapProfile::DebugPrintStats( |
| 703 | RetainerHeapProfile::Printer* printer) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 704 | // Print clusters that have no equivalents, aggregating their retainers. |
| 705 | AggregatingRetainerTreePrinter agg_printer(&coarser_, printer); |
| 706 | retainers_tree_.ForEach(&agg_printer); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 707 | // Print clusters that have equivalents. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 708 | SimpleRetainerTreePrinter s_printer(printer); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 709 | aggregator_->output_tree().ForEach(&s_printer); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
| 713 | void RetainerHeapProfile::PrintStats() { |
| 714 | RetainersPrinter printer; |
| 715 | DebugPrintStats(&printer); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | // |
| 720 | // HeapProfiler class implementation. |
| 721 | // |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 722 | static void StackWeakReferenceCallback(Persistent<Value> object, |
| 723 | void* trace) { |
| 724 | DeleteArray(static_cast<Address*>(trace)); |
| 725 | object.Dispose(); |
| 726 | } |
| 727 | |
| 728 | |
| 729 | static void PrintProducerStackTrace(Object* obj, void* trace) { |
| 730 | if (!obj->IsJSObject()) return; |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 731 | String* constructor = GetConstructorNameForHeapProfile(JSObject::cast(obj)); |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 732 | SmartPointer<char> s_name( |
| 733 | constructor->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL)); |
| 734 | LOG(HeapSampleJSProducerEvent(GetConstructorName(*s_name), |
| 735 | reinterpret_cast<Address*>(trace))); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | |
| 739 | void HeapProfiler::WriteSample() { |
| 740 | LOG(HeapSampleBeginEvent("Heap", "allocated")); |
| 741 | LOG(HeapSampleStats( |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 742 | "Heap", "allocated", Heap::CommittedMemory(), Heap::SizeOfObjects())); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 743 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 744 | AggregatedHeapSnapshot snapshot; |
| 745 | AggregatedHeapSnapshotGenerator generator(&snapshot); |
| 746 | generator.GenerateSnapshot(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 747 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 748 | HistogramInfo* info = snapshot.info(); |
| 749 | for (int i = FIRST_NONSTRING_TYPE; |
| 750 | i <= AggregatedHeapSnapshotGenerator::kAllStringsType; |
| 751 | ++i) { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 752 | if (info[i].bytes() > 0) { |
| 753 | LOG(HeapSampleItemEvent(info[i].name(), info[i].number(), |
| 754 | info[i].bytes())); |
| 755 | } |
| 756 | } |
| 757 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 758 | snapshot.js_cons_profile()->PrintStats(); |
| 759 | snapshot.js_retainer_profile()->PrintStats(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 760 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 761 | GlobalHandles::IterateWeakRoots(PrintProducerStackTrace, |
| 762 | StackWeakReferenceCallback); |
| 763 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 764 | LOG(HeapSampleEndEvent("Heap", "allocated")); |
| 765 | } |
| 766 | |
| 767 | |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 768 | AggregatedHeapSnapshot::AggregatedHeapSnapshot() |
| 769 | : info_(NewArray<HistogramInfo>( |
| 770 | AggregatedHeapSnapshotGenerator::kAllStringsType + 1)) { |
| 771 | #define DEF_TYPE_NAME(name) info_[name].set_name(#name); |
| 772 | INSTANCE_TYPE_LIST(DEF_TYPE_NAME); |
| 773 | #undef DEF_TYPE_NAME |
| 774 | info_[AggregatedHeapSnapshotGenerator::kAllStringsType].set_name( |
| 775 | "STRING_TYPE"); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | AggregatedHeapSnapshot::~AggregatedHeapSnapshot() { |
| 780 | DeleteArray(info_); |
| 781 | } |
| 782 | |
| 783 | |
| 784 | AggregatedHeapSnapshotGenerator::AggregatedHeapSnapshotGenerator( |
| 785 | AggregatedHeapSnapshot* agg_snapshot) |
| 786 | : agg_snapshot_(agg_snapshot) { |
| 787 | } |
| 788 | |
| 789 | |
| 790 | void AggregatedHeapSnapshotGenerator::CalculateStringsStats() { |
| 791 | HistogramInfo* info = agg_snapshot_->info(); |
| 792 | HistogramInfo& strings = info[kAllStringsType]; |
| 793 | // Lump all the string types together. |
| 794 | #define INCREMENT_SIZE(type, size, name, camel_name) \ |
| 795 | strings.increment_number(info[type].number()); \ |
| 796 | strings.increment_bytes(info[type].bytes()); |
| 797 | STRING_TYPE_LIST(INCREMENT_SIZE); |
| 798 | #undef INCREMENT_SIZE |
| 799 | } |
| 800 | |
| 801 | |
| 802 | void AggregatedHeapSnapshotGenerator::CollectStats(HeapObject* obj) { |
| 803 | InstanceType type = obj->map()->instance_type(); |
| 804 | ASSERT(0 <= type && type <= LAST_TYPE); |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 805 | agg_snapshot_->info()[type].increment_number(1); |
| 806 | agg_snapshot_->info()[type].increment_bytes(obj->Size()); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | void AggregatedHeapSnapshotGenerator::GenerateSnapshot() { |
| Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 811 | HeapIterator iterator(HeapIterator::kFilterFreeListNodes); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 812 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 813 | CollectStats(obj); |
| 814 | agg_snapshot_->js_cons_profile()->CollectStats(obj); |
| 815 | agg_snapshot_->js_retainer_profile()->CollectStats(obj); |
| 816 | } |
| 817 | CalculateStringsStats(); |
| 818 | agg_snapshot_->js_retainer_profile()->CoarseAndAggregate(); |
| 819 | } |
| 820 | |
| 821 | |
| 822 | class CountingConstructorHeapProfileIterator { |
| 823 | public: |
| 824 | CountingConstructorHeapProfileIterator() |
| 825 | : entities_count_(0), children_count_(0) { |
| 826 | } |
| 827 | |
| 828 | void Call(const JSObjectsCluster& cluster, |
| 829 | const NumberAndSizeInfo& number_and_size) { |
| 830 | ++entities_count_; |
| 831 | children_count_ += number_and_size.number(); |
| 832 | } |
| 833 | |
| 834 | int entities_count() { return entities_count_; } |
| 835 | int children_count() { return children_count_; } |
| 836 | |
| 837 | private: |
| 838 | int entities_count_; |
| 839 | int children_count_; |
| 840 | }; |
| 841 | |
| 842 | |
| 843 | static HeapEntry* AddEntryFromAggregatedSnapshot(HeapSnapshot* snapshot, |
| 844 | int* root_child_index, |
| 845 | HeapEntry::Type type, |
| 846 | const char* name, |
| 847 | int count, |
| 848 | int size, |
| 849 | int children_count, |
| 850 | int retainers_count) { |
| 851 | HeapEntry* entry = snapshot->AddEntry( |
| 852 | type, name, count, size, children_count, retainers_count); |
| 853 | ASSERT(entry != NULL); |
| 854 | snapshot->root()->SetUnidirElementReference(*root_child_index, |
| 855 | *root_child_index + 1, |
| 856 | entry); |
| 857 | *root_child_index = *root_child_index + 1; |
| 858 | return entry; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | class AllocatingConstructorHeapProfileIterator { |
| 863 | public: |
| 864 | AllocatingConstructorHeapProfileIterator(HeapSnapshot* snapshot, |
| 865 | int* root_child_index) |
| 866 | : snapshot_(snapshot), |
| 867 | root_child_index_(root_child_index) { |
| 868 | } |
| 869 | |
| 870 | void Call(const JSObjectsCluster& cluster, |
| 871 | const NumberAndSizeInfo& number_and_size) { |
| 872 | const char* name = cluster.GetSpecialCaseName(); |
| 873 | if (name == NULL) { |
| 874 | name = snapshot_->collection()->GetFunctionName(cluster.constructor()); |
| 875 | } |
| 876 | AddEntryFromAggregatedSnapshot(snapshot_, |
| 877 | root_child_index_, |
| 878 | HeapEntry::kObject, |
| 879 | name, |
| 880 | number_and_size.number(), |
| 881 | number_and_size.bytes(), |
| 882 | 0, |
| 883 | 0); |
| 884 | } |
| 885 | |
| 886 | private: |
| 887 | HeapSnapshot* snapshot_; |
| 888 | int* root_child_index_; |
| 889 | }; |
| 890 | |
| 891 | |
| 892 | static HeapObject* ClusterAsHeapObject(const JSObjectsCluster& cluster) { |
| 893 | return cluster.can_be_coarsed() ? |
| 894 | reinterpret_cast<HeapObject*>(cluster.instance()) : cluster.constructor(); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | static JSObjectsCluster HeapObjectAsCluster(HeapObject* object) { |
| 899 | if (object->IsString()) { |
| 900 | return JSObjectsCluster(String::cast(object)); |
| 901 | } else { |
| 902 | JSObject* js_obj = JSObject::cast(object); |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 903 | String* constructor = GetConstructorNameForHeapProfile( |
| 904 | JSObject::cast(js_obj)); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 905 | return JSObjectsCluster(constructor, object); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | class CountingRetainersIterator { |
| 911 | public: |
| 912 | CountingRetainersIterator(const JSObjectsCluster& child_cluster, |
| 913 | HeapEntriesMap* map) |
| 914 | : child_(ClusterAsHeapObject(child_cluster)), map_(map) { |
| 915 | if (map_->Map(child_) == NULL) |
| 916 | map_->Pair(child_, HeapEntriesMap::kHeapEntryPlaceholder); |
| 917 | } |
| 918 | |
| 919 | void Call(const JSObjectsCluster& cluster, |
| 920 | const NumberAndSizeInfo& number_and_size) { |
| 921 | if (map_->Map(ClusterAsHeapObject(cluster)) == NULL) |
| 922 | map_->Pair(ClusterAsHeapObject(cluster), |
| 923 | HeapEntriesMap::kHeapEntryPlaceholder); |
| 924 | map_->CountReference(ClusterAsHeapObject(cluster), child_); |
| 925 | } |
| 926 | |
| 927 | private: |
| 928 | HeapObject* child_; |
| 929 | HeapEntriesMap* map_; |
| 930 | }; |
| 931 | |
| 932 | |
| 933 | class AllocatingRetainersIterator { |
| 934 | public: |
| 935 | AllocatingRetainersIterator(const JSObjectsCluster& child_cluster, |
| 936 | HeapEntriesMap* map) |
| 937 | : child_(ClusterAsHeapObject(child_cluster)), map_(map) { |
| 938 | child_entry_ = map_->Map(child_); |
| 939 | ASSERT(child_entry_ != NULL); |
| 940 | } |
| 941 | |
| 942 | void Call(const JSObjectsCluster& cluster, |
| 943 | const NumberAndSizeInfo& number_and_size) { |
| 944 | int child_index, retainer_index; |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 945 | map_->CountReference(ClusterAsHeapObject(cluster), |
| 946 | child_, |
| 947 | &child_index, |
| 948 | &retainer_index); |
| 949 | map_->Map(ClusterAsHeapObject(cluster))->SetIndexedReference( |
| 950 | HeapGraphEdge::kElement, |
| 951 | child_index, |
| 952 | number_and_size.number(), |
| 953 | child_entry_, |
| 954 | retainer_index); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | private: |
| 958 | HeapObject* child_; |
| 959 | HeapEntriesMap* map_; |
| 960 | HeapEntry* child_entry_; |
| 961 | }; |
| 962 | |
| 963 | |
| 964 | template<class RetainersIterator> |
| 965 | class AggregatingRetainerTreeIterator { |
| 966 | public: |
| 967 | explicit AggregatingRetainerTreeIterator(ClustersCoarser* coarser, |
| 968 | HeapEntriesMap* map) |
| 969 | : coarser_(coarser), map_(map) { |
| 970 | } |
| 971 | |
| 972 | void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree) { |
| 973 | if (coarser_ != NULL && |
| 974 | !coarser_->GetCoarseEquivalent(cluster).is_null()) return; |
| 975 | JSObjectsClusterTree* tree_to_iterate = tree; |
| 976 | ZoneScope zs(DELETE_ON_EXIT); |
| 977 | JSObjectsClusterTree dest_tree_; |
| 978 | if (coarser_ != NULL) { |
| 979 | RetainersAggregator retainers_aggregator(coarser_, &dest_tree_); |
| 980 | tree->ForEach(&retainers_aggregator); |
| 981 | tree_to_iterate = &dest_tree_; |
| 982 | } |
| 983 | RetainersIterator iterator(cluster, map_); |
| 984 | tree_to_iterate->ForEach(&iterator); |
| 985 | } |
| 986 | |
| 987 | private: |
| 988 | ClustersCoarser* coarser_; |
| 989 | HeapEntriesMap* map_; |
| 990 | }; |
| 991 | |
| 992 | |
| 993 | class AggregatedRetainerTreeAllocator { |
| 994 | public: |
| 995 | AggregatedRetainerTreeAllocator(HeapSnapshot* snapshot, |
| 996 | int* root_child_index) |
| 997 | : snapshot_(snapshot), root_child_index_(root_child_index) { |
| 998 | } |
| 999 | |
| 1000 | HeapEntry* GetEntry( |
| 1001 | HeapObject* obj, int children_count, int retainers_count) { |
| 1002 | JSObjectsCluster cluster = HeapObjectAsCluster(obj); |
| 1003 | const char* name = cluster.GetSpecialCaseName(); |
| 1004 | if (name == NULL) { |
| 1005 | name = snapshot_->collection()->GetFunctionName(cluster.constructor()); |
| 1006 | } |
| 1007 | return AddEntryFromAggregatedSnapshot( |
| 1008 | snapshot_, root_child_index_, HeapEntry::kObject, name, |
| 1009 | 0, 0, children_count, retainers_count); |
| 1010 | } |
| 1011 | |
| 1012 | private: |
| 1013 | HeapSnapshot* snapshot_; |
| 1014 | int* root_child_index_; |
| 1015 | }; |
| 1016 | |
| 1017 | |
| 1018 | template<class Iterator> |
| 1019 | void AggregatedHeapSnapshotGenerator::IterateRetainers( |
| 1020 | HeapEntriesMap* entries_map) { |
| 1021 | RetainerHeapProfile* p = agg_snapshot_->js_retainer_profile(); |
| 1022 | AggregatingRetainerTreeIterator<Iterator> agg_ret_iter_1( |
| 1023 | p->coarser(), entries_map); |
| 1024 | p->retainers_tree()->ForEach(&agg_ret_iter_1); |
| 1025 | AggregatingRetainerTreeIterator<Iterator> agg_ret_iter_2(NULL, entries_map); |
| 1026 | p->aggregator()->output_tree().ForEach(&agg_ret_iter_2); |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | void AggregatedHeapSnapshotGenerator::FillHeapSnapshot(HeapSnapshot* snapshot) { |
| 1031 | // Count the number of entities. |
| 1032 | int histogram_entities_count = 0; |
| 1033 | int histogram_children_count = 0; |
| 1034 | int histogram_retainers_count = 0; |
| 1035 | for (int i = FIRST_NONSTRING_TYPE; i <= kAllStringsType; ++i) { |
| 1036 | if (agg_snapshot_->info()[i].bytes() > 0) { |
| 1037 | ++histogram_entities_count; |
| 1038 | } |
| 1039 | } |
| 1040 | CountingConstructorHeapProfileIterator counting_cons_iter; |
| 1041 | agg_snapshot_->js_cons_profile()->ForEach(&counting_cons_iter); |
| 1042 | histogram_entities_count += counting_cons_iter.entities_count(); |
| 1043 | HeapEntriesMap entries_map; |
| 1044 | IterateRetainers<CountingRetainersIterator>(&entries_map); |
| 1045 | histogram_entities_count += entries_map.entries_count(); |
| 1046 | histogram_children_count += entries_map.total_children_count(); |
| 1047 | histogram_retainers_count += entries_map.total_retainers_count(); |
| 1048 | |
| 1049 | // Root entry references all other entries. |
| 1050 | histogram_children_count += histogram_entities_count; |
| 1051 | int root_children_count = histogram_entities_count; |
| 1052 | ++histogram_entities_count; |
| 1053 | |
| 1054 | // Allocate and fill entries in the snapshot, allocate references. |
| 1055 | snapshot->AllocateEntries(histogram_entities_count, |
| 1056 | histogram_children_count, |
| 1057 | histogram_retainers_count); |
| 1058 | snapshot->AddEntry(HeapSnapshot::kInternalRootObject, |
| 1059 | root_children_count, |
| 1060 | 0); |
| 1061 | int root_child_index = 0; |
| 1062 | for (int i = FIRST_NONSTRING_TYPE; i <= kAllStringsType; ++i) { |
| 1063 | if (agg_snapshot_->info()[i].bytes() > 0) { |
| 1064 | AddEntryFromAggregatedSnapshot(snapshot, |
| 1065 | &root_child_index, |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1066 | HeapEntry::kHidden, |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1067 | agg_snapshot_->info()[i].name(), |
| 1068 | agg_snapshot_->info()[i].number(), |
| 1069 | agg_snapshot_->info()[i].bytes(), |
| 1070 | 0, |
| 1071 | 0); |
| 1072 | } |
| 1073 | } |
| 1074 | AllocatingConstructorHeapProfileIterator alloc_cons_iter( |
| 1075 | snapshot, &root_child_index); |
| 1076 | agg_snapshot_->js_cons_profile()->ForEach(&alloc_cons_iter); |
| 1077 | AggregatedRetainerTreeAllocator allocator(snapshot, &root_child_index); |
| 1078 | entries_map.UpdateEntries(&allocator); |
| 1079 | |
| 1080 | // Fill up references. |
| 1081 | IterateRetainers<AllocatingRetainersIterator>(&entries_map); |
| Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1082 | |
| 1083 | snapshot->SetDominatorsToSelf(); |
| Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1087 | bool ProducerHeapProfile::can_log_ = false; |
| 1088 | |
| 1089 | void ProducerHeapProfile::Setup() { |
| 1090 | can_log_ = true; |
| 1091 | } |
| 1092 | |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1093 | void ProducerHeapProfile::DoRecordJSObjectAllocation(Object* obj) { |
| 1094 | ASSERT(FLAG_log_producers); |
| 1095 | if (!can_log_) return; |
| Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1096 | int framesCount = 0; |
| 1097 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) { |
| 1098 | ++framesCount; |
| 1099 | } |
| 1100 | if (framesCount == 0) return; |
| 1101 | ++framesCount; // Reserve place for the terminator item. |
| 1102 | Vector<Address> stack(NewArray<Address>(framesCount), framesCount); |
| 1103 | int i = 0; |
| 1104 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) { |
| 1105 | stack[i++] = it.frame()->pc(); |
| 1106 | } |
| 1107 | stack[i] = NULL; |
| 1108 | Handle<Object> handle = GlobalHandles::Create(obj); |
| 1109 | GlobalHandles::MakeWeak(handle.location(), |
| 1110 | static_cast<void*>(stack.start()), |
| 1111 | StackWeakReferenceCallback); |
| 1112 | } |
| 1113 | |
| 1114 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1115 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 1116 | |
| 1117 | |
| 1118 | } } // namespace v8::internal |