Revision: 6141
Author: [email protected]
Date: Tue Jan  4 01:09:50 2011
Log: Added labelled thread names to help with some debugging activity. Right now,
the only platform that it works on is linux (using the prctl API to set the
names of the threads). Other platforms are setup to build properly if the
flag is set, but their thread names are not currently set.

Patch by Mark Lam from Hewlett-Packard Development Company, LP

Review URL: http://codereview.chromium.org/6070009

http://code.google.com/p/v8/source/detail?r=6141

Modified:
 /branches/bleeding_edge/src/cpu-profiler.cc
 /branches/bleeding_edge/src/d8-debug.h
 /branches/bleeding_edge/src/d8.cc
 /branches/bleeding_edge/src/debug-agent.h
 /branches/bleeding_edge/src/debug.cc
 /branches/bleeding_edge/src/log.cc
 /branches/bleeding_edge/src/platform-freebsd.cc
 /branches/bleeding_edge/src/platform-linux.cc
 /branches/bleeding_edge/src/platform-macos.cc
 /branches/bleeding_edge/src/platform-nullos.cc
 /branches/bleeding_edge/src/platform-openbsd.cc
 /branches/bleeding_edge/src/platform-solaris.cc
 /branches/bleeding_edge/src/platform-win32.cc
 /branches/bleeding_edge/src/platform.h
 /branches/bleeding_edge/src/top.cc
 /branches/bleeding_edge/src/v8threads.cc

=======================================
--- /branches/bleeding_edge/src/cpu-profiler.cc Tue Dec  7 03:31:57 2010
+++ /branches/bleeding_edge/src/cpu-profiler.cc Tue Jan  4 01:09:50 2011
@@ -47,7 +47,8 @@


ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator)
-    : generator_(generator),
+    : Thread("v8:ProfEvntProc"),
+      generator_(generator),
       running_(true),
       ticks_buffer_(sizeof(TickSampleEventRecord),
                     kTickSamplesBufferChunkSize,
=======================================
--- /branches/bleeding_edge/src/d8-debug.h      Tue Dec  7 03:01:02 2010
+++ /branches/bleeding_edge/src/d8-debug.h      Tue Jan  4 01:09:50 2011
@@ -98,7 +98,8 @@
 class ReceiverThread: public i::Thread {
  public:
   explicit ReceiverThread(RemoteDebugger* remote_debugger)
-      : remote_debugger_(remote_debugger) {}
+      : Thread("d8:ReceiverThrd"),
+        remote_debugger_(remote_debugger) {}
   ~ReceiverThread() {}

   void Run();
@@ -112,7 +113,8 @@
 class KeyboardThread: public i::Thread {
  public:
   explicit KeyboardThread(RemoteDebugger* remote_debugger)
-      : remote_debugger_(remote_debugger) {}
+      : Thread("d8:KeyboardThrd"),
+        remote_debugger_(remote_debugger) {}
   ~KeyboardThread() {}

   void Run();
=======================================
--- /branches/bleeding_edge/src/d8.cc   Tue Dec  7 03:01:02 2010
+++ /branches/bleeding_edge/src/d8.cc   Tue Jan  4 01:09:50 2011
@@ -599,7 +599,8 @@
 class ShellThread : public i::Thread {
  public:
   ShellThread(int no, i::Vector<const char> files)
-    : no_(no), files_(files) { }
+    : Thread("d8:ShellThread"),
+      no_(no), files_(files) { }
   virtual void Run();
  private:
   int no_;
=======================================
--- /branches/bleeding_edge/src/debug-agent.h   Tue Dec  7 03:01:02 2010
+++ /branches/bleeding_edge/src/debug-agent.h   Tue Jan  4 01:09:50 2011
@@ -44,7 +44,8 @@
 class DebuggerAgent: public Thread {
  public:
   explicit DebuggerAgent(const char* name, int port)
-      : name_(StrDup(name)), port_(port),
+      : Thread(name),
+        name_(StrDup(name)), port_(port),
         server_(OS::CreateSocket()), terminate_(false),
         session_access_(OS::CreateMutex()), session_(NULL),
         terminate_now_(OS::CreateSemaphore(0)),
@@ -90,7 +91,8 @@
 class DebuggerAgentSession: public Thread {
  public:
   DebuggerAgentSession(DebuggerAgent* agent, Socket* client)
-      : agent_(agent), client_(client) {}
+      : Thread("v8:DbgAgntSessn"),
+        agent_(agent), client_(client) {}

   void DebuggerMessage(Vector<uint16_t> message);
   void Shutdown();
=======================================
--- /branches/bleeding_edge/src/debug.cc        Tue Dec 21 02:51:50 2010
+++ /branches/bleeding_edge/src/debug.cc        Tue Jan  4 01:09:50 2011
@@ -3037,7 +3037,8 @@


 MessageDispatchHelperThread::MessageDispatchHelperThread()
-    : sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
+    : Thread("v8:MsgDispHelpr"),
+      sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
       already_signalled_(false) {
 }

=======================================
--- /branches/bleeding_edge/src/log.cc  Thu Dec 16 04:14:56 2010
+++ /branches/bleeding_edge/src/log.cc  Tue Jan  4 01:09:50 2011
@@ -276,7 +276,8 @@
 // Profiler implementation.
 //
 Profiler::Profiler()
-    : head_(0),
+    : Thread("v8:Profiler"),
+      head_(0),
       tail_(0),
       overflow_(false),
       buffer_semaphore_(OS::CreateSemaphore(0)),
=======================================
--- /branches/bleeding_edge/src/platform-freebsd.cc     Tue Dec  7 03:31:57 2010
+++ /branches/bleeding_edge/src/platform-freebsd.cc     Tue Jan  4 01:09:50 2011
@@ -411,6 +411,12 @@


 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
+  set_name("v8:<unknown>");
+}
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  set_names(name);
 }


@@ -428,6 +434,12 @@
   thread->Run();
   return NULL;
 }
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
+}


 void Thread::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-linux.cc       Wed Dec  8 10:08:23 2010
+++ /branches/bleeding_edge/src/platform-linux.cc       Tue Jan  4 01:09:50 2011
@@ -31,6 +31,7 @@
 #include <pthread.h>
 #include <semaphore.h>
 #include <signal.h>
+#include <sys/prctl.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/syscall.h>
@@ -551,6 +552,12 @@


 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
+  set_name("v8:<unknown>");
+}
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  set_name(name);
 }


@@ -563,11 +570,18 @@
// This is also initialized by the first argument to pthread_create() but we
   // don't know which thread will run first (the original thread or the new
   // one) so we initialize it here too.
+  prctl(PR_SET_NAME, thread->name(), 0, 0, 0);
   thread->thread_handle_data()->thread_ = pthread_self();
   ASSERT(thread->IsValid());
   thread->Run();
   return NULL;
 }
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
+}


 void Thread::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-macos.cc       Tue Dec  7 03:31:57 2010
+++ /branches/bleeding_edge/src/platform-macos.cc       Tue Jan  4 01:09:50 2011
@@ -28,6 +28,8 @@
// Platform specific code for MacOS goes here. For the POSIX comaptible parts
 // the implementation is in platform-posix.cc.

+#include <dlfcn.h>
+#include <string>
 #include <unistd.h>
 #include <sys/mman.h>
 #include <mach/mach_init.h>
@@ -411,11 +413,34 @@


 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
+  set_name("v8:<unknown>");
+}
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  set_name(name);
 }


 Thread::~Thread() {
 }
+
+
+
+static void SetThreadName(const 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*);
+  *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
+    dlsym(RTLD_DEFAULT, "pthread_setname_np");
+  if (!dynamic_pthread_setname_np)
+    return;
+
+  // Mac OS X does not expose the length limit of the name, so hardcode it.
+  const int kMaxNameLength = 63;
+  std::string shortened_name = std::string(name).substr(0, kMaxNameLength);
+  dynamic_pthread_setname_np(shortened_name.c_str());
+}


 static void* ThreadEntry(void* arg) {
@@ -424,10 +449,17 @@
   // don't know which thread will run first (the original thread or the new
   // one) so we initialize it here too.
   thread->thread_handle_data()->thread_ = pthread_self();
+  SetThreadName(thread->name());
   ASSERT(thread->IsValid());
   thread->Run();
   return NULL;
 }
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
+}


 void Thread::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-nullos.cc      Mon Dec 20 02:38:19 2010
+++ /branches/bleeding_edge/src/platform-nullos.cc      Tue Jan  4 01:09:50 2011
@@ -335,13 +335,26 @@


 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
+  set_name("v8:<unknown>");
   UNIMPLEMENTED();
 }
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  set_name(name);
+  UNIMPLEMENTED();
+}


 Thread::~Thread() {
   UNIMPLEMENTED();
 }
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
+}


 void Thread::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-openbsd.cc     Tue Dec  7 03:31:57 2010
+++ /branches/bleeding_edge/src/platform-openbsd.cc     Tue Jan  4 01:09:50 2011
@@ -387,6 +387,12 @@


 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
+  set_name("v8:<unknown>");
+}
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  set_name(name);
 }


@@ -404,6 +410,12 @@
   thread->Run();
   return NULL;
 }
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
+}


 void Thread::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-solaris.cc     Tue Dec  7 03:31:57 2010
+++ /branches/bleeding_edge/src/platform-solaris.cc     Tue Jan  4 01:09:50 2011
@@ -401,6 +401,12 @@


 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
+  set_name("v8:<unknown>");
+}
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  set_name(name);
 }


@@ -418,6 +424,12 @@
   thread->Run();
   return NULL;
 }
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
+}


 void Thread::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-win32.cc       Mon Dec 20 02:38:19 2010
+++ /branches/bleeding_edge/src/platform-win32.cc       Tue Jan  4 01:09:50 2011
@@ -1463,6 +1463,19 @@

 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
   data_ = new PlatformData(kNoThread);
+  set_name("v8:<unknown>");
+}
+
+
+Thread::Thread(const char* name) : ThreadHandle(ThreadHandle::INVALID) {
+  data_ = new PlatformData(kNoThread);
+  set_name(name);
+}
+
+
+void Thread::set_name(const char* name) {
+  strncpy(name_, name, sizeof(name_));
+  name_[sizeof(name_) - 1] = '\0';
 }


=======================================
--- /branches/bleeding_edge/src/platform.h      Mon Dec 20 02:38:19 2010
+++ /branches/bleeding_edge/src/platform.h      Tue Jan  4 01:09:50 2011
@@ -387,6 +387,7 @@

   // Create new thread.
   Thread();
+  explicit Thread(const char* name);
   virtual ~Thread();

   // Start new thread by calling the Run() method in the new thread.
@@ -395,6 +396,10 @@
   // Wait until thread terminates.
   void Join();

+  inline const char* name() const {
+    return name_;
+  }
+
   // Abstract method for run handler.
   virtual void Run() = 0;

@@ -417,8 +422,16 @@
   static void YieldCPU();

  private:
+  void set_name(const char *name);
+
   class PlatformData;
   PlatformData* data_;
+
+ // The thread name length is limited to 16 based on Linux's implementation of
+  // prctl().
+  static const int kMaxThreadNameLength = 16;
+  char name_[kMaxThreadNameLength];
+
   DISALLOW_COPY_AND_ASSIGN(Thread);
 };

=======================================
--- /branches/bleeding_edge/src/top.cc  Thu Dec 16 04:26:04 2010
+++ /branches/bleeding_edge/src/top.cc  Tue Jan  4 01:09:50 2011
@@ -170,7 +170,9 @@
 // into for use by a stacks only core dump (aka minidump).
 class PreallocatedMemoryThread: public Thread {
  public:
-  PreallocatedMemoryThread() : keep_running_(true) {
+  PreallocatedMemoryThread()
+    : Thread("v8:PreallocMem"),
+      keep_running_(true) {
     wait_for_ever_semaphore_ = OS::CreateSemaphore(0);
     data_ready_semaphore_ = OS::CreateSemaphore(0);
   }
=======================================
--- /branches/bleeding_edge/src/v8threads.cc    Tue Dec  7 03:01:02 2010
+++ /branches/bleeding_edge/src/v8threads.cc    Tue Jan  4 01:09:50 2011
@@ -380,7 +380,8 @@


 ContextSwitcher::ContextSwitcher(int every_n_ms)
-  : keep_going_(true),
+  : Thread("v8:CtxtSwitcher"),
+    keep_going_(true),
     sleep_ms_(every_n_ms) {
 }

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to