Tweak JIT hotness threshold.

bug:23128949
bug:27398183

Change-Id: Iffde6ba064e54546827cb8fc2a670baedf2e2409
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index 7dbd89c..5bd9a6b 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -41,20 +41,42 @@
 JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& options) {
   auto* jit_options = new JitOptions;
   jit_options->use_jit_ = options.GetOrDefault(RuntimeArgumentMap::UseJIT);
+
   jit_options->code_cache_initial_capacity_ =
       options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheInitialCapacity);
   jit_options->code_cache_max_capacity_ =
       options.GetOrDefault(RuntimeArgumentMap::JITCodeCacheMaxCapacity);
-  jit_options->compile_threshold_ =
-      options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
-  // TODO(ngeoffray): Make this a proper option.
-  jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
-  jit_options->warmup_threshold_ =
-      options.GetOrDefault(RuntimeArgumentMap::JITWarmupThreshold);
   jit_options->dump_info_on_shutdown_ =
       options.Exists(RuntimeArgumentMap::DumpJITInfoOnShutdown);
   jit_options->save_profiling_info_ =
-      options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);;
+      options.GetOrDefault(RuntimeArgumentMap::JITSaveProfilingInfo);
+
+  jit_options->compile_threshold_ = options.GetOrDefault(RuntimeArgumentMap::JITCompileThreshold);
+  if (jit_options->compile_threshold_ > std::numeric_limits<uint16_t>::max()) {
+    LOG(FATAL) << "Method compilation threshold is above its internal limit.";
+  }
+
+  if (options.Exists(RuntimeArgumentMap::JITWarmupThreshold)) {
+    jit_options->warmup_threshold_ = *options.Get(RuntimeArgumentMap::JITWarmupThreshold);
+    if (jit_options->warmup_threshold_ > std::numeric_limits<uint16_t>::max()) {
+      LOG(FATAL) << "Method warmup threshold is above its internal limit.";
+    }
+  } else {
+    jit_options->warmup_threshold_ = jit_options->compile_threshold_ / 2;
+  }
+
+  if (options.Exists(RuntimeArgumentMap::JITOsrThreshold)) {
+    jit_options->osr_threshold_ = *options.Get(RuntimeArgumentMap::JITOsrThreshold);
+    if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
+      LOG(FATAL) << "Method on stack replacement threshold is above its internal limit.";
+    }
+  } else {
+    jit_options->osr_threshold_ = jit_options->compile_threshold_ * 2;
+    if (jit_options->osr_threshold_ > std::numeric_limits<uint16_t>::max()) {
+      jit_options->osr_threshold_ = std::numeric_limits<uint16_t>::max();
+    }
+  }
+
   return jit_options;
 }
 
diff --git a/runtime/jit/jit.h b/runtime/jit/jit.h
index ee416d8..d5c2134 100644
--- a/runtime/jit/jit.h
+++ b/runtime/jit/jit.h
@@ -43,8 +43,7 @@
 class Jit {
  public:
   static constexpr bool kStressMode = kIsDebugBuild;
-  static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 500;
-  static constexpr size_t kDefaultWarmupThreshold = kDefaultCompileThreshold / 2;
+  static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000;
 
   virtual ~Jit();
   static Jit* Create(JitOptions* options, std::string* error_msg);
diff --git a/runtime/parsed_options.cc b/runtime/parsed_options.cc
index d64aa43..60403f9 100644
--- a/runtime/parsed_options.cc
+++ b/runtime/parsed_options.cc
@@ -166,6 +166,9 @@
       .Define("-Xjitwarmupthreshold:_")
           .WithType<unsigned int>()
           .IntoKey(M::JITWarmupThreshold)
+      .Define("-Xjitosrthreshold:_")
+          .WithType<unsigned int>()
+          .IntoKey(M::JITOsrThreshold)
       .Define("-Xjitsaveprofilinginfo")
           .WithValue(true)
           .IntoKey(M::JITSaveProfilingInfo)
@@ -694,6 +697,8 @@
   UsageMessage(stream, "  -Xusejit:booleanvalue\n");
   UsageMessage(stream, "  -Xjitinitialsize:N\n");
   UsageMessage(stream, "  -Xjitmaxsize:N\n");
+  UsageMessage(stream, "  -Xjitwarmupthreshold:integervalue\n");
+  UsageMessage(stream, "  -Xjitosrthreshold:integervalue\n");
   UsageMessage(stream, "  -X[no]relocate\n");
   UsageMessage(stream, "  -X[no]dex2oat (Whether to invoke dex2oat on the application)\n");
   UsageMessage(stream, "  -X[no]image-dex2oat (Whether to create and use a boot image)\n");
diff --git a/runtime/runtime_options.def b/runtime/runtime_options.def
index 838d1a9..3fd9905 100644
--- a/runtime/runtime_options.def
+++ b/runtime/runtime_options.def
@@ -69,7 +69,8 @@
 RUNTIME_OPTIONS_KEY (bool,                UseJIT,                         false)
 RUNTIME_OPTIONS_KEY (bool,                DumpNativeStackOnSigQuit,       true)
 RUNTIME_OPTIONS_KEY (unsigned int,        JITCompileThreshold,            jit::Jit::kDefaultCompileThreshold)
-RUNTIME_OPTIONS_KEY (unsigned int,        JITWarmupThreshold,             jit::Jit::kDefaultWarmupThreshold)
+RUNTIME_OPTIONS_KEY (unsigned int,        JITWarmupThreshold)
+RUNTIME_OPTIONS_KEY (unsigned int,        JITOsrThreshold)
 RUNTIME_OPTIONS_KEY (MemoryKiB,           JITCodeCacheInitialCapacity,    jit::JitCodeCache::kInitialCapacity)
 RUNTIME_OPTIONS_KEY (MemoryKiB,           JITCodeCacheMaxCapacity,        jit::JitCodeCache::kMaxCapacity)
 RUNTIME_OPTIONS_KEY (bool,                JITSaveProfilingInfo,           false)