views:

613

answers:

2

I'm using this dtrace script from here to try to find when context switches occur for the threads of a java program.

I'm trying to match the data gathered from the script with trace data gathered from the running program (things like method entry/exit). I get the pthread id of the running thread using a short JNI method that simply returns the value of pthread_self().

The problem i'm having is that the thread id I get from calling pthread_self() is completely different from any thread id I get in the dtrace script. I'm wondering if it's because i'm calling pthread_self() incorrectly since it returns a pointer, however it's been hard to find information about what pthread_t actually is on mac osx.

+1  A: 

From /usr/include/pthread.h:

typedef __darwin_pthread_t pthread_t;

then from /usr/include/sys/_types.h:

struct _opaque_pthread_t {
  long __sig;
  struct __darwin_pthread_handler_rec* __cleanup_stack;
  char __opaque[__PTHREAD_SIZE__];
};
typedef struct _opaque_pthread_t* __darwin_pthread_t;

Source code is your friend :)

Nikolai N Fetissov
+2  A: 

So i'll answer my own question, the curthread and tid variables in dtrace are the pointer values for the kernal thread structures, to get these values to compare dtrace with user space thread data I had to create a kernel extension to get these internal values for threads in user space.

In general this is a bad idea since it's non-portable, could easily break if the kernel was changed and is probably a security risk. Unfortunately I haven't found another way to achieve what I want.

Paul Johnson
+1 for finding a solution ... would be even better if you could post it :)
Nikolai N Fetissov