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 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/shared/ptrQueue.hpp"
27 #include "memory/allocation.hpp"
28 #include "runtime/interfaceSupport.inline.hpp"
29 #include "runtime/orderAccess.hpp"
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"
36 #include "unittest.hpp"
37
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;
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() {
153 while (OrderAccess::load_acquire(_continue_running)) {
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);
160 Atomic::add(_allocations, _total_allocations);
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);
187 } else if (!OrderAccess::load_acquire(_continue_running)) {
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,
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);
227 this_thread->sleep(milliseconds_to_run);
228 }
229 OrderAccess::release_store(&allocator_running, false);
230 for (uint i = 0; i < nthreads; ++i) {
231 ThreadInVMfromNative invm(this_thread);
232 post.wait_with_safepoint_check(this_thread);
233 }
234 OrderAccess::release_store(&processor_running, false);
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 }
|
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 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/shared/ptrQueue.hpp"
27 #include "memory/allocation.hpp"
28 #include "runtime/interfaceSupport.inline.hpp"
29 #include "runtime/atomic.hpp"
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"
36 #include "unittest.hpp"
37
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;
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() {
153 while (Atomic::load_acquire(_continue_running)) {
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);
160 Atomic::add(_allocations, _total_allocations);
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);
187 } else if (!Atomic::load_acquire(_continue_running)) {
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,
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);
227 this_thread->sleep(milliseconds_to_run);
228 }
229 Atomic::release_store(&allocator_running, false);
230 for (uint i = 0; i < nthreads; ++i) {
231 ThreadInVMfromNative invm(this_thread);
232 post.wait_with_safepoint_check(this_thread);
233 }
234 Atomic::release_store(&processor_running, false);
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 }
|