1 /*
   2  * Copyright (c) 2001, 2010, 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/dirtyCardQueue.hpp"
  27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "runtime/safepoint.hpp"
  32 #include "runtime/thread.inline.hpp"
  33 #include "utilities/workgroup.hpp"
  34 
  35 bool DirtyCardQueue::apply_closure(CardTableEntryClosure* cl,
  36                                    bool consume,
  37                                    uint worker_i) {
  38   bool res = true;
  39   if (_buf != NULL) {
  40     res = apply_closure_to_buffer(cl, _buf, _index, _sz,
  41                                   consume,
  42                                   worker_i);
  43     if (res && consume) _index = _sz;
  44   }
  45   return res;
  46 }
  47 
  48 bool DirtyCardQueue::apply_closure_to_buffer(CardTableEntryClosure* cl,
  49                                              void** buf,
  50                                              size_t index, size_t sz,
  51                                              bool consume,
  52                                              uint worker_i) {
  53   if (cl == NULL) return true;
  54   for (size_t i = index; i < sz; i += oopSize) {
  55     int ind = byte_index_to_index((int)i);
  56     jbyte* card_ptr = (jbyte*)buf[ind];
  57     if (card_ptr != NULL) {
  58       // Set the entry to null, so we don't do it again (via the test
  59       // above) if we reconsider this buffer.
  60       if (consume) buf[ind] = NULL;
  61       if (!cl->do_card_ptr(card_ptr, worker_i)) return false;
  62     }
  63   }
  64   return true;
  65 }
  66 
  67 DirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) :
  68   PtrQueueSet(notify_when_complete),
  69   _mut_process_closure(NULL),
  70   _free_ids(NULL),
  71   _processed_buffers_mut(0), _processed_buffers_rs_thread(0)
  72 {
  73   _shared_dirty_card_queue = DirtyCardQueue(this, true /*perm*/);
  74   _all_active = true;
  75 }
  76 
  77 // Determines how many mutator threads can process the buffers in parallel.
  78 uint DirtyCardQueueSet::num_par_ids() {
  79   return (uint)os::processor_count();
  80 }
  81 
  82 void DirtyCardQueueSet::initialize(CardTableEntryClosure* cl, Monitor* cbl_mon, Mutex* fl_lock,
  83                                    int process_completed_threshold,
  84                                    int max_completed_queue,
  85                                    Mutex* lock, PtrQueueSet* fl_owner) {
  86   _mut_process_closure = cl;
  87   PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold,
  88                           max_completed_queue, fl_owner);
  89   set_buffer_size(G1UpdateBufferSize);
  90   _shared_dirty_card_queue.set_lock(lock);
  91   _free_ids = new FreeIdSet((int) num_par_ids(), _cbl_mon);
  92 }
  93 
  94 void DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) {
  95   t->dirty_card_queue().handle_zero_index();
  96 }
  97 
  98 void DirtyCardQueueSet::iterate_closure_all_threads(CardTableEntryClosure* cl,
  99                                                     bool consume,
 100                                                     uint worker_i) {
 101   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 102   for(JavaThread* t = Threads::first(); t; t = t->next()) {
 103     bool b = t->dirty_card_queue().apply_closure(cl, consume);
 104     guarantee(b, "Should not be interrupted.");
 105   }
 106   bool b = shared_dirty_card_queue()->apply_closure(cl,
 107                                                     consume,
 108                                                     worker_i);
 109   guarantee(b, "Should not be interrupted.");
 110 }
 111 
 112 bool DirtyCardQueueSet::mut_process_buffer(void** buf) {
 113 
 114   // Used to determine if we had already claimed a par_id
 115   // before entering this method.
 116   bool already_claimed = false;
 117 
 118   // We grab the current JavaThread.
 119   JavaThread* thread = JavaThread::current();
 120 
 121   // We get the the number of any par_id that this thread
 122   // might have already claimed.
 123   uint worker_i = thread->get_claimed_par_id();
 124 
 125   // If worker_i is not UINT_MAX then the thread has already claimed
 126   // a par_id. We make note of it using the already_claimed value
 127   if (worker_i != UINT_MAX) {
 128     already_claimed = true;
 129   } else {
 130 
 131     // Otherwise we need to claim a par id
 132     worker_i = _free_ids->claim_par_id();
 133 
 134     // And store the par_id value in the thread
 135     thread->set_claimed_par_id(worker_i);
 136   }
 137 
 138   bool b = false;
 139   if (worker_i != UINT_MAX) {
 140     b = DirtyCardQueue::apply_closure_to_buffer(_mut_process_closure, buf, 0,
 141                                                 _sz, true, worker_i);
 142     if (b) Atomic::inc(&_processed_buffers_mut);
 143 
 144     // If we had not claimed an id before entering the method
 145     // then we must release the id.
 146     if (!already_claimed) {
 147 
 148       // we release the id
 149       _free_ids->release_par_id(worker_i);
 150 
 151       // and set the claimed_id in the thread to UINT_MAX
 152       thread->set_claimed_par_id(UINT_MAX);
 153     }
 154   }
 155   return b;
 156 }
 157 
 158 
 159 BufferNode*
 160 DirtyCardQueueSet::get_completed_buffer(int stop_at) {
 161   BufferNode* nd = NULL;
 162   MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 163 
 164   if ((int)_n_completed_buffers <= stop_at) {
 165     _process_completed = false;
 166     return NULL;
 167   }
 168 
 169   if (_completed_buffers_head != NULL) {
 170     nd = _completed_buffers_head;
 171     _completed_buffers_head = nd->next();
 172     if (_completed_buffers_head == NULL)
 173       _completed_buffers_tail = NULL;
 174     _n_completed_buffers--;
 175     assert(_n_completed_buffers >= 0, "Invariant");
 176   }
 177   debug_only(assert_completed_buffer_list_len_correct_locked());
 178   return nd;
 179 }
 180 
 181 bool DirtyCardQueueSet::
 182 apply_closure_to_completed_buffer_helper(CardTableEntryClosure* cl,
 183                                          uint worker_i,
 184                                          BufferNode* nd) {
 185   if (nd != NULL) {
 186     void **buf = BufferNode::make_buffer_from_node(nd);
 187     size_t index = nd->index();
 188     bool b =
 189       DirtyCardQueue::apply_closure_to_buffer(cl, buf,
 190                                               index, _sz,
 191                                               true, worker_i);
 192     if (b) {
 193       deallocate_buffer(buf);
 194       return true;  // In normal case, go on to next buffer.
 195     } else {
 196       enqueue_complete_buffer(buf, index);
 197       return false;
 198     }
 199   } else {
 200     return false;
 201   }
 202 }
 203 
 204 bool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl,
 205                                                           uint worker_i,
 206                                                           int stop_at,
 207                                                           bool during_pause) {
 208   assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause");
 209   BufferNode* nd = get_completed_buffer(stop_at);
 210   bool res = apply_closure_to_completed_buffer_helper(cl, worker_i, nd);
 211   if (res) Atomic::inc(&_processed_buffers_rs_thread);
 212   return res;
 213 }
 214 
 215 void DirtyCardQueueSet::apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) {
 216   BufferNode* nd = _completed_buffers_head;
 217   while (nd != NULL) {
 218     bool b =
 219       DirtyCardQueue::apply_closure_to_buffer(cl,
 220                                               BufferNode::make_buffer_from_node(nd),
 221                                               0, _sz, false);
 222     guarantee(b, "Should not stop early.");
 223     nd = nd->next();
 224   }
 225 }
 226 
 227 void DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) {
 228   BufferNode* nd = _cur_par_buffer_node;
 229   while (nd != NULL) {
 230     BufferNode* next = (BufferNode*)nd->next();
 231     BufferNode* actual = (BufferNode*)Atomic::cmpxchg_ptr((void*)next, (volatile void*)&_cur_par_buffer_node, (void*)nd);
 232     if (actual == nd) {
 233       bool b =
 234         DirtyCardQueue::apply_closure_to_buffer(cl,
 235                                                 BufferNode::make_buffer_from_node(actual),
 236                                                 0, _sz, false);
 237       guarantee(b, "Should not stop early.");
 238       nd = next;
 239     } else {
 240       nd = actual;
 241     }
 242   }
 243 }
 244 
 245 // Deallocates any completed log buffers
 246 void DirtyCardQueueSet::clear() {
 247   BufferNode* buffers_to_delete = NULL;
 248   {
 249     MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 250     while (_completed_buffers_head != NULL) {
 251       BufferNode* nd = _completed_buffers_head;
 252       _completed_buffers_head = nd->next();
 253       nd->set_next(buffers_to_delete);
 254       buffers_to_delete = nd;
 255     }
 256     _n_completed_buffers = 0;
 257     _completed_buffers_tail = NULL;
 258     debug_only(assert_completed_buffer_list_len_correct_locked());
 259   }
 260   while (buffers_to_delete != NULL) {
 261     BufferNode* nd = buffers_to_delete;
 262     buffers_to_delete = nd->next();
 263     deallocate_buffer(BufferNode::make_buffer_from_node(nd));
 264   }
 265 
 266 }
 267 
 268 void DirtyCardQueueSet::abandon_logs() {
 269   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 270   clear();
 271   // Since abandon is done only at safepoints, we can safely manipulate
 272   // these queues.
 273   for (JavaThread* t = Threads::first(); t; t = t->next()) {
 274     t->dirty_card_queue().reset();
 275   }
 276   shared_dirty_card_queue()->reset();
 277 }
 278 
 279 
 280 void DirtyCardQueueSet::concatenate_logs() {
 281   // Iterate over all the threads, if we find a partial log add it to
 282   // the global list of logs.  Temporarily turn off the limit on the number
 283   // of outstanding buffers.
 284   int save_max_completed_queue = _max_completed_queue;
 285   _max_completed_queue = max_jint;
 286   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 287   for (JavaThread* t = Threads::first(); t; t = t->next()) {
 288     DirtyCardQueue& dcq = t->dirty_card_queue();
 289     if (dcq.size() != 0) {
 290       void **buf = t->dirty_card_queue().get_buf();
 291       // We must NULL out the unused entries, then enqueue.
 292       for (size_t i = 0; i < t->dirty_card_queue().get_index(); i += oopSize) {
 293         buf[PtrQueue::byte_index_to_index((int)i)] = NULL;
 294       }
 295       enqueue_complete_buffer(dcq.get_buf(), dcq.get_index());
 296       dcq.reinitialize();
 297     }
 298   }
 299   if (_shared_dirty_card_queue.size() != 0) {
 300     enqueue_complete_buffer(_shared_dirty_card_queue.get_buf(),
 301                             _shared_dirty_card_queue.get_index());
 302     _shared_dirty_card_queue.reinitialize();
 303   }
 304   // Restore the completed buffer queue limit.
 305   _max_completed_queue = save_max_completed_queue;
 306 }