Pthreads: programmatically collect time spent in different states?

In C, Linux 3.2: Is there a way to programmatically collect statistics about the state of each pthread thread pthread in a program? For example, I would like to get the time spent executing each thread, and in the idle state.

+6
source share
2 answers

clock_gettime() can return the processor time, depending on the thread. Just do:

 struct timespec ts; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); 

But, in my opinion, this is the sum of the user and system time of this stream. You should also consider the Warning message regarding SMP systems at the end of the man page.

Also, if you do not want to get time information in the current thread, but on some kind of pthread, you can use clockid_t to use with clock_gettime() using int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id) .

+1
source

getrusage ()

EDIT: to get the idle time, I would subtract the system and user time from the total time when the thread was active.

Other tools you can use for sensing include: system tap, swtrace, tprof, oprofile, perf, sysprof, ptt, etc.

+1
source

Source: https://habr.com/ru/post/916786/


All Articles