Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: runtime/vm/heap.cc

Issue 2985863002: Changes new space allocation from simple bump pointer allocation from (Closed)
Patch Set: Fixes formatting and properly abandons mutator thread TLAB Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/heap.h ('k') | runtime/vm/isolate.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/flags.h" 9 #include "vm/flags.h"
10 #include "vm/isolate.h" 10 #include "vm/isolate.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 Heap::~Heap() { 50 Heap::~Heap() {
51 delete barrier_; 51 delete barrier_;
52 delete barrier_done_; 52 delete barrier_done_;
53 53
54 for (int sel = 0; sel < kNumWeakSelectors; sel++) { 54 for (int sel = 0; sel < kNumWeakSelectors; sel++) {
55 delete new_weak_tables_[sel]; 55 delete new_weak_tables_[sel];
56 delete old_weak_tables_[sel]; 56 delete old_weak_tables_[sel];
57 } 57 }
58 } 58 }
59 59
60 void Heap::AbandonRemainingTLAB(Thread* thread) {
61 uword start = thread->top();
62 uword end = thread->end();
63 ASSERT(end >= start);
64 intptr_t size = end - start;
65 if (end == new_space_.end()) {
66 size = 0;
67 }
68 ASSERT(Utils::IsAligned(size, kObjectAlignment));
69 if (size >= kObjectAlignment) {
70 FreeListElement::AsElement(start, size);
71 ASSERT(RawObject::FromAddr(start)->Size() == size);
72 ASSERT((start + size) == new_space_.top());
73 }
74 thread->set_top(0);
75 thread->set_end(0);
76 }
77
78 intptr_t Heap::CalculateTLABSize() {
79 intptr_t size = new_space_.end() - new_space_.top();
80 return Utils::RoundDown(size, kObjectAlignment);
81 }
82
60 uword Heap::AllocateNew(intptr_t size) { 83 uword Heap::AllocateNew(intptr_t size) {
61 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); 84 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0);
62 // Currently, only the Dart thread may allocate in new space. 85 // Currently, only the Dart thread may allocate in new space.
63 isolate()->AssertCurrentThreadIsMutator(); 86 isolate()->AssertCurrentThreadIsMutator();
64 Thread* thread = Thread::Current(); 87 Thread* thread = Thread::Current();
65 uword addr = new_space_.TryAllocateInTLAB(thread, size); 88 uword addr = new_space_.TryAllocateInTLAB(thread, size);
66 if (addr == 0) { 89 if (addr != 0) {
67 // This call to CollectGarbage might end up "reusing" a collection spawned 90 return addr;
68 // from a different thread and will be racing to allocate the requested 91 }
69 // memory with other threads being released after the collection. 92
70 CollectGarbage(kNew); 93 intptr_t tlab_size = CalculateTLABSize();
71 addr = new_space_.TryAllocateInTLAB(thread, size); 94 if ((tlab_size > 0) && (size > tlab_size)) {
72 if (addr == 0) { 95 return AllocateOld(size, HeapPage::kData);
73 return AllocateOld(size, HeapPage::kData); 96 }
97
98 AbandonRemainingTLAB(thread);
99 if (tlab_size > 0) {
100 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size);
101 if (tlab_top != 0) {
102 addr = new_space_.TryAllocateInTLAB(thread, size);
103 ASSERT(addr != 0);
104 return addr;
74 } 105 }
75 } 106 }
76 return addr; 107
108 ASSERT(!thread->HasActiveTLAB());
109
110 // This call to CollectGarbage might end up "reusing" a collection spawned
111 // from a different thread and will be racing to allocate the requested
112 // memory with other threads being released after the collection.
113 CollectGarbage(kNew);
114 tlab_size = CalculateTLABSize();
115 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size);
116 if (tlab_top != 0) {
117 addr = new_space_.TryAllocateInTLAB(thread, size);
118 // It is possible a GC doesn't clear enough space.
119 // In that case, we must fall through and allocate into old space.
120 if (addr != 0) {
121 return addr;
122 }
123 }
124 return AllocateOld(size, HeapPage::kData);
77 } 125 }
78 126
79 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { 127 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) {
80 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); 128 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0);
81 uword addr = old_space_.TryAllocate(size, type); 129 uword addr = old_space_.TryAllocate(size, type);
82 if (addr != 0) { 130 if (addr != 0) {
83 return addr; 131 return addr;
84 } 132 }
85 // If we are in the process of running a sweep, wait for the sweeper to free 133 // If we are in the process of running a sweep, wait for the sweeper to free
86 // memory. 134 // memory.
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 596
549 bool Heap::Verify(MarkExpectation mark_expectation) const { 597 bool Heap::Verify(MarkExpectation mark_expectation) const {
550 HeapIterationScope heap_iteration_scope; 598 HeapIterationScope heap_iteration_scope;
551 return VerifyGC(mark_expectation); 599 return VerifyGC(mark_expectation);
552 } 600 }
553 601
554 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { 602 bool Heap::VerifyGC(MarkExpectation mark_expectation) const {
555 StackZone stack_zone(Thread::Current()); 603 StackZone stack_zone(Thread::Current());
556 604
557 // Change the new space's top_ with the more up-to-date thread's view of top_ 605 // Change the new space's top_ with the more up-to-date thread's view of top_
558 new_space_.FlushTLS(); 606 uword saved_top = new_space_.FlushTLS();
559 607
560 ObjectSet* allocated_set = 608 ObjectSet* allocated_set =
561 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); 609 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation);
562 VerifyPointersVisitor visitor(isolate(), allocated_set); 610 VerifyPointersVisitor visitor(isolate(), allocated_set);
563 VisitObjectPointers(&visitor); 611 VisitObjectPointers(&visitor);
564 612
613 new_space_.UnflushTLS(saved_top);
565 // Only returning a value so that Heap::Validate can be called from an ASSERT. 614 // Only returning a value so that Heap::Validate can be called from an ASSERT.
566 return true; 615 return true;
567 } 616 }
568 617
569 void Heap::PrintSizes() const { 618 void Heap::PrintSizes() const {
570 OS::PrintErr( 619 OS::PrintErr(
571 "New space (%" Pd64 "k of %" Pd64 620 "New space (%" Pd64 "k of %" Pd64
572 "k) " 621 "k) "
573 "Old space (%" Pd64 "k of %" Pd64 "k)\n", 622 "Old space (%" Pd64 "k of %" Pd64 "k)\n",
574 (UsedInWords(kNew) / KBInWords), (CapacityInWords(kNew) / KBInWords), 623 (UsedInWords(kNew) / KBInWords), (CapacityInWords(kNew) / KBInWords),
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 : StackResource(thread) { 878 : StackResource(thread) {
830 Dart::vm_isolate()->heap()->WriteProtect(false); 879 Dart::vm_isolate()->heap()->WriteProtect(false);
831 } 880 }
832 881
833 WritableVMIsolateScope::~WritableVMIsolateScope() { 882 WritableVMIsolateScope::~WritableVMIsolateScope() {
834 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); 883 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0);
835 Dart::vm_isolate()->heap()->WriteProtect(true); 884 Dart::vm_isolate()->heap()->WriteProtect(true);
836 } 885 }
837 886
838 } // namespace dart 887 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/heap.h ('k') | runtime/vm/isolate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698