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
parent cc2246cf
...@@ -81,6 +81,18 @@ JNIEnv* AttachCurrentThread() { ...@@ -81,6 +81,18 @@ JNIEnv* AttachCurrentThread() {
return env; 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() { void DetachFromVM() {
// Ignore the return value, if the thread is not attached, DetachCurrentThread // 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. // will fail. But it is ok as the native thread may never be attached.
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <jni.h> #include <jni.h>
#include <sys/types.h> #include <sys/types.h>
#include <string>
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "base/atomicops.h" #include "base/atomicops.h"
#include "base/base_export.h" #include "base/base_export.h"
...@@ -25,10 +27,17 @@ struct RegistrationMethod { ...@@ -25,10 +27,17 @@ struct RegistrationMethod {
bool (*func)(JNIEnv* env); 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(); 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(); BASE_EXPORT void DetachFromVM();
// Initializes the global JVM. It is not necessarily called before // Initializes the global JVM. It is not necessarily called before
......
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_thread_delegate.h" #include "content/public/browser/browser_thread_delegate.h"
#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#endif
namespace content { namespace content {
namespace { namespace {
...@@ -218,6 +222,15 @@ MSVC_POP_WARNING() ...@@ -218,6 +222,15 @@ MSVC_POP_WARNING()
MSVC_ENABLE_OPTIMIZE(); MSVC_ENABLE_OPTIMIZE();
void BrowserThreadImpl::Run(base::MessageLoop* message_loop) { 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; BrowserThread::ID thread_id = ID_COUNT;
if (!GetCurrentThreadIdentifier(&thread_id)) if (!GetCurrentThreadIdentifier(&thread_id))
return Thread::Run(message_loop); return Thread::Run(message_loop);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment