diff options
author | byungchul@chromium.org <byungchul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-20 00:38:19 +0000 |
---|---|---|
committer | byungchul@chromium.org <byungchul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-20 00:38:19 +0000 |
commit | f3225bdbd75f7df42e33f13d67d58f21a90b266d (patch) | |
tree | b7d056b1c71cffaa36de1cbaf73f12dcea17def6 | |
parent | cc2246cfdf5fdc114c951ec4ed6fe2fd6b357c03 (diff) | |
download | chromium_src-f3225bdbd75f7df42e33f13d67d58f21a90b266d.zip chromium_src-f3225bdbd75f7df42e33f13d67d58f21a90b266d.tar.gz chromium_src-f3225bdbd75f7df42e33f13d67d58f21a90b266d.tar.bz2 |
Set chrome thread name in JVM.
JVM's AttachCurrentThread() resets thread name if the caller doesn't pass
thread name. Pass it first time call of AttachCurrentThread(). Not call next
times to reduce syscall (prctl) overhead.
BUG=384603
Review URL: https://codereview.chromium.org/330823004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278551 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/android/jni_android.cc | 12 | ||||
-rw-r--r-- | base/android/jni_android.h | 13 | ||||
-rw-r--r-- | content/browser/browser_thread_impl.cc | 13 |
3 files changed, 36 insertions, 2 deletions
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc index 59f25e2..b112530 100644 --- a/base/android/jni_android.cc +++ b/base/android/jni_android.cc @@ -81,6 +81,18 @@ JNIEnv* AttachCurrentThread() { return env; } +JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name) { + DCHECK(g_jvm); + JavaVMAttachArgs args; + args.version = JNI_VERSION_1_2; + args.name = thread_name.c_str(); + args.group = NULL; + JNIEnv* env = NULL; + jint ret = g_jvm->AttachCurrentThread(&env, &args); + DCHECK_EQ(JNI_OK, ret); + return env; +} + void DetachFromVM() { // Ignore the return value, if the thread is not attached, DetachCurrentThread // will fail. But it is ok as the native thread may never be attached. diff --git a/base/android/jni_android.h b/base/android/jni_android.h index 7a00632..faf53b7 100644 --- a/base/android/jni_android.h +++ b/base/android/jni_android.h @@ -8,6 +8,8 @@ #include <jni.h> #include <sys/types.h> +#include <string> + #include "base/android/scoped_java_ref.h" #include "base/atomicops.h" #include "base/base_export.h" @@ -25,10 +27,17 @@ struct RegistrationMethod { bool (*func)(JNIEnv* env); }; -// Attach the current thread to the VM (if necessary) and return the JNIEnv*. +// Attaches the current thread to the VM (if necessary) and return the JNIEnv*. BASE_EXPORT JNIEnv* AttachCurrentThread(); -// Detach the current thread from VM if it is attached. +// Same to AttachCurrentThread except that thread name will be set to +// |thread_name| if it is the first call. Otherwise, thread_name won't be +// changed. AttachCurrentThread() doesn't regard underlying platform thread +// name, but just resets it to "Thread-???". This function should be called +// right after new thread is created if it is important to keep thread name. +BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name); + +// Detaches the current thread from VM if it is attached. BASE_EXPORT void DetachFromVM(); // Initializes the global JVM. It is not necessarily called before diff --git a/content/browser/browser_thread_impl.cc b/content/browser/browser_thread_impl.cc index d3c416d..641056f 100644 --- a/content/browser/browser_thread_impl.cc +++ b/content/browser/browser_thread_impl.cc @@ -16,6 +16,10 @@ #include "base/threading/thread_restrictions.h" #include "content/public/browser/browser_thread_delegate.h" +#if defined(OS_ANDROID) +#include "base/android/jni_android.h" +#endif + namespace content { namespace { @@ -218,6 +222,15 @@ MSVC_POP_WARNING() MSVC_ENABLE_OPTIMIZE(); void BrowserThreadImpl::Run(base::MessageLoop* message_loop) { +#if defined(OS_ANDROID) + // Not to reset thread name to "Thread-???" by VM, attach VM with thread name. + // Though it may create unnecessary VM thread objects, keeping thread name + // gives more benefit in debugging in the platform. + if (!thread_name().empty()) { + base::android::AttachCurrentThreadWithName(thread_name()); + } +#endif + BrowserThread::ID thread_id = ID_COUNT; if (!GetCurrentThreadIdentifier(&thread_id)) return Thread::Run(message_loop); |