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