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", 117 length, _length); 118 } 119 120 return ret; 121 } 122 123 bool YoungList::check_list_empty(bool check_sample) { 124 bool ret = true; 125 126 if (_length != 0) { 127 log_info(gc, verify)("### YOUNG LIST should have 0 length, not %u", _length); 128 ret = false; 129 } 130 if (check_sample && _last_sampled_rs_lengths != 0) { 131 log_info(gc, verify)("### YOUNG LIST has non-zero last sampled RS lengths"); 132 ret = false; 133 } 134 if (_head != NULL) { 135 log_info(gc, verify)("### YOUNG LIST does not have a NULL head"); 136 ret = false; 137 } 138 if (!ret) { 139 log_info(gc, verify)("### 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 } 175 } 176 177 void 178 YoungList::reset_auxilary_lists() { 179 guarantee( is_empty(), "young list should be empty" ); 180 assert(check_list_well_formed(), "young list should be well formed"); 181 182 // Add survivor regions to SurvRateGroup. 183 _g1h->g1_policy()->note_start_adding_survivor_regions(); 184 _g1h->g1_policy()->finished_recalculating_age_indexes(true /* is_survivors */); 185 186 int young_index_in_cset = 0; 187 for (HeapRegion* curr = _survivor_head; 188 curr != NULL; 189 curr = curr->get_next_young_region()) { 190 _g1h->g1_policy()->set_region_survivor(curr, young_index_in_cset); 191 192 // The region is a non-empty survivor so let's add it to 193 // the incremental collection set for the next evacuation 194 // pause. 195 _g1h->g1_policy()->add_region_to_incremental_cset_rhs(curr); 196 young_index_in_cset += 1; 197 } 198 assert((uint) young_index_in_cset == _survivor_length, "post-condition"); 199 _g1h->g1_policy()->note_stop_adding_survivor_regions(); 200 201 _head = _survivor_head; 202 _length = _survivor_length; 203 if (_survivor_head != NULL) { 204 assert(_survivor_tail != NULL, "cause it shouldn't be"); 205 assert(_survivor_length > 0, "invariant"); 206 _survivor_tail->set_next_young_region(NULL); 207 } 208 209 // Don't clear the survivor list handles until the start of 210 // the next evacuation pause - we need it in order to re-tag 211 // the survivor regions from this evacuation pause as 'young' 212 // at the start of the next. 213 214 _g1h->g1_policy()->finished_recalculating_age_indexes(false /* is_survivors */); 215 216 assert(check_list_well_formed(), "young list should be well formed"); 217 } 218 219 void YoungList::print() { 220 HeapRegion* lists[] = {_head, _survivor_head}; 221 const char* names[] = {"YOUNG", "SURVIVOR"}; 222 223 for (uint list = 0; list < ARRAY_SIZE(lists); ++list) { 224 tty->print_cr("%s LIST CONTENTS", names[list]); 225 HeapRegion *curr = lists[list]; 226 if (curr == NULL) { 227 tty->print_cr(" empty"); 228 } 229 while (curr != NULL) { 230 tty->print_cr(" " HR_FORMAT ", P: " PTR_FORMAT ", N: " PTR_FORMAT ", age: %4d", 231 HR_FORMAT_PARAMS(curr), 232 p2i(curr->prev_top_at_mark_start()), 233 p2i(curr->next_top_at_mark_start()), 234 curr->age_in_surv_rate_group_cond()); 235 curr = curr->get_next_young_region(); 236 } 237 } 238 239 tty->cr(); 240 }