1 /*
  2  * Copyright (c) 2015, 2019, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
 28 
 29 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahPacer.inline.hpp"
 32 #include "runtime/atomic.hpp"
 33 
 34 HeapWord* ShenandoahHeapRegion::allocate_aligned(size_t size, ShenandoahAllocRequest &req, size_t alignment_in_bytes) {
 35   shenandoah_assert_heaplocked_or_safepoint();
 36   assert(req.is_lab_alloc(), "allocate_aligned() only applies to LAB allocations");
 37   assert(is_object_aligned(size), "alloc size breaks alignment: " SIZE_FORMAT, size);
 38   assert(is_old(), "aligned allocations are only taken from OLD regions to support PLABs");
 39   assert(is_aligned(alignment_in_bytes, HeapWordSize), "Expect hea word alignment");
 40 
 41   HeapWord* orig_top = top();
 42   size_t alignment_in_words = alignment_in_bytes / HeapWordSize;
 43 
 44   // unalignment_words is the amount by which current top() exceeds the desired alignment point.  We subtract this amount
 45   // from alignment_in_words to determine padding required to next alignment point.
 46 
 47   HeapWord* aligned_obj = (HeapWord*) align_up(orig_top, alignment_in_bytes);
 48   size_t pad_words = aligned_obj - orig_top;
 49   if ((pad_words > 0) && (pad_words < ShenandoahHeap::min_fill_size())) {
 50     pad_words += alignment_in_words;
 51     aligned_obj += alignment_in_words;
 52   }
 53 
 54   if (pointer_delta(end(), aligned_obj) < size) {
 55     // Shrink size to fit within available space and align it
 56     size = pointer_delta(end(), aligned_obj);
 57     size = align_down(size, alignment_in_words);
 58   }
 59 
 60   // Both originally requested size and adjusted size must be properly aligned
 61   assert (is_aligned(size, alignment_in_words), "Size must be multiple of alignment constraint");
 62   if (size >= req.min_size()) {
 63     // Even if req.min_size() may not be a multiple of card size, we know that size is.
 64     if (pad_words > 0) {
 65       assert(pad_words >= ShenandoahHeap::min_fill_size(), "pad_words expanded above to meet size constraint");
 66       ShenandoahHeap::fill_with_object(orig_top, pad_words);
 67       ShenandoahHeap::heap()->card_scan()->register_object(orig_top);
 68     }
 69 
 70     make_regular_allocation(req.affiliation());
 71     adjust_alloc_metadata(req.type(), size);
 72 
 73     HeapWord* new_top = aligned_obj + size;
 74     assert(new_top <= end(), "PLAB cannot span end of heap region");
 75     set_top(new_top);
 76     req.set_actual_size(size);
 77     req.set_waste(pad_words);
 78     assert(is_object_aligned(new_top), "new top breaks alignment: " PTR_FORMAT, p2i(new_top));
 79     assert(is_aligned(aligned_obj, alignment_in_bytes), "obj is not aligned: " PTR_FORMAT, p2i(aligned_obj));
 80     return aligned_obj;
 81   } else {
 82     // The aligned size that fits in this region is smaller than min_size, so don't align top and don't allocate.  Return failure.
 83     return nullptr;
 84   }
 85 }
 86 
 87 HeapWord* ShenandoahHeapRegion::allocate(size_t size, ShenandoahAllocRequest req) {
 88   shenandoah_assert_heaplocked_or_safepoint();
 89   assert(is_object_aligned(size), "alloc size breaks alignment: " SIZE_FORMAT, size);
 90 
 91   HeapWord* obj = top();
 92   if (pointer_delta(end(), obj) >= size) {
 93     make_regular_allocation(req.affiliation());
 94     adjust_alloc_metadata(req.type(), size);
 95 
 96     HeapWord* new_top = obj + size;
 97     set_top(new_top);
 98 
 99     assert(is_object_aligned(new_top), "new top breaks alignment: " PTR_FORMAT, p2i(new_top));
100     assert(is_object_aligned(obj),     "obj is not aligned: "       PTR_FORMAT, p2i(obj));
101 
102     return obj;
103   } else {
104     return nullptr;
105   }
106 }
107 
108 inline void ShenandoahHeapRegion::adjust_alloc_metadata(ShenandoahAllocRequest::Type type, size_t size) {
109   switch (type) {
110     case ShenandoahAllocRequest::_alloc_shared:
111     case ShenandoahAllocRequest::_alloc_shared_gc:
112       // Counted implicitly by tlab/gclab allocs
113       break;
114     case ShenandoahAllocRequest::_alloc_tlab:
115       _tlab_allocs += size;
116       break;
117     case ShenandoahAllocRequest::_alloc_gclab:
118       _gclab_allocs += size;
119       break;
120     case ShenandoahAllocRequest::_alloc_plab:
121       _plab_allocs += size;
122       break;
123     default:
124       ShouldNotReachHere();
125   }
126 }
127 
128 inline void ShenandoahHeapRegion::increase_live_data_alloc_words(size_t s) {
129   internal_increase_live_data(s);
130 }
131 
132 inline void ShenandoahHeapRegion::increase_live_data_gc_words(size_t s) {
133   internal_increase_live_data(s);
134   if (ShenandoahPacing) {
135     ShenandoahHeap::heap()->pacer()->report_mark(s);
136   }
137 }
138 
139 inline void ShenandoahHeapRegion::internal_increase_live_data(size_t s) {
140   size_t new_live_data = Atomic::add(&_live_data, s, memory_order_relaxed);
141 }
142 
143 inline void ShenandoahHeapRegion::clear_live_data() {
144   Atomic::store(&_live_data, (size_t)0);
145 }
146 
147 inline size_t ShenandoahHeapRegion::get_live_data_words() const {
148   return Atomic::load(&_live_data);
149 }
150 
151 inline size_t ShenandoahHeapRegion::get_live_data_bytes() const {
152   return get_live_data_words() * HeapWordSize;
153 }
154 
155 inline bool ShenandoahHeapRegion::has_live() const {
156   return get_live_data_words() != 0;
157 }
158 
159 inline size_t ShenandoahHeapRegion::garbage() const {
160   assert(used() >= get_live_data_bytes(),
161          "Live Data must be a subset of used() live: " SIZE_FORMAT " used: " SIZE_FORMAT,
162          get_live_data_bytes(), used());
163 
164   size_t result = used() - get_live_data_bytes();
165   return result;
166 }
167 
168 inline size_t ShenandoahHeapRegion::garbage_before_padded_for_promote() const {
169   assert(get_top_before_promote() != nullptr, "top before promote should not equal null");
170   size_t used_before_promote = byte_size(bottom(), get_top_before_promote());
171   assert(used_before_promote >= get_live_data_bytes(),
172          "Live Data must be a subset of used before promotion live: " SIZE_FORMAT " used: " SIZE_FORMAT,
173          get_live_data_bytes(), used_before_promote);
174   size_t result = used_before_promote - get_live_data_bytes();
175   return result;
176 
177 }
178 
179 inline HeapWord* ShenandoahHeapRegion::get_update_watermark() const {
180   HeapWord* watermark = Atomic::load_acquire(&_update_watermark);
181   assert(bottom() <= watermark && watermark <= top(), "within bounds");
182   return watermark;
183 }
184 
185 inline void ShenandoahHeapRegion::set_update_watermark(HeapWord* w) {
186   assert(bottom() <= w && w <= top(), "within bounds");
187   Atomic::release_store(&_update_watermark, w);
188 }
189 
190 // Fast version that avoids synchronization, only to be used at safepoints.
191 inline void ShenandoahHeapRegion::set_update_watermark_at_safepoint(HeapWord* w) {
192   assert(bottom() <= w && w <= top(), "within bounds");
193   assert(SafepointSynchronize::is_at_safepoint(), "Should be at Shenandoah safepoint");
194   _update_watermark = w;
195 }
196 
197 inline ShenandoahAffiliation ShenandoahHeapRegion::affiliation() const {
198   return ShenandoahHeap::heap()->region_affiliation(this);
199 }
200 
201 inline const char* ShenandoahHeapRegion::affiliation_name() const {
202   return shenandoah_affiliation_name(affiliation());
203 }
204 
205 inline bool ShenandoahHeapRegion::is_young() const {
206   return affiliation() == YOUNG_GENERATION;
207 }
208 
209 inline bool ShenandoahHeapRegion::is_old() const {
210   return affiliation() == OLD_GENERATION;
211 }
212 
213 inline bool ShenandoahHeapRegion::is_affiliated() const {
214   return affiliation() != FREE;
215 }
216 
217 inline void ShenandoahHeapRegion::save_top_before_promote() {
218   _top_before_promoted = _top;
219 }
220 
221 inline void ShenandoahHeapRegion::restore_top_before_promote() {
222   _top = _top_before_promoted;
223   _top_before_promoted = nullptr;
224  }
225 
226 
227 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP