3

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?

6
  • 1
    The information can be inferred by running foo 10000 times, and keeping a histogram of the time taken. Assuming that foo 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. Commented Oct 14, 2016 at 3:56
  • 2
    If you're calling the clock() system function, then you're already getting clock values back that only take into account the times at which you thread was actually running. (i.e. the values returned by clock() only increase when your thread is actually running) Commented Oct 14, 2016 at 4:01
  • @JeremyFriesner Doesn't clock return the process time. With multiple threads in a single process, I don't think that clock will give you the thread time. Commented Oct 14, 2016 at 5:07
  • @Jeremy Using CLOCK_THREAD_CPUTIME_ID isn't a bad suggestion. Unfortunately the overhead is about 6X worse than CLOCK_REALTIME.
    – user79126
    Commented Oct 14, 2016 at 5:31
  • 1
    @user3386109 That's a good idea. Unfortunately assume that the code in question has a long right tail, which is of particular interest. The median can be ascertained reliably, but if we want a reliable estimate of the 99.9th percentile.
    – user79126
    Commented Oct 14, 2016 at 5:34

2 Answers 2

6

In linux int getrusage(int who, struct rusage *usage); can be used to fill a struct containing timeval ru_utime (user CPU time used) and timeval ru_stime (system CPU time used), for a thread or a process.

These values along with the system clock will let you know how much CPU time your process/thread was actually running compared to how much time wasn't spent running your process/thread.

For example something like (ru_time + ru_stime) / (clock_endtime - clock_startstart) * 100 will give you CPU usage as a percent of the time elpased between start and end.

There are also some stats in there for number of context switches under certain circumstances, but that info isn't very useful.

1
  • Great suggestion, pretty close to what I'm looking for. Thanks
    – user79126
    Commented Oct 15, 2016 at 21:19
5

On Linux you can read and parse the nonvoluntary_ctxt_switches: line from /proc/self/status (probably best to just do a single 4096-byte read() before and after, then parse them both afterwards).

1
  • That's pretty clever, good point about parsing lazily. Thanks
    – user79126
    Commented Oct 15, 2016 at 21:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.