Tracing Pthread Scheduling

How to trace pthread scheduling in Linux?

strace - simple tool to trace process behavior, but seems can not check the core id.

lttng - need patch the kernel, but more impressive, you can know exactly what each core is doing like context switch/interrupt handling.

Update: As Mathieu Desnoyers mentioned, lttng can be used as Linux kernel modules since 2.6.36, no kernel patching needed now.

pthread scheduling

I assume that what you're after is simulating a scheduler for some OS, using pthreads as the basis for your implementation. What you can do is to create a pthread for each simulated thread, and put an implicit wait on a condition variable in each of the threading primitives of your OS. You can then, by picking which condition variable to signal, decide which thread gets to run when. I'm glossing over the details (ask if you want more info), but it's fairly straight-forward to implement.

Keep track of pthread

  1. A thread's entire identity resides in pthread_t

  2. Initializing a thread returns its pthread_t typed ID to its parent

  3. Each thread can get it's own ID with pthread_self()

  4. You can compare thread IDs using the function:int pthread_equal (pthread_t, pthread_t)

So: Maintain a common data structure where you can store thread status as STARTED, RUNNING, FINISHED using the pthread_t IDs and pthread_equal comparison function to differentiate between the threads. The parent sets the value to STARTED when it starts the thread, the thread itself sets its own state to RUNNING, does its work, and sets itself to FINISHED when done. Use a mutex to make sure values are not changed while being read.

EDIT:
You can set up a sort of 'thread destructor' using pthread_cleanup_push:
http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cleanup_pop.html

i.e. register a routine to be called when the thread exits (either itself, or by cancellation externally). This routine can update the status.

litmus-rt scheduling traces

Seems like the Feather-Trace wasn't enabled by default when applying the litmus-rt patch to my kernel. After entering menuconfig (make menuconfig) I disabled the RELOCATABLE dependency under processor type and features. Afterwards the option Feather-Trace Infrastructure was made visible, found under LITMUS RT -> tracing.



Related Topics



Leave a reply



Submit