1 /*
   2  * Copyright (c) 2001, 2016, 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 inline AllocationContextStats& G1CollectedHeap::allocation_context_stats() {
  61   return _allocation_context_stats;
  62 }
  63 
  64 // Return the region with the given index. It assumes the index is valid.
  65 inline HeapRegion* G1CollectedHeap::region_at(uint index) const { return _hrm.at(index); }
  66 
  67 inline HeapRegion* G1CollectedHeap::next_region_in_humongous(HeapRegion* hr) const {
  68   return _hrm.next_region_in_humongous(hr);
  69 }
  70 
  71 inline uint G1CollectedHeap::addr_to_region(HeapWord* addr) const {
  72   assert(is_in_reserved(addr),
  73          "Cannot calculate region index for address " PTR_FORMAT " that is outside of the heap [" PTR_FORMAT ", " PTR_FORMAT ")",
  74          p2i(addr), p2i(reserved_region().start()), p2i(reserved_region().end()));
  75   return (uint)(pointer_delta(addr, reserved_region().start(), sizeof(uint8_t)) >> HeapRegion::LogOfHRGrainBytes);
  76 }
  77 
  78 inline HeapWord* G1CollectedHeap::bottom_addr_for_region(uint index) const {
  79   return _hrm.reserved().start() + index * HeapRegion::GrainWords;
  80 }
  81 
  82 template <class T>
  83 inline HeapRegion* G1CollectedHeap::heap_region_containing(const T addr) const {
  84   assert(addr != NULL, "invariant");
  85   assert(is_in_g1_reserved((const void*) addr),
  86          "Address " PTR_FORMAT " is outside of the heap ranging from [" PTR_FORMAT " to " PTR_FORMAT ")",
  87          p2i((void*)addr), p2i(g1_reserved().start()), p2i(g1_reserved().end()));
  88   return _hrm.addr_to_region((HeapWord*) addr);
  89 }
  90 
  91 inline void G1CollectedHeap::reset_gc_time_stamp() {
  92   _gc_time_stamp = 0;
  93   OrderAccess::fence();
  94   // Clear the cached CSet starting regions and time stamps.
  95   // Their validity is dependent on the GC timestamp.
  96   clear_cset_start_regions();
  97 }
  98 
  99 inline void G1CollectedHeap::increment_gc_time_stamp() {
 100   ++_gc_time_stamp;
 101   OrderAccess::fence();
 102 }
 103 
 104 inline void G1CollectedHeap::old_set_add(HeapRegion* hr) {
 105   _old_set.add(hr);
 106 }
 107 
 108 inline void G1CollectedHeap::old_set_remove(HeapRegion* hr) {
 109   _old_set.remove(hr);
 110 }
 111 
 112 // It dirties the cards that cover the block so that the post
 113 // write barrier never queues anything when updating objects on this
 114 // block. It is assumed (and in fact we assert) that the block
 115 // belongs to a young region.
 116 inline void
 117 G1CollectedHeap::dirty_young_block(HeapWord* start, size_t word_size) {
 118   assert_heap_not_locked();
 119 
 120   // Assign the containing region to containing_hr so that we don't
 121   // have to keep calling heap_region_containing() in the
 122   // asserts below.
 123   DEBUG_ONLY(HeapRegion* containing_hr = heap_region_containing(start);)
 124   assert(word_size > 0, "pre-condition");
 125   assert(containing_hr->is_in(start), "it should contain start");
 126   assert(containing_hr->is_young(), "it should be young");
 127   assert(!containing_hr->is_humongous(), "it should not be humongous");
 128 
 129   HeapWord* end = start + word_size;
 130   assert(containing_hr->is_in(end - 1), "it should also contain end - 1");
 131 
 132   MemRegion mr(start, end);
 133   g1_barrier_set()->g1_mark_as_young(mr);
 134 }
 135 
 136 inline RefToScanQueue* G1CollectedHeap::task_queue(uint i) const {
 137   return _task_queues->queue(i);
 138 }
 139 
 140 inline bool G1CollectedHeap::isMarkedPrev(oop obj) const {
 141   return _cm->prevMarkBitMap()->isMarked((HeapWord *)obj);
 142 }
 143 
 144 inline bool G1CollectedHeap::isMarkedNext(oop obj) const {
 145   return _cm->nextMarkBitMap()->isMarked((HeapWord *)obj);
 146 }
 147 
 148 // This is a fast test on whether a reference points into the
 149 // collection set or not. Assume that the reference
 150 // points into the heap.
 151 inline bool G1CollectedHeap::is_in_cset(oop obj) {
 152   bool ret = _in_cset_fast_test.is_in_cset((HeapWord*)obj);
 153   // let's make sure the result is consistent with what the slower
 154   // test returns
 155   assert( ret || !obj_in_cs(obj), "sanity");
 156   assert(!ret ||  obj_in_cs(obj), "sanity");
 157   return ret;
 158 }
 159 
 160 bool G1CollectedHeap::is_in_cset(const HeapRegion* hr) {
 161   return _in_cset_fast_test.is_in_cset(hr);
 162 }
 163 
 164 bool G1CollectedHeap::is_in_cset_or_humongous(const oop obj) {
 165   return _in_cset_fast_test.is_in_cset_or_humongous((HeapWord*)obj);
 166 }
 167 
 168 InCSetState G1CollectedHeap::in_cset_state(const oop obj) {
 169   return _in_cset_fast_test.at((HeapWord*)obj);
 170 }
 171 
 172 void G1CollectedHeap::register_humongous_region_with_cset(uint index) {
 173   _in_cset_fast_test.set_humongous(index);
 174 }
 175 
 176 #ifndef PRODUCT
 177 // Support for G1EvacuationFailureALot
 178 
 179 inline bool
 180 G1CollectedHeap::evacuation_failure_alot_for_gc_type(bool gcs_are_young,
 181                                                      bool during_initial_mark,
 182                                                      bool during_marking) {
 183   bool res = false;
 184   if (during_marking) {
 185     res |= G1EvacuationFailureALotDuringConcMark;
 186   }
 187   if (during_initial_mark) {
 188     res |= G1EvacuationFailureALotDuringInitialMark;
 189   }
 190   if (gcs_are_young) {
 191     res |= G1EvacuationFailureALotDuringYoungGC;
 192   } else {
 193     // GCs are mixed
 194     res |= G1EvacuationFailureALotDuringMixedGC;
 195   }
 196   return res;
 197 }
 198 
 199 inline void
 200 G1CollectedHeap::set_evacuation_failure_alot_for_current_gc() {
 201   if (G1EvacuationFailureALot) {
 202     // Note we can't assert that _evacuation_failure_alot_for_current_gc
 203     // is clear here. It may have been set during a previous GC but that GC
 204     // did not copy enough objects (i.e. G1EvacuationFailureALotCount) to
 205     // trigger an evacuation failure and clear the flags and and counts.
 206 
 207     // Check if we have gone over the interval.
 208     const size_t gc_num = total_collections();
 209     const size_t elapsed_gcs = gc_num - _evacuation_failure_alot_gc_number;
 210 
 211     _evacuation_failure_alot_for_current_gc = (elapsed_gcs >= G1EvacuationFailureALotInterval);
 212 
 213     // Now check if G1EvacuationFailureALot is enabled for the current GC type.
 214     const bool gcs_are_young = collector_state()->gcs_are_young();
 215     const bool during_im = collector_state()->during_initial_mark_pause();
 216     const bool during_marking = collector_state()->mark_in_progress();
 217 
 218     _evacuation_failure_alot_for_current_gc &=
 219       evacuation_failure_alot_for_gc_type(gcs_are_young,
 220                                           during_im,
 221                                           during_marking);
 222   }
 223 }
 224 
 225 inline bool G1CollectedHeap::evacuation_should_fail() {
 226   if (!G1EvacuationFailureALot || !_evacuation_failure_alot_for_current_gc) {
 227     return false;
 228   }
 229   // G1EvacuationFailureALot is in effect for current GC
 230   // Access to _evacuation_failure_alot_count is not atomic;
 231   // the value does not have to be exact.
 232   if (++_evacuation_failure_alot_count < G1EvacuationFailureALotCount) {
 233     return false;
 234   }
 235   _evacuation_failure_alot_count = 0;
 236   return true;
 237 }
 238 
 239 inline void G1CollectedHeap::reset_evacuation_should_fail() {
 240   if (G1EvacuationFailureALot) {
 241     _evacuation_failure_alot_gc_number = total_collections();
 242     _evacuation_failure_alot_count = 0;
 243     _evacuation_failure_alot_for_current_gc = false;
 244   }
 245 }
 246 #endif  // #ifndef PRODUCT
 247 
 248 inline bool G1CollectedHeap::is_in_young(const oop obj) {
 249   if (obj == NULL) {
 250     return false;
 251   }
 252   return heap_region_containing(obj)->is_young();
 253 }
 254 
 255 // We don't need barriers for initializing stores to objects
 256 // in the young gen: for the SATB pre-barrier, there is no
 257 // pre-value that needs to be remembered; for the remembered-set
 258 // update logging post-barrier, we don't maintain remembered set
 259 // information for young gen objects.
 260 inline bool G1CollectedHeap::can_elide_initializing_store_barrier(oop new_obj) {
 261   return is_in_young(new_obj);
 262 }
 263 
 264 inline bool G1CollectedHeap::is_obj_dead(const oop obj) const {
 265   if (obj == NULL) {
 266     return false;
 267   }
 268   return is_obj_dead(obj, heap_region_containing(obj));
 269 }
 270 
 271 inline bool G1CollectedHeap::is_obj_ill(const oop obj) const {
 272   if (obj == NULL) {
 273     return false;
 274   }
 275   return is_obj_ill(obj, heap_region_containing(obj));
 276 }
 277 
 278 inline void G1CollectedHeap::set_humongous_reclaim_candidate(uint region, bool value) {
 279   assert(_hrm.at(region)->is_starts_humongous(), "Must start a humongous object");
 280   _humongous_reclaim_candidates.set_candidate(region, value);
 281 }
 282 
 283 inline bool G1CollectedHeap::is_humongous_reclaim_candidate(uint region) {
 284   assert(_hrm.at(region)->is_starts_humongous(), "Must start a humongous object");
 285   return _humongous_reclaim_candidates.is_candidate(region);
 286 }
 287 
 288 inline void G1CollectedHeap::set_humongous_is_live(oop obj) {
 289   uint region = addr_to_region((HeapWord*)obj);
 290   // Clear the flag in the humongous_reclaim_candidates table.  Also
 291   // reset the entry in the _in_cset_fast_test table so that subsequent references
 292   // to the same humongous object do not go into the slow path again.
 293   // This is racy, as multiple threads may at the same time enter here, but this
 294   // is benign.
 295   // During collection we only ever clear the "candidate" flag, and only ever clear the
 296   // entry in the in_cset_fast_table.
 297   // We only ever evaluate the contents of these tables (in the VM thread) after
 298   // having synchronized the worker threads with the VM thread, or in the same
 299   // thread (i.e. within the VM thread).
 300   if (is_humongous_reclaim_candidate(region)) {
 301     set_humongous_reclaim_candidate(region, false);
 302     _in_cset_fast_test.clear_humongous(region);
 303   }
 304 }
 305 
 306 #endif // SHARE_VM_GC_G1_G1COLLECTEDHEAP_INLINE_HPP