2

I am trying to get the total time a particular thread spent so far programatically.

getrusage returns a thread's CPU time but I want the total time i.e. including the time spent by the thread being blocked for whatever reason.

Please note that I will be making use of this functionality by instrumenting a given program using a profiler that I wrote.

A program may have many threads (I am focusing on profiling servers so there can be many). At any given time I would want to know how much time a particular thread spent (so far). So its not convenient to start a timer for every thread as they are spawned. So I would want something of usage similar to getrusage e.g. it returns the total time of the current thread or maybe I can pass to it a thread id. So manual mechanisms like taking a timestamp when the thread was spawned and one later then taking their difference won't be very helpful for me.

Can anyone suggest how to do this?

Thanks!

Dev2017
  • 857
  • 9
  • 31
  • 1
    You could use `chrono::steady_clock` to measure the total elapsed time since your thread started. – paddy Mar 06 '17 at 11:44
  • I've found [boost cpu timer](http://www.boost.org/doc/libs/1_63_0/libs/timer/doc/cpu_timers.html) to be useful in the past. It gives user, system and wall clock times by default if I remember correctly. – G.M. Mar 06 '17 at 11:46
  • What Operating System? – Galik Mar 06 '17 at 11:49
  • guys I edited the question to give more explanation of how I will use this function, please take a look :) – Dev2017 Mar 06 '17 at 11:49
  • @Galik ubuntu 14.04 – Dev2017 Mar 06 '17 at 11:50
  • 1
    Maybe try `clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...);` – Galik Mar 06 '17 at 11:54
  • Nope, it also return the CPU time. Please read my question carefully :) – Dev2017 Mar 06 '17 at 11:55
  • Then don't you just want an ordinary, non thread specific clock like @paddy suggested? – Galik Mar 06 '17 at 11:58
  • a program may have many many threads (I am focusing on profiling servers so they can be many). At any given time I would want to know how much time a particular thread spent (so far). So its not convenient to start a timer for every thread as they are spawned. – Dev2017 Mar 06 '17 at 12:00
  • 3
    Boost is the jquery of c++. The standard library should be sufficient for everything, but every one just likes black magic. – Iharob Al Asimi Mar 06 '17 at 12:10
  • 1
    Sounds like you want to know how long the thread spends _in context_ whether blocked or not. Maybe you want to look at the source code for **strace** and see how it knows. – paddy Mar 06 '17 at 12:14
  • 2
    @paddy finally someone got me haha. Yes that is what I want. Can you elaborate more on strace? – Dev2017 Mar 06 '17 at 12:16
  • Not really, I barely understand it. Usually I use Instruments on OS X which is basically a fancy front-end for strace. What you're looking for is some way to track a thread's context switches. You might need to log them and compute totals later. – paddy Mar 06 '17 at 12:19
  • It is not clear how your thread time is different from the wall clock time. Why two different threads from the same process or from different processes should show different times? Is there time that the thread is considered not running? If so, what is it exactly? – n. m. could be an AI Mar 06 '17 at 13:55
  • 1
    @paddy "In context" == running. Blocked threads don't busy-wait, they are preempted and put in the queue. A thread that busy-waits is not considered blocked. – n. m. could be an AI Mar 06 '17 at 14:19

2 Answers2

0

Save the current time at the point when the thread is started. The total time spent by the thread, counting both running and blocked time, is then just:

current_time - start_time

Of course this is almost always useless/meaningless, which is why there's no dedicated API for it.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • That is not what I wanted. – Dev2017 Mar 06 '17 at 13:16
  • 2
    @DanishAlsayed Judging by what you have written, this is exactly what you want, so you need to describe what you want differently. – n. m. could be an AI Mar 06 '17 at 13:56
  • 1
    No, we want to understand your question, which in its current state is rather difficult. The only sensible interpretation of "total time i.e. including the time spent by the thread being blocked for whatever reason" is "wall clock tine" but you explicitly reject wall clock time and don't offer any alternative interpretation. – n. m. could be an AI Mar 08 '17 at 10:14
  • Let me say it in other words; I rejected the idea of taking 2 time stamps and then calculating their difference. I was asking for an API/existing library that can return me this time value OR a library that can return my the time when the thread was created, then I can subtract it from the current timestamp. – Dev2017 Mar 08 '17 at 17:48
0

Depending on what you want to use this for, one possibility to think about is to sum the number of clock ticks consumed during blocking, which typically is slow enough hide a little overhead like that. So from that sum and the surrounding thread interval you also measure, you can compute the real time load on your thread over that interval. Of course, time-slicing with other processes will throw this off some amount, and capturing all blocking may be very easy or very hard, depending on your situation.

Chris Cochran
  • 321
  • 2
  • 9