1 /*
  2  * Copyright (c) 2001, 2020, 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_GC_G1_G1COLLECTEDHEAP_INLINE_HPP
 26 #define SHARE_GC_G1_G1COLLECTEDHEAP_INLINE_HPP
 27 
 28 #include "gc/g1/g1CollectedHeap.hpp"
 29 
 30 #include "gc/g1/g1BarrierSet.hpp"
 31 #include "gc/g1/g1CardSetContainers.hpp"
 32 #include "gc/g1/g1CollectorState.hpp"
 33 #include "gc/g1/g1EvacFailureRegions.hpp"
 34 #include "gc/g1/g1Policy.hpp"
 35 #include "gc/g1/g1RemSet.hpp"
 36 #include "gc/g1/heapRegion.inline.hpp"
 37 #include "gc/g1/heapRegionManager.inline.hpp"
 38 #include "gc/g1/heapRegionRemSet.hpp"
 39 #include "gc/g1/heapRegionSet.inline.hpp"
 40 #include "gc/shared/markBitMap.inline.hpp"
 41 #include "gc/shared/taskqueue.inline.hpp"
 42 #include "runtime/atomic.hpp"
 43 #include "utilities/bitMap.inline.hpp"
 44 
 45 G1GCPhaseTimes* G1CollectedHeap::phase_times() const {
 46   return _policy->phase_times();
 47 }
 48 
 49 G1EvacStats* G1CollectedHeap::alloc_buffer_stats(G1HeapRegionAttr dest) {
 50   switch (dest.type()) {
 51     case G1HeapRegionAttr::Young:
 52       return &_survivor_evac_stats;
 53     case G1HeapRegionAttr::Old:
 54       return &_old_evac_stats;
 55     default:
 56       ShouldNotReachHere();
 57       return NULL; // Keep some compilers happy
 58   }
 59 }
 60 
 61 size_t G1CollectedHeap::desired_plab_sz(G1HeapRegionAttr dest) {
 62   size_t gclab_word_size = alloc_buffer_stats(dest)->desired_plab_sz(workers()->active_workers());
 63   // Prevent humongous PLAB sizes for two reasons:
 64   // * PLABs are allocated using a similar paths as oops, but should
 65   //   never be in a humongous region
 66   // * Allowing humongous PLABs needlessly churns the region free lists
 67   return MIN2(_humongous_object_threshold_in_words, gclab_word_size);
 68 }
 69 
 70 // Inline functions for G1CollectedHeap
 71 
 72 // Return the region with the given index. It assumes the index is valid.
 73 inline HeapRegion* G1CollectedHeap::region_at(uint index) const { return _hrm.at(index); }
 74 
 75 // Return the region with the given index, or NULL if unmapped. It assumes the index is valid.
 76 inline HeapRegion* G1CollectedHeap::region_at_or_null(uint index) const { return _hrm.at_or_null(index); }
 77 
 78 inline HeapRegion* G1CollectedHeap::next_region_in_humongous(HeapRegion* hr) const {
 79   return _hrm.next_region_in_humongous(hr);
 80 }
 81 
 82 inline uint G1CollectedHeap::addr_to_region(HeapWord* addr) const {
 83   assert(is_in_reserved(addr),
 84          "Cannot calculate region index for address " PTR_FORMAT " that is outside of the heap [" PTR_FORMAT ", " PTR_FORMAT ")",
 85          p2i(addr), p2i(reserved().start()), p2i(reserved().end()));
 86   return (uint)(pointer_delta(addr, reserved().start(), sizeof(uint8_t)) >> HeapRegion::LogOfHRGrainBytes);
 87 }
 88 
 89 inline HeapWord* G1CollectedHeap::bottom_addr_for_region(uint index) const {
 90   return _hrm.reserved().start() + index * HeapRegion::GrainWords;
 91 }
 92 
 93 template <class T>
 94 inline HeapRegion* G1CollectedHeap::heap_region_containing(const T addr) const {
 95   assert(addr != NULL, "invariant");
 96   assert(is_in_reserved((const void*) addr),
 97          "Address " PTR_FORMAT " is outside of the heap ranging from [" PTR_FORMAT " to " PTR_FORMAT ")",
 98          p2i((void*)addr), p2i(reserved().start()), p2i(reserved().end()));
 99   return _hrm.addr_to_region((HeapWord*)(void*) addr);
100 }
101 
102 template <class T>
103 inline HeapRegion* G1CollectedHeap::heap_region_containing_or_null(const T addr) const {
104   assert(addr != NULL, "invariant");
105   assert(is_in_reserved((const void*) addr),
106          "Address " PTR_FORMAT " is outside of the heap ranging from [" PTR_FORMAT " to " PTR_FORMAT ")",
107          p2i((void*)addr), p2i(reserved().start()), p2i(reserved().end()));
108   uint const region_idx = addr_to_region(addr);
109   return region_at_or_null(region_idx);
110 }
111 
112 inline void G1CollectedHeap::old_set_add(HeapRegion* hr) {
113   _old_set.add(hr);
114 }
115 
116 inline void G1CollectedHeap::old_set_remove(HeapRegion* hr) {
117   _old_set.remove(hr);
118 }
119 
120 inline void G1CollectedHeap::archive_set_add(HeapRegion* hr) {
121   _archive_set.add(hr);
122 }
123 
124 // It dirties the cards that cover the block so that the post
125 // write barrier never queues anything when updating objects on this
126 // block. It is assumed (and in fact we assert) that the block
127 // belongs to a young region.
128 inline void
129 G1CollectedHeap::dirty_young_block(HeapWord* start, size_t word_size) {
130   assert_heap_not_locked();
131 
132   // Assign the containing region to containing_hr so that we don't
133   // have to keep calling heap_region_containing() in the
134   // asserts below.
135   DEBUG_ONLY(HeapRegion* containing_hr = heap_region_containing(start);)
136   assert(word_size > 0, "pre-condition");
137   assert(containing_hr->is_in(start), "it should contain start");
138   assert(containing_hr->is_young(), "it should be young");
139   assert(!containing_hr->is_humongous(), "it should not be humongous");
140 
141   HeapWord* end = start + word_size;
142   assert(containing_hr->is_in(end - 1), "it should also contain end - 1");
143 
144   MemRegion mr(start, end);
145   card_table()->g1_mark_as_young(mr);
146 }
147 
148 inline G1ScannerTasksQueueSet* G1CollectedHeap::task_queues() const {
149   return _task_queues;
150 }
151 
152 inline G1ScannerTasksQueue* G1CollectedHeap::task_queue(uint i) const {
153   return _task_queues->queue(i);
154 }
155 
156 inline bool G1CollectedHeap::is_marked_next(oop obj) const {
157   return _cm->next_mark_bitmap()->is_marked(obj);
158 }
159 
160 inline bool G1CollectedHeap::is_in_cset(oop obj) {
161   return is_in_cset(cast_from_oop<HeapWord*>(obj));
162 }
163 
164 inline bool G1CollectedHeap::is_in_cset(HeapWord* addr) {
165   return _region_attr.is_in_cset(addr);
166 }
167 
168 bool G1CollectedHeap::is_in_cset(const HeapRegion* hr) {
169   return _region_attr.is_in_cset(hr);
170 }
171 
172 bool G1CollectedHeap::is_in_cset_or_humongous(const oop obj) {
173   return _region_attr.is_in_cset_or_humongous(cast_from_oop<HeapWord*>(obj));
174 }
175 
176 G1HeapRegionAttr G1CollectedHeap::region_attr(const void* addr) const {
177   return _region_attr.at((HeapWord*)addr);
178 }
179 
180 G1HeapRegionAttr G1CollectedHeap::region_attr(uint idx) const {
181   return _region_attr.get_by_index(idx);
182 }
183 
184 void G1CollectedHeap::register_humongous_region_with_region_attr(uint index) {
185   _region_attr.set_humongous(index, region_at(index)->rem_set()->is_tracked());
186 }
187 
188 void G1CollectedHeap::register_new_survivor_region_with_region_attr(HeapRegion* r) {
189   _region_attr.set_new_survivor_region(r->hrm_index());
190 }
191 
192 void G1CollectedHeap::register_region_with_region_attr(HeapRegion* r) {
193   _region_attr.set_has_remset(r->hrm_index(), r->rem_set()->is_tracked());
194 }
195 
196 void G1CollectedHeap::register_old_region_with_region_attr(HeapRegion* r) {
197   _region_attr.set_in_old(r->hrm_index(), r->rem_set()->is_tracked());
198   _rem_set->exclude_region_from_scan(r->hrm_index());
199 }
200 
201 void G1CollectedHeap::register_optional_region_with_region_attr(HeapRegion* r) {
202   _region_attr.set_optional(r->hrm_index(), r->rem_set()->is_tracked());
203 }
204 
205 
206 inline bool G1CollectedHeap::is_in_young(const oop obj) const {
207   if (obj == NULL) {
208     return false;
209   }
210   return heap_region_containing(obj)->is_young();
211 }
212 
213 inline bool G1CollectedHeap::is_obj_dead(const oop obj, const HeapRegion* hr) const {
214   return hr->is_obj_dead(obj, _cm->prev_mark_bitmap());
215 }
216 
217 inline bool G1CollectedHeap::is_obj_dead(const oop obj) const {
218   if (obj == NULL) {
219     return false;
220   }
221   return is_obj_dead(obj, heap_region_containing(obj));
222 }
223 
224 inline bool G1CollectedHeap::is_obj_ill(const oop obj, const HeapRegion* hr) const {
225   return
226     !hr->obj_allocated_since_next_marking(obj) &&
227     !is_marked_next(obj) &&
228     !hr->is_closed_archive();
229 }
230 
231 inline bool G1CollectedHeap::is_obj_ill(const oop obj) const {
232   if (obj == NULL) {
233     return false;
234   }
235   return is_obj_ill(obj, heap_region_containing(obj));
236 }
237 
238 inline bool G1CollectedHeap::is_obj_dead_full(const oop obj, const HeapRegion* hr) const {
239    return !is_marked_next(obj) && !hr->is_closed_archive();
240 }
241 
242 inline bool G1CollectedHeap::is_obj_dead_full(const oop obj) const {
243     return is_obj_dead_full(obj, heap_region_containing(obj));
244 }
245 
246 inline void G1CollectedHeap::set_humongous_reclaim_candidate(uint region, bool value) {
247   assert(_hrm.at(region)->is_starts_humongous(), "Must start a humongous object");
248   _humongous_reclaim_candidates.set_candidate(region, value);
249 }
250 
251 inline bool G1CollectedHeap::is_humongous_reclaim_candidate(uint region) {
252   assert(_hrm.at(region)->is_starts_humongous(), "Must start a humongous object");
253   return _humongous_reclaim_candidates.is_candidate(region);
254 }
255 
256 inline void G1CollectedHeap::set_humongous_is_live(oop obj) {
257   uint region = addr_to_region(cast_from_oop<HeapWord*>(obj));
258   // Clear the flag in the humongous_reclaim_candidates table.  Also
259   // reset the entry in the region attribute table so that subsequent references
260   // to the same humongous object do not go into the slow path again.
261   // This is racy, as multiple threads may at the same time enter here, but this
262   // is benign.
263   // During collection we only ever clear the "candidate" flag, and only ever clear the
264   // entry in the in_cset_fast_table.
265   // We only ever evaluate the contents of these tables (in the VM thread) after
266   // having synchronized the worker threads with the VM thread, or in the same
267   // thread (i.e. within the VM thread).
268   if (is_humongous_reclaim_candidate(region)) {
269     set_humongous_reclaim_candidate(region, false);
270     _region_attr.clear_humongous(region);
271   }
272 }
273 
274 inline bool G1CollectedHeap::requires_barriers(oop obj) const {
275   assert (obj != NULL, "");
276   return !heap_region_containing(obj)->is_young(); // is_in_young does an unnecessary NULL check
277 }
278 
279 #endif // SHARE_GC_G1_G1COLLECTEDHEAP_INLINE_HPP