Print this page
G1: Use SoftMaxHeapSize to guide GC heuristics

Split Close
Expand all
Collapse all
          --- old/src/hotspot/share/gc/g1/g1CollectedHeap.cpp
          +++ new/src/hotspot/share/gc/g1/g1CollectedHeap.cpp
↓ open down ↓ 1171 lines elided ↑ open up ↑
1172 1172  
1173 1173    // Capacity, free and used after the GC counted as full regions to
1174 1174    // include the waste in the following calculations.
1175 1175    const size_t capacity_after_gc = capacity();
1176 1176    const size_t used_after_gc = capacity_after_gc - unused_committed_regions_in_bytes();
1177 1177  
1178 1178    // This is enforced in arguments.cpp.
1179 1179    assert(MinHeapFreeRatio <= MaxHeapFreeRatio,
1180 1180           "otherwise the code below doesn't make sense");
1181 1181  
1182      -  // We don't have floating point command-line arguments
1183      -  const double minimum_free_percentage = (double) MinHeapFreeRatio / 100.0;
1184      -  const double maximum_used_percentage = 1.0 - minimum_free_percentage;
1185      -  const double maximum_free_percentage = (double) MaxHeapFreeRatio / 100.0;
1186      -  const double minimum_used_percentage = 1.0 - maximum_free_percentage;
1187      -
1188      -  // We have to be careful here as these two calculations can overflow
1189      -  // 32-bit size_t's.
1190      -  double used_after_gc_d = (double) used_after_gc;
1191      -  double minimum_desired_capacity_d = used_after_gc_d / maximum_used_percentage;
1192      -  double maximum_desired_capacity_d = used_after_gc_d / minimum_used_percentage;
1193      -
1194      -  // Let's make sure that they are both under the max heap size, which
1195      -  // by default will make them fit into a size_t.
1196      -  double desired_capacity_upper_bound = (double) MaxHeapSize;
1197      -  minimum_desired_capacity_d = MIN2(minimum_desired_capacity_d,
1198      -                                    desired_capacity_upper_bound);
1199      -  maximum_desired_capacity_d = MIN2(maximum_desired_capacity_d,
1200      -                                    desired_capacity_upper_bound);
1201      -
1202      -  // We can now safely turn them into size_t's.
1203      -  size_t minimum_desired_capacity = (size_t) minimum_desired_capacity_d;
1204      -  size_t maximum_desired_capacity = (size_t) maximum_desired_capacity_d;
     1182 +  size_t minimum_desired_capacity = _heap_sizing_policy->target_heap_capacity(used_after_gc, MinHeapFreeRatio);
     1183 +  size_t maximum_desired_capacity = _heap_sizing_policy->target_heap_capacity(used_after_gc, MinHeapFreeRatio);
1205 1184  
1206 1185    // This assert only makes sense here, before we adjust them
1207 1186    // with respect to the min and max heap size.
1208 1187    assert(minimum_desired_capacity <= maximum_desired_capacity,
1209 1188           "minimum_desired_capacity = " SIZE_FORMAT ", "
1210 1189           "maximum_desired_capacity = " SIZE_FORMAT,
1211 1190           minimum_desired_capacity, maximum_desired_capacity);
1212 1191  
1213 1192    // Should not be greater than the heap max size. No need to adjust
1214 1193    // it with respect to the heap min size as it's a lower bound (i.e.,
↓ open down ↓ 1204 lines elided ↑ open up ↑
2419 2398  }
2420 2399  
2421 2400  size_t G1CollectedHeap::max_capacity() const {
2422 2401    return _hrm->max_expandable_length() * HeapRegion::GrainBytes;
2423 2402  }
2424 2403  
2425 2404  size_t G1CollectedHeap::max_reserved_capacity() const {
2426 2405    return _hrm->max_length() * HeapRegion::GrainBytes;
2427 2406  }
2428 2407  
     2408 +size_t G1CollectedHeap::soft_max_capacity() const {
     2409 +  return clamp(align_up(SoftMaxHeapSize, HeapAlignment), MinHeapSize, max_capacity());
     2410 +}
     2411 +
2429 2412  jlong G1CollectedHeap::millis_since_last_gc() {
2430 2413    // See the notes in GenCollectedHeap::millis_since_last_gc()
2431 2414    // for more information about the implementation.
2432 2415    jlong ret_val = (os::javaTimeNanos() / NANOSECS_PER_MILLISEC) -
2433 2416                    _policy->collection_pause_end_millis();
2434 2417    if (ret_val < 0) {
2435 2418      log_warning(gc)("millis_since_last_gc() would return : " JLONG_FORMAT
2436 2419        ". returning zero instead.", ret_val);
2437 2420      return 0;
2438 2421    }
↓ open down ↓ 506 lines elided ↑ open up ↑
2945 2928    if (VerifyRememberedSets) {
2946 2929      log_info(gc, verify)("[Verifying RemSets after GC]");
2947 2930      VerifyRegionRemSetClosure v_cl;
2948 2931      heap_region_iterate(&v_cl);
2949 2932    }
2950 2933    _verifier->verify_after_gc(type);
2951 2934    _verifier->check_bitmaps("GC End");
2952 2935    verify_numa_regions("GC End");
2953 2936  }
2954 2937  
2955      -void G1CollectedHeap::expand_heap_after_young_collection(){
2956      -  size_t expand_bytes = _heap_sizing_policy->expansion_amount();
     2938 +void G1CollectedHeap::resize_heap_after_young_collection() {
     2939 +  Ticks start = Ticks::now();
     2940 +  if (!expand_heap_after_young_collection()) {
     2941 +    // If we don't attempt to expand heap, try if we need to shrink the heap
     2942 +    shrink_heap_after_young_collection();
     2943 +  }
     2944 +  phase_times()->record_resize_heap_time((Ticks::now() - start).seconds() * 1000.0);
     2945 +}
     2946 +
     2947 +bool G1CollectedHeap::expand_heap_after_young_collection(){
     2948 +  size_t expand_bytes = _heap_sizing_policy->expansion_amount_after_young_collection();
2957 2949    if (expand_bytes > 0) {
2958      -    // No need for an ergo logging here,
2959      -    // expansion_amount() does this when it returns a value > 0.
2960      -    double expand_ms;
2961      -    if (!expand(expand_bytes, _workers, &expand_ms)) {
     2950 +    if (expand(expand_bytes, _workers, NULL)) {
2962 2951        // We failed to expand the heap. Cannot do anything about it.
2963 2952      }
2964      -    phase_times()->record_expand_heap_time(expand_ms);
     2953 +    return true;
     2954 +  }
     2955 +  return false;
     2956 +}
     2957 +
     2958 +void G1CollectedHeap::shrink_heap_after_young_collection() {
     2959 +  if (!collector_state()->finish_of_mixed_gc()) {
     2960 +    // Do the shrink only after finish of mixed gc
     2961 +    return;
     2962 +  }
     2963 +  size_t shrink_bytes = _heap_sizing_policy->shrink_amount_after_mixed_collections();
     2964 +  if (shrink_bytes > 0) {
     2965 +    shrink(shrink_bytes);
     2966 +  }
     2967 +}
     2968 +
     2969 +void G1CollectedHeap::expand_heap_after_concurrent_mark() {
     2970 +  size_t expand_bytes = _heap_sizing_policy->expansion_amount_after_concurrent_mark();
     2971 +  if (expand_bytes > 0) {
     2972 +    expand(expand_bytes, _workers, NULL);
2965 2973    }
2966 2974  }
2967 2975  
2968 2976  const char* G1CollectedHeap::young_gc_name() const {
2969 2977    if (collector_state()->in_initial_mark_gc()) {
2970 2978      return "Pause Young (Concurrent Start)";
2971 2979    } else if (collector_state()->in_young_only_phase()) {
2972 2980      if (collector_state()->in_young_gc_before_mixed()) {
2973 2981        return "Pause Young (Prepare Mixed)";
2974 2982      } else {
↓ open down ↓ 135 lines elided ↑ open up ↑
3110 3118            concurrent_mark()->post_initial_mark();
3111 3119            // Note that we don't actually trigger the CM thread at
3112 3120            // this point. We do that later when we're sure that
3113 3121            // the current thread has completed its logging output.
3114 3122          }
3115 3123  
3116 3124          allocate_dummy_regions();
3117 3125  
3118 3126          _allocator->init_mutator_alloc_regions();
3119 3127  
3120      -        expand_heap_after_young_collection();
     3128 +        resize_heap_after_young_collection();
3121 3129  
3122 3130          double sample_end_time_sec = os::elapsedTime();
3123 3131          double pause_time_ms = (sample_end_time_sec - sample_start_time_sec) * MILLIUNITS;
3124 3132          policy()->record_collection_pause_end(pause_time_ms);
3125 3133        }
3126 3134  
3127 3135        verify_after_young_collection(verify_type);
3128 3136  
3129 3137  #ifdef TRACESPINNING
3130 3138        ParallelTaskTerminator::print_termination_counts();
↓ open down ↓ 1853 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX