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