< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahFreeSet.cpp

Print this page

  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 #include "precompiled.hpp"
 26 #include "gc/shared/tlab_globals.hpp"

 27 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
 30 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"


 31 #include "logging/logStream.hpp"
 32 #include "memory/resourceArea.hpp"
 33 #include "runtime/orderAccess.hpp"
 34 
 35 ShenandoahFreeSet::ShenandoahFreeSet(ShenandoahHeap* heap, size_t max_regions) :
 36   _heap(heap),
 37   _mutator_free_bitmap(max_regions, mtGC),
 38   _collector_free_bitmap(max_regions, mtGC),
 39   _max(max_regions)
 40 {
 41   clear_internal();
 42 }
 43 
 44 void ShenandoahFreeSet::increase_used(size_t num_bytes) {
 45   shenandoah_assert_heaplocked();
 46   _used += num_bytes;
 47 
 48   assert(_used <= _capacity, "must not use more than we have: used: " SIZE_FORMAT
 49          ", capacity: " SIZE_FORMAT ", num_bytes: " SIZE_FORMAT, _used, _capacity, num_bytes);
 50 }
 51 
 52 bool ShenandoahFreeSet::is_mutator_free(size_t idx) const {
 53   assert (idx < _max, "index is sane: " SIZE_FORMAT " < " SIZE_FORMAT " (left: " SIZE_FORMAT ", right: " SIZE_FORMAT ")",
 54           idx, _max, _mutator_leftmost, _mutator_rightmost);
 55   return _mutator_free_bitmap.at(idx);
 56 }
 57 
 58 bool ShenandoahFreeSet::is_collector_free(size_t idx) const {
 59   assert (idx < _max, "index is sane: " SIZE_FORMAT " < " SIZE_FORMAT " (left: " SIZE_FORMAT ", right: " SIZE_FORMAT ")",
 60           idx, _max, _collector_leftmost, _collector_rightmost);
 61   return _collector_free_bitmap.at(idx);
 62 }
 63 

















 64 HeapWord* ShenandoahFreeSet::allocate_single(ShenandoahAllocRequest& req, bool& in_new_region) {
 65   // Scan the bitmap looking for a first fit.
 66   //
 67   // Leftmost and rightmost bounds provide enough caching to walk bitmap efficiently. Normally,
 68   // we would find the region to allocate at right away.
 69   //
 70   // Allocations are biased: new application allocs go to beginning of the heap, and GC allocs
 71   // go to the end. This makes application allocation faster, because we would clear lots
 72   // of regions from the beginning most of the time.
 73   //
 74   // Free set maintains mutator and collector views, and normally they allocate in their views only,
 75   // unless we special cases for stealing and mixed allocations.
 76 
 77   switch (req.type()) {
 78     case ShenandoahAllocRequest::_alloc_tlab:
 79     case ShenandoahAllocRequest::_alloc_shared: {
 80 
 81       // Try to allocate in the mutator view
 82       for (size_t idx = _mutator_leftmost; idx <= _mutator_rightmost; idx++) {
 83         if (is_mutator_free(idx)) {
 84           HeapWord* result = try_allocate_in(_heap->get_region(idx), req, in_new_region);
 85           if (result != NULL) {
 86             return result;
 87           }
 88         }
 89       }
 90 
 91       // There is no recovery. Mutator does not touch collector view at all.
 92       break;
 93     }
 94     case ShenandoahAllocRequest::_alloc_gclab:

 95     case ShenandoahAllocRequest::_alloc_shared_gc: {
 96       // size_t is unsigned, need to dodge underflow when _leftmost = 0
 97 
 98       // Fast-path: try to allocate in the collector view first
 99       for (size_t c = _collector_rightmost + 1; c > _collector_leftmost; c--) {
100         size_t idx = c - 1;
101         if (is_collector_free(idx)) {
102           HeapWord* result = try_allocate_in(_heap->get_region(idx), req, in_new_region);
103           if (result != NULL) {
104             return result;
105           }
106         }
107       }
108 
109       // No dice. Can we borrow space from mutator view?
110       if (!ShenandoahEvacReserveOverflow) {
111         return NULL;
112       }
113 
114       // Try to steal the empty region from the mutator view
115       for (size_t c = _mutator_rightmost + 1; c > _mutator_leftmost; c--) {
116         size_t idx = c - 1;
117         if (is_mutator_free(idx)) {
118           ShenandoahHeapRegion* r = _heap->get_region(idx);
119           if (can_allocate_from(r)) {
120             flip_to_gc(r);
121             HeapWord *result = try_allocate_in(r, req, in_new_region);
122             if (result != NULL) {
123               return result;
124             }
125           }
126         }
127       }
128 
129       // No dice. Do not try to mix mutator and GC allocations, because
130       // URWM moves due to GC allocations would expose unparsable mutator
131       // allocations.
132 
133       break;
134     }
135     default:
136       ShouldNotReachHere();
137   }
138 
139   return NULL;
140 }
141 
142 HeapWord* ShenandoahFreeSet::try_allocate_in(ShenandoahHeapRegion* r, ShenandoahAllocRequest& req, bool& in_new_region) {
143   assert (!has_no_alloc_capacity(r), "Performance: should avoid full regions on this path: " SIZE_FORMAT, r->index());
144 
145   if (_heap->is_concurrent_weak_root_in_progress() &&
146       r->is_trash()) {
147     return NULL;
148   }
149 
150   try_recycle_trashed(r);
151 



























152   in_new_region = r->is_empty();
153 
154   HeapWord* result = NULL;
155   size_t size = req.size();
156 

157   if (ShenandoahElasticTLAB && req.is_lab_alloc()) {
158     size_t free = align_down(r->free() >> LogHeapWordSize, MinObjAlignment);
159     if (size > free) {
160       size = free;



























161     }
162     if (size >= req.min_size()) {
163       result = r->allocate(size, req.type());
164       assert (result != NULL, "Allocation must succeed: free " SIZE_FORMAT ", actual " SIZE_FORMAT, free, size);















165     }
166   } else {
167     result = r->allocate(size, req.type());
168   }
169 
170   if (result != NULL) {



171     // Allocation successful, bump stats:
172     if (req.is_mutator_alloc()) {
173       increase_used(size * HeapWordSize);









174     }
175 
176     // Record actual allocation size
177     req.set_actual_size(size);
178 
179     if (req.is_gc_alloc()) {
180       r->set_update_watermark(r->top());
181     }
182   }
183 
184   if (result == NULL || has_no_alloc_capacity(r)) {
185     // Region cannot afford this or future allocations. Retire it.
186     //
187     // While this seems a bit harsh, especially in the case when this large allocation does not
188     // fit, but the next small one would, we are risking to inflate scan times when lots of
189     // almost-full regions precede the fully-empty region where we want allocate the entire TLAB.
190     // TODO: Record first fully-empty region, and use that for large allocations
191 
192     // Record the remainder as allocation waste
193     if (req.is_mutator_alloc()) {
194       size_t waste = r->free();
195       if (waste > 0) {
196         increase_used(waste);

197         _heap->notify_mutator_alloc_words(waste >> LogHeapWordSize, true);
198       }
199     }
200 
201     size_t num = r->index();
202     _collector_free_bitmap.clear_bit(num);
203     _mutator_free_bitmap.clear_bit(num);
204     // Touched the bounds? Need to update:
205     if (touches_bounds(num)) {
206       adjust_bounds();
207     }
208     assert_bounds();
209   }
210   return result;
211 }
212 
213 bool ShenandoahFreeSet::touches_bounds(size_t num) const {
214   return num == _collector_leftmost || num == _collector_rightmost || num == _mutator_leftmost || num == _mutator_rightmost;
215 }
216 

265       return NULL;
266     }
267 
268     // If regions are not adjacent, then current [beg; end] is useless, and we may fast-forward.
269     // If region is not completely free, the current [beg; end] is useless, and we may fast-forward.
270     if (!is_mutator_free(end) || !can_allocate_from(_heap->get_region(end))) {
271       end++;
272       beg = end;
273       continue;
274     }
275 
276     if ((end - beg + 1) == num) {
277       // found the match
278       break;
279     }
280 
281     end++;
282   };
283 
284   size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();

285 
286   // Initialize regions:
287   for (size_t i = beg; i <= end; i++) {
288     ShenandoahHeapRegion* r = _heap->get_region(i);
289     try_recycle_trashed(r);
290 
291     assert(i == beg || _heap->get_region(i - 1)->index() + 1 == r->index(), "Should be contiguous");
292     assert(r->is_empty(), "Should be empty");
293 
294     if (i == beg) {
295       r->make_humongous_start();
296     } else {
297       r->make_humongous_cont();
298     }
299 
300     // Trailing region may be non-full, record the remainder there
301     size_t used_words;
302     if ((i == end) && (remainder != 0)) {
303       used_words = remainder;
304     } else {
305       used_words = ShenandoahHeapRegion::region_size_words();
306     }
307 
308     r->set_top(r->bottom() + used_words);













309 
310     _mutator_free_bitmap.clear_bit(r->index());
311   }
312 
313   // While individual regions report their true use, all humongous regions are
314   // marked used in the free set.
315   increase_used(ShenandoahHeapRegion::region_size_bytes() * num);
316 






317   if (remainder != 0) {
318     // Record this remainder as allocation waste
319     _heap->notify_mutator_alloc_words(ShenandoahHeapRegion::region_size_words() - remainder, true);


320   }
321 
322   // Allocated at left/rightmost? Move the bounds appropriately.
323   if (beg == _mutator_leftmost || end == _mutator_rightmost) {
324     adjust_bounds();
325   }
326   assert_bounds();
327 
328   req.set_actual_size(words_size);
329   return _heap->get_region(beg)->bottom();
330 }
331 
332 bool ShenandoahFreeSet::can_allocate_from(ShenandoahHeapRegion *r) {
333   return r->is_empty() || (r->is_trash() && !_heap->is_concurrent_weak_root_in_progress());
334 }
335 
336 size_t ShenandoahFreeSet::alloc_capacity(ShenandoahHeapRegion *r) {
337   if (r->is_trash()) {
338     // This would be recycled on allocation path
339     return ShenandoahHeapRegion::region_size_bytes();

367   }
368 }
369 
370 void ShenandoahFreeSet::flip_to_gc(ShenandoahHeapRegion* r) {
371   size_t idx = r->index();
372 
373   assert(_mutator_free_bitmap.at(idx), "Should be in mutator view");
374   assert(can_allocate_from(r), "Should not be allocated");
375 
376   _mutator_free_bitmap.clear_bit(idx);
377   _collector_free_bitmap.set_bit(idx);
378   _collector_leftmost = MIN2(idx, _collector_leftmost);
379   _collector_rightmost = MAX2(idx, _collector_rightmost);
380 
381   _capacity -= alloc_capacity(r);
382 
383   if (touches_bounds(idx)) {
384     adjust_bounds();
385   }
386   assert_bounds();




387 }
388 
389 void ShenandoahFreeSet::clear() {
390   shenandoah_assert_heaplocked();
391   clear_internal();
392 }
393 
394 void ShenandoahFreeSet::clear_internal() {
395   _mutator_free_bitmap.clear();
396   _collector_free_bitmap.clear();
397   _mutator_leftmost = _max;
398   _mutator_rightmost = 0;
399   _collector_leftmost = _max;
400   _collector_rightmost = 0;
401   _capacity = 0;
402   _used = 0;
403 }
404 
405 void ShenandoahFreeSet::rebuild() {
406   shenandoah_assert_heaplocked();
407   clear();
408 

409   for (size_t idx = 0; idx < _heap->num_regions(); idx++) {
410     ShenandoahHeapRegion* region = _heap->get_region(idx);
411     if (region->is_alloc_allowed() || region->is_trash()) {
412       assert(!region->is_cset(), "Shouldn't be adding those to the free set");
413 
414       // Do not add regions that would surely fail allocation
415       if (has_no_alloc_capacity(region)) continue;
416 
417       _capacity += alloc_capacity(region);
418       assert(_used <= _capacity, "must not use more than we have");
419 
420       assert(!is_mutator_free(idx), "We are about to add it, it shouldn't be there already");
421       _mutator_free_bitmap.set_bit(idx);


422     }
423   }
424 
425   // Evac reserve: reserve trailing space for evacuations
426   size_t to_reserve = _heap->max_capacity() / 100 * ShenandoahEvacReserve;
427   size_t reserved = 0;
428 
429   for (size_t idx = _heap->num_regions() - 1; idx > 0; idx--) {
430     if (reserved >= to_reserve) break;
431 
432     ShenandoahHeapRegion* region = _heap->get_region(idx);
433     if (_mutator_free_bitmap.at(idx) && can_allocate_from(region)) {
434       _mutator_free_bitmap.clear_bit(idx);
435       _collector_free_bitmap.set_bit(idx);
436       size_t ac = alloc_capacity(region);
437       _capacity -= ac;
438       reserved += ac;

439     }
440   }
441 
442   recompute_bounds();
443   assert_bounds();
444 }
445 
446 void ShenandoahFreeSet::log_status() {
447   shenandoah_assert_heaplocked();
448 
449   LogTarget(Info, gc, ergo) lt;
450   if (lt.is_enabled()) {
451     ResourceMark rm;
452     LogStream ls(lt);
453 
454     {
455       size_t last_idx = 0;
456       size_t max = 0;
457       size_t max_contig = 0;
458       size_t empty_contig = 0;

527         }
528       }
529 
530       ls.print_cr("Reserve: " SIZE_FORMAT "%s, Max: " SIZE_FORMAT "%s",
531                   byte_size_in_proper_unit(total_free), proper_unit_for_byte_size(total_free),
532                   byte_size_in_proper_unit(max),        proper_unit_for_byte_size(max));
533     }
534   }
535 }
536 
537 HeapWord* ShenandoahFreeSet::allocate(ShenandoahAllocRequest& req, bool& in_new_region) {
538   shenandoah_assert_heaplocked();
539   assert_bounds();
540 
541   if (req.size() > ShenandoahHeapRegion::humongous_threshold_words()) {
542     switch (req.type()) {
543       case ShenandoahAllocRequest::_alloc_shared:
544       case ShenandoahAllocRequest::_alloc_shared_gc:
545         in_new_region = true;
546         return allocate_contiguous(req);

547       case ShenandoahAllocRequest::_alloc_gclab:
548       case ShenandoahAllocRequest::_alloc_tlab:
549         in_new_region = false;
550         assert(false, "Trying to allocate TLAB larger than the humongous threshold: " SIZE_FORMAT " > " SIZE_FORMAT,
551                req.size(), ShenandoahHeapRegion::humongous_threshold_words());
552         return NULL;
553       default:
554         ShouldNotReachHere();
555         return NULL;
556     }
557   } else {
558     return allocate_single(req, in_new_region);
559   }
560 }
561 
562 size_t ShenandoahFreeSet::unsafe_peek_free() const {
563   // Deliberately not locked, this method is unsafe when free set is modified.
564 
565   for (size_t index = _mutator_leftmost; index <= _mutator_rightmost; index++) {
566     if (index < _max && is_mutator_free(index)) {

  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 #include "precompiled.hpp"
 26 #include "gc/shared/tlab_globals.hpp"
 27 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 28 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 30 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
 31 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
 32 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
 33 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
 34 #include "logging/logStream.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "runtime/orderAccess.hpp"
 37 
 38 ShenandoahFreeSet::ShenandoahFreeSet(ShenandoahHeap* heap, size_t max_regions) :
 39   _heap(heap),
 40   _mutator_free_bitmap(max_regions, mtGC),
 41   _collector_free_bitmap(max_regions, mtGC),
 42   _max(max_regions)
 43 {
 44   clear_internal();
 45 }
 46 
 47 void ShenandoahFreeSet::increase_used(size_t num_bytes) {
 48   shenandoah_assert_heaplocked();
 49   _used += num_bytes;
 50 
 51   assert(_used <= _capacity, "must not use more than we have: used: " SIZE_FORMAT
 52          ", capacity: " SIZE_FORMAT ", num_bytes: " SIZE_FORMAT, _used, _capacity, num_bytes);
 53 }
 54 
 55 bool ShenandoahFreeSet::is_mutator_free(size_t idx) const {
 56   assert (idx < _max, "index is sane: " SIZE_FORMAT " < " SIZE_FORMAT " (left: " SIZE_FORMAT ", right: " SIZE_FORMAT ")",
 57           idx, _max, _mutator_leftmost, _mutator_rightmost);
 58   return _mutator_free_bitmap.at(idx);
 59 }
 60 
 61 bool ShenandoahFreeSet::is_collector_free(size_t idx) const {
 62   assert (idx < _max, "index is sane: " SIZE_FORMAT " < " SIZE_FORMAT " (left: " SIZE_FORMAT ", right: " SIZE_FORMAT ")",
 63           idx, _max, _collector_leftmost, _collector_rightmost);
 64   return _collector_free_bitmap.at(idx);
 65 }
 66 
 67 HeapWord* ShenandoahFreeSet::allocate_with_affiliation(ShenandoahRegionAffiliation affiliation, ShenandoahAllocRequest& req, bool& in_new_region) {
 68   for (size_t c = _collector_rightmost + 1; c > _collector_leftmost; c--) {
 69     // size_t is unsigned, need to dodge underflow when _leftmost = 0
 70     size_t idx = c - 1;
 71     if (is_collector_free(idx)) {
 72       ShenandoahHeapRegion* r = _heap->get_region(idx);
 73       if (r->affiliation() == affiliation) {
 74         HeapWord* result = try_allocate_in(r, req, in_new_region);
 75         if (result != NULL) {
 76           return result;
 77         }
 78       }
 79     }
 80   }
 81   return NULL;
 82 }
 83 
 84 HeapWord* ShenandoahFreeSet::allocate_single(ShenandoahAllocRequest& req, bool& in_new_region) {
 85   // Scan the bitmap looking for a first fit.
 86   //
 87   // Leftmost and rightmost bounds provide enough caching to walk bitmap efficiently. Normally,
 88   // we would find the region to allocate at right away.
 89   //
 90   // Allocations are biased: new application allocs go to beginning of the heap, and GC allocs
 91   // go to the end. This makes application allocation faster, because we would clear lots
 92   // of regions from the beginning most of the time.
 93   //
 94   // Free set maintains mutator and collector views, and normally they allocate in their views only,
 95   // unless we special cases for stealing and mixed allocations.
 96 
 97   switch (req.type()) {
 98     case ShenandoahAllocRequest::_alloc_tlab:
 99     case ShenandoahAllocRequest::_alloc_shared: {
100 
101       // Try to allocate in the mutator view
102       for (size_t idx = _mutator_leftmost; idx <= _mutator_rightmost; idx++) {
103         if (is_mutator_free(idx)) {
104           HeapWord* result = try_allocate_in(_heap->get_region(idx), req, in_new_region);
105           if (result != NULL) {
106             return result;
107           }
108         }
109       }
110 
111       // There is no recovery. Mutator does not touch collector view at all.
112       break;
113     }
114     case ShenandoahAllocRequest::_alloc_gclab:
115     case ShenandoahAllocRequest::_alloc_plab:
116     case ShenandoahAllocRequest::_alloc_shared_gc: {
117       // First try to fit into a region that is already in use in the same generation.
118       HeapWord* result = allocate_with_affiliation(req.affiliation(), req, in_new_region);
119       if (result != NULL) {
120         return result;
121       }
122       // Then try a free region that is dedicated to GC allocations.
123       result = allocate_with_affiliation(FREE, req, in_new_region);
124       if (result != NULL) {
125         return result;


126       }
127 
128       // No dice. Can we borrow space from mutator view?
129       if (!ShenandoahEvacReserveOverflow) {
130         return NULL;
131       }
132 
133       // Try to steal an empty region from the mutator view.
134       for (size_t c = _mutator_rightmost + 1; c > _mutator_leftmost; c--) {
135         size_t idx = c - 1;
136         if (is_mutator_free(idx)) {
137           ShenandoahHeapRegion* r = _heap->get_region(idx);
138           if (can_allocate_from(r)) {
139             flip_to_gc(r);
140             HeapWord *result = try_allocate_in(r, req, in_new_region);
141             if (result != NULL) {
142               return result;
143             }
144           }
145         }
146       }
147 
148       // No dice. Do not try to mix mutator and GC allocations, because
149       // URWM moves due to GC allocations would expose unparsable mutator
150       // allocations.

151       break;
152     }
153     default:
154       ShouldNotReachHere();
155   }

156   return NULL;
157 }
158 
159 HeapWord* ShenandoahFreeSet::try_allocate_in(ShenandoahHeapRegion* r, ShenandoahAllocRequest& req, bool& in_new_region) {
160   assert (!has_no_alloc_capacity(r), "Performance: should avoid full regions on this path: " SIZE_FORMAT, r->index());
161 
162   if (_heap->is_concurrent_weak_root_in_progress() &&
163       r->is_trash()) {
164     return NULL;
165   }
166 
167   try_recycle_trashed(r);
168 
169   if (r->affiliation() == ShenandoahRegionAffiliation::FREE) {
170     ShenandoahMarkingContext* const ctx = _heap->complete_marking_context();
171 
172     r->set_affiliation(req.affiliation());
173     r->set_update_watermark(r->bottom());
174 
175     // Any OLD region allocated during concurrent coalesce-and-fill does not need to be coalesced and filled because
176     // all objects allocated within this region are above TAMS (and thus are implicitly marked).  In case this is an
177     // OLD region and concurrent preparation for mixed evacuations visits this region before the start of the next
178     // old-gen concurrent mark (i.e. this region is allocated following the start of old-gen concurrent mark but before
179     // concurrent preparations for mixed evacuations are completed), we mark this region as not requiring any
180     // coalesce-and-fill processing.  This code is only necessary if req.affiliation() is OLD, but harmless if not.
181     r->end_preemptible_coalesce_and_fill();
182     ctx->capture_top_at_mark_start(r);
183 
184     assert(ctx->top_at_mark_start(r) == r->bottom(), "Newly established allocation region starts with TAMS equal to bottom");
185     assert(ctx->is_bitmap_clear_range(ctx->top_bitmap(r), r->end()), "Bitmap above top_bitmap() must be clear");
186 
187     // Leave top_bitmap alone.  The first time a heap region is put into service, top_bitmap should equal end.
188     // Thereafter, it should represent the upper bound on parts of the bitmap that need to be cleared.
189     log_debug(gc)("NOT clearing bitmap for region " SIZE_FORMAT ", top_bitmap: "
190                   PTR_FORMAT " at transition from FREE to %s",
191                   r->index(), p2i(ctx->top_bitmap(r)), affiliation_name(req.affiliation()));
192   } else if (r->affiliation() != req.affiliation()) {
193     return NULL;
194   }
195 
196   in_new_region = r->is_empty();
197 
198   HeapWord* result = NULL;
199   size_t size = req.size();
200 
201   // req.size() is in words, free() is in bytes.
202   if (ShenandoahElasticTLAB && req.is_lab_alloc()) {
203     if (req.type() == ShenandoahAllocRequest::_alloc_plab) {
204       // Need to assure that plabs are aligned on multiple of card region.
205       size_t free = r->free();
206       size_t usable_free = (free / CardTable::card_size) << CardTable::card_shift;
207       free /= HeapWordSize;
208       usable_free /= HeapWordSize;
209       if (size > usable_free) {
210         size = usable_free;
211       }
212       if (size >= req.min_size()) {
213         result = r->allocate_aligned(size, req, CardTable::card_size);
214         if (result != nullptr && free > usable_free) {
215           // Account for the alignment padding
216           size_t padding = (free - usable_free) * HeapWordSize;
217           increase_used(padding);
218           assert(r->affiliation() == ShenandoahRegionAffiliation::OLD_GENERATION, "All PLABs reside in old-gen");
219           _heap->old_generation()->increase_used(padding);
220           // For verification consistency, we need to report this padding to _heap
221           _heap->increase_used(padding);
222         }
223       }
224     } else {
225       size_t free = align_down(r->free() >> LogHeapWordSize, MinObjAlignment);
226       if (size > free) {
227         size = free;
228       }
229       if (size >= req.min_size()) {
230         result = r->allocate(size, req);
231         assert (result != NULL, "Allocation must succeed: free " SIZE_FORMAT ", actual " SIZE_FORMAT, free, size);
232       }
233     }
234   } else if (req.is_lab_alloc() && req.type() == ShenandoahAllocRequest::_alloc_plab) {
235     size_t free = r->free();
236     size_t usable_free = (free / CardTable::card_size) << CardTable::card_shift;
237     free /= HeapWordSize;
238     usable_free /= HeapWordSize;
239     if (size <= usable_free) {
240       assert(size % CardTable::card_size_in_words == 0, "PLAB size must be multiple of remembered set card size");
241 
242       result = r->allocate_aligned(size, req, CardTable::card_size);
243       if (result != nullptr) {
244         // Account for the alignment padding
245         size_t padding = (free - usable_free) * HeapWordSize;
246         increase_used(padding);
247         assert(r->affiliation() == ShenandoahRegionAffiliation::OLD_GENERATION, "All PLABs reside in old-gen");
248         _heap->old_generation()->increase_used(padding);
249         // For verification consistency, we need to report this padding to _heap
250         _heap->increase_used(padding);
251       }
252     }
253   } else {
254     result = r->allocate(size, req);
255   }
256 
257   if (result != NULL) {
258     // Record actual allocation size
259     req.set_actual_size(size);
260 
261     // Allocation successful, bump stats:
262     if (req.is_mutator_alloc()) {
263       increase_used(size * HeapWordSize);
264     } else if (req.is_gc_alloc()) {
265       // For GC allocations, we advance update_watermark because the objects relocated into this memory during
266       // evacuation are not updated during evacuation.  For both young and old regions r, it is essential that all
267       // PLABs be made parsable at the end of evacuation.  This is enabled by retiring all plabs at end of evacuation.
268       // TODO: Making a PLAB parsable involves placing a filler object in its remnant memory but does not require
269       // that the PLAB be disabled for all future purposes.  We may want to introduce a new service to make the
270       // PLABs parsable while still allowing the PLAB to serve future allocation requests that arise during the
271       // next evacuation pass.
272       r->set_update_watermark(r->top());
273     }
274 
275     if (r->affiliation() == ShenandoahRegionAffiliation::YOUNG_GENERATION) {
276       _heap->young_generation()->increase_used(size * HeapWordSize);
277     } else if (r->affiliation() == ShenandoahRegionAffiliation::OLD_GENERATION) {
278       _heap->old_generation()->increase_used(size * HeapWordSize);

279     }
280   }
281 
282   if (result == NULL || has_no_alloc_capacity(r)) {
283     // Region cannot afford this or future allocations. Retire it.
284     //
285     // While this seems a bit harsh, especially in the case when this large allocation does not
286     // fit, but the next small one would, we are risking to inflate scan times when lots of
287     // almost-full regions precede the fully-empty region where we want allocate the entire TLAB.
288     // TODO: Record first fully-empty region, and use that for large allocations
289 
290     // Record the remainder as allocation waste
291     if (req.is_mutator_alloc()) {
292       size_t waste = r->free();
293       if (waste > 0) {
294         increase_used(waste);
295         _heap->generation_for(req.affiliation())->increase_allocated(waste);
296         _heap->notify_mutator_alloc_words(waste >> LogHeapWordSize, true);
297       }
298     }
299 
300     size_t num = r->index();
301     _collector_free_bitmap.clear_bit(num);
302     _mutator_free_bitmap.clear_bit(num);
303     // Touched the bounds? Need to update:
304     if (touches_bounds(num)) {
305       adjust_bounds();
306     }
307     assert_bounds();
308   }
309   return result;
310 }
311 
312 bool ShenandoahFreeSet::touches_bounds(size_t num) const {
313   return num == _collector_leftmost || num == _collector_rightmost || num == _mutator_leftmost || num == _mutator_rightmost;
314 }
315 

364       return NULL;
365     }
366 
367     // If regions are not adjacent, then current [beg; end] is useless, and we may fast-forward.
368     // If region is not completely free, the current [beg; end] is useless, and we may fast-forward.
369     if (!is_mutator_free(end) || !can_allocate_from(_heap->get_region(end))) {
370       end++;
371       beg = end;
372       continue;
373     }
374 
375     if ((end - beg + 1) == num) {
376       // found the match
377       break;
378     }
379 
380     end++;
381   };
382 
383   size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
384   ShenandoahMarkingContext* const ctx = _heap->complete_marking_context();
385 
386   // Initialize regions:
387   for (size_t i = beg; i <= end; i++) {
388     ShenandoahHeapRegion* r = _heap->get_region(i);
389     try_recycle_trashed(r);
390 
391     assert(i == beg || _heap->get_region(i - 1)->index() + 1 == r->index(), "Should be contiguous");
392     assert(r->is_empty(), "Should be empty");
393 
394     if (i == beg) {
395       r->make_humongous_start();
396     } else {
397       r->make_humongous_cont();
398     }
399 
400     // Trailing region may be non-full, record the remainder there
401     size_t used_words;
402     if ((i == end) && (remainder != 0)) {
403       used_words = remainder;
404     } else {
405       used_words = ShenandoahHeapRegion::region_size_words();
406     }
407 
408     r->set_affiliation(req.affiliation());
409     r->set_update_watermark(r->bottom());
410     r->set_top(r->bottom());    // Set top to bottom so we can capture TAMS
411     ctx->capture_top_at_mark_start(r);
412     r->set_top(r->bottom() + used_words); // Then change top to reflect allocation of humongous object.
413     assert(ctx->top_at_mark_start(r) == r->bottom(), "Newly established allocation region starts with TAMS equal to bottom");
414     assert(ctx->is_bitmap_clear_range(ctx->top_bitmap(r), r->end()), "Bitmap above top_bitmap() must be clear");
415 
416     // Leave top_bitmap alone.  The first time a heap region is put into service, top_bitmap should equal end.
417     // Thereafter, it should represent the upper bound on parts of the bitmap that need to be cleared.
418     // ctx->clear_bitmap(r);
419     log_debug(gc)("NOT clearing bitmap for Humongous region [" PTR_FORMAT ", " PTR_FORMAT "], top_bitmap: "
420                   PTR_FORMAT " at transition from FREE to %s",
421                   p2i(r->bottom()), p2i(r->end()), p2i(ctx->top_bitmap(r)), affiliation_name(req.affiliation()));
422 
423     _mutator_free_bitmap.clear_bit(r->index());
424   }
425 
426   // While individual regions report their true use, all humongous regions are
427   // marked used in the free set.
428   increase_used(ShenandoahHeapRegion::region_size_bytes() * num);
429 
430   if (req.affiliation() == ShenandoahRegionAffiliation::YOUNG_GENERATION) {
431     _heap->young_generation()->increase_used(words_size * HeapWordSize);
432   } else if (req.affiliation() == ShenandoahRegionAffiliation::OLD_GENERATION) {
433     _heap->old_generation()->increase_used(words_size * HeapWordSize);
434   }
435 
436   if (remainder != 0) {
437     // Record this remainder as allocation waste
438     size_t waste = ShenandoahHeapRegion::region_size_words() - remainder;
439     _heap->notify_mutator_alloc_words(waste, true);
440     _heap->generation_for(req.affiliation())->increase_allocated(waste * HeapWordSize);
441   }
442 
443   // Allocated at left/rightmost? Move the bounds appropriately.
444   if (beg == _mutator_leftmost || end == _mutator_rightmost) {
445     adjust_bounds();
446   }
447   assert_bounds();
448 
449   req.set_actual_size(words_size);
450   return _heap->get_region(beg)->bottom();
451 }
452 
453 bool ShenandoahFreeSet::can_allocate_from(ShenandoahHeapRegion *r) {
454   return r->is_empty() || (r->is_trash() && !_heap->is_concurrent_weak_root_in_progress());
455 }
456 
457 size_t ShenandoahFreeSet::alloc_capacity(ShenandoahHeapRegion *r) {
458   if (r->is_trash()) {
459     // This would be recycled on allocation path
460     return ShenandoahHeapRegion::region_size_bytes();

488   }
489 }
490 
491 void ShenandoahFreeSet::flip_to_gc(ShenandoahHeapRegion* r) {
492   size_t idx = r->index();
493 
494   assert(_mutator_free_bitmap.at(idx), "Should be in mutator view");
495   assert(can_allocate_from(r), "Should not be allocated");
496 
497   _mutator_free_bitmap.clear_bit(idx);
498   _collector_free_bitmap.set_bit(idx);
499   _collector_leftmost = MIN2(idx, _collector_leftmost);
500   _collector_rightmost = MAX2(idx, _collector_rightmost);
501 
502   _capacity -= alloc_capacity(r);
503 
504   if (touches_bounds(idx)) {
505     adjust_bounds();
506   }
507   assert_bounds();
508 
509   // We do not ensure that the region is no longer trash,
510   // relying on try_allocate_in(), which always comes next,
511   // to recycle trash before attempting to allocate anything in the region.
512 }
513 
514 void ShenandoahFreeSet::clear() {
515   shenandoah_assert_heaplocked();
516   clear_internal();
517 }
518 
519 void ShenandoahFreeSet::clear_internal() {
520   _mutator_free_bitmap.clear();
521   _collector_free_bitmap.clear();
522   _mutator_leftmost = _max;
523   _mutator_rightmost = 0;
524   _collector_leftmost = _max;
525   _collector_rightmost = 0;
526   _capacity = 0;
527   _used = 0;
528 }
529 
530 void ShenandoahFreeSet::rebuild() {
531   shenandoah_assert_heaplocked();
532   clear();
533 
534   log_debug(gc)("Rebuilding FreeSet");
535   for (size_t idx = 0; idx < _heap->num_regions(); idx++) {
536     ShenandoahHeapRegion* region = _heap->get_region(idx);
537     if (region->is_alloc_allowed() || region->is_trash()) {
538       assert(!region->is_cset(), "Shouldn't be adding those to the free set");
539 
540       // Do not add regions that would surely fail allocation
541       if (has_no_alloc_capacity(region)) continue;
542 
543       _capacity += alloc_capacity(region);
544       assert(_used <= _capacity, "must not use more than we have");
545 
546       assert(!is_mutator_free(idx), "We are about to add it, it shouldn't be there already");
547       _mutator_free_bitmap.set_bit(idx);
548 
549       log_debug(gc)("  Setting Region " SIZE_FORMAT " _mutator_free_bitmap bit to true", idx);
550     }
551   }
552 
553   // Evac reserve: reserve trailing space for evacuations
554   size_t to_reserve = _heap->max_capacity() / 100 * ShenandoahEvacReserve;
555   size_t reserved = 0;
556 
557   for (size_t idx = _heap->num_regions() - 1; idx > 0; idx--) {
558     if (reserved >= to_reserve) break;
559 
560     ShenandoahHeapRegion* region = _heap->get_region(idx);
561     if (_mutator_free_bitmap.at(idx) && can_allocate_from(region)) {
562       _mutator_free_bitmap.clear_bit(idx);
563       _collector_free_bitmap.set_bit(idx);
564       size_t ac = alloc_capacity(region);
565       _capacity -= ac;
566       reserved += ac;
567       log_debug(gc)("  Shifting region " SIZE_FORMAT " from mutator_free to collector_free", idx);
568     }
569   }
570 
571   recompute_bounds();
572   assert_bounds();
573 }
574 
575 void ShenandoahFreeSet::log_status() {
576   shenandoah_assert_heaplocked();
577 
578   LogTarget(Info, gc, ergo) lt;
579   if (lt.is_enabled()) {
580     ResourceMark rm;
581     LogStream ls(lt);
582 
583     {
584       size_t last_idx = 0;
585       size_t max = 0;
586       size_t max_contig = 0;
587       size_t empty_contig = 0;

656         }
657       }
658 
659       ls.print_cr("Reserve: " SIZE_FORMAT "%s, Max: " SIZE_FORMAT "%s",
660                   byte_size_in_proper_unit(total_free), proper_unit_for_byte_size(total_free),
661                   byte_size_in_proper_unit(max),        proper_unit_for_byte_size(max));
662     }
663   }
664 }
665 
666 HeapWord* ShenandoahFreeSet::allocate(ShenandoahAllocRequest& req, bool& in_new_region) {
667   shenandoah_assert_heaplocked();
668   assert_bounds();
669 
670   if (req.size() > ShenandoahHeapRegion::humongous_threshold_words()) {
671     switch (req.type()) {
672       case ShenandoahAllocRequest::_alloc_shared:
673       case ShenandoahAllocRequest::_alloc_shared_gc:
674         in_new_region = true;
675         return allocate_contiguous(req);
676       case ShenandoahAllocRequest::_alloc_plab:
677       case ShenandoahAllocRequest::_alloc_gclab:
678       case ShenandoahAllocRequest::_alloc_tlab:
679         in_new_region = false;
680         assert(false, "Trying to allocate TLAB larger than the humongous threshold: " SIZE_FORMAT " > " SIZE_FORMAT,
681                req.size(), ShenandoahHeapRegion::humongous_threshold_words());
682         return NULL;
683       default:
684         ShouldNotReachHere();
685         return NULL;
686     }
687   } else {
688     return allocate_single(req, in_new_region);
689   }
690 }
691 
692 size_t ShenandoahFreeSet::unsafe_peek_free() const {
693   // Deliberately not locked, this method is unsafe when free set is modified.
694 
695   for (size_t index = _mutator_leftmost; index <= _mutator_rightmost; index++) {
696     if (index < _max && is_mutator_free(index)) {
< prev index next >