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/ostream.hpp"
  35 
  36 YoungList::YoungList(G1CollectedHeap* g1h) :
  37     _g1h(g1h), _head(NULL), _length(0),
  38     _survivor_head(NULL), _survivor_tail(NULL), _survivor_length(0) {
  39   guarantee(check_list_empty(), "just making sure...");
  40 }
  41 
  42 void YoungList::push_region(HeapRegion *hr) {
  43   assert(!hr->is_young(), "should not already be young");
  44   assert(hr->get_next_young_region() == NULL, "cause it should!");
  45 
  46   hr->set_next_young_region(_head);
  47   _head = hr;
  48 
  49   _g1h->g1_policy()->set_region_eden(hr);
  50   ++_length;
  51 }
  52 
  53 void YoungList::add_survivor_region(HeapRegion* hr) {
  54   assert(hr->is_survivor(), "should be flagged as survivor region");
  55   assert(hr->get_next_young_region() == NULL, "cause it should!");
  56 
  57   hr->set_next_young_region(_survivor_head);
  58   if (_survivor_head == NULL) {
  59     _survivor_tail = hr;
  60   }
  61   _survivor_head = hr;
  62   ++_survivor_length;
  63 }
  64 
  65 void YoungList::empty_list(HeapRegion* list) {
  66   while (list != NULL) {
  67     HeapRegion* next = list->get_next_young_region();
  68     list->set_next_young_region(NULL);
  69     list->uninstall_surv_rate_group();
  70     // This is called before a Full GC and all the non-empty /
  71     // non-humongous regions at the end of the Full GC will end up as
  72     // old anyway.
  73     list->set_old();
  74     list = next;
  75   }
  76 }
  77 
  78 void YoungList::empty_list() {
  79   assert(check_list_well_formed(), "young list should be well formed");
  80 
  81   empty_list(_head);
  82   _head = NULL;
  83   _length = 0;
  84 
  85   empty_list(_survivor_head);
  86   _survivor_head = NULL;
  87   _survivor_tail = NULL;
  88   _survivor_length = 0;
  89 
  90   assert(check_list_empty(), "just making sure...");
  91 }
  92 
  93 bool YoungList::check_list_well_formed() {
  94   bool ret = true;
  95 
  96   uint length = 0;
  97   HeapRegion* curr = _head;
  98   HeapRegion* last = NULL;
  99   while (curr != NULL) {
 100     if (!curr->is_young()) {
 101       log_error(gc, verify)("### YOUNG REGION " PTR_FORMAT "-" PTR_FORMAT " "
 102                             "incorrectly tagged (y: %d, surv: %d)",
 103                             p2i(curr->bottom()), p2i(curr->end()),
 104                             curr->is_young(), curr->is_survivor());
 105       ret = false;
 106     }
 107     ++length;
 108     last = curr;
 109     curr = curr->get_next_young_region();
 110   }
 111   ret = ret && (length == _length);
 112 
 113   if (!ret) {
 114     log_error(gc, verify)("### YOUNG LIST seems not well formed!");
 115     log_error(gc, verify)("###   list has %u entries, _length is %u", length, _length);
 116   }
 117 
 118   return ret;
 119 }
 120 
 121 bool YoungList::check_list_empty() {
 122   bool ret = true;
 123 
 124   if (_length != 0) {
 125     log_error(gc, verify)("### YOUNG LIST should have 0 length, not %u", _length);
 126     ret = false;
 127   }
 128   if (_head != NULL) {
 129     log_error(gc, verify)("### YOUNG LIST does not have a NULL head");
 130     ret = false;
 131   }
 132   if (!ret) {
 133     log_error(gc, verify)("### YOUNG LIST does not seem empty");
 134   }
 135 
 136   return ret;
 137 }
 138 
 139 void
 140 YoungList::reset_auxilary_lists() {
 141   guarantee( is_empty(), "young list should be empty" );
 142   assert(check_list_well_formed(), "young list should be well formed");
 143 
 144   // Add survivor regions to SurvRateGroup.
 145   _g1h->g1_policy()->note_start_adding_survivor_regions();
 146   _g1h->g1_policy()->finished_recalculating_age_indexes(true /* is_survivors */);
 147 
 148   for (HeapRegion* curr = _survivor_head;
 149        curr != NULL;
 150        curr = curr->get_next_young_region()) {
 151     _g1h->g1_policy()->set_region_survivor(curr);
 152 
 153     // The region is a non-empty survivor so let's add it to
 154     // the incremental collection set for the next evacuation
 155     // pause.
 156     _g1h->collection_set()->add_survivor_regions(curr);
 157   }
 158   _g1h->g1_policy()->note_stop_adding_survivor_regions();
 159 
 160   _head   = _survivor_head;
 161   _length = _survivor_length;
 162   if (_survivor_head != NULL) {
 163     assert(_survivor_tail != NULL, "cause it shouldn't be");
 164     assert(_survivor_length > 0, "invariant");
 165     _survivor_tail->set_next_young_region(NULL);
 166   }
 167 
 168   // Don't clear the survivor list handles until the start of
 169   // the next evacuation pause - we need it in order to re-tag
 170   // the survivor regions from this evacuation pause as 'young'
 171   // at the start of the next.
 172 
 173   _g1h->g1_policy()->finished_recalculating_age_indexes(false /* is_survivors */);
 174 
 175   assert(check_list_well_formed(), "young list should be well formed");
 176 }
 177 
 178 void YoungList::print() {
 179   HeapRegion* lists[] = {_head,   _survivor_head};
 180   const char* names[] = {"YOUNG", "SURVIVOR"};
 181 
 182   for (uint list = 0; list < ARRAY_SIZE(lists); ++list) {
 183     tty->print_cr("%s LIST CONTENTS", names[list]);
 184     HeapRegion *curr = lists[list];
 185     if (curr == NULL) {
 186       tty->print_cr("  empty");
 187     }
 188     while (curr != NULL) {
 189       tty->print_cr("  " HR_FORMAT ", P: " PTR_FORMAT ", N: " PTR_FORMAT ", age: %4d",
 190                              HR_FORMAT_PARAMS(curr),
 191                              p2i(curr->prev_top_at_mark_start()),
 192                              p2i(curr->next_top_at_mark_start()),
 193                              curr->age_in_surv_rate_group_cond());
 194       curr = curr->get_next_young_region();
 195     }
 196   }
 197 
 198   tty->cr();
 199 }