Path: blob/master/test/hotspot/gtest/gc/shared/test_ptrQueueBufferAllocator.cpp
41149 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/shared/ptrQueue.hpp"26#include "memory/allocation.hpp"27#include "runtime/interfaceSupport.inline.hpp"28#include "runtime/atomic.hpp"29#include "runtime/semaphore.inline.hpp"30#include "runtime/thread.hpp"31#include "utilities/globalCounter.inline.hpp"32#include "utilities/globalDefinitions.hpp"33#include "utilities/ostream.hpp"34#include "threadHelper.inline.hpp"35#include "unittest.hpp"3637class BufferNode::TestSupport : AllStatic {38public:39static bool try_transfer_pending(Allocator* allocator) {40return allocator->try_transfer_pending();41}4243class CompletedList;44class AllocatorThread;45class ProcessorThread;46};4748typedef BufferNode::TestSupport::CompletedList CompletedList;49typedef BufferNode::TestSupport::AllocatorThread AllocatorThread;50typedef BufferNode::TestSupport::ProcessorThread ProcessorThread;5152// Some basic testing of BufferNode::Allocator.53TEST_VM(PtrQueueBufferAllocatorTest, test) {54const size_t buffer_size = 256;55BufferNode::Allocator allocator("Test Buffer Allocator", buffer_size);56ASSERT_EQ(buffer_size, allocator.buffer_size());5758// Allocate some new nodes for use in testing.59BufferNode* nodes[10] = {};60const size_t node_count = ARRAY_SIZE(nodes);61for (size_t i = 0; i < node_count; ++i) {62ASSERT_EQ(0u, allocator.free_count());63nodes[i] = allocator.allocate();64ASSERT_EQ((BufferNode*)NULL, nodes[i]->next());65}6667// Release the nodes, adding them to the allocator's free list.68for (size_t i = 0; i < node_count; ++i) {69allocator.release(nodes[i]);70}71ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));72ASSERT_EQ(node_count, allocator.free_count());73for (size_t i = 0; i < node_count; ++i) {74if (i == 0) {75ASSERT_EQ((BufferNode*)NULL, nodes[i]->next());76} else {77ASSERT_EQ(nodes[i - 1], nodes[i]->next());78}79}8081// Allocate nodes from the free list.82for (size_t i = 0; i < node_count; ++i) {83size_t j = node_count - i;84ASSERT_EQ(nodes[j - 1], allocator.allocate());85}86ASSERT_EQ(0u, allocator.free_count());8788// Release nodes back to the free list.89for (size_t i = 0; i < node_count; ++i) {90allocator.release(nodes[i]);91}92ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));93ASSERT_EQ(node_count, allocator.free_count());9495// Destroy some nodes in the free list.96// We don't have a way to verify destruction, but we can at97// least verify we don't crash along the way.98size_t count = allocator.free_count();99ASSERT_EQ(count, allocator.reduce_free_list(count));100// destroy allocator.101}102103// Stress test with lock-free allocator and completed buffer list.104// Completed buffer list pop avoids ABA by also being in a critical105// section that is synchronized by the allocator's release.106107class BufferNode::TestSupport::CompletedList {108BufferNode::Stack _completed_list;109110public:111CompletedList() : _completed_list() {}112113~CompletedList() {114assert(_completed_list.empty(), "completed list not empty");115}116117void push(BufferNode* node) {118assert(node != NULL, "precondition");119_completed_list.push(*node);120}121122BufferNode* pop() {123GlobalCounter::CriticalSection cs(Thread::current());124return _completed_list.pop();125}126};127128// Simulate a mutator thread, allocating buffers and adding them to129// the completed buffer list.130class BufferNode::TestSupport::AllocatorThread : public JavaTestThread {131BufferNode::Allocator* _allocator;132CompletedList* _cbl;133volatile size_t* _total_allocations;134volatile bool* _continue_running;135size_t _allocations;136137public:138AllocatorThread(Semaphore* post,139BufferNode::Allocator* allocator,140CompletedList* cbl,141volatile size_t* total_allocations,142volatile bool* continue_running) :143JavaTestThread(post),144_allocator(allocator),145_cbl(cbl),146_total_allocations(total_allocations),147_continue_running(continue_running),148_allocations(0)149{}150151virtual void main_run() {152while (Atomic::load_acquire(_continue_running)) {153BufferNode* node = _allocator->allocate();154_cbl->push(node);155++_allocations;156ThreadBlockInVM tbiv(this); // Safepoint check.157}158tty->print_cr("allocations: " SIZE_FORMAT, _allocations);159Atomic::add(_total_allocations, _allocations);160}161};162163// Simulate a GC thread, taking buffers from the completed buffer list164// and returning them to the allocator.165class BufferNode::TestSupport::ProcessorThread : public JavaTestThread {166BufferNode::Allocator* _allocator;167CompletedList* _cbl;168volatile bool* _continue_running;169170public:171ProcessorThread(Semaphore* post,172BufferNode::Allocator* allocator,173CompletedList* cbl,174volatile bool* continue_running) :175JavaTestThread(post),176_allocator(allocator),177_cbl(cbl),178_continue_running(continue_running)179{}180181virtual void main_run() {182while (true) {183BufferNode* node = _cbl->pop();184if (node != NULL) {185_allocator->release(node);186} else if (!Atomic::load_acquire(_continue_running)) {187return;188}189ThreadBlockInVM tbiv(this); // Safepoint check.190}191}192};193194static void run_test(BufferNode::Allocator* allocator, CompletedList* cbl) {195const uint nthreads = 4;196const uint milliseconds_to_run = 1000;197198Semaphore post;199volatile size_t total_allocations = 0;200volatile bool allocator_running = true;201volatile bool processor_running = true;202203ProcessorThread* proc_threads[nthreads] = {};204for (uint i = 0; i < nthreads; ++i) {205proc_threads[i] = new ProcessorThread(&post,206allocator,207cbl,208&processor_running);209proc_threads[i]->doit();210}211212AllocatorThread* alloc_threads[nthreads] = {};213for (uint i = 0; i < nthreads; ++i) {214alloc_threads[i] = new AllocatorThread(&post,215allocator,216cbl,217&total_allocations,218&allocator_running);219alloc_threads[i]->doit();220}221222JavaThread* this_thread = JavaThread::current();223tty->print_cr("Stressing allocator for %u ms", milliseconds_to_run);224{225ThreadInVMfromNative invm(this_thread);226this_thread->sleep(milliseconds_to_run);227}228Atomic::release_store(&allocator_running, false);229for (uint i = 0; i < nthreads; ++i) {230ThreadInVMfromNative invm(this_thread);231post.wait_with_safepoint_check(this_thread);232}233Atomic::release_store(&processor_running, false);234for (uint i = 0; i < nthreads; ++i) {235ThreadInVMfromNative invm(this_thread);236post.wait_with_safepoint_check(this_thread);237}238ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(allocator));239tty->print_cr("total allocations: " SIZE_FORMAT, total_allocations);240tty->print_cr("allocator free count: " SIZE_FORMAT, allocator->free_count());241}242243const size_t buffer_size = 1024;244245TEST_VM(PtrQueueBufferAllocatorTest, stress_free_list_allocator) {246BufferNode::Allocator allocator("Test Allocator", buffer_size);247CompletedList completed;248run_test(&allocator, &completed);249}250251252