OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/class_table.h" | 5 #include "vm/class_table.h" |
6 #include "vm/flags.h" | 6 #include "vm/flags.h" |
7 #include "vm/freelist.h" | 7 #include "vm/freelist.h" |
8 #include "vm/object.h" | 8 #include "vm/object.h" |
9 #include "vm/raw_object.h" | 9 #include "vm/raw_object.h" |
10 #include "vm/visitor.h" | 10 #include "vm/visitor.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 DEFINE_FLAG(bool, print_class_table, false, "Print initial class table."); | 14 DEFINE_FLAG(bool, print_class_table, false, "Print initial class table."); |
15 | 15 |
16 ClassTable::ClassTable() | 16 ClassTable::ClassTable() |
17 : top_(kNumPredefinedCids), capacity_(0), table_(NULL) { | 17 : top_(kNumPredefinedCids), capacity_(0), table_(NULL), |
| 18 class_heap_stats_table_(NULL), |
| 19 predefined_class_heap_stats_table_(NULL) { |
18 if (Dart::vm_isolate() == NULL) { | 20 if (Dart::vm_isolate() == NULL) { |
19 capacity_ = initial_capacity_; | 21 capacity_ = initial_capacity_; |
20 table_ = reinterpret_cast<RawClass**>( | 22 table_ = reinterpret_cast<RawClass**>( |
21 calloc(capacity_, sizeof(RawClass*))); // NOLINT | 23 calloc(capacity_, sizeof(RawClass*))); // NOLINT |
22 } else { | 24 } else { |
23 // Duplicate the class table from the VM isolate. | 25 // Duplicate the class table from the VM isolate. |
24 ClassTable* vm_class_table = Dart::vm_isolate()->class_table(); | 26 ClassTable* vm_class_table = Dart::vm_isolate()->class_table(); |
25 capacity_ = vm_class_table->capacity_; | 27 capacity_ = vm_class_table->capacity_; |
26 table_ = reinterpret_cast<RawClass**>( | 28 table_ = reinterpret_cast<RawClass**>( |
27 calloc(capacity_, sizeof(RawClass*))); // NOLINT | 29 calloc(capacity_, sizeof(RawClass*))); // NOLINT |
28 for (intptr_t i = kObjectCid; i < kInstanceCid; i++) { | 30 for (intptr_t i = kObjectCid; i < kInstanceCid; i++) { |
29 table_[i] = vm_class_table->At(i); | 31 table_[i] = vm_class_table->At(i); |
30 } | 32 } |
31 table_[kFreeListElement] = vm_class_table->At(kFreeListElement); | 33 table_[kFreeListElement] = vm_class_table->At(kFreeListElement); |
32 table_[kDynamicCid] = vm_class_table->At(kDynamicCid); | 34 table_[kDynamicCid] = vm_class_table->At(kDynamicCid); |
33 table_[kVoidCid] = vm_class_table->At(kVoidCid); | 35 table_[kVoidCid] = vm_class_table->At(kVoidCid); |
| 36 predefined_class_heap_stats_table_ = reinterpret_cast<ClassHeapStats*>( |
| 37 calloc(kNumPredefinedCids, sizeof(ClassHeapStats))); // NOLINT |
| 38 class_heap_stats_table_ = reinterpret_cast<ClassHeapStats*>( |
| 39 calloc(capacity_, sizeof(ClassHeapStats))); // NOLINT |
| 40 for (intptr_t i = 0; i < capacity_; i++) { |
| 41 class_heap_stats_table_[i].Initialize(); |
| 42 } |
| 43 } |
| 44 predefined_class_heap_stats_table_ = reinterpret_cast<ClassHeapStats*>( |
| 45 calloc(kNumPredefinedCids, sizeof(ClassHeapStats))); // NOLINT |
| 46 for (intptr_t i = 0; i < kNumPredefinedCids; i++) { |
| 47 predefined_class_heap_stats_table_[i].Initialize(); |
34 } | 48 } |
35 } | 49 } |
36 | 50 |
37 | 51 |
38 ClassTable::~ClassTable() { | 52 ClassTable::~ClassTable() { |
39 free(table_); | 53 free(table_); |
| 54 free(predefined_class_heap_stats_table_); |
| 55 free(class_heap_stats_table_); |
40 } | 56 } |
41 | 57 |
42 | 58 |
43 void ClassTable::Register(const Class& cls) { | 59 void ClassTable::Register(const Class& cls) { |
44 intptr_t index = cls.id(); | 60 intptr_t index = cls.id(); |
45 if (index != kIllegalCid) { | 61 if (index != kIllegalCid) { |
46 ASSERT(index > 0); | 62 ASSERT(index > 0); |
47 ASSERT(index < kNumPredefinedCids); | 63 ASSERT(index < kNumPredefinedCids); |
48 ASSERT(table_[index] == 0); | 64 ASSERT(table_[index] == 0); |
49 ASSERT(index < capacity_); | 65 ASSERT(index < capacity_); |
50 table_[index] = cls.raw(); | 66 table_[index] = cls.raw(); |
51 // Add the vtable for this predefined class into the static vtable registry | 67 // Add the vtable for this predefined class into the static vtable registry |
52 // if it has not been setup yet. | 68 // if it has not been setup yet. |
53 cpp_vtable cls_vtable = cls.handle_vtable(); | 69 cpp_vtable cls_vtable = cls.handle_vtable(); |
54 cpp_vtable table_entry = Object::builtin_vtables_[index]; | 70 cpp_vtable table_entry = Object::builtin_vtables_[index]; |
55 ASSERT((table_entry == 0) || (table_entry == cls_vtable)); | 71 ASSERT((table_entry == 0) || (table_entry == cls_vtable)); |
56 if (table_entry == 0) { | 72 if (table_entry == 0) { |
57 Object::builtin_vtables_[index] = cls_vtable; | 73 Object::builtin_vtables_[index] = cls_vtable; |
58 } | 74 } |
59 } else { | 75 } else { |
60 if (top_ == capacity_) { | 76 if (top_ == capacity_) { |
61 // Grow the capacity of the class table. | 77 // Grow the capacity of the class table. |
62 intptr_t new_capacity = capacity_ + capacity_increment_; | 78 intptr_t new_capacity = capacity_ + capacity_increment_; |
63 RawClass** new_table = reinterpret_cast<RawClass**>( | 79 RawClass** new_table = reinterpret_cast<RawClass**>( |
64 realloc(table_, new_capacity * sizeof(RawClass*))); // NOLINT | 80 realloc(table_, new_capacity * sizeof(RawClass*))); // NOLINT |
| 81 ClassHeapStats* new_stats_table = reinterpret_cast<ClassHeapStats*>( |
| 82 realloc(class_heap_stats_table_, |
| 83 new_capacity * sizeof(ClassHeapStats))); // NOLINT |
65 for (intptr_t i = capacity_; i < new_capacity; i++) { | 84 for (intptr_t i = capacity_; i < new_capacity; i++) { |
66 new_table[i] = NULL; | 85 new_table[i] = NULL; |
| 86 new_stats_table[i].Initialize(); |
67 } | 87 } |
68 capacity_ = new_capacity; | 88 capacity_ = new_capacity; |
69 table_ = new_table; | 89 table_ = new_table; |
| 90 class_heap_stats_table_ = new_stats_table; |
70 } | 91 } |
71 ASSERT(top_ < capacity_); | 92 ASSERT(top_ < capacity_); |
72 cls.set_id(top_); | 93 cls.set_id(top_); |
73 table_[top_] = cls.raw(); | 94 table_[top_] = cls.raw(); |
74 top_++; // Increment next index. | 95 top_++; // Increment next index. |
75 } | 96 } |
76 } | 97 } |
77 | 98 |
78 | 99 |
79 void ClassTable::VisitObjectPointers(ObjectPointerVisitor* visitor) { | 100 void ClassTable::VisitObjectPointers(ObjectPointerVisitor* visitor) { |
(...skipping 24 matching lines...) Expand all Loading... |
104 JSONArray members(&jsobj, "members"); | 125 JSONArray members(&jsobj, "members"); |
105 for (intptr_t i = 1; i < top_; i++) { | 126 for (intptr_t i = 1; i < top_; i++) { |
106 if (HasValidClassAt(i)) { | 127 if (HasValidClassAt(i)) { |
107 cls = At(i); | 128 cls = At(i); |
108 members.AddValue(cls); | 129 members.AddValue(cls); |
109 } | 130 } |
110 } | 131 } |
111 } | 132 } |
112 } | 133 } |
113 | 134 |
| 135 |
| 136 void ClassHeapStats::Initialize() { |
| 137 live_count_old_space = 0; |
| 138 live_count_new_space = 0; |
| 139 live_size_old_space = 0; |
| 140 live_size_new_space = 0; |
| 141 new_count_since_gc_new_space = 0; |
| 142 new_count_since_gc_old_space = 0; |
| 143 new_size_since_gc_new_space = 0; |
| 144 new_size_since_gc_old_space = 0; |
| 145 } |
| 146 |
| 147 |
| 148 void ClassHeapStats::ResetAtGC() { |
| 149 live_count_old_space = 0; |
| 150 live_count_new_space = 0; |
| 151 live_size_old_space = 0; |
| 152 live_size_new_space = 0; |
| 153 new_count_since_gc_new_space = 0; |
| 154 new_count_since_gc_old_space = 0; |
| 155 new_size_since_gc_new_space = 0; |
| 156 new_size_since_gc_old_space = 0; |
| 157 } |
| 158 |
| 159 |
| 160 void ClassTable::AllocateClassNewSpace(intptr_t cid, intptr_t size) { |
| 161 ClassHeapStats* stats = FindClassHeapStats(cid); |
| 162 ASSERT(stats != NULL); |
| 163 ASSERT(size != 0); |
| 164 stats->new_count_since_gc_new_space++; |
| 165 stats->new_size_since_gc_new_space += size; |
| 166 } |
| 167 |
| 168 |
| 169 void ClassTable::AllocateClassOldSpace(intptr_t cid, intptr_t size) { |
| 170 ClassHeapStats* stats = FindClassHeapStats(cid); |
| 171 ASSERT(stats != NULL); |
| 172 ASSERT(size != 0); |
| 173 stats->new_count_since_gc_old_space++; |
| 174 stats->new_size_since_gc_old_space += size; |
| 175 } |
| 176 |
| 177 |
| 178 // #define DEBUG_PRINT |
| 179 |
| 180 #if defined(DEBUG_PRINT) |
| 181 static void PrintClassHeapStats(intptr_t cid, const ClassHeapStats& stat) { |
| 182 int new_new = static_cast<int>(stat.new_count_since_gc_new_space); |
| 183 int new_old = static_cast<int>(stat.new_count_since_gc_old_space); |
| 184 int b_new = static_cast<int>(stat.new_size_since_gc_new_space); |
| 185 int b_old = static_cast<int>(stat.new_size_since_gc_old_space); |
| 186 printf("%d (%d %d) [%d %d]\n", static_cast<int>(cid), |
| 187 new_new, b_new, new_old, b_old); |
| 188 } |
| 189 #endif |
| 190 |
| 191 |
| 192 void ClassTable::Collect() { |
| 193 Isolate* isolate = Isolate::Current(); |
| 194 ASSERT(isolate != NULL); |
| 195 #if defined(DEBUG_PRINT) |
| 196 for (intptr_t i = 0; i < kNumPredefinedCids; i++) { |
| 197 const ClassHeapStats& stat = predefined_class_heap_stats_table_[i]; |
| 198 if ((stat.new_count_since_gc_new_space > 0) || |
| 199 (stat.new_count_since_gc_old_space > 0)) { |
| 200 PrintClassHeapStats(i, stat); |
| 201 } |
| 202 } |
| 203 for (intptr_t i = kNumPredefinedCids; i < top_; i++) { |
| 204 const ClassHeapStats& stat = class_heap_stats_table_[i]; |
| 205 if ((stat.new_count_since_gc_new_space > 0) || |
| 206 (stat.new_count_since_gc_old_space > 0)) { |
| 207 PrintClassHeapStats(i, stat); |
| 208 } |
| 209 } |
| 210 #endif |
| 211 ResetCounters(); |
| 212 } |
| 213 |
| 214 |
| 215 ClassHeapStats* ClassTable::FindClassHeapStats(intptr_t cid) { |
| 216 ASSERT(cid > 0); |
| 217 if (cid < kNumPredefinedCids) { |
| 218 return &predefined_class_heap_stats_table_[cid]; |
| 219 } |
| 220 ASSERT(cid < top_); |
| 221 return &class_heap_stats_table_[cid]; |
| 222 } |
| 223 |
| 224 |
| 225 void ClassTable::ResetCounters() { |
| 226 for (intptr_t i = 0; i < kNumPredefinedCids; i++) { |
| 227 predefined_class_heap_stats_table_[i].ResetAtGC(); |
| 228 } |
| 229 for (intptr_t i = kNumPredefinedCids; i < top_; i++) { |
| 230 class_heap_stats_table_[i].ResetAtGC(); |
| 231 } |
| 232 } |
| 233 |
| 234 |
| 235 void ClassTable::ReportLiveOldSpace(intptr_t cid, intptr_t size) { |
| 236 ClassHeapStats* stats = FindClassHeapStats(cid); |
| 237 ASSERT(stats != NULL); |
| 238 ASSERT(size != 0); |
| 239 stats->live_count_old_space++; |
| 240 stats->live_size_old_space += size; |
| 241 } |
| 242 |
| 243 |
| 244 void ClassTable::ReportLiveNewSpace(intptr_t cid, intptr_t size) { |
| 245 ClassHeapStats* stats = FindClassHeapStats(cid); |
| 246 ASSERT(stats != NULL); |
| 247 ASSERT(size != 0); |
| 248 stats->live_count_new_space++; |
| 249 stats->live_size_new_space += size; |
| 250 } |
| 251 |
| 252 |
114 } // namespace dart | 253 } // namespace dart |
OLD | NEW |