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