1 /*
   2  * Copyright (c) 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/g1/g1CollectedHeap.inline.hpp"
  27 #include "gc/g1/g1SATBMarkQueueSet.hpp"
  28 #include "gc/g1/g1ThreadLocalData.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 #include "gc/shared/satbMarkQueue.hpp"
  31 #include "oops/oop.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 G1SATBMarkQueueSet::G1SATBMarkQueueSet() : _g1h(NULL) {}
  36 
  37 void G1SATBMarkQueueSet::initialize(G1CollectedHeap* g1h,
  38                                     Monitor* cbl_mon,
  39                                     BufferNode::Allocator* allocator,
  40                                     size_t process_completed_buffers_threshold,
  41                                     uint buffer_enqueue_threshold_percentage,
  42                                     Mutex* lock) {
  43   SATBMarkQueueSet::initialize(cbl_mon,
  44                                allocator,
  45                                process_completed_buffers_threshold,
  46                                buffer_enqueue_threshold_percentage,
  47                                lock);
  48   _g1h = g1h;
  49 }
  50 
  51 void G1SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
  52   G1ThreadLocalData::satb_mark_queue(t).handle_zero_index();
  53 }
  54 
  55 SATBMarkQueue& G1SATBMarkQueueSet::satb_queue_for_thread(JavaThread* const t) const{
  56   return G1ThreadLocalData::satb_mark_queue(t);
  57 }
  58 
  59 // Return true if a SATB buffer entry refers to an object that
  60 // requires marking.
  61 //
  62 // The entry must point into the G1 heap.  In particular, it must not
  63 // be a NULL pointer.  NULL pointers are pre-filtered and never
  64 // inserted into a SATB buffer.
  65 //
  66 // An entry that is below the NTAMS pointer for the containing heap
  67 // region requires marking. Such an entry must point to a valid object.
  68 //
  69 // An entry that is at least the NTAMS pointer for the containing heap
  70 // region might be any of the following, none of which should be marked.
  71 //
  72 // * A reference to an object allocated since marking started.
  73 //   According to SATB, such objects are implicitly kept live and do
  74 //   not need to be dealt with via SATB buffer processing.
  75 //
  76 // * A reference to a young generation object. Young objects are
  77 //   handled separately and are not marked by concurrent marking.
  78 //
  79 // * A stale reference to a young generation object. If a young
  80 //   generation object reference is recorded and not filtered out
  81 //   before being moved by a young collection, the reference becomes
  82 //   stale.
  83 //
  84 // * A stale reference to an eagerly reclaimed humongous object.  If a
  85 //   humongous object is recorded and then reclaimed, the reference
  86 //   becomes stale.
  87 //
  88 // The stale reference cases are implicitly handled by the NTAMS
  89 // comparison. Because of the possibility of stale references, buffer
  90 // processing must be somewhat circumspect and not assume entries
  91 // in an unfiltered buffer refer to valid objects.
  92 
  93 static inline bool requires_marking(const void* entry, G1CollectedHeap* g1h) {
  94   // Includes rejection of NULL pointers.
  95   assert(g1h->is_in_reserved(entry),
  96          "Non-heap pointer in SATB buffer: " PTR_FORMAT, p2i(entry));
  97 
  98   HeapRegion* region = g1h->heap_region_containing(entry);
  99   assert(region != NULL, "No region for " PTR_FORMAT, p2i(entry));
 100   if (entry >= region->next_top_at_mark_start()) {
 101     return false;
 102   }
 103 
 104   assert(oopDesc::is_oop(oop(entry), true /* ignore mark word */),
 105          "Invalid oop in SATB buffer: " PTR_FORMAT, p2i(entry));
 106 
 107   return true;
 108 }
 109 
 110 static inline bool discard_entry(const void* entry, G1CollectedHeap* g1h) {
 111   return !requires_marking(entry, g1h) || g1h->is_marked_next((oop)entry);
 112 }
 113 
 114 // Workaround for not yet having std::bind.
 115 class G1SATBMarkQueueFilterFn {
 116   G1CollectedHeap* _g1h;
 117 
 118 public:
 119   G1SATBMarkQueueFilterFn(G1CollectedHeap* g1h) : _g1h(g1h) {}
 120 
 121   // Return true if entry should be filtered out (removed), false if
 122   // it should be retained.
 123   bool operator()(const void* entry) const {
 124     return discard_entry(entry, _g1h);
 125   }
 126 };
 127 
 128 void G1SATBMarkQueueSet::filter(SATBMarkQueue* queue) {
 129   assert(_g1h != NULL, "SATB queue set not initialized");
 130   apply_filter(G1SATBMarkQueueFilterFn(_g1h), queue);
 131 }