1 /*
   2  * Copyright (c) 2001, 2018, 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/shared/satbMarkQueue.hpp"
  27 #include "gc/shared/collectedHeap.hpp"
  28 #include "logging/log.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/mutexLocker.hpp"
  32 #include "runtime/os.hpp"
  33 #include "runtime/safepoint.hpp"
  34 #include "runtime/thread.hpp"
  35 #include "runtime/threadSMR.hpp"
  36 #include "runtime/vmThread.hpp"
  37 
  38 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) :
  39   // SATB queues are only active during marking cycles. We create
  40   // them with their active field set to false. If a thread is
  41   // created during a cycle and its SATB queue needs to be activated
  42   // before the thread starts running, we'll need to set its active
  43   // field to true. This must be done in the collector-specific
  44   // BarrierSet thread attachment protocol.
  45   PtrQueue(qset, permanent, false /* active */)
  46 { }
  47 
  48 void SATBMarkQueue::flush() {
  49   // Filter now to possibly save work later.  If filtering empties the
  50   // buffer then flush_impl can deallocate the buffer.
  51   filter();
  52   flush_impl();
  53 }
  54 
  55 // This method will first apply filtering to the buffer. If filtering
  56 // retains a small enough collection in the buffer, we can continue to
  57 // use the buffer as-is, instead of enqueueing and replacing it.
  58 
  59 bool SATBMarkQueue::should_enqueue_buffer() {
  60   // This method should only be called if there is a non-NULL buffer
  61   // that is full.
  62   assert(index() == 0, "pre-condition");
  63   assert(_buf != NULL, "pre-condition");
  64 
  65   filter();
  66 
  67   SATBMarkQueueSet* satb_qset = static_cast<SATBMarkQueueSet*>(qset());
  68   size_t threshold = satb_qset->buffer_enqueue_threshold();
  69   // Ensure we'll enqueue completely full buffers.
  70   assert(threshold > 0, "enqueue threshold = 0");
  71   // Ensure we won't enqueue empty buffers.
  72   assert(threshold <= capacity(),
  73          "enqueue threshold " SIZE_FORMAT " exceeds capacity " SIZE_FORMAT,
  74          threshold, capacity());
  75   return index() < threshold;
  76 }
  77 
  78 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) {
  79   assert(SafepointSynchronize::is_at_safepoint(),
  80          "SATB queues must only be processed at safepoints");
  81   if (_buf != NULL) {
  82     cl->do_buffer(&_buf[index()], size());
  83     reset();
  84   }
  85 }
  86 
  87 #ifndef PRODUCT
  88 // Helpful for debugging
  89 
  90 static void print_satb_buffer(const char* name,
  91                               void** buf,
  92                               size_t index,
  93                               size_t capacity) {
  94   tty->print_cr("  SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT
  95                 " capacity: " SIZE_FORMAT,
  96                 name, p2i(buf), index, capacity);
  97 }
  98 
  99 void SATBMarkQueue::print(const char* name) {
 100   print_satb_buffer(name, _buf, index(), capacity());
 101 }
 102 
 103 #endif // PRODUCT
 104 
 105 SATBMarkQueueSet::SATBMarkQueueSet() :
 106   PtrQueueSet(),
 107   _buffer_enqueue_threshold(0)
 108 {}
 109 
 110 void SATBMarkQueueSet::initialize(Monitor* cbl_mon,
 111                                   BufferNode::Allocator* allocator,
 112                                   size_t process_completed_buffers_threshold,
 113                                   uint buffer_enqueue_threshold_percentage) {
 114   PtrQueueSet::initialize(cbl_mon, allocator);
 115   set_process_completed_buffers_threshold(process_completed_buffers_threshold);
 116   assert(buffer_size() != 0, "buffer size not initialized");
 117   // Minimum threshold of 1 ensures enqueuing of completely full buffers.
 118   size_t size = buffer_size();
 119   size_t enqueue_qty = (size * buffer_enqueue_threshold_percentage) / 100;
 120   _buffer_enqueue_threshold = MAX2(size - enqueue_qty, (size_t)1);
 121 }
 122 
 123 #ifdef ASSERT
 124 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
 125   log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE");
 126   log_error(gc, verify)("Actual SATB active states:");
 127   log_error(gc, verify)("  Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
 128 
 129   class DumpThreadStateClosure : public ThreadClosure {
 130     SATBMarkQueueSet* _qset;
 131   public:
 132     DumpThreadStateClosure(SATBMarkQueueSet* qset) : _qset(qset) {}
 133     virtual void do_thread(Thread* t) {
 134       SATBMarkQueue& queue = _qset->satb_queue_for_thread(t);
 135       log_error(gc, verify)("  Thread \"%s\" queue: %s",
 136                             t->name(),
 137                             queue.is_active() ? "ACTIVE" : "INACTIVE");
 138     }
 139   } closure(this);
 140   Threads::threads_do(&closure);
 141 }
 142 
 143 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
 144   // Verify queue set state
 145   if (is_active() != expected_active) {
 146     dump_active_states(expected_active);
 147     fatal("SATB queue set has an unexpected active state");
 148   }
 149 
 150   // Verify thread queue states
 151   class VerifyThreadStatesClosure : public ThreadClosure {
 152     SATBMarkQueueSet* _qset;
 153     bool _expected_active;
 154   public:
 155     VerifyThreadStatesClosure(SATBMarkQueueSet* qset, bool expected_active) :
 156       _qset(qset), _expected_active(expected_active) {}
 157     virtual void do_thread(Thread* t) {
 158       if (_qset->satb_queue_for_thread(t).is_active() != _expected_active) {
 159         _qset->dump_active_states(_expected_active);
 160         fatal("Thread SATB queue has an unexpected active state");
 161       }
 162     }
 163   } closure(this, expected_active);
 164   Threads::threads_do(&closure);
 165 }
 166 #endif // ASSERT
 167 
 168 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
 169   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 170 #ifdef ASSERT
 171   verify_active_states(expected_active);
 172 #endif // ASSERT
 173   _all_active = active;
 174 
 175   class SetThreadActiveClosure : public ThreadClosure {
 176     SATBMarkQueueSet* _qset;
 177     bool _active;
 178   public:
 179     SetThreadActiveClosure(SATBMarkQueueSet* qset, bool active) :
 180       _qset(qset), _active(active) {}
 181     virtual void do_thread(Thread* t) {
 182       _qset->satb_queue_for_thread(t).set_active(_active);
 183     }
 184   } closure(this, active);
 185   Threads::threads_do(&closure);
 186 }
 187 
 188 void SATBMarkQueueSet::filter_thread_buffers() {
 189   class FilterThreadBufferClosure : public ThreadClosure {
 190     SATBMarkQueueSet* _qset;
 191   public:
 192     FilterThreadBufferClosure(SATBMarkQueueSet* qset) : _qset(qset) {}
 193     virtual void do_thread(Thread* t) {
 194       _qset->satb_queue_for_thread(t).filter();
 195     }
 196   } closure(this);
 197   Threads::threads_do(&closure);
 198 }
 199 
 200 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
 201   BufferNode* nd = get_completed_buffer();
 202   if (nd != NULL) {
 203     void **buf = BufferNode::make_buffer_from_node(nd);
 204     size_t index = nd->index();
 205     size_t size = buffer_size();
 206     assert(index <= size, "invariant");
 207     cl->do_buffer(buf + index, size - index);
 208     deallocate_buffer(nd);
 209     return true;
 210   } else {
 211     return false;
 212   }
 213 }
 214 
 215 #ifndef PRODUCT
 216 // Helpful for debugging
 217 
 218 #define SATB_PRINTER_BUFFER_SIZE 256
 219 
 220 void SATBMarkQueueSet::print_all(const char* msg) {
 221   char buffer[SATB_PRINTER_BUFFER_SIZE];
 222   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 223 
 224   tty->cr();
 225   tty->print_cr("SATB BUFFERS [%s]", msg);
 226 
 227   BufferNode* nd = completed_buffers_head();
 228   int i = 0;
 229   while (nd != NULL) {
 230     void** buf = BufferNode::make_buffer_from_node(nd);
 231     os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
 232     print_satb_buffer(buffer, buf, nd->index(), buffer_size());
 233     nd = nd->next();
 234     i += 1;
 235   }
 236 
 237   class PrintThreadClosure : public ThreadClosure {
 238     SATBMarkQueueSet* _qset;
 239     char* _buffer;
 240 
 241   public:
 242     PrintThreadClosure(SATBMarkQueueSet* qset, char* buffer) :
 243       _qset(qset), _buffer(buffer) {}
 244 
 245     virtual void do_thread(Thread* t) {
 246       os::snprintf(_buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
 247       _qset->satb_queue_for_thread(t).print(_buffer);
 248     }
 249   } closure(this, buffer);
 250   Threads::threads_do(&closure);
 251 
 252   tty->cr();
 253 }
 254 #endif // PRODUCT
 255 
 256 void SATBMarkQueueSet::abandon_partial_marking() {
 257   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 258   abandon_completed_buffers();
 259 
 260   class AbandonThreadQueueClosure : public ThreadClosure {
 261     SATBMarkQueueSet* _qset;
 262   public:
 263     AbandonThreadQueueClosure(SATBMarkQueueSet* qset) : _qset(qset) {}
 264     virtual void do_thread(Thread* t) {
 265       _qset->satb_queue_for_thread(t).reset();
 266     }
 267   } closure(this);
 268   Threads::threads_do(&closure);
 269 }