0

It seems the Linux high resolution timer starts counting within the scope of each thread - not globally in the application.

I tried using CLOCK_PROCESS_CPUTIME_ID but it gives me that thread behavior?

   CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)
          High-resolution per-process timer from the CPU.

   CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)
          Thread-specific CPU-time clock.

Looking for a high resolution timer across threads.
(The global timers are in milli seconds while the high resolution in nanoseconds.)

How do I get high resolution time in a multithreaded environment?

P.S.
  • 384
  • 3
  • 18
  • Doe it need to be human time or compute time. Human time is in seconds and compute time is in instructions? – Mikhail Nov 25 '13 at 00:02
  • @mikhail: compute time as much as possible, looking for nano/micro seconds – P.S. Nov 25 '13 at 00:05
  • What do you mean by *starts within the scope of a threat*? What have you tried so far? According to [clock_gettime(2)](http://man7.org/linux/man-pages/man2/clock_gettime.2.html), Linux provides a high-resolution per-process timer from the CPU. – Oswald Nov 25 '13 at 00:06
  • @oswald: it seems that the time returned is relative from the start of the thread. – P.S. Nov 25 '13 at 00:10
  • @oswald: tried CLOCK_PROCESS_CPUTIME_ID, gives me thread behavior... – P.S. Nov 25 '13 at 00:16
  • There is a way that you can get access to the internal x86 timer registers. If I was writing a profile I would use them. They are completely worthless for things like hardware synchronization because modern CPUs change clock so you can't get a human number from them. They are called RTIMERs or something like that. If I could remember I would post an answer. – Mikhail Nov 25 '13 at 00:29
  • @P.S. Have you considered `getTimeofday` which gives resolution in `microseconds`? Ref: http://linux.die.net/man/2/gettimeofday – Ganesh Nov 25 '13 at 00:32
  • @mikhail: is that the one that is the cpu clock count, I think that one will change with the speed step technology....? – P.S. Nov 25 '13 at 00:40
  • @ganesh: that may work....thanks – P.S. Nov 25 '13 at 00:41
  • @P.S., yes but if you are writing a program that tells you the percentage that the program was in a certain function is this is desired. – Mikhail Nov 25 '13 at 01:02

1 Answers1

1

Please try with getTimeofday which should give time in the resolution of microseconds (Reference: Linux Man Page)

You may also want to refer to this question: How to create a high resolution timer in Linux to measure program performance?

Community
  • 1
  • 1
Ganesh
  • 5,880
  • 2
  • 36
  • 54