OSDN Git Service

Allow the framework to register sensistive threads to the runtime
authorCalin Juravle <calin@google.com>
Fri, 15 Apr 2016 15:16:35 +0000 (16:16 +0100)
committerCalin Juravle <calin@google.com>
Thu, 28 Apr 2016 15:12:33 +0000 (16:12 +0100)
Bug: 27865109
Bug: 28065407

(cherry picked from commit 8fff24953c78bd58f3a42ac83b340b90e7e7228a)

Change-Id: I82094e46a2fd12617e091d98831193f9ff56d26b

runtime/jit/jit.cc
runtime/native/dalvik_system_VMRuntime.cc
runtime/runtime.cc
runtime/runtime.h
runtime/thread.cc
runtime/thread.h

index c36543f..654cea0 100644 (file)
@@ -106,10 +106,8 @@ JitOptions* JitOptions::CreateFromRuntimeArguments(const RuntimeArgumentMap& opt
 }
 
 bool Jit::ShouldUsePriorityThreadWeight() {
-  // TODO(calin): verify that IsSensitiveThread covers only the cases we are interested on.
-  // In particular if apps can set StrictMode policies for any of their threads, case in which
-  // we need to find another way to track sensitive threads.
-  return Runtime::Current()->InJankPerceptibleProcessState() && Thread::IsSensitiveThread();
+  return Runtime::Current()->InJankPerceptibleProcessState()
+      && Thread::Current()->IsJitSensitiveThread();
 }
 
 void Jit::DumpInfo(std::ostream& os) {
index f355c2a..5ba8df7 100644 (file)
@@ -212,6 +212,10 @@ static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jint bytes)
   Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, static_cast<size_t>(bytes));
 }
 
+static void VMRuntime_registerSensitiveThread(JNIEnv*, jobject) {
+  Runtime::Current()->RegisterSensitiveThread();
+}
+
 static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) {
   if (UNLIKELY(bytes < 0)) {
     ScopedObjectAccess soa(env);
@@ -643,6 +647,7 @@ static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
   NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"),
   NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(I)V"),
+  NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
   NATIVE_METHOD(VMRuntime, registerNativeFree, "(I)V"),
   NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
   NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
index 95995fb..f0510af 100644 (file)
@@ -1992,4 +1992,8 @@ void Runtime::UpdateProcessState(ProcessState process_state) {
   GetHeap()->UpdateProcessState(old_process_state, process_state);
 }
 
+void Runtime::RegisterSensitiveThread() const {
+  Thread::SetJitSensitiveThread();
+}
+
 }  // namespace art
index b6a9125..fd4b5c8 100644 (file)
@@ -636,6 +636,8 @@ class Runtime {
     return process_state_ == kProcessStateJankPerceptible;
   }
 
+  void RegisterSensitiveThread() const;
+
   void SetZygoteNoThreadSection(bool val) {
     zygote_no_threads_ = val;
   }
index cdbf995..e3feda6 100644 (file)
@@ -89,6 +89,7 @@ pthread_key_t Thread::pthread_key_self_;
 ConditionVariable* Thread::resume_cond_ = nullptr;
 const size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA);
 bool (*Thread::is_sensitive_thread_hook_)() = nullptr;
+Thread* Thread::jit_sensitive_thread_ = nullptr;
 
 static constexpr bool kVerifyImageObjectsMarked = kIsDebugBuild;
 
index 2092feb..582a0cd 100644 (file)
@@ -1093,6 +1093,12 @@ class Thread {
     return debug_disallow_read_barrier_;
   }
 
+  // Returns true if the current thread is the jit sensitive thread.
+  bool IsJitSensitiveThread() const {
+    return this == jit_sensitive_thread_;
+  }
+
+  // Returns true if StrictMode events are traced for the current thread.
   static bool IsSensitiveThread() {
     if (is_sensitive_thread_hook_ != nullptr) {
       return (*is_sensitive_thread_hook_)();
@@ -1175,6 +1181,16 @@ class Thread {
   ALWAYS_INLINE void PassActiveSuspendBarriers()
       REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_);
 
+  // Registers the current thread as the jit sensitive thread. Should be called just once.
+  static void SetJitSensitiveThread() {
+    if (jit_sensitive_thread_ == nullptr) {
+      jit_sensitive_thread_ = Thread::Current();
+    } else {
+      LOG(WARNING) << "Attempt to set the sensitive thread twice. Tid:"
+          << Thread::Current()->GetTid();
+    }
+  }
+
   static void SetSensitiveThreadHook(bool (*is_sensitive_thread_hook)()) {
     is_sensitive_thread_hook_ = is_sensitive_thread_hook;
   }
@@ -1224,6 +1240,8 @@ class Thread {
   // Hook passed by framework which returns true
   // when StrictMode events are traced for the current thread.
   static bool (*is_sensitive_thread_hook_)();
+  // Stores the jit sensitive thread (which for now is the UI thread).
+  static Thread* jit_sensitive_thread_;
 
   /***********************************************************************************************/
   // Thread local storage. Fields are grouped by size to enable 32 <-> 64 searching to account for