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 #ifndef SHARE_VM_GC_G1_G1COLLECTEDHEAP_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1COLLECTEDHEAP_INLINE_HPP
  27 
  28 #include "gc/g1/g1CollectedHeap.hpp"
  29 #include "gc/g1/g1CollectorState.hpp"
  30 #include "gc/g1/g1ConcurrentMark.inline.hpp"
  31 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  32 #include "gc/g1/heapRegionManager.inline.hpp"
  33 #include "gc/g1/heapRegionSet.inline.hpp"
  34 #include "gc/shared/taskqueue.hpp"
  35 #include "runtime/orderAccess.inline.hpp"
  36 
  37 G1EvacStats* G1CollectedHeap::alloc_buffer_stats(InCSetState dest) {
  38   switch (dest.value()) {
  39     case InCSetState::Young:
  40       return &_survivor_evac_stats;
  41     case InCSetState::Old:
  42       return &_old_evac_stats;
  43     default:
  44       ShouldNotReachHere();
  45       return NULL; // Keep some compilers happy
  46   }
  47 }
  48 
  49 size_t G1CollectedHeap::desired_plab_sz(InCSetState dest) {
  50   size_t gclab_word_size = alloc_buffer_stats(dest)->desired_plab_sz(G1CollectedHeap::heap()->workers()->active_workers());
  51   // Prevent humongous PLAB sizes for two reasons:
  52   // * PLABs are allocated using a similar paths as oops, but should
  53   //   never be in a humongous region
  54   // * Allowing humongous PLABs needlessly churns the region free lists
  55   return MIN2(_humongous_object_threshold_in_words, gclab_word_size);
  56 }
  57 
  58 // Inline functions for G1CollectedHeap
  59 
  60 // Return the region with the given index. It assumes the index is valid.
  61 inline HeapRegion* G1CollectedHeap::region_at(uint index) const { return _hrm.at(index); }
  62 
  63 inline HeapRegion* G1CollectedHeap::next_region_in_humongous(HeapRegion* hr) const {
  64   return _hrm.next_region_in_humongous(hr);
  65 }
  66 
  67 inline uint G1CollectedHeap::addr_to_region(HeapWord* addr) const {
  68   assert(is_in_reserved(addr),
  69          "Cannot calculate region index for address " PTR_FORMAT " that is outside of the heap [" PTR_FORMAT ", " PTR_FORMAT ")",
  70          p2i(addr), p2i(reserved_region().start()), p2i(reserved_region().end()));
  71   return (uint)(pointer_delta(addr, reserved_region().start(), sizeof(uint8_t)) >> HeapRegion::LogOfHRGrainBytes);
  72 }
  73 
  74 inline HeapWord* G1CollectedHeap::bottom_addr_for_region(uint index) const {
  75   return _hrm.reserved().start() + index * HeapRegion::GrainWords;
  76 }
  77 
  78 template <class T>
  79 inline HeapRegion* G1CollectedHeap::heap_region_containing(const T addr) const {
  80   assert(addr != NULL, "invariant");
  81   assert(is_in_g1_reserved((const void*) addr),
  82          "Address " PTR_FORMAT " is outside of the heap ranging from [" PTR_FORMAT " to " PTR_FORMAT ")",
  83          p2i((void*)addr), p2i(g1_reserved().start()), p2i(g1_reserved().end()));
  84   return _hrm.addr_to_region((HeapWord*) addr);
  85 }
  86 
  87 inline void G1CollectedHeap::reset_gc_time_stamp() {
  88   assert_at_safepoint(true);
  89   _gc_time_stamp = 0;
  90 }
  91 
  92 inline void G1CollectedHeap::increment_gc_time_stamp() {
  93   assert_at_safepoint(true);
  94   ++_gc_time_stamp;
  95 }
  96 
  97 inline void G1CollectedHeap::old_set_add(HeapRegion* hr) {
  98   _old_set.add(hr);
  99 }
 100 
 101 inline void G1CollectedHeap::old_set_remove(HeapRegion* hr) {
 102   _old_set.remove(hr);
 103 }
 104 
 105 // It dirties the cards that cover the block so that the post
 106 // write barrier never queues anything when updating objects on this
 107 // block. It is assumed (and in fact we assert) that the block
 108 // belongs to a young region.
 109 inline void
 110 G1CollectedHeap::dirty_young_block(HeapWord* start, size_t word_size) {
 111   assert_heap_not_locked();
 112 
 113   // Assign the containing region to containing_hr so that we don't
 114   // have to keep calling heap_region_containing() in the
 115   // asserts below.
 116   DEBUG_ONLY(HeapRegion* containing_hr = heap_region_containing(start);)
 117   assert(word_size > 0, "pre-condition");
 118   assert(containing_hr->is_in(start), "it should contain start");
 119   assert(containing_hr->is_young(), "it should be young");
 120   assert(!containing_hr->is_humongous(), "it should not be humongous");
 121 
 122   HeapWord* end = start + word_size;
 123   assert(containing_hr->is_in(end - 1), "it should also contain end - 1");
 124 
 125   MemRegion mr(start, end);
 126   g1_barrier_set()->g1_mark_as_young(mr);
 127 }
 128 
 129 inline RefToScanQueue* G1CollectedHeap::task_queue(uint i) const {
 130   return _task_queues->queue(i);
 131 }
 132 
 133 inline bool G1CollectedHeap::isMarkedNext(oop obj) const {
 134   return _cm->next_mark_bitmap()->is_marked((HeapWord*)obj);
 135 }
 136 
 137 inline bool G1CollectedHeap::is_in_cset(oop obj) {
 138   return is_in_cset((HeapWord*)obj);
 139 }
 140 
 141 inline bool G1CollectedHeap::is_in_cset(HeapWord* addr) {
 142   return _in_cset_fast_test.is_in_cset(addr);
 143 }
 144 
 145 bool G1CollectedHeap::is_in_cset(const HeapRegion* hr) {
 146   return _in_cset_fast_test.is_in_cset(hr);
 147 }
 148 
 149 bool G1CollectedHeap::is_in_cset_or_humongous(const oop obj) {
 150   return _in_cset_fast_test.is_in_cset_or_humongous((HeapWord*)obj);
 151 }
 152 
 153 InCSetState G1CollectedHeap::in_cset_state(const oop obj) {
 154   return _in_cset_fast_test.at((HeapWord*)obj);
 155 }
 156 
 157 void G1CollectedHeap::register_humongous_region_with_cset(uint index) {
 158   _in_cset_fast_test.set_humongous(index);
 159 }
 160 
 161 #ifndef PRODUCT
 162 // Support for G1EvacuationFailureALot
 163 
 164 inline bool
 165 G1CollectedHeap::evacuation_failure_alot_for_gc_type(bool gcs_are_young,
 166                                                      bool during_initial_mark,
 167                                                      bool during_marking) {
 168   bool res = false;
 169   if (during_marking) {
 170     res |= G1EvacuationFailureALotDuringConcMark;
 171   }
 172   if (during_initial_mark) {
 173     res |= G1EvacuationFailureALotDuringInitialMark;
 174   }
 175   if (gcs_are_young) {
 176     res |= G1EvacuationFailureALotDuringYoungGC;
 177   } else {
 178     // GCs are mixed
 179     res |= G1EvacuationFailureALotDuringMixedGC;
 180   }
 181   return res;
 182 }
 183 
 184 inline void
 185 G1CollectedHeap::set_evacuation_failure_alot_for_current_gc() {
 186   if (G1EvacuationFailureALot) {
 187     // Note we can't assert that _evacuation_failure_alot_for_current_gc
 188     // is clear here. It may have been set during a previous GC but that GC
 189     // did not copy enough objects (i.e. G1EvacuationFailureALotCount) to
 190     // trigger an evacuation failure and clear the flags and and counts.
 191 
 192     // Check if we have gone over the interval.
 193     const size_t gc_num = total_collections();
 194     const size_t elapsed_gcs = gc_num - _evacuation_failure_alot_gc_number;
 195 
 196     _evacuation_failure_alot_for_current_gc = (elapsed_gcs >= G1EvacuationFailureALotInterval);
 197 
 198     // Now check if G1EvacuationFailureALot is enabled for the current GC type.
 199     const bool gcs_are_young = collector_state()->gcs_are_young();
 200     const bool during_im = collector_state()->during_initial_mark_pause();
 201     const bool during_marking = collector_state()->mark_in_progress();
 202 
 203     _evacuation_failure_alot_for_current_gc &=
 204       evacuation_failure_alot_for_gc_type(gcs_are_young,
 205                                           during_im,
 206                                           during_marking);
 207   }
 208 }
 209 
 210 inline bool G1CollectedHeap::evacuation_should_fail() {
 211   if (!G1EvacuationFailureALot || !_evacuation_failure_alot_for_current_gc) {
 212     return false;
 213   }
 214   // G1EvacuationFailureALot is in effect for current GC
 215   // Access to _evacuation_failure_alot_count is not atomic;
 216   // the value does not have to be exact.
 217   if (++_evacuation_failure_alot_count < G1EvacuationFailureALotCount) {
 218     return false;
 219   }
 220   _evacuation_failure_alot_count = 0;
 221   return true;
 222 }
 223 
 224 inline void G1CollectedHeap::reset_evacuation_should_fail() {
 225   if (G1EvacuationFailureALot) {
 226     _evacuation_failure_alot_gc_number = total_collections();
 227     _evacuation_failure_alot_count = 0;
 228     _evacuation_failure_alot_for_current_gc = false;
 229   }
 230 }
 231 #endif  // #ifndef PRODUCT
 232 
 233 inline bool G1CollectedHeap::is_in_young(const oop obj) {
 234   if (obj == NULL) {
 235     return false;
 236   }
 237   return heap_region_containing(obj)->is_young();
 238 }
 239 
 240 inline bool G1CollectedHeap::is_obj_dead(const oop obj) const {
 241   if (obj == NULL) {
 242     return false;
 243   }
 244   return is_obj_dead(obj, heap_region_containing(obj));
 245 }
 246 
 247 inline bool G1CollectedHeap::is_obj_ill(const oop obj) const {
 248   if (obj == NULL) {
 249     return false;
 250   }
 251   return is_obj_ill(obj, heap_region_containing(obj));
 252 }
 253 
 254 inline bool G1CollectedHeap::is_obj_dead_full(const oop obj, const HeapRegion* hr) const {
 255    return !isMarkedNext(obj) && !hr->is_archive();
 256 }
 257 
 258 inline bool G1CollectedHeap::is_obj_dead_full(const oop obj) const {
 259     return is_obj_dead_full(obj, heap_region_containing(obj));
 260 }
 261 
 262 inline void G1CollectedHeap::set_humongous_reclaim_candidate(uint region, bool value) {
 263   assert(_hrm.at(region)->is_starts_humongous(), "Must start a humongous object");
 264   _humongous_reclaim_candidates.set_candidate(region, value);
 265 }
 266 
 267 inline bool G1CollectedHeap::is_humongous_reclaim_candidate(uint region) {
 268   assert(_hrm.at(region)->is_starts_humongous(), "Must start a humongous object");
 269   return _humongous_reclaim_candidates.is_candidate(region);
 270 }
 271 
 272 inline void G1CollectedHeap::set_humongous_is_live(oop obj) {
 273   uint region = addr_to_region((HeapWord*)obj);
 274   // Clear the flag in the humongous_reclaim_candidates table.  Also
 275   // reset the entry in the _in_cset_fast_test table so that subsequent references
 276   // to the same humongous object do not go into the slow path again.
 277   // This is racy, as multiple threads may at the same time enter here, but this
 278   // is benign.
 279   // During collection we only ever clear the "candidate" flag, and only ever clear the
 280   // entry in the in_cset_fast_table.
 281   // We only ever evaluate the contents of these tables (in the VM thread) after
 282   // having synchronized the worker threads with the VM thread, or in the same
 283   // thread (i.e. within the VM thread).
 284   if (is_humongous_reclaim_candidate(region)) {
 285     set_humongous_reclaim_candidate(region, false);
 286     _in_cset_fast_test.clear_humongous(region);
 287   }
 288 }
 289 
 290 #endif // SHARE_VM_GC_G1_G1COLLECTEDHEAP_INLINE_HPP