1 /* 2 * Copyright (c) 2016, Red Hat, Inc. and/or its affiliates. 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 #include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp" 26 #include "gc_implementation/shenandoah/shenandoahCollectionSet.inline.hpp" 27 #include "gc_implementation/shenandoah/shenandoahHeap.hpp" 28 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 29 #include "gc_implementation/shenandoah/shenandoahHeapRegion.hpp" 30 #include "gc_implementation/shenandoah/shenandoahHeapRegionSet.hpp" 31 #include "runtime/atomic.hpp" 32 #include "utilities/copy.hpp" 33 34 ShenandoahCollectionSet::ShenandoahCollectionSet(ShenandoahHeap* heap, HeapWord* heap_base) : 35 _garbage(0), _live_data(0), _heap(heap), _region_count(0), 36 _map_size(heap->num_regions()), _current_index(0) { 37 // Use 1-byte data type 38 STATIC_ASSERT(sizeof(jbyte) == 1); 39 40 _cset_map = NEW_C_HEAP_ARRAY(jbyte, _map_size, mtGC); 41 // Bias cset map's base address for fast test if an oop is in cset 42 _biased_cset_map = _cset_map - ((uintx)heap_base >> ShenandoahHeapRegion::region_size_bytes_shift()); 43 44 // Initialize cset map 45 Copy::zero_to_bytes(_cset_map, _map_size); 46 } 47 48 void ShenandoahCollectionSet::add_region(ShenandoahHeapRegion* r) { 49 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 50 assert(Thread::current()->is_VM_thread(), "Must be VMThread"); 51 assert(!is_in(r), "Already in collection set"); 52 _cset_map[r->region_number()] = 1; 53 _region_count ++; 54 _garbage += r->garbage(); 55 _live_data += r->get_live_data_bytes(); 56 } 57 58 void ShenandoahCollectionSet::remove_region(ShenandoahHeapRegion* r) { 59 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 60 assert(Thread::current()->is_VM_thread(), "Must be VMThread"); 61 assert(is_in(r), "Not in collection set"); 62 _cset_map[r->region_number()] = 0; 63 _region_count --; 64 } 65 66 void ShenandoahCollectionSet::update_region_status() { 67 for (size_t index = 0; index < _heap->num_regions(); index ++) { 68 ShenandoahHeapRegion* r = _heap->get_region(index); 69 if (is_in(r)) { 70 r->make_cset(); 71 } else { 72 assert (!r->is_cset(), "should not be cset"); 73 } 74 } 75 } 76 77 void ShenandoahCollectionSet::clear() { 78 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 79 Copy::zero_to_bytes(_cset_map, _map_size); 80 81 #ifdef ASSERT 82 for (size_t index = 0; index < _heap->num_regions(); index ++) { 83 assert (!_heap->get_region(index)->is_cset(), "should have been cleared before"); 84 } 85 #endif 86 87 _garbage = 0; 88 _live_data = 0; 89 90 _region_count = 0; 91 _current_index = 0; 92 } 93 94 ShenandoahHeapRegion* ShenandoahCollectionSet::claim_next() { 95 size_t num_regions = _heap->num_regions(); 96 if (_current_index >= (jint)num_regions) { 97 return NULL; 98 } 99 100 jint saved_current = _current_index; 101 size_t index = (size_t)saved_current; 102 103 while(index < num_regions) { 104 if (is_in(index)) { 105 jint cur = Atomic::cmpxchg((jint)(index + 1), &_current_index, saved_current); 106 assert(cur >= (jint)saved_current, "Must move forward"); 107 if (cur == saved_current) { 108 assert(is_in(index), "Invariant"); 109 return _heap->get_region(index); 110 } else { 111 index = (size_t)cur; 112 saved_current = cur; 113 } 114 } else { 115 index ++; 116 } 117 } 118 return NULL; 119 } 120 121 122 ShenandoahHeapRegion* ShenandoahCollectionSet::next() { 123 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint"); 124 assert(Thread::current()->is_VM_thread(), "Must be VMThread"); 125 size_t num_regions = _heap->num_regions(); 126 for (size_t index = (size_t)_current_index; index < num_regions; index ++) { 127 if (is_in(index)) { 128 _current_index = (jint)(index + 1); 129 return _heap->get_region(index); 130 } 131 } 132 133 return NULL; 134 } 135 136 137 void ShenandoahCollectionSet::print_on(outputStream* out) const { 138 out->print_cr("Collection Set : " SIZE_FORMAT "", count()); 139 140 debug_only(size_t regions = 0;) 141 for (size_t index = 0; index < _heap->num_regions(); index ++) { 142 if (is_in(index)) { 143 _heap->get_region(index)->print_on(out); 144 debug_only(regions ++;) 145 } 146 } 147 assert(regions == count(), "Must match"); 148 }