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