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