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