In a Linux/GNU/C environment, is there any visibility a running thread has into whether it has been put to sleep. For example say you have a function like
void foo() {
startClock();
bar();
endClock();
}
But you're only concerned with the running time of the code itself. I.e. you don't care about any time related to the thread being suspended during the run. Ideally you'd be able to leverage some system call or library, like countThreadSwitches(), such that:
void foo() {
int lastCount = countThreadSwitches();
startClock();
bar();
endClock();
if (countThreadSwitches() != lastCount)
discardClock();
}
Being able to tell whether the thread has switched in between two statements, would allow us to only measure runs unaffected by context switches.
So, is there anything like that hypothetical countThreadSwitches() call? Or is that information opaque to the thread itself?
foo
10000 times, and keeping a histogram of the time taken. Assuming thatfoo
is fairly short (less than 10msec), most of the times will be grouped at the low end of the timescale, which is the actual runtime.clock
return the process time. With multiple threads in a single process, I don't think thatclock
will give you the thread time.