summaryrefslogtreecommitdiffstats
path: root/base/threading
diff options
context:
space:
mode:
authornduca@chromium.org <nduca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-09 15:16:25 +0000
committernduca@chromium.org <nduca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-09 15:16:25 +0000
commit5b4926e1893925ce5be80d1e43fe753f27952e9c (patch)
treed35c2bbc7e91a18ea5256f0690912ff8493d5aa8 /base/threading
parent151648c6d04cb19e8bfe868bb668342912952e44 (diff)
downloadchromium_src-5b4926e1893925ce5be80d1e43fe753f27952e9c.zip
chromium_src-5b4926e1893925ce5be80d1e43fe753f27952e9c.tar.gz
chromium_src-5b4926e1893925ce5be80d1e43fe753f27952e9c.tar.bz2
Implement backend for thread names and process names.
Added a new trace event phase 'M'/TRACE_EVENT_PHASE_METADATA, which can become a general mechanism for adding metadata to traces. The two M-type events that we then add are: {ph=M pid=<pid> name="process_name" args={ name="name of pid" }} {ph=M pid=<pid> tid=<tid> name="thread_name" args={ name="name of tid" }} base::thread is instrumented to set names automatically. I will do a followon changelist to add instrumentation to Chrome for its various processes and threads. Review URL: http://codereview.chromium.org/7495031 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95997 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/threading')
-rw-r--r--base/threading/platform_thread.h7
-rw-r--r--base/threading/platform_thread_mac.mm10
-rw-r--r--base/threading/platform_thread_posix.cc22
-rw-r--r--base/threading/platform_thread_win.cc11
4 files changed, 47 insertions, 3 deletions
diff --git a/base/threading/platform_thread.h b/base/threading/platform_thread.h
index 4703b96..8382bbe 100644
--- a/base/threading/platform_thread.h
+++ b/base/threading/platform_thread.h
@@ -74,9 +74,14 @@ class BASE_EXPORT PlatformThread {
// Sleeps for the specified duration (units are milliseconds).
static void Sleep(int duration_ms);
- // Sets the thread name visible to a debugger. This has no effect otherwise.
+ // Sets the thread name visible to debuggers/tools. This has no effect
+ // otherwise. This name pointer is not copied internally. Thus, it must stay
+ // valid until the thread ends.
static void SetName(const char* name);
+ // Gets the thread name, if previously set by SetName.
+ static const char* GetName();
+
// Creates a new thread. The |stack_size| parameter can be 0 to indicate
// that the default stack size should be used. Upon success,
// |*thread_handle| will be assigned a handle to the newly created thread,
diff --git a/base/threading/platform_thread_mac.mm b/base/threading/platform_thread_mac.mm
index 7d59064..9894a1d 100644
--- a/base/threading/platform_thread_mac.mm
+++ b/base/threading/platform_thread_mac.mm
@@ -11,9 +11,12 @@
#include <mach/thread_policy.h>
#include "base/logging.h"
+#include "base/threading/thread_local.h"
namespace base {
+static ThreadLocalPointer<char> current_thread_name;
+
// If Cocoa is to be used on more than one thread, it must know that the
// application is multithreaded. Since it's possible to enter Cocoa code
// from threads created by pthread_thread_create, Cocoa won't necessarily
@@ -37,6 +40,8 @@ void InitThreading() {
// static
void PlatformThread::SetName(const char* name) {
+ current_thread_name.Set(const_cast<char*>(name));
+
// pthread_setname_np is only available in 10.6 or later, so test
// for it at runtime.
int (*dynamic_pthread_setname_np)(const char*);
@@ -54,6 +59,11 @@ void PlatformThread::SetName(const char* name) {
dynamic_pthread_setname_np(shortened_name.c_str());
}
+// static
+const char* PlatformThread::GetName() {
+ return current_thread_name.Get();
+}
+
namespace {
void SetPriorityNormal(mach_port_t mach_thread_id) {
diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc
index baa3ab1..c924a16 100644
--- a/base/threading/platform_thread_posix.cc
+++ b/base/threading/platform_thread_posix.cc
@@ -10,6 +10,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/safe_strerror_posix.h"
+#include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h"
#if defined(OS_MACOSX)
@@ -41,6 +42,8 @@ void InitThreading();
namespace {
+static ThreadLocalPointer<char> current_thread_name;
+
struct ThreadParams {
PlatformThread::Delegate* delegate;
bool joinable;
@@ -168,6 +171,10 @@ void PlatformThread::Sleep(int duration_ms) {
#if 0 && defined(OS_LINUX)
// static
void PlatformThread::SetName(const char* name) {
+ // have to cast away const because ThreadLocalPointer does not support const
+ // void*
+ current_thread_name.Set(const_cast<char*>(name));
+
// http://0pointer.de/blog/projects/name-your-threads.html
// glibc recently added support for pthread_setname_np, but it's not
@@ -198,14 +205,25 @@ void PlatformThread::SetName(const char* name) {
// Mac is implemented in platform_thread_mac.mm.
#else
// static
-void PlatformThread::SetName(const char* /*name*/) {
- // Leave it unimplemented.
+void PlatformThread::SetName(const char* name) {
+ // have to cast away const because ThreadLocalPointer does not support const
+ // void*
+ current_thread_name.Set(const_cast<char*>(name));
// (This should be relatively simple to implement for the BSDs; I
// just don't have one handy to test the code on.)
}
#endif // defined(OS_LINUX)
+
+#if !defined(OS_MACOSX)
+// Mac is implemented in platform_thread_mac.mm.
+// static
+const char* PlatformThread::GetName() {
+ return current_thread_name.Get();
+}
+#endif
+
// static
bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle) {
diff --git a/base/threading/platform_thread_win.cc b/base/threading/platform_thread_win.cc
index 56d7381..e2d90d8 100644
--- a/base/threading/platform_thread_win.cc
+++ b/base/threading/platform_thread_win.cc
@@ -5,13 +5,17 @@
#include "base/threading/platform_thread.h"
#include "base/logging.h"
+#include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h"
+
#include "base/win/windows_version.h"
namespace base {
namespace {
+static ThreadLocalPointer<char> current_thread_name;
+
// The information on how to set the thread name comes from
// a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
const DWORD kVCThreadNameException = 0x406D1388;
@@ -94,6 +98,8 @@ void PlatformThread::Sleep(int duration_ms) {
// static
void PlatformThread::SetName(const char* name) {
+ current_thread_name.Set(const_cast<char*>(name));
+
// The debugger needs to be around to catch the name in the exception. If
// there isn't a debugger, we are just needlessly throwing an exception.
if (!::IsDebuggerPresent())
@@ -113,6 +119,11 @@ void PlatformThread::SetName(const char* name) {
}
// static
+const char* PlatformThread::GetName() {
+ return current_thread_name.Get();
+}
+
+// static
bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle) {
DCHECK(thread_handle);