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