1 /*
   2  * Copyright (c) 2001, 2012, 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_implementation/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 PtrQueue::PtrQueue(PtrQueueSet* qset, bool perm, bool active) :
  34   _qset(qset), _buf(NULL), _index(0), _active(active),
  35   _perm(perm), _lock(NULL)
  36 {}
  37 
  38 void PtrQueue::flush() {
  39   if (!_perm && _buf != NULL) {
  40     if (_index == _sz) {
  41       // No work to do.
  42       qset()->deallocate_buffer(_buf);
  43     } else {
  44       // We must NULL out the unused entries, then enqueue.
  45       for (size_t i = 0; i < _index; i += oopSize) {
  46         _buf[byte_index_to_index((int)i)] = NULL;
  47       }
  48       assert(_buf != NULL, "must be non-null");
  49       qset()->enqueue_complete_buffer(_buf);
  50     }
  51     _buf = NULL;
  52     _index = 0;
  53   }
  54 }
  55 
  56 
  57 void PtrQueue::enqueue_known_active(void* ptr) {
  58   assert(0 <= _index && _index <= _sz, "Invariant.");
  59   assert(_index == 0 || _buf != NULL, "invariant");
  60 
  61   while (_index == 0) {
  62     handle_zero_index();
  63   }
  64 
  65   assert(_index > 0, "postcondition");
  66   _index -= oopSize;
  67   _buf[byte_index_to_index((int)_index)] = ptr;
  68   assert(0 <= _index && _index <= _sz, "Invariant.");
  69 }
  70 
  71 void PtrQueue::locking_enqueue_completed_buffer(void** buf) {
  72   assert(_lock->owned_by_self(), "Required.");
  73 
  74   // We have to unlock _lock (which may be Shared_DirtyCardQ_lock) before
  75   // we acquire DirtyCardQ_CBL_mon inside enqeue_complete_buffer as they
  76   // have the same rank and we may get the "possible deadlock" message
  77   _lock->unlock();
  78 
  79   qset()->enqueue_complete_buffer(buf);
  80   // We must relock only because the caller will unlock, for the normal
  81   // case.
  82   _lock->lock_without_safepoint_check();
  83 }
  84 
  85 
  86 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
  87   _max_completed_queue(0),
  88   _cbl_mon(NULL), _fl_lock(NULL),
  89   _notify_when_complete(notify_when_complete),
  90   _sz(0),
  91   _completed_buffers_head(NULL),
  92   _completed_buffers_tail(NULL),
  93   _n_completed_buffers(0),
  94   _process_completed_threshold(0), _process_completed(false),
  95   _buf_free_list(NULL), _buf_free_list_sz(0)
  96 {
  97   _fl_owner = this;
  98 }
  99 
 100 void** PtrQueueSet::allocate_buffer() {
 101   assert(_sz > 0, "Didn't set a buffer size.");
 102   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
 103   if (_fl_owner->_buf_free_list != NULL) {
 104     void** res = BufferNode::make_buffer_from_node(_fl_owner->_buf_free_list);
 105     _fl_owner->_buf_free_list = _fl_owner->_buf_free_list->next();
 106     _fl_owner->_buf_free_list_sz--;
 107     return res;
 108   } else {
 109     // Allocate space for the BufferNode in front of the buffer.
 110     char *b =  NEW_C_HEAP_ARRAY(char, _sz + BufferNode::aligned_size(), mtGC);
 111     return BufferNode::make_buffer_from_block(b);
 112   }
 113 }
 114 
 115 void PtrQueueSet::deallocate_buffer(void** buf) {
 116   assert(_sz > 0, "Didn't set a buffer size.");
 117   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
 118   BufferNode *node = BufferNode::make_node_from_buffer(buf);
 119   node->set_next(_fl_owner->_buf_free_list);
 120   _fl_owner->_buf_free_list = node;
 121   _fl_owner->_buf_free_list_sz++;
 122 }
 123 
 124 void PtrQueueSet::reduce_free_list() {
 125   assert(_fl_owner == this, "Free list reduction is allowed only for the owner");
 126   // For now we'll adopt the strategy of deleting half.
 127   MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
 128   size_t n = _buf_free_list_sz / 2;
 129   while (n > 0) {
 130     assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong.");
 131     void* b = BufferNode::make_block_from_node(_buf_free_list);
 132     _buf_free_list = _buf_free_list->next();
 133     FREE_C_HEAP_ARRAY(char, b, mtGC);
 134     _buf_free_list_sz --;
 135     n--;
 136   }
 137 }
 138 
 139 void PtrQueue::handle_zero_index() {
 140   assert(_index == 0, "Precondition.");
 141 
 142   // This thread records the full buffer and allocates a new one (while
 143   // holding the lock if there is one).
 144   if (_buf != NULL) {
 145     if (!should_enqueue_buffer()) {
 146       assert(_index > 0, "the buffer can only be re-used if it's not full");
 147       return;
 148     }
 149 
 150     if (_lock) {
 151       assert(_lock->owned_by_self(), "Required.");
 152 
 153       // The current PtrQ may be the shared dirty card queue and
 154       // may be being manipulated by more than one worker thread
 155       // during a pause. Since the enqueuing of the completed
 156       // buffer unlocks the Shared_DirtyCardQ_lock more than one
 157       // worker thread can 'race' on reading the shared queue attributes
 158       // (_buf and _index) and multiple threads can call into this
 159       // routine for the same buffer. This will cause the completed
 160       // buffer to be added to the CBL multiple times.
 161 
 162       // We "claim" the current buffer by caching value of _buf in
 163       // a local and clearing the field while holding _lock. When
 164       // _lock is released (while enqueueing the completed buffer)
 165       // the thread that acquires _lock will skip this code,
 166       // preventing the subsequent the multiple enqueue, and
 167       // install a newly allocated buffer below.
 168 
 169       void** buf = _buf;   // local pointer to completed buffer
 170       _buf = NULL;         // clear shared _buf field
 171 
 172       locking_enqueue_completed_buffer(buf);  // enqueue completed buffer
 173 
 174       // While the current thread was enqueuing the buffer another thread
 175       // may have a allocated a new buffer and inserted it into this pointer
 176       // queue. If that happens then we just return so that the current
 177       // thread doesn't overwrite the buffer allocated by the other thread
 178       // and potentially losing some dirtied cards.
 179 
 180       if (_buf != NULL) return;
 181     } else {
 182       if (qset()->process_or_enqueue_complete_buffer(_buf)) {
 183         // Recycle the buffer. No allocation.
 184         _sz = qset()->buffer_size();
 185         _index = _sz;
 186         return;
 187       }
 188     }
 189   }
 190   // Reallocate the buffer
 191   _buf = qset()->allocate_buffer();
 192   _sz = qset()->buffer_size();
 193   _index = _sz;
 194   assert(0 <= _index && _index <= _sz, "Invariant.");
 195 }
 196 
 197 bool PtrQueueSet::process_or_enqueue_complete_buffer(void** buf) {
 198   if (Thread::current()->is_Java_thread()) {
 199     // We don't lock. It is fine to be epsilon-precise here.
 200     if (_max_completed_queue == 0 || _max_completed_queue > 0 &&
 201         _n_completed_buffers >= _max_completed_queue + _completed_queue_padding) {
 202       bool b = mut_process_buffer(buf);
 203       if (b) {
 204         // True here means that the buffer hasn't been deallocated and the caller may reuse it.
 205         return true;
 206       }
 207     }
 208   }
 209   // The buffer will be enqueued. The caller will have to get a new one.
 210   enqueue_complete_buffer(buf);
 211   return false;
 212 }
 213 
 214 void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index) {
 215   assert(buf != NULL, "must be non-null");
 216   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 217   BufferNode* cbn = BufferNode::new_from_buffer(buf);
 218   cbn->set_index(index);
 219   if (_completed_buffers_tail == NULL) {
 220     assert(_completed_buffers_head == NULL, "Well-formedness");
 221     _completed_buffers_head = cbn;
 222     _completed_buffers_tail = cbn;
 223   } else {
 224     _completed_buffers_tail->set_next(cbn);
 225     _completed_buffers_tail = cbn;
 226   }
 227   _n_completed_buffers++;
 228 
 229   if (!_process_completed && _process_completed_threshold >= 0 &&
 230       _n_completed_buffers >= _process_completed_threshold) {
 231     _process_completed = true;
 232     if (_notify_when_complete)
 233       _cbl_mon->notify();
 234   }
 235   debug_only(assert_completed_buffer_list_len_correct_locked());
 236 }
 237 
 238 int PtrQueueSet::completed_buffers_list_length() {
 239   int n = 0;
 240   BufferNode* cbn = _completed_buffers_head;
 241   while (cbn != NULL) {
 242     n++;
 243     cbn = cbn->next();
 244   }
 245   return n;
 246 }
 247 
 248 void PtrQueueSet::assert_completed_buffer_list_len_correct() {
 249   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 250   assert_completed_buffer_list_len_correct_locked();
 251 }
 252 
 253 void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
 254   guarantee(completed_buffers_list_length() ==  _n_completed_buffers,
 255             "Completed buffer length is wrong.");
 256 }
 257 
 258 void PtrQueueSet::set_buffer_size(size_t sz) {
 259   assert(_sz == 0 && sz > 0, "Should be called only once.");
 260   _sz = sz * oopSize;
 261 }
 262 
 263 // Merge lists of buffers. Notify the processing threads.
 264 // The source queue is emptied as a result. The queues
 265 // must share the monitor.
 266 void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
 267   assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
 268   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 269   if (_completed_buffers_tail == NULL) {
 270     assert(_completed_buffers_head == NULL, "Well-formedness");
 271     _completed_buffers_head = src->_completed_buffers_head;
 272     _completed_buffers_tail = src->_completed_buffers_tail;
 273   } else {
 274     assert(_completed_buffers_head != NULL, "Well formedness");
 275     if (src->_completed_buffers_head != NULL) {
 276       _completed_buffers_tail->set_next(src->_completed_buffers_head);
 277       _completed_buffers_tail = src->_completed_buffers_tail;
 278     }
 279   }
 280   _n_completed_buffers += src->_n_completed_buffers;
 281 
 282   src->_n_completed_buffers = 0;
 283   src->_completed_buffers_head = NULL;
 284   src->_completed_buffers_tail = NULL;
 285 
 286   assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL ||
 287          _completed_buffers_head != NULL && _completed_buffers_tail != NULL,
 288          "Sanity");
 289 }
 290 
 291 void PtrQueueSet::notify_if_necessary() {
 292   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 293   if (_n_completed_buffers >= _process_completed_threshold || _max_completed_queue == 0) {
 294     _process_completed = true;
 295     if (_notify_when_complete)
 296       _cbl_mon->notify();
 297   }
 298 }