< prev index next >

src/share/vm/gc/g1/ptrQueue.cpp

Print this page
rev 12859 : imported patch pqsize


  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/g1/ptrQueue.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/mutex.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 
  33 #include <new>
  34 
  35 PtrQueue::PtrQueue(PtrQueueSet* qset, bool permanent, bool active) :
  36   _qset(qset), _buf(NULL), _index(0), _sz(0), _active(active),
  37   _permanent(permanent), _lock(NULL)





  38 {}
  39 
  40 PtrQueue::~PtrQueue() {
  41   assert(_permanent || (_buf == NULL), "queue must be flushed before delete");
  42 }
  43 
  44 void PtrQueue::flush_impl() {
  45   if (_buf != NULL) {
  46     BufferNode* node = BufferNode::make_node_from_buffer(_buf, _index);
  47     if (is_empty()) {
  48       // No work to do.
  49       qset()->deallocate_buffer(node);
  50     } else {
  51       qset()->enqueue_complete_buffer(node);
  52     }
  53     _buf = NULL;
  54     _index = 0;
  55   }
  56 }
  57 
  58 
  59 void PtrQueue::enqueue_known_active(void* ptr) {
  60   assert(_index <= _sz, "Invariant.");
  61   assert(_index == 0 || _buf != NULL, "invariant");
  62 
  63   while (_index == 0) {
  64     handle_zero_index();
  65   }
  66 
  67   assert(_index > 0, "postcondition");
  68   _index -= sizeof(void*);
  69   _buf[byte_index_to_index(_index)] = ptr;
  70   assert(_index <= _sz, "Invariant.");

  71 }
  72 
  73 void PtrQueue::locking_enqueue_completed_buffer(BufferNode* node) {
  74   assert(_lock->owned_by_self(), "Required.");
  75 
  76   // We have to unlock _lock (which may be Shared_DirtyCardQ_lock) before
  77   // we acquire DirtyCardQ_CBL_mon inside enqueue_complete_buffer as they
  78   // have the same rank and we may get the "possible deadlock" message
  79   _lock->unlock();
  80 
  81   qset()->enqueue_complete_buffer(node);
  82   // We must relock only because the caller will unlock, for the normal
  83   // case.
  84   _lock->lock_without_safepoint_check();
  85 }
  86 
  87 
  88 BufferNode* BufferNode::allocate(size_t byte_size) {
  89   assert(byte_size > 0, "precondition");
  90   assert(is_size_aligned(byte_size, sizeof(void**)),
  91          "Invalid buffer size " SIZE_FORMAT, byte_size);
  92   void* data = NEW_C_HEAP_ARRAY(char, buffer_offset() + byte_size, mtGC);
  93   return new (data) BufferNode;
  94 }
  95 
  96 void BufferNode::deallocate(BufferNode* node) {
  97   node->~BufferNode();
  98   FREE_C_HEAP_ARRAY(char, node);
  99 }
 100 
 101 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :

 102   _max_completed_queue(0),
 103   _cbl_mon(NULL), _fl_lock(NULL),
 104   _notify_when_complete(notify_when_complete),
 105   _sz(0),
 106   _completed_buffers_head(NULL),
 107   _completed_buffers_tail(NULL),
 108   _n_completed_buffers(0),
 109   _process_completed_threshold(0), _process_completed(false),
 110   _buf_free_list(NULL), _buf_free_list_sz(0)
 111 {
 112   _fl_owner = this;
 113 }
 114 
 115 PtrQueueSet::~PtrQueueSet() {
 116   // There are presently only a couple (derived) instances ever
 117   // created, and they are permanent, so no harm currently done by
 118   // doing nothing here.
 119 }
 120 
 121 void PtrQueueSet::initialize(Monitor* cbl_mon,
 122                              Mutex* fl_lock,
 123                              int process_completed_threshold,
 124                              int max_completed_queue,
 125                              PtrQueueSet *fl_owner) {
 126   _max_completed_queue = max_completed_queue;
 127   _process_completed_threshold = process_completed_threshold;
 128   _completed_queue_padding = 0;
 129   assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
 130   _cbl_mon = cbl_mon;
 131   _fl_lock = fl_lock;
 132   _fl_owner = (fl_owner != NULL) ? fl_owner : this;
 133 }
 134 
 135 void** PtrQueueSet::allocate_buffer() {
 136   assert(_sz > 0, "Didn't set a buffer size.");
 137   BufferNode* node = NULL;
 138   {
 139     MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
 140     node = _fl_owner->_buf_free_list;
 141     if (node != NULL) {
 142       _fl_owner->_buf_free_list = node->next();
 143       _fl_owner->_buf_free_list_sz--;
 144     }
 145   }
 146   if (node == NULL) {
 147     node = BufferNode::allocate(_sz);
 148   } else {
 149     // Reinitialize buffer obtained from free list.
 150     node->set_index(0);
 151     node->set_next(NULL);
 152   }
 153   return BufferNode::make_buffer_from_node(node);
 154 }
 155 
 156 void PtrQueueSet::deallocate_buffer(BufferNode* node) {
 157   assert(_sz > 0, "Didn't set a buffer size.");
 158   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
 159   node->set_next(_fl_owner->_buf_free_list);
 160   _fl_owner->_buf_free_list = node;
 161   _fl_owner->_buf_free_list_sz++;
 162 }
 163 
 164 void PtrQueueSet::reduce_free_list() {
 165   assert(_fl_owner == this, "Free list reduction is allowed only for the owner");
 166   // For now we'll adopt the strategy of deleting half.
 167   MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
 168   size_t n = _buf_free_list_sz / 2;
 169   for (size_t i = 0; i < n; ++i) {
 170     assert(_buf_free_list != NULL,
 171            "_buf_free_list_sz is wrong: " SIZE_FORMAT, _buf_free_list_sz);
 172     BufferNode* node = _buf_free_list;
 173     _buf_free_list = node->next();
 174     _buf_free_list_sz--;
 175     BufferNode::deallocate(node);
 176   }
 177 }
 178 
 179 void PtrQueue::handle_zero_index() {
 180   assert(_index == 0, "Precondition.");
 181 
 182   // This thread records the full buffer and allocates a new one (while
 183   // holding the lock if there is one).
 184   if (_buf != NULL) {
 185     if (!should_enqueue_buffer()) {
 186       assert(_index > 0, "the buffer can only be re-used if it's not full");
 187       return;
 188     }
 189 
 190     if (_lock) {
 191       assert(_lock->owned_by_self(), "Required.");
 192 
 193       // The current PtrQ may be the shared dirty card queue and
 194       // may be being manipulated by more than one worker thread
 195       // during a pause. Since the enqueueing of the completed
 196       // buffer unlocks the Shared_DirtyCardQ_lock more than one
 197       // worker thread can 'race' on reading the shared queue attributes
 198       // (_buf and _index) and multiple threads can call into this
 199       // routine for the same buffer. This will cause the completed
 200       // buffer to be added to the CBL multiple times.
 201 
 202       // We "claim" the current buffer by caching value of _buf in
 203       // a local and clearing the field while holding _lock. When
 204       // _lock is released (while enqueueing the completed buffer)
 205       // the thread that acquires _lock will skip this code,
 206       // preventing the subsequent the multiple enqueue, and
 207       // install a newly allocated buffer below.
 208 
 209       BufferNode* node = BufferNode::make_node_from_buffer(_buf, _index);
 210       _buf = NULL;         // clear shared _buf field
 211 
 212       locking_enqueue_completed_buffer(node); // enqueue completed buffer
 213 
 214       // While the current thread was enqueueing the buffer another thread
 215       // may have a allocated a new buffer and inserted it into this pointer
 216       // queue. If that happens then we just return so that the current
 217       // thread doesn't overwrite the buffer allocated by the other thread
 218       // and potentially losing some dirtied cards.
 219 
 220       if (_buf != NULL) return;
 221     } else {
 222       BufferNode* node = BufferNode::make_node_from_buffer(_buf, _index);
 223       if (qset()->process_or_enqueue_complete_buffer(node)) {
 224         // Recycle the buffer. No allocation.
 225         assert(_buf == BufferNode::make_buffer_from_node(node), "invariant");
 226         assert(_sz == qset()->buffer_size(), "invariant");
 227         _index = _sz;
 228         return;
 229       }
 230     }
 231   }
 232   // Reallocate the buffer


 233   _buf = qset()->allocate_buffer();
 234   _sz = qset()->buffer_size();
 235   _index = _sz;
 236 }
 237 
 238 bool PtrQueueSet::process_or_enqueue_complete_buffer(BufferNode* node) {
 239   if (Thread::current()->is_Java_thread()) {
 240     // We don't lock. It is fine to be epsilon-precise here.
 241     if (_max_completed_queue == 0 || _max_completed_queue > 0 &&
 242         _n_completed_buffers >= _max_completed_queue + _completed_queue_padding) {
 243       bool b = mut_process_buffer(node);
 244       if (b) {
 245         // True here means that the buffer hasn't been deallocated and the caller may reuse it.
 246         return true;
 247       }
 248     }
 249   }
 250   // The buffer will be enqueued. The caller will have to get a new one.
 251   enqueue_complete_buffer(node);
 252   return false;
 253 }
 254 
 255 void PtrQueueSet::enqueue_complete_buffer(BufferNode* cbn) {


 279   size_t n = 0;
 280   BufferNode* cbn = _completed_buffers_head;
 281   while (cbn != NULL) {
 282     n++;
 283     cbn = cbn->next();
 284   }
 285   return n;
 286 }
 287 
 288 void PtrQueueSet::assert_completed_buffer_list_len_correct() {
 289   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 290   assert_completed_buffer_list_len_correct_locked();
 291 }
 292 
 293 void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
 294   guarantee(completed_buffers_list_length() ==  _n_completed_buffers,
 295             "Completed buffer length is wrong.");
 296 }
 297 
 298 void PtrQueueSet::set_buffer_size(size_t sz) {
 299   assert(_sz == 0 && sz > 0, "Should be called only once.");
 300   _sz = sz * sizeof(void*);
 301 }
 302 
 303 // Merge lists of buffers. Notify the processing threads.
 304 // The source queue is emptied as a result. The queues
 305 // must share the monitor.
 306 void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
 307   assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
 308   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 309   if (_completed_buffers_tail == NULL) {
 310     assert(_completed_buffers_head == NULL, "Well-formedness");
 311     _completed_buffers_head = src->_completed_buffers_head;
 312     _completed_buffers_tail = src->_completed_buffers_tail;
 313   } else {
 314     assert(_completed_buffers_head != NULL, "Well formedness");
 315     if (src->_completed_buffers_head != NULL) {
 316       _completed_buffers_tail->set_next(src->_completed_buffers_head);
 317       _completed_buffers_tail = src->_completed_buffers_tail;
 318     }
 319   }
 320   _n_completed_buffers += src->_n_completed_buffers;


  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/g1/ptrQueue.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/mutex.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 
  33 #include <new>
  34 
  35 PtrQueue::PtrQueue(PtrQueueSet* qset, bool permanent, bool active) :
  36   _qset(qset),
  37   _active(active),
  38   _permanent(permanent),
  39   _index(0),
  40   _capacity_in_bytes(0),
  41   _buf(NULL),
  42   _lock(NULL)
  43 {}
  44 
  45 PtrQueue::~PtrQueue() {
  46   assert(_permanent || (_buf == NULL), "queue must be flushed before delete");
  47 }
  48 
  49 void PtrQueue::flush_impl() {
  50   if (_buf != NULL) {
  51     BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
  52     if (is_empty()) {
  53       // No work to do.
  54       qset()->deallocate_buffer(node);
  55     } else {
  56       qset()->enqueue_complete_buffer(node);
  57     }
  58     _buf = NULL;
  59     set_index(0);
  60   }
  61 }
  62 
  63 
  64 void PtrQueue::enqueue_known_active(void* ptr) {



  65   while (_index == 0) {
  66     handle_zero_index();
  67   }
  68 
  69   assert(_buf != NULL, "postcondition");
  70   assert(index() > 0, "postcondition");
  71   assert(index() <= capacity(), "invariant");
  72   _index -= _element_size;
  73   _buf[index()] = ptr;
  74 }
  75 
  76 void PtrQueue::locking_enqueue_completed_buffer(BufferNode* node) {
  77   assert(_lock->owned_by_self(), "Required.");
  78 
  79   // We have to unlock _lock (which may be Shared_DirtyCardQ_lock) before
  80   // we acquire DirtyCardQ_CBL_mon inside enqueue_complete_buffer as they
  81   // have the same rank and we may get the "possible deadlock" message
  82   _lock->unlock();
  83 
  84   qset()->enqueue_complete_buffer(node);
  85   // We must relock only because the caller will unlock, for the normal
  86   // case.
  87   _lock->lock_without_safepoint_check();
  88 }
  89 
  90 
  91 BufferNode* BufferNode::allocate(size_t size) {
  92   size_t byte_size = size * sizeof(void*);


  93   void* data = NEW_C_HEAP_ARRAY(char, buffer_offset() + byte_size, mtGC);
  94   return new (data) BufferNode;
  95 }
  96 
  97 void BufferNode::deallocate(BufferNode* node) {
  98   node->~BufferNode();
  99   FREE_C_HEAP_ARRAY(char, node);
 100 }
 101 
 102 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
 103   _buffer_size(0),
 104   _max_completed_queue(0),
 105   _cbl_mon(NULL), _fl_lock(NULL),
 106   _notify_when_complete(notify_when_complete),

 107   _completed_buffers_head(NULL),
 108   _completed_buffers_tail(NULL),
 109   _n_completed_buffers(0),
 110   _process_completed_threshold(0), _process_completed(false),
 111   _buf_free_list(NULL), _buf_free_list_sz(0)
 112 {
 113   _fl_owner = this;
 114 }
 115 
 116 PtrQueueSet::~PtrQueueSet() {
 117   // There are presently only a couple (derived) instances ever
 118   // created, and they are permanent, so no harm currently done by
 119   // doing nothing here.
 120 }
 121 
 122 void PtrQueueSet::initialize(Monitor* cbl_mon,
 123                              Mutex* fl_lock,
 124                              int process_completed_threshold,
 125                              int max_completed_queue,
 126                              PtrQueueSet *fl_owner) {
 127   _max_completed_queue = max_completed_queue;
 128   _process_completed_threshold = process_completed_threshold;
 129   _completed_queue_padding = 0;
 130   assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
 131   _cbl_mon = cbl_mon;
 132   _fl_lock = fl_lock;
 133   _fl_owner = (fl_owner != NULL) ? fl_owner : this;
 134 }
 135 
 136 void** PtrQueueSet::allocate_buffer() {

 137   BufferNode* node = NULL;
 138   {
 139     MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
 140     node = _fl_owner->_buf_free_list;
 141     if (node != NULL) {
 142       _fl_owner->_buf_free_list = node->next();
 143       _fl_owner->_buf_free_list_sz--;
 144     }
 145   }
 146   if (node == NULL) {
 147     node = BufferNode::allocate(buffer_size());
 148   } else {
 149     // Reinitialize buffer obtained from free list.
 150     node->set_index(0);
 151     node->set_next(NULL);
 152   }
 153   return BufferNode::make_buffer_from_node(node);
 154 }
 155 
 156 void PtrQueueSet::deallocate_buffer(BufferNode* node) {

 157   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
 158   node->set_next(_fl_owner->_buf_free_list);
 159   _fl_owner->_buf_free_list = node;
 160   _fl_owner->_buf_free_list_sz++;
 161 }
 162 
 163 void PtrQueueSet::reduce_free_list() {
 164   assert(_fl_owner == this, "Free list reduction is allowed only for the owner");
 165   // For now we'll adopt the strategy of deleting half.
 166   MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
 167   size_t n = _buf_free_list_sz / 2;
 168   for (size_t i = 0; i < n; ++i) {
 169     assert(_buf_free_list != NULL,
 170            "_buf_free_list_sz is wrong: " SIZE_FORMAT, _buf_free_list_sz);
 171     BufferNode* node = _buf_free_list;
 172     _buf_free_list = node->next();
 173     _buf_free_list_sz--;
 174     BufferNode::deallocate(node);
 175   }
 176 }
 177 
 178 void PtrQueue::handle_zero_index() {
 179   assert(index() == 0, "precondition");
 180 
 181   // This thread records the full buffer and allocates a new one (while
 182   // holding the lock if there is one).
 183   if (_buf != NULL) {
 184     if (!should_enqueue_buffer()) {
 185       assert(index() > 0, "the buffer can only be re-used if it's not full");
 186       return;
 187     }
 188 
 189     if (_lock) {
 190       assert(_lock->owned_by_self(), "Required.");
 191 
 192       // The current PtrQ may be the shared dirty card queue and
 193       // may be being manipulated by more than one worker thread
 194       // during a pause. Since the enqueueing of the completed
 195       // buffer unlocks the Shared_DirtyCardQ_lock more than one
 196       // worker thread can 'race' on reading the shared queue attributes
 197       // (_buf and _index) and multiple threads can call into this
 198       // routine for the same buffer. This will cause the completed
 199       // buffer to be added to the CBL multiple times.
 200 
 201       // We "claim" the current buffer by caching value of _buf in
 202       // a local and clearing the field while holding _lock. When
 203       // _lock is released (while enqueueing the completed buffer)
 204       // the thread that acquires _lock will skip this code,
 205       // preventing the subsequent the multiple enqueue, and
 206       // install a newly allocated buffer below.
 207 
 208       BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
 209       _buf = NULL;         // clear shared _buf field
 210 
 211       locking_enqueue_completed_buffer(node); // enqueue completed buffer
 212 
 213       // While the current thread was enqueueing the buffer another thread
 214       // may have a allocated a new buffer and inserted it into this pointer
 215       // queue. If that happens then we just return so that the current
 216       // thread doesn't overwrite the buffer allocated by the other thread
 217       // and potentially losing some dirtied cards.
 218 
 219       if (_buf != NULL) return;
 220     } else {
 221       BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
 222       if (qset()->process_or_enqueue_complete_buffer(node)) {
 223         // Recycle the buffer. No allocation.
 224         assert(_buf == BufferNode::make_buffer_from_node(node), "invariant");
 225         assert(capacity() == qset()->buffer_size(), "invariant");
 226         reset();
 227         return;
 228       }
 229     }
 230   }
 231   // Set capacity in case this is the first allocation.
 232   set_capacity(qset()->buffer_size());
 233   // Allocate a new buffer.
 234   _buf = qset()->allocate_buffer();
 235   reset();

 236 }
 237 
 238 bool PtrQueueSet::process_or_enqueue_complete_buffer(BufferNode* node) {
 239   if (Thread::current()->is_Java_thread()) {
 240     // We don't lock. It is fine to be epsilon-precise here.
 241     if (_max_completed_queue == 0 || _max_completed_queue > 0 &&
 242         _n_completed_buffers >= _max_completed_queue + _completed_queue_padding) {
 243       bool b = mut_process_buffer(node);
 244       if (b) {
 245         // True here means that the buffer hasn't been deallocated and the caller may reuse it.
 246         return true;
 247       }
 248     }
 249   }
 250   // The buffer will be enqueued. The caller will have to get a new one.
 251   enqueue_complete_buffer(node);
 252   return false;
 253 }
 254 
 255 void PtrQueueSet::enqueue_complete_buffer(BufferNode* cbn) {


 279   size_t n = 0;
 280   BufferNode* cbn = _completed_buffers_head;
 281   while (cbn != NULL) {
 282     n++;
 283     cbn = cbn->next();
 284   }
 285   return n;
 286 }
 287 
 288 void PtrQueueSet::assert_completed_buffer_list_len_correct() {
 289   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 290   assert_completed_buffer_list_len_correct_locked();
 291 }
 292 
 293 void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
 294   guarantee(completed_buffers_list_length() ==  _n_completed_buffers,
 295             "Completed buffer length is wrong.");
 296 }
 297 
 298 void PtrQueueSet::set_buffer_size(size_t sz) {
 299   assert(_buffer_size == 0 && sz > 0, "Should be called only once.");
 300   _buffer_size = sz;
 301 }
 302 
 303 // Merge lists of buffers. Notify the processing threads.
 304 // The source queue is emptied as a result. The queues
 305 // must share the monitor.
 306 void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
 307   assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
 308   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 309   if (_completed_buffers_tail == NULL) {
 310     assert(_completed_buffers_head == NULL, "Well-formedness");
 311     _completed_buffers_head = src->_completed_buffers_head;
 312     _completed_buffers_tail = src->_completed_buffers_tail;
 313   } else {
 314     assert(_completed_buffers_head != NULL, "Well formedness");
 315     if (src->_completed_buffers_head != NULL) {
 316       _completed_buffers_tail->set_next(src->_completed_buffers_head);
 317       _completed_buffers_tail = src->_completed_buffers_tail;
 318     }
 319   }
 320   _n_completed_buffers += src->_n_completed_buffers;
< prev index next >