src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp

Tue, 01 Mar 2011 14:56:48 -0800

author
iveresov
date
Tue, 01 Mar 2011 14:56:48 -0800
changeset 2606
0ac769a57c64
parent 2504
c33825b68624
child 2593
4e0069ff33df
permissions
-rw-r--r--

6627983: G1: Bad oop deference during marking
Summary: Bulk zeroing reduction didn't work with G1, because arraycopy would call pre-barriers on uninitialized oops. The solution is to have version of arraycopy stubs that don't have pre-barriers. Also refactored arraycopy stubs generation on SPARC to be more readable and reduced the number of stubs necessary in some cases.
Reviewed-by: jrose, kvn, never

ysr@777 1 /*
tonyp@2453 2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
ysr@777 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@777 4 *
ysr@777 5 * This code is free software; you can redistribute it and/or modify it
ysr@777 6 * under the terms of the GNU General Public License version 2 only, as
ysr@777 7 * published by the Free Software Foundation.
ysr@777 8 *
ysr@777 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@777 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@777 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@777 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@777 13 * accompanied this code).
ysr@777 14 *
ysr@777 15 * You should have received a copy of the GNU General Public License version
ysr@777 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@777 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@777 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
ysr@777 22 *
ysr@777 23 */
ysr@777 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "code/icBuffer.hpp"
stefank@2314 27 #include "gc_implementation/g1/bufferingOopClosure.hpp"
stefank@2314 28 #include "gc_implementation/g1/concurrentG1Refine.hpp"
stefank@2314 29 #include "gc_implementation/g1/concurrentG1RefineThread.hpp"
stefank@2314 30 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
stefank@2314 31 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
stefank@2314 32 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
stefank@2314 33 #include "gc_implementation/g1/g1MarkSweep.hpp"
stefank@2314 34 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
stefank@2314 35 #include "gc_implementation/g1/g1RemSet.inline.hpp"
stefank@2314 36 #include "gc_implementation/g1/heapRegionRemSet.hpp"
stefank@2314 37 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
stefank@2314 38 #include "gc_implementation/g1/vm_operations_g1.hpp"
stefank@2314 39 #include "gc_implementation/shared/isGCActiveMark.hpp"
stefank@2314 40 #include "memory/gcLocker.inline.hpp"
stefank@2314 41 #include "memory/genOopClosures.inline.hpp"
stefank@2314 42 #include "memory/generationSpec.hpp"
stefank@2314 43 #include "oops/oop.inline.hpp"
stefank@2314 44 #include "oops/oop.pcgc.inline.hpp"
stefank@2314 45 #include "runtime/aprofiler.hpp"
stefank@2314 46 #include "runtime/vmThread.hpp"
ysr@777 47
tonyp@1377 48 size_t G1CollectedHeap::_humongous_object_threshold_in_words = 0;
tonyp@1377 49
ysr@777 50 // turn it on so that the contents of the young list (scan-only /
ysr@777 51 // to-be-collected) are printed at "strategic" points before / during
ysr@777 52 // / after the collection --- this is useful for debugging
johnc@1829 53 #define YOUNG_LIST_VERBOSE 0
ysr@777 54 // CURRENT STATUS
ysr@777 55 // This file is under construction. Search for "FIXME".
ysr@777 56
ysr@777 57 // INVARIANTS/NOTES
ysr@777 58 //
ysr@777 59 // All allocation activity covered by the G1CollectedHeap interface is
tonyp@2315 60 // serialized by acquiring the HeapLock. This happens in mem_allocate
tonyp@2315 61 // and allocate_new_tlab, which are the "entry" points to the
tonyp@2315 62 // allocation code from the rest of the JVM. (Note that this does not
tonyp@2315 63 // apply to TLAB allocation, which is not part of this interface: it
tonyp@2315 64 // is done by clients of this interface.)
ysr@777 65
ysr@777 66 // Local to this file.
ysr@777 67
ysr@777 68 class RefineCardTableEntryClosure: public CardTableEntryClosure {
ysr@777 69 SuspendibleThreadSet* _sts;
ysr@777 70 G1RemSet* _g1rs;
ysr@777 71 ConcurrentG1Refine* _cg1r;
ysr@777 72 bool _concurrent;
ysr@777 73 public:
ysr@777 74 RefineCardTableEntryClosure(SuspendibleThreadSet* sts,
ysr@777 75 G1RemSet* g1rs,
ysr@777 76 ConcurrentG1Refine* cg1r) :
ysr@777 77 _sts(sts), _g1rs(g1rs), _cg1r(cg1r), _concurrent(true)
ysr@777 78 {}
ysr@777 79 bool do_card_ptr(jbyte* card_ptr, int worker_i) {
johnc@2060 80 bool oops_into_cset = _g1rs->concurrentRefineOneCard(card_ptr, worker_i, false);
johnc@2060 81 // This path is executed by the concurrent refine or mutator threads,
johnc@2060 82 // concurrently, and so we do not care if card_ptr contains references
johnc@2060 83 // that point into the collection set.
johnc@2060 84 assert(!oops_into_cset, "should be");
johnc@2060 85
ysr@777 86 if (_concurrent && _sts->should_yield()) {
ysr@777 87 // Caller will actually yield.
ysr@777 88 return false;
ysr@777 89 }
ysr@777 90 // Otherwise, we finished successfully; return true.
ysr@777 91 return true;
ysr@777 92 }
ysr@777 93 void set_concurrent(bool b) { _concurrent = b; }
ysr@777 94 };
ysr@777 95
ysr@777 96
ysr@777 97 class ClearLoggedCardTableEntryClosure: public CardTableEntryClosure {
ysr@777 98 int _calls;
ysr@777 99 G1CollectedHeap* _g1h;
ysr@777 100 CardTableModRefBS* _ctbs;
ysr@777 101 int _histo[256];
ysr@777 102 public:
ysr@777 103 ClearLoggedCardTableEntryClosure() :
ysr@777 104 _calls(0)
ysr@777 105 {
ysr@777 106 _g1h = G1CollectedHeap::heap();
ysr@777 107 _ctbs = (CardTableModRefBS*)_g1h->barrier_set();
ysr@777 108 for (int i = 0; i < 256; i++) _histo[i] = 0;
ysr@777 109 }
ysr@777 110 bool do_card_ptr(jbyte* card_ptr, int worker_i) {
ysr@777 111 if (_g1h->is_in_reserved(_ctbs->addr_for(card_ptr))) {
ysr@777 112 _calls++;
ysr@777 113 unsigned char* ujb = (unsigned char*)card_ptr;
ysr@777 114 int ind = (int)(*ujb);
ysr@777 115 _histo[ind]++;
ysr@777 116 *card_ptr = -1;
ysr@777 117 }
ysr@777 118 return true;
ysr@777 119 }
ysr@777 120 int calls() { return _calls; }
ysr@777 121 void print_histo() {
ysr@777 122 gclog_or_tty->print_cr("Card table value histogram:");
ysr@777 123 for (int i = 0; i < 256; i++) {
ysr@777 124 if (_histo[i] != 0) {
ysr@777 125 gclog_or_tty->print_cr(" %d: %d", i, _histo[i]);
ysr@777 126 }
ysr@777 127 }
ysr@777 128 }
ysr@777 129 };
ysr@777 130
ysr@777 131 class RedirtyLoggedCardTableEntryClosure: public CardTableEntryClosure {
ysr@777 132 int _calls;
ysr@777 133 G1CollectedHeap* _g1h;
ysr@777 134 CardTableModRefBS* _ctbs;
ysr@777 135 public:
ysr@777 136 RedirtyLoggedCardTableEntryClosure() :
ysr@777 137 _calls(0)
ysr@777 138 {
ysr@777 139 _g1h = G1CollectedHeap::heap();
ysr@777 140 _ctbs = (CardTableModRefBS*)_g1h->barrier_set();
ysr@777 141 }
ysr@777 142 bool do_card_ptr(jbyte* card_ptr, int worker_i) {
ysr@777 143 if (_g1h->is_in_reserved(_ctbs->addr_for(card_ptr))) {
ysr@777 144 _calls++;
ysr@777 145 *card_ptr = 0;
ysr@777 146 }
ysr@777 147 return true;
ysr@777 148 }
ysr@777 149 int calls() { return _calls; }
ysr@777 150 };
ysr@777 151
iveresov@1051 152 class RedirtyLoggedCardTableEntryFastClosure : public CardTableEntryClosure {
iveresov@1051 153 public:
iveresov@1051 154 bool do_card_ptr(jbyte* card_ptr, int worker_i) {
iveresov@1051 155 *card_ptr = CardTableModRefBS::dirty_card_val();
iveresov@1051 156 return true;
iveresov@1051 157 }
iveresov@1051 158 };
iveresov@1051 159
ysr@777 160 YoungList::YoungList(G1CollectedHeap* g1h)
ysr@777 161 : _g1h(g1h), _head(NULL),
johnc@1829 162 _length(0),
ysr@777 163 _last_sampled_rs_lengths(0),
apetrusenko@980 164 _survivor_head(NULL), _survivor_tail(NULL), _survivor_length(0)
ysr@777 165 {
ysr@777 166 guarantee( check_list_empty(false), "just making sure..." );
ysr@777 167 }
ysr@777 168
ysr@777 169 void YoungList::push_region(HeapRegion *hr) {
ysr@777 170 assert(!hr->is_young(), "should not already be young");
ysr@777 171 assert(hr->get_next_young_region() == NULL, "cause it should!");
ysr@777 172
ysr@777 173 hr->set_next_young_region(_head);
ysr@777 174 _head = hr;
ysr@777 175
ysr@777 176 hr->set_young();
ysr@777 177 double yg_surv_rate = _g1h->g1_policy()->predict_yg_surv_rate((int)_length);
ysr@777 178 ++_length;
ysr@777 179 }
ysr@777 180
ysr@777 181 void YoungList::add_survivor_region(HeapRegion* hr) {
apetrusenko@980 182 assert(hr->is_survivor(), "should be flagged as survivor region");
ysr@777 183 assert(hr->get_next_young_region() == NULL, "cause it should!");
ysr@777 184
ysr@777 185 hr->set_next_young_region(_survivor_head);
ysr@777 186 if (_survivor_head == NULL) {
apetrusenko@980 187 _survivor_tail = hr;
ysr@777 188 }
ysr@777 189 _survivor_head = hr;
ysr@777 190
ysr@777 191 ++_survivor_length;
ysr@777 192 }
ysr@777 193
ysr@777 194 void YoungList::empty_list(HeapRegion* list) {
ysr@777 195 while (list != NULL) {
ysr@777 196 HeapRegion* next = list->get_next_young_region();
ysr@777 197 list->set_next_young_region(NULL);
ysr@777 198 list->uninstall_surv_rate_group();
ysr@777 199 list->set_not_young();
ysr@777 200 list = next;
ysr@777 201 }
ysr@777 202 }
ysr@777 203
ysr@777 204 void YoungList::empty_list() {
ysr@777 205 assert(check_list_well_formed(), "young list should be well formed");
ysr@777 206
ysr@777 207 empty_list(_head);
ysr@777 208 _head = NULL;
ysr@777 209 _length = 0;
ysr@777 210
ysr@777 211 empty_list(_survivor_head);
ysr@777 212 _survivor_head = NULL;
apetrusenko@980 213 _survivor_tail = NULL;
ysr@777 214 _survivor_length = 0;
ysr@777 215
ysr@777 216 _last_sampled_rs_lengths = 0;
ysr@777 217
ysr@777 218 assert(check_list_empty(false), "just making sure...");
ysr@777 219 }
ysr@777 220
ysr@777 221 bool YoungList::check_list_well_formed() {
ysr@777 222 bool ret = true;
ysr@777 223
ysr@777 224 size_t length = 0;
ysr@777 225 HeapRegion* curr = _head;
ysr@777 226 HeapRegion* last = NULL;
ysr@777 227 while (curr != NULL) {
johnc@1829 228 if (!curr->is_young()) {
ysr@777 229 gclog_or_tty->print_cr("### YOUNG REGION "PTR_FORMAT"-"PTR_FORMAT" "
johnc@1829 230 "incorrectly tagged (y: %d, surv: %d)",
ysr@777 231 curr->bottom(), curr->end(),
johnc@1829 232 curr->is_young(), curr->is_survivor());
ysr@777 233 ret = false;
ysr@777 234 }
ysr@777 235 ++length;
ysr@777 236 last = curr;
ysr@777 237 curr = curr->get_next_young_region();
ysr@777 238 }
ysr@777 239 ret = ret && (length == _length);
ysr@777 240
ysr@777 241 if (!ret) {
ysr@777 242 gclog_or_tty->print_cr("### YOUNG LIST seems not well formed!");
ysr@777 243 gclog_or_tty->print_cr("### list has %d entries, _length is %d",
ysr@777 244 length, _length);
ysr@777 245 }
ysr@777 246
johnc@1829 247 return ret;
ysr@777 248 }
ysr@777 249
johnc@1829 250 bool YoungList::check_list_empty(bool check_sample) {
ysr@777 251 bool ret = true;
ysr@777 252
ysr@777 253 if (_length != 0) {
ysr@777 254 gclog_or_tty->print_cr("### YOUNG LIST should have 0 length, not %d",
ysr@777 255 _length);
ysr@777 256 ret = false;
ysr@777 257 }
ysr@777 258 if (check_sample && _last_sampled_rs_lengths != 0) {
ysr@777 259 gclog_or_tty->print_cr("### YOUNG LIST has non-zero last sampled RS lengths");
ysr@777 260 ret = false;
ysr@777 261 }
ysr@777 262 if (_head != NULL) {
ysr@777 263 gclog_or_tty->print_cr("### YOUNG LIST does not have a NULL head");
ysr@777 264 ret = false;
ysr@777 265 }
ysr@777 266 if (!ret) {
ysr@777 267 gclog_or_tty->print_cr("### YOUNG LIST does not seem empty");
ysr@777 268 }
ysr@777 269
johnc@1829 270 return ret;
ysr@777 271 }
ysr@777 272
ysr@777 273 void
ysr@777 274 YoungList::rs_length_sampling_init() {
ysr@777 275 _sampled_rs_lengths = 0;
ysr@777 276 _curr = _head;
ysr@777 277 }
ysr@777 278
ysr@777 279 bool
ysr@777 280 YoungList::rs_length_sampling_more() {
ysr@777 281 return _curr != NULL;
ysr@777 282 }
ysr@777 283
ysr@777 284 void
ysr@777 285 YoungList::rs_length_sampling_next() {
ysr@777 286 assert( _curr != NULL, "invariant" );
johnc@1829 287 size_t rs_length = _curr->rem_set()->occupied();
johnc@1829 288
johnc@1829 289 _sampled_rs_lengths += rs_length;
johnc@1829 290
johnc@1829 291 // The current region may not yet have been added to the
johnc@1829 292 // incremental collection set (it gets added when it is
johnc@1829 293 // retired as the current allocation region).
johnc@1829 294 if (_curr->in_collection_set()) {
johnc@1829 295 // Update the collection set policy information for this region
johnc@1829 296 _g1h->g1_policy()->update_incremental_cset_info(_curr, rs_length);
johnc@1829 297 }
johnc@1829 298
ysr@777 299 _curr = _curr->get_next_young_region();
ysr@777 300 if (_curr == NULL) {
ysr@777 301 _last_sampled_rs_lengths = _sampled_rs_lengths;
ysr@777 302 // gclog_or_tty->print_cr("last sampled RS lengths = %d", _last_sampled_rs_lengths);
ysr@777 303 }
ysr@777 304 }
ysr@777 305
ysr@777 306 void
ysr@777 307 YoungList::reset_auxilary_lists() {
ysr@777 308 guarantee( is_empty(), "young list should be empty" );
ysr@777 309 assert(check_list_well_formed(), "young list should be well formed");
ysr@777 310
ysr@777 311 // Add survivor regions to SurvRateGroup.
ysr@777 312 _g1h->g1_policy()->note_start_adding_survivor_regions();
apetrusenko@980 313 _g1h->g1_policy()->finished_recalculating_age_indexes(true /* is_survivors */);
johnc@1829 314
ysr@777 315 for (HeapRegion* curr = _survivor_head;
ysr@777 316 curr != NULL;
ysr@777 317 curr = curr->get_next_young_region()) {
ysr@777 318 _g1h->g1_policy()->set_region_survivors(curr);
johnc@1829 319
johnc@1829 320 // The region is a non-empty survivor so let's add it to
johnc@1829 321 // the incremental collection set for the next evacuation
johnc@1829 322 // pause.
johnc@1829 323 _g1h->g1_policy()->add_region_to_incremental_cset_rhs(curr);
ysr@777 324 }
ysr@777 325 _g1h->g1_policy()->note_stop_adding_survivor_regions();
ysr@777 326
johnc@1829 327 _head = _survivor_head;
johnc@1829 328 _length = _survivor_length;
ysr@777 329 if (_survivor_head != NULL) {
johnc@1829 330 assert(_survivor_tail != NULL, "cause it shouldn't be");
johnc@1829 331 assert(_survivor_length > 0, "invariant");
johnc@1829 332 _survivor_tail->set_next_young_region(NULL);
johnc@1829 333 }
johnc@1829 334
johnc@1829 335 // Don't clear the survivor list handles until the start of
johnc@1829 336 // the next evacuation pause - we need it in order to re-tag
johnc@1829 337 // the survivor regions from this evacuation pause as 'young'
johnc@1829 338 // at the start of the next.
johnc@1829 339
apetrusenko@980 340 _g1h->g1_policy()->finished_recalculating_age_indexes(false /* is_survivors */);
ysr@777 341
ysr@777 342 assert(check_list_well_formed(), "young list should be well formed");
ysr@777 343 }
ysr@777 344
ysr@777 345 void YoungList::print() {
johnc@1829 346 HeapRegion* lists[] = {_head, _survivor_head};
johnc@1829 347 const char* names[] = {"YOUNG", "SURVIVOR"};
ysr@777 348
ysr@777 349 for (unsigned int list = 0; list < ARRAY_SIZE(lists); ++list) {
ysr@777 350 gclog_or_tty->print_cr("%s LIST CONTENTS", names[list]);
ysr@777 351 HeapRegion *curr = lists[list];
ysr@777 352 if (curr == NULL)
ysr@777 353 gclog_or_tty->print_cr(" empty");
ysr@777 354 while (curr != NULL) {
ysr@777 355 gclog_or_tty->print_cr(" [%08x-%08x], t: %08x, P: %08x, N: %08x, C: %08x, "
johnc@1829 356 "age: %4d, y: %d, surv: %d",
ysr@777 357 curr->bottom(), curr->end(),
ysr@777 358 curr->top(),
ysr@777 359 curr->prev_top_at_mark_start(),
ysr@777 360 curr->next_top_at_mark_start(),
ysr@777 361 curr->top_at_conc_mark_count(),
ysr@777 362 curr->age_in_surv_rate_group_cond(),
ysr@777 363 curr->is_young(),
ysr@777 364 curr->is_survivor());
ysr@777 365 curr = curr->get_next_young_region();
ysr@777 366 }
ysr@777 367 }
ysr@777 368
ysr@777 369 gclog_or_tty->print_cr("");
ysr@777 370 }
ysr@777 371
apetrusenko@1231 372 void G1CollectedHeap::push_dirty_cards_region(HeapRegion* hr)
apetrusenko@1231 373 {
apetrusenko@1231 374 // Claim the right to put the region on the dirty cards region list
apetrusenko@1231 375 // by installing a self pointer.
apetrusenko@1231 376 HeapRegion* next = hr->get_next_dirty_cards_region();
apetrusenko@1231 377 if (next == NULL) {
apetrusenko@1231 378 HeapRegion* res = (HeapRegion*)
apetrusenko@1231 379 Atomic::cmpxchg_ptr(hr, hr->next_dirty_cards_region_addr(),
apetrusenko@1231 380 NULL);
apetrusenko@1231 381 if (res == NULL) {
apetrusenko@1231 382 HeapRegion* head;
apetrusenko@1231 383 do {
apetrusenko@1231 384 // Put the region to the dirty cards region list.
apetrusenko@1231 385 head = _dirty_cards_region_list;
apetrusenko@1231 386 next = (HeapRegion*)
apetrusenko@1231 387 Atomic::cmpxchg_ptr(hr, &_dirty_cards_region_list, head);
apetrusenko@1231 388 if (next == head) {
apetrusenko@1231 389 assert(hr->get_next_dirty_cards_region() == hr,
apetrusenko@1231 390 "hr->get_next_dirty_cards_region() != hr");
apetrusenko@1231 391 if (next == NULL) {
apetrusenko@1231 392 // The last region in the list points to itself.
apetrusenko@1231 393 hr->set_next_dirty_cards_region(hr);
apetrusenko@1231 394 } else {
apetrusenko@1231 395 hr->set_next_dirty_cards_region(next);
apetrusenko@1231 396 }
apetrusenko@1231 397 }
apetrusenko@1231 398 } while (next != head);
apetrusenko@1231 399 }
apetrusenko@1231 400 }
apetrusenko@1231 401 }
apetrusenko@1231 402
apetrusenko@1231 403 HeapRegion* G1CollectedHeap::pop_dirty_cards_region()
apetrusenko@1231 404 {
apetrusenko@1231 405 HeapRegion* head;
apetrusenko@1231 406 HeapRegion* hr;
apetrusenko@1231 407 do {
apetrusenko@1231 408 head = _dirty_cards_region_list;
apetrusenko@1231 409 if (head == NULL) {
apetrusenko@1231 410 return NULL;
apetrusenko@1231 411 }
apetrusenko@1231 412 HeapRegion* new_head = head->get_next_dirty_cards_region();
apetrusenko@1231 413 if (head == new_head) {
apetrusenko@1231 414 // The last region.
apetrusenko@1231 415 new_head = NULL;
apetrusenko@1231 416 }
apetrusenko@1231 417 hr = (HeapRegion*)Atomic::cmpxchg_ptr(new_head, &_dirty_cards_region_list,
apetrusenko@1231 418 head);
apetrusenko@1231 419 } while (hr != head);
apetrusenko@1231 420 assert(hr != NULL, "invariant");
apetrusenko@1231 421 hr->set_next_dirty_cards_region(NULL);
apetrusenko@1231 422 return hr;
apetrusenko@1231 423 }
apetrusenko@1231 424
ysr@777 425 void G1CollectedHeap::stop_conc_gc_threads() {
iveresov@1229 426 _cg1r->stop();
ysr@777 427 _cmThread->stop();
ysr@777 428 }
ysr@777 429
ysr@777 430 void G1CollectedHeap::check_ct_logs_at_safepoint() {
ysr@777 431 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
ysr@777 432 CardTableModRefBS* ct_bs = (CardTableModRefBS*)barrier_set();
ysr@777 433
ysr@777 434 // Count the dirty cards at the start.
ysr@777 435 CountNonCleanMemRegionClosure count1(this);
ysr@777 436 ct_bs->mod_card_iterate(&count1);
ysr@777 437 int orig_count = count1.n();
ysr@777 438
ysr@777 439 // First clear the logged cards.
ysr@777 440 ClearLoggedCardTableEntryClosure clear;
ysr@777 441 dcqs.set_closure(&clear);
ysr@777 442 dcqs.apply_closure_to_all_completed_buffers();
ysr@777 443 dcqs.iterate_closure_all_threads(false);
ysr@777 444 clear.print_histo();
ysr@777 445
ysr@777 446 // Now ensure that there's no dirty cards.
ysr@777 447 CountNonCleanMemRegionClosure count2(this);
ysr@777 448 ct_bs->mod_card_iterate(&count2);
ysr@777 449 if (count2.n() != 0) {
ysr@777 450 gclog_or_tty->print_cr("Card table has %d entries; %d originally",
ysr@777 451 count2.n(), orig_count);
ysr@777 452 }
ysr@777 453 guarantee(count2.n() == 0, "Card table should be clean.");
ysr@777 454
ysr@777 455 RedirtyLoggedCardTableEntryClosure redirty;
ysr@777 456 JavaThread::dirty_card_queue_set().set_closure(&redirty);
ysr@777 457 dcqs.apply_closure_to_all_completed_buffers();
ysr@777 458 dcqs.iterate_closure_all_threads(false);
ysr@777 459 gclog_or_tty->print_cr("Log entries = %d, dirty cards = %d.",
ysr@777 460 clear.calls(), orig_count);
ysr@777 461 guarantee(redirty.calls() == clear.calls(),
ysr@777 462 "Or else mechanism is broken.");
ysr@777 463
ysr@777 464 CountNonCleanMemRegionClosure count3(this);
ysr@777 465 ct_bs->mod_card_iterate(&count3);
ysr@777 466 if (count3.n() != orig_count) {
ysr@777 467 gclog_or_tty->print_cr("Should have restored them all: orig = %d, final = %d.",
ysr@777 468 orig_count, count3.n());
ysr@777 469 guarantee(count3.n() >= orig_count, "Should have restored them all.");
ysr@777 470 }
ysr@777 471
ysr@777 472 JavaThread::dirty_card_queue_set().set_closure(_refine_cte_cl);
ysr@777 473 }
ysr@777 474
ysr@777 475 // Private class members.
ysr@777 476
ysr@777 477 G1CollectedHeap* G1CollectedHeap::_g1h;
ysr@777 478
ysr@777 479 // Private methods.
ysr@777 480
tonyp@2472 481 HeapRegion*
tonyp@2472 482 G1CollectedHeap::new_region_try_secondary_free_list(size_t word_size) {
tonyp@2472 483 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 484 while (!_secondary_free_list.is_empty() || free_regions_coming()) {
tonyp@2472 485 if (!_secondary_free_list.is_empty()) {
tonyp@2472 486 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 487 gclog_or_tty->print_cr("G1ConcRegionFreeing [region alloc] : "
tonyp@2472 488 "secondary_free_list has "SIZE_FORMAT" entries",
tonyp@2472 489 _secondary_free_list.length());
tonyp@2472 490 }
tonyp@2472 491 // It looks as if there are free regions available on the
tonyp@2472 492 // secondary_free_list. Let's move them to the free_list and try
tonyp@2472 493 // again to allocate from it.
tonyp@2472 494 append_secondary_free_list();
tonyp@2472 495
tonyp@2472 496 assert(!_free_list.is_empty(), "if the secondary_free_list was not "
tonyp@2472 497 "empty we should have moved at least one entry to the free_list");
tonyp@2472 498 HeapRegion* res = _free_list.remove_head();
tonyp@2472 499 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 500 gclog_or_tty->print_cr("G1ConcRegionFreeing [region alloc] : "
tonyp@2472 501 "allocated "HR_FORMAT" from secondary_free_list",
tonyp@2472 502 HR_FORMAT_PARAMS(res));
tonyp@2472 503 }
tonyp@2472 504 return res;
tonyp@2472 505 }
tonyp@2472 506
tonyp@2472 507 // Wait here until we get notifed either when (a) there are no
tonyp@2472 508 // more free regions coming or (b) some regions have been moved on
tonyp@2472 509 // the secondary_free_list.
tonyp@2472 510 SecondaryFreeList_lock->wait(Mutex::_no_safepoint_check_flag);
tonyp@2472 511 }
tonyp@2472 512
tonyp@2472 513 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 514 gclog_or_tty->print_cr("G1ConcRegionFreeing [region alloc] : "
tonyp@2472 515 "could not allocate from secondary_free_list");
tonyp@2472 516 }
tonyp@2472 517 return NULL;
tonyp@2472 518 }
tonyp@2472 519
tonyp@2472 520 HeapRegion* G1CollectedHeap::new_region_work(size_t word_size,
tonyp@2472 521 bool do_expand) {
tonyp@2472 522 assert(!isHumongous(word_size) ||
tonyp@2472 523 word_size <= (size_t) HeapRegion::GrainWords,
tonyp@2472 524 "the only time we use this to allocate a humongous region is "
tonyp@2472 525 "when we are allocating a single humongous region");
tonyp@2472 526
tonyp@2472 527 HeapRegion* res;
tonyp@2472 528 if (G1StressConcRegionFreeing) {
tonyp@2472 529 if (!_secondary_free_list.is_empty()) {
tonyp@2472 530 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 531 gclog_or_tty->print_cr("G1ConcRegionFreeing [region alloc] : "
tonyp@2472 532 "forced to look at the secondary_free_list");
tonyp@2472 533 }
tonyp@2472 534 res = new_region_try_secondary_free_list(word_size);
tonyp@2472 535 if (res != NULL) {
tonyp@2472 536 return res;
tonyp@2472 537 }
tonyp@2472 538 }
tonyp@2472 539 }
tonyp@2472 540 res = _free_list.remove_head_or_null();
tonyp@2472 541 if (res == NULL) {
tonyp@2472 542 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 543 gclog_or_tty->print_cr("G1ConcRegionFreeing [region alloc] : "
tonyp@2472 544 "res == NULL, trying the secondary_free_list");
tonyp@2472 545 }
tonyp@2472 546 res = new_region_try_secondary_free_list(word_size);
tonyp@2472 547 }
ysr@777 548 if (res == NULL && do_expand) {
johnc@2504 549 if (expand(word_size * HeapWordSize)) {
johnc@2504 550 // The expansion succeeded and so we should have at least one
johnc@2504 551 // region on the free list.
johnc@2504 552 res = _free_list.remove_head();
johnc@2504 553 }
ysr@777 554 }
apetrusenko@1900 555 if (res != NULL) {
apetrusenko@1900 556 if (G1PrintHeapRegions) {
tonyp@2472 557 gclog_or_tty->print_cr("new alloc region %d:["PTR_FORMAT","PTR_FORMAT"], "
tonyp@2472 558 "top "PTR_FORMAT, res->hrs_index(),
tonyp@2472 559 res->bottom(), res->end(), res->top());
ysr@777 560 }
ysr@777 561 }
ysr@777 562 return res;
ysr@777 563 }
ysr@777 564
tonyp@2472 565 HeapRegion* G1CollectedHeap::new_gc_alloc_region(int purpose,
tonyp@2472 566 size_t word_size) {
ysr@777 567 HeapRegion* alloc_region = NULL;
ysr@777 568 if (_gc_alloc_region_counts[purpose] < g1_policy()->max_regions(purpose)) {
tonyp@2472 569 alloc_region = new_region_work(word_size, true /* do_expand */);
ysr@777 570 if (purpose == GCAllocForSurvived && alloc_region != NULL) {
apetrusenko@980 571 alloc_region->set_survivor();
ysr@777 572 }
ysr@777 573 ++_gc_alloc_region_counts[purpose];
ysr@777 574 } else {
ysr@777 575 g1_policy()->note_alloc_region_limit_reached(purpose);
ysr@777 576 }
ysr@777 577 return alloc_region;
ysr@777 578 }
ysr@777 579
tonyp@2472 580 int G1CollectedHeap::humongous_obj_allocate_find_first(size_t num_regions,
tonyp@2472 581 size_t word_size) {
tonyp@2472 582 int first = -1;
tonyp@2472 583 if (num_regions == 1) {
tonyp@2472 584 // Only one region to allocate, no need to go through the slower
tonyp@2472 585 // path. The caller will attempt the expasion if this fails, so
tonyp@2472 586 // let's not try to expand here too.
tonyp@2472 587 HeapRegion* hr = new_region_work(word_size, false /* do_expand */);
tonyp@2472 588 if (hr != NULL) {
tonyp@2472 589 first = hr->hrs_index();
tonyp@2472 590 } else {
tonyp@2472 591 first = -1;
tonyp@2472 592 }
tonyp@2472 593 } else {
tonyp@2472 594 // We can't allocate humongous regions while cleanupComplete() is
tonyp@2472 595 // running, since some of the regions we find to be empty might not
tonyp@2472 596 // yet be added to the free list and it is not straightforward to
tonyp@2472 597 // know which list they are on so that we can remove them. Note
tonyp@2472 598 // that we only need to do this if we need to allocate more than
tonyp@2472 599 // one region to satisfy the current humongous allocation
tonyp@2472 600 // request. If we are only allocating one region we use the common
tonyp@2472 601 // region allocation code (see above).
tonyp@2472 602 wait_while_free_regions_coming();
tonyp@2472 603 append_secondary_free_list_if_not_empty();
tonyp@2472 604
tonyp@2472 605 if (free_regions() >= num_regions) {
tonyp@2472 606 first = _hrs->find_contiguous(num_regions);
tonyp@2472 607 if (first != -1) {
tonyp@2472 608 for (int i = first; i < first + (int) num_regions; ++i) {
tonyp@2472 609 HeapRegion* hr = _hrs->at(i);
tonyp@2472 610 assert(hr->is_empty(), "sanity");
tonyp@2472 611 assert(is_on_free_list(hr), "sanity");
tonyp@2472 612 hr->set_pending_removal(true);
tonyp@2472 613 }
tonyp@2472 614 _free_list.remove_all_pending(num_regions);
tonyp@2472 615 }
tonyp@2472 616 }
tonyp@2472 617 }
tonyp@2472 618 return first;
tonyp@2472 619 }
tonyp@2472 620
ysr@777 621 // If could fit into free regions w/o expansion, try.
ysr@777 622 // Otherwise, if can expand, do so.
ysr@777 623 // Otherwise, if using ex regions might help, try with ex given back.
tonyp@2315 624 HeapWord* G1CollectedHeap::humongous_obj_allocate(size_t word_size) {
tonyp@2472 625 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
tonyp@2472 626
tonyp@2472 627 verify_region_sets_optional();
ysr@777 628
ysr@777 629 size_t num_regions =
tonyp@2315 630 round_to(word_size, HeapRegion::GrainWords) / HeapRegion::GrainWords;
ysr@777 631 size_t x_size = expansion_regions();
tonyp@2472 632 size_t fs = _hrs->free_suffix();
tonyp@2472 633 int first = humongous_obj_allocate_find_first(num_regions, word_size);
tonyp@2472 634 if (first == -1) {
tonyp@2472 635 // The only thing we can do now is attempt expansion.
ysr@777 636 if (fs + x_size >= num_regions) {
johnc@2504 637 // If the number of regions we're trying to allocate for this
johnc@2504 638 // object is at most the number of regions in the free suffix,
johnc@2504 639 // then the call to humongous_obj_allocate_find_first() above
johnc@2504 640 // should have succeeded and we wouldn't be here.
johnc@2504 641 //
johnc@2504 642 // We should only be trying to expand when the free suffix is
johnc@2504 643 // not sufficient for the object _and_ we have some expansion
johnc@2504 644 // room available.
johnc@2504 645 assert(num_regions > fs, "earlier allocation should have succeeded");
johnc@2504 646
johnc@2504 647 if (expand((num_regions - fs) * HeapRegion::GrainBytes)) {
johnc@2504 648 first = humongous_obj_allocate_find_first(num_regions, word_size);
johnc@2504 649 // If the expansion was successful then the allocation
johnc@2504 650 // should have been successful.
johnc@2504 651 assert(first != -1, "this should have worked");
johnc@2504 652 }
tonyp@2472 653 }
tonyp@2472 654 }
tonyp@2472 655
tonyp@2472 656 if (first != -1) {
tonyp@2472 657 // Index of last region in the series + 1.
tonyp@2472 658 int last = first + (int) num_regions;
tonyp@2472 659
tonyp@2472 660 // We need to initialize the region(s) we just discovered. This is
tonyp@2472 661 // a bit tricky given that it can happen concurrently with
tonyp@2472 662 // refinement threads refining cards on these regions and
tonyp@2472 663 // potentially wanting to refine the BOT as they are scanning
tonyp@2472 664 // those cards (this can happen shortly after a cleanup; see CR
tonyp@2472 665 // 6991377). So we have to set up the region(s) carefully and in
tonyp@2472 666 // a specific order.
tonyp@2472 667
tonyp@2472 668 // The word size sum of all the regions we will allocate.
tonyp@2472 669 size_t word_size_sum = num_regions * HeapRegion::GrainWords;
tonyp@2472 670 assert(word_size <= word_size_sum, "sanity");
tonyp@2472 671
tonyp@2472 672 // This will be the "starts humongous" region.
tonyp@2472 673 HeapRegion* first_hr = _hrs->at(first);
tonyp@2472 674 // The header of the new object will be placed at the bottom of
tonyp@2472 675 // the first region.
tonyp@2472 676 HeapWord* new_obj = first_hr->bottom();
tonyp@2472 677 // This will be the new end of the first region in the series that
tonyp@2472 678 // should also match the end of the last region in the seriers.
tonyp@2472 679 HeapWord* new_end = new_obj + word_size_sum;
tonyp@2472 680 // This will be the new top of the first region that will reflect
tonyp@2472 681 // this allocation.
tonyp@2472 682 HeapWord* new_top = new_obj + word_size;
tonyp@2472 683
tonyp@2472 684 // First, we need to zero the header of the space that we will be
tonyp@2472 685 // allocating. When we update top further down, some refinement
tonyp@2472 686 // threads might try to scan the region. By zeroing the header we
tonyp@2472 687 // ensure that any thread that will try to scan the region will
tonyp@2472 688 // come across the zero klass word and bail out.
tonyp@2472 689 //
tonyp@2472 690 // NOTE: It would not have been correct to have used
tonyp@2472 691 // CollectedHeap::fill_with_object() and make the space look like
tonyp@2472 692 // an int array. The thread that is doing the allocation will
tonyp@2472 693 // later update the object header to a potentially different array
tonyp@2472 694 // type and, for a very short period of time, the klass and length
tonyp@2472 695 // fields will be inconsistent. This could cause a refinement
tonyp@2472 696 // thread to calculate the object size incorrectly.
tonyp@2472 697 Copy::fill_to_words(new_obj, oopDesc::header_size(), 0);
tonyp@2472 698
tonyp@2472 699 // We will set up the first region as "starts humongous". This
tonyp@2472 700 // will also update the BOT covering all the regions to reflect
tonyp@2472 701 // that there is a single object that starts at the bottom of the
tonyp@2472 702 // first region.
tonyp@2472 703 first_hr->set_startsHumongous(new_top, new_end);
tonyp@2472 704
tonyp@2472 705 // Then, if there are any, we will set up the "continues
tonyp@2472 706 // humongous" regions.
tonyp@2472 707 HeapRegion* hr = NULL;
tonyp@2472 708 for (int i = first + 1; i < last; ++i) {
tonyp@2472 709 hr = _hrs->at(i);
tonyp@2472 710 hr->set_continuesHumongous(first_hr);
tonyp@2472 711 }
tonyp@2472 712 // If we have "continues humongous" regions (hr != NULL), then the
tonyp@2472 713 // end of the last one should match new_end.
tonyp@2472 714 assert(hr == NULL || hr->end() == new_end, "sanity");
tonyp@2472 715
tonyp@2472 716 // Up to this point no concurrent thread would have been able to
tonyp@2472 717 // do any scanning on any region in this series. All the top
tonyp@2472 718 // fields still point to bottom, so the intersection between
tonyp@2472 719 // [bottom,top] and [card_start,card_end] will be empty. Before we
tonyp@2472 720 // update the top fields, we'll do a storestore to make sure that
tonyp@2472 721 // no thread sees the update to top before the zeroing of the
tonyp@2472 722 // object header and the BOT initialization.
tonyp@2472 723 OrderAccess::storestore();
tonyp@2472 724
tonyp@2472 725 // Now that the BOT and the object header have been initialized,
tonyp@2472 726 // we can update top of the "starts humongous" region.
tonyp@2472 727 assert(first_hr->bottom() < new_top && new_top <= first_hr->end(),
tonyp@2472 728 "new_top should be in this region");
tonyp@2472 729 first_hr->set_top(new_top);
tonyp@2472 730
tonyp@2472 731 // Now, we will update the top fields of the "continues humongous"
tonyp@2472 732 // regions. The reason we need to do this is that, otherwise,
tonyp@2472 733 // these regions would look empty and this will confuse parts of
tonyp@2472 734 // G1. For example, the code that looks for a consecutive number
tonyp@2472 735 // of empty regions will consider them empty and try to
tonyp@2472 736 // re-allocate them. We can extend is_empty() to also include
tonyp@2472 737 // !continuesHumongous(), but it is easier to just update the top
tonyp@2472 738 // fields here. The way we set top for all regions (i.e., top ==
tonyp@2472 739 // end for all regions but the last one, top == new_top for the
tonyp@2472 740 // last one) is actually used when we will free up the humongous
tonyp@2472 741 // region in free_humongous_region().
tonyp@2472 742 hr = NULL;
tonyp@2472 743 for (int i = first + 1; i < last; ++i) {
tonyp@2472 744 hr = _hrs->at(i);
tonyp@2472 745 if ((i + 1) == last) {
tonyp@2472 746 // last continues humongous region
tonyp@2472 747 assert(hr->bottom() < new_top && new_top <= hr->end(),
tonyp@2472 748 "new_top should fall on this region");
tonyp@2472 749 hr->set_top(new_top);
tonyp@2472 750 } else {
tonyp@2472 751 // not last one
tonyp@2472 752 assert(new_top > hr->end(), "new_top should be above this region");
tonyp@2472 753 hr->set_top(hr->end());
ysr@777 754 }
ysr@777 755 }
tonyp@2472 756 // If we have continues humongous regions (hr != NULL), then the
tonyp@2472 757 // end of the last one should match new_end and its top should
tonyp@2472 758 // match new_top.
tonyp@2472 759 assert(hr == NULL ||
tonyp@2472 760 (hr->end() == new_end && hr->top() == new_top), "sanity");
tonyp@2472 761
tonyp@2472 762 assert(first_hr->used() == word_size * HeapWordSize, "invariant");
tonyp@2472 763 _summary_bytes_used += first_hr->used();
tonyp@2472 764 _humongous_set.add(first_hr);
tonyp@2472 765
tonyp@2472 766 return new_obj;
tonyp@2472 767 }
tonyp@2472 768
tonyp@2472 769 verify_region_sets_optional();
tonyp@2472 770 return NULL;
ysr@777 771 }
ysr@777 772
tonyp@2315 773 void
tonyp@2315 774 G1CollectedHeap::retire_cur_alloc_region(HeapRegion* cur_alloc_region) {
tonyp@2454 775 // Other threads might still be trying to allocate using CASes out
tonyp@2454 776 // of the region we are retiring, as they can do so without holding
tonyp@2454 777 // the Heap_lock. So we first have to make sure that noone else can
tonyp@2454 778 // allocate in it by doing a maximal allocation. Even if our CAS
tonyp@2454 779 // attempt fails a few times, we'll succeed sooner or later given
tonyp@2454 780 // that a failed CAS attempt mean that the region is getting closed
tonyp@2454 781 // to being full (someone else succeeded in allocating into it).
tonyp@2454 782 size_t free_word_size = cur_alloc_region->free() / HeapWordSize;
tonyp@2454 783
tonyp@2454 784 // This is the minimum free chunk we can turn into a dummy
tonyp@2454 785 // object. If the free space falls below this, then noone can
tonyp@2454 786 // allocate in this region anyway (all allocation requests will be
tonyp@2454 787 // of a size larger than this) so we won't have to perform the dummy
tonyp@2454 788 // allocation.
tonyp@2454 789 size_t min_word_size_to_fill = CollectedHeap::min_fill_size();
tonyp@2454 790
tonyp@2454 791 while (free_word_size >= min_word_size_to_fill) {
tonyp@2454 792 HeapWord* dummy =
tonyp@2454 793 cur_alloc_region->par_allocate_no_bot_updates(free_word_size);
tonyp@2454 794 if (dummy != NULL) {
tonyp@2454 795 // If the allocation was successful we should fill in the space.
tonyp@2454 796 CollectedHeap::fill_with_object(dummy, free_word_size);
tonyp@2454 797 break;
tonyp@2454 798 }
tonyp@2454 799
tonyp@2454 800 free_word_size = cur_alloc_region->free() / HeapWordSize;
tonyp@2454 801 // It's also possible that someone else beats us to the
tonyp@2454 802 // allocation and they fill up the region. In that case, we can
tonyp@2454 803 // just get out of the loop
tonyp@2454 804 }
tonyp@2454 805 assert(cur_alloc_region->free() / HeapWordSize < min_word_size_to_fill,
tonyp@2454 806 "sanity");
tonyp@2454 807
tonyp@2315 808 retire_cur_alloc_region_common(cur_alloc_region);
tonyp@2315 809 assert(_cur_alloc_region == NULL, "post-condition");
tonyp@2315 810 }
tonyp@2315 811
tonyp@2315 812 // See the comment in the .hpp file about the locking protocol and
tonyp@2315 813 // assumptions of this method (and other related ones).
ysr@777 814 HeapWord*
tonyp@2315 815 G1CollectedHeap::replace_cur_alloc_region_and_allocate(size_t word_size,
tonyp@2315 816 bool at_safepoint,
tonyp@2333 817 bool do_dirtying,
tonyp@2333 818 bool can_expand) {
tonyp@2472 819 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
tonyp@2315 820 assert(_cur_alloc_region == NULL,
tonyp@2315 821 "replace_cur_alloc_region_and_allocate() should only be called "
tonyp@2315 822 "after retiring the previous current alloc region");
tonyp@2315 823 assert(SafepointSynchronize::is_at_safepoint() == at_safepoint,
tonyp@2315 824 "at_safepoint and is_at_safepoint() should be a tautology");
tonyp@2333 825 assert(!can_expand || g1_policy()->can_expand_young_list(),
tonyp@2333 826 "we should not call this method with can_expand == true if "
tonyp@2333 827 "we are not allowed to expand the young gen");
tonyp@2333 828
tonyp@2333 829 if (can_expand || !g1_policy()->is_young_list_full()) {
tonyp@2472 830 HeapRegion* new_cur_alloc_region = new_alloc_region(word_size);
tonyp@2315 831 if (new_cur_alloc_region != NULL) {
tonyp@2315 832 assert(new_cur_alloc_region->is_empty(),
tonyp@2315 833 "the newly-allocated region should be empty, "
tonyp@2315 834 "as right now we only allocate new regions out of the free list");
tonyp@2315 835 g1_policy()->update_region_num(true /* next_is_young */);
tonyp@2315 836 set_region_short_lived_locked(new_cur_alloc_region);
tonyp@2315 837
tonyp@2315 838 assert(!new_cur_alloc_region->isHumongous(),
tonyp@2315 839 "Catch a regression of this bug.");
tonyp@2315 840
tonyp@2315 841 // We need to ensure that the stores to _cur_alloc_region and,
tonyp@2315 842 // subsequently, to top do not float above the setting of the
tonyp@2315 843 // young type.
tonyp@2315 844 OrderAccess::storestore();
tonyp@2315 845
tonyp@2454 846 // Now, perform the allocation out of the region we just
tonyp@2454 847 // allocated. Note that noone else can access that region at
tonyp@2454 848 // this point (as _cur_alloc_region has not been updated yet),
tonyp@2454 849 // so we can just go ahead and do the allocation without any
tonyp@2454 850 // atomics (and we expect this allocation attempt to
tonyp@2454 851 // suceeded). Given that other threads can attempt an allocation
tonyp@2454 852 // with a CAS and without needing the Heap_lock, if we assigned
tonyp@2454 853 // the new region to _cur_alloc_region before first allocating
tonyp@2454 854 // into it other threads might have filled up the new region
tonyp@2454 855 // before we got a chance to do the allocation ourselves. In
tonyp@2454 856 // that case, we would have needed to retire the region, grab a
tonyp@2454 857 // new one, and go through all this again. Allocating out of the
tonyp@2454 858 // new region before assigning it to _cur_alloc_region avoids
tonyp@2454 859 // all this.
tonyp@2454 860 HeapWord* result =
tonyp@2454 861 new_cur_alloc_region->allocate_no_bot_updates(word_size);
tonyp@2315 862 assert(result != NULL, "we just allocate out of an empty region "
tonyp@2315 863 "so allocation should have been successful");
tonyp@2315 864 assert(is_in(result), "result should be in the heap");
tonyp@2315 865
tonyp@2454 866 // Now make sure that the store to _cur_alloc_region does not
tonyp@2454 867 // float above the store to top.
tonyp@2454 868 OrderAccess::storestore();
tonyp@2315 869 _cur_alloc_region = new_cur_alloc_region;
tonyp@2315 870
tonyp@2315 871 if (!at_safepoint) {
tonyp@2315 872 Heap_lock->unlock();
johnc@1829 873 }
tonyp@2315 874
tonyp@2315 875 // do the dirtying, if necessary, after we release the Heap_lock
tonyp@2315 876 if (do_dirtying) {
tonyp@2315 877 dirty_young_block(result, word_size);
tonyp@2315 878 }
tonyp@2315 879 return result;
ysr@777 880 }
tonyp@2315 881 }
tonyp@2315 882
tonyp@2315 883 assert(_cur_alloc_region == NULL, "we failed to allocate a new current "
tonyp@2315 884 "alloc region, it should still be NULL");
tonyp@2472 885 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
tonyp@2315 886 return NULL;
tonyp@2315 887 }
tonyp@2315 888
tonyp@2315 889 // See the comment in the .hpp file about the locking protocol and
tonyp@2315 890 // assumptions of this method (and other related ones).
tonyp@2315 891 HeapWord*
tonyp@2315 892 G1CollectedHeap::attempt_allocation_slow(size_t word_size) {
tonyp@2315 893 assert_heap_locked_and_not_at_safepoint();
tonyp@2315 894 assert(!isHumongous(word_size), "attempt_allocation_slow() should not be "
tonyp@2315 895 "used for humongous allocations");
tonyp@2315 896
tonyp@2472 897 // We should only reach here when we were unable to allocate
tonyp@2472 898 // otherwise. So, we should have not active current alloc region.
tonyp@2472 899 assert(_cur_alloc_region == NULL, "current alloc region should be NULL");
tonyp@2472 900
tonyp@2315 901 // We will loop while succeeded is false, which means that we tried
tonyp@2315 902 // to do a collection, but the VM op did not succeed. So, when we
tonyp@2315 903 // exit the loop, either one of the allocation attempts was
tonyp@2315 904 // successful, or we succeeded in doing the VM op but which was
tonyp@2315 905 // unable to allocate after the collection.
tonyp@2315 906 for (int try_count = 1; /* we'll return or break */; try_count += 1) {
tonyp@2315 907 bool succeeded = true;
tonyp@2315 908
tonyp@2454 909 // Every time we go round the loop we should be holding the Heap_lock.
tonyp@2454 910 assert_heap_locked();
tonyp@2454 911
tonyp@2315 912 if (GC_locker::is_active_and_needs_gc()) {
tonyp@2333 913 // We are locked out of GC because of the GC locker. We can
tonyp@2333 914 // allocate a new region only if we can expand the young gen.
tonyp@2333 915
tonyp@2333 916 if (g1_policy()->can_expand_young_list()) {
tonyp@2333 917 // Yes, we are allowed to expand the young gen. Let's try to
tonyp@2333 918 // allocate a new current alloc region.
tonyp@2333 919 HeapWord* result =
tonyp@2333 920 replace_cur_alloc_region_and_allocate(word_size,
tonyp@2333 921 false, /* at_safepoint */
tonyp@2333 922 true, /* do_dirtying */
tonyp@2333 923 true /* can_expand */);
tonyp@2333 924 if (result != NULL) {
tonyp@2333 925 assert_heap_not_locked();
tonyp@2333 926 return result;
tonyp@2333 927 }
tonyp@2333 928 }
tonyp@2333 929 // We could not expand the young gen further (or we could but we
tonyp@2333 930 // failed to allocate a new region). We'll stall until the GC
tonyp@2333 931 // locker forces a GC.
tonyp@2315 932
tonyp@2315 933 // If this thread is not in a jni critical section, we stall
tonyp@2315 934 // the requestor until the critical section has cleared and
tonyp@2315 935 // GC allowed. When the critical section clears, a GC is
tonyp@2315 936 // initiated by the last thread exiting the critical section; so
tonyp@2315 937 // we retry the allocation sequence from the beginning of the loop,
tonyp@2315 938 // rather than causing more, now probably unnecessary, GC attempts.
tonyp@2315 939 JavaThread* jthr = JavaThread::current();
tonyp@2315 940 assert(jthr != NULL, "sanity");
tonyp@2454 941 if (jthr->in_critical()) {
tonyp@2315 942 if (CheckJNICalls) {
tonyp@2315 943 fatal("Possible deadlock due to allocating while"
tonyp@2315 944 " in jni critical section");
tonyp@2315 945 }
tonyp@2454 946 // We are returning NULL so the protocol is that we're still
tonyp@2454 947 // holding the Heap_lock.
tonyp@2454 948 assert_heap_locked();
tonyp@2315 949 return NULL;
tonyp@2315 950 }
tonyp@2454 951
tonyp@2454 952 Heap_lock->unlock();
tonyp@2454 953 GC_locker::stall_until_clear();
tonyp@2454 954
tonyp@2454 955 // No need to relock the Heap_lock. We'll fall off to the code
tonyp@2454 956 // below the else-statement which assumes that we are not
tonyp@2454 957 // holding the Heap_lock.
tonyp@2315 958 } else {
tonyp@2315 959 // We are not locked out. So, let's try to do a GC. The VM op
tonyp@2315 960 // will retry the allocation before it completes.
tonyp@2315 961
tonyp@2315 962 // Read the GC count while holding the Heap_lock
tonyp@2315 963 unsigned int gc_count_before = SharedHeap::heap()->total_collections();
tonyp@2315 964
tonyp@2315 965 Heap_lock->unlock();
tonyp@2315 966
tonyp@2315 967 HeapWord* result =
tonyp@2315 968 do_collection_pause(word_size, gc_count_before, &succeeded);
tonyp@2315 969 assert_heap_not_locked();
tonyp@2315 970 if (result != NULL) {
tonyp@2315 971 assert(succeeded, "the VM op should have succeeded");
tonyp@2315 972
tonyp@2315 973 // Allocations that take place on VM operations do not do any
tonyp@2315 974 // card dirtying and we have to do it here.
tonyp@2315 975 dirty_young_block(result, word_size);
tonyp@2315 976 return result;
tonyp@2315 977 }
tonyp@2315 978 }
tonyp@2315 979
tonyp@2454 980 // Both paths that get us here from above unlock the Heap_lock.
tonyp@2454 981 assert_heap_not_locked();
tonyp@2315 982
tonyp@2315 983 // We can reach here when we were unsuccessful in doing a GC,
tonyp@2315 984 // because another thread beat us to it, or because we were locked
tonyp@2315 985 // out of GC due to the GC locker. In either case a new alloc
tonyp@2315 986 // region might be available so we will retry the allocation.
tonyp@2315 987 HeapWord* result = attempt_allocation(word_size);
tonyp@2315 988 if (result != NULL) {
tonyp@2315 989 assert_heap_not_locked();
tonyp@2315 990 return result;
tonyp@2315 991 }
tonyp@2315 992
tonyp@2315 993 // So far our attempts to allocate failed. The only time we'll go
tonyp@2315 994 // around the loop and try again is if we tried to do a GC and the
tonyp@2315 995 // VM op that we tried to schedule was not successful because
tonyp@2315 996 // another thread beat us to it. If that happened it's possible
tonyp@2315 997 // that by the time we grabbed the Heap_lock again and tried to
tonyp@2315 998 // allocate other threads filled up the young generation, which
tonyp@2315 999 // means that the allocation attempt after the GC also failed. So,
tonyp@2315 1000 // it's worth trying to schedule another GC pause.
tonyp@2315 1001 if (succeeded) {
tonyp@2315 1002 break;
tonyp@2315 1003 }
tonyp@2315 1004
tonyp@2315 1005 // Give a warning if we seem to be looping forever.
tonyp@2315 1006 if ((QueuedAllocationWarningCount > 0) &&
tonyp@2315 1007 (try_count % QueuedAllocationWarningCount == 0)) {
tonyp@2315 1008 warning("G1CollectedHeap::attempt_allocation_slow() "
tonyp@2315 1009 "retries %d times", try_count);
tonyp@2315 1010 }
tonyp@2315 1011 }
tonyp@2315 1012
tonyp@2315 1013 assert_heap_locked();
tonyp@2315 1014 return NULL;
tonyp@2315 1015 }
tonyp@2315 1016
tonyp@2315 1017 // See the comment in the .hpp file about the locking protocol and
tonyp@2315 1018 // assumptions of this method (and other related ones).
tonyp@2315 1019 HeapWord*
tonyp@2315 1020 G1CollectedHeap::attempt_allocation_humongous(size_t word_size,
tonyp@2315 1021 bool at_safepoint) {
tonyp@2315 1022 // This is the method that will allocate a humongous object. All
tonyp@2315 1023 // allocation paths that attempt to allocate a humongous object
tonyp@2315 1024 // should eventually reach here. Currently, the only paths are from
tonyp@2315 1025 // mem_allocate() and attempt_allocation_at_safepoint().
tonyp@2472 1026 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
tonyp@2315 1027 assert(isHumongous(word_size), "attempt_allocation_humongous() "
tonyp@2315 1028 "should only be used for humongous allocations");
tonyp@2315 1029 assert(SafepointSynchronize::is_at_safepoint() == at_safepoint,
tonyp@2315 1030 "at_safepoint and is_at_safepoint() should be a tautology");
tonyp@2315 1031
tonyp@2315 1032 HeapWord* result = NULL;
tonyp@2315 1033
tonyp@2315 1034 // We will loop while succeeded is false, which means that we tried
tonyp@2315 1035 // to do a collection, but the VM op did not succeed. So, when we
tonyp@2315 1036 // exit the loop, either one of the allocation attempts was
tonyp@2315 1037 // successful, or we succeeded in doing the VM op but which was
tonyp@2315 1038 // unable to allocate after the collection.
tonyp@2315 1039 for (int try_count = 1; /* we'll return or break */; try_count += 1) {
tonyp@2315 1040 bool succeeded = true;
tonyp@2315 1041
tonyp@2315 1042 // Given that humongous objects are not allocated in young
tonyp@2315 1043 // regions, we'll first try to do the allocation without doing a
tonyp@2315 1044 // collection hoping that there's enough space in the heap.
tonyp@2315 1045 result = humongous_obj_allocate(word_size);
ysr@777 1046 assert(_cur_alloc_region == NULL || !_cur_alloc_region->isHumongous(),
tonyp@2315 1047 "catch a regression of this bug.");
tonyp@2315 1048 if (result != NULL) {
tonyp@2315 1049 if (!at_safepoint) {
tonyp@2315 1050 // If we're not at a safepoint, unlock the Heap_lock.
tonyp@2315 1051 Heap_lock->unlock();
johnc@2021 1052 }
tonyp@2315 1053 return result;
ysr@777 1054 }
tonyp@2315 1055
tonyp@2315 1056 // If we failed to allocate the humongous object, we should try to
tonyp@2315 1057 // do a collection pause (if we're allowed) in case it reclaims
tonyp@2315 1058 // enough space for the allocation to succeed after the pause.
tonyp@2315 1059 if (!at_safepoint) {
tonyp@2315 1060 // Read the GC count while holding the Heap_lock
tonyp@2315 1061 unsigned int gc_count_before = SharedHeap::heap()->total_collections();
tonyp@2315 1062
tonyp@2315 1063 // If we're allowed to do a collection we're not at a
tonyp@2315 1064 // safepoint, so it is safe to unlock the Heap_lock.
ysr@777 1065 Heap_lock->unlock();
tonyp@2315 1066
tonyp@2315 1067 result = do_collection_pause(word_size, gc_count_before, &succeeded);
tonyp@2315 1068 assert_heap_not_locked();
tonyp@2315 1069 if (result != NULL) {
tonyp@2315 1070 assert(succeeded, "the VM op should have succeeded");
tonyp@2315 1071 return result;
tonyp@2315 1072 }
tonyp@2315 1073
tonyp@2315 1074 // If we get here, the VM operation either did not succeed
tonyp@2315 1075 // (i.e., another thread beat us to it) or it succeeded but
tonyp@2315 1076 // failed to allocate the object.
tonyp@2315 1077
tonyp@2315 1078 // If we're allowed to do a collection we're not at a
tonyp@2315 1079 // safepoint, so it is safe to lock the Heap_lock.
tonyp@2315 1080 Heap_lock->lock();
ysr@777 1081 }
ysr@777 1082
tonyp@2315 1083 assert(result == NULL, "otherwise we should have exited the loop earlier");
tonyp@2315 1084
tonyp@2315 1085 // So far our attempts to allocate failed. The only time we'll go
tonyp@2315 1086 // around the loop and try again is if we tried to do a GC and the
tonyp@2315 1087 // VM op that we tried to schedule was not successful because
tonyp@2315 1088 // another thread beat us to it. That way it's possible that some
tonyp@2315 1089 // space was freed up by the thread that successfully scheduled a
tonyp@2315 1090 // GC. So it's worth trying to allocate again.
tonyp@2315 1091 if (succeeded) {
tonyp@2315 1092 break;
ysr@777 1093 }
tonyp@2315 1094
tonyp@2315 1095 // Give a warning if we seem to be looping forever.
tonyp@2315 1096 if ((QueuedAllocationWarningCount > 0) &&
tonyp@2315 1097 (try_count % QueuedAllocationWarningCount == 0)) {
tonyp@2315 1098 warning("G1CollectedHeap::attempt_allocation_humongous "
tonyp@2315 1099 "retries %d times", try_count);
tonyp@2315 1100 }
tonyp@2315 1101 }
tonyp@2315 1102
tonyp@2472 1103 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
tonyp@2315 1104 return NULL;
tonyp@2315 1105 }
tonyp@2315 1106
tonyp@2315 1107 HeapWord* G1CollectedHeap::attempt_allocation_at_safepoint(size_t word_size,
tonyp@2315 1108 bool expect_null_cur_alloc_region) {
tonyp@2472 1109 assert_at_safepoint(true /* should_be_vm_thread */);
tonyp@2315 1110 assert(_cur_alloc_region == NULL || !expect_null_cur_alloc_region,
tonyp@2317 1111 err_msg("the current alloc region was unexpectedly found "
tonyp@2317 1112 "to be non-NULL, cur alloc region: "PTR_FORMAT" "
tonyp@2317 1113 "expect_null_cur_alloc_region: %d word_size: "SIZE_FORMAT,
tonyp@2317 1114 _cur_alloc_region, expect_null_cur_alloc_region, word_size));
tonyp@2315 1115
tonyp@2315 1116 if (!isHumongous(word_size)) {
tonyp@2315 1117 if (!expect_null_cur_alloc_region) {
tonyp@2315 1118 HeapRegion* cur_alloc_region = _cur_alloc_region;
tonyp@2315 1119 if (cur_alloc_region != NULL) {
tonyp@2454 1120 // We are at a safepoint so no reason to use the MT-safe version.
tonyp@2454 1121 HeapWord* result = cur_alloc_region->allocate_no_bot_updates(word_size);
tonyp@2315 1122 if (result != NULL) {
tonyp@2315 1123 assert(is_in(result), "result should be in the heap");
tonyp@2315 1124
tonyp@2315 1125 // We will not do any dirtying here. This is guaranteed to be
tonyp@2315 1126 // called during a safepoint and the thread that scheduled the
tonyp@2315 1127 // pause will do the dirtying if we return a non-NULL result.
tonyp@2315 1128 return result;
tonyp@2315 1129 }
tonyp@2315 1130
tonyp@2315 1131 retire_cur_alloc_region_common(cur_alloc_region);
tonyp@2315 1132 }
tonyp@2315 1133 }
tonyp@2315 1134
tonyp@2315 1135 assert(_cur_alloc_region == NULL,
tonyp@2315 1136 "at this point we should have no cur alloc region");
tonyp@2315 1137 return replace_cur_alloc_region_and_allocate(word_size,
tonyp@2315 1138 true, /* at_safepoint */
tonyp@2333 1139 false /* do_dirtying */,
tonyp@2333 1140 false /* can_expand */);
tonyp@2315 1141 } else {
tonyp@2315 1142 return attempt_allocation_humongous(word_size,
tonyp@2315 1143 true /* at_safepoint */);
tonyp@2315 1144 }
tonyp@2315 1145
tonyp@2315 1146 ShouldNotReachHere();
tonyp@2315 1147 }
tonyp@2315 1148
tonyp@2315 1149 HeapWord* G1CollectedHeap::allocate_new_tlab(size_t word_size) {
tonyp@2315 1150 assert_heap_not_locked_and_not_at_safepoint();
tonyp@2315 1151 assert(!isHumongous(word_size), "we do not allow TLABs of humongous size");
tonyp@2315 1152
tonyp@2454 1153 // First attempt: Try allocating out of the current alloc region
tonyp@2454 1154 // using a CAS. If that fails, take the Heap_lock and retry the
tonyp@2454 1155 // allocation, potentially replacing the current alloc region.
tonyp@2315 1156 HeapWord* result = attempt_allocation(word_size);
tonyp@2315 1157 if (result != NULL) {
tonyp@2315 1158 assert_heap_not_locked();
tonyp@2315 1159 return result;
tonyp@2315 1160 }
tonyp@2315 1161
tonyp@2454 1162 // Second attempt: Go to the slower path where we might try to
tonyp@2454 1163 // schedule a collection.
tonyp@2315 1164 result = attempt_allocation_slow(word_size);
tonyp@2315 1165 if (result != NULL) {
tonyp@2315 1166 assert_heap_not_locked();
tonyp@2315 1167 return result;
tonyp@2315 1168 }
tonyp@2315 1169
tonyp@2315 1170 assert_heap_locked();
tonyp@2454 1171 // Need to unlock the Heap_lock before returning.
tonyp@2315 1172 Heap_lock->unlock();
tonyp@2315 1173 return NULL;
ysr@777 1174 }
ysr@777 1175
ysr@777 1176 HeapWord*
ysr@777 1177 G1CollectedHeap::mem_allocate(size_t word_size,
ysr@777 1178 bool is_noref,
ysr@777 1179 bool is_tlab,
tonyp@2315 1180 bool* gc_overhead_limit_was_exceeded) {
tonyp@2315 1181 assert_heap_not_locked_and_not_at_safepoint();
tonyp@2315 1182 assert(!is_tlab, "mem_allocate() this should not be called directly "
tonyp@2315 1183 "to allocate TLABs");
ysr@777 1184
ysr@777 1185 // Loop until the allocation is satisified,
ysr@777 1186 // or unsatisfied after GC.
tonyp@2315 1187 for (int try_count = 1; /* we'll return */; try_count += 1) {
tonyp@2315 1188 unsigned int gc_count_before;
ysr@777 1189 {
tonyp@2315 1190 if (!isHumongous(word_size)) {
tonyp@2454 1191 // First attempt: Try allocating out of the current alloc region
tonyp@2454 1192 // using a CAS. If that fails, take the Heap_lock and retry the
tonyp@2454 1193 // allocation, potentially replacing the current alloc region.
tonyp@2315 1194 HeapWord* result = attempt_allocation(word_size);
tonyp@2315 1195 if (result != NULL) {
tonyp@2315 1196 assert_heap_not_locked();
tonyp@2315 1197 return result;
tonyp@2315 1198 }
tonyp@2315 1199
tonyp@2315 1200 assert_heap_locked();
tonyp@2315 1201
tonyp@2454 1202 // Second attempt: Go to the slower path where we might try to
tonyp@2454 1203 // schedule a collection.
tonyp@2315 1204 result = attempt_allocation_slow(word_size);
tonyp@2315 1205 if (result != NULL) {
tonyp@2315 1206 assert_heap_not_locked();
tonyp@2315 1207 return result;
tonyp@2315 1208 }
tonyp@2315 1209 } else {
tonyp@2454 1210 // attempt_allocation_humongous() requires the Heap_lock to be held.
tonyp@2454 1211 Heap_lock->lock();
tonyp@2454 1212
tonyp@2315 1213 HeapWord* result = attempt_allocation_humongous(word_size,
tonyp@2315 1214 false /* at_safepoint */);
tonyp@2315 1215 if (result != NULL) {
tonyp@2315 1216 assert_heap_not_locked();
tonyp@2315 1217 return result;
tonyp@2315 1218 }
ysr@777 1219 }
tonyp@2315 1220
tonyp@2315 1221 assert_heap_locked();
ysr@777 1222 // Read the gc count while the heap lock is held.
ysr@777 1223 gc_count_before = SharedHeap::heap()->total_collections();
tonyp@2454 1224
tonyp@2454 1225 // Release the Heap_lock before attempting the collection.
ysr@777 1226 Heap_lock->unlock();
ysr@777 1227 }
ysr@777 1228
ysr@777 1229 // Create the garbage collection operation...
tonyp@2315 1230 VM_G1CollectForAllocation op(gc_count_before, word_size);
ysr@777 1231 // ...and get the VM thread to execute it.
ysr@777 1232 VMThread::execute(&op);
tonyp@2315 1233
tonyp@2315 1234 assert_heap_not_locked();
tonyp@2315 1235 if (op.prologue_succeeded() && op.pause_succeeded()) {
tonyp@2315 1236 // If the operation was successful we'll return the result even
tonyp@2315 1237 // if it is NULL. If the allocation attempt failed immediately
tonyp@2315 1238 // after a Full GC, it's unlikely we'll be able to allocate now.
tonyp@2315 1239 HeapWord* result = op.result();
tonyp@2315 1240 if (result != NULL && !isHumongous(word_size)) {
tonyp@2315 1241 // Allocations that take place on VM operations do not do any
tonyp@2315 1242 // card dirtying and we have to do it here. We only have to do
tonyp@2315 1243 // this for non-humongous allocations, though.
tonyp@2315 1244 dirty_young_block(result, word_size);
tonyp@2315 1245 }
ysr@777 1246 return result;
tonyp@2315 1247 } else {
tonyp@2315 1248 assert(op.result() == NULL,
tonyp@2315 1249 "the result should be NULL if the VM op did not succeed");
ysr@777 1250 }
ysr@777 1251
ysr@777 1252 // Give a warning if we seem to be looping forever.
ysr@777 1253 if ((QueuedAllocationWarningCount > 0) &&
ysr@777 1254 (try_count % QueuedAllocationWarningCount == 0)) {
tonyp@2315 1255 warning("G1CollectedHeap::mem_allocate retries %d times", try_count);
ysr@777 1256 }
ysr@777 1257 }
tonyp@2315 1258
tonyp@2315 1259 ShouldNotReachHere();
ysr@777 1260 }
ysr@777 1261
ysr@777 1262 void G1CollectedHeap::abandon_cur_alloc_region() {
tonyp@2472 1263 assert_at_safepoint(true /* should_be_vm_thread */);
tonyp@2472 1264
tonyp@2472 1265 HeapRegion* cur_alloc_region = _cur_alloc_region;
tonyp@2472 1266 if (cur_alloc_region != NULL) {
tonyp@2472 1267 assert(!cur_alloc_region->is_empty(),
tonyp@2472 1268 "the current alloc region can never be empty");
tonyp@2472 1269 assert(cur_alloc_region->is_young(),
tonyp@2472 1270 "the current alloc region should be young");
tonyp@2472 1271
tonyp@2472 1272 retire_cur_alloc_region_common(cur_alloc_region);
tonyp@2472 1273 }
tonyp@2472 1274 assert(_cur_alloc_region == NULL, "post-condition");
ysr@777 1275 }
ysr@777 1276
tonyp@1071 1277 void G1CollectedHeap::abandon_gc_alloc_regions() {
tonyp@1071 1278 // first, make sure that the GC alloc region list is empty (it should!)
tonyp@1071 1279 assert(_gc_alloc_region_list == NULL, "invariant");
tonyp@1071 1280 release_gc_alloc_regions(true /* totally */);
tonyp@1071 1281 }
tonyp@1071 1282
ysr@777 1283 class PostMCRemSetClearClosure: public HeapRegionClosure {
ysr@777 1284 ModRefBarrierSet* _mr_bs;
ysr@777 1285 public:
ysr@777 1286 PostMCRemSetClearClosure(ModRefBarrierSet* mr_bs) : _mr_bs(mr_bs) {}
ysr@777 1287 bool doHeapRegion(HeapRegion* r) {
ysr@777 1288 r->reset_gc_time_stamp();
ysr@777 1289 if (r->continuesHumongous())
ysr@777 1290 return false;
ysr@777 1291 HeapRegionRemSet* hrrs = r->rem_set();
ysr@777 1292 if (hrrs != NULL) hrrs->clear();
ysr@777 1293 // You might think here that we could clear just the cards
ysr@777 1294 // corresponding to the used region. But no: if we leave a dirty card
ysr@777 1295 // in a region we might allocate into, then it would prevent that card
ysr@777 1296 // from being enqueued, and cause it to be missed.
ysr@777 1297 // Re: the performance cost: we shouldn't be doing full GC anyway!
ysr@777 1298 _mr_bs->clear(MemRegion(r->bottom(), r->end()));
ysr@777 1299 return false;
ysr@777 1300 }
ysr@777 1301 };
ysr@777 1302
ysr@777 1303
ysr@777 1304 class PostMCRemSetInvalidateClosure: public HeapRegionClosure {
ysr@777 1305 ModRefBarrierSet* _mr_bs;
ysr@777 1306 public:
ysr@777 1307 PostMCRemSetInvalidateClosure(ModRefBarrierSet* mr_bs) : _mr_bs(mr_bs) {}
ysr@777 1308 bool doHeapRegion(HeapRegion* r) {
ysr@777 1309 if (r->continuesHumongous()) return false;
ysr@777 1310 if (r->used_region().word_size() != 0) {
ysr@777 1311 _mr_bs->invalidate(r->used_region(), true /*whole heap*/);
ysr@777 1312 }
ysr@777 1313 return false;
ysr@777 1314 }
ysr@777 1315 };
ysr@777 1316
apetrusenko@1061 1317 class RebuildRSOutOfRegionClosure: public HeapRegionClosure {
apetrusenko@1061 1318 G1CollectedHeap* _g1h;
apetrusenko@1061 1319 UpdateRSOopClosure _cl;
apetrusenko@1061 1320 int _worker_i;
apetrusenko@1061 1321 public:
apetrusenko@1061 1322 RebuildRSOutOfRegionClosure(G1CollectedHeap* g1, int worker_i = 0) :
johnc@2216 1323 _cl(g1->g1_rem_set(), worker_i),
apetrusenko@1061 1324 _worker_i(worker_i),
apetrusenko@1061 1325 _g1h(g1)
apetrusenko@1061 1326 { }
johnc@2302 1327
apetrusenko@1061 1328 bool doHeapRegion(HeapRegion* r) {
apetrusenko@1061 1329 if (!r->continuesHumongous()) {
apetrusenko@1061 1330 _cl.set_from(r);
apetrusenko@1061 1331 r->oop_iterate(&_cl);
apetrusenko@1061 1332 }
apetrusenko@1061 1333 return false;
apetrusenko@1061 1334 }
apetrusenko@1061 1335 };
apetrusenko@1061 1336
apetrusenko@1061 1337 class ParRebuildRSTask: public AbstractGangTask {
apetrusenko@1061 1338 G1CollectedHeap* _g1;
apetrusenko@1061 1339 public:
apetrusenko@1061 1340 ParRebuildRSTask(G1CollectedHeap* g1)
apetrusenko@1061 1341 : AbstractGangTask("ParRebuildRSTask"),
apetrusenko@1061 1342 _g1(g1)
apetrusenko@1061 1343 { }
apetrusenko@1061 1344
apetrusenko@1061 1345 void work(int i) {
apetrusenko@1061 1346 RebuildRSOutOfRegionClosure rebuild_rs(_g1, i);
apetrusenko@1061 1347 _g1->heap_region_par_iterate_chunked(&rebuild_rs, i,
apetrusenko@1061 1348 HeapRegion::RebuildRSClaimValue);
apetrusenko@1061 1349 }
apetrusenko@1061 1350 };
apetrusenko@1061 1351
tonyp@2315 1352 bool G1CollectedHeap::do_collection(bool explicit_gc,
tonyp@2011 1353 bool clear_all_soft_refs,
ysr@777 1354 size_t word_size) {
tonyp@2472 1355 assert_at_safepoint(true /* should_be_vm_thread */);
tonyp@2472 1356
tonyp@1794 1357 if (GC_locker::check_active_before_gc()) {
tonyp@2315 1358 return false;
tonyp@1794 1359 }
tonyp@1794 1360
kamg@2445 1361 SvcGCMarker sgcm(SvcGCMarker::FULL);
ysr@777 1362 ResourceMark rm;
ysr@777 1363
tonyp@1273 1364 if (PrintHeapAtGC) {
tonyp@1273 1365 Universe::print_heap_before_gc();
tonyp@1273 1366 }
tonyp@1273 1367
tonyp@2472 1368 verify_region_sets_optional();
ysr@777 1369
jmasa@1822 1370 const bool do_clear_all_soft_refs = clear_all_soft_refs ||
jmasa@1822 1371 collector_policy()->should_clear_all_soft_refs();
jmasa@1822 1372
jmasa@1822 1373 ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy());
jmasa@1822 1374
ysr@777 1375 {
ysr@777 1376 IsGCActiveMark x;
ysr@777 1377
ysr@777 1378 // Timing
tonyp@2011 1379 bool system_gc = (gc_cause() == GCCause::_java_lang_system_gc);
tonyp@2011 1380 assert(!system_gc || explicit_gc, "invariant");
ysr@777 1381 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
ysr@777 1382 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
tonyp@2011 1383 TraceTime t(system_gc ? "Full GC (System.gc())" : "Full GC",
jmasa@1822 1384 PrintGC, true, gclog_or_tty);
ysr@777 1385
tonyp@1524 1386 TraceMemoryManagerStats tms(true /* fullGC */);
tonyp@1524 1387
ysr@777 1388 double start = os::elapsedTime();
ysr@777 1389 g1_policy()->record_full_collection_start();
ysr@777 1390
tonyp@2472 1391 wait_while_free_regions_coming();
tonyp@2472 1392 append_secondary_free_list_if_not_empty();
tonyp@2472 1393
ysr@777 1394 gc_prologue(true);
tonyp@1273 1395 increment_total_collections(true /* full gc */);
ysr@777 1396
ysr@777 1397 size_t g1h_prev_used = used();
ysr@777 1398 assert(used() == recalculate_used(), "Should be equal");
ysr@777 1399
ysr@777 1400 if (VerifyBeforeGC && total_collections() >= VerifyGCStartAt) {
ysr@777 1401 HandleMark hm; // Discard invalid handles created during verification
ysr@777 1402 prepare_for_verify();
ysr@777 1403 gclog_or_tty->print(" VerifyBeforeGC:");
ysr@777 1404 Universe::verify(true);
ysr@777 1405 }
ysr@777 1406
ysr@777 1407 COMPILER2_PRESENT(DerivedPointerTable::clear());
ysr@777 1408
ysr@777 1409 // We want to discover references, but not process them yet.
ysr@777 1410 // This mode is disabled in
ysr@777 1411 // instanceRefKlass::process_discovered_references if the
ysr@777 1412 // generation does some collection work, or
ysr@777 1413 // instanceRefKlass::enqueue_discovered_references if the
ysr@777 1414 // generation returns without doing any work.
ysr@777 1415 ref_processor()->disable_discovery();
ysr@777 1416 ref_processor()->abandon_partial_discovery();
ysr@777 1417 ref_processor()->verify_no_references_recorded();
ysr@777 1418
ysr@777 1419 // Abandon current iterations of concurrent marking and concurrent
ysr@777 1420 // refinement, if any are in progress.
ysr@777 1421 concurrent_mark()->abort();
ysr@777 1422
ysr@777 1423 // Make sure we'll choose a new allocation region afterwards.
ysr@777 1424 abandon_cur_alloc_region();
tonyp@1071 1425 abandon_gc_alloc_regions();
ysr@777 1426 assert(_cur_alloc_region == NULL, "Invariant.");
johnc@2216 1427 g1_rem_set()->cleanupHRRS();
ysr@777 1428 tear_down_region_lists();
johnc@1829 1429
johnc@1829 1430 // We may have added regions to the current incremental collection
johnc@1829 1431 // set between the last GC or pause and now. We need to clear the
johnc@1829 1432 // incremental collection set and then start rebuilding it afresh
johnc@1829 1433 // after this full GC.
johnc@1829 1434 abandon_collection_set(g1_policy()->inc_cset_head());
johnc@1829 1435 g1_policy()->clear_incremental_cset();
johnc@1829 1436 g1_policy()->stop_incremental_cset_building();
johnc@1829 1437
ysr@777 1438 if (g1_policy()->in_young_gc_mode()) {
ysr@777 1439 empty_young_list();
ysr@777 1440 g1_policy()->set_full_young_gcs(true);
ysr@777 1441 }
ysr@777 1442
johnc@2316 1443 // See the comment in G1CollectedHeap::ref_processing_init() about
johnc@2316 1444 // how reference processing currently works in G1.
johnc@2316 1445
ysr@777 1446 // Temporarily make reference _discovery_ single threaded (non-MT).
ysr@777 1447 ReferenceProcessorMTMutator rp_disc_ser(ref_processor(), false);
ysr@777 1448
ysr@777 1449 // Temporarily make refs discovery atomic
ysr@777 1450 ReferenceProcessorAtomicMutator rp_disc_atomic(ref_processor(), true);
ysr@777 1451
ysr@777 1452 // Temporarily clear _is_alive_non_header
ysr@777 1453 ReferenceProcessorIsAliveMutator rp_is_alive_null(ref_processor(), NULL);
ysr@777 1454
ysr@777 1455 ref_processor()->enable_discovery();
jmasa@1822 1456 ref_processor()->setup_policy(do_clear_all_soft_refs);
ysr@777 1457
ysr@777 1458 // Do collection work
ysr@777 1459 {
ysr@777 1460 HandleMark hm; // Discard invalid handles created during gc
jmasa@1822 1461 G1MarkSweep::invoke_at_safepoint(ref_processor(), do_clear_all_soft_refs);
ysr@777 1462 }
tonyp@2472 1463 assert(free_regions() == 0, "we should not have added any free regions");
ysr@777 1464 rebuild_region_lists();
ysr@777 1465
ysr@777 1466 _summary_bytes_used = recalculate_used();
ysr@777 1467
ysr@777 1468 ref_processor()->enqueue_discovered_references();
ysr@777 1469
ysr@777 1470 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
ysr@777 1471
tonyp@1524 1472 MemoryService::track_memory_usage();
tonyp@1524 1473
ysr@777 1474 if (VerifyAfterGC && total_collections() >= VerifyGCStartAt) {
ysr@777 1475 HandleMark hm; // Discard invalid handles created during verification
ysr@777 1476 gclog_or_tty->print(" VerifyAfterGC:");
iveresov@1072 1477 prepare_for_verify();
ysr@777 1478 Universe::verify(false);
ysr@777 1479 }
ysr@777 1480 NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
ysr@777 1481
ysr@777 1482 reset_gc_time_stamp();
ysr@777 1483 // Since everything potentially moved, we will clear all remembered
apetrusenko@1061 1484 // sets, and clear all cards. Later we will rebuild remebered
apetrusenko@1061 1485 // sets. We will also reset the GC time stamps of the regions.
ysr@777 1486 PostMCRemSetClearClosure rs_clear(mr_bs());
ysr@777 1487 heap_region_iterate(&rs_clear);
ysr@777 1488
ysr@777 1489 // Resize the heap if necessary.
tonyp@2011 1490 resize_if_necessary_after_full_collection(explicit_gc ? 0 : word_size);
ysr@777 1491
ysr@777 1492 if (_cg1r->use_cache()) {
ysr@777 1493 _cg1r->clear_and_record_card_counts();
ysr@777 1494 _cg1r->clear_hot_cache();
ysr@777 1495 }
ysr@777 1496
apetrusenko@1061 1497 // Rebuild remembered sets of all regions.
jmasa@2188 1498
jmasa@2188 1499 if (G1CollectedHeap::use_parallel_gc_threads()) {
apetrusenko@1061 1500 ParRebuildRSTask rebuild_rs_task(this);
apetrusenko@1061 1501 assert(check_heap_region_claim_values(
apetrusenko@1061 1502 HeapRegion::InitialClaimValue), "sanity check");
apetrusenko@1061 1503 set_par_threads(workers()->total_workers());
apetrusenko@1061 1504 workers()->run_task(&rebuild_rs_task);
apetrusenko@1061 1505 set_par_threads(0);
apetrusenko@1061 1506 assert(check_heap_region_claim_values(
apetrusenko@1061 1507 HeapRegion::RebuildRSClaimValue), "sanity check");
apetrusenko@1061 1508 reset_heap_region_claim_values();
apetrusenko@1061 1509 } else {
apetrusenko@1061 1510 RebuildRSOutOfRegionClosure rebuild_rs(this);
apetrusenko@1061 1511 heap_region_iterate(&rebuild_rs);
apetrusenko@1061 1512 }
apetrusenko@1061 1513
ysr@777 1514 if (PrintGC) {
ysr@777 1515 print_size_transition(gclog_or_tty, g1h_prev_used, used(), capacity());
ysr@777 1516 }
ysr@777 1517
ysr@777 1518 if (true) { // FIXME
ysr@777 1519 // Ask the permanent generation to adjust size for full collections
ysr@777 1520 perm()->compute_new_size();
ysr@777 1521 }
ysr@777 1522
johnc@1829 1523 // Start a new incremental collection set for the next pause
johnc@1829 1524 assert(g1_policy()->collection_set() == NULL, "must be");
johnc@1829 1525 g1_policy()->start_incremental_cset_building();
johnc@1829 1526
johnc@1829 1527 // Clear the _cset_fast_test bitmap in anticipation of adding
johnc@1829 1528 // regions to the incremental collection set for the next
johnc@1829 1529 // evacuation pause.
johnc@1829 1530 clear_cset_fast_test();
johnc@1829 1531
ysr@777 1532 double end = os::elapsedTime();
ysr@777 1533 g1_policy()->record_full_collection_end();
ysr@777 1534
jmasa@981 1535 #ifdef TRACESPINNING
jmasa@981 1536 ParallelTaskTerminator::print_termination_counts();
jmasa@981 1537 #endif
jmasa@981 1538
ysr@777 1539 gc_epilogue(true);
ysr@777 1540
iveresov@1229 1541 // Discard all rset updates
iveresov@1229 1542 JavaThread::dirty_card_queue_set().abandon_logs();
iveresov@1051 1543 assert(!G1DeferredRSUpdate
iveresov@1051 1544 || (G1DeferredRSUpdate && (dirty_card_queue_set().completed_buffers_num() == 0)), "Should not be any");
ysr@777 1545 }
ysr@777 1546
ysr@777 1547 if (g1_policy()->in_young_gc_mode()) {
ysr@777 1548 _young_list->reset_sampled_info();
johnc@1829 1549 // At this point there should be no regions in the
johnc@1829 1550 // entire heap tagged as young.
johnc@1829 1551 assert( check_young_list_empty(true /* check_heap */),
ysr@777 1552 "young list should be empty at this point");
ysr@777 1553 }
tonyp@1273 1554
tonyp@2011 1555 // Update the number of full collections that have been completed.
tonyp@2372 1556 increment_full_collections_completed(false /* concurrent */);
tonyp@2011 1557
tonyp@2472 1558 verify_region_sets_optional();
tonyp@2472 1559
tonyp@1273 1560 if (PrintHeapAtGC) {
tonyp@1273 1561 Universe::print_heap_after_gc();
tonyp@1273 1562 }
tonyp@2315 1563
tonyp@2315 1564 return true;
ysr@777 1565 }
ysr@777 1566
ysr@777 1567 void G1CollectedHeap::do_full_collection(bool clear_all_soft_refs) {
tonyp@2315 1568 // do_collection() will return whether it succeeded in performing
tonyp@2315 1569 // the GC. Currently, there is no facility on the
tonyp@2315 1570 // do_full_collection() API to notify the caller than the collection
tonyp@2315 1571 // did not succeed (e.g., because it was locked out by the GC
tonyp@2315 1572 // locker). So, right now, we'll ignore the return value.
tonyp@2315 1573 bool dummy = do_collection(true, /* explicit_gc */
tonyp@2315 1574 clear_all_soft_refs,
tonyp@2315 1575 0 /* word_size */);
ysr@777 1576 }
ysr@777 1577
ysr@777 1578 // This code is mostly copied from TenuredGeneration.
ysr@777 1579 void
ysr@777 1580 G1CollectedHeap::
ysr@777 1581 resize_if_necessary_after_full_collection(size_t word_size) {
ysr@777 1582 assert(MinHeapFreeRatio <= MaxHeapFreeRatio, "sanity check");
ysr@777 1583
ysr@777 1584 // Include the current allocation, if any, and bytes that will be
ysr@777 1585 // pre-allocated to support collections, as "used".
ysr@777 1586 const size_t used_after_gc = used();
ysr@777 1587 const size_t capacity_after_gc = capacity();
ysr@777 1588 const size_t free_after_gc = capacity_after_gc - used_after_gc;
ysr@777 1589
tonyp@2072 1590 // This is enforced in arguments.cpp.
tonyp@2072 1591 assert(MinHeapFreeRatio <= MaxHeapFreeRatio,
tonyp@2072 1592 "otherwise the code below doesn't make sense");
tonyp@2072 1593
ysr@777 1594 // We don't have floating point command-line arguments
tonyp@2072 1595 const double minimum_free_percentage = (double) MinHeapFreeRatio / 100.0;
ysr@777 1596 const double maximum_used_percentage = 1.0 - minimum_free_percentage;
tonyp@2072 1597 const double maximum_free_percentage = (double) MaxHeapFreeRatio / 100.0;
ysr@777 1598 const double minimum_used_percentage = 1.0 - maximum_free_percentage;
ysr@777 1599
tonyp@2072 1600 const size_t min_heap_size = collector_policy()->min_heap_byte_size();
tonyp@2072 1601 const size_t max_heap_size = collector_policy()->max_heap_byte_size();
tonyp@2072 1602
tonyp@2072 1603 // We have to be careful here as these two calculations can overflow
tonyp@2072 1604 // 32-bit size_t's.
tonyp@2072 1605 double used_after_gc_d = (double) used_after_gc;
tonyp@2072 1606 double minimum_desired_capacity_d = used_after_gc_d / maximum_used_percentage;
tonyp@2072 1607 double maximum_desired_capacity_d = used_after_gc_d / minimum_used_percentage;
tonyp@2072 1608
tonyp@2072 1609 // Let's make sure that they are both under the max heap size, which
tonyp@2072 1610 // by default will make them fit into a size_t.
tonyp@2072 1611 double desired_capacity_upper_bound = (double) max_heap_size;
tonyp@2072 1612 minimum_desired_capacity_d = MIN2(minimum_desired_capacity_d,
tonyp@2072 1613 desired_capacity_upper_bound);
tonyp@2072 1614 maximum_desired_capacity_d = MIN2(maximum_desired_capacity_d,
tonyp@2072 1615 desired_capacity_upper_bound);
tonyp@2072 1616
tonyp@2072 1617 // We can now safely turn them into size_t's.
tonyp@2072 1618 size_t minimum_desired_capacity = (size_t) minimum_desired_capacity_d;
tonyp@2072 1619 size_t maximum_desired_capacity = (size_t) maximum_desired_capacity_d;
tonyp@2072 1620
tonyp@2072 1621 // This assert only makes sense here, before we adjust them
tonyp@2072 1622 // with respect to the min and max heap size.
tonyp@2072 1623 assert(minimum_desired_capacity <= maximum_desired_capacity,
tonyp@2072 1624 err_msg("minimum_desired_capacity = "SIZE_FORMAT", "
tonyp@2072 1625 "maximum_desired_capacity = "SIZE_FORMAT,
tonyp@2072 1626 minimum_desired_capacity, maximum_desired_capacity));
tonyp@2072 1627
tonyp@2072 1628 // Should not be greater than the heap max size. No need to adjust
tonyp@2072 1629 // it with respect to the heap min size as it's a lower bound (i.e.,
tonyp@2072 1630 // we'll try to make the capacity larger than it, not smaller).
tonyp@2072 1631 minimum_desired_capacity = MIN2(minimum_desired_capacity, max_heap_size);
tonyp@2072 1632 // Should not be less than the heap min size. No need to adjust it
tonyp@2072 1633 // with respect to the heap max size as it's an upper bound (i.e.,
tonyp@2072 1634 // we'll try to make the capacity smaller than it, not greater).
tonyp@2072 1635 maximum_desired_capacity = MAX2(maximum_desired_capacity, min_heap_size);
ysr@777 1636
ysr@777 1637 if (PrintGC && Verbose) {
tonyp@2072 1638 const double free_percentage =
tonyp@2072 1639 (double) free_after_gc / (double) capacity_after_gc;
ysr@777 1640 gclog_or_tty->print_cr("Computing new size after full GC ");
ysr@777 1641 gclog_or_tty->print_cr(" "
ysr@777 1642 " minimum_free_percentage: %6.2f",
ysr@777 1643 minimum_free_percentage);
ysr@777 1644 gclog_or_tty->print_cr(" "
ysr@777 1645 " maximum_free_percentage: %6.2f",
ysr@777 1646 maximum_free_percentage);
ysr@777 1647 gclog_or_tty->print_cr(" "
ysr@777 1648 " capacity: %6.1fK"
ysr@777 1649 " minimum_desired_capacity: %6.1fK"
ysr@777 1650 " maximum_desired_capacity: %6.1fK",
tonyp@2072 1651 (double) capacity_after_gc / (double) K,
tonyp@2072 1652 (double) minimum_desired_capacity / (double) K,
tonyp@2072 1653 (double) maximum_desired_capacity / (double) K);
ysr@777 1654 gclog_or_tty->print_cr(" "
tonyp@2072 1655 " free_after_gc: %6.1fK"
tonyp@2072 1656 " used_after_gc: %6.1fK",
tonyp@2072 1657 (double) free_after_gc / (double) K,
tonyp@2072 1658 (double) used_after_gc / (double) K);
ysr@777 1659 gclog_or_tty->print_cr(" "
ysr@777 1660 " free_percentage: %6.2f",
ysr@777 1661 free_percentage);
ysr@777 1662 }
tonyp@2072 1663 if (capacity_after_gc < minimum_desired_capacity) {
ysr@777 1664 // Don't expand unless it's significant
ysr@777 1665 size_t expand_bytes = minimum_desired_capacity - capacity_after_gc;
johnc@2504 1666 if (expand(expand_bytes)) {
johnc@2504 1667 if (PrintGC && Verbose) {
johnc@2504 1668 gclog_or_tty->print_cr(" "
johnc@2504 1669 " expanding:"
johnc@2504 1670 " max_heap_size: %6.1fK"
johnc@2504 1671 " minimum_desired_capacity: %6.1fK"
johnc@2504 1672 " expand_bytes: %6.1fK",
johnc@2504 1673 (double) max_heap_size / (double) K,
johnc@2504 1674 (double) minimum_desired_capacity / (double) K,
johnc@2504 1675 (double) expand_bytes / (double) K);
johnc@2504 1676 }
ysr@777 1677 }
ysr@777 1678
ysr@777 1679 // No expansion, now see if we want to shrink
tonyp@2072 1680 } else if (capacity_after_gc > maximum_desired_capacity) {
ysr@777 1681 // Capacity too large, compute shrinking size
ysr@777 1682 size_t shrink_bytes = capacity_after_gc - maximum_desired_capacity;
ysr@777 1683 shrink(shrink_bytes);
ysr@777 1684 if (PrintGC && Verbose) {
ysr@777 1685 gclog_or_tty->print_cr(" "
ysr@777 1686 " shrinking:"
tonyp@2072 1687 " min_heap_size: %6.1fK"
tonyp@2072 1688 " maximum_desired_capacity: %6.1fK"
tonyp@2072 1689 " shrink_bytes: %6.1fK",
tonyp@2072 1690 (double) min_heap_size / (double) K,
tonyp@2072 1691 (double) maximum_desired_capacity / (double) K,
tonyp@2072 1692 (double) shrink_bytes / (double) K);
ysr@777 1693 }
ysr@777 1694 }
ysr@777 1695 }
ysr@777 1696
ysr@777 1697
ysr@777 1698 HeapWord*
tonyp@2315 1699 G1CollectedHeap::satisfy_failed_allocation(size_t word_size,
tonyp@2315 1700 bool* succeeded) {
tonyp@2472 1701 assert_at_safepoint(true /* should_be_vm_thread */);
tonyp@2315 1702
tonyp@2315 1703 *succeeded = true;
tonyp@2315 1704 // Let's attempt the allocation first.
tonyp@2315 1705 HeapWord* result = attempt_allocation_at_safepoint(word_size,
tonyp@2315 1706 false /* expect_null_cur_alloc_region */);
tonyp@2315 1707 if (result != NULL) {
tonyp@2315 1708 assert(*succeeded, "sanity");
tonyp@2315 1709 return result;
tonyp@2315 1710 }
ysr@777 1711
ysr@777 1712 // In a G1 heap, we're supposed to keep allocation from failing by
ysr@777 1713 // incremental pauses. Therefore, at least for now, we'll favor
ysr@777 1714 // expansion over collection. (This might change in the future if we can
ysr@777 1715 // do something smarter than full collection to satisfy a failed alloc.)
ysr@777 1716 result = expand_and_allocate(word_size);
ysr@777 1717 if (result != NULL) {
tonyp@2315 1718 assert(*succeeded, "sanity");
ysr@777 1719 return result;
ysr@777 1720 }
ysr@777 1721
tonyp@2315 1722 // Expansion didn't work, we'll try to do a Full GC.
tonyp@2315 1723 bool gc_succeeded = do_collection(false, /* explicit_gc */
tonyp@2315 1724 false, /* clear_all_soft_refs */
tonyp@2315 1725 word_size);
tonyp@2315 1726 if (!gc_succeeded) {
tonyp@2315 1727 *succeeded = false;
tonyp@2315 1728 return NULL;
tonyp@2315 1729 }
tonyp@2315 1730
tonyp@2315 1731 // Retry the allocation
tonyp@2315 1732 result = attempt_allocation_at_safepoint(word_size,
tonyp@2315 1733 true /* expect_null_cur_alloc_region */);
ysr@777 1734 if (result != NULL) {
tonyp@2315 1735 assert(*succeeded, "sanity");
ysr@777 1736 return result;
ysr@777 1737 }
ysr@777 1738
tonyp@2315 1739 // Then, try a Full GC that will collect all soft references.
tonyp@2315 1740 gc_succeeded = do_collection(false, /* explicit_gc */
tonyp@2315 1741 true, /* clear_all_soft_refs */
tonyp@2315 1742 word_size);
tonyp@2315 1743 if (!gc_succeeded) {
tonyp@2315 1744 *succeeded = false;
tonyp@2315 1745 return NULL;
tonyp@2315 1746 }
tonyp@2315 1747
tonyp@2315 1748 // Retry the allocation once more
tonyp@2315 1749 result = attempt_allocation_at_safepoint(word_size,
tonyp@2315 1750 true /* expect_null_cur_alloc_region */);
ysr@777 1751 if (result != NULL) {
tonyp@2315 1752 assert(*succeeded, "sanity");
ysr@777 1753 return result;
ysr@777 1754 }
ysr@777 1755
jmasa@1822 1756 assert(!collector_policy()->should_clear_all_soft_refs(),
tonyp@2315 1757 "Flag should have been handled and cleared prior to this point");
jmasa@1822 1758
ysr@777 1759 // What else? We might try synchronous finalization later. If the total
ysr@777 1760 // space available is large enough for the allocation, then a more
ysr@777 1761 // complete compaction phase than we've tried so far might be
ysr@777 1762 // appropriate.
tonyp@2315 1763 assert(*succeeded, "sanity");
ysr@777 1764 return NULL;
ysr@777 1765 }
ysr@777 1766
ysr@777 1767 // Attempting to expand the heap sufficiently
ysr@777 1768 // to support an allocation of the given "word_size". If
ysr@777 1769 // successful, perform the allocation and return the address of the
ysr@777 1770 // allocated block, or else "NULL".
ysr@777 1771
ysr@777 1772 HeapWord* G1CollectedHeap::expand_and_allocate(size_t word_size) {
tonyp@2472 1773 assert_at_safepoint(true /* should_be_vm_thread */);
tonyp@2472 1774
tonyp@2472 1775 verify_region_sets_optional();
tonyp@2315 1776
johnc@2504 1777 size_t expand_bytes = MAX2(word_size * HeapWordSize, MinHeapDeltaBytes);
johnc@2504 1778 if (expand(expand_bytes)) {
johnc@2504 1779 verify_region_sets_optional();
johnc@2504 1780 return attempt_allocation_at_safepoint(word_size,
johnc@2504 1781 false /* expect_null_cur_alloc_region */);
johnc@2504 1782 }
johnc@2504 1783 return NULL;
ysr@777 1784 }
ysr@777 1785
johnc@2504 1786 bool G1CollectedHeap::expand(size_t expand_bytes) {
ysr@777 1787 size_t old_mem_size = _g1_storage.committed_size();
johnc@2504 1788 size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes);
ysr@777 1789 aligned_expand_bytes = align_size_up(aligned_expand_bytes,
ysr@777 1790 HeapRegion::GrainBytes);
johnc@2504 1791
johnc@2504 1792 if (Verbose && PrintGC) {
johnc@2504 1793 gclog_or_tty->print("Expanding garbage-first heap from %ldK by %ldK",
johnc@2504 1794 old_mem_size/K, aligned_expand_bytes/K);
johnc@2504 1795 }
johnc@2504 1796
johnc@2504 1797 HeapWord* old_end = (HeapWord*)_g1_storage.high();
johnc@2504 1798 bool successful = _g1_storage.expand_by(aligned_expand_bytes);
johnc@2504 1799 if (successful) {
johnc@2504 1800 HeapWord* new_end = (HeapWord*)_g1_storage.high();
johnc@2504 1801
johnc@2504 1802 // Expand the committed region.
johnc@2504 1803 _g1_committed.set_end(new_end);
johnc@2504 1804
johnc@2504 1805 // Tell the cardtable about the expansion.
johnc@2504 1806 Universe::heap()->barrier_set()->resize_covered_region(_g1_committed);
johnc@2504 1807
johnc@2504 1808 // And the offset table as well.
johnc@2504 1809 _bot_shared->resize(_g1_committed.word_size());
johnc@2504 1810
johnc@2504 1811 expand_bytes = aligned_expand_bytes;
johnc@2504 1812 HeapWord* base = old_end;
johnc@2504 1813
johnc@2504 1814 // Create the heap regions for [old_end, new_end)
johnc@2504 1815 while (expand_bytes > 0) {
johnc@2504 1816 HeapWord* high = base + HeapRegion::GrainWords;
johnc@2504 1817
ysr@777 1818 // Create a new HeapRegion.
ysr@777 1819 MemRegion mr(base, high);
ysr@777 1820 bool is_zeroed = !_g1_max_committed.contains(base);
ysr@777 1821 HeapRegion* hr = new HeapRegion(_bot_shared, mr, is_zeroed);
ysr@777 1822
ysr@777 1823 // Add it to the HeapRegionSeq.
ysr@777 1824 _hrs->insert(hr);
tonyp@2472 1825 _free_list.add_as_tail(hr);
johnc@2504 1826
ysr@777 1827 // And we used up an expansion region to create it.
ysr@777 1828 _expansion_regions--;
johnc@2504 1829
johnc@2504 1830 expand_bytes -= HeapRegion::GrainBytes;
johnc@2504 1831 base += HeapRegion::GrainWords;
johnc@2504 1832 }
johnc@2504 1833 assert(base == new_end, "sanity");
johnc@2504 1834
johnc@2504 1835 // Now update max_committed if necessary.
johnc@2504 1836 _g1_max_committed.set_end(MAX2(_g1_max_committed.end(), new_end));
johnc@2504 1837
johnc@2504 1838 } else {
johnc@2504 1839 // The expansion of the virtual storage space was unsuccessful.
johnc@2504 1840 // Let's see if it was because we ran out of swap.
johnc@2504 1841 if (G1ExitOnExpansionFailure &&
johnc@2504 1842 _g1_storage.uncommitted_size() >= aligned_expand_bytes) {
johnc@2504 1843 // We had head room...
johnc@2504 1844 vm_exit_out_of_memory(aligned_expand_bytes, "G1 heap expansion");
ysr@777 1845 }
ysr@777 1846 }
tonyp@2472 1847
ysr@777 1848 if (Verbose && PrintGC) {
ysr@777 1849 size_t new_mem_size = _g1_storage.committed_size();
johnc@2504 1850 gclog_or_tty->print_cr("...%s, expanded to %ldK",
johnc@2504 1851 (successful ? "Successful" : "Failed"),
ysr@777 1852 new_mem_size/K);
ysr@777 1853 }
johnc@2504 1854 return successful;
ysr@777 1855 }
ysr@777 1856
ysr@777 1857 void G1CollectedHeap::shrink_helper(size_t shrink_bytes)
ysr@777 1858 {
ysr@777 1859 size_t old_mem_size = _g1_storage.committed_size();
ysr@777 1860 size_t aligned_shrink_bytes =
ysr@777 1861 ReservedSpace::page_align_size_down(shrink_bytes);
ysr@777 1862 aligned_shrink_bytes = align_size_down(aligned_shrink_bytes,
ysr@777 1863 HeapRegion::GrainBytes);
ysr@777 1864 size_t num_regions_deleted = 0;
ysr@777 1865 MemRegion mr = _hrs->shrink_by(aligned_shrink_bytes, num_regions_deleted);
ysr@777 1866
ysr@777 1867 assert(mr.end() == (HeapWord*)_g1_storage.high(), "Bad shrink!");
ysr@777 1868 if (mr.byte_size() > 0)
ysr@777 1869 _g1_storage.shrink_by(mr.byte_size());
ysr@777 1870 assert(mr.start() == (HeapWord*)_g1_storage.high(), "Bad shrink!");
ysr@777 1871
ysr@777 1872 _g1_committed.set_end(mr.start());
ysr@777 1873 _expansion_regions += num_regions_deleted;
ysr@777 1874
ysr@777 1875 // Tell the cardtable about it.
ysr@777 1876 Universe::heap()->barrier_set()->resize_covered_region(_g1_committed);
ysr@777 1877
ysr@777 1878 // And the offset table as well.
ysr@777 1879 _bot_shared->resize(_g1_committed.word_size());
ysr@777 1880
ysr@777 1881 HeapRegionRemSet::shrink_heap(n_regions());
ysr@777 1882
ysr@777 1883 if (Verbose && PrintGC) {
ysr@777 1884 size_t new_mem_size = _g1_storage.committed_size();
ysr@777 1885 gclog_or_tty->print_cr("Shrinking garbage-first heap from %ldK by %ldK to %ldK",
ysr@777 1886 old_mem_size/K, aligned_shrink_bytes/K,
ysr@777 1887 new_mem_size/K);
ysr@777 1888 }
ysr@777 1889 }
ysr@777 1890
ysr@777 1891 void G1CollectedHeap::shrink(size_t shrink_bytes) {
tonyp@2472 1892 verify_region_sets_optional();
tonyp@2472 1893
tonyp@1071 1894 release_gc_alloc_regions(true /* totally */);
tonyp@2472 1895 // Instead of tearing down / rebuilding the free lists here, we
tonyp@2472 1896 // could instead use the remove_all_pending() method on free_list to
tonyp@2472 1897 // remove only the ones that we need to remove.
ysr@777 1898 tear_down_region_lists(); // We will rebuild them in a moment.
ysr@777 1899 shrink_helper(shrink_bytes);
ysr@777 1900 rebuild_region_lists();
tonyp@2472 1901
tonyp@2472 1902 verify_region_sets_optional();
ysr@777 1903 }
ysr@777 1904
ysr@777 1905 // Public methods.
ysr@777 1906
ysr@777 1907 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
ysr@777 1908 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
ysr@777 1909 #endif // _MSC_VER
ysr@777 1910
ysr@777 1911
ysr@777 1912 G1CollectedHeap::G1CollectedHeap(G1CollectorPolicy* policy_) :
ysr@777 1913 SharedHeap(policy_),
ysr@777 1914 _g1_policy(policy_),
iveresov@1546 1915 _dirty_card_queue_set(false),
johnc@2060 1916 _into_cset_dirty_card_queue_set(false),
johnc@2379 1917 _is_alive_closure(this),
ysr@777 1918 _ref_processor(NULL),
ysr@777 1919 _process_strong_tasks(new SubTasksDone(G1H_PS_NumElements)),
ysr@777 1920 _bot_shared(NULL),
ysr@777 1921 _objs_with_preserved_marks(NULL), _preserved_marks_of_objs(NULL),
ysr@777 1922 _evac_failure_scan_stack(NULL) ,
ysr@777 1923 _mark_in_progress(false),
tonyp@2472 1924 _cg1r(NULL), _summary_bytes_used(0),
ysr@777 1925 _cur_alloc_region(NULL),
ysr@777 1926 _refine_cte_cl(NULL),
ysr@777 1927 _full_collection(false),
tonyp@2472 1928 _free_list("Master Free List"),
tonyp@2472 1929 _secondary_free_list("Secondary Free List"),
tonyp@2472 1930 _humongous_set("Master Humongous Set"),
tonyp@2472 1931 _free_regions_coming(false),
ysr@777 1932 _young_list(new YoungList(this)),
ysr@777 1933 _gc_time_stamp(0),
tonyp@961 1934 _surviving_young_words(NULL),
tonyp@2011 1935 _full_collections_completed(0),
tonyp@961 1936 _in_cset_fast_test(NULL),
apetrusenko@1231 1937 _in_cset_fast_test_base(NULL),
apetrusenko@1231 1938 _dirty_cards_region_list(NULL) {
ysr@777 1939 _g1h = this; // To catch bugs.
ysr@777 1940 if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) {
ysr@777 1941 vm_exit_during_initialization("Failed necessary allocation.");
ysr@777 1942 }
tonyp@1377 1943
tonyp@1377 1944 _humongous_object_threshold_in_words = HeapRegion::GrainWords / 2;
tonyp@1377 1945
ysr@777 1946 int n_queues = MAX2((int)ParallelGCThreads, 1);
ysr@777 1947 _task_queues = new RefToScanQueueSet(n_queues);
ysr@777 1948
ysr@777 1949 int n_rem_sets = HeapRegionRemSet::num_par_rem_sets();
ysr@777 1950 assert(n_rem_sets > 0, "Invariant.");
ysr@777 1951
ysr@777 1952 HeapRegionRemSetIterator** iter_arr =
ysr@777 1953 NEW_C_HEAP_ARRAY(HeapRegionRemSetIterator*, n_queues);
ysr@777 1954 for (int i = 0; i < n_queues; i++) {
ysr@777 1955 iter_arr[i] = new HeapRegionRemSetIterator();
ysr@777 1956 }
ysr@777 1957 _rem_set_iterator = iter_arr;
ysr@777 1958
ysr@777 1959 for (int i = 0; i < n_queues; i++) {
ysr@777 1960 RefToScanQueue* q = new RefToScanQueue();
ysr@777 1961 q->initialize();
ysr@777 1962 _task_queues->register_queue(i, q);
ysr@777 1963 }
ysr@777 1964
ysr@777 1965 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
tonyp@1071 1966 _gc_alloc_regions[ap] = NULL;
tonyp@1071 1967 _gc_alloc_region_counts[ap] = 0;
tonyp@1071 1968 _retained_gc_alloc_regions[ap] = NULL;
tonyp@1071 1969 // by default, we do not retain a GC alloc region for each ap;
tonyp@1071 1970 // we'll override this, when appropriate, below
tonyp@1071 1971 _retain_gc_alloc_region[ap] = false;
tonyp@1071 1972 }
tonyp@1071 1973
tonyp@1071 1974 // We will try to remember the last half-full tenured region we
tonyp@1071 1975 // allocated to at the end of a collection so that we can re-use it
tonyp@1071 1976 // during the next collection.
tonyp@1071 1977 _retain_gc_alloc_region[GCAllocForTenured] = true;
tonyp@1071 1978
ysr@777 1979 guarantee(_task_queues != NULL, "task_queues allocation failure.");
ysr@777 1980 }
ysr@777 1981
ysr@777 1982 jint G1CollectedHeap::initialize() {
ysr@1601 1983 CollectedHeap::pre_initialize();
ysr@777 1984 os::enable_vtime();
ysr@777 1985
ysr@777 1986 // Necessary to satisfy locking discipline assertions.
ysr@777 1987
ysr@777 1988 MutexLocker x(Heap_lock);
ysr@777 1989
ysr@777 1990 // While there are no constraints in the GC code that HeapWordSize
ysr@777 1991 // be any particular value, there are multiple other areas in the
ysr@777 1992 // system which believe this to be true (e.g. oop->object_size in some
ysr@777 1993 // cases incorrectly returns the size in wordSize units rather than
ysr@777 1994 // HeapWordSize).
ysr@777 1995 guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
ysr@777 1996
ysr@777 1997 size_t init_byte_size = collector_policy()->initial_heap_byte_size();
ysr@777 1998 size_t max_byte_size = collector_policy()->max_heap_byte_size();
ysr@777 1999
ysr@777 2000 // Ensure that the sizes are properly aligned.
ysr@777 2001 Universe::check_alignment(init_byte_size, HeapRegion::GrainBytes, "g1 heap");
ysr@777 2002 Universe::check_alignment(max_byte_size, HeapRegion::GrainBytes, "g1 heap");
ysr@777 2003
ysr@777 2004 _cg1r = new ConcurrentG1Refine();
ysr@777 2005
ysr@777 2006 // Reserve the maximum.
ysr@777 2007 PermanentGenerationSpec* pgs = collector_policy()->permanent_generation();
ysr@777 2008 // Includes the perm-gen.
kvn@1077 2009
kvn@1077 2010 const size_t total_reserved = max_byte_size + pgs->max_size();
kvn@1077 2011 char* addr = Universe::preferred_heap_base(total_reserved, Universe::UnscaledNarrowOop);
kvn@1077 2012
ysr@777 2013 ReservedSpace heap_rs(max_byte_size + pgs->max_size(),
ysr@777 2014 HeapRegion::GrainBytes,
brutisso@2455 2015 UseLargePages, addr);
kvn@1077 2016
kvn@1077 2017 if (UseCompressedOops) {
kvn@1077 2018 if (addr != NULL && !heap_rs.is_reserved()) {
kvn@1077 2019 // Failed to reserve at specified address - the requested memory
kvn@1077 2020 // region is taken already, for example, by 'java' launcher.
kvn@1077 2021 // Try again to reserver heap higher.
kvn@1077 2022 addr = Universe::preferred_heap_base(total_reserved, Universe::ZeroBasedNarrowOop);
kvn@1077 2023 ReservedSpace heap_rs0(total_reserved, HeapRegion::GrainBytes,
brutisso@2455 2024 UseLargePages, addr);
kvn@1077 2025 if (addr != NULL && !heap_rs0.is_reserved()) {
kvn@1077 2026 // Failed to reserve at specified address again - give up.
kvn@1077 2027 addr = Universe::preferred_heap_base(total_reserved, Universe::HeapBasedNarrowOop);
kvn@1077 2028 assert(addr == NULL, "");
kvn@1077 2029 ReservedSpace heap_rs1(total_reserved, HeapRegion::GrainBytes,
brutisso@2455 2030 UseLargePages, addr);
kvn@1077 2031 heap_rs = heap_rs1;
kvn@1077 2032 } else {
kvn@1077 2033 heap_rs = heap_rs0;
kvn@1077 2034 }
kvn@1077 2035 }
kvn@1077 2036 }
ysr@777 2037
ysr@777 2038 if (!heap_rs.is_reserved()) {
ysr@777 2039 vm_exit_during_initialization("Could not reserve enough space for object heap");
ysr@777 2040 return JNI_ENOMEM;
ysr@777 2041 }
ysr@777 2042
ysr@777 2043 // It is important to do this in a way such that concurrent readers can't
ysr@777 2044 // temporarily think somethings in the heap. (I've actually seen this
ysr@777 2045 // happen in asserts: DLD.)
ysr@777 2046 _reserved.set_word_size(0);
ysr@777 2047 _reserved.set_start((HeapWord*)heap_rs.base());
ysr@777 2048 _reserved.set_end((HeapWord*)(heap_rs.base() + heap_rs.size()));
ysr@777 2049
ysr@777 2050 _expansion_regions = max_byte_size/HeapRegion::GrainBytes;
ysr@777 2051
ysr@777 2052 // Create the gen rem set (and barrier set) for the entire reserved region.
ysr@777 2053 _rem_set = collector_policy()->create_rem_set(_reserved, 2);
ysr@777 2054 set_barrier_set(rem_set()->bs());
ysr@777 2055 if (barrier_set()->is_a(BarrierSet::ModRef)) {
ysr@777 2056 _mr_bs = (ModRefBarrierSet*)_barrier_set;
ysr@777 2057 } else {
ysr@777 2058 vm_exit_during_initialization("G1 requires a mod ref bs.");
ysr@777 2059 return JNI_ENOMEM;
ysr@777 2060 }
ysr@777 2061
ysr@777 2062 // Also create a G1 rem set.
johnc@2216 2063 if (mr_bs()->is_a(BarrierSet::CardTableModRef)) {
johnc@2216 2064 _g1_rem_set = new G1RemSet(this, (CardTableModRefBS*)mr_bs());
ysr@777 2065 } else {
johnc@2216 2066 vm_exit_during_initialization("G1 requires a cardtable mod ref bs.");
johnc@2216 2067 return JNI_ENOMEM;
ysr@777 2068 }
ysr@777 2069
ysr@777 2070 // Carve out the G1 part of the heap.
ysr@777 2071
ysr@777 2072 ReservedSpace g1_rs = heap_rs.first_part(max_byte_size);
ysr@777 2073 _g1_reserved = MemRegion((HeapWord*)g1_rs.base(),
ysr@777 2074 g1_rs.size()/HeapWordSize);
ysr@777 2075 ReservedSpace perm_gen_rs = heap_rs.last_part(max_byte_size);
ysr@777 2076
ysr@777 2077 _perm_gen = pgs->init(perm_gen_rs, pgs->init_size(), rem_set());
ysr@777 2078
ysr@777 2079 _g1_storage.initialize(g1_rs, 0);
ysr@777 2080 _g1_committed = MemRegion((HeapWord*)_g1_storage.low(), (size_t) 0);
ysr@777 2081 _g1_max_committed = _g1_committed;
iveresov@828 2082 _hrs = new HeapRegionSeq(_expansion_regions);
ysr@777 2083 guarantee(_hrs != NULL, "Couldn't allocate HeapRegionSeq");
ysr@777 2084 guarantee(_cur_alloc_region == NULL, "from constructor");
ysr@777 2085
johnc@1242 2086 // 6843694 - ensure that the maximum region index can fit
johnc@1242 2087 // in the remembered set structures.
johnc@1242 2088 const size_t max_region_idx = ((size_t)1 << (sizeof(RegionIdx_t)*BitsPerByte-1)) - 1;
johnc@1242 2089 guarantee((max_regions() - 1) <= max_region_idx, "too many regions");
johnc@1242 2090
johnc@1242 2091 size_t max_cards_per_region = ((size_t)1 << (sizeof(CardIdx_t)*BitsPerByte-1)) - 1;
tonyp@1377 2092 guarantee(HeapRegion::CardsPerRegion > 0, "make sure it's initialized");
tonyp@1377 2093 guarantee((size_t) HeapRegion::CardsPerRegion < max_cards_per_region,
tonyp@1377 2094 "too many cards per region");
johnc@1242 2095
tonyp@2472 2096 HeapRegionSet::set_unrealistically_long_length(max_regions() + 1);
tonyp@2472 2097
ysr@777 2098 _bot_shared = new G1BlockOffsetSharedArray(_reserved,
ysr@777 2099 heap_word_size(init_byte_size));
ysr@777 2100
ysr@777 2101 _g1h = this;
ysr@777 2102
johnc@1829 2103 _in_cset_fast_test_length = max_regions();
johnc@1829 2104 _in_cset_fast_test_base = NEW_C_HEAP_ARRAY(bool, _in_cset_fast_test_length);
johnc@1829 2105
johnc@1829 2106 // We're biasing _in_cset_fast_test to avoid subtracting the
johnc@1829 2107 // beginning of the heap every time we want to index; basically
johnc@1829 2108 // it's the same with what we do with the card table.
johnc@1829 2109 _in_cset_fast_test = _in_cset_fast_test_base -
johnc@1829 2110 ((size_t) _g1_reserved.start() >> HeapRegion::LogOfHRGrainBytes);
johnc@1829 2111
johnc@1829 2112 // Clear the _cset_fast_test bitmap in anticipation of adding
johnc@1829 2113 // regions to the incremental collection set for the first
johnc@1829 2114 // evacuation pause.
johnc@1829 2115 clear_cset_fast_test();
johnc@1829 2116
ysr@777 2117 // Create the ConcurrentMark data structure and thread.
ysr@777 2118 // (Must do this late, so that "max_regions" is defined.)
ysr@777 2119 _cm = new ConcurrentMark(heap_rs, (int) max_regions());
ysr@777 2120 _cmThread = _cm->cmThread();
ysr@777 2121
ysr@777 2122 // Initialize the from_card cache structure of HeapRegionRemSet.
ysr@777 2123 HeapRegionRemSet::init_heap(max_regions());
ysr@777 2124
apetrusenko@1112 2125 // Now expand into the initial heap size.
johnc@2504 2126 if (!expand(init_byte_size)) {
johnc@2504 2127 vm_exit_during_initialization("Failed to allocate initial heap.");
johnc@2504 2128 return JNI_ENOMEM;
johnc@2504 2129 }
ysr@777 2130
ysr@777 2131 // Perform any initialization actions delegated to the policy.
ysr@777 2132 g1_policy()->init();
ysr@777 2133
ysr@777 2134 g1_policy()->note_start_of_mark_thread();
ysr@777 2135
ysr@777 2136 _refine_cte_cl =
ysr@777 2137 new RefineCardTableEntryClosure(ConcurrentG1RefineThread::sts(),
ysr@777 2138 g1_rem_set(),
ysr@777 2139 concurrent_g1_refine());
ysr@777 2140 JavaThread::dirty_card_queue_set().set_closure(_refine_cte_cl);
ysr@777 2141
ysr@777 2142 JavaThread::satb_mark_queue_set().initialize(SATB_Q_CBL_mon,
ysr@777 2143 SATB_Q_FL_lock,
iveresov@1546 2144 G1SATBProcessCompletedThreshold,
ysr@777 2145 Shared_SATB_Q_lock);
iveresov@1229 2146
iveresov@1229 2147 JavaThread::dirty_card_queue_set().initialize(DirtyCardQ_CBL_mon,
iveresov@1229 2148 DirtyCardQ_FL_lock,
iveresov@1546 2149 concurrent_g1_refine()->yellow_zone(),
iveresov@1546 2150 concurrent_g1_refine()->red_zone(),
iveresov@1229 2151 Shared_DirtyCardQ_lock);
iveresov@1229 2152
iveresov@1051 2153 if (G1DeferredRSUpdate) {
iveresov@1051 2154 dirty_card_queue_set().initialize(DirtyCardQ_CBL_mon,
iveresov@1051 2155 DirtyCardQ_FL_lock,
iveresov@1546 2156 -1, // never trigger processing
iveresov@1546 2157 -1, // no limit on length
iveresov@1051 2158 Shared_DirtyCardQ_lock,
iveresov@1051 2159 &JavaThread::dirty_card_queue_set());
iveresov@1051 2160 }
johnc@2060 2161
johnc@2060 2162 // Initialize the card queue set used to hold cards containing
johnc@2060 2163 // references into the collection set.
johnc@2060 2164 _into_cset_dirty_card_queue_set.initialize(DirtyCardQ_CBL_mon,
johnc@2060 2165 DirtyCardQ_FL_lock,
johnc@2060 2166 -1, // never trigger processing
johnc@2060 2167 -1, // no limit on length
johnc@2060 2168 Shared_DirtyCardQ_lock,
johnc@2060 2169 &JavaThread::dirty_card_queue_set());
johnc@2060 2170
ysr@777 2171 // In case we're keeping closure specialization stats, initialize those
ysr@777 2172 // counts and that mechanism.
ysr@777 2173 SpecializationStats::clear();
ysr@777 2174
ysr@777 2175 _gc_alloc_region_list = NULL;
ysr@777 2176
ysr@777 2177 // Do later initialization work for concurrent refinement.
ysr@777 2178 _cg1r->init();
ysr@777 2179
ysr@777 2180 return JNI_OK;
ysr@777 2181 }
ysr@777 2182
ysr@777 2183 void G1CollectedHeap::ref_processing_init() {
johnc@2316 2184 // Reference processing in G1 currently works as follows:
johnc@2316 2185 //
johnc@2316 2186 // * There is only one reference processor instance that
johnc@2316 2187 // 'spans' the entire heap. It is created by the code
johnc@2316 2188 // below.
johnc@2316 2189 // * Reference discovery is not enabled during an incremental
johnc@2316 2190 // pause (see 6484982).
johnc@2316 2191 // * Discoverered refs are not enqueued nor are they processed
johnc@2316 2192 // during an incremental pause (see 6484982).
johnc@2316 2193 // * Reference discovery is enabled at initial marking.
johnc@2316 2194 // * Reference discovery is disabled and the discovered
johnc@2316 2195 // references processed etc during remarking.
johnc@2316 2196 // * Reference discovery is MT (see below).
johnc@2316 2197 // * Reference discovery requires a barrier (see below).
johnc@2316 2198 // * Reference processing is currently not MT (see 6608385).
johnc@2316 2199 // * A full GC enables (non-MT) reference discovery and
johnc@2316 2200 // processes any discovered references.
johnc@2316 2201
ysr@777 2202 SharedHeap::ref_processing_init();
ysr@777 2203 MemRegion mr = reserved_region();
ysr@777 2204 _ref_processor = ReferenceProcessor::create_ref_processor(
ysr@777 2205 mr, // span
ysr@777 2206 false, // Reference discovery is not atomic
ysr@777 2207 true, // mt_discovery
johnc@2379 2208 &_is_alive_closure, // is alive closure
johnc@2379 2209 // for efficiency
ysr@777 2210 ParallelGCThreads,
ysr@777 2211 ParallelRefProcEnabled,
ysr@777 2212 true); // Setting next fields of discovered
ysr@777 2213 // lists requires a barrier.
ysr@777 2214 }
ysr@777 2215
ysr@777 2216 size_t G1CollectedHeap::capacity() const {
ysr@777 2217 return _g1_committed.byte_size();
ysr@777 2218 }
ysr@777 2219
johnc@2060 2220 void G1CollectedHeap::iterate_dirty_card_closure(CardTableEntryClosure* cl,
johnc@2060 2221 DirtyCardQueue* into_cset_dcq,
johnc@2060 2222 bool concurrent,
ysr@777 2223 int worker_i) {
johnc@1324 2224 // Clean cards in the hot card cache
johnc@2060 2225 concurrent_g1_refine()->clean_up_cache(worker_i, g1_rem_set(), into_cset_dcq);
johnc@1324 2226
ysr@777 2227 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
ysr@777 2228 int n_completed_buffers = 0;
johnc@2060 2229 while (dcqs.apply_closure_to_completed_buffer(cl, worker_i, 0, true)) {
ysr@777 2230 n_completed_buffers++;
ysr@777 2231 }
ysr@777 2232 g1_policy()->record_update_rs_processed_buffers(worker_i,
ysr@777 2233 (double) n_completed_buffers);
ysr@777 2234 dcqs.clear_n_completed_buffers();
ysr@777 2235 assert(!dcqs.completed_buffers_exist_dirty(), "Completed buffers exist!");
ysr@777 2236 }
ysr@777 2237
ysr@777 2238
ysr@777 2239 // Computes the sum of the storage used by the various regions.
ysr@777 2240
ysr@777 2241 size_t G1CollectedHeap::used() const {
ysr@1297 2242 assert(Heap_lock->owner() != NULL,
ysr@1297 2243 "Should be owned on this thread's behalf.");
ysr@777 2244 size_t result = _summary_bytes_used;
ysr@1280 2245 // Read only once in case it is set to NULL concurrently
ysr@1280 2246 HeapRegion* hr = _cur_alloc_region;
ysr@1280 2247 if (hr != NULL)
ysr@1280 2248 result += hr->used();
ysr@777 2249 return result;
ysr@777 2250 }
ysr@777 2251
tonyp@1281 2252 size_t G1CollectedHeap::used_unlocked() const {
tonyp@1281 2253 size_t result = _summary_bytes_used;
tonyp@1281 2254 return result;
tonyp@1281 2255 }
tonyp@1281 2256
ysr@777 2257 class SumUsedClosure: public HeapRegionClosure {
ysr@777 2258 size_t _used;
ysr@777 2259 public:
ysr@777 2260 SumUsedClosure() : _used(0) {}
ysr@777 2261 bool doHeapRegion(HeapRegion* r) {
ysr@777 2262 if (!r->continuesHumongous()) {
ysr@777 2263 _used += r->used();
ysr@777 2264 }
ysr@777 2265 return false;
ysr@777 2266 }
ysr@777 2267 size_t result() { return _used; }
ysr@777 2268 };
ysr@777 2269
ysr@777 2270 size_t G1CollectedHeap::recalculate_used() const {
ysr@777 2271 SumUsedClosure blk;
ysr@777 2272 _hrs->iterate(&blk);
ysr@777 2273 return blk.result();
ysr@777 2274 }
ysr@777 2275
ysr@777 2276 #ifndef PRODUCT
ysr@777 2277 class SumUsedRegionsClosure: public HeapRegionClosure {
ysr@777 2278 size_t _num;
ysr@777 2279 public:
apetrusenko@1112 2280 SumUsedRegionsClosure() : _num(0) {}
ysr@777 2281 bool doHeapRegion(HeapRegion* r) {
ysr@777 2282 if (r->continuesHumongous() || r->used() > 0 || r->is_gc_alloc_region()) {
ysr@777 2283 _num += 1;
ysr@777 2284 }
ysr@777 2285 return false;
ysr@777 2286 }
ysr@777 2287 size_t result() { return _num; }
ysr@777 2288 };
ysr@777 2289
ysr@777 2290 size_t G1CollectedHeap::recalculate_used_regions() const {
ysr@777 2291 SumUsedRegionsClosure blk;
ysr@777 2292 _hrs->iterate(&blk);
ysr@777 2293 return blk.result();
ysr@777 2294 }
ysr@777 2295 #endif // PRODUCT
ysr@777 2296
ysr@777 2297 size_t G1CollectedHeap::unsafe_max_alloc() {
tonyp@2472 2298 if (free_regions() > 0) return HeapRegion::GrainBytes;
ysr@777 2299 // otherwise, is there space in the current allocation region?
ysr@777 2300
ysr@777 2301 // We need to store the current allocation region in a local variable
ysr@777 2302 // here. The problem is that this method doesn't take any locks and
ysr@777 2303 // there may be other threads which overwrite the current allocation
ysr@777 2304 // region field. attempt_allocation(), for example, sets it to NULL
ysr@777 2305 // and this can happen *after* the NULL check here but before the call
ysr@777 2306 // to free(), resulting in a SIGSEGV. Note that this doesn't appear
ysr@777 2307 // to be a problem in the optimized build, since the two loads of the
ysr@777 2308 // current allocation region field are optimized away.
ysr@777 2309 HeapRegion* car = _cur_alloc_region;
ysr@777 2310
ysr@777 2311 // FIXME: should iterate over all regions?
ysr@777 2312 if (car == NULL) {
ysr@777 2313 return 0;
ysr@777 2314 }
ysr@777 2315 return car->free();
ysr@777 2316 }
ysr@777 2317
tonyp@2011 2318 bool G1CollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
tonyp@2011 2319 return
tonyp@2011 2320 ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
tonyp@2011 2321 (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent));
tonyp@2011 2322 }
tonyp@2011 2323
tonyp@2372 2324 void G1CollectedHeap::increment_full_collections_completed(bool concurrent) {
tonyp@2011 2325 MonitorLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
tonyp@2011 2326
tonyp@2372 2327 // We assume that if concurrent == true, then the caller is a
tonyp@2372 2328 // concurrent thread that was joined the Suspendible Thread
tonyp@2372 2329 // Set. If there's ever a cheap way to check this, we should add an
tonyp@2372 2330 // assert here.
tonyp@2372 2331
tonyp@2011 2332 // We have already incremented _total_full_collections at the start
tonyp@2011 2333 // of the GC, so total_full_collections() represents how many full
tonyp@2011 2334 // collections have been started.
tonyp@2011 2335 unsigned int full_collections_started = total_full_collections();
tonyp@2011 2336
tonyp@2011 2337 // Given that this method is called at the end of a Full GC or of a
tonyp@2011 2338 // concurrent cycle, and those can be nested (i.e., a Full GC can
tonyp@2011 2339 // interrupt a concurrent cycle), the number of full collections
tonyp@2011 2340 // completed should be either one (in the case where there was no
tonyp@2011 2341 // nesting) or two (when a Full GC interrupted a concurrent cycle)
tonyp@2011 2342 // behind the number of full collections started.
tonyp@2011 2343
tonyp@2011 2344 // This is the case for the inner caller, i.e. a Full GC.
tonyp@2372 2345 assert(concurrent ||
tonyp@2011 2346 (full_collections_started == _full_collections_completed + 1) ||
tonyp@2011 2347 (full_collections_started == _full_collections_completed + 2),
tonyp@2372 2348 err_msg("for inner caller (Full GC): full_collections_started = %u "
tonyp@2011 2349 "is inconsistent with _full_collections_completed = %u",
tonyp@2011 2350 full_collections_started, _full_collections_completed));
tonyp@2011 2351
tonyp@2011 2352 // This is the case for the outer caller, i.e. the concurrent cycle.
tonyp@2372 2353 assert(!concurrent ||
tonyp@2011 2354 (full_collections_started == _full_collections_completed + 1),
tonyp@2372 2355 err_msg("for outer caller (concurrent cycle): "
tonyp@2372 2356 "full_collections_started = %u "
tonyp@2011 2357 "is inconsistent with _full_collections_completed = %u",
tonyp@2011 2358 full_collections_started, _full_collections_completed));
tonyp@2011 2359
tonyp@2011 2360 _full_collections_completed += 1;
tonyp@2011 2361
johnc@2195 2362 // We need to clear the "in_progress" flag in the CM thread before
johnc@2195 2363 // we wake up any waiters (especially when ExplicitInvokesConcurrent
johnc@2195 2364 // is set) so that if a waiter requests another System.gc() it doesn't
johnc@2195 2365 // incorrectly see that a marking cyle is still in progress.
tonyp@2372 2366 if (concurrent) {
johnc@2195 2367 _cmThread->clear_in_progress();
johnc@2195 2368 }
johnc@2195 2369
tonyp@2011 2370 // This notify_all() will ensure that a thread that called
tonyp@2011 2371 // System.gc() with (with ExplicitGCInvokesConcurrent set or not)
tonyp@2011 2372 // and it's waiting for a full GC to finish will be woken up. It is
tonyp@2011 2373 // waiting in VM_G1IncCollectionPause::doit_epilogue().
tonyp@2011 2374 FullGCCount_lock->notify_all();
tonyp@2011 2375 }
tonyp@2011 2376
ysr@777 2377 void G1CollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
tonyp@2472 2378 assert_at_safepoint(true /* should_be_vm_thread */);
ysr@777 2379 GCCauseSetter gcs(this, cause);
ysr@777 2380 switch (cause) {
ysr@777 2381 case GCCause::_heap_inspection:
ysr@777 2382 case GCCause::_heap_dump: {
ysr@777 2383 HandleMark hm;
ysr@777 2384 do_full_collection(false); // don't clear all soft refs
ysr@777 2385 break;
ysr@777 2386 }
ysr@777 2387 default: // XXX FIX ME
ysr@777 2388 ShouldNotReachHere(); // Unexpected use of this function
ysr@777 2389 }
ysr@777 2390 }
ysr@777 2391
ysr@1523 2392 void G1CollectedHeap::collect(GCCause::Cause cause) {
ysr@1523 2393 // The caller doesn't have the Heap_lock
ysr@1523 2394 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
ysr@1523 2395
tonyp@2011 2396 unsigned int gc_count_before;
tonyp@2011 2397 unsigned int full_gc_count_before;
ysr@777 2398 {
ysr@1523 2399 MutexLocker ml(Heap_lock);
tonyp@2315 2400
ysr@1523 2401 // Read the GC count while holding the Heap_lock
ysr@1523 2402 gc_count_before = SharedHeap::heap()->total_collections();
tonyp@2011 2403 full_gc_count_before = SharedHeap::heap()->total_full_collections();
tonyp@2011 2404 }
tonyp@2011 2405
tonyp@2011 2406 if (should_do_concurrent_full_gc(cause)) {
tonyp@2011 2407 // Schedule an initial-mark evacuation pause that will start a
tonyp@2315 2408 // concurrent cycle. We're setting word_size to 0 which means that
tonyp@2315 2409 // we are not requesting a post-GC allocation.
tonyp@2011 2410 VM_G1IncCollectionPause op(gc_count_before,
tonyp@2315 2411 0, /* word_size */
tonyp@2315 2412 true, /* should_initiate_conc_mark */
tonyp@2011 2413 g1_policy()->max_pause_time_ms(),
tonyp@2011 2414 cause);
tonyp@2011 2415 VMThread::execute(&op);
tonyp@2011 2416 } else {
tonyp@2011 2417 if (cause == GCCause::_gc_locker
tonyp@2011 2418 DEBUG_ONLY(|| cause == GCCause::_scavenge_alot)) {
tonyp@2011 2419
tonyp@2315 2420 // Schedule a standard evacuation pause. We're setting word_size
tonyp@2315 2421 // to 0 which means that we are not requesting a post-GC allocation.
tonyp@2011 2422 VM_G1IncCollectionPause op(gc_count_before,
tonyp@2315 2423 0, /* word_size */
tonyp@2011 2424 false, /* should_initiate_conc_mark */
tonyp@2011 2425 g1_policy()->max_pause_time_ms(),
tonyp@2011 2426 cause);
ysr@1523 2427 VMThread::execute(&op);
tonyp@2011 2428 } else {
tonyp@2011 2429 // Schedule a Full GC.
tonyp@2011 2430 VM_G1CollectFull op(gc_count_before, full_gc_count_before, cause);
ysr@1523 2431 VMThread::execute(&op);
ysr@1523 2432 }
ysr@777 2433 }
ysr@777 2434 }
ysr@777 2435
ysr@777 2436 bool G1CollectedHeap::is_in(const void* p) const {
ysr@777 2437 if (_g1_committed.contains(p)) {
ysr@777 2438 HeapRegion* hr = _hrs->addr_to_region(p);
ysr@777 2439 return hr->is_in(p);
ysr@777 2440 } else {
ysr@777 2441 return _perm_gen->as_gen()->is_in(p);
ysr@777 2442 }
ysr@777 2443 }
ysr@777 2444
ysr@777 2445 // Iteration functions.
ysr@777 2446
ysr@777 2447 // Iterates an OopClosure over all ref-containing fields of objects
ysr@777 2448 // within a HeapRegion.
ysr@777 2449
ysr@777 2450 class IterateOopClosureRegionClosure: public HeapRegionClosure {
ysr@777 2451 MemRegion _mr;
ysr@777 2452 OopClosure* _cl;
ysr@777 2453 public:
ysr@777 2454 IterateOopClosureRegionClosure(MemRegion mr, OopClosure* cl)
ysr@777 2455 : _mr(mr), _cl(cl) {}
ysr@777 2456 bool doHeapRegion(HeapRegion* r) {
ysr@777 2457 if (! r->continuesHumongous()) {
ysr@777 2458 r->oop_iterate(_cl);
ysr@777 2459 }
ysr@777 2460 return false;
ysr@777 2461 }
ysr@777 2462 };
ysr@777 2463
iveresov@1113 2464 void G1CollectedHeap::oop_iterate(OopClosure* cl, bool do_perm) {
ysr@777 2465 IterateOopClosureRegionClosure blk(_g1_committed, cl);
ysr@777 2466 _hrs->iterate(&blk);
iveresov@1113 2467 if (do_perm) {
iveresov@1113 2468 perm_gen()->oop_iterate(cl);
iveresov@1113 2469 }
ysr@777 2470 }
ysr@777 2471
iveresov@1113 2472 void G1CollectedHeap::oop_iterate(MemRegion mr, OopClosure* cl, bool do_perm) {
ysr@777 2473 IterateOopClosureRegionClosure blk(mr, cl);
ysr@777 2474 _hrs->iterate(&blk);
iveresov@1113 2475 if (do_perm) {
iveresov@1113 2476 perm_gen()->oop_iterate(cl);
iveresov@1113 2477 }
ysr@777 2478 }
ysr@777 2479
ysr@777 2480 // Iterates an ObjectClosure over all objects within a HeapRegion.
ysr@777 2481
ysr@777 2482 class IterateObjectClosureRegionClosure: public HeapRegionClosure {
ysr@777 2483 ObjectClosure* _cl;
ysr@777 2484 public:
ysr@777 2485 IterateObjectClosureRegionClosure(ObjectClosure* cl) : _cl(cl) {}
ysr@777 2486 bool doHeapRegion(HeapRegion* r) {
ysr@777 2487 if (! r->continuesHumongous()) {
ysr@777 2488 r->object_iterate(_cl);
ysr@777 2489 }
ysr@777 2490 return false;
ysr@777 2491 }
ysr@777 2492 };
ysr@777 2493
iveresov@1113 2494 void G1CollectedHeap::object_iterate(ObjectClosure* cl, bool do_perm) {
ysr@777 2495 IterateObjectClosureRegionClosure blk(cl);
ysr@777 2496 _hrs->iterate(&blk);
iveresov@1113 2497 if (do_perm) {
iveresov@1113 2498 perm_gen()->object_iterate(cl);
iveresov@1113 2499 }
ysr@777 2500 }
ysr@777 2501
ysr@777 2502 void G1CollectedHeap::object_iterate_since_last_GC(ObjectClosure* cl) {
ysr@777 2503 // FIXME: is this right?
ysr@777 2504 guarantee(false, "object_iterate_since_last_GC not supported by G1 heap");
ysr@777 2505 }
ysr@777 2506
ysr@777 2507 // Calls a SpaceClosure on a HeapRegion.
ysr@777 2508
ysr@777 2509 class SpaceClosureRegionClosure: public HeapRegionClosure {
ysr@777 2510 SpaceClosure* _cl;
ysr@777 2511 public:
ysr@777 2512 SpaceClosureRegionClosure(SpaceClosure* cl) : _cl(cl) {}
ysr@777 2513 bool doHeapRegion(HeapRegion* r) {
ysr@777 2514 _cl->do_space(r);
ysr@777 2515 return false;
ysr@777 2516 }
ysr@777 2517 };
ysr@777 2518
ysr@777 2519 void G1CollectedHeap::space_iterate(SpaceClosure* cl) {
ysr@777 2520 SpaceClosureRegionClosure blk(cl);
ysr@777 2521 _hrs->iterate(&blk);
ysr@777 2522 }
ysr@777 2523
ysr@777 2524 void G1CollectedHeap::heap_region_iterate(HeapRegionClosure* cl) {
ysr@777 2525 _hrs->iterate(cl);
ysr@777 2526 }
ysr@777 2527
ysr@777 2528 void G1CollectedHeap::heap_region_iterate_from(HeapRegion* r,
ysr@777 2529 HeapRegionClosure* cl) {
ysr@777 2530 _hrs->iterate_from(r, cl);
ysr@777 2531 }
ysr@777 2532
ysr@777 2533 void
ysr@777 2534 G1CollectedHeap::heap_region_iterate_from(int idx, HeapRegionClosure* cl) {
ysr@777 2535 _hrs->iterate_from(idx, cl);
ysr@777 2536 }
ysr@777 2537
ysr@777 2538 HeapRegion* G1CollectedHeap::region_at(size_t idx) { return _hrs->at(idx); }
ysr@777 2539
ysr@777 2540 void
ysr@777 2541 G1CollectedHeap::heap_region_par_iterate_chunked(HeapRegionClosure* cl,
ysr@777 2542 int worker,
ysr@777 2543 jint claim_value) {
tonyp@790 2544 const size_t regions = n_regions();
jmasa@2188 2545 const size_t worker_num = (G1CollectedHeap::use_parallel_gc_threads() ? ParallelGCThreads : 1);
tonyp@790 2546 // try to spread out the starting points of the workers
tonyp@790 2547 const size_t start_index = regions / worker_num * (size_t) worker;
tonyp@790 2548
tonyp@790 2549 // each worker will actually look at all regions
tonyp@790 2550 for (size_t count = 0; count < regions; ++count) {
tonyp@790 2551 const size_t index = (start_index + count) % regions;
tonyp@790 2552 assert(0 <= index && index < regions, "sanity");
tonyp@790 2553 HeapRegion* r = region_at(index);
tonyp@790 2554 // we'll ignore "continues humongous" regions (we'll process them
tonyp@790 2555 // when we come across their corresponding "start humongous"
tonyp@790 2556 // region) and regions already claimed
tonyp@790 2557 if (r->claim_value() == claim_value || r->continuesHumongous()) {
tonyp@790 2558 continue;
tonyp@790 2559 }
tonyp@790 2560 // OK, try to claim it
ysr@777 2561 if (r->claimHeapRegion(claim_value)) {
tonyp@790 2562 // success!
tonyp@790 2563 assert(!r->continuesHumongous(), "sanity");
tonyp@790 2564 if (r->startsHumongous()) {
tonyp@790 2565 // If the region is "starts humongous" we'll iterate over its
tonyp@790 2566 // "continues humongous" first; in fact we'll do them
tonyp@790 2567 // first. The order is important. In on case, calling the
tonyp@790 2568 // closure on the "starts humongous" region might de-allocate
tonyp@790 2569 // and clear all its "continues humongous" regions and, as a
tonyp@790 2570 // result, we might end up processing them twice. So, we'll do
tonyp@790 2571 // them first (notice: most closures will ignore them anyway) and
tonyp@790 2572 // then we'll do the "starts humongous" region.
tonyp@790 2573 for (size_t ch_index = index + 1; ch_index < regions; ++ch_index) {
tonyp@790 2574 HeapRegion* chr = region_at(ch_index);
tonyp@790 2575
tonyp@790 2576 // if the region has already been claimed or it's not
tonyp@790 2577 // "continues humongous" we're done
tonyp@790 2578 if (chr->claim_value() == claim_value ||
tonyp@790 2579 !chr->continuesHumongous()) {
tonyp@790 2580 break;
tonyp@790 2581 }
tonyp@790 2582
tonyp@790 2583 // Noone should have claimed it directly. We can given
tonyp@790 2584 // that we claimed its "starts humongous" region.
tonyp@790 2585 assert(chr->claim_value() != claim_value, "sanity");
tonyp@790 2586 assert(chr->humongous_start_region() == r, "sanity");
tonyp@790 2587
tonyp@790 2588 if (chr->claimHeapRegion(claim_value)) {
tonyp@790 2589 // we should always be able to claim it; noone else should
tonyp@790 2590 // be trying to claim this region
tonyp@790 2591
tonyp@790 2592 bool res2 = cl->doHeapRegion(chr);
tonyp@790 2593 assert(!res2, "Should not abort");
tonyp@790 2594
tonyp@790 2595 // Right now, this holds (i.e., no closure that actually
tonyp@790 2596 // does something with "continues humongous" regions
tonyp@790 2597 // clears them). We might have to weaken it in the future,
tonyp@790 2598 // but let's leave these two asserts here for extra safety.
tonyp@790 2599 assert(chr->continuesHumongous(), "should still be the case");
tonyp@790 2600 assert(chr->humongous_start_region() == r, "sanity");
tonyp@790 2601 } else {
tonyp@790 2602 guarantee(false, "we should not reach here");
tonyp@790 2603 }
tonyp@790 2604 }
tonyp@790 2605 }
tonyp@790 2606
tonyp@790 2607 assert(!r->continuesHumongous(), "sanity");
tonyp@790 2608 bool res = cl->doHeapRegion(r);
tonyp@790 2609 assert(!res, "Should not abort");
tonyp@790 2610 }
tonyp@790 2611 }
tonyp@790 2612 }
tonyp@790 2613
tonyp@825 2614 class ResetClaimValuesClosure: public HeapRegionClosure {
tonyp@825 2615 public:
tonyp@825 2616 bool doHeapRegion(HeapRegion* r) {
tonyp@825 2617 r->set_claim_value(HeapRegion::InitialClaimValue);
tonyp@825 2618 return false;
tonyp@825 2619 }
tonyp@825 2620 };
tonyp@825 2621
tonyp@825 2622 void
tonyp@825 2623 G1CollectedHeap::reset_heap_region_claim_values() {
tonyp@825 2624 ResetClaimValuesClosure blk;
tonyp@825 2625 heap_region_iterate(&blk);
tonyp@825 2626 }
tonyp@825 2627
tonyp@790 2628 #ifdef ASSERT
tonyp@790 2629 // This checks whether all regions in the heap have the correct claim
tonyp@790 2630 // value. I also piggy-backed on this a check to ensure that the
tonyp@790 2631 // humongous_start_region() information on "continues humongous"
tonyp@790 2632 // regions is correct.
tonyp@790 2633
tonyp@790 2634 class CheckClaimValuesClosure : public HeapRegionClosure {
tonyp@790 2635 private:
tonyp@790 2636 jint _claim_value;
tonyp@790 2637 size_t _failures;
tonyp@790 2638 HeapRegion* _sh_region;
tonyp@790 2639 public:
tonyp@790 2640 CheckClaimValuesClosure(jint claim_value) :
tonyp@790 2641 _claim_value(claim_value), _failures(0), _sh_region(NULL) { }
tonyp@790 2642 bool doHeapRegion(HeapRegion* r) {
tonyp@790 2643 if (r->claim_value() != _claim_value) {
tonyp@790 2644 gclog_or_tty->print_cr("Region ["PTR_FORMAT","PTR_FORMAT"), "
tonyp@790 2645 "claim value = %d, should be %d",
tonyp@790 2646 r->bottom(), r->end(), r->claim_value(),
tonyp@790 2647 _claim_value);
tonyp@790 2648 ++_failures;
tonyp@790 2649 }
tonyp@790 2650 if (!r->isHumongous()) {
tonyp@790 2651 _sh_region = NULL;
tonyp@790 2652 } else if (r->startsHumongous()) {
tonyp@790 2653 _sh_region = r;
tonyp@790 2654 } else if (r->continuesHumongous()) {
tonyp@790 2655 if (r->humongous_start_region() != _sh_region) {
tonyp@790 2656 gclog_or_tty->print_cr("Region ["PTR_FORMAT","PTR_FORMAT"), "
tonyp@790 2657 "HS = "PTR_FORMAT", should be "PTR_FORMAT,
tonyp@790 2658 r->bottom(), r->end(),
tonyp@790 2659 r->humongous_start_region(),
tonyp@790 2660 _sh_region);
tonyp@790 2661 ++_failures;
ysr@777 2662 }
ysr@777 2663 }
tonyp@790 2664 return false;
tonyp@790 2665 }
tonyp@790 2666 size_t failures() {
tonyp@790 2667 return _failures;
tonyp@790 2668 }
tonyp@790 2669 };
tonyp@790 2670
tonyp@790 2671 bool G1CollectedHeap::check_heap_region_claim_values(jint claim_value) {
tonyp@790 2672 CheckClaimValuesClosure cl(claim_value);
tonyp@790 2673 heap_region_iterate(&cl);
tonyp@790 2674 return cl.failures() == 0;
tonyp@790 2675 }
tonyp@790 2676 #endif // ASSERT
ysr@777 2677
ysr@777 2678 void G1CollectedHeap::collection_set_iterate(HeapRegionClosure* cl) {
ysr@777 2679 HeapRegion* r = g1_policy()->collection_set();
ysr@777 2680 while (r != NULL) {
ysr@777 2681 HeapRegion* next = r->next_in_collection_set();
ysr@777 2682 if (cl->doHeapRegion(r)) {
ysr@777 2683 cl->incomplete();
ysr@777 2684 return;
ysr@777 2685 }
ysr@777 2686 r = next;
ysr@777 2687 }
ysr@777 2688 }
ysr@777 2689
ysr@777 2690 void G1CollectedHeap::collection_set_iterate_from(HeapRegion* r,
ysr@777 2691 HeapRegionClosure *cl) {
tonyp@2011 2692 if (r == NULL) {
tonyp@2011 2693 // The CSet is empty so there's nothing to do.
tonyp@2011 2694 return;
tonyp@2011 2695 }
tonyp@2011 2696
ysr@777 2697 assert(r->in_collection_set(),
ysr@777 2698 "Start region must be a member of the collection set.");
ysr@777 2699 HeapRegion* cur = r;
ysr@777 2700 while (cur != NULL) {
ysr@777 2701 HeapRegion* next = cur->next_in_collection_set();
ysr@777 2702 if (cl->doHeapRegion(cur) && false) {
ysr@777 2703 cl->incomplete();
ysr@777 2704 return;
ysr@777 2705 }
ysr@777 2706 cur = next;
ysr@777 2707 }
ysr@777 2708 cur = g1_policy()->collection_set();
ysr@777 2709 while (cur != r) {
ysr@777 2710 HeapRegion* next = cur->next_in_collection_set();
ysr@777 2711 if (cl->doHeapRegion(cur) && false) {
ysr@777 2712 cl->incomplete();
ysr@777 2713 return;
ysr@777 2714 }
ysr@777 2715 cur = next;
ysr@777 2716 }
ysr@777 2717 }
ysr@777 2718
ysr@777 2719 CompactibleSpace* G1CollectedHeap::first_compactible_space() {
ysr@777 2720 return _hrs->length() > 0 ? _hrs->at(0) : NULL;
ysr@777 2721 }
ysr@777 2722
ysr@777 2723
ysr@777 2724 Space* G1CollectedHeap::space_containing(const void* addr) const {
ysr@777 2725 Space* res = heap_region_containing(addr);
ysr@777 2726 if (res == NULL)
ysr@777 2727 res = perm_gen()->space_containing(addr);
ysr@777 2728 return res;
ysr@777 2729 }
ysr@777 2730
ysr@777 2731 HeapWord* G1CollectedHeap::block_start(const void* addr) const {
ysr@777 2732 Space* sp = space_containing(addr);
ysr@777 2733 if (sp != NULL) {
ysr@777 2734 return sp->block_start(addr);
ysr@777 2735 }
ysr@777 2736 return NULL;
ysr@777 2737 }
ysr@777 2738
ysr@777 2739 size_t G1CollectedHeap::block_size(const HeapWord* addr) const {
ysr@777 2740 Space* sp = space_containing(addr);
ysr@777 2741 assert(sp != NULL, "block_size of address outside of heap");
ysr@777 2742 return sp->block_size(addr);
ysr@777 2743 }
ysr@777 2744
ysr@777 2745 bool G1CollectedHeap::block_is_obj(const HeapWord* addr) const {
ysr@777 2746 Space* sp = space_containing(addr);
ysr@777 2747 return sp->block_is_obj(addr);
ysr@777 2748 }
ysr@777 2749
ysr@777 2750 bool G1CollectedHeap::supports_tlab_allocation() const {
ysr@777 2751 return true;
ysr@777 2752 }
ysr@777 2753
ysr@777 2754 size_t G1CollectedHeap::tlab_capacity(Thread* ignored) const {
ysr@777 2755 return HeapRegion::GrainBytes;
ysr@777 2756 }
ysr@777 2757
ysr@777 2758 size_t G1CollectedHeap::unsafe_max_tlab_alloc(Thread* ignored) const {
ysr@777 2759 // Return the remaining space in the cur alloc region, but not less than
ysr@777 2760 // the min TLAB size.
johnc@1748 2761
johnc@1748 2762 // Also, this value can be at most the humongous object threshold,
johnc@1748 2763 // since we can't allow tlabs to grow big enough to accomodate
johnc@1748 2764 // humongous objects.
johnc@1748 2765
johnc@1748 2766 // We need to store the cur alloc region locally, since it might change
johnc@1748 2767 // between when we test for NULL and when we use it later.
ysr@777 2768 ContiguousSpace* cur_alloc_space = _cur_alloc_region;
johnc@1748 2769 size_t max_tlab_size = _humongous_object_threshold_in_words * wordSize;
johnc@1748 2770
ysr@777 2771 if (cur_alloc_space == NULL) {
johnc@1748 2772 return max_tlab_size;
ysr@777 2773 } else {
johnc@1748 2774 return MIN2(MAX2(cur_alloc_space->free(), (size_t)MinTLABSize),
johnc@1748 2775 max_tlab_size);
ysr@777 2776 }
ysr@777 2777 }
ysr@777 2778
ysr@777 2779 size_t G1CollectedHeap::large_typearray_limit() {
ysr@777 2780 // FIXME
ysr@777 2781 return HeapRegion::GrainBytes/HeapWordSize;
ysr@777 2782 }
ysr@777 2783
ysr@777 2784 size_t G1CollectedHeap::max_capacity() const {
johnc@2504 2785 return _g1_reserved.byte_size();
ysr@777 2786 }
ysr@777 2787
ysr@777 2788 jlong G1CollectedHeap::millis_since_last_gc() {
ysr@777 2789 // assert(false, "NYI");
ysr@777 2790 return 0;
ysr@777 2791 }
ysr@777 2792
ysr@777 2793 void G1CollectedHeap::prepare_for_verify() {
ysr@777 2794 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) {
ysr@777 2795 ensure_parsability(false);
ysr@777 2796 }
ysr@777 2797 g1_rem_set()->prepare_for_verify();
ysr@777 2798 }
ysr@777 2799
ysr@777 2800 class VerifyLivenessOopClosure: public OopClosure {
ysr@777 2801 G1CollectedHeap* g1h;
ysr@777 2802 public:
ysr@777 2803 VerifyLivenessOopClosure(G1CollectedHeap* _g1h) {
ysr@777 2804 g1h = _g1h;
ysr@777 2805 }
ysr@1280 2806 void do_oop(narrowOop *p) { do_oop_work(p); }
ysr@1280 2807 void do_oop( oop *p) { do_oop_work(p); }
ysr@1280 2808
ysr@1280 2809 template <class T> void do_oop_work(T *p) {
ysr@1280 2810 oop obj = oopDesc::load_decode_heap_oop(p);
ysr@1280 2811 guarantee(obj == NULL || !g1h->is_obj_dead(obj),
ysr@1280 2812 "Dead object referenced by a not dead object");
ysr@777 2813 }
ysr@777 2814 };
ysr@777 2815
ysr@777 2816 class VerifyObjsInRegionClosure: public ObjectClosure {
tonyp@1246 2817 private:
ysr@777 2818 G1CollectedHeap* _g1h;
ysr@777 2819 size_t _live_bytes;
ysr@777 2820 HeapRegion *_hr;
tonyp@1246 2821 bool _use_prev_marking;
ysr@777 2822 public:
tonyp@1246 2823 // use_prev_marking == true -> use "prev" marking information,
tonyp@1246 2824 // use_prev_marking == false -> use "next" marking information
tonyp@1246 2825 VerifyObjsInRegionClosure(HeapRegion *hr, bool use_prev_marking)
tonyp@1246 2826 : _live_bytes(0), _hr(hr), _use_prev_marking(use_prev_marking) {
ysr@777 2827 _g1h = G1CollectedHeap::heap();
ysr@777 2828 }
ysr@777 2829 void do_object(oop o) {
ysr@777 2830 VerifyLivenessOopClosure isLive(_g1h);
ysr@777 2831 assert(o != NULL, "Huh?");
tonyp@1246 2832 if (!_g1h->is_obj_dead_cond(o, _use_prev_marking)) {
ysr@777 2833 o->oop_iterate(&isLive);
johnc@1824 2834 if (!_hr->obj_allocated_since_prev_marking(o)) {
johnc@1824 2835 size_t obj_size = o->size(); // Make sure we don't overflow
johnc@1824 2836 _live_bytes += (obj_size * HeapWordSize);
johnc@1824 2837 }
ysr@777 2838 }
ysr@777 2839 }
ysr@777 2840 size_t live_bytes() { return _live_bytes; }
ysr@777 2841 };
ysr@777 2842
ysr@777 2843 class PrintObjsInRegionClosure : public ObjectClosure {
ysr@777 2844 HeapRegion *_hr;
ysr@777 2845 G1CollectedHeap *_g1;
ysr@777 2846 public:
ysr@777 2847 PrintObjsInRegionClosure(HeapRegion *hr) : _hr(hr) {
ysr@777 2848 _g1 = G1CollectedHeap::heap();
ysr@777 2849 };
ysr@777 2850
ysr@777 2851 void do_object(oop o) {
ysr@777 2852 if (o != NULL) {
ysr@777 2853 HeapWord *start = (HeapWord *) o;
ysr@777 2854 size_t word_sz = o->size();
ysr@777 2855 gclog_or_tty->print("\nPrinting obj "PTR_FORMAT" of size " SIZE_FORMAT
ysr@777 2856 " isMarkedPrev %d isMarkedNext %d isAllocSince %d\n",
ysr@777 2857 (void*) o, word_sz,
ysr@777 2858 _g1->isMarkedPrev(o),
ysr@777 2859 _g1->isMarkedNext(o),
ysr@777 2860 _hr->obj_allocated_since_prev_marking(o));
ysr@777 2861 HeapWord *end = start + word_sz;
ysr@777 2862 HeapWord *cur;
ysr@777 2863 int *val;
ysr@777 2864 for (cur = start; cur < end; cur++) {
ysr@777 2865 val = (int *) cur;
ysr@777 2866 gclog_or_tty->print("\t "PTR_FORMAT":"PTR_FORMAT"\n", val, *val);
ysr@777 2867 }
ysr@777 2868 }
ysr@777 2869 }
ysr@777 2870 };
ysr@777 2871
ysr@777 2872 class VerifyRegionClosure: public HeapRegionClosure {
tonyp@1246 2873 private:
ysr@777 2874 bool _allow_dirty;
tonyp@825 2875 bool _par;
tonyp@1246 2876 bool _use_prev_marking;
tonyp@1455 2877 bool _failures;
tonyp@1246 2878 public:
tonyp@1246 2879 // use_prev_marking == true -> use "prev" marking information,
tonyp@1246 2880 // use_prev_marking == false -> use "next" marking information
tonyp@1246 2881 VerifyRegionClosure(bool allow_dirty, bool par, bool use_prev_marking)
ysr@1280 2882 : _allow_dirty(allow_dirty),
ysr@1280 2883 _par(par),
tonyp@1455 2884 _use_prev_marking(use_prev_marking),
tonyp@1455 2885 _failures(false) {}
tonyp@1455 2886
tonyp@1455 2887 bool failures() {
tonyp@1455 2888 return _failures;
tonyp@1455 2889 }
ysr@1280 2890
ysr@777 2891 bool doHeapRegion(HeapRegion* r) {
tonyp@825 2892 guarantee(_par || r->claim_value() == HeapRegion::InitialClaimValue,
tonyp@825 2893 "Should be unclaimed at verify points.");
iveresov@1072 2894 if (!r->continuesHumongous()) {
tonyp@1455 2895 bool failures = false;
tonyp@1455 2896 r->verify(_allow_dirty, _use_prev_marking, &failures);
tonyp@1455 2897 if (failures) {
tonyp@1455 2898 _failures = true;
tonyp@1455 2899 } else {
tonyp@1455 2900 VerifyObjsInRegionClosure not_dead_yet_cl(r, _use_prev_marking);
tonyp@1455 2901 r->object_iterate(&not_dead_yet_cl);
tonyp@1455 2902 if (r->max_live_bytes() < not_dead_yet_cl.live_bytes()) {
tonyp@1455 2903 gclog_or_tty->print_cr("["PTR_FORMAT","PTR_FORMAT"] "
tonyp@1455 2904 "max_live_bytes "SIZE_FORMAT" "
tonyp@1455 2905 "< calculated "SIZE_FORMAT,
tonyp@1455 2906 r->bottom(), r->end(),
tonyp@1455 2907 r->max_live_bytes(),
tonyp@1455 2908 not_dead_yet_cl.live_bytes());
tonyp@1455 2909 _failures = true;
tonyp@1455 2910 }
tonyp@1455 2911 }
ysr@777 2912 }
tonyp@1455 2913 return false; // stop the region iteration if we hit a failure
ysr@777 2914 }
ysr@777 2915 };
ysr@777 2916
ysr@777 2917 class VerifyRootsClosure: public OopsInGenClosure {
ysr@777 2918 private:
ysr@777 2919 G1CollectedHeap* _g1h;
tonyp@1455 2920 bool _use_prev_marking;
ysr@777 2921 bool _failures;
ysr@777 2922 public:
tonyp@1246 2923 // use_prev_marking == true -> use "prev" marking information,
tonyp@1246 2924 // use_prev_marking == false -> use "next" marking information
tonyp@1246 2925 VerifyRootsClosure(bool use_prev_marking) :
ysr@1280 2926 _g1h(G1CollectedHeap::heap()),
tonyp@1455 2927 _use_prev_marking(use_prev_marking),
tonyp@1455 2928 _failures(false) { }
ysr@777 2929
ysr@777 2930 bool failures() { return _failures; }
ysr@777 2931
ysr@1280 2932 template <class T> void do_oop_nv(T* p) {
ysr@1280 2933 T heap_oop = oopDesc::load_heap_oop(p);
ysr@1280 2934 if (!oopDesc::is_null(heap_oop)) {
ysr@1280 2935 oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
tonyp@1246 2936 if (_g1h->is_obj_dead_cond(obj, _use_prev_marking)) {
ysr@777 2937 gclog_or_tty->print_cr("Root location "PTR_FORMAT" "
tonyp@1455 2938 "points to dead obj "PTR_FORMAT, p, (void*) obj);
ysr@777 2939 obj->print_on(gclog_or_tty);
ysr@777 2940 _failures = true;
ysr@777 2941 }
ysr@777 2942 }
ysr@777 2943 }
ysr@1280 2944
ysr@1280 2945 void do_oop(oop* p) { do_oop_nv(p); }
ysr@1280 2946 void do_oop(narrowOop* p) { do_oop_nv(p); }
ysr@777 2947 };
ysr@777 2948
tonyp@825 2949 // This is the task used for parallel heap verification.
tonyp@825 2950
tonyp@825 2951 class G1ParVerifyTask: public AbstractGangTask {
tonyp@825 2952 private:
tonyp@825 2953 G1CollectedHeap* _g1h;
tonyp@825 2954 bool _allow_dirty;
tonyp@1246 2955 bool _use_prev_marking;
tonyp@1455 2956 bool _failures;
tonyp@825 2957
tonyp@825 2958 public:
tonyp@1246 2959 // use_prev_marking == true -> use "prev" marking information,
tonyp@1246 2960 // use_prev_marking == false -> use "next" marking information
tonyp@1246 2961 G1ParVerifyTask(G1CollectedHeap* g1h, bool allow_dirty,
tonyp@1246 2962 bool use_prev_marking) :
tonyp@825 2963 AbstractGangTask("Parallel verify task"),
ysr@1280 2964 _g1h(g1h),
ysr@1280 2965 _allow_dirty(allow_dirty),
tonyp@1455 2966 _use_prev_marking(use_prev_marking),
tonyp@1455 2967 _failures(false) { }
tonyp@1455 2968
tonyp@1455 2969 bool failures() {
tonyp@1455 2970 return _failures;
tonyp@1455 2971 }
tonyp@825 2972
tonyp@825 2973 void work(int worker_i) {
iveresov@1072 2974 HandleMark hm;
tonyp@1246 2975 VerifyRegionClosure blk(_allow_dirty, true, _use_prev_marking);
tonyp@825 2976 _g1h->heap_region_par_iterate_chunked(&blk, worker_i,
tonyp@825 2977 HeapRegion::ParVerifyClaimValue);
tonyp@1455 2978 if (blk.failures()) {
tonyp@1455 2979 _failures = true;
tonyp@1455 2980 }
tonyp@825 2981 }
tonyp@825 2982 };
tonyp@825 2983
ysr@777 2984 void G1CollectedHeap::verify(bool allow_dirty, bool silent) {
tonyp@1246 2985 verify(allow_dirty, silent, /* use_prev_marking */ true);
tonyp@1246 2986 }
tonyp@1246 2987
tonyp@1246 2988 void G1CollectedHeap::verify(bool allow_dirty,
tonyp@1246 2989 bool silent,
tonyp@1246 2990 bool use_prev_marking) {
ysr@777 2991 if (SafepointSynchronize::is_at_safepoint() || ! UseTLAB) {
ysr@777 2992 if (!silent) { gclog_or_tty->print("roots "); }
tonyp@1246 2993 VerifyRootsClosure rootsCl(use_prev_marking);
jrose@1424 2994 CodeBlobToOopClosure blobsCl(&rootsCl, /*do_marking=*/ false);
jrose@1424 2995 process_strong_roots(true, // activate StrongRootsScope
jrose@1424 2996 false,
ysr@777 2997 SharedHeap::SO_AllClasses,
ysr@777 2998 &rootsCl,
jrose@1424 2999 &blobsCl,
ysr@777 3000 &rootsCl);
tonyp@1455 3001 bool failures = rootsCl.failures();
ysr@777 3002 rem_set()->invalidate(perm_gen()->used_region(), false);
tonyp@2472 3003 if (!silent) { gclog_or_tty->print("HeapRegionSets "); }
tonyp@2472 3004 verify_region_sets();
tonyp@2472 3005 if (!silent) { gclog_or_tty->print("HeapRegions "); }
tonyp@825 3006 if (GCParallelVerificationEnabled && ParallelGCThreads > 1) {
tonyp@825 3007 assert(check_heap_region_claim_values(HeapRegion::InitialClaimValue),
tonyp@825 3008 "sanity check");
tonyp@825 3009
tonyp@1246 3010 G1ParVerifyTask task(this, allow_dirty, use_prev_marking);
tonyp@825 3011 int n_workers = workers()->total_workers();
tonyp@825 3012 set_par_threads(n_workers);
tonyp@825 3013 workers()->run_task(&task);
tonyp@825 3014 set_par_threads(0);
tonyp@1455 3015 if (task.failures()) {
tonyp@1455 3016 failures = true;
tonyp@1455 3017 }
tonyp@825 3018
tonyp@825 3019 assert(check_heap_region_claim_values(HeapRegion::ParVerifyClaimValue),
tonyp@825 3020 "sanity check");
tonyp@825 3021
tonyp@825 3022 reset_heap_region_claim_values();
tonyp@825 3023
tonyp@825 3024 assert(check_heap_region_claim_values(HeapRegion::InitialClaimValue),
tonyp@825 3025 "sanity check");
tonyp@825 3026 } else {
tonyp@1246 3027 VerifyRegionClosure blk(allow_dirty, false, use_prev_marking);
tonyp@825 3028 _hrs->iterate(&blk);
tonyp@1455 3029 if (blk.failures()) {
tonyp@1455 3030 failures = true;
tonyp@1455 3031 }
tonyp@825 3032 }
tonyp@2472 3033 if (!silent) gclog_or_tty->print("RemSet ");
ysr@777 3034 rem_set()->verify();
tonyp@1455 3035
tonyp@1455 3036 if (failures) {
tonyp@1455 3037 gclog_or_tty->print_cr("Heap:");
tonyp@1455 3038 print_on(gclog_or_tty, true /* extended */);
tonyp@1455 3039 gclog_or_tty->print_cr("");
jcoomes@1902 3040 #ifndef PRODUCT
tonyp@1479 3041 if (VerifyDuringGC && G1VerifyDuringGCPrintReachable) {
tonyp@1823 3042 concurrent_mark()->print_reachable("at-verification-failure",
tonyp@1823 3043 use_prev_marking, false /* all */);
tonyp@1455 3044 }
jcoomes@1902 3045 #endif
tonyp@1455 3046 gclog_or_tty->flush();
tonyp@1455 3047 }
tonyp@1455 3048 guarantee(!failures, "there should not have been any failures");
ysr@777 3049 } else {
ysr@777 3050 if (!silent) gclog_or_tty->print("(SKIPPING roots, heapRegions, remset) ");
ysr@777 3051 }
ysr@777 3052 }
ysr@777 3053
ysr@777 3054 class PrintRegionClosure: public HeapRegionClosure {
ysr@777 3055 outputStream* _st;
ysr@777 3056 public:
ysr@777 3057 PrintRegionClosure(outputStream* st) : _st(st) {}
ysr@777 3058 bool doHeapRegion(HeapRegion* r) {
ysr@777 3059 r->print_on(_st);
ysr@777 3060 return false;
ysr@777 3061 }
ysr@777 3062 };
ysr@777 3063
tonyp@1273 3064 void G1CollectedHeap::print() const { print_on(tty); }
ysr@777 3065
ysr@777 3066 void G1CollectedHeap::print_on(outputStream* st) const {
tonyp@1273 3067 print_on(st, PrintHeapAtGCExtended);
tonyp@1273 3068 }
tonyp@1273 3069
tonyp@1273 3070 void G1CollectedHeap::print_on(outputStream* st, bool extended) const {
tonyp@1273 3071 st->print(" %-20s", "garbage-first heap");
tonyp@1273 3072 st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
tonyp@1281 3073 capacity()/K, used_unlocked()/K);
tonyp@1273 3074 st->print(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
tonyp@1273 3075 _g1_storage.low_boundary(),
tonyp@1273 3076 _g1_storage.high(),
tonyp@1273 3077 _g1_storage.high_boundary());
tonyp@1273 3078 st->cr();
tonyp@1273 3079 st->print(" region size " SIZE_FORMAT "K, ",
tonyp@1273 3080 HeapRegion::GrainBytes/K);
tonyp@1273 3081 size_t young_regions = _young_list->length();
tonyp@1273 3082 st->print(SIZE_FORMAT " young (" SIZE_FORMAT "K), ",
tonyp@1273 3083 young_regions, young_regions * HeapRegion::GrainBytes / K);
tonyp@1273 3084 size_t survivor_regions = g1_policy()->recorded_survivor_regions();
tonyp@1273 3085 st->print(SIZE_FORMAT " survivors (" SIZE_FORMAT "K)",
tonyp@1273 3086 survivor_regions, survivor_regions * HeapRegion::GrainBytes / K);
tonyp@1273 3087 st->cr();
tonyp@1273 3088 perm()->as_gen()->print_on(st);
tonyp@1273 3089 if (extended) {
tonyp@1455 3090 st->cr();
tonyp@1273 3091 print_on_extended(st);
tonyp@1273 3092 }
tonyp@1273 3093 }
tonyp@1273 3094
tonyp@1273 3095 void G1CollectedHeap::print_on_extended(outputStream* st) const {
ysr@777 3096 PrintRegionClosure blk(st);
ysr@777 3097 _hrs->iterate(&blk);
ysr@777 3098 }
ysr@777 3099
ysr@777 3100 void G1CollectedHeap::print_gc_threads_on(outputStream* st) const {
jmasa@2188 3101 if (G1CollectedHeap::use_parallel_gc_threads()) {
tonyp@1454 3102 workers()->print_worker_threads_on(st);
tonyp@1454 3103 }
tonyp@1454 3104 _cmThread->print_on(st);
ysr@777 3105 st->cr();
tonyp@1454 3106 _cm->print_worker_threads_on(st);
tonyp@1454 3107 _cg1r->print_worker_threads_on(st);
ysr@777 3108 st->cr();
ysr@777 3109 }
ysr@777 3110
ysr@777 3111 void G1CollectedHeap::gc_threads_do(ThreadClosure* tc) const {
jmasa@2188 3112 if (G1CollectedHeap::use_parallel_gc_threads()) {
ysr@777 3113 workers()->threads_do(tc);
ysr@777 3114 }
ysr@777 3115 tc->do_thread(_cmThread);
iveresov@1229 3116 _cg1r->threads_do(tc);
ysr@777 3117 }
ysr@777 3118
ysr@777 3119 void G1CollectedHeap::print_tracing_info() const {
ysr@777 3120 // We'll overload this to mean "trace GC pause statistics."
ysr@777 3121 if (TraceGen0Time || TraceGen1Time) {
ysr@777 3122 // The "G1CollectorPolicy" is keeping track of these stats, so delegate
ysr@777 3123 // to that.
ysr@777 3124 g1_policy()->print_tracing_info();
ysr@777 3125 }
johnc@1186 3126 if (G1SummarizeRSetStats) {
ysr@777 3127 g1_rem_set()->print_summary_info();
ysr@777 3128 }
tonyp@1717 3129 if (G1SummarizeConcMark) {
ysr@777 3130 concurrent_mark()->print_summary_info();
ysr@777 3131 }
ysr@777 3132 g1_policy()->print_yg_surv_rate_info();
ysr@777 3133 SpecializationStats::print();
ysr@777 3134 }
ysr@777 3135
ysr@777 3136 int G1CollectedHeap::addr_to_arena_id(void* addr) const {
ysr@777 3137 HeapRegion* hr = heap_region_containing(addr);
ysr@777 3138 if (hr == NULL) {
ysr@777 3139 return 0;
ysr@777 3140 } else {
ysr@777 3141 return 1;
ysr@777 3142 }
ysr@777 3143 }
ysr@777 3144
ysr@777 3145 G1CollectedHeap* G1CollectedHeap::heap() {
ysr@777 3146 assert(_sh->kind() == CollectedHeap::G1CollectedHeap,
ysr@777 3147 "not a garbage-first heap");
ysr@777 3148 return _g1h;
ysr@777 3149 }
ysr@777 3150
ysr@777 3151 void G1CollectedHeap::gc_prologue(bool full /* Ignored */) {
ysr@1680 3152 // always_do_update_barrier = false;
ysr@777 3153 assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
ysr@777 3154 // Call allocation profiler
ysr@777 3155 AllocationProfiler::iterate_since_last_gc();
ysr@777 3156 // Fill TLAB's and such
ysr@777 3157 ensure_parsability(true);
ysr@777 3158 }
ysr@777 3159
ysr@777 3160 void G1CollectedHeap::gc_epilogue(bool full /* Ignored */) {
ysr@777 3161 // FIXME: what is this about?
ysr@777 3162 // I'm ignoring the "fill_newgen()" call if "alloc_event_enabled"
ysr@777 3163 // is set.
ysr@777 3164 COMPILER2_PRESENT(assert(DerivedPointerTable::is_empty(),
ysr@777 3165 "derived pointer present"));
ysr@1680 3166 // always_do_update_barrier = true;
ysr@777 3167 }
ysr@777 3168
tonyp@2315 3169 HeapWord* G1CollectedHeap::do_collection_pause(size_t word_size,
tonyp@2315 3170 unsigned int gc_count_before,
tonyp@2315 3171 bool* succeeded) {
tonyp@2315 3172 assert_heap_not_locked_and_not_at_safepoint();
ysr@777 3173 g1_policy()->record_stop_world_start();
tonyp@2315 3174 VM_G1IncCollectionPause op(gc_count_before,
tonyp@2315 3175 word_size,
tonyp@2315 3176 false, /* should_initiate_conc_mark */
tonyp@2315 3177 g1_policy()->max_pause_time_ms(),
tonyp@2315 3178 GCCause::_g1_inc_collection_pause);
tonyp@2315 3179 VMThread::execute(&op);
tonyp@2315 3180
tonyp@2315 3181 HeapWord* result = op.result();
tonyp@2315 3182 bool ret_succeeded = op.prologue_succeeded() && op.pause_succeeded();
tonyp@2315 3183 assert(result == NULL || ret_succeeded,
tonyp@2315 3184 "the result should be NULL if the VM did not succeed");
tonyp@2315 3185 *succeeded = ret_succeeded;
tonyp@2315 3186
tonyp@2315 3187 assert_heap_not_locked();
tonyp@2315 3188 return result;
ysr@777 3189 }
ysr@777 3190
ysr@777 3191 void
ysr@777 3192 G1CollectedHeap::doConcurrentMark() {
ysr@1280 3193 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
ysr@1280 3194 if (!_cmThread->in_progress()) {
ysr@1280 3195 _cmThread->set_started();
ysr@1280 3196 CGC_lock->notify();
ysr@777 3197 }
ysr@777 3198 }
ysr@777 3199
ysr@777 3200 class VerifyMarkedObjsClosure: public ObjectClosure {
ysr@777 3201 G1CollectedHeap* _g1h;
ysr@777 3202 public:
ysr@777 3203 VerifyMarkedObjsClosure(G1CollectedHeap* g1h) : _g1h(g1h) {}
ysr@777 3204 void do_object(oop obj) {
ysr@777 3205 assert(obj->mark()->is_marked() ? !_g1h->is_obj_dead(obj) : true,
ysr@777 3206 "markandsweep mark should agree with concurrent deadness");
ysr@777 3207 }
ysr@777 3208 };
ysr@777 3209
ysr@777 3210 void
ysr@777 3211 G1CollectedHeap::checkConcurrentMark() {
ysr@777 3212 VerifyMarkedObjsClosure verifycl(this);
ysr@777 3213 // MutexLockerEx x(getMarkBitMapLock(),
ysr@777 3214 // Mutex::_no_safepoint_check_flag);
iveresov@1113 3215 object_iterate(&verifycl, false);
ysr@777 3216 }
ysr@777 3217
ysr@777 3218 void G1CollectedHeap::do_sync_mark() {
ysr@777 3219 _cm->checkpointRootsInitial();
ysr@777 3220 _cm->markFromRoots();
ysr@777 3221 _cm->checkpointRootsFinal(false);
ysr@777 3222 }
ysr@777 3223
ysr@777 3224 // <NEW PREDICTION>
ysr@777 3225
ysr@777 3226 double G1CollectedHeap::predict_region_elapsed_time_ms(HeapRegion *hr,
ysr@777 3227 bool young) {
ysr@777 3228 return _g1_policy->predict_region_elapsed_time_ms(hr, young);
ysr@777 3229 }
ysr@777 3230
ysr@777 3231 void G1CollectedHeap::check_if_region_is_too_expensive(double
ysr@777 3232 predicted_time_ms) {
ysr@777 3233 _g1_policy->check_if_region_is_too_expensive(predicted_time_ms);
ysr@777 3234 }
ysr@777 3235
ysr@777 3236 size_t G1CollectedHeap::pending_card_num() {
ysr@777 3237 size_t extra_cards = 0;
ysr@777 3238 JavaThread *curr = Threads::first();
ysr@777 3239 while (curr != NULL) {
ysr@777 3240 DirtyCardQueue& dcq = curr->dirty_card_queue();
ysr@777 3241 extra_cards += dcq.size();
ysr@777 3242 curr = curr->next();
ysr@777 3243 }
ysr@777 3244 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
ysr@777 3245 size_t buffer_size = dcqs.buffer_size();
ysr@777 3246 size_t buffer_num = dcqs.completed_buffers_num();
ysr@777 3247 return buffer_size * buffer_num + extra_cards;
ysr@777 3248 }
ysr@777 3249
ysr@777 3250 size_t G1CollectedHeap::max_pending_card_num() {
ysr@777 3251 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
ysr@777 3252 size_t buffer_size = dcqs.buffer_size();
ysr@777 3253 size_t buffer_num = dcqs.completed_buffers_num();
ysr@777 3254 int thread_num = Threads::number_of_threads();
ysr@777 3255 return (buffer_num + thread_num) * buffer_size;
ysr@777 3256 }
ysr@777 3257
ysr@777 3258 size_t G1CollectedHeap::cards_scanned() {
johnc@2216 3259 return g1_rem_set()->cardsScanned();
ysr@777 3260 }
ysr@777 3261
ysr@777 3262 void
ysr@777 3263 G1CollectedHeap::setup_surviving_young_words() {
ysr@777 3264 guarantee( _surviving_young_words == NULL, "pre-condition" );
ysr@777 3265 size_t array_length = g1_policy()->young_cset_length();
ysr@777 3266 _surviving_young_words = NEW_C_HEAP_ARRAY(size_t, array_length);
ysr@777 3267 if (_surviving_young_words == NULL) {
ysr@777 3268 vm_exit_out_of_memory(sizeof(size_t) * array_length,
ysr@777 3269 "Not enough space for young surv words summary.");
ysr@777 3270 }
ysr@777 3271 memset(_surviving_young_words, 0, array_length * sizeof(size_t));
ysr@1280 3272 #ifdef ASSERT
ysr@777 3273 for (size_t i = 0; i < array_length; ++i) {
ysr@1280 3274 assert( _surviving_young_words[i] == 0, "memset above" );
ysr@1280 3275 }
ysr@1280 3276 #endif // !ASSERT
ysr@777 3277 }
ysr@777 3278
ysr@777 3279 void
ysr@777 3280 G1CollectedHeap::update_surviving_young_words(size_t* surv_young_words) {
ysr@777 3281 MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
ysr@777 3282 size_t array_length = g1_policy()->young_cset_length();
ysr@777 3283 for (size_t i = 0; i < array_length; ++i)
ysr@777 3284 _surviving_young_words[i] += surv_young_words[i];
ysr@777 3285 }
ysr@777 3286
ysr@777 3287 void
ysr@777 3288 G1CollectedHeap::cleanup_surviving_young_words() {
ysr@777 3289 guarantee( _surviving_young_words != NULL, "pre-condition" );
ysr@777 3290 FREE_C_HEAP_ARRAY(size_t, _surviving_young_words);
ysr@777 3291 _surviving_young_words = NULL;
ysr@777 3292 }
ysr@777 3293
ysr@777 3294 // </NEW PREDICTION>
ysr@777 3295
iveresov@1696 3296 struct PrepareForRSScanningClosure : public HeapRegionClosure {
iveresov@1696 3297 bool doHeapRegion(HeapRegion *r) {
iveresov@1696 3298 r->rem_set()->set_iter_claimed(0);
iveresov@1696 3299 return false;
iveresov@1696 3300 }
iveresov@1696 3301 };
iveresov@1696 3302
jcoomes@2064 3303 #if TASKQUEUE_STATS
jcoomes@2064 3304 void G1CollectedHeap::print_taskqueue_stats_hdr(outputStream* const st) {
jcoomes@2064 3305 st->print_raw_cr("GC Task Stats");
jcoomes@2064 3306 st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr();
jcoomes@2064 3307 st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr();
jcoomes@2064 3308 }
jcoomes@2064 3309
jcoomes@2064 3310 void G1CollectedHeap::print_taskqueue_stats(outputStream* const st) const {
jcoomes@2064 3311 print_taskqueue_stats_hdr(st);
jcoomes@2064 3312
jcoomes@2064 3313 TaskQueueStats totals;
jcoomes@2110 3314 const int n = workers() != NULL ? workers()->total_workers() : 1;
jcoomes@2064 3315 for (int i = 0; i < n; ++i) {
jcoomes@2064 3316 st->print("%3d ", i); task_queue(i)->stats.print(st); st->cr();
jcoomes@2064 3317 totals += task_queue(i)->stats;
jcoomes@2064 3318 }
jcoomes@2064 3319 st->print_raw("tot "); totals.print(st); st->cr();
jcoomes@2064 3320
jcoomes@2064 3321 DEBUG_ONLY(totals.verify());
jcoomes@2064 3322 }
jcoomes@2064 3323
jcoomes@2064 3324 void G1CollectedHeap::reset_taskqueue_stats() {
jcoomes@2110 3325 const int n = workers() != NULL ? workers()->total_workers() : 1;
jcoomes@2064 3326 for (int i = 0; i < n; ++i) {
jcoomes@2064 3327 task_queue(i)->stats.reset();
jcoomes@2064 3328 }
jcoomes@2064 3329 }
jcoomes@2064 3330 #endif // TASKQUEUE_STATS
jcoomes@2064 3331
tonyp@2315 3332 bool
tonyp@2011 3333 G1CollectedHeap::do_collection_pause_at_safepoint(double target_pause_time_ms) {
tonyp@2472 3334 assert_at_safepoint(true /* should_be_vm_thread */);
tonyp@2472 3335 guarantee(!is_gc_active(), "collection is not reentrant");
tonyp@2472 3336
tonyp@1794 3337 if (GC_locker::check_active_before_gc()) {
tonyp@2315 3338 return false;
tonyp@1794 3339 }
tonyp@1794 3340
kamg@2445 3341 SvcGCMarker sgcm(SvcGCMarker::MINOR);
tonyp@2381 3342 ResourceMark rm;
tonyp@2381 3343
tonyp@1273 3344 if (PrintHeapAtGC) {
tonyp@1273 3345 Universe::print_heap_before_gc();
tonyp@1273 3346 }
tonyp@1273 3347
tonyp@2472 3348 verify_region_sets_optional();
tonyp@2472 3349
tonyp@1273 3350 {
tonyp@1794 3351 // This call will decide whether this pause is an initial-mark
tonyp@1794 3352 // pause. If it is, during_initial_mark_pause() will return true
tonyp@1794 3353 // for the duration of this pause.
tonyp@1794 3354 g1_policy()->decide_on_conc_mark_initiation();
tonyp@1794 3355
tonyp@1273 3356 char verbose_str[128];
tonyp@1273 3357 sprintf(verbose_str, "GC pause ");
tonyp@1273 3358 if (g1_policy()->in_young_gc_mode()) {
tonyp@1273 3359 if (g1_policy()->full_young_gcs())
tonyp@1273 3360 strcat(verbose_str, "(young)");
tonyp@1273 3361 else
tonyp@1273 3362 strcat(verbose_str, "(partial)");
tonyp@1273 3363 }
tonyp@2011 3364 if (g1_policy()->during_initial_mark_pause()) {
tonyp@1273 3365 strcat(verbose_str, " (initial-mark)");
tonyp@2011 3366 // We are about to start a marking cycle, so we increment the
tonyp@2011 3367 // full collection counter.
tonyp@2011 3368 increment_total_full_collections();
tonyp@2011 3369 }
tonyp@1273 3370
tonyp@1273 3371 // if PrintGCDetails is on, we'll print long statistics information
tonyp@1273 3372 // in the collector policy code, so let's not print this as the output
tonyp@1273 3373 // is messy if we do.
tonyp@1273 3374 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
tonyp@1273 3375 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
tonyp@1273 3376 TraceTime t(verbose_str, PrintGC && !PrintGCDetails, true, gclog_or_tty);
tonyp@1273 3377
tonyp@1524 3378 TraceMemoryManagerStats tms(false /* fullGC */);
tonyp@1524 3379
tonyp@2472 3380 // If there are any free regions available on the secondary_free_list
tonyp@2472 3381 // make sure we append them to the free_list. However, we don't
tonyp@2472 3382 // have to wait for the rest of the cleanup operation to
tonyp@2472 3383 // finish. If it's still going on that's OK. If we run out of
tonyp@2472 3384 // regions, the region allocation code will check the
tonyp@2472 3385 // secondary_free_list and potentially wait if more free regions
tonyp@2472 3386 // are coming (see new_region_try_secondary_free_list()).
tonyp@2472 3387 if (!G1StressConcRegionFreeing) {
tonyp@2472 3388 append_secondary_free_list_if_not_empty();
tonyp@2472 3389 }
tonyp@1273 3390
tonyp@1273 3391 increment_gc_time_stamp();
tonyp@1273 3392
tonyp@1273 3393 if (g1_policy()->in_young_gc_mode()) {
tonyp@1273 3394 assert(check_young_list_well_formed(),
tonyp@1273 3395 "young list should be well formed");
tonyp@1273 3396 }
tonyp@1273 3397
tonyp@1273 3398 { // Call to jvmpi::post_class_unload_events must occur outside of active GC
tonyp@1273 3399 IsGCActiveMark x;
tonyp@1273 3400
tonyp@1273 3401 gc_prologue(false);
tonyp@1273 3402 increment_total_collections(false /* full gc */);
ysr@777 3403
ysr@777 3404 #if G1_REM_SET_LOGGING
tonyp@1273 3405 gclog_or_tty->print_cr("\nJust chose CS, heap:");
ysr@777 3406 print();
ysr@777 3407 #endif
ysr@777 3408
tonyp@1273 3409 if (VerifyBeforeGC && total_collections() >= VerifyGCStartAt) {
tonyp@1273 3410 HandleMark hm; // Discard invalid handles created during verification
tonyp@1273 3411 prepare_for_verify();
tonyp@1273 3412 gclog_or_tty->print(" VerifyBeforeGC:");
tonyp@1273 3413 Universe::verify(false);
tonyp@1273 3414 }
tonyp@1273 3415
tonyp@1273 3416 COMPILER2_PRESENT(DerivedPointerTable::clear());
tonyp@1273 3417
johnc@2316 3418 // Please see comment in G1CollectedHeap::ref_processing_init()
johnc@2316 3419 // to see how reference processing currently works in G1.
johnc@2316 3420 //
tonyp@1273 3421 // We want to turn off ref discovery, if necessary, and turn it back on
ysr@1280 3422 // on again later if we do. XXX Dubious: why is discovery disabled?
tonyp@1273 3423 bool was_enabled = ref_processor()->discovery_enabled();
tonyp@1273 3424 if (was_enabled) ref_processor()->disable_discovery();
tonyp@1273 3425
tonyp@1273 3426 // Forget the current alloc region (we might even choose it to be part
tonyp@1273 3427 // of the collection set!).
tonyp@1273 3428 abandon_cur_alloc_region();
tonyp@1273 3429
tonyp@1273 3430 // The elapsed time induced by the start time below deliberately elides
tonyp@1273 3431 // the possible verification above.
tonyp@1273 3432 double start_time_sec = os::elapsedTime();
tonyp@1273 3433 size_t start_used_bytes = used();
tonyp@1273 3434
johnc@1829 3435 #if YOUNG_LIST_VERBOSE
johnc@1829 3436 gclog_or_tty->print_cr("\nBefore recording pause start.\nYoung_list:");
johnc@1829 3437 _young_list->print();
johnc@1829 3438 g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
johnc@1829 3439 #endif // YOUNG_LIST_VERBOSE
johnc@1829 3440
tonyp@1273 3441 g1_policy()->record_collection_pause_start(start_time_sec,
tonyp@1273 3442 start_used_bytes);
tonyp@1273 3443
johnc@1829 3444 #if YOUNG_LIST_VERBOSE
johnc@1829 3445 gclog_or_tty->print_cr("\nAfter recording pause start.\nYoung_list:");
tonyp@1273 3446 _young_list->print();
johnc@1829 3447 #endif // YOUNG_LIST_VERBOSE
ysr@777 3448
tonyp@1794 3449 if (g1_policy()->during_initial_mark_pause()) {
tonyp@1273 3450 concurrent_mark()->checkpointRootsInitialPre();
ysr@777 3451 }
tonyp@1273 3452 save_marks();
tonyp@1273 3453
tonyp@1273 3454 // We must do this before any possible evacuation that should propagate
tonyp@1273 3455 // marks.
tonyp@1273 3456 if (mark_in_progress()) {
tonyp@1273 3457 double start_time_sec = os::elapsedTime();
tonyp@1273 3458
tonyp@1273 3459 _cm->drainAllSATBBuffers();
tonyp@1273 3460 double finish_mark_ms = (os::elapsedTime() - start_time_sec) * 1000.0;
tonyp@1273 3461 g1_policy()->record_satb_drain_time(finish_mark_ms);
tonyp@1273 3462 }
tonyp@1273 3463 // Record the number of elements currently on the mark stack, so we
tonyp@1273 3464 // only iterate over these. (Since evacuation may add to the mark
tonyp@1273 3465 // stack, doing more exposes race conditions.) If no mark is in
tonyp@1273 3466 // progress, this will be zero.
tonyp@1273 3467 _cm->set_oops_do_bound();
tonyp@1273 3468
tonyp@1273 3469 if (mark_in_progress())
tonyp@1273 3470 concurrent_mark()->newCSet();
tonyp@1273 3471
johnc@1829 3472 #if YOUNG_LIST_VERBOSE
johnc@1829 3473 gclog_or_tty->print_cr("\nBefore choosing collection set.\nYoung_list:");
johnc@1829 3474 _young_list->print();
johnc@1829 3475 g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
johnc@1829 3476 #endif // YOUNG_LIST_VERBOSE
johnc@1829 3477
tonyp@2062 3478 g1_policy()->choose_collection_set(target_pause_time_ms);
tonyp@1273 3479
tonyp@1273 3480 // Nothing to do if we were unable to choose a collection set.
tonyp@1273 3481 #if G1_REM_SET_LOGGING
tonyp@2062 3482 gclog_or_tty->print_cr("\nAfter pause, heap:");
tonyp@2062 3483 print();
tonyp@1273 3484 #endif
tonyp@2062 3485 PrepareForRSScanningClosure prepare_for_rs_scan;
tonyp@2062 3486 collection_set_iterate(&prepare_for_rs_scan);
tonyp@2062 3487
tonyp@2062 3488 setup_surviving_young_words();
tonyp@2062 3489
tonyp@2062 3490 // Set up the gc allocation regions.
tonyp@2062 3491 get_gc_alloc_regions();
tonyp@2062 3492
tonyp@2062 3493 // Actually do the work...
tonyp@2062 3494 evacuate_collection_set();
tonyp@2062 3495
tonyp@2062 3496 free_collection_set(g1_policy()->collection_set());
tonyp@2062 3497 g1_policy()->clear_collection_set();
tonyp@2062 3498
tonyp@2062 3499 cleanup_surviving_young_words();
tonyp@2062 3500
tonyp@2062 3501 // Start a new incremental collection set for the next pause.
tonyp@2062 3502 g1_policy()->start_incremental_cset_building();
tonyp@2062 3503
tonyp@2062 3504 // Clear the _cset_fast_test bitmap in anticipation of adding
tonyp@2062 3505 // regions to the incremental collection set for the next
tonyp@2062 3506 // evacuation pause.
tonyp@2062 3507 clear_cset_fast_test();
tonyp@2062 3508
tonyp@2062 3509 if (g1_policy()->in_young_gc_mode()) {
tonyp@2062 3510 _young_list->reset_sampled_info();
tonyp@2062 3511
tonyp@2062 3512 // Don't check the whole heap at this point as the
tonyp@2062 3513 // GC alloc regions from this pause have been tagged
tonyp@2062 3514 // as survivors and moved on to the survivor list.
tonyp@2062 3515 // Survivor regions will fail the !is_young() check.
tonyp@2062 3516 assert(check_young_list_empty(false /* check_heap */),
tonyp@2062 3517 "young list should be empty");
johnc@1829 3518
johnc@1829 3519 #if YOUNG_LIST_VERBOSE
tonyp@2062 3520 gclog_or_tty->print_cr("Before recording survivors.\nYoung List:");
tonyp@2062 3521 _young_list->print();
johnc@1829 3522 #endif // YOUNG_LIST_VERBOSE
tonyp@1273 3523
tonyp@2062 3524 g1_policy()->record_survivor_regions(_young_list->survivor_length(),
tonyp@1273 3525 _young_list->first_survivor_region(),
tonyp@1273 3526 _young_list->last_survivor_region());
johnc@1829 3527
tonyp@2062 3528 _young_list->reset_auxilary_lists();
tonyp@1273 3529 }
tonyp@1273 3530
tonyp@1273 3531 if (evacuation_failed()) {
tonyp@1273 3532 _summary_bytes_used = recalculate_used();
tonyp@1273 3533 } else {
tonyp@1273 3534 // The "used" of the the collection set have already been subtracted
tonyp@1273 3535 // when they were freed. Add in the bytes evacuated.
tonyp@1273 3536 _summary_bytes_used += g1_policy()->bytes_in_to_space();
tonyp@1273 3537 }
tonyp@1273 3538
tonyp@1273 3539 if (g1_policy()->in_young_gc_mode() &&
tonyp@1794 3540 g1_policy()->during_initial_mark_pause()) {
tonyp@1273 3541 concurrent_mark()->checkpointRootsInitialPost();
tonyp@1273 3542 set_marking_started();
ysr@1280 3543 // CAUTION: after the doConcurrentMark() call below,
ysr@1280 3544 // the concurrent marking thread(s) could be running
ysr@1280 3545 // concurrently with us. Make sure that anything after
ysr@1280 3546 // this point does not assume that we are the only GC thread
ysr@1280 3547 // running. Note: of course, the actual marking work will
ysr@1280 3548 // not start until the safepoint itself is released in
ysr@1280 3549 // ConcurrentGCThread::safepoint_desynchronize().
tonyp@1273 3550 doConcurrentMark();
tonyp@1273 3551 }
tonyp@1273 3552
johnc@1829 3553 #if YOUNG_LIST_VERBOSE
johnc@1829 3554 gclog_or_tty->print_cr("\nEnd of the pause.\nYoung_list:");
tonyp@1273 3555 _young_list->print();
johnc@1829 3556 g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
johnc@1829 3557 #endif // YOUNG_LIST_VERBOSE
tonyp@1273 3558
tonyp@1273 3559 double end_time_sec = os::elapsedTime();
tonyp@1273 3560 double pause_time_ms = (end_time_sec - start_time_sec) * MILLIUNITS;
tonyp@1273 3561 g1_policy()->record_pause_time_ms(pause_time_ms);
tonyp@2062 3562 g1_policy()->record_collection_pause_end();
tonyp@1273 3563
tonyp@1524 3564 MemoryService::track_memory_usage();
tonyp@1524 3565
tonyp@1273 3566 if (VerifyAfterGC && total_collections() >= VerifyGCStartAt) {
tonyp@1273 3567 HandleMark hm; // Discard invalid handles created during verification
tonyp@1273 3568 gclog_or_tty->print(" VerifyAfterGC:");
tonyp@1273 3569 prepare_for_verify();
tonyp@1273 3570 Universe::verify(false);
tonyp@1273 3571 }
tonyp@1273 3572
tonyp@1273 3573 if (was_enabled) ref_processor()->enable_discovery();
tonyp@1273 3574
tonyp@1273 3575 {
tonyp@1273 3576 size_t expand_bytes = g1_policy()->expansion_amount();
tonyp@1273 3577 if (expand_bytes > 0) {
tonyp@1273 3578 size_t bytes_before = capacity();
johnc@2504 3579 if (!expand(expand_bytes)) {
johnc@2504 3580 // We failed to expand the heap so let's verify that
johnc@2504 3581 // committed/uncommitted amount match the backing store
johnc@2504 3582 assert(capacity() == _g1_storage.committed_size(), "committed size mismatch");
johnc@2504 3583 assert(max_capacity() == _g1_storage.reserved_size(), "reserved size mismatch");
johnc@2504 3584 }
tonyp@1273 3585 }
tonyp@1273 3586 }
tonyp@1273 3587
tonyp@1273 3588 if (mark_in_progress()) {
tonyp@1273 3589 concurrent_mark()->update_g1_committed();
tonyp@1273 3590 }
tonyp@1273 3591
tonyp@1273 3592 #ifdef TRACESPINNING
tonyp@1273 3593 ParallelTaskTerminator::print_termination_counts();
tonyp@1273 3594 #endif
tonyp@1273 3595
tonyp@1273 3596 gc_epilogue(false);
ysr@777 3597 }
ysr@777 3598
tonyp@1273 3599 if (ExitAfterGCNum > 0 && total_collections() == ExitAfterGCNum) {
tonyp@1273 3600 gclog_or_tty->print_cr("Stopping after GC #%d", ExitAfterGCNum);
tonyp@1273 3601 print_tracing_info();
tonyp@1273 3602 vm_exit(-1);
ysr@777 3603 }
tonyp@1273 3604 }
tonyp@1273 3605
tonyp@2472 3606 verify_region_sets_optional();
tonyp@2472 3607
jcoomes@2064 3608 TASKQUEUE_STATS_ONLY(if (ParallelGCVerbose) print_taskqueue_stats());
jcoomes@2064 3609 TASKQUEUE_STATS_ONLY(reset_taskqueue_stats());
jcoomes@2064 3610
tonyp@1273 3611 if (PrintHeapAtGC) {
tonyp@1273 3612 Universe::print_heap_after_gc();
ysr@777 3613 }
tonyp@1319 3614 if (G1SummarizeRSetStats &&
tonyp@1319 3615 (G1SummarizeRSetStatsPeriod > 0) &&
tonyp@1319 3616 (total_collections() % G1SummarizeRSetStatsPeriod == 0)) {
tonyp@1319 3617 g1_rem_set()->print_summary_info();
tonyp@1319 3618 }
tonyp@2315 3619
tonyp@2315 3620 return true;
ysr@777 3621 }
ysr@777 3622
apetrusenko@1826 3623 size_t G1CollectedHeap::desired_plab_sz(GCAllocPurpose purpose)
apetrusenko@1826 3624 {
apetrusenko@1826 3625 size_t gclab_word_size;
apetrusenko@1826 3626 switch (purpose) {
apetrusenko@1826 3627 case GCAllocForSurvived:
apetrusenko@1826 3628 gclab_word_size = YoungPLABSize;
apetrusenko@1826 3629 break;
apetrusenko@1826 3630 case GCAllocForTenured:
apetrusenko@1826 3631 gclab_word_size = OldPLABSize;
apetrusenko@1826 3632 break;
apetrusenko@1826 3633 default:
apetrusenko@1826 3634 assert(false, "unknown GCAllocPurpose");
apetrusenko@1826 3635 gclab_word_size = OldPLABSize;
apetrusenko@1826 3636 break;
apetrusenko@1826 3637 }
apetrusenko@1826 3638 return gclab_word_size;
apetrusenko@1826 3639 }
apetrusenko@1826 3640
apetrusenko@1826 3641
ysr@777 3642 void G1CollectedHeap::set_gc_alloc_region(int purpose, HeapRegion* r) {
ysr@777 3643 assert(purpose >= 0 && purpose < GCAllocPurposeCount, "invalid purpose");
tonyp@1071 3644 // make sure we don't call set_gc_alloc_region() multiple times on
tonyp@1071 3645 // the same region
tonyp@1071 3646 assert(r == NULL || !r->is_gc_alloc_region(),
tonyp@1071 3647 "shouldn't already be a GC alloc region");
johnc@1795 3648 assert(r == NULL || !r->isHumongous(),
johnc@1795 3649 "humongous regions shouldn't be used as GC alloc regions");
johnc@1795 3650
ysr@777 3651 HeapWord* original_top = NULL;
ysr@777 3652 if (r != NULL)
ysr@777 3653 original_top = r->top();
ysr@777 3654
ysr@777 3655 // We will want to record the used space in r as being there before gc.
ysr@777 3656 // One we install it as a GC alloc region it's eligible for allocation.
ysr@777 3657 // So record it now and use it later.
ysr@777 3658 size_t r_used = 0;
ysr@777 3659 if (r != NULL) {
ysr@777 3660 r_used = r->used();
ysr@777 3661
jmasa@2188 3662 if (G1CollectedHeap::use_parallel_gc_threads()) {
ysr@777 3663 // need to take the lock to guard against two threads calling
ysr@777 3664 // get_gc_alloc_region concurrently (very unlikely but...)
ysr@777 3665 MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);
ysr@777 3666 r->save_marks();
ysr@777 3667 }
ysr@777 3668 }
ysr@777 3669 HeapRegion* old_alloc_region = _gc_alloc_regions[purpose];
ysr@777 3670 _gc_alloc_regions[purpose] = r;
ysr@777 3671 if (old_alloc_region != NULL) {
ysr@777 3672 // Replace aliases too.
ysr@777 3673 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
ysr@777 3674 if (_gc_alloc_regions[ap] == old_alloc_region) {
ysr@777 3675 _gc_alloc_regions[ap] = r;
ysr@777 3676 }
ysr@777 3677 }
ysr@777 3678 }
ysr@777 3679 if (r != NULL) {
ysr@777 3680 push_gc_alloc_region(r);
ysr@777 3681 if (mark_in_progress() && original_top != r->next_top_at_mark_start()) {
ysr@777 3682 // We are using a region as a GC alloc region after it has been used
ysr@777 3683 // as a mutator allocation region during the current marking cycle.
ysr@777 3684 // The mutator-allocated objects are currently implicitly marked, but
ysr@777 3685 // when we move hr->next_top_at_mark_start() forward at the the end
ysr@777 3686 // of the GC pause, they won't be. We therefore mark all objects in
ysr@777 3687 // the "gap". We do this object-by-object, since marking densely
ysr@777 3688 // does not currently work right with marking bitmap iteration. This
ysr@777 3689 // means we rely on TLAB filling at the start of pauses, and no
ysr@777 3690 // "resuscitation" of filled TLAB's. If we want to do this, we need
ysr@777 3691 // to fix the marking bitmap iteration.
ysr@777 3692 HeapWord* curhw = r->next_top_at_mark_start();
ysr@777 3693 HeapWord* t = original_top;
ysr@777 3694
ysr@777 3695 while (curhw < t) {
ysr@777 3696 oop cur = (oop)curhw;
ysr@777 3697 // We'll assume parallel for generality. This is rare code.
ysr@777 3698 concurrent_mark()->markAndGrayObjectIfNecessary(cur); // can't we just mark them?
ysr@777 3699 curhw = curhw + cur->size();
ysr@777 3700 }
ysr@777 3701 assert(curhw == t, "Should have parsed correctly.");
ysr@777 3702 }
ysr@777 3703 if (G1PolicyVerbose > 1) {
ysr@777 3704 gclog_or_tty->print("New alloc region ["PTR_FORMAT", "PTR_FORMAT", " PTR_FORMAT") "
ysr@777 3705 "for survivors:", r->bottom(), original_top, r->end());
ysr@777 3706 r->print();
ysr@777 3707 }
ysr@777 3708 g1_policy()->record_before_bytes(r_used);
ysr@777 3709 }
ysr@777 3710 }
ysr@777 3711
ysr@777 3712 void G1CollectedHeap::push_gc_alloc_region(HeapRegion* hr) {
ysr@777 3713 assert(Thread::current()->is_VM_thread() ||
tonyp@2472 3714 FreeList_lock->owned_by_self(), "Precondition");
ysr@777 3715 assert(!hr->is_gc_alloc_region() && !hr->in_collection_set(),
ysr@777 3716 "Precondition.");
ysr@777 3717 hr->set_is_gc_alloc_region(true);
ysr@777 3718 hr->set_next_gc_alloc_region(_gc_alloc_region_list);
ysr@777 3719 _gc_alloc_region_list = hr;
ysr@777 3720 }
ysr@777 3721
ysr@777 3722 #ifdef G1_DEBUG
ysr@777 3723 class FindGCAllocRegion: public HeapRegionClosure {
ysr@777 3724 public:
ysr@777 3725 bool doHeapRegion(HeapRegion* r) {
ysr@777 3726 if (r->is_gc_alloc_region()) {
ysr@777 3727 gclog_or_tty->print_cr("Region %d ["PTR_FORMAT"...] is still a gc_alloc_region.",
ysr@777 3728 r->hrs_index(), r->bottom());
ysr@777 3729 }
ysr@777 3730 return false;
ysr@777 3731 }
ysr@777 3732 };
ysr@777 3733 #endif // G1_DEBUG
ysr@777 3734
ysr@777 3735 void G1CollectedHeap::forget_alloc_region_list() {
tonyp@2472 3736 assert_at_safepoint(true /* should_be_vm_thread */);
ysr@777 3737 while (_gc_alloc_region_list != NULL) {
ysr@777 3738 HeapRegion* r = _gc_alloc_region_list;
ysr@777 3739 assert(r->is_gc_alloc_region(), "Invariant.");
iveresov@1072 3740 // We need HeapRegion::oops_on_card_seq_iterate_careful() to work on
iveresov@1072 3741 // newly allocated data in order to be able to apply deferred updates
iveresov@1072 3742 // before the GC is done for verification purposes (i.e to allow
iveresov@1072 3743 // G1HRRSFlushLogBuffersOnVerify). It's safe thing to do after the
iveresov@1072 3744 // collection.
iveresov@1072 3745 r->ContiguousSpace::set_saved_mark();
ysr@777 3746 _gc_alloc_region_list = r->next_gc_alloc_region();
ysr@777 3747 r->set_next_gc_alloc_region(NULL);
ysr@777 3748 r->set_is_gc_alloc_region(false);
apetrusenko@980 3749 if (r->is_survivor()) {
apetrusenko@980 3750 if (r->is_empty()) {
apetrusenko@980 3751 r->set_not_young();
apetrusenko@980 3752 } else {
apetrusenko@980 3753 _young_list->add_survivor_region(r);
apetrusenko@980 3754 }
apetrusenko@980 3755 }
ysr@777 3756 }
ysr@777 3757 #ifdef G1_DEBUG
ysr@777 3758 FindGCAllocRegion fa;
ysr@777 3759 heap_region_iterate(&fa);
ysr@777 3760 #endif // G1_DEBUG
ysr@777 3761 }
ysr@777 3762
ysr@777 3763
ysr@777 3764 bool G1CollectedHeap::check_gc_alloc_regions() {
ysr@777 3765 // TODO: allocation regions check
ysr@777 3766 return true;
ysr@777 3767 }
ysr@777 3768
ysr@777 3769 void G1CollectedHeap::get_gc_alloc_regions() {
tonyp@1071 3770 // First, let's check that the GC alloc region list is empty (it should)
tonyp@1071 3771 assert(_gc_alloc_region_list == NULL, "invariant");
tonyp@1071 3772
ysr@777 3773 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
tonyp@1071 3774 assert(_gc_alloc_regions[ap] == NULL, "invariant");
apetrusenko@1296 3775 assert(_gc_alloc_region_counts[ap] == 0, "invariant");
tonyp@1071 3776
ysr@777 3777 // Create new GC alloc regions.
tonyp@1071 3778 HeapRegion* alloc_region = _retained_gc_alloc_regions[ap];
tonyp@1071 3779 _retained_gc_alloc_regions[ap] = NULL;
tonyp@1071 3780
tonyp@1071 3781 if (alloc_region != NULL) {
tonyp@1071 3782 assert(_retain_gc_alloc_region[ap], "only way to retain a GC region");
tonyp@1071 3783
tonyp@1071 3784 // let's make sure that the GC alloc region is not tagged as such
tonyp@1071 3785 // outside a GC operation
tonyp@1071 3786 assert(!alloc_region->is_gc_alloc_region(), "sanity");
tonyp@1071 3787
tonyp@1071 3788 if (alloc_region->in_collection_set() ||
tonyp@1071 3789 alloc_region->top() == alloc_region->end() ||
johnc@1795 3790 alloc_region->top() == alloc_region->bottom() ||
johnc@1795 3791 alloc_region->isHumongous()) {
johnc@1795 3792 // we will discard the current GC alloc region if
johnc@1795 3793 // * it's in the collection set (it can happen!),
johnc@1795 3794 // * it's already full (no point in using it),
johnc@1795 3795 // * it's empty (this means that it was emptied during
johnc@1795 3796 // a cleanup and it should be on the free list now), or
johnc@1795 3797 // * it's humongous (this means that it was emptied
johnc@1795 3798 // during a cleanup and was added to the free list, but
johnc@1795 3799 // has been subseqently used to allocate a humongous
johnc@1795 3800 // object that may be less than the region size).
tonyp@1071 3801
tonyp@1071 3802 alloc_region = NULL;
tonyp@1071 3803 }
tonyp@1071 3804 }
tonyp@1071 3805
tonyp@1071 3806 if (alloc_region == NULL) {
tonyp@1071 3807 // we will get a new GC alloc region
johnc@2504 3808 alloc_region = new_gc_alloc_region(ap, HeapRegion::GrainWords);
apetrusenko@1296 3809 } else {
apetrusenko@1296 3810 // the region was retained from the last collection
apetrusenko@1296 3811 ++_gc_alloc_region_counts[ap];
tonyp@1823 3812 if (G1PrintHeapRegions) {
tonyp@1823 3813 gclog_or_tty->print_cr("new alloc region %d:["PTR_FORMAT", "PTR_FORMAT"], "
tonyp@1823 3814 "top "PTR_FORMAT,
tonyp@1823 3815 alloc_region->hrs_index(), alloc_region->bottom(), alloc_region->end(), alloc_region->top());
tonyp@1823 3816 }
ysr@777 3817 }
tonyp@1071 3818
ysr@777 3819 if (alloc_region != NULL) {
tonyp@1071 3820 assert(_gc_alloc_regions[ap] == NULL, "pre-condition");
ysr@777 3821 set_gc_alloc_region(ap, alloc_region);
ysr@777 3822 }
tonyp@1071 3823
tonyp@1071 3824 assert(_gc_alloc_regions[ap] == NULL ||
tonyp@1071 3825 _gc_alloc_regions[ap]->is_gc_alloc_region(),
tonyp@1071 3826 "the GC alloc region should be tagged as such");
tonyp@1071 3827 assert(_gc_alloc_regions[ap] == NULL ||
tonyp@1071 3828 _gc_alloc_regions[ap] == _gc_alloc_region_list,
tonyp@1071 3829 "the GC alloc region should be the same as the GC alloc list head");
ysr@777 3830 }
ysr@777 3831 // Set alternative regions for allocation purposes that have reached
tonyp@1071 3832 // their limit.
ysr@777 3833 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
ysr@777 3834 GCAllocPurpose alt_purpose = g1_policy()->alternative_purpose(ap);
ysr@777 3835 if (_gc_alloc_regions[ap] == NULL && alt_purpose != ap) {
ysr@777 3836 _gc_alloc_regions[ap] = _gc_alloc_regions[alt_purpose];
ysr@777 3837 }
ysr@777 3838 }
ysr@777 3839 assert(check_gc_alloc_regions(), "alloc regions messed up");
ysr@777 3840 }
ysr@777 3841
tonyp@1071 3842 void G1CollectedHeap::release_gc_alloc_regions(bool totally) {
ysr@777 3843 // We keep a separate list of all regions that have been alloc regions in
tonyp@1071 3844 // the current collection pause. Forget that now. This method will
tonyp@1071 3845 // untag the GC alloc regions and tear down the GC alloc region
tonyp@1071 3846 // list. It's desirable that no regions are tagged as GC alloc
tonyp@1071 3847 // outside GCs.
johnc@2316 3848
ysr@777 3849 forget_alloc_region_list();
ysr@777 3850
ysr@777 3851 // The current alloc regions contain objs that have survived
ysr@777 3852 // collection. Make them no longer GC alloc regions.
ysr@777 3853 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
ysr@777 3854 HeapRegion* r = _gc_alloc_regions[ap];
tonyp@1071 3855 _retained_gc_alloc_regions[ap] = NULL;
apetrusenko@1296 3856 _gc_alloc_region_counts[ap] = 0;
tonyp@1071 3857
tonyp@1071 3858 if (r != NULL) {
tonyp@1071 3859 // we retain nothing on _gc_alloc_regions between GCs
tonyp@1071 3860 set_gc_alloc_region(ap, NULL);
tonyp@1071 3861
tonyp@1071 3862 if (r->is_empty()) {
tonyp@2472 3863 // We didn't actually allocate anything in it; let's just put
tonyp@2472 3864 // it back on the free list.
tonyp@2472 3865 _free_list.add_as_tail(r);
tonyp@1071 3866 } else if (_retain_gc_alloc_region[ap] && !totally) {
tonyp@1071 3867 // retain it so that we can use it at the beginning of the next GC
tonyp@1071 3868 _retained_gc_alloc_regions[ap] = r;
ysr@777 3869 }
ysr@777 3870 }
tonyp@1071 3871 }
tonyp@1071 3872 }
tonyp@1071 3873
tonyp@1071 3874 #ifndef PRODUCT
tonyp@1071 3875 // Useful for debugging
tonyp@1071 3876
tonyp@1071 3877 void G1CollectedHeap::print_gc_alloc_regions() {
tonyp@1071 3878 gclog_or_tty->print_cr("GC alloc regions");
tonyp@1071 3879 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
tonyp@1071 3880 HeapRegion* r = _gc_alloc_regions[ap];
tonyp@1071 3881 if (r == NULL) {
tonyp@1071 3882 gclog_or_tty->print_cr(" %2d : "PTR_FORMAT, ap, NULL);
tonyp@1071 3883 } else {
tonyp@1071 3884 gclog_or_tty->print_cr(" %2d : "PTR_FORMAT" "SIZE_FORMAT,
tonyp@1071 3885 ap, r->bottom(), r->used());
tonyp@1071 3886 }
tonyp@1071 3887 }
tonyp@1071 3888 }
tonyp@1071 3889 #endif // PRODUCT
ysr@777 3890
ysr@777 3891 void G1CollectedHeap::init_for_evac_failure(OopsInHeapRegionClosure* cl) {
ysr@777 3892 _drain_in_progress = false;
ysr@777 3893 set_evac_failure_closure(cl);
ysr@777 3894 _evac_failure_scan_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true);
ysr@777 3895 }
ysr@777 3896
ysr@777 3897 void G1CollectedHeap::finalize_for_evac_failure() {
ysr@777 3898 assert(_evac_failure_scan_stack != NULL &&
ysr@777 3899 _evac_failure_scan_stack->length() == 0,
ysr@777 3900 "Postcondition");
ysr@777 3901 assert(!_drain_in_progress, "Postcondition");
apetrusenko@1480 3902 delete _evac_failure_scan_stack;
ysr@777 3903 _evac_failure_scan_stack = NULL;
ysr@777 3904 }
ysr@777 3905
ysr@777 3906
ysr@777 3907
ysr@777 3908 // *** Sequential G1 Evacuation
ysr@777 3909
ysr@777 3910 class G1IsAliveClosure: public BoolObjectClosure {
ysr@777 3911 G1CollectedHeap* _g1;
ysr@777 3912 public:
ysr@777 3913 G1IsAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
ysr@777 3914 void do_object(oop p) { assert(false, "Do not call."); }
ysr@777 3915 bool do_object_b(oop p) {
ysr@777 3916 // It is reachable if it is outside the collection set, or is inside
ysr@777 3917 // and forwarded.
ysr@777 3918
ysr@777 3919 #ifdef G1_DEBUG
ysr@777 3920 gclog_or_tty->print_cr("is alive "PTR_FORMAT" in CS %d forwarded %d overall %d",
ysr@777 3921 (void*) p, _g1->obj_in_cs(p), p->is_forwarded(),
ysr@777 3922 !_g1->obj_in_cs(p) || p->is_forwarded());
ysr@777 3923 #endif // G1_DEBUG
ysr@777 3924
ysr@777 3925 return !_g1->obj_in_cs(p) || p->is_forwarded();
ysr@777 3926 }
ysr@777 3927 };
ysr@777 3928
ysr@777 3929 class G1KeepAliveClosure: public OopClosure {
ysr@777 3930 G1CollectedHeap* _g1;
ysr@777 3931 public:
ysr@777 3932 G1KeepAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
ysr@1280 3933 void do_oop(narrowOop* p) { guarantee(false, "Not needed"); }
ysr@1280 3934 void do_oop( oop* p) {
ysr@777 3935 oop obj = *p;
ysr@777 3936 #ifdef G1_DEBUG
ysr@777 3937 if (PrintGC && Verbose) {
ysr@777 3938 gclog_or_tty->print_cr("keep alive *"PTR_FORMAT" = "PTR_FORMAT" "PTR_FORMAT,
ysr@777 3939 p, (void*) obj, (void*) *p);
ysr@777 3940 }
ysr@777 3941 #endif // G1_DEBUG
ysr@777 3942
ysr@777 3943 if (_g1->obj_in_cs(obj)) {
ysr@777 3944 assert( obj->is_forwarded(), "invariant" );
ysr@777 3945 *p = obj->forwardee();
ysr@777 3946 #ifdef G1_DEBUG
ysr@777 3947 gclog_or_tty->print_cr(" in CSet: moved "PTR_FORMAT" -> "PTR_FORMAT,
ysr@777 3948 (void*) obj, (void*) *p);
ysr@777 3949 #endif // G1_DEBUG
ysr@777 3950 }
ysr@777 3951 }
ysr@777 3952 };
ysr@777 3953
iveresov@1051 3954 class UpdateRSetDeferred : public OopsInHeapRegionClosure {
iveresov@1051 3955 private:
iveresov@1051 3956 G1CollectedHeap* _g1;
iveresov@1051 3957 DirtyCardQueue *_dcq;
iveresov@1051 3958 CardTableModRefBS* _ct_bs;
iveresov@1051 3959
iveresov@1051 3960 public:
iveresov@1051 3961 UpdateRSetDeferred(G1CollectedHeap* g1, DirtyCardQueue* dcq) :
iveresov@1051 3962 _g1(g1), _ct_bs((CardTableModRefBS*)_g1->barrier_set()), _dcq(dcq) {}
iveresov@1051 3963
ysr@1280 3964 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
ysr@1280 3965 virtual void do_oop( oop* p) { do_oop_work(p); }
ysr@1280 3966 template <class T> void do_oop_work(T* p) {
iveresov@1051 3967 assert(_from->is_in_reserved(p), "paranoia");
ysr@1280 3968 if (!_from->is_in_reserved(oopDesc::load_decode_heap_oop(p)) &&
ysr@1280 3969 !_from->is_survivor()) {
iveresov@1051 3970 size_t card_index = _ct_bs->index_for(p);
iveresov@1051 3971 if (_ct_bs->mark_card_deferred(card_index)) {
iveresov@1051 3972 _dcq->enqueue((jbyte*)_ct_bs->byte_for_index(card_index));
iveresov@1051 3973 }
iveresov@1051 3974 }
iveresov@1051 3975 }
iveresov@1051 3976 };
iveresov@1051 3977
ysr@777 3978 class RemoveSelfPointerClosure: public ObjectClosure {
ysr@777 3979 private:
ysr@777 3980 G1CollectedHeap* _g1;
ysr@777 3981 ConcurrentMark* _cm;
ysr@777 3982 HeapRegion* _hr;
ysr@777 3983 size_t _prev_marked_bytes;
ysr@777 3984 size_t _next_marked_bytes;
iveresov@1051 3985 OopsInHeapRegionClosure *_cl;
ysr@777 3986 public:
tonyp@2453 3987 RemoveSelfPointerClosure(G1CollectedHeap* g1, HeapRegion* hr,
tonyp@2453 3988 OopsInHeapRegionClosure* cl) :
tonyp@2453 3989 _g1(g1), _hr(hr), _cm(_g1->concurrent_mark()), _prev_marked_bytes(0),
iveresov@1051 3990 _next_marked_bytes(0), _cl(cl) {}
ysr@777 3991
ysr@777 3992 size_t prev_marked_bytes() { return _prev_marked_bytes; }
ysr@777 3993 size_t next_marked_bytes() { return _next_marked_bytes; }
ysr@777 3994
tonyp@2453 3995 // <original comment>
iveresov@787 3996 // The original idea here was to coalesce evacuated and dead objects.
iveresov@787 3997 // However that caused complications with the block offset table (BOT).
iveresov@787 3998 // In particular if there were two TLABs, one of them partially refined.
iveresov@787 3999 // |----- TLAB_1--------|----TLAB_2-~~~(partially refined part)~~~|
iveresov@787 4000 // The BOT entries of the unrefined part of TLAB_2 point to the start
iveresov@787 4001 // of TLAB_2. If the last object of the TLAB_1 and the first object
iveresov@787 4002 // of TLAB_2 are coalesced, then the cards of the unrefined part
iveresov@787 4003 // would point into middle of the filler object.
tonyp@2453 4004 // The current approach is to not coalesce and leave the BOT contents intact.
tonyp@2453 4005 // </original comment>
iveresov@787 4006 //
tonyp@2453 4007 // We now reset the BOT when we start the object iteration over the
tonyp@2453 4008 // region and refine its entries for every object we come across. So
tonyp@2453 4009 // the above comment is not really relevant and we should be able
tonyp@2453 4010 // to coalesce dead objects if we want to.
iveresov@787 4011 void do_object(oop obj) {
tonyp@2453 4012 HeapWord* obj_addr = (HeapWord*) obj;
tonyp@2453 4013 assert(_hr->is_in(obj_addr), "sanity");
tonyp@2453 4014 size_t obj_size = obj->size();
tonyp@2453 4015 _hr->update_bot_for_object(obj_addr, obj_size);
iveresov@787 4016 if (obj->is_forwarded() && obj->forwardee() == obj) {
iveresov@787 4017 // The object failed to move.
iveresov@787 4018 assert(!_g1->is_obj_dead(obj), "We should not be preserving dead objs.");
iveresov@787 4019 _cm->markPrev(obj);
iveresov@787 4020 assert(_cm->isPrevMarked(obj), "Should be marked!");
tonyp@2453 4021 _prev_marked_bytes += (obj_size * HeapWordSize);
iveresov@787 4022 if (_g1->mark_in_progress() && !_g1->is_obj_ill(obj)) {
iveresov@787 4023 _cm->markAndGrayObjectIfNecessary(obj);
iveresov@787 4024 }
iveresov@787 4025 obj->set_mark(markOopDesc::prototype());
iveresov@787 4026 // While we were processing RSet buffers during the
iveresov@787 4027 // collection, we actually didn't scan any cards on the
iveresov@787 4028 // collection set, since we didn't want to update remebered
iveresov@787 4029 // sets with entries that point into the collection set, given
iveresov@787 4030 // that live objects fromthe collection set are about to move
iveresov@787 4031 // and such entries will be stale very soon. This change also
iveresov@787 4032 // dealt with a reliability issue which involved scanning a
iveresov@787 4033 // card in the collection set and coming across an array that
iveresov@787 4034 // was being chunked and looking malformed. The problem is
iveresov@787 4035 // that, if evacuation fails, we might have remembered set
iveresov@787 4036 // entries missing given that we skipped cards on the
iveresov@787 4037 // collection set. So, we'll recreate such entries now.
iveresov@1051 4038 obj->oop_iterate(_cl);
iveresov@787 4039 assert(_cm->isPrevMarked(obj), "Should be marked!");
iveresov@787 4040 } else {
iveresov@787 4041 // The object has been either evacuated or is dead. Fill it with a
iveresov@787 4042 // dummy object.
tonyp@2453 4043 MemRegion mr((HeapWord*)obj, obj_size);
jcoomes@916 4044 CollectedHeap::fill_with_object(mr);
ysr@777 4045 _cm->clearRangeBothMaps(mr);
ysr@777 4046 }
ysr@777 4047 }
ysr@777 4048 };
ysr@777 4049
ysr@777 4050 void G1CollectedHeap::remove_self_forwarding_pointers() {
johnc@2060 4051 UpdateRSetImmediate immediate_update(_g1h->g1_rem_set());
iveresov@1051 4052 DirtyCardQueue dcq(&_g1h->dirty_card_queue_set());
iveresov@1051 4053 UpdateRSetDeferred deferred_update(_g1h, &dcq);
iveresov@1051 4054 OopsInHeapRegionClosure *cl;
iveresov@1051 4055 if (G1DeferredRSUpdate) {
iveresov@1051 4056 cl = &deferred_update;
iveresov@1051 4057 } else {
iveresov@1051 4058 cl = &immediate_update;
iveresov@1051 4059 }
ysr@777 4060 HeapRegion* cur = g1_policy()->collection_set();
ysr@777 4061 while (cur != NULL) {
ysr@777 4062 assert(g1_policy()->assertMarkedBytesDataOK(), "Should be!");
tonyp@2453 4063 assert(!cur->isHumongous(), "sanity");
tonyp@2453 4064
ysr@777 4065 if (cur->evacuation_failed()) {
ysr@777 4066 assert(cur->in_collection_set(), "bad CS");
tonyp@2453 4067 RemoveSelfPointerClosure rspc(_g1h, cur, cl);
tonyp@2453 4068
tonyp@2453 4069 cur->reset_bot();
iveresov@1051 4070 cl->set_region(cur);
ysr@777 4071 cur->object_iterate(&rspc);
ysr@777 4072
ysr@777 4073 // A number of manipulations to make the TAMS be the current top,
ysr@777 4074 // and the marked bytes be the ones observed in the iteration.
ysr@777 4075 if (_g1h->concurrent_mark()->at_least_one_mark_complete()) {
ysr@777 4076 // The comments below are the postconditions achieved by the
ysr@777 4077 // calls. Note especially the last such condition, which says that
ysr@777 4078 // the count of marked bytes has been properly restored.
ysr@777 4079 cur->note_start_of_marking(false);
ysr@777 4080 // _next_top_at_mark_start == top, _next_marked_bytes == 0
ysr@777 4081 cur->add_to_marked_bytes(rspc.prev_marked_bytes());
ysr@777 4082 // _next_marked_bytes == prev_marked_bytes.
ysr@777 4083 cur->note_end_of_marking();
ysr@777 4084 // _prev_top_at_mark_start == top(),
ysr@777 4085 // _prev_marked_bytes == prev_marked_bytes
ysr@777 4086 }
ysr@777 4087 // If there is no mark in progress, we modified the _next variables
ysr@777 4088 // above needlessly, but harmlessly.
ysr@777 4089 if (_g1h->mark_in_progress()) {
ysr@777 4090 cur->note_start_of_marking(false);
ysr@777 4091 // _next_top_at_mark_start == top, _next_marked_bytes == 0
ysr@777 4092 // _next_marked_bytes == next_marked_bytes.
ysr@777 4093 }
ysr@777 4094
ysr@777 4095 // Now make sure the region has the right index in the sorted array.
ysr@777 4096 g1_policy()->note_change_in_marked_bytes(cur);
ysr@777 4097 }
ysr@777 4098 cur = cur->next_in_collection_set();
ysr@777 4099 }
ysr@777 4100 assert(g1_policy()->assertMarkedBytesDataOK(), "Should be!");
ysr@777 4101
ysr@777 4102 // Now restore saved marks, if any.
ysr@777 4103 if (_objs_with_preserved_marks != NULL) {
ysr@777 4104 assert(_preserved_marks_of_objs != NULL, "Both or none.");
ysr@777 4105 guarantee(_objs_with_preserved_marks->length() ==
ysr@777 4106 _preserved_marks_of_objs->length(), "Both or none.");
ysr@777 4107 for (int i = 0; i < _objs_with_preserved_marks->length(); i++) {
ysr@777 4108 oop obj = _objs_with_preserved_marks->at(i);
ysr@777 4109 markOop m = _preserved_marks_of_objs->at(i);
ysr@777 4110 obj->set_mark(m);
ysr@777 4111 }
ysr@777 4112 // Delete the preserved marks growable arrays (allocated on the C heap).
ysr@777 4113 delete _objs_with_preserved_marks;
ysr@777 4114 delete _preserved_marks_of_objs;
ysr@777 4115 _objs_with_preserved_marks = NULL;
ysr@777 4116 _preserved_marks_of_objs = NULL;
ysr@777 4117 }
ysr@777 4118 }
ysr@777 4119
ysr@777 4120 void G1CollectedHeap::push_on_evac_failure_scan_stack(oop obj) {
ysr@777 4121 _evac_failure_scan_stack->push(obj);
ysr@777 4122 }
ysr@777 4123
ysr@777 4124 void G1CollectedHeap::drain_evac_failure_scan_stack() {
ysr@777 4125 assert(_evac_failure_scan_stack != NULL, "precondition");
ysr@777 4126
ysr@777 4127 while (_evac_failure_scan_stack->length() > 0) {
ysr@777 4128 oop obj = _evac_failure_scan_stack->pop();
ysr@777 4129 _evac_failure_closure->set_region(heap_region_containing(obj));
ysr@777 4130 obj->oop_iterate_backwards(_evac_failure_closure);
ysr@777 4131 }
ysr@777 4132 }
ysr@777 4133
ysr@777 4134 oop
ysr@777 4135 G1CollectedHeap::handle_evacuation_failure_par(OopsInHeapRegionClosure* cl,
ysr@777 4136 oop old) {
ysr@777 4137 markOop m = old->mark();
ysr@777 4138 oop forward_ptr = old->forward_to_atomic(old);
ysr@777 4139 if (forward_ptr == NULL) {
ysr@777 4140 // Forward-to-self succeeded.
ysr@777 4141 if (_evac_failure_closure != cl) {
ysr@777 4142 MutexLockerEx x(EvacFailureStack_lock, Mutex::_no_safepoint_check_flag);
ysr@777 4143 assert(!_drain_in_progress,
ysr@777 4144 "Should only be true while someone holds the lock.");
ysr@777 4145 // Set the global evac-failure closure to the current thread's.
ysr@777 4146 assert(_evac_failure_closure == NULL, "Or locking has failed.");
ysr@777 4147 set_evac_failure_closure(cl);
ysr@777 4148 // Now do the common part.
ysr@777 4149 handle_evacuation_failure_common(old, m);
ysr@777 4150 // Reset to NULL.
ysr@777 4151 set_evac_failure_closure(NULL);
ysr@777 4152 } else {
ysr@777 4153 // The lock is already held, and this is recursive.
ysr@777 4154 assert(_drain_in_progress, "This should only be the recursive case.");
ysr@777 4155 handle_evacuation_failure_common(old, m);
ysr@777 4156 }
ysr@777 4157 return old;
ysr@777 4158 } else {
ysr@777 4159 // Someone else had a place to copy it.
ysr@777 4160 return forward_ptr;
ysr@777 4161 }
ysr@777 4162 }
ysr@777 4163
ysr@777 4164 void G1CollectedHeap::handle_evacuation_failure_common(oop old, markOop m) {
ysr@777 4165 set_evacuation_failed(true);
ysr@777 4166
ysr@777 4167 preserve_mark_if_necessary(old, m);
ysr@777 4168
ysr@777 4169 HeapRegion* r = heap_region_containing(old);
ysr@777 4170 if (!r->evacuation_failed()) {
ysr@777 4171 r->set_evacuation_failed(true);
tonyp@1717 4172 if (G1PrintHeapRegions) {
tonyp@2074 4173 gclog_or_tty->print("overflow in heap region "PTR_FORMAT" "
ysr@777 4174 "["PTR_FORMAT","PTR_FORMAT")\n",
ysr@777 4175 r, r->bottom(), r->end());
ysr@777 4176 }
ysr@777 4177 }
ysr@777 4178
ysr@777 4179 push_on_evac_failure_scan_stack(old);
ysr@777 4180
ysr@777 4181 if (!_drain_in_progress) {
ysr@777 4182 // prevent recursion in copy_to_survivor_space()
ysr@777 4183 _drain_in_progress = true;
ysr@777 4184 drain_evac_failure_scan_stack();
ysr@777 4185 _drain_in_progress = false;
ysr@777 4186 }
ysr@777 4187 }
ysr@777 4188
ysr@777 4189 void G1CollectedHeap::preserve_mark_if_necessary(oop obj, markOop m) {
ysr@2380 4190 assert(evacuation_failed(), "Oversaving!");
ysr@2380 4191 // We want to call the "for_promotion_failure" version only in the
ysr@2380 4192 // case of a promotion failure.
ysr@2380 4193 if (m->must_be_preserved_for_promotion_failure(obj)) {
ysr@777 4194 if (_objs_with_preserved_marks == NULL) {
ysr@777 4195 assert(_preserved_marks_of_objs == NULL, "Both or none.");
ysr@777 4196 _objs_with_preserved_marks =
ysr@777 4197 new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true);
ysr@777 4198 _preserved_marks_of_objs =
ysr@777 4199 new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true);
ysr@777 4200 }
ysr@777 4201 _objs_with_preserved_marks->push(obj);
ysr@777 4202 _preserved_marks_of_objs->push(m);
ysr@777 4203 }
ysr@777 4204 }
ysr@777 4205
ysr@777 4206 // *** Parallel G1 Evacuation
ysr@777 4207
ysr@777 4208 HeapWord* G1CollectedHeap::par_allocate_during_gc(GCAllocPurpose purpose,
ysr@777 4209 size_t word_size) {
tonyp@2073 4210 assert(!isHumongous(word_size),
tonyp@2073 4211 err_msg("we should not be seeing humongous allocation requests "
tonyp@2073 4212 "during GC, word_size = "SIZE_FORMAT, word_size));
tonyp@2073 4213
ysr@777 4214 HeapRegion* alloc_region = _gc_alloc_regions[purpose];
ysr@777 4215 // let the caller handle alloc failure
ysr@777 4216 if (alloc_region == NULL) return NULL;
ysr@777 4217
ysr@777 4218 HeapWord* block = alloc_region->par_allocate(word_size);
ysr@777 4219 if (block == NULL) {
ysr@777 4220 block = allocate_during_gc_slow(purpose, alloc_region, true, word_size);
ysr@777 4221 }
ysr@777 4222 return block;
ysr@777 4223 }
ysr@777 4224
apetrusenko@980 4225 void G1CollectedHeap::retire_alloc_region(HeapRegion* alloc_region,
apetrusenko@980 4226 bool par) {
apetrusenko@980 4227 // Another thread might have obtained alloc_region for the given
apetrusenko@980 4228 // purpose, and might be attempting to allocate in it, and might
apetrusenko@980 4229 // succeed. Therefore, we can't do the "finalization" stuff on the
apetrusenko@980 4230 // region below until we're sure the last allocation has happened.
apetrusenko@980 4231 // We ensure this by allocating the remaining space with a garbage
apetrusenko@980 4232 // object.
apetrusenko@980 4233 if (par) par_allocate_remaining_space(alloc_region);
apetrusenko@980 4234 // Now we can do the post-GC stuff on the region.
apetrusenko@980 4235 alloc_region->note_end_of_copying();
apetrusenko@980 4236 g1_policy()->record_after_bytes(alloc_region->used());
apetrusenko@980 4237 }
apetrusenko@980 4238
ysr@777 4239 HeapWord*
ysr@777 4240 G1CollectedHeap::allocate_during_gc_slow(GCAllocPurpose purpose,
ysr@777 4241 HeapRegion* alloc_region,
ysr@777 4242 bool par,
ysr@777 4243 size_t word_size) {
tonyp@2073 4244 assert(!isHumongous(word_size),
tonyp@2073 4245 err_msg("we should not be seeing humongous allocation requests "
tonyp@2073 4246 "during GC, word_size = "SIZE_FORMAT, word_size));
tonyp@2073 4247
tonyp@2472 4248 // We need to make sure we serialize calls to this method. Given
tonyp@2472 4249 // that the FreeList_lock guards accesses to the free_list anyway,
tonyp@2472 4250 // and we need to potentially remove a region from it, we'll use it
tonyp@2472 4251 // to protect the whole call.
tonyp@2472 4252 MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 4253
ysr@777 4254 HeapWord* block = NULL;
ysr@777 4255 // In the parallel case, a previous thread to obtain the lock may have
ysr@777 4256 // already assigned a new gc_alloc_region.
ysr@777 4257 if (alloc_region != _gc_alloc_regions[purpose]) {
ysr@777 4258 assert(par, "But should only happen in parallel case.");
ysr@777 4259 alloc_region = _gc_alloc_regions[purpose];
ysr@777 4260 if (alloc_region == NULL) return NULL;
ysr@777 4261 block = alloc_region->par_allocate(word_size);
ysr@777 4262 if (block != NULL) return block;
ysr@777 4263 // Otherwise, continue; this new region is empty, too.
ysr@777 4264 }
ysr@777 4265 assert(alloc_region != NULL, "We better have an allocation region");
apetrusenko@980 4266 retire_alloc_region(alloc_region, par);
ysr@777 4267
ysr@777 4268 if (_gc_alloc_region_counts[purpose] >= g1_policy()->max_regions(purpose)) {
ysr@777 4269 // Cannot allocate more regions for the given purpose.
ysr@777 4270 GCAllocPurpose alt_purpose = g1_policy()->alternative_purpose(purpose);
ysr@777 4271 // Is there an alternative?
ysr@777 4272 if (purpose != alt_purpose) {
ysr@777 4273 HeapRegion* alt_region = _gc_alloc_regions[alt_purpose];
ysr@777 4274 // Has not the alternative region been aliased?
apetrusenko@980 4275 if (alloc_region != alt_region && alt_region != NULL) {
ysr@777 4276 // Try to allocate in the alternative region.
ysr@777 4277 if (par) {
ysr@777 4278 block = alt_region->par_allocate(word_size);
ysr@777 4279 } else {
ysr@777 4280 block = alt_region->allocate(word_size);
ysr@777 4281 }
ysr@777 4282 // Make an alias.
ysr@777 4283 _gc_alloc_regions[purpose] = _gc_alloc_regions[alt_purpose];
apetrusenko@980 4284 if (block != NULL) {
apetrusenko@980 4285 return block;
apetrusenko@980 4286 }
apetrusenko@980 4287 retire_alloc_region(alt_region, par);
ysr@777 4288 }
ysr@777 4289 // Both the allocation region and the alternative one are full
ysr@777 4290 // and aliased, replace them with a new allocation region.
ysr@777 4291 purpose = alt_purpose;
ysr@777 4292 } else {
ysr@777 4293 set_gc_alloc_region(purpose, NULL);
ysr@777 4294 return NULL;
ysr@777 4295 }
ysr@777 4296 }
ysr@777 4297
ysr@777 4298 // Now allocate a new region for allocation.
tonyp@2472 4299 alloc_region = new_gc_alloc_region(purpose, word_size);
ysr@777 4300
ysr@777 4301 // let the caller handle alloc failure
ysr@777 4302 if (alloc_region != NULL) {
ysr@777 4303
ysr@777 4304 assert(check_gc_alloc_regions(), "alloc regions messed up");
ysr@777 4305 assert(alloc_region->saved_mark_at_top(),
ysr@777 4306 "Mark should have been saved already.");
ysr@777 4307 // This must be done last: once it's installed, other regions may
ysr@777 4308 // allocate in it (without holding the lock.)
ysr@777 4309 set_gc_alloc_region(purpose, alloc_region);
ysr@777 4310
ysr@777 4311 if (par) {
ysr@777 4312 block = alloc_region->par_allocate(word_size);
ysr@777 4313 } else {
ysr@777 4314 block = alloc_region->allocate(word_size);
ysr@777 4315 }
ysr@777 4316 // Caller handles alloc failure.
ysr@777 4317 } else {
ysr@777 4318 // This sets other apis using the same old alloc region to NULL, also.
ysr@777 4319 set_gc_alloc_region(purpose, NULL);
ysr@777 4320 }
ysr@777 4321 return block; // May be NULL.
ysr@777 4322 }
ysr@777 4323
ysr@777 4324 void G1CollectedHeap::par_allocate_remaining_space(HeapRegion* r) {
ysr@777 4325 HeapWord* block = NULL;
ysr@777 4326 size_t free_words;
ysr@777 4327 do {
ysr@777 4328 free_words = r->free()/HeapWordSize;
ysr@777 4329 // If there's too little space, no one can allocate, so we're done.
kvn@1926 4330 if (free_words < CollectedHeap::min_fill_size()) return;
ysr@777 4331 // Otherwise, try to claim it.
ysr@777 4332 block = r->par_allocate(free_words);
ysr@777 4333 } while (block == NULL);
jcoomes@916 4334 fill_with_object(block, free_words);
ysr@777 4335 }
ysr@777 4336
ysr@777 4337 #ifndef PRODUCT
ysr@777 4338 bool GCLabBitMapClosure::do_bit(size_t offset) {
ysr@777 4339 HeapWord* addr = _bitmap->offsetToHeapWord(offset);
ysr@777 4340 guarantee(_cm->isMarked(oop(addr)), "it should be!");
ysr@777 4341 return true;
ysr@777 4342 }
ysr@777 4343 #endif // PRODUCT
ysr@777 4344
ysr@1280 4345 G1ParScanThreadState::G1ParScanThreadState(G1CollectedHeap* g1h, int queue_num)
ysr@1280 4346 : _g1h(g1h),
ysr@1280 4347 _refs(g1h->task_queue(queue_num)),
ysr@1280 4348 _dcq(&g1h->dirty_card_queue_set()),
ysr@1280 4349 _ct_bs((CardTableModRefBS*)_g1h->barrier_set()),
ysr@1280 4350 _g1_rem(g1h->g1_rem_set()),
ysr@1280 4351 _hash_seed(17), _queue_num(queue_num),
ysr@1280 4352 _term_attempts(0),
apetrusenko@1826 4353 _surviving_alloc_buffer(g1h->desired_plab_sz(GCAllocForSurvived)),
apetrusenko@1826 4354 _tenured_alloc_buffer(g1h->desired_plab_sz(GCAllocForTenured)),
ysr@1280 4355 _age_table(false),
ysr@1280 4356 _strong_roots_time(0), _term_time(0),
ysr@1280 4357 _alloc_buffer_waste(0), _undo_waste(0)
ysr@1280 4358 {
ysr@1280 4359 // we allocate G1YoungSurvRateNumRegions plus one entries, since
ysr@1280 4360 // we "sacrifice" entry 0 to keep track of surviving bytes for
ysr@1280 4361 // non-young regions (where the age is -1)
ysr@1280 4362 // We also add a few elements at the beginning and at the end in
ysr@1280 4363 // an attempt to eliminate cache contention
ysr@1280 4364 size_t real_length = 1 + _g1h->g1_policy()->young_cset_length();
ysr@1280 4365 size_t array_length = PADDING_ELEM_NUM +
ysr@1280 4366 real_length +
ysr@1280 4367 PADDING_ELEM_NUM;
ysr@1280 4368 _surviving_young_words_base = NEW_C_HEAP_ARRAY(size_t, array_length);
ysr@1280 4369 if (_surviving_young_words_base == NULL)
ysr@1280 4370 vm_exit_out_of_memory(array_length * sizeof(size_t),
ysr@1280 4371 "Not enough space for young surv histo.");
ysr@1280 4372 _surviving_young_words = _surviving_young_words_base + PADDING_ELEM_NUM;
ysr@1280 4373 memset(_surviving_young_words, 0, real_length * sizeof(size_t));
ysr@1280 4374
apetrusenko@1826 4375 _alloc_buffers[GCAllocForSurvived] = &_surviving_alloc_buffer;
apetrusenko@1826 4376 _alloc_buffers[GCAllocForTenured] = &_tenured_alloc_buffer;
apetrusenko@1826 4377
ysr@1280 4378 _start = os::elapsedTime();
ysr@1280 4379 }
ysr@777 4380
jcoomes@2064 4381 void
jcoomes@2064 4382 G1ParScanThreadState::print_termination_stats_hdr(outputStream* const st)
jcoomes@2064 4383 {
jcoomes@2064 4384 st->print_raw_cr("GC Termination Stats");
jcoomes@2064 4385 st->print_raw_cr(" elapsed --strong roots-- -------termination-------"
jcoomes@2064 4386 " ------waste (KiB)------");
jcoomes@2064 4387 st->print_raw_cr("thr ms ms % ms % attempts"
jcoomes@2064 4388 " total alloc undo");
jcoomes@2064 4389 st->print_raw_cr("--- --------- --------- ------ --------- ------ --------"
jcoomes@2064 4390 " ------- ------- -------");
jcoomes@2064 4391 }
jcoomes@2064 4392
jcoomes@2064 4393 void
jcoomes@2064 4394 G1ParScanThreadState::print_termination_stats(int i,
jcoomes@2064 4395 outputStream* const st) const
jcoomes@2064 4396 {
jcoomes@2064 4397 const double elapsed_ms = elapsed_time() * 1000.0;
jcoomes@2064 4398 const double s_roots_ms = strong_roots_time() * 1000.0;
jcoomes@2064 4399 const double term_ms = term_time() * 1000.0;
jcoomes@2064 4400 st->print_cr("%3d %9.2f %9.2f %6.2f "
jcoomes@2064 4401 "%9.2f %6.2f " SIZE_FORMAT_W(8) " "
jcoomes@2064 4402 SIZE_FORMAT_W(7) " " SIZE_FORMAT_W(7) " " SIZE_FORMAT_W(7),
jcoomes@2064 4403 i, elapsed_ms, s_roots_ms, s_roots_ms * 100 / elapsed_ms,
jcoomes@2064 4404 term_ms, term_ms * 100 / elapsed_ms, term_attempts(),
jcoomes@2064 4405 (alloc_buffer_waste() + undo_waste()) * HeapWordSize / K,
jcoomes@2064 4406 alloc_buffer_waste() * HeapWordSize / K,
jcoomes@2064 4407 undo_waste() * HeapWordSize / K);
jcoomes@2064 4408 }
jcoomes@2064 4409
jcoomes@2217 4410 #ifdef ASSERT
jcoomes@2217 4411 bool G1ParScanThreadState::verify_ref(narrowOop* ref) const {
jcoomes@2217 4412 assert(ref != NULL, "invariant");
jcoomes@2217 4413 assert(UseCompressedOops, "sanity");
jcoomes@2217 4414 assert(!has_partial_array_mask(ref), err_msg("ref=" PTR_FORMAT, ref));
jcoomes@2217 4415 oop p = oopDesc::load_decode_heap_oop(ref);
jcoomes@2217 4416 assert(_g1h->is_in_g1_reserved(p),
jcoomes@2217 4417 err_msg("ref=" PTR_FORMAT " p=" PTR_FORMAT, ref, intptr_t(p)));
jcoomes@2217 4418 return true;
jcoomes@2217 4419 }
jcoomes@2217 4420
jcoomes@2217 4421 bool G1ParScanThreadState::verify_ref(oop* ref) const {
jcoomes@2217 4422 assert(ref != NULL, "invariant");
jcoomes@2217 4423 if (has_partial_array_mask(ref)) {
jcoomes@2217 4424 // Must be in the collection set--it's already been copied.
jcoomes@2217 4425 oop p = clear_partial_array_mask(ref);
jcoomes@2217 4426 assert(_g1h->obj_in_cs(p),
jcoomes@2217 4427 err_msg("ref=" PTR_FORMAT " p=" PTR_FORMAT, ref, intptr_t(p)));
jcoomes@2217 4428 } else {
jcoomes@2217 4429 oop p = oopDesc::load_decode_heap_oop(ref);
jcoomes@2217 4430 assert(_g1h->is_in_g1_reserved(p),
jcoomes@2217 4431 err_msg("ref=" PTR_FORMAT " p=" PTR_FORMAT, ref, intptr_t(p)));
jcoomes@2217 4432 }
jcoomes@2217 4433 return true;
jcoomes@2217 4434 }
jcoomes@2217 4435
jcoomes@2217 4436 bool G1ParScanThreadState::verify_task(StarTask ref) const {
jcoomes@2217 4437 if (ref.is_narrow()) {
jcoomes@2217 4438 return verify_ref((narrowOop*) ref);
jcoomes@2217 4439 } else {
jcoomes@2217 4440 return verify_ref((oop*) ref);
jcoomes@2217 4441 }
jcoomes@2217 4442 }
jcoomes@2217 4443 #endif // ASSERT
jcoomes@2217 4444
jcoomes@2217 4445 void G1ParScanThreadState::trim_queue() {
jcoomes@2217 4446 StarTask ref;
jcoomes@2217 4447 do {
jcoomes@2217 4448 // Drain the overflow stack first, so other threads can steal.
jcoomes@2217 4449 while (refs()->pop_overflow(ref)) {
jcoomes@2217 4450 deal_with_reference(ref);
jcoomes@2217 4451 }
jcoomes@2217 4452 while (refs()->pop_local(ref)) {
jcoomes@2217 4453 deal_with_reference(ref);
jcoomes@2217 4454 }
jcoomes@2217 4455 } while (!refs()->is_empty());
jcoomes@2217 4456 }
jcoomes@2217 4457
ysr@777 4458 G1ParClosureSuper::G1ParClosureSuper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
ysr@777 4459 _g1(g1), _g1_rem(_g1->g1_rem_set()), _cm(_g1->concurrent_mark()),
ysr@777 4460 _par_scan_state(par_scan_state) { }
ysr@777 4461
ysr@1280 4462 template <class T> void G1ParCopyHelper::mark_forwardee(T* p) {
ysr@777 4463 // This is called _after_ do_oop_work has been called, hence after
ysr@777 4464 // the object has been relocated to its new location and *p points
ysr@777 4465 // to its new location.
ysr@777 4466
ysr@1280 4467 T heap_oop = oopDesc::load_heap_oop(p);
ysr@1280 4468 if (!oopDesc::is_null(heap_oop)) {
ysr@1280 4469 oop obj = oopDesc::decode_heap_oop(heap_oop);
ysr@1280 4470 assert((_g1->evacuation_failed()) || (!_g1->obj_in_cs(obj)),
ysr@777 4471 "shouldn't still be in the CSet if evacuation didn't fail.");
ysr@1280 4472 HeapWord* addr = (HeapWord*)obj;
ysr@777 4473 if (_g1->is_in_g1_reserved(addr))
ysr@777 4474 _cm->grayRoot(oop(addr));
ysr@777 4475 }
ysr@777 4476 }
ysr@777 4477
ysr@777 4478 oop G1ParCopyHelper::copy_to_survivor_space(oop old) {
ysr@777 4479 size_t word_sz = old->size();
ysr@777 4480 HeapRegion* from_region = _g1->heap_region_containing_raw(old);
ysr@777 4481 // +1 to make the -1 indexes valid...
ysr@777 4482 int young_index = from_region->young_index_in_cset()+1;
ysr@777 4483 assert( (from_region->is_young() && young_index > 0) ||
ysr@777 4484 (!from_region->is_young() && young_index == 0), "invariant" );
ysr@777 4485 G1CollectorPolicy* g1p = _g1->g1_policy();
ysr@777 4486 markOop m = old->mark();
apetrusenko@980 4487 int age = m->has_displaced_mark_helper() ? m->displaced_mark_helper()->age()
apetrusenko@980 4488 : m->age();
apetrusenko@980 4489 GCAllocPurpose alloc_purpose = g1p->evacuation_destination(from_region, age,
ysr@777 4490 word_sz);
ysr@777 4491 HeapWord* obj_ptr = _par_scan_state->allocate(alloc_purpose, word_sz);
ysr@777 4492 oop obj = oop(obj_ptr);
ysr@777 4493
ysr@777 4494 if (obj_ptr == NULL) {
ysr@777 4495 // This will either forward-to-self, or detect that someone else has
ysr@777 4496 // installed a forwarding pointer.
ysr@777 4497 OopsInHeapRegionClosure* cl = _par_scan_state->evac_failure_closure();
ysr@777 4498 return _g1->handle_evacuation_failure_par(cl, old);
ysr@777 4499 }
ysr@777 4500
tonyp@961 4501 // We're going to allocate linearly, so might as well prefetch ahead.
tonyp@961 4502 Prefetch::write(obj_ptr, PrefetchCopyIntervalInBytes);
tonyp@961 4503
ysr@777 4504 oop forward_ptr = old->forward_to_atomic(obj);
ysr@777 4505 if (forward_ptr == NULL) {
ysr@777 4506 Copy::aligned_disjoint_words((HeapWord*) old, obj_ptr, word_sz);
tonyp@961 4507 if (g1p->track_object_age(alloc_purpose)) {
tonyp@961 4508 // We could simply do obj->incr_age(). However, this causes a
tonyp@961 4509 // performance issue. obj->incr_age() will first check whether
tonyp@961 4510 // the object has a displaced mark by checking its mark word;
tonyp@961 4511 // getting the mark word from the new location of the object
tonyp@961 4512 // stalls. So, given that we already have the mark word and we
tonyp@961 4513 // are about to install it anyway, it's better to increase the
tonyp@961 4514 // age on the mark word, when the object does not have a
tonyp@961 4515 // displaced mark word. We're not expecting many objects to have
tonyp@961 4516 // a displaced marked word, so that case is not optimized
tonyp@961 4517 // further (it could be...) and we simply call obj->incr_age().
tonyp@961 4518
tonyp@961 4519 if (m->has_displaced_mark_helper()) {
tonyp@961 4520 // in this case, we have to install the mark word first,
tonyp@961 4521 // otherwise obj looks to be forwarded (the old mark word,
tonyp@961 4522 // which contains the forward pointer, was copied)
tonyp@961 4523 obj->set_mark(m);
tonyp@961 4524 obj->incr_age();
tonyp@961 4525 } else {
tonyp@961 4526 m = m->incr_age();
apetrusenko@980 4527 obj->set_mark(m);
tonyp@961 4528 }
apetrusenko@980 4529 _par_scan_state->age_table()->add(obj, word_sz);
apetrusenko@980 4530 } else {
apetrusenko@980 4531 obj->set_mark(m);
tonyp@961 4532 }
tonyp@961 4533
ysr@777 4534 // preserve "next" mark bit
ysr@777 4535 if (_g1->mark_in_progress() && !_g1->is_obj_ill(old)) {
ysr@777 4536 if (!use_local_bitmaps ||
ysr@777 4537 !_par_scan_state->alloc_buffer(alloc_purpose)->mark(obj_ptr)) {
ysr@777 4538 // if we couldn't mark it on the local bitmap (this happens when
ysr@777 4539 // the object was not allocated in the GCLab), we have to bite
ysr@777 4540 // the bullet and do the standard parallel mark
ysr@777 4541 _cm->markAndGrayObjectIfNecessary(obj);
ysr@777 4542 }
ysr@777 4543 #if 1
ysr@777 4544 if (_g1->isMarkedNext(old)) {
ysr@777 4545 _cm->nextMarkBitMap()->parClear((HeapWord*)old);
ysr@777 4546 }
ysr@777 4547 #endif
ysr@777 4548 }
ysr@777 4549
ysr@777 4550 size_t* surv_young_words = _par_scan_state->surviving_young_words();
ysr@777 4551 surv_young_words[young_index] += word_sz;
ysr@777 4552
ysr@777 4553 if (obj->is_objArray() && arrayOop(obj)->length() >= ParGCArrayScanChunk) {
ysr@777 4554 arrayOop(old)->set_length(0);
ysr@1280 4555 oop* old_p = set_partial_array_mask(old);
ysr@1280 4556 _par_scan_state->push_on_queue(old_p);
ysr@777 4557 } else {
tonyp@961 4558 // No point in using the slower heap_region_containing() method,
tonyp@961 4559 // given that we know obj is in the heap.
tonyp@961 4560 _scanner->set_region(_g1->heap_region_containing_raw(obj));
ysr@777 4561 obj->oop_iterate_backwards(_scanner);
ysr@777 4562 }
ysr@777 4563 } else {
ysr@777 4564 _par_scan_state->undo_allocation(alloc_purpose, obj_ptr, word_sz);
ysr@777 4565 obj = forward_ptr;
ysr@777 4566 }
ysr@777 4567 return obj;
ysr@777 4568 }
ysr@777 4569
iveresov@1696 4570 template <bool do_gen_barrier, G1Barrier barrier, bool do_mark_forwardee>
ysr@1280 4571 template <class T>
iveresov@1696 4572 void G1ParCopyClosure <do_gen_barrier, barrier, do_mark_forwardee>
ysr@1280 4573 ::do_oop_work(T* p) {
ysr@1280 4574 oop obj = oopDesc::load_decode_heap_oop(p);
ysr@777 4575 assert(barrier != G1BarrierRS || obj != NULL,
ysr@777 4576 "Precondition: G1BarrierRS implies obj is nonNull");
ysr@777 4577
tonyp@961 4578 // here the null check is implicit in the cset_fast_test() test
iveresov@1696 4579 if (_g1->in_cset_fast_test(obj)) {
ysr@777 4580 #if G1_REM_SET_LOGGING
tonyp@961 4581 gclog_or_tty->print_cr("Loc "PTR_FORMAT" contains pointer "PTR_FORMAT" "
tonyp@961 4582 "into CS.", p, (void*) obj);
ysr@777 4583 #endif
tonyp@961 4584 if (obj->is_forwarded()) {
ysr@1280 4585 oopDesc::encode_store_heap_oop(p, obj->forwardee());
tonyp@961 4586 } else {
ysr@1280 4587 oop copy_oop = copy_to_survivor_space(obj);
ysr@1280 4588 oopDesc::encode_store_heap_oop(p, copy_oop);
ysr@777 4589 }
tonyp@961 4590 // When scanning the RS, we only care about objs in CS.
tonyp@961 4591 if (barrier == G1BarrierRS) {
iveresov@1051 4592 _par_scan_state->update_rs(_from, p, _par_scan_state->queue_num());
ysr@777 4593 }
tonyp@961 4594 }
tonyp@961 4595
tonyp@961 4596 if (barrier == G1BarrierEvac && obj != NULL) {
iveresov@1051 4597 _par_scan_state->update_rs(_from, p, _par_scan_state->queue_num());
tonyp@961 4598 }
tonyp@961 4599
tonyp@961 4600 if (do_gen_barrier && obj != NULL) {
tonyp@961 4601 par_do_barrier(p);
tonyp@961 4602 }
tonyp@961 4603 }
tonyp@961 4604
iveresov@1696 4605 template void G1ParCopyClosure<false, G1BarrierEvac, false>::do_oop_work(oop* p);
iveresov@1696 4606 template void G1ParCopyClosure<false, G1BarrierEvac, false>::do_oop_work(narrowOop* p);
ysr@1280 4607
ysr@1280 4608 template <class T> void G1ParScanPartialArrayClosure::do_oop_nv(T* p) {
tonyp@961 4609 assert(has_partial_array_mask(p), "invariant");
tonyp@961 4610 oop old = clear_partial_array_mask(p);
ysr@777 4611 assert(old->is_objArray(), "must be obj array");
ysr@777 4612 assert(old->is_forwarded(), "must be forwarded");
ysr@777 4613 assert(Universe::heap()->is_in_reserved(old), "must be in heap.");
ysr@777 4614
ysr@777 4615 objArrayOop obj = objArrayOop(old->forwardee());
ysr@777 4616 assert((void*)old != (void*)old->forwardee(), "self forwarding here?");
ysr@777 4617 // Process ParGCArrayScanChunk elements now
ysr@777 4618 // and push the remainder back onto queue
ysr@777 4619 int start = arrayOop(old)->length();
ysr@777 4620 int end = obj->length();
ysr@777 4621 int remainder = end - start;
ysr@777 4622 assert(start <= end, "just checking");
ysr@777 4623 if (remainder > 2 * ParGCArrayScanChunk) {
ysr@777 4624 // Test above combines last partial chunk with a full chunk
ysr@777 4625 end = start + ParGCArrayScanChunk;
ysr@777 4626 arrayOop(old)->set_length(end);
ysr@777 4627 // Push remainder.
ysr@1280 4628 oop* old_p = set_partial_array_mask(old);
ysr@1280 4629 assert(arrayOop(old)->length() < obj->length(), "Empty push?");
ysr@1280 4630 _par_scan_state->push_on_queue(old_p);
ysr@777 4631 } else {
ysr@777 4632 // Restore length so that the heap remains parsable in
ysr@777 4633 // case of evacuation failure.
ysr@777 4634 arrayOop(old)->set_length(end);
ysr@777 4635 }
ysr@1280 4636 _scanner.set_region(_g1->heap_region_containing_raw(obj));
ysr@777 4637 // process our set of indices (include header in first chunk)
ysr@1280 4638 obj->oop_iterate_range(&_scanner, start, end);
ysr@777 4639 }
ysr@777 4640
ysr@777 4641 class G1ParEvacuateFollowersClosure : public VoidClosure {
ysr@777 4642 protected:
ysr@777 4643 G1CollectedHeap* _g1h;
ysr@777 4644 G1ParScanThreadState* _par_scan_state;
ysr@777 4645 RefToScanQueueSet* _queues;
ysr@777 4646 ParallelTaskTerminator* _terminator;
ysr@777 4647
ysr@777 4648 G1ParScanThreadState* par_scan_state() { return _par_scan_state; }
ysr@777 4649 RefToScanQueueSet* queues() { return _queues; }
ysr@777 4650 ParallelTaskTerminator* terminator() { return _terminator; }
ysr@777 4651
ysr@777 4652 public:
ysr@777 4653 G1ParEvacuateFollowersClosure(G1CollectedHeap* g1h,
ysr@777 4654 G1ParScanThreadState* par_scan_state,
ysr@777 4655 RefToScanQueueSet* queues,
ysr@777 4656 ParallelTaskTerminator* terminator)
ysr@777 4657 : _g1h(g1h), _par_scan_state(par_scan_state),
ysr@777 4658 _queues(queues), _terminator(terminator) {}
ysr@777 4659
jcoomes@2217 4660 void do_void();
jcoomes@2217 4661
jcoomes@2217 4662 private:
jcoomes@2217 4663 inline bool offer_termination();
jcoomes@2217 4664 };
jcoomes@2217 4665
jcoomes@2217 4666 bool G1ParEvacuateFollowersClosure::offer_termination() {
jcoomes@2217 4667 G1ParScanThreadState* const pss = par_scan_state();
jcoomes@2217 4668 pss->start_term_time();
jcoomes@2217 4669 const bool res = terminator()->offer_termination();
jcoomes@2217 4670 pss->end_term_time();
jcoomes@2217 4671 return res;
jcoomes@2217 4672 }
jcoomes@2217 4673
jcoomes@2217 4674 void G1ParEvacuateFollowersClosure::do_void() {
jcoomes@2217 4675 StarTask stolen_task;
jcoomes@2217 4676 G1ParScanThreadState* const pss = par_scan_state();
jcoomes@2217 4677 pss->trim_queue();
jcoomes@2217 4678
jcoomes@2217 4679 do {
jcoomes@2217 4680 while (queues()->steal(pss->queue_num(), pss->hash_seed(), stolen_task)) {
jcoomes@2217 4681 assert(pss->verify_task(stolen_task), "sanity");
jcoomes@2217 4682 if (stolen_task.is_narrow()) {
tonyp@2238 4683 pss->deal_with_reference((narrowOop*) stolen_task);
jcoomes@2217 4684 } else {
tonyp@2238 4685 pss->deal_with_reference((oop*) stolen_task);
jcoomes@2217 4686 }
tonyp@2238 4687
tonyp@2238 4688 // We've just processed a reference and we might have made
tonyp@2238 4689 // available new entries on the queues. So we have to make sure
tonyp@2238 4690 // we drain the queues as necessary.
ysr@777 4691 pss->trim_queue();
ysr@777 4692 }
jcoomes@2217 4693 } while (!offer_termination());
jcoomes@2217 4694
jcoomes@2217 4695 pss->retire_alloc_buffers();
jcoomes@2217 4696 }
ysr@777 4697
ysr@777 4698 class G1ParTask : public AbstractGangTask {
ysr@777 4699 protected:
ysr@777 4700 G1CollectedHeap* _g1h;
ysr@777 4701 RefToScanQueueSet *_queues;
ysr@777 4702 ParallelTaskTerminator _terminator;
ysr@1280 4703 int _n_workers;
ysr@777 4704
ysr@777 4705 Mutex _stats_lock;
ysr@777 4706 Mutex* stats_lock() { return &_stats_lock; }
ysr@777 4707
ysr@777 4708 size_t getNCards() {
ysr@777 4709 return (_g1h->capacity() + G1BlockOffsetSharedArray::N_bytes - 1)
ysr@777 4710 / G1BlockOffsetSharedArray::N_bytes;
ysr@777 4711 }
ysr@777 4712
ysr@777 4713 public:
ysr@777 4714 G1ParTask(G1CollectedHeap* g1h, int workers, RefToScanQueueSet *task_queues)
ysr@777 4715 : AbstractGangTask("G1 collection"),
ysr@777 4716 _g1h(g1h),
ysr@777 4717 _queues(task_queues),
ysr@777 4718 _terminator(workers, _queues),
ysr@1280 4719 _stats_lock(Mutex::leaf, "parallel G1 stats lock", true),
ysr@1280 4720 _n_workers(workers)
ysr@777 4721 {}
ysr@777 4722
ysr@777 4723 RefToScanQueueSet* queues() { return _queues; }
ysr@777 4724
ysr@777 4725 RefToScanQueue *work_queue(int i) {
ysr@777 4726 return queues()->queue(i);
ysr@777 4727 }
ysr@777 4728
ysr@777 4729 void work(int i) {
ysr@1280 4730 if (i >= _n_workers) return; // no work needed this round
tonyp@1966 4731
tonyp@1966 4732 double start_time_ms = os::elapsedTime() * 1000.0;
tonyp@1966 4733 _g1h->g1_policy()->record_gc_worker_start_time(i, start_time_ms);
tonyp@1966 4734
ysr@777 4735 ResourceMark rm;
ysr@777 4736 HandleMark hm;
ysr@777 4737
tonyp@961 4738 G1ParScanThreadState pss(_g1h, i);
tonyp@961 4739 G1ParScanHeapEvacClosure scan_evac_cl(_g1h, &pss);
tonyp@961 4740 G1ParScanHeapEvacFailureClosure evac_failure_cl(_g1h, &pss);
tonyp@961 4741 G1ParScanPartialArrayClosure partial_scan_cl(_g1h, &pss);
ysr@777 4742
ysr@777 4743 pss.set_evac_closure(&scan_evac_cl);
ysr@777 4744 pss.set_evac_failure_closure(&evac_failure_cl);
ysr@777 4745 pss.set_partial_scan_closure(&partial_scan_cl);
ysr@777 4746
ysr@777 4747 G1ParScanExtRootClosure only_scan_root_cl(_g1h, &pss);
ysr@777 4748 G1ParScanPermClosure only_scan_perm_cl(_g1h, &pss);
ysr@777 4749 G1ParScanHeapRSClosure only_scan_heap_rs_cl(_g1h, &pss);
iveresov@1696 4750 G1ParPushHeapRSClosure push_heap_rs_cl(_g1h, &pss);
iveresov@1051 4751
ysr@777 4752 G1ParScanAndMarkExtRootClosure scan_mark_root_cl(_g1h, &pss);
ysr@777 4753 G1ParScanAndMarkPermClosure scan_mark_perm_cl(_g1h, &pss);
ysr@777 4754 G1ParScanAndMarkHeapRSClosure scan_mark_heap_rs_cl(_g1h, &pss);
ysr@777 4755
ysr@777 4756 OopsInHeapRegionClosure *scan_root_cl;
ysr@777 4757 OopsInHeapRegionClosure *scan_perm_cl;
ysr@777 4758
tonyp@1794 4759 if (_g1h->g1_policy()->during_initial_mark_pause()) {
ysr@777 4760 scan_root_cl = &scan_mark_root_cl;
ysr@777 4761 scan_perm_cl = &scan_mark_perm_cl;
ysr@777 4762 } else {
ysr@777 4763 scan_root_cl = &only_scan_root_cl;
ysr@777 4764 scan_perm_cl = &only_scan_perm_cl;
ysr@777 4765 }
ysr@777 4766
ysr@777 4767 pss.start_strong_roots();
ysr@777 4768 _g1h->g1_process_strong_roots(/* not collecting perm */ false,
ysr@777 4769 SharedHeap::SO_AllClasses,
ysr@777 4770 scan_root_cl,
iveresov@1696 4771 &push_heap_rs_cl,
ysr@777 4772 scan_perm_cl,
ysr@777 4773 i);
ysr@777 4774 pss.end_strong_roots();
ysr@777 4775 {
ysr@777 4776 double start = os::elapsedTime();
ysr@777 4777 G1ParEvacuateFollowersClosure evac(_g1h, &pss, _queues, &_terminator);
ysr@777 4778 evac.do_void();
ysr@777 4779 double elapsed_ms = (os::elapsedTime()-start)*1000.0;
ysr@777 4780 double term_ms = pss.term_time()*1000.0;
ysr@777 4781 _g1h->g1_policy()->record_obj_copy_time(i, elapsed_ms-term_ms);
tonyp@1966 4782 _g1h->g1_policy()->record_termination(i, term_ms, pss.term_attempts());
ysr@777 4783 }
tonyp@1717 4784 _g1h->g1_policy()->record_thread_age_table(pss.age_table());
ysr@777 4785 _g1h->update_surviving_young_words(pss.surviving_young_words()+1);
ysr@777 4786
ysr@777 4787 // Clean up any par-expanded rem sets.
ysr@777 4788 HeapRegionRemSet::par_cleanup();
ysr@777 4789
ysr@777 4790 if (ParallelGCVerbose) {
jcoomes@2064 4791 MutexLocker x(stats_lock());
jcoomes@2064 4792 pss.print_termination_stats(i);
ysr@777 4793 }
ysr@777 4794
jcoomes@2217 4795 assert(pss.refs()->is_empty(), "should be empty");
tonyp@1966 4796 double end_time_ms = os::elapsedTime() * 1000.0;
tonyp@1966 4797 _g1h->g1_policy()->record_gc_worker_end_time(i, end_time_ms);
ysr@777 4798 }
ysr@777 4799 };
ysr@777 4800
ysr@777 4801 // *** Common G1 Evacuation Stuff
ysr@777 4802
jmasa@2188 4803 // This method is run in a GC worker.
jmasa@2188 4804
ysr@777 4805 void
ysr@777 4806 G1CollectedHeap::
ysr@777 4807 g1_process_strong_roots(bool collecting_perm_gen,
ysr@777 4808 SharedHeap::ScanningOption so,
ysr@777 4809 OopClosure* scan_non_heap_roots,
ysr@777 4810 OopsInHeapRegionClosure* scan_rs,
ysr@777 4811 OopsInGenClosure* scan_perm,
ysr@777 4812 int worker_i) {
ysr@777 4813 // First scan the strong roots, including the perm gen.
ysr@777 4814 double ext_roots_start = os::elapsedTime();
ysr@777 4815 double closure_app_time_sec = 0.0;
ysr@777 4816
ysr@777 4817 BufferingOopClosure buf_scan_non_heap_roots(scan_non_heap_roots);
ysr@777 4818 BufferingOopsInGenClosure buf_scan_perm(scan_perm);
ysr@777 4819 buf_scan_perm.set_generation(perm_gen());
ysr@777 4820
jrose@1424 4821 // Walk the code cache w/o buffering, because StarTask cannot handle
jrose@1424 4822 // unaligned oop locations.
jrose@1424 4823 CodeBlobToOopClosure eager_scan_code_roots(scan_non_heap_roots, /*do_marking=*/ true);
jrose@1424 4824
jrose@1424 4825 process_strong_roots(false, // no scoping; this is parallel code
jrose@1424 4826 collecting_perm_gen, so,
ysr@777 4827 &buf_scan_non_heap_roots,
jrose@1424 4828 &eager_scan_code_roots,
ysr@777 4829 &buf_scan_perm);
johnc@1829 4830
ysr@777 4831 // Finish up any enqueued closure apps.
ysr@777 4832 buf_scan_non_heap_roots.done();
ysr@777 4833 buf_scan_perm.done();
ysr@777 4834 double ext_roots_end = os::elapsedTime();
ysr@777 4835 g1_policy()->reset_obj_copy_time(worker_i);
ysr@777 4836 double obj_copy_time_sec =
ysr@777 4837 buf_scan_non_heap_roots.closure_app_seconds() +
ysr@777 4838 buf_scan_perm.closure_app_seconds();
ysr@777 4839 g1_policy()->record_obj_copy_time(worker_i, obj_copy_time_sec * 1000.0);
ysr@777 4840 double ext_root_time_ms =
ysr@777 4841 ((ext_roots_end - ext_roots_start) - obj_copy_time_sec) * 1000.0;
ysr@777 4842 g1_policy()->record_ext_root_scan_time(worker_i, ext_root_time_ms);
ysr@777 4843
ysr@777 4844 // Scan strong roots in mark stack.
ysr@777 4845 if (!_process_strong_tasks->is_task_claimed(G1H_PS_mark_stack_oops_do)) {
ysr@777 4846 concurrent_mark()->oops_do(scan_non_heap_roots);
ysr@777 4847 }
ysr@777 4848 double mark_stack_scan_ms = (os::elapsedTime() - ext_roots_end) * 1000.0;
ysr@777 4849 g1_policy()->record_mark_stack_scan_time(worker_i, mark_stack_scan_ms);
ysr@777 4850
ysr@777 4851 // XXX What should this be doing in the parallel case?
ysr@777 4852 g1_policy()->record_collection_pause_end_CH_strong_roots();
ysr@777 4853 // Now scan the complement of the collection set.
ysr@777 4854 if (scan_rs != NULL) {
ysr@777 4855 g1_rem_set()->oops_into_collection_set_do(scan_rs, worker_i);
ysr@777 4856 }
ysr@777 4857 // Finish with the ref_processor roots.
ysr@777 4858 if (!_process_strong_tasks->is_task_claimed(G1H_PS_refProcessor_oops_do)) {
johnc@2316 4859 // We need to treat the discovered reference lists as roots and
johnc@2316 4860 // keep entries (which are added by the marking threads) on them
johnc@2316 4861 // live until they can be processed at the end of marking.
johnc@2316 4862 ref_processor()->weak_oops_do(scan_non_heap_roots);
ysr@777 4863 ref_processor()->oops_do(scan_non_heap_roots);
ysr@777 4864 }
ysr@777 4865 g1_policy()->record_collection_pause_end_G1_strong_roots();
ysr@777 4866 _process_strong_tasks->all_tasks_completed();
ysr@777 4867 }
ysr@777 4868
ysr@777 4869 void
ysr@777 4870 G1CollectedHeap::g1_process_weak_roots(OopClosure* root_closure,
ysr@777 4871 OopClosure* non_root_closure) {
jrose@1424 4872 CodeBlobToOopClosure roots_in_blobs(root_closure, /*do_marking=*/ false);
jrose@1424 4873 SharedHeap::process_weak_roots(root_closure, &roots_in_blobs, non_root_closure);
ysr@777 4874 }
ysr@777 4875
ysr@777 4876
ysr@777 4877 class SaveMarksClosure: public HeapRegionClosure {
ysr@777 4878 public:
ysr@777 4879 bool doHeapRegion(HeapRegion* r) {
ysr@777 4880 r->save_marks();
ysr@777 4881 return false;
ysr@777 4882 }
ysr@777 4883 };
ysr@777 4884
ysr@777 4885 void G1CollectedHeap::save_marks() {
jmasa@2188 4886 if (!CollectedHeap::use_parallel_gc_threads()) {
ysr@777 4887 SaveMarksClosure sm;
ysr@777 4888 heap_region_iterate(&sm);
ysr@777 4889 }
ysr@777 4890 // We do this even in the parallel case
ysr@777 4891 perm_gen()->save_marks();
ysr@777 4892 }
ysr@777 4893
ysr@777 4894 void G1CollectedHeap::evacuate_collection_set() {
ysr@777 4895 set_evacuation_failed(false);
ysr@777 4896
ysr@777 4897 g1_rem_set()->prepare_for_oops_into_collection_set_do();
ysr@777 4898 concurrent_g1_refine()->set_use_cache(false);
johnc@1324 4899 concurrent_g1_refine()->clear_hot_cache_claimed_index();
johnc@1324 4900
ysr@777 4901 int n_workers = (ParallelGCThreads > 0 ? workers()->total_workers() : 1);
ysr@777 4902 set_par_threads(n_workers);
ysr@777 4903 G1ParTask g1_par_task(this, n_workers, _task_queues);
ysr@777 4904
ysr@777 4905 init_for_evac_failure(NULL);
ysr@777 4906
ysr@777 4907 rem_set()->prepare_for_younger_refs_iterate(true);
iveresov@1051 4908
iveresov@1051 4909 assert(dirty_card_queue_set().completed_buffers_num() == 0, "Should be empty");
ysr@777 4910 double start_par = os::elapsedTime();
jmasa@2188 4911 if (G1CollectedHeap::use_parallel_gc_threads()) {
ysr@777 4912 // The individual threads will set their evac-failure closures.
jrose@1424 4913 StrongRootsScope srs(this);
jcoomes@2064 4914 if (ParallelGCVerbose) G1ParScanThreadState::print_termination_stats_hdr();
ysr@777 4915 workers()->run_task(&g1_par_task);
ysr@777 4916 } else {
jrose@1424 4917 StrongRootsScope srs(this);
ysr@777 4918 g1_par_task.work(0);
ysr@777 4919 }
ysr@777 4920
ysr@777 4921 double par_time = (os::elapsedTime() - start_par) * 1000.0;
ysr@777 4922 g1_policy()->record_par_time(par_time);
ysr@777 4923 set_par_threads(0);
ysr@777 4924 // Is this the right thing to do here? We don't save marks
ysr@777 4925 // on individual heap regions when we allocate from
ysr@777 4926 // them in parallel, so this seems like the correct place for this.
apetrusenko@980 4927 retire_all_alloc_regions();
johnc@2316 4928
johnc@2316 4929 // Weak root processing.
johnc@2316 4930 // Note: when JSR 292 is enabled and code blobs can contain
johnc@2316 4931 // non-perm oops then we will need to process the code blobs
johnc@2316 4932 // here too.
ysr@777 4933 {
ysr@777 4934 G1IsAliveClosure is_alive(this);
ysr@777 4935 G1KeepAliveClosure keep_alive(this);
ysr@777 4936 JNIHandles::weak_oops_do(&is_alive, &keep_alive);
ysr@777 4937 }
apetrusenko@1375 4938 release_gc_alloc_regions(false /* totally */);
ysr@777 4939 g1_rem_set()->cleanup_after_oops_into_collection_set_do();
iveresov@1051 4940
johnc@1324 4941 concurrent_g1_refine()->clear_hot_cache();
ysr@777 4942 concurrent_g1_refine()->set_use_cache(true);
ysr@777 4943
ysr@777 4944 finalize_for_evac_failure();
ysr@777 4945
ysr@777 4946 // Must do this before removing self-forwarding pointers, which clears
ysr@777 4947 // the per-region evac-failure flags.
ysr@777 4948 concurrent_mark()->complete_marking_in_collection_set();
ysr@777 4949
ysr@777 4950 if (evacuation_failed()) {
ysr@777 4951 remove_self_forwarding_pointers();
ysr@777 4952 if (PrintGCDetails) {
tonyp@2074 4953 gclog_or_tty->print(" (to-space overflow)");
ysr@777 4954 } else if (PrintGC) {
ysr@777 4955 gclog_or_tty->print("--");
ysr@777 4956 }
ysr@777 4957 }
ysr@777 4958
iveresov@1051 4959 if (G1DeferredRSUpdate) {
iveresov@1051 4960 RedirtyLoggedCardTableEntryFastClosure redirty;
iveresov@1051 4961 dirty_card_queue_set().set_closure(&redirty);
iveresov@1051 4962 dirty_card_queue_set().apply_closure_to_all_completed_buffers();
iveresov@1546 4963
iveresov@1546 4964 DirtyCardQueueSet& dcq = JavaThread::dirty_card_queue_set();
iveresov@1546 4965 dcq.merge_bufferlists(&dirty_card_queue_set());
iveresov@1051 4966 assert(dirty_card_queue_set().completed_buffers_num() == 0, "All should be consumed");
iveresov@1051 4967 }
ysr@777 4968 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
ysr@777 4969 }
ysr@777 4970
tonyp@2493 4971 void G1CollectedHeap::free_region_if_empty(HeapRegion* hr,
tonyp@2472 4972 size_t* pre_used,
tonyp@2472 4973 FreeRegionList* free_list,
tonyp@2472 4974 HumongousRegionSet* humongous_proxy_set,
tonyp@2493 4975 HRRSCleanupTask* hrrs_cleanup_task,
tonyp@2472 4976 bool par) {
tonyp@2472 4977 if (hr->used() > 0 && hr->max_live_bytes() == 0 && !hr->is_young()) {
tonyp@2472 4978 if (hr->isHumongous()) {
tonyp@2472 4979 assert(hr->startsHumongous(), "we should only see starts humongous");
tonyp@2472 4980 free_humongous_region(hr, pre_used, free_list, humongous_proxy_set, par);
tonyp@2472 4981 } else {
tonyp@2472 4982 free_region(hr, pre_used, free_list, par);
tonyp@2472 4983 }
tonyp@2493 4984 } else {
tonyp@2493 4985 hr->rem_set()->do_cleanup_work(hrrs_cleanup_task);
tonyp@2472 4986 }
ysr@777 4987 }
ysr@777 4988
tonyp@2472 4989 void G1CollectedHeap::free_region(HeapRegion* hr,
tonyp@2472 4990 size_t* pre_used,
tonyp@2472 4991 FreeRegionList* free_list,
ysr@777 4992 bool par) {
tonyp@2472 4993 assert(!hr->isHumongous(), "this is only for non-humongous regions");
tonyp@2472 4994 assert(!hr->is_empty(), "the region should not be empty");
tonyp@2472 4995 assert(free_list != NULL, "pre-condition");
tonyp@2472 4996
tonyp@2472 4997 *pre_used += hr->used();
tonyp@2472 4998 hr->hr_clear(par, true /* clear_space */);
tonyp@2472 4999 free_list->add_as_tail(hr);
tonyp@2472 5000 }
tonyp@2472 5001
tonyp@2472 5002 void G1CollectedHeap::free_humongous_region(HeapRegion* hr,
tonyp@2472 5003 size_t* pre_used,
tonyp@2472 5004 FreeRegionList* free_list,
tonyp@2472 5005 HumongousRegionSet* humongous_proxy_set,
tonyp@2472 5006 bool par) {
tonyp@2472 5007 assert(hr->startsHumongous(), "this is only for starts humongous regions");
tonyp@2472 5008 assert(free_list != NULL, "pre-condition");
tonyp@2472 5009 assert(humongous_proxy_set != NULL, "pre-condition");
tonyp@2472 5010
tonyp@2472 5011 size_t hr_used = hr->used();
tonyp@2472 5012 size_t hr_capacity = hr->capacity();
tonyp@2472 5013 size_t hr_pre_used = 0;
tonyp@2472 5014 _humongous_set.remove_with_proxy(hr, humongous_proxy_set);
tonyp@2472 5015 hr->set_notHumongous();
tonyp@2472 5016 free_region(hr, &hr_pre_used, free_list, par);
tonyp@2472 5017
tonyp@2472 5018 int i = hr->hrs_index() + 1;
tonyp@2472 5019 size_t num = 1;
tonyp@2472 5020 while ((size_t) i < n_regions()) {
tonyp@2472 5021 HeapRegion* curr_hr = _hrs->at(i);
tonyp@2472 5022 if (!curr_hr->continuesHumongous()) {
tonyp@2472 5023 break;
ysr@777 5024 }
tonyp@2472 5025 curr_hr->set_notHumongous();
tonyp@2472 5026 free_region(curr_hr, &hr_pre_used, free_list, par);
tonyp@2472 5027 num += 1;
tonyp@2472 5028 i += 1;
tonyp@2472 5029 }
tonyp@2472 5030 assert(hr_pre_used == hr_used,
tonyp@2472 5031 err_msg("hr_pre_used: "SIZE_FORMAT" and hr_used: "SIZE_FORMAT" "
tonyp@2472 5032 "should be the same", hr_pre_used, hr_used));
tonyp@2472 5033 *pre_used += hr_pre_used;
ysr@777 5034 }
ysr@777 5035
tonyp@2472 5036 void G1CollectedHeap::update_sets_after_freeing_regions(size_t pre_used,
tonyp@2472 5037 FreeRegionList* free_list,
tonyp@2472 5038 HumongousRegionSet* humongous_proxy_set,
tonyp@2472 5039 bool par) {
tonyp@2472 5040 if (pre_used > 0) {
tonyp@2472 5041 Mutex* lock = (par) ? ParGCRareEvent_lock : NULL;
ysr@777 5042 MutexLockerEx x(lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5043 assert(_summary_bytes_used >= pre_used,
tonyp@2472 5044 err_msg("invariant: _summary_bytes_used: "SIZE_FORMAT" "
tonyp@2472 5045 "should be >= pre_used: "SIZE_FORMAT,
tonyp@2472 5046 _summary_bytes_used, pre_used));
ysr@777 5047 _summary_bytes_used -= pre_used;
tonyp@2472 5048 }
tonyp@2472 5049 if (free_list != NULL && !free_list->is_empty()) {
tonyp@2472 5050 MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5051 _free_list.add_as_tail(free_list);
tonyp@2472 5052 }
tonyp@2472 5053 if (humongous_proxy_set != NULL && !humongous_proxy_set->is_empty()) {
tonyp@2472 5054 MutexLockerEx x(OldSets_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5055 _humongous_set.update_from_proxy(humongous_proxy_set);
ysr@777 5056 }
ysr@777 5057 }
ysr@777 5058
ysr@777 5059 void G1CollectedHeap::dirtyCardsForYoungRegions(CardTableModRefBS* ct_bs, HeapRegion* list) {
ysr@777 5060 while (list != NULL) {
ysr@777 5061 guarantee( list->is_young(), "invariant" );
ysr@777 5062
ysr@777 5063 HeapWord* bottom = list->bottom();
ysr@777 5064 HeapWord* end = list->end();
ysr@777 5065 MemRegion mr(bottom, end);
ysr@777 5066 ct_bs->dirty(mr);
ysr@777 5067
ysr@777 5068 list = list->get_next_young_region();
ysr@777 5069 }
ysr@777 5070 }
ysr@777 5071
apetrusenko@1231 5072
apetrusenko@1231 5073 class G1ParCleanupCTTask : public AbstractGangTask {
apetrusenko@1231 5074 CardTableModRefBS* _ct_bs;
apetrusenko@1231 5075 G1CollectedHeap* _g1h;
apetrusenko@1375 5076 HeapRegion* volatile _su_head;
apetrusenko@1231 5077 public:
apetrusenko@1231 5078 G1ParCleanupCTTask(CardTableModRefBS* ct_bs,
apetrusenko@1375 5079 G1CollectedHeap* g1h,
apetrusenko@1375 5080 HeapRegion* survivor_list) :
apetrusenko@1231 5081 AbstractGangTask("G1 Par Cleanup CT Task"),
apetrusenko@1231 5082 _ct_bs(ct_bs),
apetrusenko@1375 5083 _g1h(g1h),
apetrusenko@1375 5084 _su_head(survivor_list)
apetrusenko@1231 5085 { }
apetrusenko@1231 5086
apetrusenko@1231 5087 void work(int i) {
apetrusenko@1231 5088 HeapRegion* r;
apetrusenko@1231 5089 while (r = _g1h->pop_dirty_cards_region()) {
apetrusenko@1231 5090 clear_cards(r);
apetrusenko@1231 5091 }
johnc@1829 5092 // Redirty the cards of the survivor regions.
apetrusenko@1375 5093 dirty_list(&this->_su_head);
apetrusenko@1375 5094 }
apetrusenko@1375 5095
apetrusenko@1231 5096 void clear_cards(HeapRegion* r) {
johnc@1829 5097 // Cards for Survivor regions will be dirtied later.
johnc@1829 5098 if (!r->is_survivor()) {
apetrusenko@1231 5099 _ct_bs->clear(MemRegion(r->bottom(), r->end()));
apetrusenko@1231 5100 }
apetrusenko@1231 5101 }
apetrusenko@1375 5102
apetrusenko@1375 5103 void dirty_list(HeapRegion* volatile * head_ptr) {
apetrusenko@1375 5104 HeapRegion* head;
apetrusenko@1375 5105 do {
apetrusenko@1375 5106 // Pop region off the list.
apetrusenko@1375 5107 head = *head_ptr;
apetrusenko@1375 5108 if (head != NULL) {
apetrusenko@1375 5109 HeapRegion* r = (HeapRegion*)
apetrusenko@1375 5110 Atomic::cmpxchg_ptr(head->get_next_young_region(), head_ptr, head);
apetrusenko@1375 5111 if (r == head) {
apetrusenko@1375 5112 assert(!r->isHumongous(), "Humongous regions shouldn't be on survivor list");
apetrusenko@1375 5113 _ct_bs->dirty(MemRegion(r->bottom(), r->end()));
apetrusenko@1375 5114 }
apetrusenko@1375 5115 }
apetrusenko@1375 5116 } while (*head_ptr != NULL);
apetrusenko@1375 5117 }
apetrusenko@1231 5118 };
apetrusenko@1231 5119
apetrusenko@1231 5120
apetrusenko@1375 5121 #ifndef PRODUCT
apetrusenko@1375 5122 class G1VerifyCardTableCleanup: public HeapRegionClosure {
apetrusenko@1375 5123 CardTableModRefBS* _ct_bs;
apetrusenko@1375 5124 public:
apetrusenko@1375 5125 G1VerifyCardTableCleanup(CardTableModRefBS* ct_bs)
apetrusenko@1375 5126 : _ct_bs(ct_bs)
apetrusenko@1375 5127 { }
apetrusenko@1375 5128 virtual bool doHeapRegion(HeapRegion* r)
apetrusenko@1375 5129 {
apetrusenko@1375 5130 MemRegion mr(r->bottom(), r->end());
johnc@1829 5131 if (r->is_survivor()) {
apetrusenko@1375 5132 _ct_bs->verify_dirty_region(mr);
apetrusenko@1375 5133 } else {
apetrusenko@1375 5134 _ct_bs->verify_clean_region(mr);
apetrusenko@1375 5135 }
apetrusenko@1375 5136 return false;
apetrusenko@1375 5137 }
apetrusenko@1375 5138 };
apetrusenko@1375 5139 #endif
apetrusenko@1375 5140
ysr@777 5141 void G1CollectedHeap::cleanUpCardTable() {
ysr@777 5142 CardTableModRefBS* ct_bs = (CardTableModRefBS*) (barrier_set());
ysr@777 5143 double start = os::elapsedTime();
ysr@777 5144
apetrusenko@1231 5145 // Iterate over the dirty cards region list.
apetrusenko@1375 5146 G1ParCleanupCTTask cleanup_task(ct_bs, this,
apetrusenko@1375 5147 _young_list->first_survivor_region());
johnc@1829 5148
apetrusenko@1231 5149 if (ParallelGCThreads > 0) {
apetrusenko@1231 5150 set_par_threads(workers()->total_workers());
apetrusenko@1231 5151 workers()->run_task(&cleanup_task);
apetrusenko@1231 5152 set_par_threads(0);
apetrusenko@1231 5153 } else {
apetrusenko@1231 5154 while (_dirty_cards_region_list) {
apetrusenko@1231 5155 HeapRegion* r = _dirty_cards_region_list;
apetrusenko@1231 5156 cleanup_task.clear_cards(r);
apetrusenko@1231 5157 _dirty_cards_region_list = r->get_next_dirty_cards_region();
apetrusenko@1231 5158 if (_dirty_cards_region_list == r) {
apetrusenko@1231 5159 // The last region.
apetrusenko@1231 5160 _dirty_cards_region_list = NULL;
apetrusenko@1231 5161 }
apetrusenko@1231 5162 r->set_next_dirty_cards_region(NULL);
apetrusenko@1231 5163 }
johnc@1829 5164 // now, redirty the cards of the survivor regions
apetrusenko@1375 5165 // (it seemed faster to do it this way, instead of iterating over
apetrusenko@1375 5166 // all regions and then clearing / dirtying as appropriate)
apetrusenko@1375 5167 dirtyCardsForYoungRegions(ct_bs, _young_list->first_survivor_region());
apetrusenko@1375 5168 }
johnc@1829 5169
ysr@777 5170 double elapsed = os::elapsedTime() - start;
ysr@777 5171 g1_policy()->record_clear_ct_time( elapsed * 1000.0);
apetrusenko@1375 5172 #ifndef PRODUCT
apetrusenko@1375 5173 if (G1VerifyCTCleanup || VerifyAfterGC) {
apetrusenko@1375 5174 G1VerifyCardTableCleanup cleanup_verifier(ct_bs);
apetrusenko@1375 5175 heap_region_iterate(&cleanup_verifier);
apetrusenko@1375 5176 }
apetrusenko@1375 5177 #endif
ysr@777 5178 }
ysr@777 5179
ysr@777 5180 void G1CollectedHeap::free_collection_set(HeapRegion* cs_head) {
tonyp@2472 5181 size_t pre_used = 0;
tonyp@2472 5182 FreeRegionList local_free_list("Local List for CSet Freeing");
tonyp@2472 5183
ysr@777 5184 double young_time_ms = 0.0;
ysr@777 5185 double non_young_time_ms = 0.0;
ysr@777 5186
johnc@1829 5187 // Since the collection set is a superset of the the young list,
johnc@1829 5188 // all we need to do to clear the young list is clear its
johnc@1829 5189 // head and length, and unlink any young regions in the code below
johnc@1829 5190 _young_list->clear();
johnc@1829 5191
ysr@777 5192 G1CollectorPolicy* policy = g1_policy();
ysr@777 5193
ysr@777 5194 double start_sec = os::elapsedTime();
ysr@777 5195 bool non_young = true;
ysr@777 5196
ysr@777 5197 HeapRegion* cur = cs_head;
ysr@777 5198 int age_bound = -1;
ysr@777 5199 size_t rs_lengths = 0;
ysr@777 5200
ysr@777 5201 while (cur != NULL) {
tonyp@2472 5202 assert(!is_on_free_list(cur), "sanity");
tonyp@2472 5203
ysr@777 5204 if (non_young) {
ysr@777 5205 if (cur->is_young()) {
ysr@777 5206 double end_sec = os::elapsedTime();
ysr@777 5207 double elapsed_ms = (end_sec - start_sec) * 1000.0;
ysr@777 5208 non_young_time_ms += elapsed_ms;
ysr@777 5209
ysr@777 5210 start_sec = os::elapsedTime();
ysr@777 5211 non_young = false;
ysr@777 5212 }
ysr@777 5213 } else {
tonyp@2472 5214 double end_sec = os::elapsedTime();
tonyp@2472 5215 double elapsed_ms = (end_sec - start_sec) * 1000.0;
tonyp@2472 5216 young_time_ms += elapsed_ms;
tonyp@2472 5217
tonyp@2472 5218 start_sec = os::elapsedTime();
tonyp@2472 5219 non_young = true;
ysr@777 5220 }
ysr@777 5221
ysr@777 5222 rs_lengths += cur->rem_set()->occupied();
ysr@777 5223
ysr@777 5224 HeapRegion* next = cur->next_in_collection_set();
ysr@777 5225 assert(cur->in_collection_set(), "bad CS");
ysr@777 5226 cur->set_next_in_collection_set(NULL);
ysr@777 5227 cur->set_in_collection_set(false);
ysr@777 5228
ysr@777 5229 if (cur->is_young()) {
ysr@777 5230 int index = cur->young_index_in_cset();
ysr@777 5231 guarantee( index != -1, "invariant" );
ysr@777 5232 guarantee( (size_t)index < policy->young_cset_length(), "invariant" );
ysr@777 5233 size_t words_survived = _surviving_young_words[index];
ysr@777 5234 cur->record_surv_words_in_group(words_survived);
johnc@1829 5235
johnc@1829 5236 // At this point the we have 'popped' cur from the collection set
johnc@1829 5237 // (linked via next_in_collection_set()) but it is still in the
johnc@1829 5238 // young list (linked via next_young_region()). Clear the
johnc@1829 5239 // _next_young_region field.
johnc@1829 5240 cur->set_next_young_region(NULL);
ysr@777 5241 } else {
ysr@777 5242 int index = cur->young_index_in_cset();
ysr@777 5243 guarantee( index == -1, "invariant" );
ysr@777 5244 }
ysr@777 5245
ysr@777 5246 assert( (cur->is_young() && cur->young_index_in_cset() > -1) ||
ysr@777 5247 (!cur->is_young() && cur->young_index_in_cset() == -1),
ysr@777 5248 "invariant" );
ysr@777 5249
ysr@777 5250 if (!cur->evacuation_failed()) {
ysr@777 5251 // And the region is empty.
tonyp@2472 5252 assert(!cur->is_empty(), "Should not have empty regions in a CS.");
tonyp@2472 5253 free_region(cur, &pre_used, &local_free_list, false /* par */);
ysr@777 5254 } else {
ysr@777 5255 cur->uninstall_surv_rate_group();
ysr@777 5256 if (cur->is_young())
ysr@777 5257 cur->set_young_index_in_cset(-1);
ysr@777 5258 cur->set_not_young();
ysr@777 5259 cur->set_evacuation_failed(false);
ysr@777 5260 }
ysr@777 5261 cur = next;
ysr@777 5262 }
ysr@777 5263
ysr@777 5264 policy->record_max_rs_lengths(rs_lengths);
ysr@777 5265 policy->cset_regions_freed();
ysr@777 5266
ysr@777 5267 double end_sec = os::elapsedTime();
ysr@777 5268 double elapsed_ms = (end_sec - start_sec) * 1000.0;
ysr@777 5269 if (non_young)
ysr@777 5270 non_young_time_ms += elapsed_ms;
ysr@777 5271 else
ysr@777 5272 young_time_ms += elapsed_ms;
ysr@777 5273
tonyp@2472 5274 update_sets_after_freeing_regions(pre_used, &local_free_list,
tonyp@2472 5275 NULL /* humongous_proxy_set */,
tonyp@2472 5276 false /* par */);
ysr@777 5277 policy->record_young_free_cset_time_ms(young_time_ms);
ysr@777 5278 policy->record_non_young_free_cset_time_ms(non_young_time_ms);
ysr@777 5279 }
ysr@777 5280
johnc@1829 5281 // This routine is similar to the above but does not record
johnc@1829 5282 // any policy statistics or update free lists; we are abandoning
johnc@1829 5283 // the current incremental collection set in preparation of a
johnc@1829 5284 // full collection. After the full GC we will start to build up
johnc@1829 5285 // the incremental collection set again.
johnc@1829 5286 // This is only called when we're doing a full collection
johnc@1829 5287 // and is immediately followed by the tearing down of the young list.
johnc@1829 5288
johnc@1829 5289 void G1CollectedHeap::abandon_collection_set(HeapRegion* cs_head) {
johnc@1829 5290 HeapRegion* cur = cs_head;
johnc@1829 5291
johnc@1829 5292 while (cur != NULL) {
johnc@1829 5293 HeapRegion* next = cur->next_in_collection_set();
johnc@1829 5294 assert(cur->in_collection_set(), "bad CS");
johnc@1829 5295 cur->set_next_in_collection_set(NULL);
johnc@1829 5296 cur->set_in_collection_set(false);
johnc@1829 5297 cur->set_young_index_in_cset(-1);
johnc@1829 5298 cur = next;
johnc@1829 5299 }
johnc@1829 5300 }
johnc@1829 5301
tonyp@2472 5302 void G1CollectedHeap::set_free_regions_coming() {
tonyp@2472 5303 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 5304 gclog_or_tty->print_cr("G1ConcRegionFreeing [cm thread] : "
tonyp@2472 5305 "setting free regions coming");
tonyp@2472 5306 }
tonyp@2472 5307
tonyp@2472 5308 assert(!free_regions_coming(), "pre-condition");
tonyp@2472 5309 _free_regions_coming = true;
tonyp@2472 5310 }
tonyp@2472 5311
tonyp@2472 5312 void G1CollectedHeap::reset_free_regions_coming() {
tonyp@2472 5313 {
tonyp@2472 5314 assert(free_regions_coming(), "pre-condition");
tonyp@2472 5315 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5316 _free_regions_coming = false;
tonyp@2472 5317 SecondaryFreeList_lock->notify_all();
tonyp@2472 5318 }
tonyp@2472 5319
tonyp@2472 5320 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 5321 gclog_or_tty->print_cr("G1ConcRegionFreeing [cm thread] : "
tonyp@2472 5322 "reset free regions coming");
tonyp@2472 5323 }
tonyp@2472 5324 }
tonyp@2472 5325
tonyp@2472 5326 void G1CollectedHeap::wait_while_free_regions_coming() {
tonyp@2472 5327 // Most of the time we won't have to wait, so let's do a quick test
tonyp@2472 5328 // first before we take the lock.
tonyp@2472 5329 if (!free_regions_coming()) {
tonyp@2472 5330 return;
tonyp@2472 5331 }
tonyp@2472 5332
tonyp@2472 5333 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 5334 gclog_or_tty->print_cr("G1ConcRegionFreeing [other] : "
tonyp@2472 5335 "waiting for free regions");
tonyp@2472 5336 }
tonyp@2472 5337
tonyp@2472 5338 {
tonyp@2472 5339 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5340 while (free_regions_coming()) {
tonyp@2472 5341 SecondaryFreeList_lock->wait(Mutex::_no_safepoint_check_flag);
ysr@777 5342 }
ysr@777 5343 }
tonyp@2472 5344
tonyp@2472 5345 if (G1ConcRegionFreeingVerbose) {
tonyp@2472 5346 gclog_or_tty->print_cr("G1ConcRegionFreeing [other] : "
tonyp@2472 5347 "done waiting for free regions");
tonyp@2472 5348 }
ysr@777 5349 }
ysr@777 5350
ysr@777 5351 size_t G1CollectedHeap::n_regions() {
ysr@777 5352 return _hrs->length();
ysr@777 5353 }
ysr@777 5354
ysr@777 5355 size_t G1CollectedHeap::max_regions() {
ysr@777 5356 return
johnc@2504 5357 (size_t)align_size_up(max_capacity(), HeapRegion::GrainBytes) /
ysr@777 5358 HeapRegion::GrainBytes;
ysr@777 5359 }
ysr@777 5360
ysr@777 5361 void G1CollectedHeap::set_region_short_lived_locked(HeapRegion* hr) {
ysr@777 5362 assert(heap_lock_held_for_gc(),
ysr@777 5363 "the heap lock should already be held by or for this thread");
ysr@777 5364 _young_list->push_region(hr);
ysr@777 5365 g1_policy()->set_region_short_lived(hr);
ysr@777 5366 }
ysr@777 5367
ysr@777 5368 class NoYoungRegionsClosure: public HeapRegionClosure {
ysr@777 5369 private:
ysr@777 5370 bool _success;
ysr@777 5371 public:
ysr@777 5372 NoYoungRegionsClosure() : _success(true) { }
ysr@777 5373 bool doHeapRegion(HeapRegion* r) {
ysr@777 5374 if (r->is_young()) {
ysr@777 5375 gclog_or_tty->print_cr("Region ["PTR_FORMAT", "PTR_FORMAT") tagged as young",
ysr@777 5376 r->bottom(), r->end());
ysr@777 5377 _success = false;
ysr@777 5378 }
ysr@777 5379 return false;
ysr@777 5380 }
ysr@777 5381 bool success() { return _success; }
ysr@777 5382 };
ysr@777 5383
johnc@1829 5384 bool G1CollectedHeap::check_young_list_empty(bool check_heap, bool check_sample) {
johnc@1829 5385 bool ret = _young_list->check_list_empty(check_sample);
johnc@1829 5386
johnc@1829 5387 if (check_heap) {
ysr@777 5388 NoYoungRegionsClosure closure;
ysr@777 5389 heap_region_iterate(&closure);
ysr@777 5390 ret = ret && closure.success();
ysr@777 5391 }
ysr@777 5392
ysr@777 5393 return ret;
ysr@777 5394 }
ysr@777 5395
ysr@777 5396 void G1CollectedHeap::empty_young_list() {
ysr@777 5397 assert(heap_lock_held_for_gc(),
ysr@777 5398 "the heap lock should already be held by or for this thread");
ysr@777 5399 assert(g1_policy()->in_young_gc_mode(), "should be in young GC mode");
ysr@777 5400
ysr@777 5401 _young_list->empty_list();
ysr@777 5402 }
ysr@777 5403
ysr@777 5404 bool G1CollectedHeap::all_alloc_regions_no_allocs_since_save_marks() {
ysr@777 5405 bool no_allocs = true;
ysr@777 5406 for (int ap = 0; ap < GCAllocPurposeCount && no_allocs; ++ap) {
ysr@777 5407 HeapRegion* r = _gc_alloc_regions[ap];
ysr@777 5408 no_allocs = r == NULL || r->saved_mark_at_top();
ysr@777 5409 }
ysr@777 5410 return no_allocs;
ysr@777 5411 }
ysr@777 5412
apetrusenko@980 5413 void G1CollectedHeap::retire_all_alloc_regions() {
ysr@777 5414 for (int ap = 0; ap < GCAllocPurposeCount; ++ap) {
ysr@777 5415 HeapRegion* r = _gc_alloc_regions[ap];
ysr@777 5416 if (r != NULL) {
ysr@777 5417 // Check for aliases.
ysr@777 5418 bool has_processed_alias = false;
ysr@777 5419 for (int i = 0; i < ap; ++i) {
ysr@777 5420 if (_gc_alloc_regions[i] == r) {
ysr@777 5421 has_processed_alias = true;
ysr@777 5422 break;
ysr@777 5423 }
ysr@777 5424 }
ysr@777 5425 if (!has_processed_alias) {
apetrusenko@980 5426 retire_alloc_region(r, false /* par */);
ysr@777 5427 }
ysr@777 5428 }
ysr@777 5429 }
ysr@777 5430 }
ysr@777 5431
ysr@777 5432 // Done at the start of full GC.
ysr@777 5433 void G1CollectedHeap::tear_down_region_lists() {
tonyp@2472 5434 _free_list.remove_all();
ysr@777 5435 }
ysr@777 5436
ysr@777 5437 class RegionResetter: public HeapRegionClosure {
tonyp@2472 5438 G1CollectedHeap* _g1h;
tonyp@2472 5439 FreeRegionList _local_free_list;
tonyp@2472 5440
ysr@777 5441 public:
tonyp@2472 5442 RegionResetter() : _g1h(G1CollectedHeap::heap()),
tonyp@2472 5443 _local_free_list("Local Free List for RegionResetter") { }
tonyp@2472 5444
ysr@777 5445 bool doHeapRegion(HeapRegion* r) {
ysr@777 5446 if (r->continuesHumongous()) return false;
ysr@777 5447 if (r->top() > r->bottom()) {
ysr@777 5448 if (r->top() < r->end()) {
ysr@777 5449 Copy::fill_to_words(r->top(),
ysr@777 5450 pointer_delta(r->end(), r->top()));
ysr@777 5451 }
ysr@777 5452 } else {
ysr@777 5453 assert(r->is_empty(), "tautology");
tonyp@2472 5454 _local_free_list.add_as_tail(r);
ysr@777 5455 }
ysr@777 5456 return false;
ysr@777 5457 }
ysr@777 5458
tonyp@2472 5459 void update_free_lists() {
tonyp@2472 5460 _g1h->update_sets_after_freeing_regions(0, &_local_free_list, NULL,
tonyp@2472 5461 false /* par */);
tonyp@2472 5462 }
ysr@777 5463 };
ysr@777 5464
ysr@777 5465 // Done at the end of full GC.
ysr@777 5466 void G1CollectedHeap::rebuild_region_lists() {
ysr@777 5467 // This needs to go at the end of the full GC.
ysr@777 5468 RegionResetter rs;
ysr@777 5469 heap_region_iterate(&rs);
tonyp@2472 5470 rs.update_free_lists();
ysr@777 5471 }
ysr@777 5472
ysr@777 5473 void G1CollectedHeap::set_refine_cte_cl_concurrency(bool concurrent) {
ysr@777 5474 _refine_cte_cl->set_concurrent(concurrent);
ysr@777 5475 }
ysr@777 5476
tonyp@2472 5477 #ifdef ASSERT
ysr@777 5478
ysr@777 5479 bool G1CollectedHeap::is_in_closed_subset(const void* p) const {
ysr@777 5480 HeapRegion* hr = heap_region_containing(p);
ysr@777 5481 if (hr == NULL) {
ysr@777 5482 return is_in_permanent(p);
ysr@777 5483 } else {
ysr@777 5484 return hr->is_in(p);
ysr@777 5485 }
ysr@777 5486 }
tonyp@2472 5487 #endif // ASSERT
tonyp@2472 5488
tonyp@2472 5489 class VerifyRegionListsClosure : public HeapRegionClosure {
tonyp@2472 5490 private:
tonyp@2472 5491 HumongousRegionSet* _humongous_set;
tonyp@2472 5492 FreeRegionList* _free_list;
tonyp@2472 5493 size_t _region_count;
tonyp@2472 5494
tonyp@2472 5495 public:
tonyp@2472 5496 VerifyRegionListsClosure(HumongousRegionSet* humongous_set,
tonyp@2472 5497 FreeRegionList* free_list) :
tonyp@2472 5498 _humongous_set(humongous_set), _free_list(free_list),
tonyp@2472 5499 _region_count(0) { }
tonyp@2472 5500
tonyp@2472 5501 size_t region_count() { return _region_count; }
tonyp@2472 5502
tonyp@2472 5503 bool doHeapRegion(HeapRegion* hr) {
tonyp@2472 5504 _region_count += 1;
tonyp@2472 5505
tonyp@2472 5506 if (hr->continuesHumongous()) {
tonyp@2472 5507 return false;
tonyp@2472 5508 }
tonyp@2472 5509
tonyp@2472 5510 if (hr->is_young()) {
tonyp@2472 5511 // TODO
tonyp@2472 5512 } else if (hr->startsHumongous()) {
tonyp@2472 5513 _humongous_set->verify_next_region(hr);
tonyp@2472 5514 } else if (hr->is_empty()) {
tonyp@2472 5515 _free_list->verify_next_region(hr);
tonyp@2472 5516 }
tonyp@2472 5517 return false;
tonyp@2472 5518 }
tonyp@2472 5519 };
tonyp@2472 5520
tonyp@2472 5521 void G1CollectedHeap::verify_region_sets() {
tonyp@2472 5522 assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
tonyp@2472 5523
tonyp@2472 5524 // First, check the explicit lists.
tonyp@2472 5525 _free_list.verify();
tonyp@2472 5526 {
tonyp@2472 5527 // Given that a concurrent operation might be adding regions to
tonyp@2472 5528 // the secondary free list we have to take the lock before
tonyp@2472 5529 // verifying it.
tonyp@2472 5530 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5531 _secondary_free_list.verify();
tonyp@2472 5532 }
tonyp@2472 5533 _humongous_set.verify();
tonyp@2472 5534
tonyp@2472 5535 // If a concurrent region freeing operation is in progress it will
tonyp@2472 5536 // be difficult to correctly attributed any free regions we come
tonyp@2472 5537 // across to the correct free list given that they might belong to
tonyp@2472 5538 // one of several (free_list, secondary_free_list, any local lists,
tonyp@2472 5539 // etc.). So, if that's the case we will skip the rest of the
tonyp@2472 5540 // verification operation. Alternatively, waiting for the concurrent
tonyp@2472 5541 // operation to complete will have a non-trivial effect on the GC's
tonyp@2472 5542 // operation (no concurrent operation will last longer than the
tonyp@2472 5543 // interval between two calls to verification) and it might hide
tonyp@2472 5544 // any issues that we would like to catch during testing.
tonyp@2472 5545 if (free_regions_coming()) {
tonyp@2472 5546 return;
tonyp@2472 5547 }
tonyp@2472 5548
tonyp@2472 5549 {
tonyp@2472 5550 MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);
tonyp@2472 5551 // Make sure we append the secondary_free_list on the free_list so
tonyp@2472 5552 // that all free regions we will come across can be safely
tonyp@2472 5553 // attributed to the free_list.
tonyp@2472 5554 append_secondary_free_list();
tonyp@2472 5555 }
tonyp@2472 5556
tonyp@2472 5557 // Finally, make sure that the region accounting in the lists is
tonyp@2472 5558 // consistent with what we see in the heap.
tonyp@2472 5559 _humongous_set.verify_start();
tonyp@2472 5560 _free_list.verify_start();
tonyp@2472 5561
tonyp@2472 5562 VerifyRegionListsClosure cl(&_humongous_set, &_free_list);
tonyp@2472 5563 heap_region_iterate(&cl);
tonyp@2472 5564
tonyp@2472 5565 _humongous_set.verify_end();
tonyp@2472 5566 _free_list.verify_end();
ysr@777 5567 }

mercurial