Comment and adjust system server profiling.

The logic is difficult to understand, so add comments around system
server profiling and precompiling.

Also prevent doing precompilation when the system server is being
profiled.

Test: m
Change-Id: I99eb098415d683d7a7c53756ae5b18dba00dc455
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index 239f207..2cc4aef 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -1317,12 +1317,21 @@
     return;
   }
   Runtime* runtime = Runtime::Current();
-  // If the runtime is debuggable, no need to precompile methods.
+  // If the runtime is debuggable, don't bother precompiling methods.
+  // If system server is being profiled, don't precompile as we are going to use
+  // the JIT to count hotness. Note that --count-hotness-in-compiled-code is
+  // only forced when we also profile the boot classpath, see
+  // AndroidRuntime.cpp.
   if (runtime->IsSystemServer() &&
       UseJitCompilation() &&
       options_->UseProfiledJitCompilation() &&
       runtime->HasImageWithProfile() &&
+      !runtime->IsSystemServerProfiled() &&
       !runtime->IsJavaDebuggable()) {
+    // Note: this precompilation is currently not running in production because:
+    // - UseProfiledJitCompilation() is not set by default.
+    // - System server dex files are registered *before* we set the runtime as
+    //   system server (though we are in the system server process).
     thread_pool_->AddTask(Thread::Current(), new JitProfileTask(dex_files, class_loader));
   }
 }
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index c51b840..1f3ee80 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -3393,8 +3393,12 @@
   WellKnownClasses::HandleJniIdTypeChange(Thread::Current()->GetJniEnv());
 }
 
+bool Runtime::IsSystemServerProfiled() const {
+  return IsSystemServer() && jit_options_->GetSaveProfilingInfo();
+}
+
 bool Runtime::GetOatFilesExecutable() const {
-  return !IsAotCompiler() && !(IsSystemServer() && jit_options_->GetSaveProfilingInfo());
+  return !IsAotCompiler() && !IsSystemServerProfiled();
 }
 
 void Runtime::ProcessWeakClass(GcRoot<mirror::Class>* root_ptr,
diff --git a/runtime/runtime.h b/runtime/runtime.h
index 4dab4fd..ccae637 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -1104,7 +1104,11 @@
   uint64_t GetMonitorTimeoutNs() const {
     return monitor_timeout_ns_;
   }
-  // Return true if we should load oat files as executable or not.
+
+  // Return whether this is system server and it is being profiled.
+  bool IsSystemServerProfiled() const;
+
+  // Return whether we should load oat files as executable or not.
   bool GetOatFilesExecutable() const;
 
   metrics::ArtMetrics* GetMetrics() { return &metrics_; }