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/satbQueue.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "memory/sharedHeap.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "runtime/thread.hpp"
  31 
  32 void ObjPtrQueue::apply_closure(ObjectClosure* cl) {
  33   if (_buf != NULL) {
  34     apply_closure_to_buffer(cl, _buf, _index, _sz);
  35     _index = _sz;
  36   }
  37 }
  38 
  39 void ObjPtrQueue::apply_closure_to_buffer(ObjectClosure* cl,
  40                                           void** buf, size_t index, size_t sz) {
  41   if (cl == NULL) return;
  42   for (size_t i = index; i < sz; i += oopSize) {
  43     oop obj = (oop)buf[byte_index_to_index((int)i)];
  44     // There can be NULL entries because of destructors.
  45     if (obj != NULL) {
  46       cl->do_object(obj);
  47     }
  48   }
  49 }
  50 
  51 #ifdef ASSERT
  52 void ObjPtrQueue::verify_oops_in_buffer() {
  53   if (_buf == NULL) return;
  54   for (size_t i = _index; i < _sz; i += oopSize) {
  55     oop obj = (oop)_buf[byte_index_to_index((int)i)];
  56     assert(obj != NULL && obj->is_oop(true /* ignore mark word */),
  57            "Not an oop");
  58   }
  59 }
  60 #endif
  61 
  62 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
  63 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
  64 #endif // _MSC_VER
  65 
  66 
  67 SATBMarkQueueSet::SATBMarkQueueSet() :
  68   PtrQueueSet(),
  69   _closure(NULL), _par_closures(NULL),
  70   _shared_satb_queue(this, true /*perm*/)
  71 {}
  72 
  73 void SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
  74                                   int process_completed_threshold,
  75                                   Mutex* lock) {
  76   PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
  77   _shared_satb_queue.set_lock(lock);
  78   if (ParallelGCThreads > 0) {
  79     _par_closures = NEW_C_HEAP_ARRAY(ObjectClosure*, ParallelGCThreads);
  80   }
  81 }
  82 
  83 
  84 void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
  85   DEBUG_ONLY(t->satb_mark_queue().verify_oops_in_buffer();)
  86   t->satb_mark_queue().handle_zero_index();
  87 }
  88 
  89 #ifdef ASSERT
  90 void SATBMarkQueueSet::dump_active_values(JavaThread* first,
  91                                           bool expected_active) {
  92   gclog_or_tty->print_cr("SATB queue active values for Java Threads");
  93   gclog_or_tty->print_cr(" SATB queue set: active is %s",
  94                          (is_active()) ? "TRUE" : "FALSE");
  95   gclog_or_tty->print_cr(" expected_active is %s",
  96                          (expected_active) ? "TRUE" : "FALSE");
  97   for (JavaThread* t = first; t; t = t->next()) {
  98     bool active = t->satb_mark_queue().is_active();
  99     gclog_or_tty->print_cr("  thread %s, active is %s",
 100                            t->name(), (active) ? "TRUE" : "FALSE");
 101   }
 102 }
 103 #endif // ASSERT
 104 
 105 void SATBMarkQueueSet::set_active_all_threads(bool b,
 106                                               bool expected_active) {
 107   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 108   JavaThread* first = Threads::first();
 109 
 110 #ifdef ASSERT
 111   if (_all_active != expected_active) {
 112     dump_active_values(first, expected_active);
 113 
 114     // I leave this here as a guarantee, instead of an assert, so
 115     // that it will still be compiled in if we choose to uncomment
 116     // the #ifdef ASSERT in a product build. The whole block is
 117     // within an #ifdef ASSERT so the guarantee will not be compiled
 118     // in a product build anyway.
 119     guarantee(false,
 120               "SATB queue set has an unexpected active value");
 121   }
 122 #endif // ASSERT
 123   _all_active = b;
 124 
 125   for (JavaThread* t = first; t; t = t->next()) {
 126 #ifdef ASSERT
 127     bool active = t->satb_mark_queue().is_active();
 128     if (active != expected_active) {
 129       dump_active_values(first, expected_active);
 130 
 131       // I leave this here as a guarantee, instead of an assert, so
 132       // that it will still be compiled in if we choose to uncomment
 133       // the #ifdef ASSERT in a product build. The whole block is
 134       // within an #ifdef ASSERT so the guarantee will not be compiled
 135       // in a product build anyway.
 136       guarantee(false,
 137                 "thread has an unexpected active value in its SATB queue");
 138     }
 139 #endif // ASSERT
 140     t->satb_mark_queue().set_active(b);
 141   }
 142 }
 143 
 144 void SATBMarkQueueSet::set_closure(ObjectClosure* closure) {
 145   _closure = closure;
 146 }
 147 
 148 void SATBMarkQueueSet::set_par_closure(int i, ObjectClosure* par_closure) {
 149   assert(ParallelGCThreads > 0 && _par_closures != NULL, "Precondition");
 150   _par_closures[i] = par_closure;
 151 }
 152 
 153 void SATBMarkQueueSet::iterate_closure_all_threads() {
 154   for(JavaThread* t = Threads::first(); t; t = t->next()) {
 155     t->satb_mark_queue().apply_closure(_closure);
 156   }
 157   shared_satb_queue()->apply_closure(_closure);
 158 }
 159 
 160 void SATBMarkQueueSet::par_iterate_closure_all_threads(int worker) {
 161   SharedHeap* sh = SharedHeap::heap();
 162   int parity = sh->strong_roots_parity();
 163 
 164   for(JavaThread* t = Threads::first(); t; t = t->next()) {
 165     if (t->claim_oops_do(true, parity)) {
 166       t->satb_mark_queue().apply_closure(_par_closures[worker]);
 167     }
 168   }
 169   // We'll have worker 0 do this one.
 170   if (worker == 0) {
 171     shared_satb_queue()->apply_closure(_par_closures[0]);
 172   }
 173 }
 174 
 175 bool SATBMarkQueueSet::apply_closure_to_completed_buffer_work(bool par,
 176                                                               int worker) {
 177   BufferNode* nd = NULL;
 178   {
 179     MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 180     if (_completed_buffers_head != NULL) {
 181       nd = _completed_buffers_head;
 182       _completed_buffers_head = nd->next();
 183       if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
 184       _n_completed_buffers--;
 185       if (_n_completed_buffers == 0) _process_completed = false;
 186     }
 187   }
 188   ObjectClosure* cl = (par ? _par_closures[worker] : _closure);
 189   if (nd != NULL) {
 190     void **buf = BufferNode::make_buffer_from_node(nd);
 191     ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
 192     deallocate_buffer(buf);
 193     return true;
 194   } else {
 195     return false;
 196   }
 197 }
 198 
 199 void SATBMarkQueueSet::abandon_partial_marking() {
 200   BufferNode* buffers_to_delete = NULL;
 201   {
 202     MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
 203     while (_completed_buffers_head != NULL) {
 204       BufferNode* nd = _completed_buffers_head;
 205       _completed_buffers_head = nd->next();
 206       nd->set_next(buffers_to_delete);
 207       buffers_to_delete = nd;
 208     }
 209     _completed_buffers_tail = NULL;
 210     _n_completed_buffers = 0;
 211     DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
 212   }
 213   while (buffers_to_delete != NULL) {
 214     BufferNode* nd = buffers_to_delete;
 215     buffers_to_delete = nd->next();
 216     deallocate_buffer(BufferNode::make_buffer_from_node(nd));
 217   }
 218   assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
 219   // So we can safely manipulate these queues.
 220   for (JavaThread* t = Threads::first(); t; t = t->next()) {
 221     t->satb_mark_queue().reset();
 222   }
 223   shared_satb_queue()->reset();
 224 }