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