Chromium Code Reviews| 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/heap.h" | 5 #include "vm/heap.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/compiler_stats.h" | 9 #include "vm/compiler_stats.h" |
| 10 #include "vm/flags.h" | 10 #include "vm/flags.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 DEFINE_FLAG(bool, verify_after_gc, false, | 24 DEFINE_FLAG(bool, verify_after_gc, false, |
| 25 "Enables heap verification after GC."); | 25 "Enables heap verification after GC."); |
| 26 DEFINE_FLAG(bool, gc_at_alloc, false, "GC at every allocation."); | 26 DEFINE_FLAG(bool, gc_at_alloc, false, "GC at every allocation."); |
| 27 DEFINE_FLAG(int, new_gen_heap_size, 32, "new gen heap size in MB," | 27 DEFINE_FLAG(int, new_gen_heap_size, 32, "new gen heap size in MB," |
| 28 "e.g: --new_gen_heap_size=64 allocates a 64MB new gen heap"); | 28 "e.g: --new_gen_heap_size=64 allocates a 64MB new gen heap"); |
| 29 DEFINE_FLAG(int, old_gen_heap_size, Heap::kHeapSizeInMB, | 29 DEFINE_FLAG(int, old_gen_heap_size, Heap::kHeapSizeInMB, |
| 30 "old gen heap size in MB," | 30 "old gen heap size in MB," |
| 31 "e.g: --old_gen_heap_size=1024 allocates a 1024MB old gen heap"); | 31 "e.g: --old_gen_heap_size=1024 allocates a 1024MB old gen heap"); |
| 32 DEFINE_FLAG(int, code_heap_size, Heap::kCodeHeapSizeInMB, | 32 DEFINE_FLAG(int, code_heap_size, Heap::kCodeHeapSizeInMB, |
| 33 "code heap size in MB," | 33 "code heap size in MB," |
| 34 "e.g: --code_heap_size=8 allocates a 8MB old gen heap"); | 34 "e.g: --code_heap_size=8 allocates a 8MB code heap"); |
| 35 DEFINE_FLAG(int, stub_code_heap_size, Heap::kStubCodeHeapSizeInKB, | |
| 36 "stub code heap size in KB," | |
| 37 "e.g: --code_heap_size=256 allocates a 256KB stub code heap"); | |
|
Ivan Posva
2012/04/26 15:09:01
Mismatch in flag comment.
siva
2012/04/26 16:51:42
Done.
| |
| 35 | 38 |
| 36 Heap::Heap() { | 39 Heap::Heap() { |
| 37 new_space_ = new Scavenger(this, | 40 new_space_ = new Scavenger(this, |
| 38 (FLAG_new_gen_heap_size * MB), | 41 (FLAG_new_gen_heap_size * MB), |
| 39 kNewObjectAlignmentOffset); | 42 kNewObjectAlignmentOffset); |
| 40 old_space_ = new PageSpace(this, (FLAG_old_gen_heap_size * MB)); | 43 old_space_ = new PageSpace(this, (FLAG_old_gen_heap_size * MB)); |
| 41 code_space_ = new PageSpace(this, (FLAG_code_heap_size * MB), true); | 44 code_space_ = new PageSpace(this, (FLAG_code_heap_size * MB), true); |
| 45 stub_code_space_ = new PageSpace(this, (FLAG_stub_code_heap_size * KB), true); | |
| 42 } | 46 } |
| 43 | 47 |
| 44 | 48 |
| 45 Heap::~Heap() { | 49 Heap::~Heap() { |
| 46 delete new_space_; | 50 delete new_space_; |
| 47 delete old_space_; | 51 delete old_space_; |
| 48 delete code_space_; | 52 delete code_space_; |
| 53 delete stub_code_space_; | |
| 49 } | 54 } |
| 50 | 55 |
| 51 | 56 |
| 52 uword Heap::AllocateNew(intptr_t size) { | 57 uword Heap::AllocateNew(intptr_t size) { |
| 53 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 58 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
| 54 uword addr = new_space_->TryAllocate(size); | 59 uword addr = new_space_->TryAllocate(size); |
| 55 if (addr != 0) { | 60 if (addr != 0) { |
| 56 return addr; | 61 return addr; |
| 57 } | 62 } |
| 58 CollectGarbage(kNew); | 63 CollectGarbage(kNew); |
| 59 if (FLAG_verbose_gc) { | 64 if (FLAG_verbose_gc) { |
| 60 OS::PrintErr("New space (%dk) Old space (%dk) Code space (%dk)\n", | 65 OS::PrintErr("New space (%dk) Old space (%dk) " |
| 66 "Code space (%dk) Stub Code space(%dk)\n", | |
| 61 (new_space_->in_use() / KB), | 67 (new_space_->in_use() / KB), |
| 62 (old_space_->in_use() / KB), | 68 (old_space_->in_use() / KB), |
| 63 (code_space_->in_use() / KB)); | 69 (code_space_->in_use() / KB), |
| 70 (stub_code_space_->in_use() / KB)); | |
| 64 } | 71 } |
| 65 addr = new_space_->TryAllocate(size); | 72 addr = new_space_->TryAllocate(size); |
| 66 if (addr != 0) { | 73 if (addr != 0) { |
| 67 return addr; | 74 return addr; |
| 68 } | 75 } |
| 69 return AllocateOld(size); | 76 return AllocateOld(size); |
| 70 } | 77 } |
| 71 | 78 |
| 72 | 79 |
| 73 uword Heap::AllocateOld(intptr_t size) { | 80 uword Heap::AllocateOld(intptr_t size) { |
| 74 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 81 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
| 75 uword addr = old_space_->TryAllocate(size); | 82 uword addr = old_space_->TryAllocate(size); |
| 76 if (addr == 0) { | 83 if (addr == 0) { |
| 77 CollectAllGarbage(); | 84 CollectAllGarbage(); |
| 78 if (FLAG_verbose_gc) { | 85 if (FLAG_verbose_gc) { |
| 79 OS::PrintErr("New space (%dk) Old space (%dk) Code space (%dk)\n", | 86 OS::PrintErr("New space (%dk) Old space (%dk) " |
| 87 "Code space (%dk) Stub Code space(%dk)\n", | |
| 80 (new_space_->in_use() / KB), | 88 (new_space_->in_use() / KB), |
| 81 (old_space_->in_use() / KB), | 89 (old_space_->in_use() / KB), |
| 82 (code_space_->in_use() / KB)); | 90 (code_space_->in_use() / KB), |
| 91 (stub_code_space_->in_use() / KB)); | |
| 83 } | 92 } |
| 84 addr = old_space_->TryAllocate(size); | 93 addr = old_space_->TryAllocate(size); |
| 85 if (addr == 0) { | 94 if (addr == 0) { |
| 86 // TODO(cshapiro): Support possible heap growth and OOM exception. | 95 // TODO(cshapiro): Support possible heap growth and OOM exception. |
| 87 FATAL("Exhausted heap space."); | 96 FATAL("Exhausted heap space."); |
| 88 } | 97 } |
| 89 } | 98 } |
| 90 return addr; | 99 return addr; |
| 91 } | 100 } |
| 92 | 101 |
| 93 | 102 |
| 94 uword Heap::AllocateCode(intptr_t size) { | 103 uword Heap::AllocateCode(PageSpace* space, intptr_t size) { |
| 95 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 104 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
| 96 ASSERT(Utils::IsAligned(size, OS::PreferredCodeAlignment())); | 105 ASSERT(Utils::IsAligned(size, OS::PreferredCodeAlignment())); |
| 97 uword addr = code_space_->TryAllocate(size); | 106 uword addr = space->TryAllocate(size); |
| 98 if (addr == 0) { | 107 if (addr == 0) { |
| 99 // TODO(iposva): Support GC. | 108 // TODO(iposva): Support GC. |
| 100 FATAL("Exhausted code heap space."); | 109 FATAL("Exhausted code heap space."); |
| 101 } | 110 } |
| 102 if (FLAG_compiler_stats) { | 111 if (FLAG_compiler_stats) { |
| 103 CompilerStats::code_allocated += size; | 112 CompilerStats::code_allocated += size; |
| 104 } | 113 } |
| 105 return addr; | 114 return addr; |
| 106 } | 115 } |
| 107 | 116 |
| 108 | 117 |
| 109 bool Heap::Contains(uword addr) const { | 118 bool Heap::Contains(uword addr) const { |
| 110 return new_space_->Contains(addr) || | 119 return new_space_->Contains(addr) || |
| 111 old_space_->Contains(addr) || | 120 old_space_->Contains(addr) || |
| 112 code_space_->Contains(addr); | 121 code_space_->Contains(addr) || |
| 122 stub_code_space_->Contains(addr); | |
| 113 } | 123 } |
| 114 | 124 |
| 115 | 125 |
| 116 bool Heap::CodeContains(uword addr) const { | 126 bool Heap::CodeContains(uword addr) const { |
| 117 return code_space_->Contains(addr); | 127 return code_space_->Contains(addr); |
| 118 } | 128 } |
| 119 | 129 |
| 120 | 130 |
| 131 bool Heap::StubCodeContains(uword addr) const { | |
| 132 return stub_code_space_->Contains(addr); | |
| 133 } | |
| 134 | |
| 135 | |
| 121 void Heap::IterateNewPointers(ObjectPointerVisitor* visitor) { | 136 void Heap::IterateNewPointers(ObjectPointerVisitor* visitor) { |
| 122 new_space_->VisitObjectPointers(visitor); | 137 new_space_->VisitObjectPointers(visitor); |
| 123 } | 138 } |
| 124 | 139 |
| 125 | 140 |
| 126 void Heap::IterateOldPointers(ObjectPointerVisitor* visitor) { | 141 void Heap::IterateOldPointers(ObjectPointerVisitor* visitor) { |
| 127 old_space_->VisitObjectPointers(visitor); | 142 old_space_->VisitObjectPointers(visitor); |
| 128 code_space_->VisitObjectPointers(visitor); | 143 code_space_->VisitObjectPointers(visitor); |
| 144 stub_code_space_->VisitObjectPointers(visitor); | |
| 129 } | 145 } |
| 130 | 146 |
| 131 | 147 |
| 132 void Heap::IterateCodePointers(ObjectPointerVisitor* visitor) { | 148 void Heap::IterateCodePointers(ObjectPointerVisitor* visitor) { |
| 133 code_space_->VisitObjectPointers(visitor); | 149 code_space_->VisitObjectPointers(visitor); |
| 134 } | 150 } |
| 135 | 151 |
| 136 | 152 |
| 153 void Heap::IterateStubCodePointers(ObjectPointerVisitor* visitor) { | |
| 154 stub_code_space_->VisitObjectPointers(visitor); | |
| 155 } | |
| 156 | |
| 157 | |
| 137 RawInstructions* Heap::FindObjectInCodeSpace(FindObjectVisitor* visitor) { | 158 RawInstructions* Heap::FindObjectInCodeSpace(FindObjectVisitor* visitor) { |
| 138 // The code heap can only have RawInstructions objects. | 159 // The code heap can only have RawInstructions objects. |
| 139 RawObject* raw_obj = code_space_->FindObject(visitor); | 160 RawObject* raw_obj = code_space_->FindObject(visitor); |
| 140 ASSERT((raw_obj == Object::null()) || | 161 ASSERT((raw_obj == Object::null()) || |
| 141 (raw_obj->ptr()->class_->ptr()->instance_kind_ == kInstructions)); | 162 (raw_obj->ptr()->class_->ptr()->instance_kind_ == kInstructions)); |
| 142 return reinterpret_cast<RawInstructions*>(raw_obj); | 163 return reinterpret_cast<RawInstructions*>(raw_obj); |
| 143 } | 164 } |
| 144 | 165 |
| 145 | 166 |
| 167 RawInstructions* Heap::FindObjectInStubCodeSpace(FindObjectVisitor* visitor) { | |
| 168 // The stub code heap can only have RawInstructions objects. | |
| 169 RawObject* raw_obj = stub_code_space_->FindObject(visitor); | |
| 170 ASSERT((raw_obj == Object::null()) || | |
| 171 (raw_obj->ptr()->class_->ptr()->instance_kind_ == kInstructions)); | |
| 172 return reinterpret_cast<RawInstructions*>(raw_obj); | |
| 173 } | |
| 174 | |
| 175 | |
| 146 void Heap::CollectGarbage(Space space, ApiCallbacks api_callbacks) { | 176 void Heap::CollectGarbage(Space space, ApiCallbacks api_callbacks) { |
| 147 bool invoke_api_callbacks = (api_callbacks == kInvokeApiCallbacks); | 177 bool invoke_api_callbacks = (api_callbacks == kInvokeApiCallbacks); |
| 148 switch (space) { | 178 switch (space) { |
| 149 case kNew: | 179 case kNew: |
| 150 new_space_->Scavenge(invoke_api_callbacks); | 180 new_space_->Scavenge(invoke_api_callbacks); |
| 151 break; | 181 break; |
| 152 case kOld: | 182 case kOld: |
| 153 old_space_->MarkSweep(invoke_api_callbacks); | 183 old_space_->MarkSweep(invoke_api_callbacks); |
| 154 break; | 184 break; |
| 155 case kExecutable: | 185 case kDartCode: |
| 156 UNIMPLEMENTED(); | 186 UNIMPLEMENTED(); |
| 157 code_space_->MarkSweep(invoke_api_callbacks); | 187 code_space_->MarkSweep(invoke_api_callbacks); |
| 158 break; | 188 break; |
| 189 case kStubCode: | |
| 190 UNIMPLEMENTED(); | |
| 191 stub_code_space_->MarkSweep(invoke_api_callbacks); | |
| 192 break; | |
| 159 default: | 193 default: |
| 160 UNREACHABLE(); | 194 UNREACHABLE(); |
| 161 } | 195 } |
| 162 } | 196 } |
| 163 | 197 |
| 164 | 198 |
| 165 void Heap::CollectGarbage(Space space) { | 199 void Heap::CollectGarbage(Space space) { |
| 166 ApiCallbacks api_callbacks; | 200 ApiCallbacks api_callbacks; |
| 167 if (space == kNew || space == kExecutable) { | 201 if (space == kOld) { |
| 202 api_callbacks = kInvokeApiCallbacks; | |
| 203 } else { | |
| 168 api_callbacks = kIgnoreApiCallbacks; | 204 api_callbacks = kIgnoreApiCallbacks; |
| 169 } else { | |
| 170 api_callbacks = kInvokeApiCallbacks; | |
| 171 } | 205 } |
| 172 CollectGarbage(space, api_callbacks); | 206 CollectGarbage(space, api_callbacks); |
| 173 } | 207 } |
| 174 | 208 |
| 175 | 209 |
| 176 void Heap::CollectAllGarbage() { | 210 void Heap::CollectAllGarbage() { |
| 177 new_space_->Scavenge(kInvokeApiCallbacks); | 211 new_space_->Scavenge(kInvokeApiCallbacks); |
| 178 old_space_->MarkSweep(kInvokeApiCallbacks); | 212 old_space_->MarkSweep(kInvokeApiCallbacks); |
| 179 // TODO(iposva): Merge old and code space. | 213 // TODO(iposva): Merge old and code space. |
| 180 // code_space_->MarkSweep(kInvokeApiCallbacks); | 214 // code_space_->MarkSweep(kInvokeApiCallbacks); |
| 215 // stub_code_space_->MarkSweep(kInvokeApiCallbacks); | |
| 181 } | 216 } |
| 182 | 217 |
| 183 | 218 |
| 184 uword Heap::TopAddress() { | 219 uword Heap::TopAddress() { |
| 185 return reinterpret_cast<uword>(new_space_->TopAddress()); | 220 return reinterpret_cast<uword>(new_space_->TopAddress()); |
| 186 } | 221 } |
| 187 | 222 |
| 188 | 223 |
| 189 uword Heap::EndAddress() { | 224 uword Heap::EndAddress() { |
| 190 return reinterpret_cast<uword>(new_space_->EndAddress()); | 225 return reinterpret_cast<uword>(new_space_->EndAddress()); |
| 191 } | 226 } |
| 192 | 227 |
| 193 | 228 |
| 194 void Heap::Init(Isolate* isolate) { | 229 void Heap::Init(Isolate* isolate) { |
| 195 ASSERT(isolate->heap() == NULL); | 230 ASSERT(isolate->heap() == NULL); |
| 196 Heap* heap = new Heap(); | 231 Heap* heap = new Heap(); |
| 197 isolate->set_heap(heap); | 232 isolate->set_heap(heap); |
| 198 } | 233 } |
| 199 | 234 |
| 200 | 235 |
| 201 bool Heap::Verify() const { | 236 bool Heap::Verify() const { |
| 202 VerifyPointersVisitor visitor; | 237 VerifyPointersVisitor visitor; |
| 203 new_space_->VisitObjectPointers(&visitor); | 238 new_space_->VisitObjectPointers(&visitor); |
| 204 old_space_->VisitObjectPointers(&visitor); | 239 old_space_->VisitObjectPointers(&visitor); |
| 205 code_space_->VisitObjectPointers(&visitor); | 240 code_space_->VisitObjectPointers(&visitor); |
| 241 stub_code_space_->VisitObjectPointers(&visitor); | |
| 206 // Only returning a value so that Heap::Validate can be called from an ASSERT. | 242 // Only returning a value so that Heap::Validate can be called from an ASSERT. |
| 207 return true; | 243 return true; |
| 208 } | 244 } |
| 209 | 245 |
| 210 | 246 |
| 211 #if defined(DEBUG) | 247 #if defined(DEBUG) |
| 212 NoGCScope::NoGCScope() : StackResource(Isolate::Current()) { | 248 NoGCScope::NoGCScope() : StackResource(Isolate::Current()) { |
| 213 isolate()->IncrementNoGCScopeDepth(); | 249 isolate()->IncrementNoGCScopeDepth(); |
| 214 } | 250 } |
| 215 | 251 |
| 216 | 252 |
| 217 NoGCScope::~NoGCScope() { | 253 NoGCScope::~NoGCScope() { |
| 218 isolate()->DecrementNoGCScopeDepth(); | 254 isolate()->DecrementNoGCScopeDepth(); |
| 219 } | 255 } |
| 220 #endif // defined(DEBUG) | 256 #endif // defined(DEBUG) |
| 221 | 257 |
| 222 } // namespace dart | 258 } // namespace dart |
| OLD | NEW |