viced: If platform supports setting a thread title, do so
authorGarrett Wollman <wollman@csail.mit.edu>
Fri, 8 Jul 2011 04:09:43 +0000 (00:09 -0400)
committerDerrick Brashear <shadow@dementia.org>
Mon, 11 Jul 2011 18:32:11 +0000 (11:32 -0700)
Some pthread libraries support setting a name or title for individual
threads (analogous to setproctitle() for processes).  This can be useful
for debugging and is sometimes published for use by utilities like ps
(again like setproctitle() for processes).  The two most common variants
of this have the same signature with slightly different function names.
If either one is present, use it in viced (which already assigns a thread
name when compiled for LWP but ignores it in pthreads compilations).

Change-Id: I5486aa6a21dbc3c8885b94ad52c2b1a66baae81f
Reviewed-on: http://gerrit.openafs.org/4950
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementia.org>

acinclude.m4
src/viced/viced.c

index 8509253..41a0019 100644 (file)
@@ -1258,6 +1258,7 @@ AC_CHECK_HEADERS([ \
                   ncurses.h \
                   netdb.h \
                   netinet/in.h \
+                  pthread_np.h \
                   pwd.h \
                   regex.h \
                   resolv.h \
@@ -1553,6 +1554,15 @@ else
     AC_MSG_RESULT(no)
 fi
 
+dnl Look for "non-portable" pthreads functions.
+save_LIBS="$LIBS"
+LIBS="$LIBS $PTHREAD_LIBS"
+AC_CHECK_FUNCS([ \
+       pthread_set_name_np \
+       pthread_setname_np \
+])
+LIBS="$save_LIBS"
+
 AC_TYPE_SIGNAL
 OPENAFS_RETSIGTYPE
 AC_CHECK_SIZEOF(void *)
index 3aaa621..d107244 100644 (file)
@@ -75,6 +75,9 @@
 #include "host.h"
 #ifdef AFS_PTHREAD_ENV
 # include <afs/softsig.h>
+# ifdef HAVE_PTHREAD_NP_H
+#  include <pthread_np.h>
+# endif /* HAVE_PTHREAD_NP_H */
 #endif
 #if defined(AFS_SGI_ENV)
 # include "sys/schedctl.h"
@@ -416,6 +419,14 @@ setThreadId(char *s)
     /* set our 'thread-id' so that the host hold table works */
     pthread_setspecific(rx_thread_id_key,
                        (void *)(intptr_t)rx_NewThreadId());
+# if defined(HAVE_PTHREAD_SET_NAME_NP)
+    /* The "NP" stands for "non-portable" so it's only just that
+     * implementations disagree about the name of the function.
+     */
+    pthread_set_name_np(pthread_self(), s);
+# elif defined(HAVE_PTHREAD_SETNAME_NP)
+    pthread_setname_np(pthread_self(), s);
+# endif
     ViceLog(0,
            ("Set thread id %p for '%s'\n",
             pthread_getspecific(rx_thread_id_key), s));