OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/assert.h" |
| 6 #include "vm/object.h" |
| 7 #include "vm/heap_class_stats.h" |
| 8 |
| 9 namespace dart { |
| 10 |
| 11 class HeapClassStatsVisitor : public ObjectVisitor { |
| 12 public: |
| 13 explicit HeapClassStatsVisitor(Isolate* isolate) : ObjectVisitor(isolate) { } |
| 14 |
| 15 virtual void VisitObject(RawObject* obj) { |
| 16 intptr_t cid = obj->GetClassId(); |
| 17 if (!obj->IsHeapObject()) { |
| 18 return; |
| 19 } |
| 20 bool new_space = obj->IsNewObject(); |
| 21 HeapClassStatistics* heap_class_stats = isolate()->heap_class_stats(); |
| 22 if (new_space) { |
| 23 heap_class_stats->ReportLiveNewSpace(cid); |
| 24 } else { |
| 25 heap_class_stats->ReportLiveOldSpace(cid); |
| 26 } |
| 27 } |
| 28 |
| 29 private: |
| 30 DISALLOW_COPY_AND_ASSIGN(HeapClassStatsVisitor); |
| 31 }; |
| 32 |
| 33 |
| 34 HeapClassStatistics::HeapClassStatistics(Isolate* isolate) : isolate_(isolate) { |
| 35 class_table_size_ = 512; |
| 36 class_table_ = reinterpret_cast<HeapClassData*>( |
| 37 calloc(class_table_size_, sizeof(HeapClassData))); // NOLINT |
| 38 } |
| 39 |
| 40 |
| 41 HeapClassStatistics::~HeapClassStatistics() { |
| 42 free(class_table_); |
| 43 } |
| 44 |
| 45 |
| 46 void HeapClassStatistics::RegisterClass(const Class& cls) { |
| 47 intptr_t cid = cls.id(); |
| 48 if (cid < class_table_size_) { |
| 49 // Already have room. |
| 50 return; |
| 51 } |
| 52 // Resize the table. |
| 53 const intptr_t new_class_table_size = class_table_size_ * 2; |
| 54 HeapClassData* new_class_table = reinterpret_cast<HeapClassData*>( |
| 55 realloc(class_table_, |
| 56 new_class_table_size * sizeof(HeapClassData))); // NOLINT |
| 57 for (intptr_t i = class_table_size_; i < new_class_table_size; i++) { |
| 58 new_class_table[i].live_old_space = 0; |
| 59 new_class_table[i].live_new_space = 0; |
| 60 new_class_table[i].allocated_since_gc = 0; |
| 61 } |
| 62 class_table_ = new_class_table; |
| 63 class_table_size_ = new_class_table_size; |
| 64 ASSERT(cid < class_table_size_); |
| 65 } |
| 66 |
| 67 |
| 68 void HeapClassStatistics::AllocateClass(intptr_t cid) { |
| 69 ASSERT(cid < class_table_size_); |
| 70 class_table_[cid].allocated_since_gc++; |
| 71 } |
| 72 |
| 73 |
| 74 void HeapClassStatistics::Collect() { |
| 75 HeapClassStatsVisitor visitor(isolate_); |
| 76 // debugging. |
| 77 for (intptr_t i = 0; i < class_table_size_; i++) { |
| 78 break; |
| 79 const HeapClassData& data = class_table_[i]; |
| 80 if ((data.live_old_space == 0) && (data.live_new_space == 0) && |
| 81 (data.allocated_since_gc == 0)) { |
| 82 continue; |
| 83 } |
| 84 printf("%ld %ld %ld %ld\n", i, data.live_old_space, data.live_new_space, |
| 85 data.allocated_since_gc); |
| 86 } |
| 87 ResetCounters(); |
| 88 isolate_->heap()->IterateObjects(&visitor); |
| 89 } |
| 90 |
| 91 |
| 92 uword HeapClassStatistics::ClassStatsTableAddress() { |
| 93 return reinterpret_cast<uword>(&class_table_); |
| 94 } |
| 95 |
| 96 |
| 97 void HeapClassStatistics::ResetCounters() { |
| 98 for (intptr_t i = 0; i < class_table_size_; i++) { |
| 99 class_table_[i].live_old_space = 0; |
| 100 class_table_[i].live_new_space = 0; |
| 101 class_table_[i].allocated_since_gc = 0; |
| 102 } |
| 103 } |
| 104 |
| 105 |
| 106 void HeapClassStatistics::ReportLiveOldSpace(intptr_t cid) { |
| 107 ASSERT(cid < class_table_size_); |
| 108 class_table_[cid].live_old_space++; |
| 109 } |
| 110 |
| 111 |
| 112 void HeapClassStatistics::ReportLiveNewSpace(intptr_t cid) { |
| 113 ASSERT(cid < class_table_size_); |
| 114 class_table_[cid].live_new_space++; |
| 115 } |
| 116 |
| 117 } // namespace dart |
OLD | NEW |