Adjust JIT thread priority post-fork.

Bug: 166406877
Test: test.py
Change-Id: I81c9a231213e62a1aeaa67cfe8c38419f5bddcd0
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index cdd69e0..f8ec005 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -1744,6 +1744,7 @@
     CHECK(code_cache_->GetZygoteMap()->IsCompilationNotified());
   }
   thread_pool_->CreateThreads();
+  thread_pool_->SetPthreadPriority(options_->GetThreadPoolPthreadPriority());
 }
 
 void Jit::BootCompleted() {
diff --git a/runtime/jit/jit.h b/runtime/jit/jit.h
index 0a5e7d3..cbfd954 100644
--- a/runtime/jit/jit.h
+++ b/runtime/jit/jit.h
@@ -261,6 +261,10 @@
       REQUIRES(!lock_)
       REQUIRES_SHARED(Locks::mutator_lock_);
 
+  int GetThreadPoolPthreadPriority() const {
+    return options_->GetThreadPoolPthreadPriority();
+  }
+
   uint16_t OSRMethodThreshold() const {
     return options_->GetOsrThreshold();
   }
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 7b907cc..95c40b4 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -685,8 +685,14 @@
 }
 
 void Runtime::PostZygoteFork() {
-  if (GetJit() != nullptr) {
-    GetJit()->PostZygoteFork();
+  jit::Jit* jit = GetJit();
+  if (jit != nullptr) {
+    jit->PostZygoteFork();
+    // Ensure that the threads in the JIT pool have been created with the right
+    // priority.
+    if (kIsDebugBuild && jit->GetThreadPool() != nullptr) {
+      jit->GetThreadPool()->CheckPthreadPriority(jit->GetThreadPoolPthreadPriority());
+    }
   }
   // Reset all stats.
   ResetStats(0xFFFFFFFF);
diff --git a/runtime/thread_pool.cc b/runtime/thread_pool.cc
index 0dc3a8f..50f5eeb 100644
--- a/runtime/thread_pool.cc
+++ b/runtime/thread_pool.cc
@@ -96,6 +96,14 @@
 #endif
 }
 
+int ThreadPoolWorker::GetPthreadPriority() {
+#if defined(ART_TARGET_ANDROID)
+  return getpriority(PRIO_PROCESS, pthread_gettid_np(pthread_));
+#else
+  return 0;
+#endif
+}
+
 void ThreadPoolWorker::Run() {
   Thread* self = Thread::Current();
   Task* task = nullptr;
@@ -313,4 +321,14 @@
   }
 }
 
+void ThreadPool::CheckPthreadPriority(int priority) {
+#if defined(ART_TARGET_ANDROID)
+  for (ThreadPoolWorker* worker : threads_) {
+    CHECK_EQ(worker->GetPthreadPriority(), priority);
+  }
+#else
+  UNUSED(priority);
+#endif
+}
+
 }  // namespace art
diff --git a/runtime/thread_pool.h b/runtime/thread_pool.h
index b04d4da..b9e5a97 100644
--- a/runtime/thread_pool.h
+++ b/runtime/thread_pool.h
@@ -83,9 +83,12 @@
 
   virtual ~ThreadPoolWorker();
 
-  // Set the "nice" priorty for this worker.
+  // Set the "nice" priority for this worker.
   void SetPthreadPriority(int priority);
 
+  // Get the "nice" priority for this worker.
+  int GetPthreadPriority();
+
   Thread* GetThread() const { return thread_; }
 
  protected:
@@ -160,9 +163,13 @@
   // thread count of the thread pool.
   void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
 
-  // Set the "nice" priorty for threads in the pool.
+  // Set the "nice" priority for threads in the pool.
   void SetPthreadPriority(int priority);
 
+  // CHECK that the "nice" priority of threads in the pool is the given
+  // `priority`.
+  void CheckPthreadPriority(int priority);
+
   // Wait for workers to be created.
   void WaitForWorkersToBeCreated();