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), _last_sampled_rs_lengths(0),
  37     _survivor_head(NULL), _survivor_tail(NULL), _survivor_length(0) {
  38   guarantee(check_list_empty(false), "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   _last_sampled_rs_lengths = 0;
  90 
  91   assert(check_list_empty(false), "just making sure...");
  92 }
  93 
  94 bool YoungList::check_list_well_formed() {
  95   bool ret = true;
  96 
  97   uint length = 0;
  98   HeapRegion* curr = _head;
  99   HeapRegion* last = NULL;
 100   while (curr != NULL) {
 101     if (!curr->is_young()) {
 102       log_info(gc, verify)("### YOUNG REGION " PTR_FORMAT "-" PTR_FORMAT " "
 103                            "incorrectly tagged (y: %d, surv: %d)",
 104                            p2i(curr->bottom()), p2i(curr->end()),
 105                            curr->is_young(), curr->is_survivor());
 106       ret = false;
 107     }
 108     ++length;
 109     last = curr;
 110     curr = curr->get_next_young_region();
 111   }
 112   ret = ret && (length == _length);
 113 
 114   if (!ret) {
 115     log_info(gc, verify)("### YOUNG LIST seems not well formed!");
 116     log_info(gc, verify)("###   list has %u entries, _length is %u", 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     log_info(gc, verify)("### YOUNG LIST should have 0 length, not %u", _length);
 127     ret = false;
 128   }
 129   if (check_sample && _last_sampled_rs_lengths != 0) {
 130     log_info(gc, verify)("### YOUNG LIST has non-zero last sampled RS lengths");
 131     ret = false;
 132   }
 133   if (_head != NULL) {
 134     log_info(gc, verify)("### YOUNG LIST does not have a NULL head");
 135     ret = false;
 136   }
 137   if (!ret) {
 138     log_info(gc, verify)("### YOUNG LIST does not seem empty");
 139   }
 140 
 141   return ret;
 142 }
 143 
 144 void
 145 YoungList::rs_length_sampling_init() {
 146   _sampled_rs_lengths = 0;
 147   _curr               = _head;
 148 }
 149 
 150 bool
 151 YoungList::rs_length_sampling_more() {
 152   return _curr != NULL;
 153 }
 154 
 155 void
 156 YoungList::rs_length_sampling_next() {
 157   assert( _curr != NULL, "invariant" );
 158   size_t rs_length = _curr->rem_set()->occupied();
 159 
 160   _sampled_rs_lengths += rs_length;
 161 
 162   // The current region may not yet have been added to the
 163   // incremental collection set (it gets added when it is
 164   // retired as the current allocation region).
 165   if (_curr->in_collection_set()) {
 166     // Update the collection set policy information for this region
 167     _g1h->g1_policy()->update_incremental_cset_info(_curr, rs_length);
 168   }
 169 
 170   _curr = _curr->get_next_young_region();
 171   if (_curr == NULL) {
 172     _last_sampled_rs_lengths = _sampled_rs_lengths;
 173   }
 174 }
 175 
 176 void
 177 YoungList::reset_auxilary_lists() {
 178   guarantee( is_empty(), "young list should be empty" );
 179   assert(check_list_well_formed(), "young list should be well formed");
 180 
 181   // Add survivor regions to SurvRateGroup.
 182   _g1h->g1_policy()->note_start_adding_survivor_regions();
 183   _g1h->g1_policy()->finished_recalculating_age_indexes(true /* is_survivors */);
 184 
 185   int young_index_in_cset = 0;
 186   for (HeapRegion* curr = _survivor_head;
 187        curr != NULL;
 188        curr = curr->get_next_young_region()) {
 189     _g1h->g1_policy()->set_region_survivor(curr, young_index_in_cset);
 190 
 191     // The region is a non-empty survivor so let's add it to
 192     // the incremental collection set for the next evacuation
 193     // pause.
 194     _g1h->g1_policy()->add_region_to_incremental_cset_rhs(curr);
 195     young_index_in_cset += 1;
 196   }
 197   assert((uint) young_index_in_cset == _survivor_length, "post-condition");
 198   _g1h->g1_policy()->note_stop_adding_survivor_regions();
 199 
 200   _head   = _survivor_head;
 201   _length = _survivor_length;
 202   if (_survivor_head != NULL) {
 203     assert(_survivor_tail != NULL, "cause it shouldn't be");
 204     assert(_survivor_length > 0, "invariant");
 205     _survivor_tail->set_next_young_region(NULL);
 206   }
 207 
 208   // Don't clear the survivor list handles until the start of
 209   // the next evacuation pause - we need it in order to re-tag
 210   // the survivor regions from this evacuation pause as 'young'
 211   // at the start of the next.
 212 
 213   _g1h->g1_policy()->finished_recalculating_age_indexes(false /* is_survivors */);
 214 
 215   assert(check_list_well_formed(), "young list should be well formed");
 216 }
 217 
 218 void YoungList::print() {
 219   HeapRegion* lists[] = {_head,   _survivor_head};
 220   const char* names[] = {"YOUNG", "SURVIVOR"};
 221 
 222   for (uint list = 0; list < ARRAY_SIZE(lists); ++list) {
 223     tty->print_cr("%s LIST CONTENTS", names[list]);
 224     HeapRegion *curr = lists[list];
 225     if (curr == NULL) {
 226       tty->print_cr("  empty");
 227     }
 228     while (curr != NULL) {
 229       tty->print_cr("  " HR_FORMAT ", P: " PTR_FORMAT ", N: " PTR_FORMAT ", age: %4d",
 230                              HR_FORMAT_PARAMS(curr),
 231                              p2i(curr->prev_top_at_mark_start()),
 232                              p2i(curr->next_top_at_mark_start()),
 233                              curr->age_in_surv_rate_group_cond());
 234       curr = curr->get_next_young_region();
 235     }
 236   }
 237 
 238   tty->cr();
 239 }