1 /*
  2  * Copyright (c) 2016, 2019, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  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 
 27 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 30 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
 31 #include "gc/shenandoah/shenandoahUtils.hpp"
 32 #include "runtime/atomic.hpp"
 33 #include "services/memTracker.hpp"
 34 #include "utilities/copy.hpp"
 35 
 36 ShenandoahCollectionSet::ShenandoahCollectionSet(ShenandoahHeap* heap, ReservedSpace space, char* heap_base) :
 37   _map_size(heap->num_regions()),
 38   _region_size_bytes_shift(ShenandoahHeapRegion::region_size_bytes_shift()),
 39   _map_space(space),
 40   _cset_map(_map_space.base() + ((uintx)heap_base >> _region_size_bytes_shift)),
 41   _biased_cset_map(_map_space.base()),
 42   _heap(heap),
 43   _has_old_regions(false),
 44   _garbage(0),
 45   _used(0),
 46   _region_count(0),
 47   _current_index(0) {
 48 
 49   // The collection set map is reserved to cover the entire heap *and* zero addresses.
 50   // This is needed to accept in-cset checks for both heap oops and NULLs, freeing
 51   // high-performance code from checking for NULL first.
 52   //
 53   // Since heap_base can be far away, committing the entire map would waste memory.
 54   // Therefore, we only commit the parts that are needed to operate: the heap view,
 55   // and the zero page.
 56   //
 57   // Note: we could instead commit the entire map, and piggyback on OS virtual memory
 58   // subsystem for mapping not-yet-written-to pages to a single physical backing page,
 59   // but this is not guaranteed, and would confuse NMT and other memory accounting tools.
 60 
 61   MemTracker::record_virtual_memory_type(_map_space.base(), mtGC);
 62 
 63   size_t page_size = (size_t)os::vm_page_size();
 64 
 65   if (!_map_space.special()) {
 66     // Commit entire pages that cover the heap cset map.
 67     char* bot_addr = align_down(_cset_map, page_size);
 68     char* top_addr = align_up(_cset_map + _map_size, page_size);
 69     os::commit_memory_or_exit(bot_addr, pointer_delta(top_addr, bot_addr, 1), false,
 70                               "Unable to commit collection set bitmap: heap");
 71 
 72     // Commit the zero page, if not yet covered by heap cset map.
 73     if (bot_addr != _biased_cset_map) {
 74       os::commit_memory_or_exit(_biased_cset_map, page_size, false,
 75                                 "Unable to commit collection set bitmap: zero page");
 76     }
 77   }
 78 
 79   Copy::zero_to_bytes(_cset_map, _map_size);
 80   Copy::zero_to_bytes(_biased_cset_map, page_size);
 81 }
 82 
 83 void ShenandoahCollectionSet::add_region(ShenandoahHeapRegion* r) {
 84   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 85   assert(Thread::current()->is_VM_thread(), "Must be VMThread");
 86   assert(!is_in(r), "Already in collection set");
 87   assert(!r->is_humongous(), "Only add regular regions to the collection set");
 88 
 89   _cset_map[r->index()] = 1;
 90   _region_count++;
 91   _has_old_regions |= r->is_old();
 92   _garbage += r->garbage();
 93   _used += r->used();
 94   // Update the region status too. State transition would be checked internally.
 95   r->make_cset();
 96 }
 97 
 98 void ShenandoahCollectionSet::clear() {
 99   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
100   Copy::zero_to_bytes(_cset_map, _map_size);
101 
102 #ifdef ASSERT
103   for (size_t index = 0; index < _heap->num_regions(); index ++) {
104     assert (!_heap->get_region(index)->is_cset(), "should have been cleared before");
105   }
106 #endif
107 
108   _garbage = 0;
109   _used = 0;
110 
111   _region_count = 0;
112   _current_index = 0;
113 
114   _has_old_regions = false;
115 }
116 
117 ShenandoahHeapRegion* ShenandoahCollectionSet::claim_next() {
118   // This code is optimized for the case when collection set contains only
119   // a few regions. In this case, it is more constructive to check for is_in
120   // before hitting the (potentially contended) atomic index.
121 
122   size_t max = _heap->num_regions();
123   size_t old = Atomic::load(&_current_index);
124 
125   for (size_t index = old; index < max; index++) {
126     if (is_in(index)) {
127       size_t cur = Atomic::cmpxchg(&_current_index, old, index + 1, memory_order_relaxed);
128       assert(cur >= old, "Always move forward");
129       if (cur == old) {
130         // Successfully moved the claim index, this is our region.
131         return _heap->get_region(index);
132       } else {
133         // Somebody else moved the claim index, restart from there.
134         index = cur - 1; // adjust for loop post-increment
135         old = cur;
136       }
137     }
138   }
139   return NULL;
140 }
141 
142 ShenandoahHeapRegion* ShenandoahCollectionSet::next() {
143   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
144   assert(Thread::current()->is_VM_thread(), "Must be VMThread");
145 
146   size_t max = _heap->num_regions();
147   for (size_t index = _current_index; index < max; index++) {
148     if (is_in(index)) {
149       _current_index = index + 1;
150       return _heap->get_region(index);
151     }
152   }
153 
154   return NULL;
155 }
156 
157 void ShenandoahCollectionSet::print_on(outputStream* out) const {
158   out->print_cr("Collection Set : " SIZE_FORMAT "", count());
159 
160   debug_only(size_t regions = 0;)
161   for (size_t index = 0; index < _heap->num_regions(); index ++) {
162     if (is_in(index)) {
163       _heap->get_region(index)->print_on(out);
164       debug_only(regions ++;)
165     }
166   }
167   assert(regions == count(), "Must match");
168 }