| 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/shenandoah/shenandoahCollectionSet.hpp" |
| 27 | #include "gc/shenandoah/shenandoahHeap.inline.hpp" |
| 28 | #include "gc/shenandoah/shenandoahHeapRegionSet.hpp" |
| 29 | #include "gc/shenandoah/shenandoahUtils.hpp" |
| 30 | #include "runtime/atomic.hpp" |
| 31 | #include "services/memTracker.hpp" |
| 32 | #include "utilities/copy.hpp" |
| 33 | |
| 34 | ShenandoahCollectionSet::ShenandoahCollectionSet(ShenandoahHeap* heap, char* heap_base, size_t size) : |
| 35 | _map_size(heap->num_regions()), |
| 36 | _region_size_bytes_shift(ShenandoahHeapRegion::region_size_bytes_shift()), |
| 37 | _map_space(align_up(((uintx)heap_base + size) >> _region_size_bytes_shift, os::vm_allocation_granularity())), |
| 38 | _cset_map(_map_space.base() + ((uintx)heap_base >> _region_size_bytes_shift)), |
| 39 | _biased_cset_map(_map_space.base()), |
| 40 | _heap(heap), |
| 41 | _garbage(0), |
| 42 | _live_data(0), |
| 43 | _used(0), |
| 44 | _region_count(0), |
| 45 | _current_index(0) { |
| 46 | |
| 47 | // The collection set map is reserved to cover the entire heap *and* zero addresses. |
| 48 | // This is needed to accept in-cset checks for both heap oops and NULLs, freeing |
| 49 | // high-performance code from checking for NULL first. |
| 50 | // |
| 51 | // Since heap_base can be far away, committing the entire map would waste memory. |
| 52 | // Therefore, we only commit the parts that are needed to operate: the heap view, |
| 53 | // and the zero page. |
| 54 | // |
| 55 | // Note: we could instead commit the entire map, and piggyback on OS virtual memory |
| 56 | // subsystem for mapping not-yet-written-to pages to a single physical backing page, |
| 57 | // but this is not guaranteed, and would confuse NMT and other memory accounting tools. |
| 58 | |
| 59 | MemTracker::record_virtual_memory_type(_map_space.base(), mtGC); |
| 60 | |
| 61 | size_t page_size = (size_t)os::vm_page_size(); |
| 62 | |
| 63 | if (!_map_space.special()) { |
| 64 | // Commit entire pages that cover the heap cset map. |
| 65 | char* bot_addr = align_down(_cset_map, page_size); |
| 66 | char* top_addr = align_up(_cset_map + _map_size, page_size); |
| 67 | os::commit_memory_or_exit(bot_addr, pointer_delta(top_addr, bot_addr, 1), false, |
| 68 | "Unable to commit collection set bitmap: heap" ); |
| 69 | |
| 70 | // Commit the zero page, if not yet covered by heap cset map. |
| 71 | if (bot_addr != _biased_cset_map) { |
| 72 | os::commit_memory_or_exit(_biased_cset_map, page_size, false, |
| 73 | "Unable to commit collection set bitmap: zero page" ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | Copy::zero_to_bytes(_cset_map, _map_size); |
| 78 | Copy::zero_to_bytes(_biased_cset_map, page_size); |
| 79 | } |
| 80 | |
| 81 | void ShenandoahCollectionSet::add_region(ShenandoahHeapRegion* r) { |
| 82 | assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint" ); |
| 83 | assert(Thread::current()->is_VM_thread(), "Must be VMThread" ); |
| 84 | assert(!is_in(r), "Already in collection set" ); |
| 85 | _cset_map[r->region_number()] = 1; |
| 86 | _region_count ++; |
| 87 | _garbage += r->garbage(); |
| 88 | _live_data += r->get_live_data_bytes(); |
| 89 | _used += r->used(); |
| 90 | } |
| 91 | |
| 92 | bool ShenandoahCollectionSet::add_region_check_for_duplicates(ShenandoahHeapRegion* r) { |
| 93 | if (!is_in(r)) { |
| 94 | add_region(r); |
| 95 | return true; |
| 96 | } else { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void ShenandoahCollectionSet::remove_region(ShenandoahHeapRegion* r) { |
| 102 | assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint" ); |
| 103 | assert(Thread::current()->is_VM_thread(), "Must be VMThread" ); |
| 104 | assert(is_in(r), "Not in collection set" ); |
| 105 | _cset_map[r->region_number()] = 0; |
| 106 | _region_count --; |
| 107 | } |
| 108 | |
| 109 | void ShenandoahCollectionSet::update_region_status() { |
| 110 | for (size_t index = 0; index < _heap->num_regions(); index ++) { |
| 111 | ShenandoahHeapRegion* r = _heap->get_region(index); |
| 112 | if (is_in(r)) { |
| 113 | r->make_cset(); |
| 114 | } else { |
| 115 | assert (!r->is_cset(), "should not be cset" ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void ShenandoahCollectionSet::clear() { |
| 121 | assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint" ); |
| 122 | Copy::zero_to_bytes(_cset_map, _map_size); |
| 123 | |
| 124 | #ifdef ASSERT |
| 125 | for (size_t index = 0; index < _heap->num_regions(); index ++) { |
| 126 | assert (!_heap->get_region(index)->is_cset(), "should have been cleared before" ); |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | _garbage = 0; |
| 131 | _live_data = 0; |
| 132 | _used = 0; |
| 133 | |
| 134 | _region_count = 0; |
| 135 | _current_index = 0; |
| 136 | } |
| 137 | |
| 138 | ShenandoahHeapRegion* ShenandoahCollectionSet::claim_next() { |
| 139 | size_t num_regions = _heap->num_regions(); |
| 140 | if (_current_index >= (jint)num_regions) { |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | jint saved_current = _current_index; |
| 145 | size_t index = (size_t)saved_current; |
| 146 | |
| 147 | while(index < num_regions) { |
| 148 | if (is_in(index)) { |
| 149 | jint cur = Atomic::cmpxchg((jint)(index + 1), &_current_index, saved_current); |
| 150 | assert(cur >= (jint)saved_current, "Must move forward" ); |
| 151 | if (cur == saved_current) { |
| 152 | assert(is_in(index), "Invariant" ); |
| 153 | return _heap->get_region(index); |
| 154 | } else { |
| 155 | index = (size_t)cur; |
| 156 | saved_current = cur; |
| 157 | } |
| 158 | } else { |
| 159 | index ++; |
| 160 | } |
| 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | ShenandoahHeapRegion* ShenandoahCollectionSet::next() { |
| 166 | assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint" ); |
| 167 | assert(Thread::current()->is_VM_thread(), "Must be VMThread" ); |
| 168 | size_t num_regions = _heap->num_regions(); |
| 169 | for (size_t index = (size_t)_current_index; index < num_regions; index ++) { |
| 170 | if (is_in(index)) { |
| 171 | _current_index = (jint)(index + 1); |
| 172 | return _heap->get_region(index); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | void ShenandoahCollectionSet::print_on(outputStream* out) const { |
| 180 | out->print_cr("Collection Set : " SIZE_FORMAT "" , count()); |
| 181 | |
| 182 | debug_only(size_t regions = 0;) |
| 183 | for (size_t index = 0; index < _heap->num_regions(); index ++) { |
| 184 | if (is_in(index)) { |
| 185 | _heap->get_region(index)->print_on(out); |
| 186 | debug_only(regions ++;) |
| 187 | } |
| 188 | } |
| 189 | assert(regions == count(), "Must match" ); |
| 190 | } |
| 191 | |