author | stefank |
Mon, 25 Nov 2019 12:31:39 +0100 | |
changeset 59249 | 29b0d0b61615 |
parent 59247 | 56bf71d64d51 |
permissions | -rw-r--r-- |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
1 |
/* |
53404 | 2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
4 |
* |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
8 |
* |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
13 |
* accompanied this code). |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
14 |
* |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
18 |
* |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
21 |
* questions. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
22 |
* |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
23 |
*/ |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
24 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
25 |
#include "precompiled.hpp" |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
26 |
#include "gc/shared/ptrQueue.hpp" |
53404 | 27 |
#include "memory/allocation.hpp" |
28 |
#include "runtime/interfaceSupport.inline.hpp" |
|
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
58095
diff
changeset
|
29 |
#include "runtime/atomic.hpp" |
53404 | 30 |
#include "runtime/semaphore.inline.hpp" |
31 |
#include "runtime/thread.hpp" |
|
32 |
#include "utilities/globalCounter.inline.hpp" |
|
33 |
#include "utilities/globalDefinitions.hpp" |
|
34 |
#include "utilities/ostream.hpp" |
|
35 |
#include "threadHelper.inline.hpp" |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
36 |
#include "unittest.hpp" |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
37 |
|
53404 | 38 |
class BufferNode::TestSupport : AllStatic { |
39 |
public: |
|
40 |
static bool try_transfer_pending(Allocator* allocator) { |
|
41 |
return allocator->try_transfer_pending(); |
|
42 |
} |
|
43 |
||
44 |
class CompletedList; |
|
45 |
class AllocatorThread; |
|
46 |
class ProcessorThread; |
|
47 |
}; |
|
48 |
||
49 |
typedef BufferNode::TestSupport::CompletedList CompletedList; |
|
50 |
typedef BufferNode::TestSupport::AllocatorThread AllocatorThread; |
|
51 |
typedef BufferNode::TestSupport::ProcessorThread ProcessorThread; |
|
52 |
||
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
53 |
// Some basic testing of BufferNode::Allocator. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
54 |
TEST_VM(PtrQueueBufferAllocatorTest, test) { |
53404 | 55 |
const size_t buffer_size = 256; |
56 |
BufferNode::Allocator allocator("Test Buffer Allocator", buffer_size); |
|
57 |
ASSERT_EQ(buffer_size, allocator.buffer_size()); |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
58 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
59 |
// Allocate some new nodes for use in testing. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
60 |
BufferNode* nodes[10] = {}; |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
61 |
const size_t node_count = ARRAY_SIZE(nodes); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
62 |
for (size_t i = 0; i < node_count; ++i) { |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
63 |
ASSERT_EQ(0u, allocator.free_count()); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
64 |
nodes[i] = allocator.allocate(); |
52624
bbdebc744bf3
8214125: [test] Fix comparison between pointer and integer in test_ptrQueueBufferAllocator.cpp
simonis
parents:
52582
diff
changeset
|
65 |
ASSERT_EQ((BufferNode*)NULL, nodes[i]->next()); |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
66 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
67 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
68 |
// Release the nodes, adding them to the allocator's free list. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
69 |
for (size_t i = 0; i < node_count; ++i) { |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
70 |
allocator.release(nodes[i]); |
53404 | 71 |
} |
72 |
ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator)); |
|
73 |
ASSERT_EQ(node_count, allocator.free_count()); |
|
74 |
for (size_t i = 0; i < node_count; ++i) { |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
75 |
if (i == 0) { |
52624
bbdebc744bf3
8214125: [test] Fix comparison between pointer and integer in test_ptrQueueBufferAllocator.cpp
simonis
parents:
52582
diff
changeset
|
76 |
ASSERT_EQ((BufferNode*)NULL, nodes[i]->next()); |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
77 |
} else { |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
78 |
ASSERT_EQ(nodes[i - 1], nodes[i]->next()); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
79 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
80 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
81 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
82 |
// Allocate nodes from the free list. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
83 |
for (size_t i = 0; i < node_count; ++i) { |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
84 |
size_t j = node_count - i; |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
85 |
ASSERT_EQ(nodes[j - 1], allocator.allocate()); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
86 |
} |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
87 |
ASSERT_EQ(0u, allocator.free_count()); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
88 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
89 |
// Release nodes back to the free list. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
90 |
for (size_t i = 0; i < node_count; ++i) { |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
91 |
allocator.release(nodes[i]); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
92 |
} |
53404 | 93 |
ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator)); |
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
94 |
ASSERT_EQ(node_count, allocator.free_count()); |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
95 |
|
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
96 |
// Destroy some nodes in the free list. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
97 |
// We don't have a way to verify destruction, but we can at |
53404 | 98 |
// least verify we don't crash along the way. |
99 |
size_t count = allocator.free_count(); |
|
100 |
ASSERT_EQ(count, allocator.reduce_free_list(count)); |
|
52582
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
101 |
// destroy allocator. |
6df094be7f58
8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents:
diff
changeset
|
102 |
} |
53404 | 103 |
|
104 |
// Stress test with lock-free allocator and completed buffer list. |
|
105 |
// Completed buffer list pop avoids ABA by also being in a critical |
|
106 |
// section that is synchronized by the allocator's release. |
|
107 |
||
108 |
class BufferNode::TestSupport::CompletedList { |
|
109 |
BufferNode::Stack _completed_list; |
|
110 |
||
111 |
public: |
|
112 |
CompletedList() : _completed_list() {} |
|
113 |
||
114 |
~CompletedList() { |
|
115 |
assert(_completed_list.empty(), "completed list not empty"); |
|
116 |
} |
|
117 |
||
118 |
void push(BufferNode* node) { |
|
119 |
assert(node != NULL, "precondition"); |
|
120 |
_completed_list.push(*node); |
|
121 |
} |
|
122 |
||
123 |
BufferNode* pop() { |
|
124 |
GlobalCounter::CriticalSection cs(Thread::current()); |
|
125 |
return _completed_list.pop(); |
|
126 |
} |
|
127 |
}; |
|
128 |
||
129 |
// Simulate a mutator thread, allocating buffers and adding them to |
|
130 |
// the completed buffer list. |
|
131 |
class BufferNode::TestSupport::AllocatorThread : public JavaTestThread { |
|
132 |
BufferNode::Allocator* _allocator; |
|
133 |
CompletedList* _cbl; |
|
134 |
volatile size_t* _total_allocations; |
|
135 |
volatile bool* _continue_running; |
|
136 |
size_t _allocations; |
|
137 |
||
138 |
public: |
|
139 |
AllocatorThread(Semaphore* post, |
|
140 |
BufferNode::Allocator* allocator, |
|
141 |
CompletedList* cbl, |
|
142 |
volatile size_t* total_allocations, |
|
143 |
volatile bool* continue_running) : |
|
144 |
JavaTestThread(post), |
|
145 |
_allocator(allocator), |
|
146 |
_cbl(cbl), |
|
147 |
_total_allocations(total_allocations), |
|
148 |
_continue_running(continue_running), |
|
149 |
_allocations(0) |
|
150 |
{} |
|
151 |
||
152 |
virtual void main_run() { |
|
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
58095
diff
changeset
|
153 |
while (Atomic::load_acquire(_continue_running)) { |
53404 | 154 |
BufferNode* node = _allocator->allocate(); |
155 |
_cbl->push(node); |
|
156 |
++_allocations; |
|
157 |
ThreadBlockInVM tbiv(this); // Safepoint check. |
|
158 |
} |
|
159 |
tty->print_cr("allocations: " SIZE_FORMAT, _allocations); |
|
59249
29b0d0b61615
8234737: Harmonize parameter order in Atomic - add
stefank
parents:
59247
diff
changeset
|
160 |
Atomic::add(_total_allocations, _allocations); |
53404 | 161 |
} |
162 |
}; |
|
163 |
||
164 |
// Simulate a GC thread, taking buffers from the completed buffer list |
|
165 |
// and returning them to the allocator. |
|
166 |
class BufferNode::TestSupport::ProcessorThread : public JavaTestThread { |
|
167 |
BufferNode::Allocator* _allocator; |
|
168 |
CompletedList* _cbl; |
|
169 |
volatile bool* _continue_running; |
|
170 |
||
171 |
public: |
|
172 |
ProcessorThread(Semaphore* post, |
|
173 |
BufferNode::Allocator* allocator, |
|
174 |
CompletedList* cbl, |
|
175 |
volatile bool* continue_running) : |
|
176 |
JavaTestThread(post), |
|
177 |
_allocator(allocator), |
|
178 |
_cbl(cbl), |
|
179 |
_continue_running(continue_running) |
|
180 |
{} |
|
181 |
||
182 |
virtual void main_run() { |
|
183 |
while (true) { |
|
184 |
BufferNode* node = _cbl->pop(); |
|
185 |
if (node != NULL) { |
|
186 |
_allocator->release(node); |
|
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
58095
diff
changeset
|
187 |
} else if (!Atomic::load_acquire(_continue_running)) { |
53404 | 188 |
return; |
189 |
} |
|
190 |
ThreadBlockInVM tbiv(this); // Safepoint check. |
|
191 |
} |
|
192 |
} |
|
193 |
}; |
|
194 |
||
195 |
static void run_test(BufferNode::Allocator* allocator, CompletedList* cbl) { |
|
196 |
const uint nthreads = 4; |
|
197 |
const uint milliseconds_to_run = 1000; |
|
198 |
||
199 |
Semaphore post; |
|
200 |
volatile size_t total_allocations = 0; |
|
201 |
volatile bool allocator_running = true; |
|
202 |
volatile bool processor_running = true; |
|
203 |
||
204 |
ProcessorThread* proc_threads[nthreads] = {}; |
|
205 |
for (uint i = 0; i < nthreads; ++i) { |
|
206 |
proc_threads[i] = new ProcessorThread(&post, |
|
207 |
allocator, |
|
208 |
cbl, |
|
209 |
&processor_running); |
|
210 |
proc_threads[i]->doit(); |
|
211 |
} |
|
212 |
||
213 |
AllocatorThread* alloc_threads[nthreads] = {}; |
|
214 |
for (uint i = 0; i < nthreads; ++i) { |
|
215 |
alloc_threads[i] = new AllocatorThread(&post, |
|
216 |
allocator, |
|
217 |
cbl, |
|
218 |
&total_allocations, |
|
219 |
&allocator_running); |
|
220 |
alloc_threads[i]->doit(); |
|
221 |
} |
|
222 |
||
223 |
JavaThread* this_thread = JavaThread::current(); |
|
224 |
tty->print_cr("Stressing allocator for %u ms", milliseconds_to_run); |
|
225 |
{ |
|
226 |
ThreadInVMfromNative invm(this_thread); |
|
58095 | 227 |
this_thread->sleep(milliseconds_to_run); |
53404 | 228 |
} |
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
58095
diff
changeset
|
229 |
Atomic::release_store(&allocator_running, false); |
53404 | 230 |
for (uint i = 0; i < nthreads; ++i) { |
231 |
ThreadInVMfromNative invm(this_thread); |
|
232 |
post.wait_with_safepoint_check(this_thread); |
|
233 |
} |
|
59247
56bf71d64d51
8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents:
58095
diff
changeset
|
234 |
Atomic::release_store(&processor_running, false); |
53404 | 235 |
for (uint i = 0; i < nthreads; ++i) { |
236 |
ThreadInVMfromNative invm(this_thread); |
|
237 |
post.wait_with_safepoint_check(this_thread); |
|
238 |
} |
|
239 |
ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(allocator)); |
|
240 |
tty->print_cr("total allocations: " SIZE_FORMAT, total_allocations); |
|
241 |
tty->print_cr("allocator free count: " SIZE_FORMAT, allocator->free_count()); |
|
242 |
} |
|
243 |
||
244 |
const size_t buffer_size = 1024; |
|
245 |
||
246 |
TEST_VM(PtrQueueBufferAllocatorTest, stress_free_list_allocator) { |
|
247 |
BufferNode::Allocator allocator("Test Allocator", buffer_size); |
|
248 |
CompletedList completed; |
|
249 |
run_test(&allocator, &completed); |
|
250 |
} |