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

Fri, 06 Nov 2009 11:10:05 -0800

author
johnc
date
Fri, 06 Nov 2009 11:10:05 -0800
changeset 1519
5f932a151fd4
parent 1279
bd02caa94611
child 1546
44f61c24ddab
permissions
-rw-r--r--

6895788: G1: SATB and update buffer allocation code allocates too much space
Summary: The type in the NEW_C_HEAP_ARRRY and FREE_C_HEAP_ARRAY calls in the buffer allocation code was changed from void* to char as the size argument had already been mulitipled by the byte size of an object pointer.
Reviewed-by: ysr, tonyp

     1 /*
     2  * Copyright 2001-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_ptrQueue.cpp.incl"
    28 PtrQueue::PtrQueue(PtrQueueSet* qset_, bool perm) :
    29   _qset(qset_), _buf(NULL), _index(0), _active(false),
    30   _perm(perm), _lock(NULL)
    31 {}
    33 void PtrQueue::flush() {
    34   if (!_perm && _buf != NULL) {
    35     if (_index == _sz) {
    36       // No work to do.
    37       qset()->deallocate_buffer(_buf);
    38     } else {
    39       // We must NULL out the unused entries, then enqueue.
    40       for (size_t i = 0; i < _index; i += oopSize) {
    41         _buf[byte_index_to_index((int)i)] = NULL;
    42       }
    43       qset()->enqueue_complete_buffer(_buf);
    44     }
    45     _buf = NULL;
    46     _index = 0;
    47   }
    48 }
    51 static int byte_index_to_index(int ind) {
    52   assert((ind % oopSize) == 0, "Invariant.");
    53   return ind / oopSize;
    54 }
    56 static int index_to_byte_index(int byte_ind) {
    57   return byte_ind * oopSize;
    58 }
    60 void PtrQueue::enqueue_known_active(void* ptr) {
    61   assert(0 <= _index && _index <= _sz, "Invariant.");
    62   assert(_index == 0 || _buf != NULL, "invariant");
    64   while (_index == 0) {
    65     handle_zero_index();
    66   }
    67   assert(_index > 0, "postcondition");
    69   _index -= oopSize;
    70   _buf[byte_index_to_index((int)_index)] = ptr;
    71   assert(0 <= _index && _index <= _sz, "Invariant.");
    72 }
    74 void PtrQueue::locking_enqueue_completed_buffer(void** buf) {
    75   assert(_lock->owned_by_self(), "Required.");
    76   _lock->unlock();
    77   qset()->enqueue_complete_buffer(buf);
    78   // We must relock only because the caller will unlock, for the normal
    79   // case.
    80   _lock->lock_without_safepoint_check();
    81 }
    84 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
    85   _max_completed_queue(0),
    86   _cbl_mon(NULL), _fl_lock(NULL),
    87   _notify_when_complete(notify_when_complete),
    88   _sz(0),
    89   _completed_buffers_head(NULL),
    90   _completed_buffers_tail(NULL),
    91   _n_completed_buffers(0),
    92   _process_completed_threshold(0), _process_completed(false),
    93   _buf_free_list(NULL), _buf_free_list_sz(0)
    94 {
    95   _fl_owner = this;
    96 }
    98 void** PtrQueueSet::allocate_buffer() {
    99   assert(_sz > 0, "Didn't set a buffer size.");
   100   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
   101   if (_fl_owner->_buf_free_list != NULL) {
   102     void** res = _fl_owner->_buf_free_list;
   103     _fl_owner->_buf_free_list = (void**)_fl_owner->_buf_free_list[0];
   104     _fl_owner->_buf_free_list_sz--;
   105     // Just override the next pointer with NULL, just in case we scan this part
   106     // of the buffer.
   107     res[0] = NULL;
   108     return res;
   109   } else {
   110     return (void**) NEW_C_HEAP_ARRAY(char, _sz);
   111   }
   112 }
   114 void PtrQueueSet::deallocate_buffer(void** buf) {
   115   assert(_sz > 0, "Didn't set a buffer size.");
   116   MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
   117   buf[0] = (void*)_fl_owner->_buf_free_list;
   118   _fl_owner->_buf_free_list = buf;
   119   _fl_owner->_buf_free_list_sz++;
   120 }
   122 void PtrQueueSet::reduce_free_list() {
   123   // For now we'll adopt the strategy of deleting half.
   124   MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
   125   size_t n = _buf_free_list_sz / 2;
   126   while (n > 0) {
   127     assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong.");
   128     void** head = _buf_free_list;
   129     _buf_free_list = (void**)_buf_free_list[0];
   130     FREE_C_HEAP_ARRAY(char, head);
   131     _buf_free_list_sz --;
   132     n--;
   133   }
   134 }
   136 void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index, bool ignore_max_completed) {
   137   // I use explicit locking here because there's a bailout in the middle.
   138   _cbl_mon->lock_without_safepoint_check();
   140   Thread* thread = Thread::current();
   141   assert( ignore_max_completed ||
   142           thread->is_Java_thread() ||
   143           SafepointSynchronize::is_at_safepoint(),
   144           "invariant" );
   145   ignore_max_completed = ignore_max_completed || !thread->is_Java_thread();
   147   if (!ignore_max_completed && _max_completed_queue > 0 &&
   148       _n_completed_buffers >= (size_t) _max_completed_queue) {
   149     _cbl_mon->unlock();
   150     bool b = mut_process_buffer(buf);
   151     if (b) {
   152       deallocate_buffer(buf);
   153       return;
   154     }
   156     // Otherwise, go ahead and enqueue the buffer.  Must reaquire the lock.
   157     _cbl_mon->lock_without_safepoint_check();
   158   }
   160   // Here we still hold the _cbl_mon.
   161   CompletedBufferNode* cbn = new CompletedBufferNode;
   162   cbn->buf = buf;
   163   cbn->next = NULL;
   164   cbn->index = index;
   165   if (_completed_buffers_tail == NULL) {
   166     assert(_completed_buffers_head == NULL, "Well-formedness");
   167     _completed_buffers_head = cbn;
   168     _completed_buffers_tail = cbn;
   169   } else {
   170     _completed_buffers_tail->next = cbn;
   171     _completed_buffers_tail = cbn;
   172   }
   173   _n_completed_buffers++;
   175   if (!_process_completed &&
   176       _n_completed_buffers >= _process_completed_threshold) {
   177     _process_completed = true;
   178     if (_notify_when_complete)
   179       _cbl_mon->notify_all();
   180   }
   181   debug_only(assert_completed_buffer_list_len_correct_locked());
   182   _cbl_mon->unlock();
   183 }
   185 int PtrQueueSet::completed_buffers_list_length() {
   186   int n = 0;
   187   CompletedBufferNode* cbn = _completed_buffers_head;
   188   while (cbn != NULL) {
   189     n++;
   190     cbn = cbn->next;
   191   }
   192   return n;
   193 }
   195 void PtrQueueSet::assert_completed_buffer_list_len_correct() {
   196   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
   197   assert_completed_buffer_list_len_correct_locked();
   198 }
   200 void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
   201   guarantee((size_t)completed_buffers_list_length() ==  _n_completed_buffers,
   202             "Completed buffer length is wrong.");
   203 }
   205 void PtrQueueSet::set_buffer_size(size_t sz) {
   206   assert(_sz == 0 && sz > 0, "Should be called only once.");
   207   _sz = sz * oopSize;
   208 }
   210 void PtrQueueSet::set_process_completed_threshold(size_t sz) {
   211   _process_completed_threshold = sz;
   212 }
   214 // Merge lists of buffers. Notify waiting threads if the length of the list
   215 // exceeds threshold. The source queue is emptied as a result. The queues
   216 // must share the monitor.
   217 void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
   218   assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
   219   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
   220   if (_completed_buffers_tail == NULL) {
   221     assert(_completed_buffers_head == NULL, "Well-formedness");
   222     _completed_buffers_head = src->_completed_buffers_head;
   223     _completed_buffers_tail = src->_completed_buffers_tail;
   224   } else {
   225     assert(_completed_buffers_head != NULL, "Well formedness");
   226     if (src->_completed_buffers_head != NULL) {
   227       _completed_buffers_tail->next = src->_completed_buffers_head;
   228       _completed_buffers_tail = src->_completed_buffers_tail;
   229     }
   230   }
   231   _n_completed_buffers += src->_n_completed_buffers;
   233   src->_n_completed_buffers = 0;
   234   src->_completed_buffers_head = NULL;
   235   src->_completed_buffers_tail = NULL;
   237   assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL ||
   238          _completed_buffers_head != NULL && _completed_buffers_tail != NULL,
   239          "Sanity");
   241   if (!_process_completed &&
   242       _n_completed_buffers >= _process_completed_threshold) {
   243     _process_completed = true;
   244     if (_notify_when_complete)
   245       _cbl_mon->notify_all();
   246   }
   247 }
   249 // Merge free lists of the two queues. The free list of the source
   250 // queue is emptied as a result. The queues must share the same
   251 // mutex that guards free lists.
   252 void PtrQueueSet::merge_freelists(PtrQueueSet* src) {
   253   assert(_fl_lock == src->_fl_lock, "Should share the same lock");
   254   MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
   255   if (_buf_free_list != NULL) {
   256     void **p = _buf_free_list;
   257     while (*p != NULL) {
   258       p = (void**)*p;
   259     }
   260     *p = src->_buf_free_list;
   261   } else {
   262     _buf_free_list = src->_buf_free_list;
   263   }
   264   _buf_free_list_sz += src->_buf_free_list_sz;
   265   src->_buf_free_list = NULL;
   266   src->_buf_free_list_sz = 0;
   267 }

mercurial