296 // update_young_list_target_length() during initialization. 297 _max_survivor_regions = 0; 298 299 assert(GCTimeRatio > 0, 300 "we should have set it to a default value set_g1_gc_flags() " 301 "if a user set it to 0"); 302 _gc_overhead_perc = 100.0 * (1.0 / (1.0 + GCTimeRatio)); 303 304 uintx reserve_perc = G1ReservePercent; 305 // Put an artificial ceiling on this so that it's not set to a silly value. 306 if (reserve_perc > 50) { 307 reserve_perc = 50; 308 warning("G1ReservePercent is set to a value that is too large, " 309 "it's been updated to %u", reserve_perc); 310 } 311 _reserve_factor = (double) reserve_perc / 100.0; 312 // This will be set when the heap is expanded 313 // for the first time during initialization. 314 _reserve_regions = 0; 315 316 initialize_all(); 317 _collectionSetChooser = new CollectionSetChooser(); 318 _young_gen_sizer = new G1YoungGenSizer(); // Must be after call to initialize_flags 319 } 320 321 void G1CollectorPolicy::initialize_flags() { 322 _min_alignment = HeapRegion::GrainBytes; 323 size_t card_table_alignment = GenRemSet::max_alignment_constraint(GenRemSet::CardTable); 324 size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size(); 325 _max_alignment = MAX3(card_table_alignment, _min_alignment, page_size); 326 if (SurvivorRatio < 1) { 327 vm_exit_during_initialization("Invalid survivor ratio specified"); 328 } 329 CollectorPolicy::initialize_flags(); 330 } 331 332 G1YoungGenSizer::G1YoungGenSizer() : _sizer_kind(SizerDefaults), _adaptive_size(true) { 333 assert(G1NewSizePercent <= G1MaxNewSizePercent, "Min larger than max"); 334 assert(G1NewSizePercent > 0 && G1NewSizePercent < 100, "Min out of bounds"); 335 assert(G1MaxNewSizePercent > 0 && G1MaxNewSizePercent < 100, "Max out of bounds"); 336 337 if (FLAG_IS_CMDLINE(NewRatio)) { 338 if (FLAG_IS_CMDLINE(NewSize) || FLAG_IS_CMDLINE(MaxNewSize)) { 339 warning("-XX:NewSize and -XX:MaxNewSize override -XX:NewRatio"); 340 } else { 341 _sizer_kind = SizerNewRatio; 342 _adaptive_size = false; 343 return; 344 } 345 } 346 347 if (FLAG_IS_CMDLINE(NewSize) && FLAG_IS_CMDLINE(MaxNewSize) && NewSize > MaxNewSize) { 348 vm_exit_during_initialization("Initial young gen size set larger than the maximum young gen size"); 349 } 350 351 if (FLAG_IS_CMDLINE(NewSize)) { 352 _min_desired_young_length = MAX2((uint) (NewSize / HeapRegion::GrainBytes), 353 1U); 354 if (FLAG_IS_CMDLINE(MaxNewSize)) { 355 _max_desired_young_length = 356 MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes), 361 _sizer_kind = SizerNewSizeOnly; 362 } 363 } else if (FLAG_IS_CMDLINE(MaxNewSize)) { 364 _max_desired_young_length = 365 MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes), 366 1U); 367 _sizer_kind = SizerMaxNewSizeOnly; 368 } 369 } 370 371 uint G1YoungGenSizer::calculate_default_min_length(uint new_number_of_heap_regions) { 372 uint default_value = (new_number_of_heap_regions * G1NewSizePercent) / 100; 373 return MAX2(1U, default_value); 374 } 375 376 uint G1YoungGenSizer::calculate_default_max_length(uint new_number_of_heap_regions) { 377 uint default_value = (new_number_of_heap_regions * G1MaxNewSizePercent) / 100; 378 return MAX2(1U, default_value); 379 } 380 381 void G1YoungGenSizer::heap_size_changed(uint new_number_of_heap_regions) { 382 assert(new_number_of_heap_regions > 0, "Heap must be initialized"); 383 384 switch (_sizer_kind) { 385 case SizerDefaults: 386 _min_desired_young_length = calculate_default_min_length(new_number_of_heap_regions); 387 _max_desired_young_length = calculate_default_max_length(new_number_of_heap_regions); 388 break; 389 case SizerNewSizeOnly: 390 _max_desired_young_length = calculate_default_max_length(new_number_of_heap_regions); 391 _max_desired_young_length = MAX2(_min_desired_young_length, _max_desired_young_length); 392 break; 393 case SizerMaxNewSizeOnly: 394 _min_desired_young_length = calculate_default_min_length(new_number_of_heap_regions); 395 _min_desired_young_length = MIN2(_min_desired_young_length, _max_desired_young_length); 396 break; 397 case SizerMaxAndNewSize: 398 // Do nothing. Values set on the command line, don't update them at runtime. 399 break; 400 case SizerNewRatio: 401 _min_desired_young_length = new_number_of_heap_regions / (NewRatio + 1); 402 _max_desired_young_length = _min_desired_young_length; 403 break; 404 default: 405 ShouldNotReachHere(); 406 } 407 408 assert(_min_desired_young_length <= _max_desired_young_length, "Invalid min/max young gen size values"); 409 } 410 411 void G1CollectorPolicy::init() { 412 // Set aside an initial future to_space. 413 _g1 = G1CollectedHeap::heap(); 414 415 assert(Heap_lock->owned_by_self(), "Locking discipline."); 416 417 initialize_gc_policy_counters(); 418 419 if (adaptive_young_list_length()) { 420 _young_list_fixed_length = 0; 421 } else { 422 _young_list_fixed_length = _young_gen_sizer->min_desired_young_length(); 423 } 424 _free_regions_at_end_of_collection = _g1->free_regions(); 425 update_young_list_target_length(); 426 427 // We may immediately start allocating regions and placing them on the 428 // collection set list. Initialize the per-collection set info | 296 // update_young_list_target_length() during initialization. 297 _max_survivor_regions = 0; 298 299 assert(GCTimeRatio > 0, 300 "we should have set it to a default value set_g1_gc_flags() " 301 "if a user set it to 0"); 302 _gc_overhead_perc = 100.0 * (1.0 / (1.0 + GCTimeRatio)); 303 304 uintx reserve_perc = G1ReservePercent; 305 // Put an artificial ceiling on this so that it's not set to a silly value. 306 if (reserve_perc > 50) { 307 reserve_perc = 50; 308 warning("G1ReservePercent is set to a value that is too large, " 309 "it's been updated to %u", reserve_perc); 310 } 311 _reserve_factor = (double) reserve_perc / 100.0; 312 // This will be set when the heap is expanded 313 // for the first time during initialization. 314 _reserve_regions = 0; 315 316 _collectionSetChooser = new CollectionSetChooser(); 317 } 318 319 void G1CollectorPolicy::initialize_alignments() { 320 _space_alignment = HeapRegion::GrainBytes; 321 size_t card_table_alignment = GenRemSet::max_alignment_constraint(GenRemSet::CardTable); 322 size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size(); 323 _heap_alignment = MAX3(card_table_alignment, _space_alignment, page_size); 324 } 325 326 void G1CollectorPolicy::initialize_flags() { 327 if (G1HeapRegionSize != HeapRegion::GrainBytes) { 328 FLAG_SET_ERGO(uintx, G1HeapRegionSize, HeapRegion::GrainBytes); 329 } 330 331 if (SurvivorRatio < 1) { 332 vm_exit_during_initialization("Invalid survivor ratio specified"); 333 } 334 CollectorPolicy::initialize_flags(); 335 _young_gen_sizer = new G1YoungGenSizer(); // Must be after call to initialize_flags 336 } 337 338 void G1CollectorPolicy::post_heap_initialize() { 339 uintx max_regions = G1CollectedHeap::heap()->max_regions(); 340 size_t max_young_size = (size_t)_young_gen_sizer->max_young_length(max_regions) * HeapRegion::GrainBytes; 341 if (max_young_size != MaxNewSize) { 342 FLAG_SET_ERGO(uintx, MaxNewSize, max_young_size); 343 } 344 } 345 346 G1YoungGenSizer::G1YoungGenSizer() : _sizer_kind(SizerDefaults), _adaptive_size(true), 347 _min_desired_young_length(0), _max_desired_young_length(0) { 348 if (FLAG_IS_CMDLINE(NewRatio)) { 349 if (FLAG_IS_CMDLINE(NewSize) || FLAG_IS_CMDLINE(MaxNewSize)) { 350 warning("-XX:NewSize and -XX:MaxNewSize override -XX:NewRatio"); 351 } else { 352 _sizer_kind = SizerNewRatio; 353 _adaptive_size = false; 354 return; 355 } 356 } 357 358 if (FLAG_IS_CMDLINE(NewSize) && FLAG_IS_CMDLINE(MaxNewSize) && NewSize > MaxNewSize) { 359 vm_exit_during_initialization("Initial young gen size set larger than the maximum young gen size"); 360 } 361 362 if (FLAG_IS_CMDLINE(NewSize)) { 363 _min_desired_young_length = MAX2((uint) (NewSize / HeapRegion::GrainBytes), 364 1U); 365 if (FLAG_IS_CMDLINE(MaxNewSize)) { 366 _max_desired_young_length = 367 MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes), 372 _sizer_kind = SizerNewSizeOnly; 373 } 374 } else if (FLAG_IS_CMDLINE(MaxNewSize)) { 375 _max_desired_young_length = 376 MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes), 377 1U); 378 _sizer_kind = SizerMaxNewSizeOnly; 379 } 380 } 381 382 uint G1YoungGenSizer::calculate_default_min_length(uint new_number_of_heap_regions) { 383 uint default_value = (new_number_of_heap_regions * G1NewSizePercent) / 100; 384 return MAX2(1U, default_value); 385 } 386 387 uint G1YoungGenSizer::calculate_default_max_length(uint new_number_of_heap_regions) { 388 uint default_value = (new_number_of_heap_regions * G1MaxNewSizePercent) / 100; 389 return MAX2(1U, default_value); 390 } 391 392 void G1YoungGenSizer::recalculate_min_max_young_length(uint number_of_heap_regions, uint* min_young_length, uint* max_young_length) { 393 assert(number_of_heap_regions > 0, "Heap must be initialized"); 394 395 switch (_sizer_kind) { 396 case SizerDefaults: 397 *min_young_length = calculate_default_min_length(number_of_heap_regions); 398 *max_young_length = calculate_default_max_length(number_of_heap_regions); 399 break; 400 case SizerNewSizeOnly: 401 *max_young_length = calculate_default_max_length(number_of_heap_regions); 402 *max_young_length = MAX2(*min_young_length, *max_young_length); 403 break; 404 case SizerMaxNewSizeOnly: 405 *min_young_length = calculate_default_min_length(number_of_heap_regions); 406 *min_young_length = MIN2(*min_young_length, *max_young_length); 407 break; 408 case SizerMaxAndNewSize: 409 // Do nothing. Values set on the command line, don't update them at runtime. 410 break; 411 case SizerNewRatio: 412 *min_young_length = number_of_heap_regions / (NewRatio + 1); 413 *max_young_length = *min_young_length; 414 break; 415 default: 416 ShouldNotReachHere(); 417 } 418 419 assert(*min_young_length <= *max_young_length, "Invalid min/max young gen size values"); 420 } 421 422 uint G1YoungGenSizer::max_young_length(uint number_of_heap_regions) { 423 // We need to pass the desired values because recalculation may not update these 424 // values in some cases. 425 uint temp = _min_desired_young_length; 426 uint result = _max_desired_young_length; 427 recalculate_min_max_young_length(number_of_heap_regions, &temp, &result); 428 return result; 429 } 430 431 void G1YoungGenSizer::heap_size_changed(uint new_number_of_heap_regions) { 432 recalculate_min_max_young_length(new_number_of_heap_regions, &_min_desired_young_length, 433 &_max_desired_young_length); 434 } 435 436 void G1CollectorPolicy::init() { 437 // Set aside an initial future to_space. 438 _g1 = G1CollectedHeap::heap(); 439 440 assert(Heap_lock->owned_by_self(), "Locking discipline."); 441 442 initialize_gc_policy_counters(); 443 444 if (adaptive_young_list_length()) { 445 _young_list_fixed_length = 0; 446 } else { 447 _young_list_fixed_length = _young_gen_sizer->min_desired_young_length(); 448 } 449 _free_regions_at_end_of_collection = _g1->free_regions(); 450 update_young_list_target_length(); 451 452 // We may immediately start allocating regions and placing them on the 453 // collection set list. Initialize the per-collection set info |