Wed, 14 Dec 2011 13:34:57 -0800
7121618: Change type of number of GC workers to unsigned int.
Summary: Change variables representing the number of GC workers to uint from int and size_t. Change the parameter in work(int i) to work(uint worker_id).
Reviewed-by: brutisso, tonyp
1 /*
2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "gc_implementation/g1/concurrentG1Refine.hpp"
27 #include "gc_implementation/g1/concurrentMark.hpp"
28 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"
29 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
30 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
31 #include "gc_implementation/g1/g1ErgoVerbose.hpp"
32 #include "gc_implementation/g1/heapRegionRemSet.hpp"
33 #include "gc_implementation/shared/gcPolicyCounters.hpp"
34 #include "runtime/arguments.hpp"
35 #include "runtime/java.hpp"
36 #include "runtime/mutexLocker.hpp"
37 #include "utilities/debug.hpp"
39 // Different defaults for different number of GC threads
40 // They were chosen by running GCOld and SPECjbb on debris with different
41 // numbers of GC threads and choosing them based on the results
43 // all the same
44 static double rs_length_diff_defaults[] = {
45 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
46 };
48 static double cost_per_card_ms_defaults[] = {
49 0.01, 0.005, 0.005, 0.003, 0.003, 0.002, 0.002, 0.0015
50 };
52 // all the same
53 static double young_cards_per_entry_ratio_defaults[] = {
54 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
55 };
57 static double cost_per_entry_ms_defaults[] = {
58 0.015, 0.01, 0.01, 0.008, 0.008, 0.0055, 0.0055, 0.005
59 };
61 static double cost_per_byte_ms_defaults[] = {
62 0.00006, 0.00003, 0.00003, 0.000015, 0.000015, 0.00001, 0.00001, 0.000009
63 };
65 // these should be pretty consistent
66 static double constant_other_time_ms_defaults[] = {
67 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0
68 };
71 static double young_other_cost_per_region_ms_defaults[] = {
72 0.3, 0.2, 0.2, 0.15, 0.15, 0.12, 0.12, 0.1
73 };
75 static double non_young_other_cost_per_region_ms_defaults[] = {
76 1.0, 0.7, 0.7, 0.5, 0.5, 0.42, 0.42, 0.30
77 };
79 // Help class for avoiding interleaved logging
80 class LineBuffer: public StackObj {
82 private:
83 static const int BUFFER_LEN = 1024;
84 static const int INDENT_CHARS = 3;
85 char _buffer[BUFFER_LEN];
86 int _indent_level;
87 int _cur;
89 void vappend(const char* format, va_list ap) {
90 int res = vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap);
91 if (res != -1) {
92 _cur += res;
93 } else {
94 DEBUG_ONLY(warning("buffer too small in LineBuffer");)
95 _buffer[BUFFER_LEN -1] = 0;
96 _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again
97 }
98 }
100 public:
101 explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) {
102 for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) {
103 _buffer[_cur] = ' ';
104 }
105 }
107 #ifndef PRODUCT
108 ~LineBuffer() {
109 assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?");
110 }
111 #endif
113 void append(const char* format, ...) {
114 va_list ap;
115 va_start(ap, format);
116 vappend(format, ap);
117 va_end(ap);
118 }
120 void append_and_print_cr(const char* format, ...) {
121 va_list ap;
122 va_start(ap, format);
123 vappend(format, ap);
124 va_end(ap);
125 gclog_or_tty->print_cr("%s", _buffer);
126 _cur = _indent_level * INDENT_CHARS;
127 }
128 };
130 G1CollectorPolicy::G1CollectorPolicy() :
131 _parallel_gc_threads(G1CollectedHeap::use_parallel_gc_threads()
132 ? ParallelGCThreads : 1),
134 _recent_gc_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
135 _all_pause_times_ms(new NumberSeq()),
136 _stop_world_start(0.0),
137 _all_stop_world_times_ms(new NumberSeq()),
138 _all_yield_times_ms(new NumberSeq()),
139 _using_new_ratio_calculations(false),
141 _summary(new Summary()),
143 _cur_clear_ct_time_ms(0.0),
144 _mark_closure_time_ms(0.0),
146 _cur_ref_proc_time_ms(0.0),
147 _cur_ref_enq_time_ms(0.0),
149 #ifndef PRODUCT
150 _min_clear_cc_time_ms(-1.0),
151 _max_clear_cc_time_ms(-1.0),
152 _cur_clear_cc_time_ms(0.0),
153 _cum_clear_cc_time_ms(0.0),
154 _num_cc_clears(0L),
155 #endif
157 _aux_num(10),
158 _all_aux_times_ms(new NumberSeq[_aux_num]),
159 _cur_aux_start_times_ms(new double[_aux_num]),
160 _cur_aux_times_ms(new double[_aux_num]),
161 _cur_aux_times_set(new bool[_aux_num]),
163 _concurrent_mark_remark_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
164 _concurrent_mark_cleanup_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
166 _alloc_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
167 _prev_collection_pause_end_ms(0.0),
168 _pending_card_diff_seq(new TruncatedSeq(TruncatedSeqLength)),
169 _rs_length_diff_seq(new TruncatedSeq(TruncatedSeqLength)),
170 _cost_per_card_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
171 _young_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)),
172 _mixed_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)),
173 _cost_per_entry_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
174 _mixed_cost_per_entry_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
175 _cost_per_byte_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
176 _cost_per_byte_ms_during_cm_seq(new TruncatedSeq(TruncatedSeqLength)),
177 _constant_other_time_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
178 _young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
179 _non_young_other_cost_per_region_ms_seq(
180 new TruncatedSeq(TruncatedSeqLength)),
182 _pending_cards_seq(new TruncatedSeq(TruncatedSeqLength)),
183 _rs_lengths_seq(new TruncatedSeq(TruncatedSeqLength)),
185 _pause_time_target_ms((double) MaxGCPauseMillis),
187 _gcs_are_young(true),
188 _young_pause_num(0),
189 _mixed_pause_num(0),
191 _during_marking(false),
192 _in_marking_window(false),
193 _in_marking_window_im(false),
195 _known_garbage_ratio(0.0),
196 _known_garbage_bytes(0),
198 _young_gc_eff_seq(new TruncatedSeq(TruncatedSeqLength)),
200 _recent_prev_end_times_for_all_gcs_sec(
201 new TruncatedSeq(NumPrevPausesForHeuristics)),
203 _recent_avg_pause_time_ratio(0.0),
205 _all_full_gc_times_ms(new NumberSeq()),
207 _initiate_conc_mark_if_possible(false),
208 _during_initial_mark_pause(false),
209 _should_revert_to_young_gcs(false),
210 _last_young_gc(false),
211 _last_gc_was_young(false),
213 _eden_bytes_before_gc(0),
214 _survivor_bytes_before_gc(0),
215 _capacity_before_gc(0),
217 _prev_collection_pause_used_at_end_bytes(0),
219 _eden_cset_region_length(0),
220 _survivor_cset_region_length(0),
221 _old_cset_region_length(0),
223 _collection_set(NULL),
224 _collection_set_bytes_used_before(0),
226 // Incremental CSet attributes
227 _inc_cset_build_state(Inactive),
228 _inc_cset_head(NULL),
229 _inc_cset_tail(NULL),
230 _inc_cset_bytes_used_before(0),
231 _inc_cset_max_finger(NULL),
232 _inc_cset_recorded_rs_lengths(0),
233 _inc_cset_recorded_rs_lengths_diffs(0),
234 _inc_cset_predicted_elapsed_time_ms(0.0),
235 _inc_cset_predicted_elapsed_time_ms_diffs(0.0),
237 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
238 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
239 #endif // _MSC_VER
241 _short_lived_surv_rate_group(new SurvRateGroup(this, "Short Lived",
242 G1YoungSurvRateNumRegionsSummary)),
243 _survivor_surv_rate_group(new SurvRateGroup(this, "Survivor",
244 G1YoungSurvRateNumRegionsSummary)),
245 // add here any more surv rate groups
246 _recorded_survivor_regions(0),
247 _recorded_survivor_head(NULL),
248 _recorded_survivor_tail(NULL),
249 _survivors_age_table(true),
251 _gc_overhead_perc(0.0) {
253 // Set up the region size and associated fields. Given that the
254 // policy is created before the heap, we have to set this up here,
255 // so it's done as soon as possible.
256 HeapRegion::setup_heap_region_size(Arguments::min_heap_size());
257 HeapRegionRemSet::setup_remset_size();
259 G1ErgoVerbose::initialize();
260 if (PrintAdaptiveSizePolicy) {
261 // Currently, we only use a single switch for all the heuristics.
262 G1ErgoVerbose::set_enabled(true);
263 // Given that we don't currently have a verboseness level
264 // parameter, we'll hardcode this to high. This can be easily
265 // changed in the future.
266 G1ErgoVerbose::set_level(ErgoHigh);
267 } else {
268 G1ErgoVerbose::set_enabled(false);
269 }
271 // Verify PLAB sizes
272 const size_t region_size = HeapRegion::GrainWords;
273 if (YoungPLABSize > region_size || OldPLABSize > region_size) {
274 char buffer[128];
275 jio_snprintf(buffer, sizeof(buffer), "%sPLABSize should be at most "SIZE_FORMAT,
276 OldPLABSize > region_size ? "Old" : "Young", region_size);
277 vm_exit_during_initialization(buffer);
278 }
280 _recent_prev_end_times_for_all_gcs_sec->add(os::elapsedTime());
281 _prev_collection_pause_end_ms = os::elapsedTime() * 1000.0;
283 _par_last_gc_worker_start_times_ms = new double[_parallel_gc_threads];
284 _par_last_ext_root_scan_times_ms = new double[_parallel_gc_threads];
285 _par_last_mark_stack_scan_times_ms = new double[_parallel_gc_threads];
287 _par_last_update_rs_times_ms = new double[_parallel_gc_threads];
288 _par_last_update_rs_processed_buffers = new double[_parallel_gc_threads];
290 _par_last_scan_rs_times_ms = new double[_parallel_gc_threads];
292 _par_last_obj_copy_times_ms = new double[_parallel_gc_threads];
294 _par_last_termination_times_ms = new double[_parallel_gc_threads];
295 _par_last_termination_attempts = new double[_parallel_gc_threads];
296 _par_last_gc_worker_end_times_ms = new double[_parallel_gc_threads];
297 _par_last_gc_worker_times_ms = new double[_parallel_gc_threads];
298 _par_last_gc_worker_other_times_ms = new double[_parallel_gc_threads];
300 // start conservatively
301 _expensive_region_limit_ms = 0.5 * (double) MaxGCPauseMillis;
303 int index;
304 if (ParallelGCThreads == 0)
305 index = 0;
306 else if (ParallelGCThreads > 8)
307 index = 7;
308 else
309 index = ParallelGCThreads - 1;
311 _pending_card_diff_seq->add(0.0);
312 _rs_length_diff_seq->add(rs_length_diff_defaults[index]);
313 _cost_per_card_ms_seq->add(cost_per_card_ms_defaults[index]);
314 _young_cards_per_entry_ratio_seq->add(
315 young_cards_per_entry_ratio_defaults[index]);
316 _cost_per_entry_ms_seq->add(cost_per_entry_ms_defaults[index]);
317 _cost_per_byte_ms_seq->add(cost_per_byte_ms_defaults[index]);
318 _constant_other_time_ms_seq->add(constant_other_time_ms_defaults[index]);
319 _young_other_cost_per_region_ms_seq->add(
320 young_other_cost_per_region_ms_defaults[index]);
321 _non_young_other_cost_per_region_ms_seq->add(
322 non_young_other_cost_per_region_ms_defaults[index]);
324 // Below, we might need to calculate the pause time target based on
325 // the pause interval. When we do so we are going to give G1 maximum
326 // flexibility and allow it to do pauses when it needs to. So, we'll
327 // arrange that the pause interval to be pause time target + 1 to
328 // ensure that a) the pause time target is maximized with respect to
329 // the pause interval and b) we maintain the invariant that pause
330 // time target < pause interval. If the user does not want this
331 // maximum flexibility, they will have to set the pause interval
332 // explicitly.
334 // First make sure that, if either parameter is set, its value is
335 // reasonable.
336 if (!FLAG_IS_DEFAULT(MaxGCPauseMillis)) {
337 if (MaxGCPauseMillis < 1) {
338 vm_exit_during_initialization("MaxGCPauseMillis should be "
339 "greater than 0");
340 }
341 }
342 if (!FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
343 if (GCPauseIntervalMillis < 1) {
344 vm_exit_during_initialization("GCPauseIntervalMillis should be "
345 "greater than 0");
346 }
347 }
349 // Then, if the pause time target parameter was not set, set it to
350 // the default value.
351 if (FLAG_IS_DEFAULT(MaxGCPauseMillis)) {
352 if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
353 // The default pause time target in G1 is 200ms
354 FLAG_SET_DEFAULT(MaxGCPauseMillis, 200);
355 } else {
356 // We do not allow the pause interval to be set without the
357 // pause time target
358 vm_exit_during_initialization("GCPauseIntervalMillis cannot be set "
359 "without setting MaxGCPauseMillis");
360 }
361 }
363 // Then, if the interval parameter was not set, set it according to
364 // the pause time target (this will also deal with the case when the
365 // pause time target is the default value).
366 if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) {
367 FLAG_SET_DEFAULT(GCPauseIntervalMillis, MaxGCPauseMillis + 1);
368 }
370 // Finally, make sure that the two parameters are consistent.
371 if (MaxGCPauseMillis >= GCPauseIntervalMillis) {
372 char buffer[256];
373 jio_snprintf(buffer, 256,
374 "MaxGCPauseMillis (%u) should be less than "
375 "GCPauseIntervalMillis (%u)",
376 MaxGCPauseMillis, GCPauseIntervalMillis);
377 vm_exit_during_initialization(buffer);
378 }
380 double max_gc_time = (double) MaxGCPauseMillis / 1000.0;
381 double time_slice = (double) GCPauseIntervalMillis / 1000.0;
382 _mmu_tracker = new G1MMUTrackerQueue(time_slice, max_gc_time);
383 _sigma = (double) G1ConfidencePercent / 100.0;
385 // start conservatively (around 50ms is about right)
386 _concurrent_mark_remark_times_ms->add(0.05);
387 _concurrent_mark_cleanup_times_ms->add(0.20);
388 _tenuring_threshold = MaxTenuringThreshold;
389 // _max_survivor_regions will be calculated by
390 // update_young_list_target_length() during initialization.
391 _max_survivor_regions = 0;
393 assert(GCTimeRatio > 0,
394 "we should have set it to a default value set_g1_gc_flags() "
395 "if a user set it to 0");
396 _gc_overhead_perc = 100.0 * (1.0 / (1.0 + GCTimeRatio));
398 uintx reserve_perc = G1ReservePercent;
399 // Put an artificial ceiling on this so that it's not set to a silly value.
400 if (reserve_perc > 50) {
401 reserve_perc = 50;
402 warning("G1ReservePercent is set to a value that is too large, "
403 "it's been updated to %u", reserve_perc);
404 }
405 _reserve_factor = (double) reserve_perc / 100.0;
406 // This will be set when the heap is expanded
407 // for the first time during initialization.
408 _reserve_regions = 0;
410 initialize_all();
411 _collectionSetChooser = new CollectionSetChooser();
412 }
414 // Increment "i", mod "len"
415 static void inc_mod(int& i, int len) {
416 i++; if (i == len) i = 0;
417 }
419 void G1CollectorPolicy::initialize_flags() {
420 set_min_alignment(HeapRegion::GrainBytes);
421 set_max_alignment(GenRemSet::max_alignment_constraint(rem_set_name()));
422 if (SurvivorRatio < 1) {
423 vm_exit_during_initialization("Invalid survivor ratio specified");
424 }
425 CollectorPolicy::initialize_flags();
426 }
428 // The easiest way to deal with the parsing of the NewSize /
429 // MaxNewSize / etc. parameteres is to re-use the code in the
430 // TwoGenerationCollectorPolicy class. This is similar to what
431 // ParallelScavenge does with its GenerationSizer class (see
432 // ParallelScavengeHeap::initialize()). We might change this in the
433 // future, but it's a good start.
434 class G1YoungGenSizer : public TwoGenerationCollectorPolicy {
435 private:
436 size_t size_to_region_num(size_t byte_size) {
437 return MAX2((size_t) 1, byte_size / HeapRegion::GrainBytes);
438 }
440 public:
441 G1YoungGenSizer() {
442 initialize_flags();
443 initialize_size_info();
444 }
445 size_t min_young_region_num() {
446 return size_to_region_num(_min_gen0_size);
447 }
448 size_t initial_young_region_num() {
449 return size_to_region_num(_initial_gen0_size);
450 }
451 size_t max_young_region_num() {
452 return size_to_region_num(_max_gen0_size);
453 }
454 };
456 void G1CollectorPolicy::update_young_list_size_using_newratio(size_t number_of_heap_regions) {
457 assert(number_of_heap_regions > 0, "Heap must be initialized");
458 size_t young_size = number_of_heap_regions / (NewRatio + 1);
459 _min_desired_young_length = young_size;
460 _max_desired_young_length = young_size;
461 }
463 void G1CollectorPolicy::init() {
464 // Set aside an initial future to_space.
465 _g1 = G1CollectedHeap::heap();
467 assert(Heap_lock->owned_by_self(), "Locking discipline.");
469 initialize_gc_policy_counters();
471 G1YoungGenSizer sizer;
472 _min_desired_young_length = sizer.min_young_region_num();
473 _max_desired_young_length = sizer.max_young_region_num();
475 if (FLAG_IS_CMDLINE(NewRatio)) {
476 if (FLAG_IS_CMDLINE(NewSize) || FLAG_IS_CMDLINE(MaxNewSize)) {
477 warning("-XX:NewSize and -XX:MaxNewSize override -XX:NewRatio");
478 } else {
479 // Treat NewRatio as a fixed size that is only recalculated when the heap size changes
480 update_young_list_size_using_newratio(_g1->n_regions());
481 _using_new_ratio_calculations = true;
482 }
483 }
485 assert(_min_desired_young_length <= _max_desired_young_length, "Invalid min/max young gen size values");
487 set_adaptive_young_list_length(_min_desired_young_length < _max_desired_young_length);
488 if (adaptive_young_list_length()) {
489 _young_list_fixed_length = 0;
490 } else {
491 assert(_min_desired_young_length == _max_desired_young_length, "Min and max young size differ");
492 _young_list_fixed_length = _min_desired_young_length;
493 }
494 _free_regions_at_end_of_collection = _g1->free_regions();
495 update_young_list_target_length();
496 _prev_eden_capacity = _young_list_target_length * HeapRegion::GrainBytes;
498 // We may immediately start allocating regions and placing them on the
499 // collection set list. Initialize the per-collection set info
500 start_incremental_cset_building();
501 }
503 // Create the jstat counters for the policy.
504 void G1CollectorPolicy::initialize_gc_policy_counters() {
505 _gc_policy_counters = new GCPolicyCounters("GarbageFirst", 1, 3);
506 }
508 bool G1CollectorPolicy::predict_will_fit(size_t young_length,
509 double base_time_ms,
510 size_t base_free_regions,
511 double target_pause_time_ms) {
512 if (young_length >= base_free_regions) {
513 // end condition 1: not enough space for the young regions
514 return false;
515 }
517 double accum_surv_rate = accum_yg_surv_rate_pred((int)(young_length - 1));
518 size_t bytes_to_copy =
519 (size_t) (accum_surv_rate * (double) HeapRegion::GrainBytes);
520 double copy_time_ms = predict_object_copy_time_ms(bytes_to_copy);
521 double young_other_time_ms = predict_young_other_time_ms(young_length);
522 double pause_time_ms = base_time_ms + copy_time_ms + young_other_time_ms;
523 if (pause_time_ms > target_pause_time_ms) {
524 // end condition 2: prediction is over the target pause time
525 return false;
526 }
528 size_t free_bytes =
529 (base_free_regions - young_length) * HeapRegion::GrainBytes;
530 if ((2.0 * sigma()) * (double) bytes_to_copy > (double) free_bytes) {
531 // end condition 3: out-of-space (conservatively!)
532 return false;
533 }
535 // success!
536 return true;
537 }
539 void G1CollectorPolicy::record_new_heap_size(size_t new_number_of_regions) {
540 // re-calculate the necessary reserve
541 double reserve_regions_d = (double) new_number_of_regions * _reserve_factor;
542 // We use ceiling so that if reserve_regions_d is > 0.0 (but
543 // smaller than 1.0) we'll get 1.
544 _reserve_regions = (size_t) ceil(reserve_regions_d);
546 if (_using_new_ratio_calculations) {
547 // -XX:NewRatio was specified so we need to update the
548 // young gen length when the heap size has changed.
549 update_young_list_size_using_newratio(new_number_of_regions);
550 }
551 }
553 size_t G1CollectorPolicy::calculate_young_list_desired_min_length(
554 size_t base_min_length) {
555 size_t desired_min_length = 0;
556 if (adaptive_young_list_length()) {
557 if (_alloc_rate_ms_seq->num() > 3) {
558 double now_sec = os::elapsedTime();
559 double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0;
560 double alloc_rate_ms = predict_alloc_rate_ms();
561 desired_min_length = (size_t) ceil(alloc_rate_ms * when_ms);
562 } else {
563 // otherwise we don't have enough info to make the prediction
564 }
565 }
566 desired_min_length += base_min_length;
567 // make sure we don't go below any user-defined minimum bound
568 return MAX2(_min_desired_young_length, desired_min_length);
569 }
571 size_t G1CollectorPolicy::calculate_young_list_desired_max_length() {
572 // Here, we might want to also take into account any additional
573 // constraints (i.e., user-defined minimum bound). Currently, we
574 // effectively don't set this bound.
575 return _max_desired_young_length;
576 }
578 void G1CollectorPolicy::update_young_list_target_length(size_t rs_lengths) {
579 if (rs_lengths == (size_t) -1) {
580 // if it's set to the default value (-1), we should predict it;
581 // otherwise, use the given value.
582 rs_lengths = (size_t) get_new_prediction(_rs_lengths_seq);
583 }
585 // Calculate the absolute and desired min bounds.
587 // This is how many young regions we already have (currently: the survivors).
588 size_t base_min_length = recorded_survivor_regions();
589 // This is the absolute minimum young length, which ensures that we
590 // can allocate one eden region in the worst-case.
591 size_t absolute_min_length = base_min_length + 1;
592 size_t desired_min_length =
593 calculate_young_list_desired_min_length(base_min_length);
594 if (desired_min_length < absolute_min_length) {
595 desired_min_length = absolute_min_length;
596 }
598 // Calculate the absolute and desired max bounds.
600 // We will try our best not to "eat" into the reserve.
601 size_t absolute_max_length = 0;
602 if (_free_regions_at_end_of_collection > _reserve_regions) {
603 absolute_max_length = _free_regions_at_end_of_collection - _reserve_regions;
604 }
605 size_t desired_max_length = calculate_young_list_desired_max_length();
606 if (desired_max_length > absolute_max_length) {
607 desired_max_length = absolute_max_length;
608 }
610 size_t young_list_target_length = 0;
611 if (adaptive_young_list_length()) {
612 if (gcs_are_young()) {
613 young_list_target_length =
614 calculate_young_list_target_length(rs_lengths,
615 base_min_length,
616 desired_min_length,
617 desired_max_length);
618 _rs_lengths_prediction = rs_lengths;
619 } else {
620 // Don't calculate anything and let the code below bound it to
621 // the desired_min_length, i.e., do the next GC as soon as
622 // possible to maximize how many old regions we can add to it.
623 }
624 } else {
625 if (gcs_are_young()) {
626 young_list_target_length = _young_list_fixed_length;
627 } else {
628 // A bit arbitrary: during mixed GCs we allocate half
629 // the young regions to try to add old regions to the CSet.
630 young_list_target_length = _young_list_fixed_length / 2;
631 // We choose to accept that we might go under the desired min
632 // length given that we intentionally ask for a smaller young gen.
633 desired_min_length = absolute_min_length;
634 }
635 }
637 // Make sure we don't go over the desired max length, nor under the
638 // desired min length. In case they clash, desired_min_length wins
639 // which is why that test is second.
640 if (young_list_target_length > desired_max_length) {
641 young_list_target_length = desired_max_length;
642 }
643 if (young_list_target_length < desired_min_length) {
644 young_list_target_length = desired_min_length;
645 }
647 assert(young_list_target_length > recorded_survivor_regions(),
648 "we should be able to allocate at least one eden region");
649 assert(young_list_target_length >= absolute_min_length, "post-condition");
650 _young_list_target_length = young_list_target_length;
652 update_max_gc_locker_expansion();
653 }
655 size_t
656 G1CollectorPolicy::calculate_young_list_target_length(size_t rs_lengths,
657 size_t base_min_length,
658 size_t desired_min_length,
659 size_t desired_max_length) {
660 assert(adaptive_young_list_length(), "pre-condition");
661 assert(gcs_are_young(), "only call this for young GCs");
663 // In case some edge-condition makes the desired max length too small...
664 if (desired_max_length <= desired_min_length) {
665 return desired_min_length;
666 }
668 // We'll adjust min_young_length and max_young_length not to include
669 // the already allocated young regions (i.e., so they reflect the
670 // min and max eden regions we'll allocate). The base_min_length
671 // will be reflected in the predictions by the
672 // survivor_regions_evac_time prediction.
673 assert(desired_min_length > base_min_length, "invariant");
674 size_t min_young_length = desired_min_length - base_min_length;
675 assert(desired_max_length > base_min_length, "invariant");
676 size_t max_young_length = desired_max_length - base_min_length;
678 double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0;
679 double survivor_regions_evac_time = predict_survivor_regions_evac_time();
680 size_t pending_cards = (size_t) get_new_prediction(_pending_cards_seq);
681 size_t adj_rs_lengths = rs_lengths + predict_rs_length_diff();
682 size_t scanned_cards = predict_young_card_num(adj_rs_lengths);
683 double base_time_ms =
684 predict_base_elapsed_time_ms(pending_cards, scanned_cards) +
685 survivor_regions_evac_time;
686 size_t available_free_regions = _free_regions_at_end_of_collection;
687 size_t base_free_regions = 0;
688 if (available_free_regions > _reserve_regions) {
689 base_free_regions = available_free_regions - _reserve_regions;
690 }
692 // Here, we will make sure that the shortest young length that
693 // makes sense fits within the target pause time.
695 if (predict_will_fit(min_young_length, base_time_ms,
696 base_free_regions, target_pause_time_ms)) {
697 // The shortest young length will fit into the target pause time;
698 // we'll now check whether the absolute maximum number of young
699 // regions will fit in the target pause time. If not, we'll do
700 // a binary search between min_young_length and max_young_length.
701 if (predict_will_fit(max_young_length, base_time_ms,
702 base_free_regions, target_pause_time_ms)) {
703 // The maximum young length will fit into the target pause time.
704 // We are done so set min young length to the maximum length (as
705 // the result is assumed to be returned in min_young_length).
706 min_young_length = max_young_length;
707 } else {
708 // The maximum possible number of young regions will not fit within
709 // the target pause time so we'll search for the optimal
710 // length. The loop invariants are:
711 //
712 // min_young_length < max_young_length
713 // min_young_length is known to fit into the target pause time
714 // max_young_length is known not to fit into the target pause time
715 //
716 // Going into the loop we know the above hold as we've just
717 // checked them. Every time around the loop we check whether
718 // the middle value between min_young_length and
719 // max_young_length fits into the target pause time. If it
720 // does, it becomes the new min. If it doesn't, it becomes
721 // the new max. This way we maintain the loop invariants.
723 assert(min_young_length < max_young_length, "invariant");
724 size_t diff = (max_young_length - min_young_length) / 2;
725 while (diff > 0) {
726 size_t young_length = min_young_length + diff;
727 if (predict_will_fit(young_length, base_time_ms,
728 base_free_regions, target_pause_time_ms)) {
729 min_young_length = young_length;
730 } else {
731 max_young_length = young_length;
732 }
733 assert(min_young_length < max_young_length, "invariant");
734 diff = (max_young_length - min_young_length) / 2;
735 }
736 // The results is min_young_length which, according to the
737 // loop invariants, should fit within the target pause time.
739 // These are the post-conditions of the binary search above:
740 assert(min_young_length < max_young_length,
741 "otherwise we should have discovered that max_young_length "
742 "fits into the pause target and not done the binary search");
743 assert(predict_will_fit(min_young_length, base_time_ms,
744 base_free_regions, target_pause_time_ms),
745 "min_young_length, the result of the binary search, should "
746 "fit into the pause target");
747 assert(!predict_will_fit(min_young_length + 1, base_time_ms,
748 base_free_regions, target_pause_time_ms),
749 "min_young_length, the result of the binary search, should be "
750 "optimal, so no larger length should fit into the pause target");
751 }
752 } else {
753 // Even the minimum length doesn't fit into the pause time
754 // target, return it as the result nevertheless.
755 }
756 return base_min_length + min_young_length;
757 }
759 double G1CollectorPolicy::predict_survivor_regions_evac_time() {
760 double survivor_regions_evac_time = 0.0;
761 for (HeapRegion * r = _recorded_survivor_head;
762 r != NULL && r != _recorded_survivor_tail->get_next_young_region();
763 r = r->get_next_young_region()) {
764 survivor_regions_evac_time += predict_region_elapsed_time_ms(r, true);
765 }
766 return survivor_regions_evac_time;
767 }
769 void G1CollectorPolicy::revise_young_list_target_length_if_necessary() {
770 guarantee( adaptive_young_list_length(), "should not call this otherwise" );
772 size_t rs_lengths = _g1->young_list()->sampled_rs_lengths();
773 if (rs_lengths > _rs_lengths_prediction) {
774 // add 10% to avoid having to recalculate often
775 size_t rs_lengths_prediction = rs_lengths * 1100 / 1000;
776 update_young_list_target_length(rs_lengths_prediction);
777 }
778 }
782 HeapWord* G1CollectorPolicy::mem_allocate_work(size_t size,
783 bool is_tlab,
784 bool* gc_overhead_limit_was_exceeded) {
785 guarantee(false, "Not using this policy feature yet.");
786 return NULL;
787 }
789 // This method controls how a collector handles one or more
790 // of its generations being fully allocated.
791 HeapWord* G1CollectorPolicy::satisfy_failed_allocation(size_t size,
792 bool is_tlab) {
793 guarantee(false, "Not using this policy feature yet.");
794 return NULL;
795 }
798 #ifndef PRODUCT
799 bool G1CollectorPolicy::verify_young_ages() {
800 HeapRegion* head = _g1->young_list()->first_region();
801 return
802 verify_young_ages(head, _short_lived_surv_rate_group);
803 // also call verify_young_ages on any additional surv rate groups
804 }
806 bool
807 G1CollectorPolicy::verify_young_ages(HeapRegion* head,
808 SurvRateGroup *surv_rate_group) {
809 guarantee( surv_rate_group != NULL, "pre-condition" );
811 const char* name = surv_rate_group->name();
812 bool ret = true;
813 int prev_age = -1;
815 for (HeapRegion* curr = head;
816 curr != NULL;
817 curr = curr->get_next_young_region()) {
818 SurvRateGroup* group = curr->surv_rate_group();
819 if (group == NULL && !curr->is_survivor()) {
820 gclog_or_tty->print_cr("## %s: encountered NULL surv_rate_group", name);
821 ret = false;
822 }
824 if (surv_rate_group == group) {
825 int age = curr->age_in_surv_rate_group();
827 if (age < 0) {
828 gclog_or_tty->print_cr("## %s: encountered negative age", name);
829 ret = false;
830 }
832 if (age <= prev_age) {
833 gclog_or_tty->print_cr("## %s: region ages are not strictly increasing "
834 "(%d, %d)", name, age, prev_age);
835 ret = false;
836 }
837 prev_age = age;
838 }
839 }
841 return ret;
842 }
843 #endif // PRODUCT
845 void G1CollectorPolicy::record_full_collection_start() {
846 _cur_collection_start_sec = os::elapsedTime();
847 // Release the future to-space so that it is available for compaction into.
848 _g1->set_full_collection();
849 }
851 void G1CollectorPolicy::record_full_collection_end() {
852 // Consider this like a collection pause for the purposes of allocation
853 // since last pause.
854 double end_sec = os::elapsedTime();
855 double full_gc_time_sec = end_sec - _cur_collection_start_sec;
856 double full_gc_time_ms = full_gc_time_sec * 1000.0;
858 _all_full_gc_times_ms->add(full_gc_time_ms);
860 update_recent_gc_times(end_sec, full_gc_time_ms);
862 _g1->clear_full_collection();
864 // "Nuke" the heuristics that control the young/mixed GC
865 // transitions and make sure we start with young GCs after the Full GC.
866 set_gcs_are_young(true);
867 _last_young_gc = false;
868 _should_revert_to_young_gcs = false;
869 clear_initiate_conc_mark_if_possible();
870 clear_during_initial_mark_pause();
871 _known_garbage_bytes = 0;
872 _known_garbage_ratio = 0.0;
873 _in_marking_window = false;
874 _in_marking_window_im = false;
876 _short_lived_surv_rate_group->start_adding_regions();
877 // also call this on any additional surv rate groups
879 record_survivor_regions(0, NULL, NULL);
881 _free_regions_at_end_of_collection = _g1->free_regions();
882 // Reset survivors SurvRateGroup.
883 _survivor_surv_rate_group->reset();
884 update_young_list_target_length();
885 _collectionSetChooser->updateAfterFullCollection();
886 }
888 void G1CollectorPolicy::record_stop_world_start() {
889 _stop_world_start = os::elapsedTime();
890 }
892 void G1CollectorPolicy::record_collection_pause_start(double start_time_sec,
893 size_t start_used) {
894 if (PrintGCDetails) {
895 gclog_or_tty->stamp(PrintGCTimeStamps);
896 gclog_or_tty->print("[GC pause");
897 gclog_or_tty->print(" (%s)", gcs_are_young() ? "young" : "mixed");
898 }
900 // We only need to do this here as the policy will only be applied
901 // to the GC we're about to start. so, no point is calculating this
902 // every time we calculate / recalculate the target young length.
903 update_survivors_policy();
905 assert(_g1->used() == _g1->recalculate_used(),
906 err_msg("sanity, used: "SIZE_FORMAT" recalculate_used: "SIZE_FORMAT,
907 _g1->used(), _g1->recalculate_used()));
909 double s_w_t_ms = (start_time_sec - _stop_world_start) * 1000.0;
910 _all_stop_world_times_ms->add(s_w_t_ms);
911 _stop_world_start = 0.0;
913 _cur_collection_start_sec = start_time_sec;
914 _cur_collection_pause_used_at_start_bytes = start_used;
915 _cur_collection_pause_used_regions_at_start = _g1->used_regions();
916 _pending_cards = _g1->pending_card_num();
917 _max_pending_cards = _g1->max_pending_card_num();
919 _bytes_in_collection_set_before_gc = 0;
920 _bytes_copied_during_gc = 0;
922 YoungList* young_list = _g1->young_list();
923 _eden_bytes_before_gc = young_list->eden_used_bytes();
924 _survivor_bytes_before_gc = young_list->survivor_used_bytes();
925 _capacity_before_gc = _g1->capacity();
927 #ifdef DEBUG
928 // initialise these to something well known so that we can spot
929 // if they are not set properly
931 for (int i = 0; i < _parallel_gc_threads; ++i) {
932 _par_last_gc_worker_start_times_ms[i] = -1234.0;
933 _par_last_ext_root_scan_times_ms[i] = -1234.0;
934 _par_last_mark_stack_scan_times_ms[i] = -1234.0;
935 _par_last_update_rs_times_ms[i] = -1234.0;
936 _par_last_update_rs_processed_buffers[i] = -1234.0;
937 _par_last_scan_rs_times_ms[i] = -1234.0;
938 _par_last_obj_copy_times_ms[i] = -1234.0;
939 _par_last_termination_times_ms[i] = -1234.0;
940 _par_last_termination_attempts[i] = -1234.0;
941 _par_last_gc_worker_end_times_ms[i] = -1234.0;
942 _par_last_gc_worker_times_ms[i] = -1234.0;
943 _par_last_gc_worker_other_times_ms[i] = -1234.0;
944 }
945 #endif
947 for (int i = 0; i < _aux_num; ++i) {
948 _cur_aux_times_ms[i] = 0.0;
949 _cur_aux_times_set[i] = false;
950 }
952 // This is initialized to zero here and is set during
953 // the evacuation pause if marking is in progress.
954 _cur_satb_drain_time_ms = 0.0;
956 _last_gc_was_young = false;
958 // do that for any other surv rate groups
959 _short_lived_surv_rate_group->stop_adding_regions();
960 _survivors_age_table.clear();
962 assert( verify_young_ages(), "region age verification" );
963 }
965 void G1CollectorPolicy::record_concurrent_mark_init_end(double
966 mark_init_elapsed_time_ms) {
967 _during_marking = true;
968 assert(!initiate_conc_mark_if_possible(), "we should have cleared it by now");
969 clear_during_initial_mark_pause();
970 _cur_mark_stop_world_time_ms = mark_init_elapsed_time_ms;
971 }
973 void G1CollectorPolicy::record_concurrent_mark_remark_start() {
974 _mark_remark_start_sec = os::elapsedTime();
975 _during_marking = false;
976 }
978 void G1CollectorPolicy::record_concurrent_mark_remark_end() {
979 double end_time_sec = os::elapsedTime();
980 double elapsed_time_ms = (end_time_sec - _mark_remark_start_sec)*1000.0;
981 _concurrent_mark_remark_times_ms->add(elapsed_time_ms);
982 _cur_mark_stop_world_time_ms += elapsed_time_ms;
983 _prev_collection_pause_end_ms += elapsed_time_ms;
985 _mmu_tracker->add_pause(_mark_remark_start_sec, end_time_sec, true);
986 }
988 void G1CollectorPolicy::record_concurrent_mark_cleanup_start() {
989 _mark_cleanup_start_sec = os::elapsedTime();
990 }
992 void G1CollectorPolicy::record_concurrent_mark_cleanup_completed() {
993 _should_revert_to_young_gcs = false;
994 _last_young_gc = true;
995 _in_marking_window = false;
996 }
998 void G1CollectorPolicy::record_concurrent_pause() {
999 if (_stop_world_start > 0.0) {
1000 double yield_ms = (os::elapsedTime() - _stop_world_start) * 1000.0;
1001 _all_yield_times_ms->add(yield_ms);
1002 }
1003 }
1005 void G1CollectorPolicy::record_concurrent_pause_end() {
1006 }
1008 template<class T>
1009 T sum_of(T* sum_arr, int start, int n, int N) {
1010 T sum = (T)0;
1011 for (int i = 0; i < n; i++) {
1012 int j = (start + i) % N;
1013 sum += sum_arr[j];
1014 }
1015 return sum;
1016 }
1018 void G1CollectorPolicy::print_par_stats(int level,
1019 const char* str,
1020 double* data) {
1021 double min = data[0], max = data[0];
1022 double total = 0.0;
1023 LineBuffer buf(level);
1024 buf.append("[%s (ms):", str);
1025 for (uint i = 0; i < no_of_gc_threads(); ++i) {
1026 double val = data[i];
1027 if (val < min)
1028 min = val;
1029 if (val > max)
1030 max = val;
1031 total += val;
1032 buf.append(" %3.1lf", val);
1033 }
1034 buf.append_and_print_cr("");
1035 double avg = total / (double) no_of_gc_threads();
1036 buf.append_and_print_cr(" Avg: %5.1lf, Min: %5.1lf, Max: %5.1lf, Diff: %5.1lf]",
1037 avg, min, max, max - min);
1038 }
1040 void G1CollectorPolicy::print_par_sizes(int level,
1041 const char* str,
1042 double* data) {
1043 double min = data[0], max = data[0];
1044 double total = 0.0;
1045 LineBuffer buf(level);
1046 buf.append("[%s :", str);
1047 for (uint i = 0; i < no_of_gc_threads(); ++i) {
1048 double val = data[i];
1049 if (val < min)
1050 min = val;
1051 if (val > max)
1052 max = val;
1053 total += val;
1054 buf.append(" %d", (int) val);
1055 }
1056 buf.append_and_print_cr("");
1057 double avg = total / (double) no_of_gc_threads();
1058 buf.append_and_print_cr(" Sum: %d, Avg: %d, Min: %d, Max: %d, Diff: %d]",
1059 (int)total, (int)avg, (int)min, (int)max, (int)max - (int)min);
1060 }
1062 void G1CollectorPolicy::print_stats(int level,
1063 const char* str,
1064 double value) {
1065 LineBuffer(level).append_and_print_cr("[%s: %5.1lf ms]", str, value);
1066 }
1068 void G1CollectorPolicy::print_stats(int level,
1069 const char* str,
1070 int value) {
1071 LineBuffer(level).append_and_print_cr("[%s: %d]", str, value);
1072 }
1074 double G1CollectorPolicy::avg_value(double* data) {
1075 if (G1CollectedHeap::use_parallel_gc_threads()) {
1076 double ret = 0.0;
1077 for (uint i = 0; i < no_of_gc_threads(); ++i) {
1078 ret += data[i];
1079 }
1080 return ret / (double) no_of_gc_threads();
1081 } else {
1082 return data[0];
1083 }
1084 }
1086 double G1CollectorPolicy::max_value(double* data) {
1087 if (G1CollectedHeap::use_parallel_gc_threads()) {
1088 double ret = data[0];
1089 for (uint i = 1; i < no_of_gc_threads(); ++i) {
1090 if (data[i] > ret) {
1091 ret = data[i];
1092 }
1093 }
1094 return ret;
1095 } else {
1096 return data[0];
1097 }
1098 }
1100 double G1CollectorPolicy::sum_of_values(double* data) {
1101 if (G1CollectedHeap::use_parallel_gc_threads()) {
1102 double sum = 0.0;
1103 for (uint i = 0; i < no_of_gc_threads(); i++) {
1104 sum += data[i];
1105 }
1106 return sum;
1107 } else {
1108 return data[0];
1109 }
1110 }
1112 double G1CollectorPolicy::max_sum(double* data1, double* data2) {
1113 double ret = data1[0] + data2[0];
1115 if (G1CollectedHeap::use_parallel_gc_threads()) {
1116 for (uint i = 1; i < no_of_gc_threads(); ++i) {
1117 double data = data1[i] + data2[i];
1118 if (data > ret) {
1119 ret = data;
1120 }
1121 }
1122 }
1123 return ret;
1124 }
1126 // Anything below that is considered to be zero
1127 #define MIN_TIMER_GRANULARITY 0.0000001
1129 void G1CollectorPolicy::record_collection_pause_end(int no_of_gc_threads) {
1130 double end_time_sec = os::elapsedTime();
1131 double elapsed_ms = _last_pause_time_ms;
1132 bool parallel = G1CollectedHeap::use_parallel_gc_threads();
1133 assert(_cur_collection_pause_used_regions_at_start >= cset_region_length(),
1134 "otherwise, the subtraction below does not make sense");
1135 size_t rs_size =
1136 _cur_collection_pause_used_regions_at_start - cset_region_length();
1137 size_t cur_used_bytes = _g1->used();
1138 assert(cur_used_bytes == _g1->recalculate_used(), "It should!");
1139 bool last_pause_included_initial_mark = false;
1140 bool update_stats = !_g1->evacuation_failed();
1141 set_no_of_gc_threads(no_of_gc_threads);
1143 #ifndef PRODUCT
1144 if (G1YoungSurvRateVerbose) {
1145 gclog_or_tty->print_cr("");
1146 _short_lived_surv_rate_group->print();
1147 // do that for any other surv rate groups too
1148 }
1149 #endif // PRODUCT
1151 last_pause_included_initial_mark = during_initial_mark_pause();
1152 if (last_pause_included_initial_mark)
1153 record_concurrent_mark_init_end(0.0);
1155 size_t marking_initiating_used_threshold =
1156 (_g1->capacity() / 100) * InitiatingHeapOccupancyPercent;
1158 if (!_g1->mark_in_progress() && !_last_young_gc) {
1159 assert(!last_pause_included_initial_mark, "invariant");
1160 if (cur_used_bytes > marking_initiating_used_threshold) {
1161 if (cur_used_bytes > _prev_collection_pause_used_at_end_bytes) {
1162 assert(!during_initial_mark_pause(), "we should not see this here");
1164 ergo_verbose3(ErgoConcCycles,
1165 "request concurrent cycle initiation",
1166 ergo_format_reason("occupancy higher than threshold")
1167 ergo_format_byte("occupancy")
1168 ergo_format_byte_perc("threshold"),
1169 cur_used_bytes,
1170 marking_initiating_used_threshold,
1171 (double) InitiatingHeapOccupancyPercent);
1173 // Note: this might have already been set, if during the last
1174 // pause we decided to start a cycle but at the beginning of
1175 // this pause we decided to postpone it. That's OK.
1176 set_initiate_conc_mark_if_possible();
1177 } else {
1178 ergo_verbose2(ErgoConcCycles,
1179 "do not request concurrent cycle initiation",
1180 ergo_format_reason("occupancy lower than previous occupancy")
1181 ergo_format_byte("occupancy")
1182 ergo_format_byte("previous occupancy"),
1183 cur_used_bytes,
1184 _prev_collection_pause_used_at_end_bytes);
1185 }
1186 }
1187 }
1189 _prev_collection_pause_used_at_end_bytes = cur_used_bytes;
1191 _mmu_tracker->add_pause(end_time_sec - elapsed_ms/1000.0,
1192 end_time_sec, false);
1194 // This assert is exempted when we're doing parallel collection pauses,
1195 // because the fragmentation caused by the parallel GC allocation buffers
1196 // can lead to more memory being used during collection than was used
1197 // before. Best leave this out until the fragmentation problem is fixed.
1198 // Pauses in which evacuation failed can also lead to negative
1199 // collections, since no space is reclaimed from a region containing an
1200 // object whose evacuation failed.
1201 // Further, we're now always doing parallel collection. But I'm still
1202 // leaving this here as a placeholder for a more precise assertion later.
1203 // (DLD, 10/05.)
1204 assert((true || parallel) // Always using GC LABs now.
1205 || _g1->evacuation_failed()
1206 || _cur_collection_pause_used_at_start_bytes >= cur_used_bytes,
1207 "Negative collection");
1209 size_t freed_bytes =
1210 _cur_collection_pause_used_at_start_bytes - cur_used_bytes;
1211 size_t surviving_bytes = _collection_set_bytes_used_before - freed_bytes;
1213 double survival_fraction =
1214 (double)surviving_bytes/
1215 (double)_collection_set_bytes_used_before;
1217 // These values are used to update the summary information that is
1218 // displayed when TraceGen0Time is enabled, and are output as part
1219 // of the PrintGCDetails output, in the non-parallel case.
1221 double ext_root_scan_time = avg_value(_par_last_ext_root_scan_times_ms);
1222 double mark_stack_scan_time = avg_value(_par_last_mark_stack_scan_times_ms);
1223 double update_rs_time = avg_value(_par_last_update_rs_times_ms);
1224 double update_rs_processed_buffers =
1225 sum_of_values(_par_last_update_rs_processed_buffers);
1226 double scan_rs_time = avg_value(_par_last_scan_rs_times_ms);
1227 double obj_copy_time = avg_value(_par_last_obj_copy_times_ms);
1228 double termination_time = avg_value(_par_last_termination_times_ms);
1230 double known_time = ext_root_scan_time +
1231 mark_stack_scan_time +
1232 update_rs_time +
1233 scan_rs_time +
1234 obj_copy_time;
1236 double other_time_ms = elapsed_ms;
1238 // Subtract the SATB drain time. It's initialized to zero at the
1239 // start of the pause and is updated during the pause if marking
1240 // is in progress.
1241 other_time_ms -= _cur_satb_drain_time_ms;
1243 if (parallel) {
1244 other_time_ms -= _cur_collection_par_time_ms;
1245 } else {
1246 other_time_ms -= known_time;
1247 }
1249 // Subtract the time taken to clean the card table from the
1250 // current value of "other time"
1251 other_time_ms -= _cur_clear_ct_time_ms;
1253 // Subtract the time spent completing marking in the collection
1254 // set. Note if marking is not in progress during the pause
1255 // the value of _mark_closure_time_ms will be zero.
1256 other_time_ms -= _mark_closure_time_ms;
1258 // TraceGen0Time and TraceGen1Time summary info updating.
1259 _all_pause_times_ms->add(elapsed_ms);
1261 if (update_stats) {
1262 _summary->record_total_time_ms(elapsed_ms);
1263 _summary->record_other_time_ms(other_time_ms);
1265 MainBodySummary* body_summary = _summary->main_body_summary();
1266 assert(body_summary != NULL, "should not be null!");
1268 // This will be non-zero iff marking is currently in progress (i.e.
1269 // _g1->mark_in_progress() == true) and the currrent pause was not
1270 // an initial mark pause. Since the body_summary items are NumberSeqs,
1271 // however, they have to be consistent and updated in lock-step with
1272 // each other. Therefore we unconditionally record the SATB drain
1273 // time - even if it's zero.
1274 body_summary->record_satb_drain_time_ms(_cur_satb_drain_time_ms);
1276 body_summary->record_ext_root_scan_time_ms(ext_root_scan_time);
1277 body_summary->record_mark_stack_scan_time_ms(mark_stack_scan_time);
1278 body_summary->record_update_rs_time_ms(update_rs_time);
1279 body_summary->record_scan_rs_time_ms(scan_rs_time);
1280 body_summary->record_obj_copy_time_ms(obj_copy_time);
1282 if (parallel) {
1283 body_summary->record_parallel_time_ms(_cur_collection_par_time_ms);
1284 body_summary->record_termination_time_ms(termination_time);
1286 double parallel_known_time = known_time + termination_time;
1287 double parallel_other_time = _cur_collection_par_time_ms - parallel_known_time;
1288 body_summary->record_parallel_other_time_ms(parallel_other_time);
1289 }
1291 body_summary->record_mark_closure_time_ms(_mark_closure_time_ms);
1292 body_summary->record_clear_ct_time_ms(_cur_clear_ct_time_ms);
1294 // We exempt parallel collection from this check because Alloc Buffer
1295 // fragmentation can produce negative collections. Same with evac
1296 // failure.
1297 // Further, we're now always doing parallel collection. But I'm still
1298 // leaving this here as a placeholder for a more precise assertion later.
1299 // (DLD, 10/05.
1300 assert((true || parallel)
1301 || _g1->evacuation_failed()
1302 || surviving_bytes <= _collection_set_bytes_used_before,
1303 "Or else negative collection!");
1305 // this is where we update the allocation rate of the application
1306 double app_time_ms =
1307 (_cur_collection_start_sec * 1000.0 - _prev_collection_pause_end_ms);
1308 if (app_time_ms < MIN_TIMER_GRANULARITY) {
1309 // This usually happens due to the timer not having the required
1310 // granularity. Some Linuxes are the usual culprits.
1311 // We'll just set it to something (arbitrarily) small.
1312 app_time_ms = 1.0;
1313 }
1314 // We maintain the invariant that all objects allocated by mutator
1315 // threads will be allocated out of eden regions. So, we can use
1316 // the eden region number allocated since the previous GC to
1317 // calculate the application's allocate rate. The only exception
1318 // to that is humongous objects that are allocated separately. But
1319 // given that humongous object allocations do not really affect
1320 // either the pause's duration nor when the next pause will take
1321 // place we can safely ignore them here.
1322 size_t regions_allocated = eden_cset_region_length();
1323 double alloc_rate_ms = (double) regions_allocated / app_time_ms;
1324 _alloc_rate_ms_seq->add(alloc_rate_ms);
1326 double interval_ms =
1327 (end_time_sec - _recent_prev_end_times_for_all_gcs_sec->oldest()) * 1000.0;
1328 update_recent_gc_times(end_time_sec, elapsed_ms);
1329 _recent_avg_pause_time_ratio = _recent_gc_times_ms->sum()/interval_ms;
1330 if (recent_avg_pause_time_ratio() < 0.0 ||
1331 (recent_avg_pause_time_ratio() - 1.0 > 0.0)) {
1332 #ifndef PRODUCT
1333 // Dump info to allow post-facto debugging
1334 gclog_or_tty->print_cr("recent_avg_pause_time_ratio() out of bounds");
1335 gclog_or_tty->print_cr("-------------------------------------------");
1336 gclog_or_tty->print_cr("Recent GC Times (ms):");
1337 _recent_gc_times_ms->dump();
1338 gclog_or_tty->print_cr("(End Time=%3.3f) Recent GC End Times (s):", end_time_sec);
1339 _recent_prev_end_times_for_all_gcs_sec->dump();
1340 gclog_or_tty->print_cr("GC = %3.3f, Interval = %3.3f, Ratio = %3.3f",
1341 _recent_gc_times_ms->sum(), interval_ms, recent_avg_pause_time_ratio());
1342 // In debug mode, terminate the JVM if the user wants to debug at this point.
1343 assert(!G1FailOnFPError, "Debugging data for CR 6898948 has been dumped above");
1344 #endif // !PRODUCT
1345 // Clip ratio between 0.0 and 1.0, and continue. This will be fixed in
1346 // CR 6902692 by redoing the manner in which the ratio is incrementally computed.
1347 if (_recent_avg_pause_time_ratio < 0.0) {
1348 _recent_avg_pause_time_ratio = 0.0;
1349 } else {
1350 assert(_recent_avg_pause_time_ratio - 1.0 > 0.0, "Ctl-point invariant");
1351 _recent_avg_pause_time_ratio = 1.0;
1352 }
1353 }
1354 }
1356 for (int i = 0; i < _aux_num; ++i) {
1357 if (_cur_aux_times_set[i]) {
1358 _all_aux_times_ms[i].add(_cur_aux_times_ms[i]);
1359 }
1360 }
1362 // PrintGCDetails output
1363 if (PrintGCDetails) {
1364 bool print_marking_info =
1365 _g1->mark_in_progress() && !last_pause_included_initial_mark;
1367 gclog_or_tty->print_cr("%s, %1.8lf secs]",
1368 (last_pause_included_initial_mark) ? " (initial-mark)" : "",
1369 elapsed_ms / 1000.0);
1371 if (print_marking_info) {
1372 print_stats(1, "SATB Drain Time", _cur_satb_drain_time_ms);
1373 }
1375 if (parallel) {
1376 print_stats(1, "Parallel Time", _cur_collection_par_time_ms);
1377 print_par_stats(2, "GC Worker Start", _par_last_gc_worker_start_times_ms);
1378 print_par_stats(2, "Ext Root Scanning", _par_last_ext_root_scan_times_ms);
1379 if (print_marking_info) {
1380 print_par_stats(2, "Mark Stack Scanning", _par_last_mark_stack_scan_times_ms);
1381 }
1382 print_par_stats(2, "Update RS", _par_last_update_rs_times_ms);
1383 print_par_sizes(3, "Processed Buffers", _par_last_update_rs_processed_buffers);
1384 print_par_stats(2, "Scan RS", _par_last_scan_rs_times_ms);
1385 print_par_stats(2, "Object Copy", _par_last_obj_copy_times_ms);
1386 print_par_stats(2, "Termination", _par_last_termination_times_ms);
1387 print_par_sizes(3, "Termination Attempts", _par_last_termination_attempts);
1388 print_par_stats(2, "GC Worker End", _par_last_gc_worker_end_times_ms);
1390 for (int i = 0; i < _parallel_gc_threads; i++) {
1391 _par_last_gc_worker_times_ms[i] = _par_last_gc_worker_end_times_ms[i] - _par_last_gc_worker_start_times_ms[i];
1393 double worker_known_time = _par_last_ext_root_scan_times_ms[i] +
1394 _par_last_mark_stack_scan_times_ms[i] +
1395 _par_last_update_rs_times_ms[i] +
1396 _par_last_scan_rs_times_ms[i] +
1397 _par_last_obj_copy_times_ms[i] +
1398 _par_last_termination_times_ms[i];
1400 _par_last_gc_worker_other_times_ms[i] = _cur_collection_par_time_ms - worker_known_time;
1401 }
1402 print_par_stats(2, "GC Worker", _par_last_gc_worker_times_ms);
1403 print_par_stats(2, "GC Worker Other", _par_last_gc_worker_other_times_ms);
1404 } else {
1405 print_stats(1, "Ext Root Scanning", ext_root_scan_time);
1406 if (print_marking_info) {
1407 print_stats(1, "Mark Stack Scanning", mark_stack_scan_time);
1408 }
1409 print_stats(1, "Update RS", update_rs_time);
1410 print_stats(2, "Processed Buffers", (int)update_rs_processed_buffers);
1411 print_stats(1, "Scan RS", scan_rs_time);
1412 print_stats(1, "Object Copying", obj_copy_time);
1413 }
1414 if (print_marking_info) {
1415 print_stats(1, "Complete CSet Marking", _mark_closure_time_ms);
1416 }
1417 print_stats(1, "Clear CT", _cur_clear_ct_time_ms);
1418 #ifndef PRODUCT
1419 print_stats(1, "Cur Clear CC", _cur_clear_cc_time_ms);
1420 print_stats(1, "Cum Clear CC", _cum_clear_cc_time_ms);
1421 print_stats(1, "Min Clear CC", _min_clear_cc_time_ms);
1422 print_stats(1, "Max Clear CC", _max_clear_cc_time_ms);
1423 if (_num_cc_clears > 0) {
1424 print_stats(1, "Avg Clear CC", _cum_clear_cc_time_ms / ((double)_num_cc_clears));
1425 }
1426 #endif
1427 print_stats(1, "Other", other_time_ms);
1428 print_stats(2, "Choose CSet",
1429 (_recorded_young_cset_choice_time_ms +
1430 _recorded_non_young_cset_choice_time_ms));
1431 print_stats(2, "Ref Proc", _cur_ref_proc_time_ms);
1432 print_stats(2, "Ref Enq", _cur_ref_enq_time_ms);
1433 print_stats(2, "Free CSet",
1434 (_recorded_young_free_cset_time_ms +
1435 _recorded_non_young_free_cset_time_ms));
1437 for (int i = 0; i < _aux_num; ++i) {
1438 if (_cur_aux_times_set[i]) {
1439 char buffer[96];
1440 sprintf(buffer, "Aux%d", i);
1441 print_stats(1, buffer, _cur_aux_times_ms[i]);
1442 }
1443 }
1444 }
1446 // Update the efficiency-since-mark vars.
1447 double proc_ms = elapsed_ms * (double) _parallel_gc_threads;
1448 if (elapsed_ms < MIN_TIMER_GRANULARITY) {
1449 // This usually happens due to the timer not having the required
1450 // granularity. Some Linuxes are the usual culprits.
1451 // We'll just set it to something (arbitrarily) small.
1452 proc_ms = 1.0;
1453 }
1454 double cur_efficiency = (double) freed_bytes / proc_ms;
1456 bool new_in_marking_window = _in_marking_window;
1457 bool new_in_marking_window_im = false;
1458 if (during_initial_mark_pause()) {
1459 new_in_marking_window = true;
1460 new_in_marking_window_im = true;
1461 }
1463 if (_last_young_gc) {
1464 if (!last_pause_included_initial_mark) {
1465 ergo_verbose2(ErgoMixedGCs,
1466 "start mixed GCs",
1467 ergo_format_byte_perc("known garbage"),
1468 _known_garbage_bytes, _known_garbage_ratio * 100.0);
1469 set_gcs_are_young(false);
1470 } else {
1471 ergo_verbose0(ErgoMixedGCs,
1472 "do not start mixed GCs",
1473 ergo_format_reason("concurrent cycle is about to start"));
1474 }
1475 _last_young_gc = false;
1476 }
1478 if (!_last_gc_was_young) {
1479 if (_should_revert_to_young_gcs) {
1480 ergo_verbose2(ErgoMixedGCs,
1481 "end mixed GCs",
1482 ergo_format_reason("mixed GCs end requested")
1483 ergo_format_byte_perc("known garbage"),
1484 _known_garbage_bytes, _known_garbage_ratio * 100.0);
1485 set_gcs_are_young(true);
1486 } else if (_known_garbage_ratio < 0.05) {
1487 ergo_verbose3(ErgoMixedGCs,
1488 "end mixed GCs",
1489 ergo_format_reason("known garbage percent lower than threshold")
1490 ergo_format_byte_perc("known garbage")
1491 ergo_format_perc("threshold"),
1492 _known_garbage_bytes, _known_garbage_ratio * 100.0,
1493 0.05 * 100.0);
1494 set_gcs_are_young(true);
1495 } else if (adaptive_young_list_length() &&
1496 (get_gc_eff_factor() * cur_efficiency < predict_young_gc_eff())) {
1497 ergo_verbose5(ErgoMixedGCs,
1498 "end mixed GCs",
1499 ergo_format_reason("current GC efficiency lower than "
1500 "predicted young GC efficiency")
1501 ergo_format_double("GC efficiency factor")
1502 ergo_format_double("current GC efficiency")
1503 ergo_format_double("predicted young GC efficiency")
1504 ergo_format_byte_perc("known garbage"),
1505 get_gc_eff_factor(), cur_efficiency,
1506 predict_young_gc_eff(),
1507 _known_garbage_bytes, _known_garbage_ratio * 100.0);
1508 set_gcs_are_young(true);
1509 }
1510 }
1511 _should_revert_to_young_gcs = false;
1513 if (_last_gc_was_young && !_during_marking) {
1514 _young_gc_eff_seq->add(cur_efficiency);
1515 }
1517 _short_lived_surv_rate_group->start_adding_regions();
1518 // do that for any other surv rate groupsx
1520 if (update_stats) {
1521 double pause_time_ms = elapsed_ms;
1523 size_t diff = 0;
1524 if (_max_pending_cards >= _pending_cards)
1525 diff = _max_pending_cards - _pending_cards;
1526 _pending_card_diff_seq->add((double) diff);
1528 double cost_per_card_ms = 0.0;
1529 if (_pending_cards > 0) {
1530 cost_per_card_ms = update_rs_time / (double) _pending_cards;
1531 _cost_per_card_ms_seq->add(cost_per_card_ms);
1532 }
1534 size_t cards_scanned = _g1->cards_scanned();
1536 double cost_per_entry_ms = 0.0;
1537 if (cards_scanned > 10) {
1538 cost_per_entry_ms = scan_rs_time / (double) cards_scanned;
1539 if (_last_gc_was_young) {
1540 _cost_per_entry_ms_seq->add(cost_per_entry_ms);
1541 } else {
1542 _mixed_cost_per_entry_ms_seq->add(cost_per_entry_ms);
1543 }
1544 }
1546 if (_max_rs_lengths > 0) {
1547 double cards_per_entry_ratio =
1548 (double) cards_scanned / (double) _max_rs_lengths;
1549 if (_last_gc_was_young) {
1550 _young_cards_per_entry_ratio_seq->add(cards_per_entry_ratio);
1551 } else {
1552 _mixed_cards_per_entry_ratio_seq->add(cards_per_entry_ratio);
1553 }
1554 }
1556 // This is defensive. For a while _max_rs_lengths could get
1557 // smaller than _recorded_rs_lengths which was causing
1558 // rs_length_diff to get very large and mess up the RSet length
1559 // predictions. The reason was unsafe concurrent updates to the
1560 // _inc_cset_recorded_rs_lengths field which the code below guards
1561 // against (see CR 7118202). This bug has now been fixed (see CR
1562 // 7119027). However, I'm still worried that
1563 // _inc_cset_recorded_rs_lengths might still end up somewhat
1564 // inaccurate. The concurrent refinement thread calculates an
1565 // RSet's length concurrently with other CR threads updating it
1566 // which might cause it to calculate the length incorrectly (if,
1567 // say, it's in mid-coarsening). So I'll leave in the defensive
1568 // conditional below just in case.
1569 size_t rs_length_diff = 0;
1570 if (_max_rs_lengths > _recorded_rs_lengths) {
1571 rs_length_diff = _max_rs_lengths - _recorded_rs_lengths;
1572 }
1573 _rs_length_diff_seq->add((double) rs_length_diff);
1575 size_t copied_bytes = surviving_bytes;
1576 double cost_per_byte_ms = 0.0;
1577 if (copied_bytes > 0) {
1578 cost_per_byte_ms = obj_copy_time / (double) copied_bytes;
1579 if (_in_marking_window) {
1580 _cost_per_byte_ms_during_cm_seq->add(cost_per_byte_ms);
1581 } else {
1582 _cost_per_byte_ms_seq->add(cost_per_byte_ms);
1583 }
1584 }
1586 double all_other_time_ms = pause_time_ms -
1587 (update_rs_time + scan_rs_time + obj_copy_time +
1588 _mark_closure_time_ms + termination_time);
1590 double young_other_time_ms = 0.0;
1591 if (young_cset_region_length() > 0) {
1592 young_other_time_ms =
1593 _recorded_young_cset_choice_time_ms +
1594 _recorded_young_free_cset_time_ms;
1595 _young_other_cost_per_region_ms_seq->add(young_other_time_ms /
1596 (double) young_cset_region_length());
1597 }
1598 double non_young_other_time_ms = 0.0;
1599 if (old_cset_region_length() > 0) {
1600 non_young_other_time_ms =
1601 _recorded_non_young_cset_choice_time_ms +
1602 _recorded_non_young_free_cset_time_ms;
1604 _non_young_other_cost_per_region_ms_seq->add(non_young_other_time_ms /
1605 (double) old_cset_region_length());
1606 }
1608 double constant_other_time_ms = all_other_time_ms -
1609 (young_other_time_ms + non_young_other_time_ms);
1610 _constant_other_time_ms_seq->add(constant_other_time_ms);
1612 double survival_ratio = 0.0;
1613 if (_bytes_in_collection_set_before_gc > 0) {
1614 survival_ratio = (double) _bytes_copied_during_gc /
1615 (double) _bytes_in_collection_set_before_gc;
1616 }
1618 _pending_cards_seq->add((double) _pending_cards);
1619 _rs_lengths_seq->add((double) _max_rs_lengths);
1621 double expensive_region_limit_ms =
1622 (double) MaxGCPauseMillis - predict_constant_other_time_ms();
1623 if (expensive_region_limit_ms < 0.0) {
1624 // this means that the other time was predicted to be longer than
1625 // than the max pause time
1626 expensive_region_limit_ms = (double) MaxGCPauseMillis;
1627 }
1628 _expensive_region_limit_ms = expensive_region_limit_ms;
1629 }
1631 _in_marking_window = new_in_marking_window;
1632 _in_marking_window_im = new_in_marking_window_im;
1633 _free_regions_at_end_of_collection = _g1->free_regions();
1634 update_young_list_target_length();
1636 // Note that _mmu_tracker->max_gc_time() returns the time in seconds.
1637 double update_rs_time_goal_ms = _mmu_tracker->max_gc_time() * MILLIUNITS * G1RSetUpdatingPauseTimePercent / 100.0;
1638 adjust_concurrent_refinement(update_rs_time, update_rs_processed_buffers, update_rs_time_goal_ms);
1640 assert(assertMarkedBytesDataOK(), "Marked regions not OK at pause end.");
1641 }
1643 #define EXT_SIZE_FORMAT "%d%s"
1644 #define EXT_SIZE_PARAMS(bytes) \
1645 byte_size_in_proper_unit((bytes)), \
1646 proper_unit_for_byte_size((bytes))
1648 void G1CollectorPolicy::print_heap_transition() {
1649 if (PrintGCDetails) {
1650 YoungList* young_list = _g1->young_list();
1651 size_t eden_bytes = young_list->eden_used_bytes();
1652 size_t survivor_bytes = young_list->survivor_used_bytes();
1653 size_t used_before_gc = _cur_collection_pause_used_at_start_bytes;
1654 size_t used = _g1->used();
1655 size_t capacity = _g1->capacity();
1656 size_t eden_capacity =
1657 (_young_list_target_length * HeapRegion::GrainBytes) - survivor_bytes;
1659 gclog_or_tty->print_cr(
1660 " [Eden: "EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")->"EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT") "
1661 "Survivors: "EXT_SIZE_FORMAT"->"EXT_SIZE_FORMAT" "
1662 "Heap: "EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")->"
1663 EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")]",
1664 EXT_SIZE_PARAMS(_eden_bytes_before_gc),
1665 EXT_SIZE_PARAMS(_prev_eden_capacity),
1666 EXT_SIZE_PARAMS(eden_bytes),
1667 EXT_SIZE_PARAMS(eden_capacity),
1668 EXT_SIZE_PARAMS(_survivor_bytes_before_gc),
1669 EXT_SIZE_PARAMS(survivor_bytes),
1670 EXT_SIZE_PARAMS(used_before_gc),
1671 EXT_SIZE_PARAMS(_capacity_before_gc),
1672 EXT_SIZE_PARAMS(used),
1673 EXT_SIZE_PARAMS(capacity));
1675 _prev_eden_capacity = eden_capacity;
1676 } else if (PrintGC) {
1677 _g1->print_size_transition(gclog_or_tty,
1678 _cur_collection_pause_used_at_start_bytes,
1679 _g1->used(), _g1->capacity());
1680 }
1681 }
1683 void G1CollectorPolicy::adjust_concurrent_refinement(double update_rs_time,
1684 double update_rs_processed_buffers,
1685 double goal_ms) {
1686 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
1687 ConcurrentG1Refine *cg1r = G1CollectedHeap::heap()->concurrent_g1_refine();
1689 if (G1UseAdaptiveConcRefinement) {
1690 const int k_gy = 3, k_gr = 6;
1691 const double inc_k = 1.1, dec_k = 0.9;
1693 int g = cg1r->green_zone();
1694 if (update_rs_time > goal_ms) {
1695 g = (int)(g * dec_k); // Can become 0, that's OK. That would mean a mutator-only processing.
1696 } else {
1697 if (update_rs_time < goal_ms && update_rs_processed_buffers > g) {
1698 g = (int)MAX2(g * inc_k, g + 1.0);
1699 }
1700 }
1701 // Change the refinement threads params
1702 cg1r->set_green_zone(g);
1703 cg1r->set_yellow_zone(g * k_gy);
1704 cg1r->set_red_zone(g * k_gr);
1705 cg1r->reinitialize_threads();
1707 int processing_threshold_delta = MAX2((int)(cg1r->green_zone() * sigma()), 1);
1708 int processing_threshold = MIN2(cg1r->green_zone() + processing_threshold_delta,
1709 cg1r->yellow_zone());
1710 // Change the barrier params
1711 dcqs.set_process_completed_threshold(processing_threshold);
1712 dcqs.set_max_completed_queue(cg1r->red_zone());
1713 }
1715 int curr_queue_size = dcqs.completed_buffers_num();
1716 if (curr_queue_size >= cg1r->yellow_zone()) {
1717 dcqs.set_completed_queue_padding(curr_queue_size);
1718 } else {
1719 dcqs.set_completed_queue_padding(0);
1720 }
1721 dcqs.notify_if_necessary();
1722 }
1724 double
1725 G1CollectorPolicy::
1726 predict_young_collection_elapsed_time_ms(size_t adjustment) {
1727 guarantee( adjustment == 0 || adjustment == 1, "invariant" );
1729 G1CollectedHeap* g1h = G1CollectedHeap::heap();
1730 size_t young_num = g1h->young_list()->length();
1731 if (young_num == 0)
1732 return 0.0;
1734 young_num += adjustment;
1735 size_t pending_cards = predict_pending_cards();
1736 size_t rs_lengths = g1h->young_list()->sampled_rs_lengths() +
1737 predict_rs_length_diff();
1738 size_t card_num;
1739 if (gcs_are_young()) {
1740 card_num = predict_young_card_num(rs_lengths);
1741 } else {
1742 card_num = predict_non_young_card_num(rs_lengths);
1743 }
1744 size_t young_byte_size = young_num * HeapRegion::GrainBytes;
1745 double accum_yg_surv_rate =
1746 _short_lived_surv_rate_group->accum_surv_rate(adjustment);
1748 size_t bytes_to_copy =
1749 (size_t) (accum_yg_surv_rate * (double) HeapRegion::GrainBytes);
1751 return
1752 predict_rs_update_time_ms(pending_cards) +
1753 predict_rs_scan_time_ms(card_num) +
1754 predict_object_copy_time_ms(bytes_to_copy) +
1755 predict_young_other_time_ms(young_num) +
1756 predict_constant_other_time_ms();
1757 }
1759 double
1760 G1CollectorPolicy::predict_base_elapsed_time_ms(size_t pending_cards) {
1761 size_t rs_length = predict_rs_length_diff();
1762 size_t card_num;
1763 if (gcs_are_young()) {
1764 card_num = predict_young_card_num(rs_length);
1765 } else {
1766 card_num = predict_non_young_card_num(rs_length);
1767 }
1768 return predict_base_elapsed_time_ms(pending_cards, card_num);
1769 }
1771 double
1772 G1CollectorPolicy::predict_base_elapsed_time_ms(size_t pending_cards,
1773 size_t scanned_cards) {
1774 return
1775 predict_rs_update_time_ms(pending_cards) +
1776 predict_rs_scan_time_ms(scanned_cards) +
1777 predict_constant_other_time_ms();
1778 }
1780 double
1781 G1CollectorPolicy::predict_region_elapsed_time_ms(HeapRegion* hr,
1782 bool young) {
1783 size_t rs_length = hr->rem_set()->occupied();
1784 size_t card_num;
1785 if (gcs_are_young()) {
1786 card_num = predict_young_card_num(rs_length);
1787 } else {
1788 card_num = predict_non_young_card_num(rs_length);
1789 }
1790 size_t bytes_to_copy = predict_bytes_to_copy(hr);
1792 double region_elapsed_time_ms =
1793 predict_rs_scan_time_ms(card_num) +
1794 predict_object_copy_time_ms(bytes_to_copy);
1796 if (young)
1797 region_elapsed_time_ms += predict_young_other_time_ms(1);
1798 else
1799 region_elapsed_time_ms += predict_non_young_other_time_ms(1);
1801 return region_elapsed_time_ms;
1802 }
1804 size_t
1805 G1CollectorPolicy::predict_bytes_to_copy(HeapRegion* hr) {
1806 size_t bytes_to_copy;
1807 if (hr->is_marked())
1808 bytes_to_copy = hr->max_live_bytes();
1809 else {
1810 guarantee( hr->is_young() && hr->age_in_surv_rate_group() != -1,
1811 "invariant" );
1812 int age = hr->age_in_surv_rate_group();
1813 double yg_surv_rate = predict_yg_surv_rate(age, hr->surv_rate_group());
1814 bytes_to_copy = (size_t) ((double) hr->used() * yg_surv_rate);
1815 }
1817 return bytes_to_copy;
1818 }
1820 void
1821 G1CollectorPolicy::init_cset_region_lengths(size_t eden_cset_region_length,
1822 size_t survivor_cset_region_length) {
1823 _eden_cset_region_length = eden_cset_region_length;
1824 _survivor_cset_region_length = survivor_cset_region_length;
1825 _old_cset_region_length = 0;
1826 }
1828 void G1CollectorPolicy::set_recorded_rs_lengths(size_t rs_lengths) {
1829 _recorded_rs_lengths = rs_lengths;
1830 }
1832 void G1CollectorPolicy::check_if_region_is_too_expensive(double
1833 predicted_time_ms) {
1834 // I don't think we need to do this when in young GC mode since
1835 // marking will be initiated next time we hit the soft limit anyway...
1836 if (predicted_time_ms > _expensive_region_limit_ms) {
1837 ergo_verbose2(ErgoMixedGCs,
1838 "request mixed GCs end",
1839 ergo_format_reason("predicted region time higher than threshold")
1840 ergo_format_ms("predicted region time")
1841 ergo_format_ms("threshold"),
1842 predicted_time_ms, _expensive_region_limit_ms);
1843 // no point in doing another mixed GC
1844 _should_revert_to_young_gcs = true;
1845 }
1846 }
1848 void G1CollectorPolicy::update_recent_gc_times(double end_time_sec,
1849 double elapsed_ms) {
1850 _recent_gc_times_ms->add(elapsed_ms);
1851 _recent_prev_end_times_for_all_gcs_sec->add(end_time_sec);
1852 _prev_collection_pause_end_ms = end_time_sec * 1000.0;
1853 }
1855 size_t G1CollectorPolicy::expansion_amount() {
1856 double recent_gc_overhead = recent_avg_pause_time_ratio() * 100.0;
1857 double threshold = _gc_overhead_perc;
1858 if (recent_gc_overhead > threshold) {
1859 // We will double the existing space, or take
1860 // G1ExpandByPercentOfAvailable % of the available expansion
1861 // space, whichever is smaller, bounded below by a minimum
1862 // expansion (unless that's all that's left.)
1863 const size_t min_expand_bytes = 1*M;
1864 size_t reserved_bytes = _g1->max_capacity();
1865 size_t committed_bytes = _g1->capacity();
1866 size_t uncommitted_bytes = reserved_bytes - committed_bytes;
1867 size_t expand_bytes;
1868 size_t expand_bytes_via_pct =
1869 uncommitted_bytes * G1ExpandByPercentOfAvailable / 100;
1870 expand_bytes = MIN2(expand_bytes_via_pct, committed_bytes);
1871 expand_bytes = MAX2(expand_bytes, min_expand_bytes);
1872 expand_bytes = MIN2(expand_bytes, uncommitted_bytes);
1874 ergo_verbose5(ErgoHeapSizing,
1875 "attempt heap expansion",
1876 ergo_format_reason("recent GC overhead higher than "
1877 "threshold after GC")
1878 ergo_format_perc("recent GC overhead")
1879 ergo_format_perc("threshold")
1880 ergo_format_byte("uncommitted")
1881 ergo_format_byte_perc("calculated expansion amount"),
1882 recent_gc_overhead, threshold,
1883 uncommitted_bytes,
1884 expand_bytes_via_pct, (double) G1ExpandByPercentOfAvailable);
1886 return expand_bytes;
1887 } else {
1888 return 0;
1889 }
1890 }
1892 class CountCSClosure: public HeapRegionClosure {
1893 G1CollectorPolicy* _g1_policy;
1894 public:
1895 CountCSClosure(G1CollectorPolicy* g1_policy) :
1896 _g1_policy(g1_policy) {}
1897 bool doHeapRegion(HeapRegion* r) {
1898 _g1_policy->_bytes_in_collection_set_before_gc += r->used();
1899 return false;
1900 }
1901 };
1903 void G1CollectorPolicy::count_CS_bytes_used() {
1904 CountCSClosure cs_closure(this);
1905 _g1->collection_set_iterate(&cs_closure);
1906 }
1908 void G1CollectorPolicy::print_summary(int level,
1909 const char* str,
1910 NumberSeq* seq) const {
1911 double sum = seq->sum();
1912 LineBuffer(level + 1).append_and_print_cr("%-24s = %8.2lf s (avg = %8.2lf ms)",
1913 str, sum / 1000.0, seq->avg());
1914 }
1916 void G1CollectorPolicy::print_summary_sd(int level,
1917 const char* str,
1918 NumberSeq* seq) const {
1919 print_summary(level, str, seq);
1920 LineBuffer(level + 6).append_and_print_cr("(num = %5d, std dev = %8.2lf ms, max = %8.2lf ms)",
1921 seq->num(), seq->sd(), seq->maximum());
1922 }
1924 void G1CollectorPolicy::check_other_times(int level,
1925 NumberSeq* other_times_ms,
1926 NumberSeq* calc_other_times_ms) const {
1927 bool should_print = false;
1928 LineBuffer buf(level + 2);
1930 double max_sum = MAX2(fabs(other_times_ms->sum()),
1931 fabs(calc_other_times_ms->sum()));
1932 double min_sum = MIN2(fabs(other_times_ms->sum()),
1933 fabs(calc_other_times_ms->sum()));
1934 double sum_ratio = max_sum / min_sum;
1935 if (sum_ratio > 1.1) {
1936 should_print = true;
1937 buf.append_and_print_cr("## CALCULATED OTHER SUM DOESN'T MATCH RECORDED ###");
1938 }
1940 double max_avg = MAX2(fabs(other_times_ms->avg()),
1941 fabs(calc_other_times_ms->avg()));
1942 double min_avg = MIN2(fabs(other_times_ms->avg()),
1943 fabs(calc_other_times_ms->avg()));
1944 double avg_ratio = max_avg / min_avg;
1945 if (avg_ratio > 1.1) {
1946 should_print = true;
1947 buf.append_and_print_cr("## CALCULATED OTHER AVG DOESN'T MATCH RECORDED ###");
1948 }
1950 if (other_times_ms->sum() < -0.01) {
1951 buf.append_and_print_cr("## RECORDED OTHER SUM IS NEGATIVE ###");
1952 }
1954 if (other_times_ms->avg() < -0.01) {
1955 buf.append_and_print_cr("## RECORDED OTHER AVG IS NEGATIVE ###");
1956 }
1958 if (calc_other_times_ms->sum() < -0.01) {
1959 should_print = true;
1960 buf.append_and_print_cr("## CALCULATED OTHER SUM IS NEGATIVE ###");
1961 }
1963 if (calc_other_times_ms->avg() < -0.01) {
1964 should_print = true;
1965 buf.append_and_print_cr("## CALCULATED OTHER AVG IS NEGATIVE ###");
1966 }
1968 if (should_print)
1969 print_summary(level, "Other(Calc)", calc_other_times_ms);
1970 }
1972 void G1CollectorPolicy::print_summary(PauseSummary* summary) const {
1973 bool parallel = G1CollectedHeap::use_parallel_gc_threads();
1974 MainBodySummary* body_summary = summary->main_body_summary();
1975 if (summary->get_total_seq()->num() > 0) {
1976 print_summary_sd(0, "Evacuation Pauses", summary->get_total_seq());
1977 if (body_summary != NULL) {
1978 print_summary(1, "SATB Drain", body_summary->get_satb_drain_seq());
1979 if (parallel) {
1980 print_summary(1, "Parallel Time", body_summary->get_parallel_seq());
1981 print_summary(2, "Ext Root Scanning", body_summary->get_ext_root_scan_seq());
1982 print_summary(2, "Mark Stack Scanning", body_summary->get_mark_stack_scan_seq());
1983 print_summary(2, "Update RS", body_summary->get_update_rs_seq());
1984 print_summary(2, "Scan RS", body_summary->get_scan_rs_seq());
1985 print_summary(2, "Object Copy", body_summary->get_obj_copy_seq());
1986 print_summary(2, "Termination", body_summary->get_termination_seq());
1987 print_summary(2, "Parallel Other", body_summary->get_parallel_other_seq());
1988 {
1989 NumberSeq* other_parts[] = {
1990 body_summary->get_ext_root_scan_seq(),
1991 body_summary->get_mark_stack_scan_seq(),
1992 body_summary->get_update_rs_seq(),
1993 body_summary->get_scan_rs_seq(),
1994 body_summary->get_obj_copy_seq(),
1995 body_summary->get_termination_seq()
1996 };
1997 NumberSeq calc_other_times_ms(body_summary->get_parallel_seq(),
1998 6, other_parts);
1999 check_other_times(2, body_summary->get_parallel_other_seq(),
2000 &calc_other_times_ms);
2001 }
2002 } else {
2003 print_summary(1, "Ext Root Scanning", body_summary->get_ext_root_scan_seq());
2004 print_summary(1, "Mark Stack Scanning", body_summary->get_mark_stack_scan_seq());
2005 print_summary(1, "Update RS", body_summary->get_update_rs_seq());
2006 print_summary(1, "Scan RS", body_summary->get_scan_rs_seq());
2007 print_summary(1, "Object Copy", body_summary->get_obj_copy_seq());
2008 }
2009 }
2010 print_summary(1, "Mark Closure", body_summary->get_mark_closure_seq());
2011 print_summary(1, "Clear CT", body_summary->get_clear_ct_seq());
2012 print_summary(1, "Other", summary->get_other_seq());
2013 {
2014 if (body_summary != NULL) {
2015 NumberSeq calc_other_times_ms;
2016 if (parallel) {
2017 // parallel
2018 NumberSeq* other_parts[] = {
2019 body_summary->get_satb_drain_seq(),
2020 body_summary->get_parallel_seq(),
2021 body_summary->get_clear_ct_seq()
2022 };
2023 calc_other_times_ms = NumberSeq(summary->get_total_seq(),
2024 3, other_parts);
2025 } else {
2026 // serial
2027 NumberSeq* other_parts[] = {
2028 body_summary->get_satb_drain_seq(),
2029 body_summary->get_update_rs_seq(),
2030 body_summary->get_ext_root_scan_seq(),
2031 body_summary->get_mark_stack_scan_seq(),
2032 body_summary->get_scan_rs_seq(),
2033 body_summary->get_obj_copy_seq()
2034 };
2035 calc_other_times_ms = NumberSeq(summary->get_total_seq(),
2036 6, other_parts);
2037 }
2038 check_other_times(1, summary->get_other_seq(), &calc_other_times_ms);
2039 }
2040 }
2041 } else {
2042 LineBuffer(1).append_and_print_cr("none");
2043 }
2044 LineBuffer(0).append_and_print_cr("");
2045 }
2047 void G1CollectorPolicy::print_tracing_info() const {
2048 if (TraceGen0Time) {
2049 gclog_or_tty->print_cr("ALL PAUSES");
2050 print_summary_sd(0, "Total", _all_pause_times_ms);
2051 gclog_or_tty->print_cr("");
2052 gclog_or_tty->print_cr("");
2053 gclog_or_tty->print_cr(" Young GC Pauses: %8d", _young_pause_num);
2054 gclog_or_tty->print_cr(" Mixed GC Pauses: %8d", _mixed_pause_num);
2055 gclog_or_tty->print_cr("");
2057 gclog_or_tty->print_cr("EVACUATION PAUSES");
2058 print_summary(_summary);
2060 gclog_or_tty->print_cr("MISC");
2061 print_summary_sd(0, "Stop World", _all_stop_world_times_ms);
2062 print_summary_sd(0, "Yields", _all_yield_times_ms);
2063 for (int i = 0; i < _aux_num; ++i) {
2064 if (_all_aux_times_ms[i].num() > 0) {
2065 char buffer[96];
2066 sprintf(buffer, "Aux%d", i);
2067 print_summary_sd(0, buffer, &_all_aux_times_ms[i]);
2068 }
2069 }
2070 }
2071 if (TraceGen1Time) {
2072 if (_all_full_gc_times_ms->num() > 0) {
2073 gclog_or_tty->print("\n%4d full_gcs: total time = %8.2f s",
2074 _all_full_gc_times_ms->num(),
2075 _all_full_gc_times_ms->sum() / 1000.0);
2076 gclog_or_tty->print_cr(" (avg = %8.2fms).", _all_full_gc_times_ms->avg());
2077 gclog_or_tty->print_cr(" [std. dev = %8.2f ms, max = %8.2f ms]",
2078 _all_full_gc_times_ms->sd(),
2079 _all_full_gc_times_ms->maximum());
2080 }
2081 }
2082 }
2084 void G1CollectorPolicy::print_yg_surv_rate_info() const {
2085 #ifndef PRODUCT
2086 _short_lived_surv_rate_group->print_surv_rate_summary();
2087 // add this call for any other surv rate groups
2088 #endif // PRODUCT
2089 }
2091 #ifndef PRODUCT
2092 // for debugging, bit of a hack...
2093 static char*
2094 region_num_to_mbs(int length) {
2095 static char buffer[64];
2096 double bytes = (double) (length * HeapRegion::GrainBytes);
2097 double mbs = bytes / (double) (1024 * 1024);
2098 sprintf(buffer, "%7.2lfMB", mbs);
2099 return buffer;
2100 }
2101 #endif // PRODUCT
2103 size_t G1CollectorPolicy::max_regions(int purpose) {
2104 switch (purpose) {
2105 case GCAllocForSurvived:
2106 return _max_survivor_regions;
2107 case GCAllocForTenured:
2108 return REGIONS_UNLIMITED;
2109 default:
2110 ShouldNotReachHere();
2111 return REGIONS_UNLIMITED;
2112 };
2113 }
2115 void G1CollectorPolicy::update_max_gc_locker_expansion() {
2116 size_t expansion_region_num = 0;
2117 if (GCLockerEdenExpansionPercent > 0) {
2118 double perc = (double) GCLockerEdenExpansionPercent / 100.0;
2119 double expansion_region_num_d = perc * (double) _young_list_target_length;
2120 // We use ceiling so that if expansion_region_num_d is > 0.0 (but
2121 // less than 1.0) we'll get 1.
2122 expansion_region_num = (size_t) ceil(expansion_region_num_d);
2123 } else {
2124 assert(expansion_region_num == 0, "sanity");
2125 }
2126 _young_list_max_length = _young_list_target_length + expansion_region_num;
2127 assert(_young_list_target_length <= _young_list_max_length, "post-condition");
2128 }
2130 // Calculates survivor space parameters.
2131 void G1CollectorPolicy::update_survivors_policy() {
2132 double max_survivor_regions_d =
2133 (double) _young_list_target_length / (double) SurvivorRatio;
2134 // We use ceiling so that if max_survivor_regions_d is > 0.0 (but
2135 // smaller than 1.0) we'll get 1.
2136 _max_survivor_regions = (size_t) ceil(max_survivor_regions_d);
2138 _tenuring_threshold = _survivors_age_table.compute_tenuring_threshold(
2139 HeapRegion::GrainWords * _max_survivor_regions);
2140 }
2142 #ifndef PRODUCT
2143 class HRSortIndexIsOKClosure: public HeapRegionClosure {
2144 CollectionSetChooser* _chooser;
2145 public:
2146 HRSortIndexIsOKClosure(CollectionSetChooser* chooser) :
2147 _chooser(chooser) {}
2149 bool doHeapRegion(HeapRegion* r) {
2150 if (!r->continuesHumongous()) {
2151 assert(_chooser->regionProperlyOrdered(r), "Ought to be.");
2152 }
2153 return false;
2154 }
2155 };
2157 bool G1CollectorPolicy::assertMarkedBytesDataOK() {
2158 HRSortIndexIsOKClosure cl(_collectionSetChooser);
2159 _g1->heap_region_iterate(&cl);
2160 return true;
2161 }
2162 #endif
2164 bool G1CollectorPolicy::force_initial_mark_if_outside_cycle(
2165 GCCause::Cause gc_cause) {
2166 bool during_cycle = _g1->concurrent_mark()->cmThread()->during_cycle();
2167 if (!during_cycle) {
2168 ergo_verbose1(ErgoConcCycles,
2169 "request concurrent cycle initiation",
2170 ergo_format_reason("requested by GC cause")
2171 ergo_format_str("GC cause"),
2172 GCCause::to_string(gc_cause));
2173 set_initiate_conc_mark_if_possible();
2174 return true;
2175 } else {
2176 ergo_verbose1(ErgoConcCycles,
2177 "do not request concurrent cycle initiation",
2178 ergo_format_reason("concurrent cycle already in progress")
2179 ergo_format_str("GC cause"),
2180 GCCause::to_string(gc_cause));
2181 return false;
2182 }
2183 }
2185 void
2186 G1CollectorPolicy::decide_on_conc_mark_initiation() {
2187 // We are about to decide on whether this pause will be an
2188 // initial-mark pause.
2190 // First, during_initial_mark_pause() should not be already set. We
2191 // will set it here if we have to. However, it should be cleared by
2192 // the end of the pause (it's only set for the duration of an
2193 // initial-mark pause).
2194 assert(!during_initial_mark_pause(), "pre-condition");
2196 if (initiate_conc_mark_if_possible()) {
2197 // We had noticed on a previous pause that the heap occupancy has
2198 // gone over the initiating threshold and we should start a
2199 // concurrent marking cycle. So we might initiate one.
2201 bool during_cycle = _g1->concurrent_mark()->cmThread()->during_cycle();
2202 if (!during_cycle) {
2203 // The concurrent marking thread is not "during a cycle", i.e.,
2204 // it has completed the last one. So we can go ahead and
2205 // initiate a new cycle.
2207 set_during_initial_mark_pause();
2208 // We do not allow mixed GCs during marking.
2209 if (!gcs_are_young()) {
2210 set_gcs_are_young(true);
2211 ergo_verbose0(ErgoMixedGCs,
2212 "end mixed GCs",
2213 ergo_format_reason("concurrent cycle is about to start"));
2214 }
2216 // And we can now clear initiate_conc_mark_if_possible() as
2217 // we've already acted on it.
2218 clear_initiate_conc_mark_if_possible();
2220 ergo_verbose0(ErgoConcCycles,
2221 "initiate concurrent cycle",
2222 ergo_format_reason("concurrent cycle initiation requested"));
2223 } else {
2224 // The concurrent marking thread is still finishing up the
2225 // previous cycle. If we start one right now the two cycles
2226 // overlap. In particular, the concurrent marking thread might
2227 // be in the process of clearing the next marking bitmap (which
2228 // we will use for the next cycle if we start one). Starting a
2229 // cycle now will be bad given that parts of the marking
2230 // information might get cleared by the marking thread. And we
2231 // cannot wait for the marking thread to finish the cycle as it
2232 // periodically yields while clearing the next marking bitmap
2233 // and, if it's in a yield point, it's waiting for us to
2234 // finish. So, at this point we will not start a cycle and we'll
2235 // let the concurrent marking thread complete the last one.
2236 ergo_verbose0(ErgoConcCycles,
2237 "do not initiate concurrent cycle",
2238 ergo_format_reason("concurrent cycle already in progress"));
2239 }
2240 }
2241 }
2243 class KnownGarbageClosure: public HeapRegionClosure {
2244 CollectionSetChooser* _hrSorted;
2246 public:
2247 KnownGarbageClosure(CollectionSetChooser* hrSorted) :
2248 _hrSorted(hrSorted)
2249 {}
2251 bool doHeapRegion(HeapRegion* r) {
2252 // We only include humongous regions in collection
2253 // sets when concurrent mark shows that their contained object is
2254 // unreachable.
2256 // Do we have any marking information for this region?
2257 if (r->is_marked()) {
2258 // We don't include humongous regions in collection
2259 // sets because we collect them immediately at the end of a marking
2260 // cycle. We also don't include young regions because we *must*
2261 // include them in the next collection pause.
2262 if (!r->isHumongous() && !r->is_young()) {
2263 _hrSorted->addMarkedHeapRegion(r);
2264 }
2265 }
2266 return false;
2267 }
2268 };
2270 class ParKnownGarbageHRClosure: public HeapRegionClosure {
2271 CollectionSetChooser* _hrSorted;
2272 jint _marked_regions_added;
2273 jint _chunk_size;
2274 jint _cur_chunk_idx;
2275 jint _cur_chunk_end; // Cur chunk [_cur_chunk_idx, _cur_chunk_end)
2276 int _worker;
2277 int _invokes;
2279 void get_new_chunk() {
2280 _cur_chunk_idx = _hrSorted->getParMarkedHeapRegionChunk(_chunk_size);
2281 _cur_chunk_end = _cur_chunk_idx + _chunk_size;
2282 }
2283 void add_region(HeapRegion* r) {
2284 if (_cur_chunk_idx == _cur_chunk_end) {
2285 get_new_chunk();
2286 }
2287 assert(_cur_chunk_idx < _cur_chunk_end, "postcondition");
2288 _hrSorted->setMarkedHeapRegion(_cur_chunk_idx, r);
2289 _marked_regions_added++;
2290 _cur_chunk_idx++;
2291 }
2293 public:
2294 ParKnownGarbageHRClosure(CollectionSetChooser* hrSorted,
2295 jint chunk_size,
2296 int worker) :
2297 _hrSorted(hrSorted), _chunk_size(chunk_size), _worker(worker),
2298 _marked_regions_added(0), _cur_chunk_idx(0), _cur_chunk_end(0),
2299 _invokes(0)
2300 {}
2302 bool doHeapRegion(HeapRegion* r) {
2303 // We only include humongous regions in collection
2304 // sets when concurrent mark shows that their contained object is
2305 // unreachable.
2306 _invokes++;
2308 // Do we have any marking information for this region?
2309 if (r->is_marked()) {
2310 // We don't include humongous regions in collection
2311 // sets because we collect them immediately at the end of a marking
2312 // cycle.
2313 // We also do not include young regions in collection sets
2314 if (!r->isHumongous() && !r->is_young()) {
2315 add_region(r);
2316 }
2317 }
2318 return false;
2319 }
2320 jint marked_regions_added() { return _marked_regions_added; }
2321 int invokes() { return _invokes; }
2322 };
2324 class ParKnownGarbageTask: public AbstractGangTask {
2325 CollectionSetChooser* _hrSorted;
2326 jint _chunk_size;
2327 G1CollectedHeap* _g1;
2328 public:
2329 ParKnownGarbageTask(CollectionSetChooser* hrSorted, jint chunk_size) :
2330 AbstractGangTask("ParKnownGarbageTask"),
2331 _hrSorted(hrSorted), _chunk_size(chunk_size),
2332 _g1(G1CollectedHeap::heap())
2333 {}
2335 void work(uint worker_id) {
2336 ParKnownGarbageHRClosure parKnownGarbageCl(_hrSorted,
2337 _chunk_size,
2338 worker_id);
2339 // Back to zero for the claim value.
2340 _g1->heap_region_par_iterate_chunked(&parKnownGarbageCl, worker_id,
2341 _g1->workers()->active_workers(),
2342 HeapRegion::InitialClaimValue);
2343 jint regions_added = parKnownGarbageCl.marked_regions_added();
2344 _hrSorted->incNumMarkedHeapRegions(regions_added);
2345 if (G1PrintParCleanupStats) {
2346 gclog_or_tty->print_cr(" Thread %d called %d times, added %d regions to list.",
2347 worker_id, parKnownGarbageCl.invokes(), regions_added);
2348 }
2349 }
2350 };
2352 void
2353 G1CollectorPolicy::record_concurrent_mark_cleanup_end(int no_of_gc_threads) {
2354 double start_sec;
2355 if (G1PrintParCleanupStats) {
2356 start_sec = os::elapsedTime();
2357 }
2359 _collectionSetChooser->clearMarkedHeapRegions();
2360 double clear_marked_end_sec;
2361 if (G1PrintParCleanupStats) {
2362 clear_marked_end_sec = os::elapsedTime();
2363 gclog_or_tty->print_cr(" clear marked regions: %8.3f ms.",
2364 (clear_marked_end_sec - start_sec) * 1000.0);
2365 }
2367 if (G1CollectedHeap::use_parallel_gc_threads()) {
2368 const size_t OverpartitionFactor = 4;
2369 size_t WorkUnit;
2370 // The use of MinChunkSize = 8 in the original code
2371 // causes some assertion failures when the total number of
2372 // region is less than 8. The code here tries to fix that.
2373 // Should the original code also be fixed?
2374 if (no_of_gc_threads > 0) {
2375 const size_t MinWorkUnit =
2376 MAX2(_g1->n_regions() / no_of_gc_threads, (size_t) 1U);
2377 WorkUnit =
2378 MAX2(_g1->n_regions() / (no_of_gc_threads * OverpartitionFactor),
2379 MinWorkUnit);
2380 } else {
2381 assert(no_of_gc_threads > 0,
2382 "The active gc workers should be greater than 0");
2383 // In a product build do something reasonable to avoid a crash.
2384 const size_t MinWorkUnit =
2385 MAX2(_g1->n_regions() / ParallelGCThreads, (size_t) 1U);
2386 WorkUnit =
2387 MAX2(_g1->n_regions() / (ParallelGCThreads * OverpartitionFactor),
2388 MinWorkUnit);
2389 }
2390 _collectionSetChooser->prepareForAddMarkedHeapRegionsPar(_g1->n_regions(),
2391 WorkUnit);
2392 ParKnownGarbageTask parKnownGarbageTask(_collectionSetChooser,
2393 (int) WorkUnit);
2394 _g1->workers()->run_task(&parKnownGarbageTask);
2396 assert(_g1->check_heap_region_claim_values(HeapRegion::InitialClaimValue),
2397 "sanity check");
2398 } else {
2399 KnownGarbageClosure knownGarbagecl(_collectionSetChooser);
2400 _g1->heap_region_iterate(&knownGarbagecl);
2401 }
2402 double known_garbage_end_sec;
2403 if (G1PrintParCleanupStats) {
2404 known_garbage_end_sec = os::elapsedTime();
2405 gclog_or_tty->print_cr(" compute known garbage: %8.3f ms.",
2406 (known_garbage_end_sec - clear_marked_end_sec) * 1000.0);
2407 }
2409 _collectionSetChooser->sortMarkedHeapRegions();
2410 double end_sec = os::elapsedTime();
2411 if (G1PrintParCleanupStats) {
2412 gclog_or_tty->print_cr(" sorting: %8.3f ms.",
2413 (end_sec - known_garbage_end_sec) * 1000.0);
2414 }
2416 double elapsed_time_ms = (end_sec - _mark_cleanup_start_sec) * 1000.0;
2417 _concurrent_mark_cleanup_times_ms->add(elapsed_time_ms);
2418 _cur_mark_stop_world_time_ms += elapsed_time_ms;
2419 _prev_collection_pause_end_ms += elapsed_time_ms;
2420 _mmu_tracker->add_pause(_mark_cleanup_start_sec, end_sec, true);
2421 }
2423 // Add the heap region at the head of the non-incremental collection set
2424 void G1CollectorPolicy::add_old_region_to_cset(HeapRegion* hr) {
2425 assert(_inc_cset_build_state == Active, "Precondition");
2426 assert(!hr->is_young(), "non-incremental add of young region");
2428 if (_g1->mark_in_progress())
2429 _g1->concurrent_mark()->registerCSetRegion(hr);
2431 assert(!hr->in_collection_set(), "should not already be in the CSet");
2432 hr->set_in_collection_set(true);
2433 hr->set_next_in_collection_set(_collection_set);
2434 _collection_set = hr;
2435 _collection_set_bytes_used_before += hr->used();
2436 _g1->register_region_with_in_cset_fast_test(hr);
2437 size_t rs_length = hr->rem_set()->occupied();
2438 _recorded_rs_lengths += rs_length;
2439 _old_cset_region_length += 1;
2440 }
2442 // Initialize the per-collection-set information
2443 void G1CollectorPolicy::start_incremental_cset_building() {
2444 assert(_inc_cset_build_state == Inactive, "Precondition");
2446 _inc_cset_head = NULL;
2447 _inc_cset_tail = NULL;
2448 _inc_cset_bytes_used_before = 0;
2450 _inc_cset_max_finger = 0;
2451 _inc_cset_recorded_rs_lengths = 0;
2452 _inc_cset_recorded_rs_lengths_diffs = 0;
2453 _inc_cset_predicted_elapsed_time_ms = 0.0;
2454 _inc_cset_predicted_elapsed_time_ms_diffs = 0.0;
2455 _inc_cset_build_state = Active;
2456 }
2458 void G1CollectorPolicy::finalize_incremental_cset_building() {
2459 assert(_inc_cset_build_state == Active, "Precondition");
2460 assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint");
2462 // The two "main" fields, _inc_cset_recorded_rs_lengths and
2463 // _inc_cset_predicted_elapsed_time_ms, are updated by the thread
2464 // that adds a new region to the CSet. Further updates by the
2465 // concurrent refinement thread that samples the young RSet lengths
2466 // are accumulated in the *_diffs fields. Here we add the diffs to
2467 // the "main" fields.
2469 if (_inc_cset_recorded_rs_lengths_diffs >= 0) {
2470 _inc_cset_recorded_rs_lengths += _inc_cset_recorded_rs_lengths_diffs;
2471 } else {
2472 // This is defensive. The diff should in theory be always positive
2473 // as RSets can only grow between GCs. However, given that we
2474 // sample their size concurrently with other threads updating them
2475 // it's possible that we might get the wrong size back, which
2476 // could make the calculations somewhat inaccurate.
2477 size_t diffs = (size_t) (-_inc_cset_recorded_rs_lengths_diffs);
2478 if (_inc_cset_recorded_rs_lengths >= diffs) {
2479 _inc_cset_recorded_rs_lengths -= diffs;
2480 } else {
2481 _inc_cset_recorded_rs_lengths = 0;
2482 }
2483 }
2484 _inc_cset_predicted_elapsed_time_ms +=
2485 _inc_cset_predicted_elapsed_time_ms_diffs;
2487 _inc_cset_recorded_rs_lengths_diffs = 0;
2488 _inc_cset_predicted_elapsed_time_ms_diffs = 0.0;
2489 }
2491 void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length) {
2492 // This routine is used when:
2493 // * adding survivor regions to the incremental cset at the end of an
2494 // evacuation pause,
2495 // * adding the current allocation region to the incremental cset
2496 // when it is retired, and
2497 // * updating existing policy information for a region in the
2498 // incremental cset via young list RSet sampling.
2499 // Therefore this routine may be called at a safepoint by the
2500 // VM thread, or in-between safepoints by mutator threads (when
2501 // retiring the current allocation region) or a concurrent
2502 // refine thread (RSet sampling).
2504 double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, true);
2505 size_t used_bytes = hr->used();
2506 _inc_cset_recorded_rs_lengths += rs_length;
2507 _inc_cset_predicted_elapsed_time_ms += region_elapsed_time_ms;
2508 _inc_cset_bytes_used_before += used_bytes;
2510 // Cache the values we have added to the aggregated informtion
2511 // in the heap region in case we have to remove this region from
2512 // the incremental collection set, or it is updated by the
2513 // rset sampling code
2514 hr->set_recorded_rs_length(rs_length);
2515 hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms);
2516 }
2518 void G1CollectorPolicy::update_incremental_cset_info(HeapRegion* hr,
2519 size_t new_rs_length) {
2520 // Update the CSet information that is dependent on the new RS length
2521 assert(hr->is_young(), "Precondition");
2522 assert(!SafepointSynchronize::is_at_safepoint(),
2523 "should not be at a safepoint");
2525 // We could have updated _inc_cset_recorded_rs_lengths and
2526 // _inc_cset_predicted_elapsed_time_ms directly but we'd need to do
2527 // that atomically, as this code is executed by a concurrent
2528 // refinement thread, potentially concurrently with a mutator thread
2529 // allocating a new region and also updating the same fields. To
2530 // avoid the atomic operations we accumulate these updates on two
2531 // separate fields (*_diffs) and we'll just add them to the "main"
2532 // fields at the start of a GC.
2534 ssize_t old_rs_length = (ssize_t) hr->recorded_rs_length();
2535 ssize_t rs_lengths_diff = (ssize_t) new_rs_length - old_rs_length;
2536 _inc_cset_recorded_rs_lengths_diffs += rs_lengths_diff;
2538 double old_elapsed_time_ms = hr->predicted_elapsed_time_ms();
2539 double new_region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, true);
2540 double elapsed_ms_diff = new_region_elapsed_time_ms - old_elapsed_time_ms;
2541 _inc_cset_predicted_elapsed_time_ms_diffs += elapsed_ms_diff;
2543 hr->set_recorded_rs_length(new_rs_length);
2544 hr->set_predicted_elapsed_time_ms(new_region_elapsed_time_ms);
2545 }
2547 void G1CollectorPolicy::add_region_to_incremental_cset_common(HeapRegion* hr) {
2548 assert(hr->is_young(), "invariant");
2549 assert(hr->young_index_in_cset() > -1, "should have already been set");
2550 assert(_inc_cset_build_state == Active, "Precondition");
2552 // We need to clear and set the cached recorded/cached collection set
2553 // information in the heap region here (before the region gets added
2554 // to the collection set). An individual heap region's cached values
2555 // are calculated, aggregated with the policy collection set info,
2556 // and cached in the heap region here (initially) and (subsequently)
2557 // by the Young List sampling code.
2559 size_t rs_length = hr->rem_set()->occupied();
2560 add_to_incremental_cset_info(hr, rs_length);
2562 HeapWord* hr_end = hr->end();
2563 _inc_cset_max_finger = MAX2(_inc_cset_max_finger, hr_end);
2565 assert(!hr->in_collection_set(), "invariant");
2566 hr->set_in_collection_set(true);
2567 assert( hr->next_in_collection_set() == NULL, "invariant");
2569 _g1->register_region_with_in_cset_fast_test(hr);
2570 }
2572 // Add the region at the RHS of the incremental cset
2573 void G1CollectorPolicy::add_region_to_incremental_cset_rhs(HeapRegion* hr) {
2574 // We should only ever be appending survivors at the end of a pause
2575 assert( hr->is_survivor(), "Logic");
2577 // Do the 'common' stuff
2578 add_region_to_incremental_cset_common(hr);
2580 // Now add the region at the right hand side
2581 if (_inc_cset_tail == NULL) {
2582 assert(_inc_cset_head == NULL, "invariant");
2583 _inc_cset_head = hr;
2584 } else {
2585 _inc_cset_tail->set_next_in_collection_set(hr);
2586 }
2587 _inc_cset_tail = hr;
2588 }
2590 // Add the region to the LHS of the incremental cset
2591 void G1CollectorPolicy::add_region_to_incremental_cset_lhs(HeapRegion* hr) {
2592 // Survivors should be added to the RHS at the end of a pause
2593 assert(!hr->is_survivor(), "Logic");
2595 // Do the 'common' stuff
2596 add_region_to_incremental_cset_common(hr);
2598 // Add the region at the left hand side
2599 hr->set_next_in_collection_set(_inc_cset_head);
2600 if (_inc_cset_head == NULL) {
2601 assert(_inc_cset_tail == NULL, "Invariant");
2602 _inc_cset_tail = hr;
2603 }
2604 _inc_cset_head = hr;
2605 }
2607 #ifndef PRODUCT
2608 void G1CollectorPolicy::print_collection_set(HeapRegion* list_head, outputStream* st) {
2609 assert(list_head == inc_cset_head() || list_head == collection_set(), "must be");
2611 st->print_cr("\nCollection_set:");
2612 HeapRegion* csr = list_head;
2613 while (csr != NULL) {
2614 HeapRegion* next = csr->next_in_collection_set();
2615 assert(csr->in_collection_set(), "bad CS");
2616 st->print_cr(" [%08x-%08x], t: %08x, P: %08x, N: %08x, C: %08x, "
2617 "age: %4d, y: %d, surv: %d",
2618 csr->bottom(), csr->end(),
2619 csr->top(),
2620 csr->prev_top_at_mark_start(),
2621 csr->next_top_at_mark_start(),
2622 csr->top_at_conc_mark_count(),
2623 csr->age_in_surv_rate_group_cond(),
2624 csr->is_young(),
2625 csr->is_survivor());
2626 csr = next;
2627 }
2628 }
2629 #endif // !PRODUCT
2631 void G1CollectorPolicy::choose_collection_set(double target_pause_time_ms) {
2632 // Set this here - in case we're not doing young collections.
2633 double non_young_start_time_sec = os::elapsedTime();
2635 YoungList* young_list = _g1->young_list();
2636 finalize_incremental_cset_building();
2638 guarantee(target_pause_time_ms > 0.0,
2639 err_msg("target_pause_time_ms = %1.6lf should be positive",
2640 target_pause_time_ms));
2641 guarantee(_collection_set == NULL, "Precondition");
2643 double base_time_ms = predict_base_elapsed_time_ms(_pending_cards);
2644 double predicted_pause_time_ms = base_time_ms;
2646 double time_remaining_ms = target_pause_time_ms - base_time_ms;
2648 ergo_verbose3(ErgoCSetConstruction | ErgoHigh,
2649 "start choosing CSet",
2650 ergo_format_ms("predicted base time")
2651 ergo_format_ms("remaining time")
2652 ergo_format_ms("target pause time"),
2653 base_time_ms, time_remaining_ms, target_pause_time_ms);
2655 // the 10% and 50% values are arbitrary...
2656 double threshold = 0.10 * target_pause_time_ms;
2657 if (time_remaining_ms < threshold) {
2658 double prev_time_remaining_ms = time_remaining_ms;
2659 time_remaining_ms = 0.50 * target_pause_time_ms;
2660 ergo_verbose3(ErgoCSetConstruction,
2661 "adjust remaining time",
2662 ergo_format_reason("remaining time lower than threshold")
2663 ergo_format_ms("remaining time")
2664 ergo_format_ms("threshold")
2665 ergo_format_ms("adjusted remaining time"),
2666 prev_time_remaining_ms, threshold, time_remaining_ms);
2667 }
2669 size_t expansion_bytes = _g1->expansion_regions() * HeapRegion::GrainBytes;
2671 HeapRegion* hr;
2672 double young_start_time_sec = os::elapsedTime();
2674 _collection_set_bytes_used_before = 0;
2675 _last_gc_was_young = gcs_are_young() ? true : false;
2677 if (_last_gc_was_young) {
2678 ++_young_pause_num;
2679 } else {
2680 ++_mixed_pause_num;
2681 }
2683 // The young list is laid with the survivor regions from the previous
2684 // pause are appended to the RHS of the young list, i.e.
2685 // [Newly Young Regions ++ Survivors from last pause].
2687 size_t survivor_region_length = young_list->survivor_length();
2688 size_t eden_region_length = young_list->length() - survivor_region_length;
2689 init_cset_region_lengths(eden_region_length, survivor_region_length);
2690 hr = young_list->first_survivor_region();
2691 while (hr != NULL) {
2692 assert(hr->is_survivor(), "badly formed young list");
2693 hr->set_young();
2694 hr = hr->get_next_young_region();
2695 }
2697 // Clear the fields that point to the survivor list - they are all young now.
2698 young_list->clear_survivors();
2700 if (_g1->mark_in_progress())
2701 _g1->concurrent_mark()->register_collection_set_finger(_inc_cset_max_finger);
2703 _collection_set = _inc_cset_head;
2704 _collection_set_bytes_used_before = _inc_cset_bytes_used_before;
2705 time_remaining_ms -= _inc_cset_predicted_elapsed_time_ms;
2706 predicted_pause_time_ms += _inc_cset_predicted_elapsed_time_ms;
2708 ergo_verbose3(ErgoCSetConstruction | ErgoHigh,
2709 "add young regions to CSet",
2710 ergo_format_region("eden")
2711 ergo_format_region("survivors")
2712 ergo_format_ms("predicted young region time"),
2713 eden_region_length, survivor_region_length,
2714 _inc_cset_predicted_elapsed_time_ms);
2716 // The number of recorded young regions is the incremental
2717 // collection set's current size
2718 set_recorded_rs_lengths(_inc_cset_recorded_rs_lengths);
2720 double young_end_time_sec = os::elapsedTime();
2721 _recorded_young_cset_choice_time_ms =
2722 (young_end_time_sec - young_start_time_sec) * 1000.0;
2724 // We are doing young collections so reset this.
2725 non_young_start_time_sec = young_end_time_sec;
2727 if (!gcs_are_young()) {
2728 bool should_continue = true;
2729 NumberSeq seq;
2730 double avg_prediction = 100000000000000000.0; // something very large
2732 double prev_predicted_pause_time_ms = predicted_pause_time_ms;
2733 do {
2734 // Note that add_old_region_to_cset() increments the
2735 // _old_cset_region_length field and cset_region_length() returns the
2736 // sum of _eden_cset_region_length, _survivor_cset_region_length, and
2737 // _old_cset_region_length. So, as old regions are added to the
2738 // CSet, _old_cset_region_length will be incremented and
2739 // cset_region_length(), which is used below, will always reflect
2740 // the the total number of regions added up to this point to the CSet.
2742 hr = _collectionSetChooser->getNextMarkedRegion(time_remaining_ms,
2743 avg_prediction);
2744 if (hr != NULL) {
2745 _g1->old_set_remove(hr);
2746 double predicted_time_ms = predict_region_elapsed_time_ms(hr, false);
2747 time_remaining_ms -= predicted_time_ms;
2748 predicted_pause_time_ms += predicted_time_ms;
2749 add_old_region_to_cset(hr);
2750 seq.add(predicted_time_ms);
2751 avg_prediction = seq.avg() + seq.sd();
2752 }
2754 should_continue = true;
2755 if (hr == NULL) {
2756 // No need for an ergo verbose message here,
2757 // getNextMarkRegion() does this when it returns NULL.
2758 should_continue = false;
2759 } else {
2760 if (adaptive_young_list_length()) {
2761 if (time_remaining_ms < 0.0) {
2762 ergo_verbose1(ErgoCSetConstruction,
2763 "stop adding old regions to CSet",
2764 ergo_format_reason("remaining time is lower than 0")
2765 ergo_format_ms("remaining time"),
2766 time_remaining_ms);
2767 should_continue = false;
2768 }
2769 } else {
2770 if (cset_region_length() >= _young_list_fixed_length) {
2771 ergo_verbose2(ErgoCSetConstruction,
2772 "stop adding old regions to CSet",
2773 ergo_format_reason("CSet length reached target")
2774 ergo_format_region("CSet")
2775 ergo_format_region("young target"),
2776 cset_region_length(), _young_list_fixed_length);
2777 should_continue = false;
2778 }
2779 }
2780 }
2781 } while (should_continue);
2783 if (!adaptive_young_list_length() &&
2784 cset_region_length() < _young_list_fixed_length) {
2785 ergo_verbose2(ErgoCSetConstruction,
2786 "request mixed GCs end",
2787 ergo_format_reason("CSet length lower than target")
2788 ergo_format_region("CSet")
2789 ergo_format_region("young target"),
2790 cset_region_length(), _young_list_fixed_length);
2791 _should_revert_to_young_gcs = true;
2792 }
2794 ergo_verbose2(ErgoCSetConstruction | ErgoHigh,
2795 "add old regions to CSet",
2796 ergo_format_region("old")
2797 ergo_format_ms("predicted old region time"),
2798 old_cset_region_length(),
2799 predicted_pause_time_ms - prev_predicted_pause_time_ms);
2800 }
2802 stop_incremental_cset_building();
2804 count_CS_bytes_used();
2806 ergo_verbose5(ErgoCSetConstruction,
2807 "finish choosing CSet",
2808 ergo_format_region("eden")
2809 ergo_format_region("survivors")
2810 ergo_format_region("old")
2811 ergo_format_ms("predicted pause time")
2812 ergo_format_ms("target pause time"),
2813 eden_region_length, survivor_region_length,
2814 old_cset_region_length(),
2815 predicted_pause_time_ms, target_pause_time_ms);
2817 double non_young_end_time_sec = os::elapsedTime();
2818 _recorded_non_young_cset_choice_time_ms =
2819 (non_young_end_time_sec - non_young_start_time_sec) * 1000.0;
2820 }