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 "utilities/ostream.hpp"
  33 
  34 YoungList::YoungList(G1CollectedHeap* g1h) :
  35     _g1h(g1h), _head(NULL), _length(0), _last_sampled_rs_lengths(0),
  36     _survivor_head(NULL), _survivor_tail(NULL), _survivor_length(0) {
  37   guarantee(check_list_empty(false), "just making sure...");
  38 }
  39 
  40 void YoungList::push_region(HeapRegion *hr) {
  41   assert(!hr->is_young(), "should not already be young");
  42   assert(hr->get_next_young_region() == NULL, "cause it should!");
  43 
  44   hr->set_next_young_region(_head);
  45   _head = hr;
  46 
  47   _g1h->g1_policy()->set_region_eden(hr, (int) _length);
  48   ++_length;
  49 }
  50 
  51 void YoungList::add_survivor_region(HeapRegion* hr) {
  52   assert(hr->is_survivor(), "should be flagged as survivor region");
  53   assert(hr->get_next_young_region() == NULL, "cause it should!");
  54 
  55   hr->set_next_young_region(_survivor_head);
  56   if (_survivor_head == NULL) {
  57     _survivor_tail = hr;
  58   }
  59   _survivor_head = hr;
  60   ++_survivor_length;
  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   empty_list(_survivor_head);
  84   _survivor_head = NULL;
  85   _survivor_tail = NULL;
  86   _survivor_length = 0;
  87 
  88   _last_sampled_rs_lengths = 0;
  89 
  90   assert(check_list_empty(false), "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       gclog_or_tty->print_cr("### 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     gclog_or_tty->print_cr("### YOUNG LIST seems not well formed!");
 115     gclog_or_tty->print_cr("###   list has %u entries, _length is %u",
 116                            length, _length);
 117   }
 118 
 119   return ret;
 120 }
 121 
 122 bool YoungList::check_list_empty(bool check_sample) {
 123   bool ret = true;
 124 
 125   if (_length != 0) {
 126     gclog_or_tty->print_cr("### YOUNG LIST should have 0 length, not %u",
 127                   _length);
 128     ret = false;
 129   }
 130   if (check_sample && _last_sampled_rs_lengths != 0) {
 131     gclog_or_tty->print_cr("### YOUNG LIST has non-zero last sampled RS lengths");
 132     ret = false;
 133   }
 134   if (_head != NULL) {
 135     gclog_or_tty->print_cr("### YOUNG LIST does not have a NULL head");
 136     ret = false;
 137   }
 138   if (!ret) {
 139     gclog_or_tty->print_cr("### YOUNG LIST does not seem empty");
 140   }
 141 
 142   return ret;
 143 }
 144 
 145 void
 146 YoungList::rs_length_sampling_init() {
 147   _sampled_rs_lengths = 0;
 148   _curr               = _head;
 149 }
 150 
 151 bool
 152 YoungList::rs_length_sampling_more() {
 153   return _curr != NULL;
 154 }
 155 
 156 void
 157 YoungList::rs_length_sampling_next() {
 158   assert( _curr != NULL, "invariant" );
 159   size_t rs_length = _curr->rem_set()->occupied();
 160 
 161   _sampled_rs_lengths += rs_length;
 162 
 163   // The current region may not yet have been added to the
 164   // incremental collection set (it gets added when it is
 165   // retired as the current allocation region).
 166   if (_curr->in_collection_set()) {
 167     // Update the collection set policy information for this region
 168     _g1h->g1_policy()->update_incremental_cset_info(_curr, rs_length);
 169   }
 170 
 171   _curr = _curr->get_next_young_region();
 172   if (_curr == NULL) {
 173     _last_sampled_rs_lengths = _sampled_rs_lengths;
 174     // gclog_or_tty->print_cr("last sampled RS lengths = %d", _last_sampled_rs_lengths);
 175   }
 176 }
 177 
 178 void
 179 YoungList::reset_auxilary_lists() {
 180   guarantee( is_empty(), "young list should be empty" );
 181   assert(check_list_well_formed(), "young list should be well formed");
 182 
 183   // Add survivor regions to SurvRateGroup.
 184   _g1h->g1_policy()->note_start_adding_survivor_regions();
 185   _g1h->g1_policy()->finished_recalculating_age_indexes(true /* is_survivors */);
 186 
 187   int young_index_in_cset = 0;
 188   for (HeapRegion* curr = _survivor_head;
 189        curr != NULL;
 190        curr = curr->get_next_young_region()) {
 191     _g1h->g1_policy()->set_region_survivor(curr, young_index_in_cset);
 192 
 193     // The region is a non-empty survivor so let's add it to
 194     // the incremental collection set for the next evacuation
 195     // pause.
 196     _g1h->g1_policy()->add_region_to_incremental_cset_rhs(curr);
 197     young_index_in_cset += 1;
 198   }
 199   assert((uint) young_index_in_cset == _survivor_length, "post-condition");
 200   _g1h->g1_policy()->note_stop_adding_survivor_regions();
 201 
 202   _head   = _survivor_head;
 203   _length = _survivor_length;
 204   if (_survivor_head != NULL) {
 205     assert(_survivor_tail != NULL, "cause it shouldn't be");
 206     assert(_survivor_length > 0, "invariant");
 207     _survivor_tail->set_next_young_region(NULL);
 208   }
 209 
 210   // Don't clear the survivor list handles until the start of
 211   // the next evacuation pause - we need it in order to re-tag
 212   // the survivor regions from this evacuation pause as 'young'
 213   // at the start of the next.
 214 
 215   _g1h->g1_policy()->finished_recalculating_age_indexes(false /* is_survivors */);
 216 
 217   assert(check_list_well_formed(), "young list should be well formed");
 218 }
 219 
 220 void YoungList::print() {
 221   HeapRegion* lists[] = {_head,   _survivor_head};
 222   const char* names[] = {"YOUNG", "SURVIVOR"};
 223 
 224   for (uint list = 0; list < ARRAY_SIZE(lists); ++list) {
 225     gclog_or_tty->print_cr("%s LIST CONTENTS", names[list]);
 226     HeapRegion *curr = lists[list];
 227     if (curr == NULL) {
 228       gclog_or_tty->print_cr("  empty");
 229     }
 230     while (curr != NULL) {
 231       gclog_or_tty->print_cr("  " HR_FORMAT ", P: " PTR_FORMAT ", N: " PTR_FORMAT ", age: %4d",
 232                              HR_FORMAT_PARAMS(curr),
 233                              p2i(curr->prev_top_at_mark_start()),
 234                              p2i(curr->next_top_at_mark_start()),
 235                              curr->age_in_surv_rate_group_cond());
 236       curr = curr->get_next_young_region();
 237     }
 238   }
 239 
 240   gclog_or_tty->cr();
 241 }