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