< prev index next >

test/hotspot/gtest/gc/shared/test_ptrQueueBufferAllocator.cpp

Print this page
rev 53142 : [mq]: tschatzl_review
   1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   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  *
  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 




  45 // Some basic testing of BufferNode::Allocator.
  46 TEST_VM(PtrQueueBufferAllocatorTest, test) {
  47   const size_t buffer_size = 256;
  48   BufferNode::Allocator allocator("Test Buffer Allocator", buffer_size);
  49   ASSERT_EQ(buffer_size, allocator.buffer_size());
  50 
  51   // Allocate some new nodes for use in testing.
  52   BufferNode* nodes[10] = {};
  53   const size_t node_count = ARRAY_SIZE(nodes);
  54   for (size_t i = 0; i < node_count; ++i) {
  55     ASSERT_EQ(0u, allocator.free_count());
  56     nodes[i] = allocator.allocate();
  57     ASSERT_EQ((BufferNode*)NULL, nodes[i]->next());
  58   }
  59 
  60   // Release the nodes, adding them to the allocator's free list.
  61   for (size_t i = 0; i < node_count; ++i) {
  62     allocator.release(nodes[i]);
  63   }
  64   ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));


  76     size_t j = node_count - i;
  77     ASSERT_EQ(nodes[j - 1], allocator.allocate());
  78   }
  79   ASSERT_EQ(0u, allocator.free_count());
  80 
  81   // Release nodes back to the free list.
  82   for (size_t i = 0; i < node_count; ++i) {
  83     allocator.release(nodes[i]);
  84   }
  85   ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));
  86   ASSERT_EQ(node_count, allocator.free_count());
  87 
  88   // Destroy some nodes in the free list.
  89   // We don't have a way to verify destruction, but we can at
  90   // least verify we don't crash along the way.
  91   size_t count = allocator.free_count();
  92   ASSERT_EQ(count, allocator.reduce_free_list(count));
  93   // destroy allocator.
  94 }
  95 
  96 // Allocator API for testing.  We don't use BufferNode::Allocator
  97 // directly so that we can add other allocators and compare them.
  98 // BufferNode::Allocator uses a lock-free free list.  Alternatives
  99 // examined include (1) free list protected by lock, (2) no free list,
 100 // just CHeap allocate and free always.  Testing the "no free list"
 101 // version properly requires simulating the full cycle, with the
 102 // allocating thread usually/always being different from the releasing
 103 // thread.  Using the same thread for both operations is often a
 104 // heavily optimized path in the underlying CHeap allocator.
 105 class TestPtrQueueBufferAllocator {
 106   // Noncopyable
 107   TestPtrQueueBufferAllocator(const TestPtrQueueBufferAllocator&);
 108   TestPtrQueueBufferAllocator& operator=(const TestPtrQueueBufferAllocator&);
 109 
 110 protected:
 111   TestPtrQueueBufferAllocator() {}
 112   ~TestPtrQueueBufferAllocator() {}
 113 
 114 public:
 115   virtual BufferNode* allocate() = 0;
 116   virtual void release(BufferNode* node) = 0;
 117   virtual size_t free_count() const = 0;
 118   virtual bool try_transfer_pending() { return true; }
 119 
 120   static const size_t buffer_size = 1024;
 121 };

 122 
 123 class TestPtrQueueBufferCBL {
 124   // Noncopyable.
 125   TestPtrQueueBufferCBL(const TestPtrQueueBufferCBL&);
 126   TestPtrQueueBufferCBL& operator=(const TestPtrQueueBufferCBL&);
 127 
 128 protected:
 129   TestPtrQueueBufferCBL() {}
 130   ~TestPtrQueueBufferCBL() {}
 131 
 132 public:
 133   virtual BufferNode* pop() = 0;
 134   virtual void push(BufferNode* node) = 0;

 135 };
 136 
 137 class PtrQueueBufferAllocatorThread : public JavaTestThread {
 138   TestPtrQueueBufferAllocator* _allocator;
 139   TestPtrQueueBufferCBL* _cbl;


 140   volatile size_t* _total_allocations;
 141   volatile bool* _continue_running;
 142   size_t _allocations;
 143 
 144 public:
 145   PtrQueueBufferAllocatorThread(Semaphore* post,
 146                                 TestPtrQueueBufferAllocator* allocator,
 147                                 TestPtrQueueBufferCBL* cbl,
 148                                 volatile size_t* total_allocations,
 149                                 volatile bool* continue_running) :
 150     JavaTestThread(post),
 151     _allocator(allocator),
 152     _cbl(cbl),
 153     _total_allocations(total_allocations),
 154     _continue_running(continue_running),
 155     _allocations(0)
 156   {}
 157 
 158   virtual void main_run() {
 159     while (OrderAccess::load_acquire(_continue_running)) {
 160       BufferNode* node = _allocator->allocate();
 161       _cbl->push(node);
 162       ++_allocations;
 163       ThreadBlockInVM tbiv(this); // Safepoint check.
 164     }
 165     tty->print_cr("allocations: " SIZE_FORMAT, _allocations);
 166     Atomic::add(_allocations, _total_allocations);
 167   }
 168 };
 169 
 170 class PtrQueueBufferProcessorThread : public JavaTestThread {
 171   TestPtrQueueBufferAllocator* _allocator;
 172   TestPtrQueueBufferCBL* _cbl;


 173   volatile bool* _continue_running;
 174 
 175 public:
 176   PtrQueueBufferProcessorThread(Semaphore* post,
 177                                 TestPtrQueueBufferAllocator* allocator,
 178                                 TestPtrQueueBufferCBL* cbl,
 179                                 volatile bool* continue_running) :
 180     JavaTestThread(post),
 181     _allocator(allocator),
 182     _cbl(cbl),
 183     _continue_running(continue_running)
 184   {}
 185 
 186   virtual void main_run() {
 187     while (true) {
 188       BufferNode* node = _cbl->pop();
 189       if (node != NULL) {
 190         _allocator->release(node);
 191       } else if (!OrderAccess::load_acquire(_continue_running)) {
 192         return;
 193       }
 194       ThreadBlockInVM tbiv(this); // Safepoint check.
 195     }
 196   }
 197 };
 198 
 199 static void run_test(TestPtrQueueBufferAllocator* allocator,
 200                      TestPtrQueueBufferCBL* cbl) {
 201   const uint nthreads = 4;
 202   const uint milliseconds_to_run = 1000;
 203 
 204   Semaphore post;
 205   volatile size_t total_allocations = 0;
 206   volatile bool allocator_running = true;
 207   volatile bool processor_running = true;
 208 
 209   PtrQueueBufferProcessorThread* proc_threads[nthreads] = {};
 210   for (uint i = 0; i < nthreads; ++i) {
 211     proc_threads[i] = new PtrQueueBufferProcessorThread(&post,
 212                                                         allocator,
 213                                                         cbl,
 214                                                         &processor_running);
 215     proc_threads[i]->doit();
 216   }
 217 
 218   PtrQueueBufferAllocatorThread* alloc_threads[nthreads] = {};
 219   for (uint i = 0; i < nthreads; ++i) {
 220     alloc_threads[i] = new PtrQueueBufferAllocatorThread(&post,
 221                                                          allocator,
 222                                                          cbl,
 223                                                          &total_allocations,
 224                                                          &allocator_running);
 225     alloc_threads[i]->doit();
 226   }
 227 
 228   JavaThread* this_thread = JavaThread::current();
 229   tty->print_cr("Stressing allocator for %u ms", milliseconds_to_run);
 230   {
 231     ThreadInVMfromNative invm(this_thread);
 232     os::sleep(this_thread, milliseconds_to_run, true);
 233   }
 234   OrderAccess::release_store(&allocator_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   OrderAccess::release_store(&processor_running, false);
 240   for (uint i = 0; i < nthreads; ++i) {
 241     ThreadInVMfromNative invm(this_thread);
 242     post.wait_with_safepoint_check(this_thread);
 243   }
 244   ASSERT_TRUE(allocator->try_transfer_pending());
 245   tty->print_cr("total allocations: " SIZE_FORMAT, total_allocations);
 246   tty->print_cr("allocator free count: " SIZE_FORMAT, allocator->free_count());
 247 }
 248 
 249 // Stress test with lock-free allocator and completed buffer list.
 250 // Completed buffer list pop avoids ABA by also being in a critical
 251 // section that is synchronized by the allocator's release.
 252 
 253 class FreeListPtrQueueBufferAllocator : public TestPtrQueueBufferAllocator {
 254   BufferNode::Allocator _allocator;
 255 
 256 public:
 257   FreeListPtrQueueBufferAllocator() :
 258     _allocator("Test Buffer Allocator", buffer_size) {}
 259 
 260   virtual BufferNode* allocate() { return _allocator.allocate(); }
 261   virtual void release(BufferNode* node) { _allocator.release(node); }
 262   virtual size_t free_count() const { return _allocator.free_count(); }
 263   virtual bool try_transfer_pending() {
 264     return BufferNode::TestSupport::try_transfer_pending(&_allocator);
 265   }
 266 };
 267 
 268 class FreeListPtrQueueBufferCompletedList : public TestPtrQueueBufferCBL {
 269   BufferNode::Stack _completed_list;
 270 
 271 public:
 272   FreeListPtrQueueBufferCompletedList() : _completed_list() {}
 273 
 274   ~FreeListPtrQueueBufferCompletedList() {
 275     assert(_completed_list.empty(), "completed list not empty");
 276   }
 277 
 278   virtual void push(BufferNode* node) {
 279     assert(node != NULL, "precondition");
 280     _completed_list.push(*node);
 281   }
 282 
 283   virtual BufferNode* pop() {
 284     GlobalCounter::CriticalSection cs(Thread::current());
 285     return _completed_list.pop();
 286   }
 287 };
 288 
 289 TEST_VM(PtrQueueBufferAllocatorTest, stress_free_list_allocator) {
 290   FreeListPtrQueueBufferAllocator allocator;
 291   FreeListPtrQueueBufferCompletedList completed;
 292   run_test(&allocator, &completed);
 293 }
   1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   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  *
  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;
  50 typedef BufferNode::TestSupport::AllocatorThread AllocatorThread;
  51 typedef BufferNode::TestSupport::ProcessorThread ProcessorThread;
  52 
  53 // Some basic testing of BufferNode::Allocator.
  54 TEST_VM(PtrQueueBufferAllocatorTest, test) {
  55   const size_t buffer_size = 256;
  56   BufferNode::Allocator allocator("Test Buffer Allocator", buffer_size);
  57   ASSERT_EQ(buffer_size, allocator.buffer_size());
  58 
  59   // Allocate some new nodes for use in testing.
  60   BufferNode* nodes[10] = {};
  61   const size_t node_count = ARRAY_SIZE(nodes);
  62   for (size_t i = 0; i < node_count; ++i) {
  63     ASSERT_EQ(0u, allocator.free_count());
  64     nodes[i] = allocator.allocate();
  65     ASSERT_EQ((BufferNode*)NULL, nodes[i]->next());
  66   }
  67 
  68   // Release the nodes, adding them to the allocator's free list.
  69   for (size_t i = 0; i < node_count; ++i) {
  70     allocator.release(nodes[i]);
  71   }
  72   ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));


  84     size_t j = node_count - i;
  85     ASSERT_EQ(nodes[j - 1], allocator.allocate());
  86   }
  87   ASSERT_EQ(0u, allocator.free_count());
  88 
  89   // Release nodes back to the free list.
  90   for (size_t i = 0; i < node_count; ++i) {
  91     allocator.release(nodes[i]);
  92   }
  93   ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));
  94   ASSERT_EQ(node_count, allocator.free_count());
  95 
  96   // Destroy some nodes in the free list.
  97   // We don't have a way to verify destruction, but we can at
  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));
 101   // destroy allocator.
 102 }
 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() {
 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,
 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);
 227     os::sleep(this_thread, milliseconds_to_run, true);
 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 }
< prev index next >