DTrace: Tracing Specific Kernel Threads

Sometimes we want to focus on profiling specific kernel threads. One approach is to trace everything and then use postprocessing scripts to extract the information of interest. The other is to use DTrace predicates for filtering.
A difficulty with kernel threads is that they all have process id and thread id set to 0. Therefore, I looked into the definition of the _kthread structure in /usr/include/sys/thread.h hoping to find alternative filtering citeria:

typedef struct _kthread {
[...truncated...]
void    (*t_startpc)(void);     /* PC where thread started */
[...truncated...]

And there is one: the structure contains the program counter (PC) of the procedure where the thread was started.

For example, the following script will profile only Emulex completion threads (emlxs`emlxs_thread):

#!/usr/sbin/dtrace -s
profile-997
/ (uintptr_t)curthread->t_startpc == (uintptr_t)&emlxs`emlxs_thread /
{
  @[stack()] = count() ;
}

[...truncated...]
unix`pg_cmt_at_capacity+0x2c
unix`disp_getwork+0x1bd
unix`disp+0xd7
unix`swtch+0xad
genunix`cv_wait+0x60
emlxs`emlxs_thread+0x11d
unix`thread_start+0x8
Thanks for sharing

Nenad Noveljic

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.