X

News, tips, partners, and perspectives for the Oracle Solaris operating system

Named threads in Oracle Solaris 11.3

We've added a new feature in Solaris 11.3, the ability to name threads.  With this feature, you can now give a semantically meaningful name to a thread.  This can make life easier when trying to figure out which threads in your application are doing what.

These are the new functions that have been added for this feature:

int pthread_setname_np(pthread_t t, const char *name );

int pthread_getname_np(pthread_t t, char *buf, size_t len);

int pthread_attr_setname_np(pthread_attr_t *attr, const char *name);

int pthread_attr_getname_np(pthread_attr_t *attr, char *buf, size_t len);


pthread_setname_np(3C) allows an existing thread to be named.  pthread_attr_setname_np(3C) allows a thread to be named before it is  created.  Both pthread_getname_np(3C) and pthread_attr_getname_np(3C)  let you retrieve the name of a thread.

Thread names are exposed by prstat(1M) and ps(1).  For example, 'ps -Loutput has been modified to include the thread name (LNAME):

$ ps -L

  PID   LWP LNAME     TTY         LTIME CMD

 2644     1 -         pts/32       0:00 bash

14320     1 moe       pts/32       0:00 a.out

14320     2 curly     pts/32       0:00 a.out

14320     3 larry     pts/32       0:00 a.out

14320     4 shemp     pts/32       0:00 a.out

14321     1 -         pts/32       0:00 ps

$

Similarly, a format specifier has been added for the '-o' option to ps(1):

$ ps -L -o pid,lwp,lname,fname

  PID   LWP LNAME     COMMAND

 2644     1 -         bash

13421     1 moe       a.out

13421     2 curly     a.out

13421     3 larry     a.out

13421     4 shemp     a.out

13422     1 -         ps

prstat(1M) now displays the thread name instead of the thread ID, if it has been set.  (If the thread hasn't been named, the thread ID is displayed.) For example:

# prstat -Lmp `pgrep nscd` 5

   PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG LWPID PROCESS/LWPNAME

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   4   0  37   0  1762 nscd/server_tsd_bind

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   1   0  13   0  5661 nscd/server_tsd_bind

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   1   0  14   0   200 nscd/server_tsd_bind

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   7   0  21   0     2 nscd/set_smf_state

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   0   0   0   0  5662 nscd/server_tsd_bind

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   0   0   0   0  5660 nscd/reaper

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   0   0   0   0  5659 nscd/revalidate

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   0   0   0   0  5657 nscd/reaper

100357 root     0.0 0.0 0.0 0.0 0.0 0.0 100 0.0   0   0   0   0  5656 nscd/revalidate

[ ... ]

We've also added a new variable to DTrace, uthreadname.  (This is for the userspace thread name. We've also added kthreadname, as we also allow for naming kernel threads.)  The following DTrace script would tell you which threads in your application are most active:

profile-397

/ pid == $target /

{

        @[uthreadname] = count();

}

 The output from this script might appear as follows:

$ ./uthr.d -p `pgrep a.out`

dtrace: script './uthr.d' matched 1 probe

^C

shemp             29

larry             35

curly             36

moe             4423

Join the discussion

Comments ( 1 )
  • Carlos Azevedo Sunday, June 25, 2017
    This is cool, but let me ask you, after a thread has been created how can I query its attributes if I can't count on the intial attr structures anymore?
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.