1 /*
   2  * Copyright (c) 2001, 2019, 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/g1/dirtyCardQueue.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1FreeIdSet.hpp"
  29 #include "gc/g1/g1RemSet.hpp"
  30 #include "gc/g1/g1ThreadLocalData.hpp"
  31 #include "gc/g1/heapRegionRemSet.hpp"
  32 #include "gc/shared/suspendibleThreadSet.hpp"
  33 #include "gc/shared/workgroup.hpp"
  34 #include "runtime/atomic.hpp"
  35 #include "runtime/flags/flagSetting.hpp"
  36 #include "runtime/mutexLocker.hpp"
  37 #include "runtime/safepoint.hpp"
  38 #include "runtime/thread.inline.hpp"
  39 #include "runtime/threadSMR.hpp"
  40 
  41 // Closure used for updating remembered sets and recording references that
  42 // point into the collection set while the mutator is running.
  43 // Assumed to be only executed concurrently with the mutator. Yields via
  44 // SuspendibleThreadSet after every card.
  45 class G1RefineCardConcurrentlyClosure: public CardTableEntryClosure {
  46 public:
  47   bool do_card_ptr(jbyte* card_ptr, uint worker_i) {
  48     G1CollectedHeap::heap()->g1_rem_set()->refine_card_concurrently(card_ptr, worker_i);
  49 
  50     if (SuspendibleThreadSet::should_yield()) {
  51       // Caller will actually yield.
  52       return false;
  53     }
  54     // Otherwise, we finished successfully; return true.
  55     return true;
  56   }
  57 };
  58 
  59 DirtyCardQueue::DirtyCardQueue(DirtyCardQueueSet* qset, bool permanent) :
  60   // Dirty card queues are always active, so we create them with their
  61   // active field set to true.
  62   PtrQueue(qset, permanent, true /* active */)
  63 { }
  64 
  65 DirtyCardQueue::~DirtyCardQueue() {
  66   if (!is_permanent()) {
  67     flush();
  68   }
  69 }
  70 
  71 DirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) :
  72   PtrQueueSet(notify_when_complete),
  73   _shared_dirty_card_queue(this, true /* permanent */),
  74   _free_ids(NULL),
  75   _processed_buffers_mut(0),
  76   _processed_buffers_rs_thread(0),
  77   _cur_par_buffer_node(NULL)
  78 {
  79   _all_active = true;
  80 }
  81 
  82 // Determines how many mutator threads can process the buffers in parallel.
  83 uint DirtyCardQueueSet::num_par_ids() {
  84   return (uint)os::initial_active_processor_count();
  85 }
  86 
  87 void DirtyCardQueueSet::initialize(Monitor* cbl_mon,
  88                                    BufferNode::Allocator* allocator,
  89                                    Mutex* lock,
  90                                    bool init_free_ids) {
  91   PtrQueueSet::initialize(cbl_mon, allocator);
  92   _shared_dirty_card_queue.set_lock(lock);
  93   if (init_free_ids) {
  94     _free_ids = new G1FreeIdSet(0, num_par_ids());
  95   }
  96 }
  97 
  98 void DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) {
  99   G1ThreadLocalData::dirty_card_queue(t).handle_zero_index();
 100 }
 101 
 102 bool DirtyCardQueueSet::apply_closure_to_buffer(CardTableEntryClosure* cl,
 103                                                 BufferNode* node,
 104                                                 bool consume,
 105                                                 uint worker_i) {
 106   if (cl == NULL) return true;
 107   bool result = true;
 108   void** buf = BufferNode::make_buffer_from_node(node);
 109   size_t i = node->index();
 110   size_t limit = buffer_size();
 111   for ( ; i < limit; ++i) {
 112     jbyte* card_ptr = static_cast<jbyte*>(buf[i]);
 113     assert(card_ptr != NULL, "invariant");
 114     if (!cl->do_card_ptr(card_ptr, worker_i)) {
 115       result = false;           // Incomplete processing.
 116       break;
 117     }
 118   }
 119   if (consume) {
 120     assert(i <= buffer_size(), "invariant");
 121     node->set_index(i);
 122   }
 123   return result;
 124 }
 125 
 126 #ifndef ASSERT
 127 #define assert_fully_consumed(node, buffer_size)
 128 #else
 129 #define assert_fully_consumed(node, buffer_size)                \
 130   do {                                                          \
 131     size_t _afc_index = (node)->index();                        \
 132     size_t _afc_size = (buffer_size);                           \
 133     assert(_afc_index == _afc_size,                             \
 134            "Buffer was not fully consumed as claimed: index: "  \
 135            SIZE_FORMAT ", size: " SIZE_FORMAT,                  \
 136             _afc_index, _afc_size);                             \
 137   } while (0)
 138 #endif // ASSERT
 139 
 140 bool DirtyCardQueueSet::mut_process_buffer(BufferNode* node) {
 141   guarantee(_free_ids != NULL, "must be");
 142 
 143   uint worker_i = _free_ids->claim_par_id(); // temporarily claim an id
 144   G1RefineCardConcurrentlyClosure cl;
 145   bool result = apply_closure_to_buffer(&cl, node, true, worker_i);
 146   _free_ids->release_par_id(worker_i); // release the id
 147 
 148   if (result) {
 149     assert_fully_consumed(node, buffer_size());
 150     Atomic::inc(&_processed_buffers_mut);
 151   }
 152   return result;
 153 }
 154 
 155 bool DirtyCardQueueSet::refine_completed_buffer_concurrently(uint worker_i, size_t stop_at) {
 156   G1RefineCardConcurrentlyClosure cl;
 157   return apply_closure_to_completed_buffer(&cl, worker_i, stop_at, false);
 158 }
 159 
 160 bool DirtyCardQueueSet::apply_closure_during_gc(CardTableEntryClosure* cl, uint worker_i) {
 161   assert_at_safepoint();
 162   return apply_closure_to_completed_buffer(cl, worker_i, 0, true);
 163 }
 164 
 165 bool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl,
 166                                                           uint worker_i,
 167                                                           size_t stop_at,
 168                                                           bool during_pause) {
 169   assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause");
 170   BufferNode* nd = get_completed_buffer(stop_at);
 171   if (nd == NULL) {
 172     return false;
 173   } else {
 174     if (apply_closure_to_buffer(cl, nd, true, worker_i)) {
 175       assert_fully_consumed(nd, buffer_size());
 176       // Done with fully processed buffer.
 177       deallocate_buffer(nd);
 178       Atomic::inc(&_processed_buffers_rs_thread);
 179     } else {
 180       // Return partially processed buffer to the queue.
 181       guarantee(!during_pause, "Should never stop early");
 182       enqueue_completed_buffer(nd);
 183     }
 184     return true;
 185   }
 186 }
 187 
 188 void DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) {
 189   BufferNode* nd = _cur_par_buffer_node;
 190   while (nd != NULL) {
 191     BufferNode* next = nd->next();
 192     BufferNode* actual = Atomic::cmpxchg(next, &_cur_par_buffer_node, nd);
 193     if (actual == nd) {
 194       bool b = apply_closure_to_buffer(cl, nd, false);
 195       guarantee(b, "Should not stop early.");
 196       nd = next;
 197     } else {
 198       nd = actual;
 199     }
 200   }
 201 }
 202 
 203 void DirtyCardQueueSet::abandon_logs() {
 204   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 205   abandon_completed_buffers();
 206   // Since abandon is done only at safepoints, we can safely manipulate
 207   // these queues.
 208   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 209     G1ThreadLocalData::dirty_card_queue(t).reset();
 210   }
 211   shared_dirty_card_queue()->reset();
 212 }
 213 
 214 void DirtyCardQueueSet::concatenate_log(DirtyCardQueue& dcq) {
 215   if (!dcq.is_empty()) {
 216     dcq.flush();
 217   }
 218 }
 219 
 220 void DirtyCardQueueSet::concatenate_logs() {
 221   // Iterate over all the threads, if we find a partial log add it to
 222   // the global list of logs.  Temporarily turn off the limit on the number
 223   // of outstanding buffers.
 224   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 225   size_t old_limit = max_completed_buffers();
 226   set_max_completed_buffers(MaxCompletedBuffersUnlimited);
 227   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 228     concatenate_log(G1ThreadLocalData::dirty_card_queue(t));
 229   }
 230   concatenate_log(_shared_dirty_card_queue);
 231   set_max_completed_buffers(old_limit);
 232 }