8059604: Add CompileThresholdScaling flag to control when methods are first compiled (with and withour TieredCompilation)

This patch adds a new flag (CompileThresholdScaling) to control when methods are first compiled

Reviewed-by: anoll, iveresov, kvn
diff --git a/hotspot/src/share/vm/opto/bytecodeInfo.cpp b/hotspot/src/share/vm/opto/bytecodeInfo.cpp
index ae8da0f..3ee6c9b 100644
--- a/hotspot/src/share/vm/opto/bytecodeInfo.cpp
+++ b/hotspot/src/share/vm/opto/bytecodeInfo.cpp
@@ -298,10 +298,19 @@
     if (is_init_with_ea(callee_method, caller_method, C)) {
       // Escape Analysis: inline all executed constructors
       return false;
-    } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
-                                                           CompileThreshold >> 1))) {
-      set_msg("executed < MinInliningThreshold times");
-      return true;
+    } else {
+      intx counter_high_value;
+      // Tiered compilation uses a different "high value" than non-tiered compilation.
+      // Determine the right value to use.
+      if (TieredCompilation) {
+        counter_high_value = InvocationCounter::count_limit / 2;
+      } else {
+        counter_high_value = CompileThreshold / 2;
+      }
+      if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold, counter_high_value))) {
+        set_msg("executed < MinInliningThreshold times");
+        return true;
+      }
     }
   }
 
diff --git a/hotspot/src/share/vm/runtime/arguments.cpp b/hotspot/src/share/vm/runtime/arguments.cpp
index e8ea4d3..6e87073 100644
--- a/hotspot/src/share/vm/runtime/arguments.cpp
+++ b/hotspot/src/share/vm/runtime/arguments.cpp
@@ -1135,6 +1135,21 @@
 }
 #endif
 
+// Returns threshold scaled with CompileThresholdScaling
+intx Arguments::get_scaled_compile_threshold(intx threshold) {
+  return (intx)(threshold * CompileThresholdScaling);
+}
+
+// Returns freq_log scaled with CompileThresholdScaling
+intx Arguments::get_scaled_freq_log(intx freq_log) {
+  intx scaled_freq = get_scaled_compile_threshold((intx)1 << freq_log);
+  if (scaled_freq == 0) {
+    return 0;
+  } else {
+    return log2_intptr(scaled_freq);
+  }
+}
+
 void Arguments::set_tiered_flags() {
   // With tiered, set default policy to AdvancedThresholdPolicy, which is 3.
   if (FLAG_IS_DEFAULT(CompilationPolicyChoice)) {
@@ -1174,6 +1189,32 @@
     Tier3InvokeNotifyFreqLog = 0;
     Tier4InvocationThreshold = 0;
   }
+  // Scale tiered compilation thresholds
+  if (!FLAG_IS_DEFAULT(CompileThresholdScaling)) {
+    FLAG_SET_ERGO(intx, Tier0InvokeNotifyFreqLog, get_scaled_freq_log(Tier0InvokeNotifyFreqLog));
+    FLAG_SET_ERGO(intx, Tier0BackedgeNotifyFreqLog, get_scaled_freq_log(Tier0BackedgeNotifyFreqLog));
+
+    FLAG_SET_ERGO(intx, Tier3InvocationThreshold, get_scaled_compile_threshold(Tier3InvocationThreshold));
+    FLAG_SET_ERGO(intx, Tier3MinInvocationThreshold, get_scaled_compile_threshold(Tier3MinInvocationThreshold));
+    FLAG_SET_ERGO(intx, Tier3CompileThreshold, get_scaled_compile_threshold(Tier3CompileThreshold));
+    FLAG_SET_ERGO(intx, Tier3BackEdgeThreshold, get_scaled_compile_threshold(Tier3BackEdgeThreshold));
+
+    // Tier2{Invocation,MinInvocation,Compile,Backedge}Threshold should be scaled here
+    // once these thresholds become supported.
+
+    FLAG_SET_ERGO(intx, Tier2InvokeNotifyFreqLog, get_scaled_freq_log(Tier2InvokeNotifyFreqLog));
+    FLAG_SET_ERGO(intx, Tier2BackedgeNotifyFreqLog, get_scaled_freq_log(Tier2BackedgeNotifyFreqLog));
+
+    FLAG_SET_ERGO(intx, Tier3InvokeNotifyFreqLog, get_scaled_freq_log(Tier3InvokeNotifyFreqLog));
+    FLAG_SET_ERGO(intx, Tier3BackedgeNotifyFreqLog, get_scaled_freq_log(Tier3BackedgeNotifyFreqLog));
+
+    FLAG_SET_ERGO(intx, Tier23InlineeNotifyFreqLog, get_scaled_freq_log(Tier23InlineeNotifyFreqLog));
+
+    FLAG_SET_ERGO(intx, Tier4InvocationThreshold, get_scaled_compile_threshold(Tier4InvocationThreshold));
+    FLAG_SET_ERGO(intx, Tier4MinInvocationThreshold, get_scaled_compile_threshold(Tier4MinInvocationThreshold));
+    FLAG_SET_ERGO(intx, Tier4CompileThreshold, get_scaled_compile_threshold(Tier4CompileThreshold));
+    FLAG_SET_ERGO(intx, Tier4BackEdgeThreshold, get_scaled_compile_threshold(Tier4BackEdgeThreshold));
+  }
 }
 
 /**
@@ -3501,7 +3542,9 @@
     // not specified.
     set_mode_flags(_int);
   }
-  if (CompileThreshold == 0) {
+
+  if ((TieredCompilation && CompileThresholdScaling == 0)
+      || (!TieredCompilation && get_scaled_compile_threshold(CompileThreshold) == 0)) {
     set_mode_flags(_int);
   }
 
@@ -3921,6 +3964,10 @@
       vm_exit_during_initialization(
         "Incompatible compilation policy selected", NULL);
     }
+    // Scale CompileThreshold
+    if (!FLAG_IS_DEFAULT(CompileThresholdScaling)) {
+      FLAG_SET_ERGO(intx, CompileThreshold, get_scaled_compile_threshold(CompileThreshold));
+    }
   }
 
 #ifdef COMPILER2
diff --git a/hotspot/src/share/vm/runtime/arguments.hpp b/hotspot/src/share/vm/runtime/arguments.hpp
index 342422e..3c8ae0f 100644
--- a/hotspot/src/share/vm/runtime/arguments.hpp
+++ b/hotspot/src/share/vm/runtime/arguments.hpp
@@ -326,6 +326,9 @@
   static bool _ClipInlining;
   static bool _CIDynamicCompilePriority;
 
+  // Scale compile thresholds
+  static intx get_scaled_compile_threshold(intx threshold);
+  static intx get_scaled_freq_log(intx freq_log);
   // Tiered
   static void set_tiered_flags();
   static int  get_min_number_of_compiler_threads();
diff --git a/hotspot/src/share/vm/runtime/globals.hpp b/hotspot/src/share/vm/runtime/globals.hpp
index 34c9074..4465d9d 100644
--- a/hotspot/src/share/vm/runtime/globals.hpp
+++ b/hotspot/src/share/vm/runtime/globals.hpp
@@ -3578,6 +3578,10 @@
   product_pd(intx, CompileThreshold,                                        \
           "number of interpreted method invocations before (re-)compiling") \
                                                                             \
+  product(double, CompileThresholdScaling, 1.0,                             \
+          "Factor to control when first compilation happens "               \
+          "(both with and without tiered compilation)")                     \
+                                                                            \
   product(intx, Tier0InvokeNotifyFreqLog, 7,                                \
           "Interpreter (tier 0) invocation notification frequency")         \
                                                                             \
diff --git a/hotspot/src/share/vm/runtime/simpleThresholdPolicy.inline.hpp b/hotspot/src/share/vm/runtime/simpleThresholdPolicy.inline.hpp
index 4917f76..a142fa8 100644
--- a/hotspot/src/share/vm/runtime/simpleThresholdPolicy.inline.hpp
+++ b/hotspot/src/share/vm/runtime/simpleThresholdPolicy.inline.hpp
@@ -30,11 +30,11 @@
   switch(level) {
   case CompLevel_none:
   case CompLevel_limited_profile:
-    return (i > Tier3InvocationThreshold * scale) ||
-           (i > Tier3MinInvocationThreshold * scale && i + b > Tier3CompileThreshold * scale);
+    return (i >= Tier3InvocationThreshold * scale) ||
+           (i >= Tier3MinInvocationThreshold * scale && i + b >= Tier3CompileThreshold * scale);
   case CompLevel_full_profile:
-   return (i > Tier4InvocationThreshold * scale) ||
-          (i > Tier4MinInvocationThreshold * scale && i + b > Tier4CompileThreshold * scale);
+   return (i >= Tier4InvocationThreshold * scale) ||
+          (i >= Tier4MinInvocationThreshold * scale && i + b >= Tier4CompileThreshold * scale);
   }
   return true;
 }
@@ -44,9 +44,9 @@
   switch(level) {
   case CompLevel_none:
   case CompLevel_limited_profile:
-    return b > Tier3BackEdgeThreshold * scale;
+    return b >= Tier3BackEdgeThreshold * scale;
   case CompLevel_full_profile:
-    return b > Tier4BackEdgeThreshold * scale;
+    return b >= Tier4BackEdgeThreshold * scale;
   }
   return true;
 }