1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. 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 #include "gc/g1/g1CollectedHeap.hpp"
  27 #include "gc/g1/g1CollectionSet.hpp"
  28 #include "gc/g1/g1Policy.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 #include "gc/g1/heapRegion.inline.hpp"
  31 #include "gc/g1/heapRegionRemSet.hpp"
  32 #include "gc/g1/youngList.hpp"
  33 #include "logging/log.hpp"
  34 #include "utilities/growableArray.hpp"
  35 #include "utilities/ostream.hpp"
  36 
  37 YoungList::YoungList(G1CollectedHeap* g1h) :
  38     _g1h(g1h),
  39     _survivor_regions(new (ResourceObj::C_HEAP, mtGC) GrowableArray<HeapRegion*>(8, true, mtGC)),
  40     _head(NULL),
  41     _length(0) {
  42   guarantee(check_list_empty(), "just making sure...");
  43 }
  44 
  45 void YoungList::push_region(HeapRegion *hr) {
  46   assert(!hr->is_young(), "should not already be young");
  47   assert(hr->get_next_young_region() == NULL, "cause it should!");
  48 
  49   hr->set_next_young_region(_head);
  50   _head = hr;
  51 
  52   _g1h->g1_policy()->set_region_eden(hr);
  53   ++_length;
  54 }
  55 
  56 void YoungList::add_survivor_region(HeapRegion* hr) {
  57   assert(hr->is_survivor(), "should be flagged as survivor region");
  58   assert(hr->get_next_young_region() == NULL, "cause it should!");
  59 
  60   _survivor_regions->append(hr);
  61 }
  62 
  63 void YoungList::empty_list(HeapRegion* list) {
  64   while (list != NULL) {
  65     HeapRegion* next = list->get_next_young_region();
  66     list->set_next_young_region(NULL);
  67     list->uninstall_surv_rate_group();
  68     // This is called before a Full GC and all the non-empty /
  69     // non-humongous regions at the end of the Full GC will end up as
  70     // old anyway.
  71     list->set_old();
  72     list = next;
  73   }
  74 }
  75 
  76 void YoungList::empty_list() {
  77   assert(check_list_well_formed(), "young list should be well formed");
  78 
  79   empty_list(_head);
  80   _head = NULL;
  81   _length = 0;
  82 
  83   if (survivor_length() > 0) {
  84     empty_list(_survivor_regions->last());
  85   }
  86   _survivor_regions->clear();
  87 
  88   assert(check_list_empty(), "just making sure...");
  89 }
  90 
  91 uint YoungList::survivor_length() {
  92   return _survivor_regions->length();
  93 }
  94 
  95 bool YoungList::check_list_well_formed() {
  96   bool ret = true;
  97 
  98   uint length = 0;
  99   HeapRegion* curr = _head;
 100   HeapRegion* last = NULL;
 101   while (curr != NULL) {
 102     if (!curr->is_young()) {
 103       log_error(gc, verify)("### YOUNG REGION " PTR_FORMAT "-" PTR_FORMAT " "
 104                             "incorrectly tagged (y: %d, surv: %d)",
 105                             p2i(curr->bottom()), p2i(curr->end()),
 106                             curr->is_young(), curr->is_survivor());
 107       ret = false;
 108     }
 109     ++length;
 110     last = curr;
 111     curr = curr->get_next_young_region();
 112   }
 113   ret = ret && (length == _length);
 114 
 115   if (!ret) {
 116     log_error(gc, verify)("### YOUNG LIST seems not well formed!");
 117     log_error(gc, verify)("###   list has %u entries, _length is %u", length, _length);
 118   }
 119 
 120   return ret;
 121 }
 122 
 123 bool YoungList::check_list_empty() {
 124   bool ret = true;
 125 
 126   if (_length != 0) {
 127     log_error(gc, verify)("### YOUNG LIST should have 0 length, not %u", _length);
 128     ret = false;
 129   }
 130   if (_head != NULL) {
 131     log_error(gc, verify)("### YOUNG LIST does not have a NULL head");
 132     ret = false;
 133   }
 134   if (!ret) {
 135     log_error(gc, verify)("### YOUNG LIST does not seem empty");
 136   }
 137 
 138   return ret;
 139 }
 140 
 141 void
 142 YoungList::reset_auxilary_lists() {
 143   guarantee( is_empty(), "young list should be empty" );
 144   assert(check_list_well_formed(), "young list should be well formed");
 145 
 146   // Add survivor regions to SurvRateGroup.
 147   _g1h->g1_policy()->note_start_adding_survivor_regions();
 148   _g1h->g1_policy()->finished_recalculating_age_indexes(true /* is_survivors */);
 149 
 150   HeapRegion* last = NULL;
 151   for (GrowableArrayIterator<HeapRegion*> it = _survivor_regions->begin();
 152        it != _survivor_regions->end();
 153        ++it) {
 154     HeapRegion* curr = *it;
 155     _g1h->g1_policy()->set_region_survivor(curr);
 156 
 157     // The region is a non-empty survivor so let's add it to
 158     // the incremental collection set for the next evacuation
 159     // pause.
 160     _g1h->collection_set()->add_survivor_regions(curr);
 161 
 162     curr->set_next_young_region(last);
 163     last = curr;
 164   }
 165   _g1h->g1_policy()->note_stop_adding_survivor_regions();
 166 
 167   _head   = last;
 168   _length = _survivor_regions->length();
 169 
 170   // Don't clear the survivor list handles until the start of
 171   // the next evacuation pause - we need it in order to re-tag
 172   // the survivor regions from this evacuation pause as 'young'
 173   // at the start of the next.
 174 
 175   _g1h->g1_policy()->finished_recalculating_age_indexes(false /* is_survivors */);
 176 
 177   assert(check_list_well_formed(), "young list should be well formed");
 178 }