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