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 "jvm.h"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/satbMarkQueue.hpp"
  29 #include "gc/shared/collectedHeap.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/mutexLocker.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 is done in G1SBarrierSet::on_thread_attach().
  44   PtrQueue(qset, permanent, false /* active */)
  45 { }
  46 
  47 void SATBMarkQueue::flush() {
  48   // Filter now to possibly save work later.  If filtering empties the
  49   // buffer then flush_impl can deallocate the buffer.
  50   filter();
  51   flush_impl();
  52 }
  53 
  54 // This method will first apply filtering to the buffer. If filtering
  55 // retains a small enough collection in the buffer, we can continue to
  56 // use the buffer as-is, instead of enqueueing and replacing it.
  57 
  58 bool SATBMarkQueue::should_enqueue_buffer() {
  59   assert(_lock == NULL || _lock->owned_by_self(),
  60          "we should have taken the lock before calling this");
  61 
  62   // If G1SATBBufferEnqueueingThresholdPercent == 0 we could skip filtering.
  63 
  64   // This method should only be called if there is a non-NULL buffer
  65   // that is full.
  66   assert(index() == 0, "pre-condition");
  67   assert(_buf != NULL, "pre-condition");
  68 
  69   filter();
  70 
  71   size_t cap = capacity();
  72   size_t percent_used = ((cap - index()) * 100) / cap;
  73   bool should_enqueue = percent_used > G1SATBBufferEnqueueingThresholdPercent;
  74   return should_enqueue;
  75 }
  76 
  77 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) {
  78   assert(SafepointSynchronize::is_at_safepoint(),
  79          "SATB queues must only be processed at safepoints");
  80   if (_buf != NULL) {
  81     cl->do_buffer(&_buf[index()], size());
  82     reset();
  83   }
  84 }
  85 
  86 #ifndef PRODUCT
  87 // Helpful for debugging
  88 
  89 static void print_satb_buffer(const char* name,
  90                               void** buf,
  91                               size_t index,
  92                               size_t capacity) {
  93   tty->print_cr("  SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT
  94                 " capacity: " SIZE_FORMAT,
  95                 name, p2i(buf), index, capacity);
  96 }
  97 
  98 void SATBMarkQueue::print(const char* name) {
  99   print_satb_buffer(name, _buf, index(), capacity());
 100 }
 101 
 102 #endif // PRODUCT
 103 
 104 SATBMarkQueueSet::SATBMarkQueueSet() :
 105   PtrQueueSet(),
 106   _shared_satb_queue(this, true /* permanent */),
 107   _filter(NULL)
 108 {}
 109 
 110 void SATBMarkQueueSet::initialize(SATBMarkQueueFilter* filter,
 111                                   Monitor* cbl_mon, Mutex* fl_lock,
 112                                   int process_completed_threshold,
 113                                   Mutex* lock) {
 114   PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
 115   _shared_satb_queue.set_lock(lock);
 116   _filter = filter;
 117 }
 118 
 119 #ifdef ASSERT
 120 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
 121   log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE");
 122   log_error(gc, verify)("Actual SATB active states:");
 123   log_error(gc, verify)("  Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
 124   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 125     log_error(gc, verify)("  Thread \"%s\" queue: %s", t->name(), satb_queue_for_thread(t).is_active() ? "ACTIVE" : "INACTIVE");
 126   }
 127   log_error(gc, verify)("  Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE");
 128 }
 129 
 130 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
 131   // Verify queue set state
 132   if (is_active() != expected_active) {
 133     dump_active_states(expected_active);
 134     guarantee(false, "SATB queue set has an unexpected active state");
 135   }
 136 
 137   // Verify thread queue states
 138   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 139     if (satb_queue_for_thread(t).is_active() != expected_active) {
 140       dump_active_states(expected_active);
 141       guarantee(false, "Thread SATB queue has an unexpected active state");
 142     }
 143   }
 144 
 145   // Verify shared queue state
 146   if (shared_satb_queue()->is_active() != expected_active) {
 147     dump_active_states(expected_active);
 148     guarantee(false, "Shared SATB queue has an unexpected active state");
 149   }
 150 }
 151 #endif // ASSERT
 152 
 153 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
 154   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 155 #ifdef ASSERT
 156   verify_active_states(expected_active);
 157 #endif // ASSERT
 158   _all_active = active;
 159   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 160     satb_queue_for_thread(t).set_active(active);
 161   }
 162   shared_satb_queue()->set_active(active);
 163 }
 164 
 165 void SATBMarkQueueSet::filter_thread_buffers() {
 166   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 167     satb_queue_for_thread(t).filter();
 168   }
 169   shared_satb_queue()->filter();
 170 }
 171 
 172 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
 173   BufferNode* nd = NULL;
 174   {
 175     MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 176     if (_completed_buffers_head != NULL) {
 177       nd = _completed_buffers_head;
 178       _completed_buffers_head = nd->next();
 179       if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
 180       _n_completed_buffers--;
 181       if (_n_completed_buffers == 0) _process_completed = false;
 182     }
 183   }
 184   if (nd != NULL) {
 185     void **buf = BufferNode::make_buffer_from_node(nd);
 186     size_t index = nd->index();
 187     size_t size = buffer_size();
 188     assert(index <= size, "invariant");
 189     cl->do_buffer(buf + index, size - index);
 190     deallocate_buffer(nd);
 191     return true;
 192   } else {
 193     return false;
 194   }
 195 }
 196 
 197 #ifndef PRODUCT
 198 // Helpful for debugging
 199 
 200 #define SATB_PRINTER_BUFFER_SIZE 256
 201 
 202 void SATBMarkQueueSet::print_all(const char* msg) {
 203   char buffer[SATB_PRINTER_BUFFER_SIZE];
 204   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 205 
 206   tty->cr();
 207   tty->print_cr("SATB BUFFERS [%s]", msg);
 208 
 209   BufferNode* nd = _completed_buffers_head;
 210   int i = 0;
 211   while (nd != NULL) {
 212     void** buf = BufferNode::make_buffer_from_node(nd);
 213     jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
 214     print_satb_buffer(buffer, buf, nd->index(), buffer_size());
 215     nd = nd->next();
 216     i += 1;
 217   }
 218 
 219   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 220     jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
 221     satb_queue_for_thread(t).print(buffer);
 222   }
 223 
 224   shared_satb_queue()->print("Shared");
 225 
 226   tty->cr();
 227 }
 228 #endif // PRODUCT
 229 
 230 void SATBMarkQueueSet::abandon_partial_marking() {
 231   BufferNode* buffers_to_delete = NULL;
 232   {
 233     MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 234     while (_completed_buffers_head != NULL) {
 235       BufferNode* nd = _completed_buffers_head;
 236       _completed_buffers_head = nd->next();
 237       nd->set_next(buffers_to_delete);
 238       buffers_to_delete = nd;
 239     }
 240     _completed_buffers_tail = NULL;
 241     _n_completed_buffers = 0;
 242     DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
 243   }
 244   while (buffers_to_delete != NULL) {
 245     BufferNode* nd = buffers_to_delete;
 246     buffers_to_delete = nd->next();
 247     deallocate_buffer(nd);
 248   }
 249   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 250   // So we can safely manipulate these queues.
 251   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 252     satb_queue_for_thread(t).reset();
 253   }
 254   shared_satb_queue()->reset();
 255 }