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