OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/heap/store-buffer.h" | 5 #include "src/heap/store-buffer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "src/counters.h" | 9 #include "src/counters.h" |
10 #include "src/heap/incremental-marking.h" | 10 #include "src/heap/incremental-marking.h" |
11 #include "src/isolate.h" | 11 #include "src/isolate.h" |
12 #include "src/objects-inl.h" | 12 #include "src/objects-inl.h" |
13 #include "src/v8.h" | 13 #include "src/v8.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 StoreBuffer::StoreBuffer(Heap* heap) | 18 StoreBuffer::StoreBuffer(Heap* heap) |
19 : heap_(heap), | 19 : heap_(heap), top_(nullptr), current_(0), virtual_memory_(nullptr) { |
20 top_(nullptr), | 20 for (int i = 0; i < kStoreBuffers; i++) { |
21 start_(nullptr), | 21 start_[i] = nullptr; |
22 limit_(nullptr), | 22 limit_[i] = nullptr; |
23 virtual_memory_(nullptr) {} | 23 lazy_top_[i].SetValue(nullptr); |
24 } | |
25 task_running_.SetValue(false); | |
26 } | |
24 | 27 |
25 void StoreBuffer::SetUp() { | 28 void StoreBuffer::SetUp() { |
26 // Allocate 3x the buffer size, so that we can start the new store buffer | 29 // Allocate 3x the buffer size, so that we can start the new store buffer |
27 // aligned to 2x the size. This lets us use a bit test to detect the end of | 30 // aligned to 2x the size. This lets us use a bit test to detect the end of |
28 // the area. | 31 // the area. |
29 virtual_memory_ = new base::VirtualMemory(kStoreBufferSize * 2); | 32 virtual_memory_ = new base::VirtualMemory(kStoreBufferSize * 3); |
30 uintptr_t start_as_int = | 33 uintptr_t start_as_int = |
31 reinterpret_cast<uintptr_t>(virtual_memory_->address()); | 34 reinterpret_cast<uintptr_t>(virtual_memory_->address()); |
32 start_ = reinterpret_cast<Address*>(RoundUp(start_as_int, kStoreBufferSize)); | 35 start_[0] = |
33 limit_ = start_ + (kStoreBufferSize / kPointerSize); | 36 reinterpret_cast<Address*>(RoundUp(start_as_int, kStoreBufferSize)); |
37 limit_[0] = start_[0] + (kStoreBufferSize / kPointerSize); | |
38 start_[1] = limit_[0]; | |
39 limit_[1] = start_[1] + (kStoreBufferSize / kPointerSize); | |
34 | 40 |
35 DCHECK(reinterpret_cast<Address>(start_) >= virtual_memory_->address()); | |
36 DCHECK(reinterpret_cast<Address>(limit_) >= virtual_memory_->address()); | |
37 Address* vm_limit = reinterpret_cast<Address*>( | 41 Address* vm_limit = reinterpret_cast<Address*>( |
38 reinterpret_cast<char*>(virtual_memory_->address()) + | 42 reinterpret_cast<char*>(virtual_memory_->address()) + |
39 virtual_memory_->size()); | 43 virtual_memory_->size()); |
40 DCHECK(start_ <= vm_limit); | 44 |
41 DCHECK(limit_ <= vm_limit); | |
42 USE(vm_limit); | 45 USE(vm_limit); |
43 DCHECK((reinterpret_cast<uintptr_t>(limit_) & kStoreBufferMask) == 0); | 46 for (int i = 0; i < kStoreBuffers; i++) { |
47 DCHECK(reinterpret_cast<Address>(start_[i]) >= virtual_memory_->address()); | |
48 DCHECK(reinterpret_cast<Address>(limit_[i]) >= virtual_memory_->address()); | |
49 DCHECK(start_[i] <= vm_limit); | |
50 DCHECK(limit_[i] <= vm_limit); | |
51 DCHECK((reinterpret_cast<uintptr_t>(limit_[i]) & kStoreBufferMask) == 0); | |
52 } | |
44 | 53 |
45 if (!virtual_memory_->Commit(reinterpret_cast<Address>(start_), | 54 if (!virtual_memory_->Commit(reinterpret_cast<Address>(start_[0]), |
46 kStoreBufferSize, | 55 kStoreBufferSize * kStoreBuffers, |
47 false)) { // Not executable. | 56 false)) { // Not executable. |
48 V8::FatalProcessOutOfMemory("StoreBuffer::SetUp"); | 57 V8::FatalProcessOutOfMemory("StoreBuffer::SetUp"); |
49 } | 58 } |
50 top_ = start_; | 59 current_ = 0; |
60 top_ = start_[current_]; | |
51 } | 61 } |
52 | 62 |
53 | 63 |
54 void StoreBuffer::TearDown() { | 64 void StoreBuffer::TearDown() { |
55 delete virtual_memory_; | 65 delete virtual_memory_; |
56 top_ = start_ = limit_ = nullptr; | 66 top_ = nullptr; |
67 for (int i = 0; i < kStoreBuffers; i++) { | |
68 start_[i] = nullptr; | |
69 limit_[i] = nullptr; | |
70 lazy_top_[i].SetValue(nullptr); | |
71 } | |
57 } | 72 } |
58 | 73 |
59 | 74 |
60 void StoreBuffer::StoreBufferOverflow(Isolate* isolate) { | 75 void StoreBuffer::StoreBufferOverflow(Isolate* isolate) { |
61 isolate->heap()->store_buffer()->MoveEntriesToRememberedSet(); | 76 isolate->heap()->store_buffer()->FlipStoreBuffers(); |
62 isolate->counters()->store_buffer_overflows()->Increment(); | 77 isolate->counters()->store_buffer_overflows()->Increment(); |
63 } | 78 } |
64 | 79 |
65 void StoreBuffer::MoveEntriesToRememberedSet() { | 80 void StoreBuffer::FlipStoreBuffers() { |
66 if (top_ == start_) return; | 81 int other = (current_ + 1) % kStoreBuffers; |
67 DCHECK(top_ <= limit_); | 82 Address* other_top = lazy_top_[other].Value(); |
68 for (Address* current = start_; current < top_; current++) { | 83 if (other_top) { |
84 MoveEntriesToRememberedSet(other); | |
85 } | |
86 | |
87 lazy_top_[current_].SetValue(top_); | |
88 current_ = other; | |
89 top_ = start_[current_]; | |
90 | |
91 if (!task_running_.Value()) { | |
ulan
2016/10/28 09:18:01
It could be that the task is almost finishing, but
Hannes Payer (out of office)
2016/10/28 09:58:32
I optimized for the case that Flip would not need
| |
92 task_running_.SetValue(true); | |
93 Task* task = new Task(heap_->isolate(), this); | |
94 V8::GetCurrentPlatform()->CallOnBackgroundThread( | |
95 task, v8::Platform::kShortRunningTask); | |
96 } | |
97 } | |
98 | |
99 void StoreBuffer::MoveEntriesToRememberedSet(int index) { | |
100 base::LockGuard<base::Mutex> guard(&mutex_); | |
101 Address* limit = lazy_top_[index].Value(); | |
102 if (!limit) return; | |
103 DCHECK_GE(index, 0); | |
104 DCHECK_LT(index, kStoreBuffers); | |
105 for (Address* current = start_[index]; current < limit; current++) { | |
69 DCHECK(!heap_->code_space()->Contains(*current)); | 106 DCHECK(!heap_->code_space()->Contains(*current)); |
70 Address addr = *current; | 107 Address addr = *current; |
71 Page* page = Page::FromAnyPointerAddress(heap_, addr); | 108 Page* page = Page::FromAnyPointerAddress(heap_, addr); |
72 RememberedSet<OLD_TO_NEW>::Insert(page, addr); | 109 if (IsDeletionAddress(addr)) { |
110 current++; | |
111 Address end = *current; | |
112 DCHECK(!IsDeletionAddress(end)); | |
113 addr = UnmarkDeletionAddress(addr); | |
114 if (end) { | |
115 RememberedSet<OLD_TO_NEW>::RemoveRange(page, addr, end, | |
116 SlotSet::PREFREE_EMPTY_BUCKETS); | |
117 } else { | |
118 RememberedSet<OLD_TO_NEW>::Remove(page, addr); | |
119 } | |
120 } else { | |
121 DCHECK(!IsDeletionAddress(addr)); | |
122 RememberedSet<OLD_TO_NEW>::Insert(page, addr); | |
123 } | |
73 } | 124 } |
74 top_ = start_; | 125 lazy_top_[index].SetValue(nullptr); |
126 } | |
127 | |
128 void StoreBuffer::MoveAllEntriesToRememberedSet() { | |
129 int other = (current_ + 1) % kStoreBuffers; | |
130 Address* other_top = lazy_top_[other].Value(); | |
131 if (other_top) { | |
132 MoveEntriesToRememberedSet(other); | |
133 } | |
134 lazy_top_[current_].SetValue(top_); | |
135 MoveEntriesToRememberedSet(current_); | |
136 top_ = start_[current_]; | |
137 } | |
138 | |
139 void StoreBuffer::ConcurrentlyProcessStoreBuffer() { | |
140 for (int i = 0; i < kStoreBuffers; i++) { | |
141 Address* top = lazy_top_[i].Value(); | |
142 if (top) { | |
143 MoveEntriesToRememberedSet(i); | |
144 } | |
145 } | |
146 task_running_.SetValue(false); | |
147 } | |
148 | |
149 void StoreBuffer::DeleteEntry(Address start, Address end) { | |
150 if (top_ + sizeof(Address) * 2 > limit_[current_]) { | |
151 StoreBufferOverflow(heap_->isolate()); | |
152 } | |
153 *top_ = MarkDeletionAddress(start); | |
154 top_++; | |
155 *top_ = end; | |
156 top_++; | |
75 } | 157 } |
76 | 158 |
77 } // namespace internal | 159 } // namespace internal |
78 } // namespace v8 | 160 } // namespace v8 |
OLD | NEW |