1 /* 2 * Copyright (c) 2016, 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp" 27 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 28 #include "gc_implementation/shenandoah/shenandoahHeapRegionSet.hpp" 29 #include "gc_implementation/shenandoah/shenandoahUtils.hpp" 30 #include "runtime/atomic.hpp" 31 #include "utilities/copy.hpp" 32 33 ShenandoahCollectionSet::ShenandoahCollectionSet(ShenandoahHeap* heap, HeapWord* heap_base) : 34 _map_size(heap->num_regions()), 35 _region_size_bytes_shift(ShenandoahHeapRegion::region_size_bytes_shift()), 36 _cset_map(NEW_C_HEAP_ARRAY(jbyte, _map_size, mtGC)), 37 _biased_cset_map(_cset_map - ((uintx)heap_base >> _region_size_bytes_shift)), 38 _heap(heap), 39 _garbage(0), 40 _live_data(0), 41 _used(0), 42 _region_count(0), 43 _current_index(0) { 44 // Use 1-byte data type 45 STATIC_ASSERT(sizeof(jbyte) == 1); 46 47 // Initialize cset map 48 Copy::zero_to_bytes(_cset_map, _map_size); 49 } 50 51 void ShenandoahCollectionSet::add_region(ShenandoahHeapRegion* r) { 52 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 53 assert(Thread::current()->is_VM_thread(), "Must be VMThread"); 54 assert(!is_in(r), "Already in collection set"); 55 _cset_map[r->region_number()] = 1; 56 _region_count ++; 57 _garbage += r->garbage(); 58 _live_data += r->get_live_data_bytes(); 59 _used += r->used(); 60 } 61 62 void ShenandoahCollectionSet::remove_region(ShenandoahHeapRegion* r) { 63 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 64 assert(Thread::current()->is_VM_thread(), "Must be VMThread"); 65 assert(is_in(r), "Not in collection set"); 66 _cset_map[r->region_number()] = 0; 67 _region_count --; 68 } 69 70 void ShenandoahCollectionSet::update_region_status() { 71 for (size_t index = 0; index < _heap->num_regions(); index ++) { 72 ShenandoahHeapRegion* r = _heap->get_region(index); 73 if (is_in(r)) { 74 r->make_cset(); 75 } else { 76 assert (!r->is_cset(), "should not be cset"); 77 } 78 } 79 } 80 81 void ShenandoahCollectionSet::clear() { 82 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 83 Copy::zero_to_bytes(_cset_map, _map_size); 84 85 #ifdef ASSERT 86 for (size_t index = 0; index < _heap->num_regions(); index ++) { 87 assert (!_heap->get_region(index)->is_cset(), "should have been cleared before"); 88 } 89 #endif 90 91 _garbage = 0; 92 _live_data = 0; 93 _used = 0; 94 95 _region_count = 0; 96 _current_index = 0; 97 } 98 99 ShenandoahHeapRegion* ShenandoahCollectionSet::claim_next() { 100 size_t num_regions = _heap->num_regions(); 101 if (_current_index >= (jint)num_regions) { 102 return NULL; 103 } 104 105 jint saved_current = _current_index; 106 size_t index = (size_t)saved_current; 107 108 while(index < num_regions) { 109 if (is_in(index)) { 110 jint cur = Atomic::cmpxchg((jint)(index + 1), &_current_index, saved_current); 111 assert(cur >= (jint)saved_current, "Must move forward"); 112 if (cur == saved_current) { 113 assert(is_in(index), "Invariant"); 114 return _heap->get_region(index); 115 } else { 116 index = (size_t)cur; 117 saved_current = cur; 118 } 119 } else { 120 index ++; 121 } 122 } 123 return NULL; 124 } 125 126 ShenandoahHeapRegion* ShenandoahCollectionSet::next() { 127 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 128 assert(Thread::current()->is_VM_thread(), "Must be VMThread"); 129 size_t num_regions = _heap->num_regions(); 130 for (size_t index = (size_t)_current_index; index < num_regions; index ++) { 131 if (is_in(index)) { 132 _current_index = (jint)(index + 1); 133 return _heap->get_region(index); 134 } 135 } 136 137 return NULL; 138 } 139 140 void ShenandoahCollectionSet::print_on(outputStream* out) const { 141 out->print_cr("Collection Set : " SIZE_FORMAT "", count()); 142 143 debug_only(size_t regions = 0;) 144 for (size_t index = 0; index < _heap->num_regions(); index ++) { 145 if (is_in(index)) { 146 _heap->get_region(index)->print_on(out); 147 debug_only(regions ++;) 148 } 149 } 150 assert(regions == count(), "Must match"); 151 }