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