Skip navigation.
Home
The QNX Community Portal

View topic - Query on ClockCycles()

Query on ClockCycles()

Discussion about the general applications that run on QNX6

Query on ClockCycles()

Postby hello » Mon Jan 14, 2013 12:06 pm

Hi all,
I need to calculate the time taken for doing some action(precision in nano seconds). I used ClockCycles() to calculate the time taken. ClockCycles() returns the current value of a free-running 64-bit cycle counter. The application is expected to run continuously for 6 months. So is there any issue in using ClockCycles() API to get time. Will the cycle count reset during this period.

Regards,
hello
hello
Senior Member
 
Posts: 101
Joined: Tue May 08, 2012 1:09 pm

Re: Query on ClockCycles()

Postby denkelly » Mon Jan 14, 2013 2:32 pm

Of course ClockCycles will overflow - probably in the order of a minute or two! Note also that on multicore x86, each core has its own counter - so you want to attach your program to one particular core.
denkelly
Senior Member
 
Posts: 161
Joined: Sat Aug 02, 2008 3:27 pm

Re: Query on ClockCycles()

Postby mario » Mon Jan 14, 2013 10:24 pm

Assume a 2Gig clock the first 31 bit will wrap around every seconds so that leaves 33 bits for representing seconds, hence that`s 8589934592 seconds, or 143165576 minutes or 2386092 hours, or 99240 days or 14202 weeks or 273.133 years. Haven`t double check the math ;-)
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Re: Query on ClockCycles()

Postby denkelly » Tue Jan 15, 2013 12:24 am

Oops. I was thinking of 32-bits. With 64 I guess you need not worry about overflow!
denkelly
Senior Member
 
Posts: 161
Joined: Sat Aug 02, 2008 3:27 pm

Re: Query on ClockCycles()

Postby hello » Tue Jan 15, 2013 3:50 am

Ok , thank you very much
hello
Senior Member
 
Posts: 101
Joined: Tue May 08, 2012 1:09 pm

Re: Query on ClockCycles()

Postby maschoen » Tue Jan 15, 2013 5:45 am

hello wrote:Ok , thank you very much


And don't forget what Dennis said about attaching to a core. ;-).
maschoen
QNX Master
 
Posts: 2644
Joined: Wed Jun 25, 2003 5:18 pm

Re: Query on ClockCycles()

Postby hello » Tue Jan 15, 2013 6:05 am

So before taking the clock cycle, i need to lock the thread to run in a single core and unlock it afterwards. I need time in nano seconds precision. Should i go for any other API?
hello
Senior Member
 
Posts: 101
Joined: Tue May 08, 2012 1:09 pm

Re: Query on ClockCycles()

Postby maschoen » Tue Jan 15, 2013 6:46 pm

hello wrote:So before taking the clock cycle, i need to lock the thread to run in a single core and unlock it afterwards. I need time in nano seconds precision. Should i go for any other API?


That's basically correct. Whether you unlock it afterwards is up to you. The catch in this is whether you are in a hurry. Locking a thread could be time consuming, especially if you do it each time you want to measure a time interval. You could lock the thread at startup and leave it. This might create other issues in your system.

If you have a completely spare cpu, you could lock everything else off of it, and have a single thread on that cpu that calls ClockCycles() in a loop, saving the result to shared memory. Then your threads could read the clock by reading the shared memory. This sounds a little crazy to me, but it could work. In case you consider this, beware of a hyper-threaded cpu.
maschoen
QNX Master
 
Posts: 2644
Joined: Wed Jun 25, 2003 5:18 pm

Re: Query on ClockCycles()

Postby hello » Wed Jan 16, 2013 4:47 am

Ok, I need to get the time frequently so that it will a overhead to lock a thread. Is there any other API to get time in precision of nano seconds
hello
Senior Member
 
Posts: 101
Joined: Tue May 08, 2012 1:09 pm

Re: Query on ClockCycles()

Postby maschoen » Wed Jan 16, 2013 6:02 am

hello wrote:Ok, I need to get the time frequently so that it will a overhead to lock a thread. Is there any other API to get time in precision of nano seconds


If you need to get the time frequently without the overhead of locking a thread to a cpu, you will have to lock it once and leave it locked. This obviously has some disadvantages, but there is no other API to help you out.

You could attach a piece of hardware with a high speed counter, but I can't think of any other way.
maschoen
QNX Master
 
Posts: 2644
Joined: Wed Jun 25, 2003 5:18 pm

Re: Query on ClockCycles()

Postby maschoen » Wed Jan 16, 2013 6:02 am

hello wrote:Ok, I need to get the time frequently so that it will a overhead to lock a thread. Is there any other API to get time in precision of nano seconds


If you need to get the time frequently without the overhead of locking a thread to a cpu, you will have to lock it once and leave it locked. This obviously has some disadvantages, but there is no other API to help you out.

You could attach a piece of hardware with a high speed counter, but I can't think of any other way.
maschoen
QNX Master
 
Posts: 2644
Joined: Wed Jun 25, 2003 5:18 pm

Re: Query on ClockCycles()

Postby hello » Thu Feb 14, 2013 2:11 pm

Hi all,

If ClockCycles() is used on an SMP machine, we must “lock” the thread to a single CPU.
In case of quad core multicore processor, will the locking mechanism work properly ?
We see some variations during testing on quad core. Please help

Regards,
hello
hello
Senior Member
 
Posts: 101
Joined: Tue May 08, 2012 1:09 pm

Re: Query on ClockCycles()

Postby maschoen » Thu Feb 14, 2013 5:13 pm

hello wrote: If ClockCycles() is used on an SMP machine, we must “lock” the thread to a single CPU.
In case of quad core multicore processor, will the locking mechanism work properly ?
We see some variations during testing on quad core. Please help
hello


I don't think this is the case. ClockCycles() should function without locking to a processor. On the other hand, the accuracy of ClockCycles() may not be all that good. At best it would be + or - the timer interrupt frequency.
maschoen
QNX Master
 
Posts: 2644
Joined: Wed Jun 25, 2003 5:18 pm

Re: Query on ClockCycles()

Postby Tim » Thu Feb 14, 2013 6:05 pm

One thing to be aware of is that you *must* turn off 'hyper threading' in the BIOS.

Hyperthreading is NOT a real CPU like a 2nd core is. But to QNX it will appear as a full CPU and threads will be scheduled on it as if they are. The execution time of a thread on a Hyperthread CPU is much slower than on a primary/secondary core.

Tim
Tim
Senior Member
 
Posts: 1392
Joined: Wed Mar 10, 2004 12:28 am

Re: Query on ClockCycles()

Postby mario » Mon Feb 18, 2013 5:48 pm

maschoen wrote:
hello wrote: If ClockCycles() is used on an SMP machine, we must “lock” the thread to a single CPU.
In case of quad core multicore processor, will the locking mechanism work properly ?
We see some variations during testing on quad core. Please help
hello


I don't think this is the case. ClockCycles() should function without locking to a processor. On the other hand, the accuracy of ClockCycles() may not be all that good. At best it would be + or - the timer interrupt frequency.


On modern x86 process ( don't know exactly what model it started), the rdtsc will be the same on every core of a processor, even if a core change frequency to save power.

The RDTSC has a mode to switch it to measure time and not clock cycles ( which is the original intent ). I don't know if QNX switches the mode.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am


Return to QNX6 - Applications

Who is online

Users browsing this forum: No registered users and 1 guest