jitzygote: JIT native methods on first use.

Compile JNI stub on first use to avoid the expensive generic stub.

(cherry picked from commit c45b5897e65d8713f008732277db2544b9af1e67)

Test: profile calendar
Bug: 119800099
Change-Id: I841c656670d12ce319e3fb1da479643480dba25d
Merged-In: Iaef9d0d528ff34c1636237cdcce6e8639c47c8ed
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index 25c747e..f8d51c6 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -815,6 +815,13 @@
     }
   }
   if (UseJitCompilation()) {
+    if (old_count == 0 &&
+        method->IsNative() &&
+        Runtime::Current()->IsUsingApexBootImageLocation()) {
+      // jitzygote: Compile JNI stub on first use to avoid the expensive generic stub.
+      CompileMethod(method, self, /* baseline= */ false, /* osr= */ false);
+      return true;
+    }
     if (old_count < HotMethodThreshold() && new_count >= HotMethodThreshold()) {
       if (!code_cache_->ContainsPc(method->GetEntryPointFromQuickCompiledCode())) {
         DCHECK(thread_pool_ != nullptr);
diff --git a/runtime/jit/jit_code_cache.cc b/runtime/jit/jit_code_cache.cc
index c22f4e3..fee5bec 100644
--- a/runtime/jit/jit_code_cache.cc
+++ b/runtime/jit/jit_code_cache.cc
@@ -2062,8 +2062,9 @@
         }
       }
       if (collection_in_progress_) {
-        CHECK(!IsInZygoteExecSpace(data->GetCode()));
-        GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(data->GetCode()));
+        if (!IsInZygoteExecSpace(data->GetCode())) {
+          GetLiveBitmap()->AtomicTestAndSet(FromCodeToAllocation(data->GetCode()));
+        }
       }
     }
     return new_compilation;