author | kbarrett |
Wed, 26 Jun 2019 13:18:38 -0400 | |
changeset 55498 | e64383344f14 |
parent 54970 | 76d3d96a8bc2 |
child 58059 | baa4dd528de0 |
permissions | -rw-r--r-- |
1374 | 1 |
/* |
53404 | 2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
1374 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5082
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5082
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5082
diff
changeset
|
21 |
* questions. |
1374 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
51441
2e91d927e00c
8154343: Make SATB related code available to other GCs
kbarrett
parents:
51332
diff
changeset
|
26 |
#include "gc/shared/ptrQueue.hpp" |
53404 | 27 |
#include "logging/log.hpp" |
7397 | 28 |
#include "memory/allocation.hpp" |
29 |
#include "memory/allocation.inline.hpp" |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
30 |
#include "runtime/atomic.hpp" |
7397 | 31 |
#include "runtime/mutex.hpp" |
32 |
#include "runtime/mutexLocker.hpp" |
|
53404 | 33 |
#include "runtime/orderAccess.hpp" |
14583
d70ee55535f4
8003935: Simplify the needed includes for using Thread::current()
stefank
parents:
13963
diff
changeset
|
34 |
#include "runtime/thread.inline.hpp" |
53404 | 35 |
#include "utilities/globalCounter.inline.hpp" |
1374 | 36 |
|
36354 | 37 |
#include <new> |
38 |
||
54255 | 39 |
PtrQueue::PtrQueue(PtrQueueSet* qset, bool active) : |
46443 | 40 |
_qset(qset), |
41 |
_active(active), |
|
42 |
_index(0), |
|
43 |
_capacity_in_bytes(0), |
|
54255 | 44 |
_buf(NULL) |
1374 | 45 |
{} |
46 |
||
28507 | 47 |
PtrQueue::~PtrQueue() { |
54255 | 48 |
assert(_buf == NULL, "queue must be flushed before delete"); |
28507 | 49 |
} |
50 |
||
51 |
void PtrQueue::flush_impl() { |
|
46305
bff6d23aa1e3
8175221: Cleanup DirtyCardQueueSet::concatenate_log
kbarrett
parents:
37065
diff
changeset
|
52 |
if (_buf != NULL) { |
46443 | 53 |
BufferNode* node = BufferNode::make_node_from_buffer(_buf, index()); |
37065 | 54 |
if (is_empty()) { |
1374 | 55 |
// No work to do. |
37065 | 56 |
qset()->deallocate_buffer(node); |
1374 | 57 |
} else { |
53102
35530ca3e0b2
8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents:
52726
diff
changeset
|
58 |
qset()->enqueue_completed_buffer(node); |
1374 | 59 |
} |
1560
1b328492b7f8
6770608: G1: Mutator thread can flush barrier and satb queues during safepoint
iveresov
parents:
1374
diff
changeset
|
60 |
_buf = NULL; |
46443 | 61 |
set_index(0); |
1374 | 62 |
} |
63 |
} |
|
64 |
||
65 |
void PtrQueue::enqueue_known_active(void* ptr) { |
|
66 |
while (_index == 0) { |
|
67 |
handle_zero_index(); |
|
68 |
} |
|
4481 | 69 |
|
46443 | 70 |
assert(_buf != NULL, "postcondition"); |
71 |
assert(index() > 0, "postcondition"); |
|
72 |
assert(index() <= capacity(), "invariant"); |
|
73 |
_index -= _element_size; |
|
74 |
_buf[index()] = ptr; |
|
1374 | 75 |
} |
76 |
||
54970
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
77 |
void PtrQueue::handle_zero_index() { |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
78 |
assert(index() == 0, "precondition"); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
79 |
|
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
80 |
if (_buf != NULL) { |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
81 |
handle_completed_buffer(); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
82 |
} else { |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
83 |
// Bootstrapping kludge; lazily initialize capacity. The initial |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
84 |
// thread's queues are constructed before the second phase of the |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
85 |
// two-phase initialization of the associated qsets. As a result, |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
86 |
// we can't initialize _capacity_in_bytes in the queue constructor. |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
87 |
if (_capacity_in_bytes == 0) { |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
88 |
_capacity_in_bytes = index_to_byte_index(qset()->buffer_size()); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
89 |
} |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
90 |
allocate_buffer(); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
91 |
} |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
92 |
} |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
93 |
|
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
94 |
void PtrQueue::allocate_buffer() { |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
95 |
_buf = qset()->allocate_buffer(); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
96 |
reset(); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
97 |
} |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
98 |
|
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
99 |
void PtrQueue::enqueue_completed_buffer() { |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
100 |
assert(_buf != NULL, "precondition"); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
101 |
BufferNode* node = BufferNode::make_node_from_buffer(_buf, index()); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
102 |
qset()->enqueue_completed_buffer(node); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
103 |
allocate_buffer(); |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
104 |
} |
76d3d96a8bc2
8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents:
54810
diff
changeset
|
105 |
|
46443 | 106 |
BufferNode* BufferNode::allocate(size_t size) { |
107 |
size_t byte_size = size * sizeof(void*); |
|
36354 | 108 |
void* data = NEW_C_HEAP_ARRAY(char, buffer_offset() + byte_size, mtGC); |
109 |
return new (data) BufferNode; |
|
110 |
} |
|
111 |
||
112 |
void BufferNode::deallocate(BufferNode* node) { |
|
113 |
node->~BufferNode(); |
|
114 |
FREE_C_HEAP_ARRAY(char, node); |
|
115 |
} |
|
116 |
||
53404 | 117 |
BufferNode::Allocator::Allocator(const char* name, size_t buffer_size) : |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
118 |
_buffer_size(buffer_size), |
53404 | 119 |
_pending_list(), |
120 |
_free_list(), |
|
121 |
_pending_count(0), |
|
122 |
_free_count(0), |
|
123 |
_transfer_lock(false) |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
124 |
{ |
54810 | 125 |
strncpy(_name, name, sizeof(_name) - 1); |
53404 | 126 |
_name[sizeof(_name) - 1] = '\0'; |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
127 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
128 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
129 |
BufferNode::Allocator::~Allocator() { |
53404 | 130 |
delete_list(_free_list.pop_all()); |
131 |
delete_list(_pending_list.pop_all()); |
|
132 |
} |
|
133 |
||
134 |
void BufferNode::Allocator::delete_list(BufferNode* list) { |
|
135 |
while (list != NULL) { |
|
136 |
BufferNode* next = list->next(); |
|
137 |
DEBUG_ONLY(list->set_next(NULL);) |
|
138 |
BufferNode::deallocate(list); |
|
139 |
list = next; |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
140 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
141 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
142 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
143 |
size_t BufferNode::Allocator::free_count() const { |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
144 |
return Atomic::load(&_free_count); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
145 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
146 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
147 |
BufferNode* BufferNode::Allocator::allocate() { |
53404 | 148 |
BufferNode* node; |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
149 |
{ |
53404 | 150 |
// Protect against ABA; see release(). |
151 |
GlobalCounter::CriticalSection cs(Thread::current()); |
|
152 |
node = _free_list.pop(); |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
153 |
} |
53404 | 154 |
if (node == NULL) { |
155 |
node = BufferNode::allocate(_buffer_size); |
|
156 |
} else { |
|
157 |
// Decrement count after getting buffer from free list. This, along |
|
158 |
// with incrementing count before adding to free list, ensures count |
|
159 |
// never underflows. |
|
160 |
size_t count = Atomic::sub(1u, &_free_count); |
|
161 |
assert((count + 1) != 0, "_free_count underflow"); |
|
162 |
} |
|
163 |
return node; |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
164 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
165 |
|
53404 | 166 |
// To solve the ABA problem for lock-free stack pop, allocate does the |
167 |
// pop inside a critical section, and release synchronizes on the |
|
168 |
// critical sections before adding to the _free_list. But we don't |
|
169 |
// want to make every release have to do a synchronize. Instead, we |
|
170 |
// initially place released nodes on the _pending_list, and transfer |
|
171 |
// them to the _free_list in batches. Only one transfer at a time is |
|
172 |
// permitted, with a lock bit to control access to that phase. A |
|
173 |
// transfer takes all the nodes from the _pending_list, synchronizes on |
|
174 |
// the _free_list pops, and then adds the former pending nodes to the |
|
175 |
// _free_list. While that's happening, other threads might be adding |
|
176 |
// other nodes to the _pending_list, to be dealt with by some later |
|
177 |
// transfer. |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
178 |
void BufferNode::Allocator::release(BufferNode* node) { |
53404 | 179 |
assert(node != NULL, "precondition"); |
180 |
assert(node->next() == NULL, "precondition"); |
|
181 |
||
182 |
// Desired minimum transfer batch size. There is relatively little |
|
183 |
// importance to the specific number. It shouldn't be too big, else |
|
184 |
// we're wasting space when the release rate is low. If the release |
|
185 |
// rate is high, we might accumulate more than this before being |
|
186 |
// able to start a new transfer, but that's okay. Also note that |
|
187 |
// the allocation rate and the release rate are going to be fairly |
|
188 |
// similar, due to how the buffers are used. |
|
189 |
const size_t trigger_transfer = 10; |
|
190 |
||
191 |
// Add to pending list. Update count first so no underflow in transfer. |
|
192 |
size_t pending_count = Atomic::add(1u, &_pending_count); |
|
193 |
_pending_list.push(*node); |
|
194 |
if (pending_count > trigger_transfer) { |
|
195 |
try_transfer_pending(); |
|
196 |
} |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
197 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
198 |
|
53404 | 199 |
// Try to transfer nodes from _pending_list to _free_list, with a |
200 |
// synchronization delay for any in-progress pops from the _free_list, |
|
201 |
// to solve ABA there. Return true if performed a (possibly empty) |
|
202 |
// transfer, false if blocked from doing so by some other thread's |
|
203 |
// in-progress transfer. |
|
204 |
bool BufferNode::Allocator::try_transfer_pending() { |
|
205 |
// Attempt to claim the lock. |
|
206 |
if (Atomic::load(&_transfer_lock) || // Skip CAS if likely to fail. |
|
207 |
Atomic::cmpxchg(true, &_transfer_lock, false)) { |
|
208 |
return false; |
|
209 |
} |
|
210 |
// Have the lock; perform the transfer. |
|
211 |
||
212 |
// Claim all the pending nodes. |
|
213 |
BufferNode* first = _pending_list.pop_all(); |
|
214 |
if (first != NULL) { |
|
215 |
// Prepare to add the claimed nodes, and update _pending_count. |
|
216 |
BufferNode* last = first; |
|
217 |
size_t count = 1; |
|
218 |
for (BufferNode* next = first->next(); next != NULL; next = next->next()) { |
|
219 |
last = next; |
|
220 |
++count; |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
221 |
} |
53404 | 222 |
Atomic::sub(count, &_pending_count); |
223 |
||
224 |
// Wait for any in-progress pops, to avoid ABA for them. |
|
225 |
GlobalCounter::write_synchronize(); |
|
226 |
||
227 |
// Add synchronized nodes to _free_list. |
|
228 |
// Update count first so no underflow in allocate(). |
|
229 |
Atomic::add(count, &_free_count); |
|
230 |
_free_list.prepend(*first, *last); |
|
231 |
log_trace(gc, ptrqueue, freelist) |
|
232 |
("Transferred %s pending to free: " SIZE_FORMAT, name(), count); |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
233 |
} |
53404 | 234 |
OrderAccess::release_store(&_transfer_lock, false); |
235 |
return true; |
|
236 |
} |
|
237 |
||
238 |
size_t BufferNode::Allocator::reduce_free_list(size_t remove_goal) { |
|
239 |
try_transfer_pending(); |
|
240 |
size_t removed = 0; |
|
241 |
for ( ; removed < remove_goal; ++removed) { |
|
242 |
BufferNode* node = _free_list.pop(); |
|
243 |
if (node == NULL) break; |
|
244 |
BufferNode::deallocate(node); |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
245 |
} |
53404 | 246 |
size_t new_count = Atomic::sub(removed, &_free_count); |
247 |
log_debug(gc, ptrqueue, freelist) |
|
248 |
("Reduced %s free list by " SIZE_FORMAT " to " SIZE_FORMAT, |
|
249 |
name(), removed, new_count); |
|
250 |
return removed; |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
251 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
252 |
|
55498 | 253 |
PtrQueueSet::PtrQueueSet() : |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
254 |
_allocator(NULL), |
53102
35530ca3e0b2
8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents:
52726
diff
changeset
|
255 |
_all_active(false) |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
256 |
{} |
1374 | 257 |
|
55498 | 258 |
PtrQueueSet::~PtrQueueSet() {} |
33761
329db4b51480
6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents:
30764
diff
changeset
|
259 |
|
55498 | 260 |
void PtrQueueSet::initialize(BufferNode::Allocator* allocator) { |
261 |
assert(allocator != NULL, "Init order issue?"); |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
262 |
_allocator = allocator; |
33761
329db4b51480
6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents:
30764
diff
changeset
|
263 |
} |
329db4b51480
6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents:
30764
diff
changeset
|
264 |
|
1374 | 265 |
void** PtrQueueSet::allocate_buffer() { |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
266 |
BufferNode* node = _allocator->allocate(); |
36354 | 267 |
return BufferNode::make_buffer_from_node(node); |
1374 | 268 |
} |
269 |
||
37065 | 270 |
void PtrQueueSet::deallocate_buffer(BufferNode* node) { |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
51441
diff
changeset
|
271 |
_allocator->release(node); |
1374 | 272 |
} |
273 |