ClockCycles() in an interrupt?

I’m using an AMD Elan 520 cpu and QNX RTP 6.1 and need to grab the precise
time of entry into a hardware interrupt. The ClockCycles() function itself
is not callable from within an interrupt so various sources have recommended
inlining of the assembly to perform the task. Unfortunately, I cannot wait
for the thread that receives control from the interrupt to use ClockCycles()
as the result is not consistent enough for our purposes.

I have downloaded this code:

ftp.qnx.com:/usr/free/qnx4/os/utils/examples/Micro_time.tgz

for QNX4, but the included pragma included doesn’t seem to want to compile
and gives me an error under RTP 6.1:

(.text+0xf): undefined reference to ‘rdtsc64’
(where rdtsc64 is the pragma name)

Does anyone have an inline version of a RDTSC instruction working under RTP
6.1 that they’d be willing to share?

Thanks

Jason Farque’

Jason Farque <jasonf@pigging.com> wrote:

I’m using an AMD Elan 520 cpu and QNX RTP 6.1 and need to grab the precise
time of entry into a hardware interrupt. The ClockCycles() function itself
is not callable from within an interrupt so various sources have recommended
inlining of the assembly to perform the task. Unfortunately, I cannot wait
for the thread that receives control from the interrupt to use ClockCycles()
as the result is not consistent enough for our purposes.

Why isn’t it callable from within an interrupt handler? The current docs
show that it is safe.

Problem solved! :wink:

chris

\

Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

“Chris McKillop” <cdm@qnx.com> wrote in message
news:a66v1r$g2t$2@nntp.qnx.com

Jason Farque <> jasonf@pigging.com> > wrote:
I’m using an AMD Elan 520 cpu and QNX RTP 6.1 and need to grab the
precise
time of entry into a hardware interrupt. The ClockCycles() function
itself
is not callable from within an interrupt so various sources have
recommended
inlining of the assembly to perform the task. Unfortunately, I cannot
wait
for the thread that receives control from the interrupt to use
ClockCycles()
as the result is not consistent enough for our purposes.


Why isn’t it callable from within an interrupt handler? The current docs
show that it is safe.

Problem solved! > :wink:

It looks like its callable from interrupt handler, but only on processors
which have rtdsc instruction. I have small test program with ClockCycles(),
it runs fine on my development machine (Athlon based) , but not on my test
machine (it’s 486 based)

Pavol Kycina

Jason Farque <jasonf@pigging.com> wrote:

I’m using an AMD Elan 520 cpu and QNX RTP 6.1 and need to grab the precise
time of entry into a hardware interrupt. The ClockCycles() function itself
is not callable from within an interrupt so various sources have recommended
inlining of the assembly to perform the task. Unfortunately, I cannot wait
for the thread that receives control from the interrupt to use ClockCycles()
as the result is not consistent enough for our purposes.

I have downloaded this code:

ftp.qnx.com:/usr/free/qnx4/os/utils/examples/Micro_time.tgz

for QNX4, but the included pragma included doesn’t seem to want to compile
and gives me an error under RTP 6.1:

(.text+0xf): undefined reference to ‘rdtsc64’
(where rdtsc64 is the pragma name)

Does anyone have an inline version of a RDTSC instruction working under RTP
6.1 that they’d be willing to share?

There are three inline versions of rdtsc in /usr/include/x86/inline.h
for various compilers. However, using rdtsc is no better than calling
ClockCycles directly; look at the imlementation of ClockCycles in
/usr/include/x86/neutrino.h: it basically issues an rdtsc intruction.

On CPUs that do not have the rdtsc instruction, the kernal catches
it as an illegal instruction, and emulates it. This is why it blows
up in an interrupt handler on a CPU without the rdtsc intstruction:
illegal intstructions in a kernal or interrupt context tend to be bad.

It sounds like the docs are incorrect if they indicate that it is
safe to call ClockCycles() in an interrupt handler, either that or
it was supposed to work, but the rdtsc emulation in the kernel is broken.

I still haven’t solved your problem, but if you are only concerned
with a specific system (the AMD Elan one) you could figure out a way
to read a timestamp without using rdtsc; there may be some
timers/counters on the system that will do the trick.

Dave

Chris,

const struct sigevent *HWInterrupt( void *arg, int id )
{
uint64_t InterruptCycles;
InterruptCycles = ClockCycles();
return( &event );
}

This little test blows up in my Elan 520 which apparently doesn’t have a
true RDTSC instruction. ClockCycles() in an interrupt on my processor
evidently crashes some part of the kernel because the system requires a hard
reboot to get things going again. The shell runs but network connections
are permanently lost and other seemingly arbitrary symptoms start cropping
up.

As a side note I have looked high and low for a manual with the instruction
set of the Elan 520 and I can’t find one. There are a couple of monster
documents on AMD’s web site but neither of them contain instruction sets.
If anyone happens to know where they can be found please let me know.

Jason

“Chris McKillop” <cdm@qnx.com> wrote in message
news:a66v1r$g2t$2@nntp.qnx.com

Jason Farque <> jasonf@pigging.com> > wrote:
I’m using an AMD Elan 520 cpu and QNX RTP 6.1 and need to grab the
precise
time of entry into a hardware interrupt. The ClockCycles() function
itself
is not callable from within an interrupt so various sources have
recommended
inlining of the assembly to perform the task. Unfortunately, I cannot
wait
for the thread that receives control from the interrupt to use
ClockCycles()
as the result is not consistent enough for our purposes.


Why isn’t it callable from within an interrupt handler? The current docs
show that it is safe.

Problem solved! > :wink:

chris

\

Chris McKillop <> cdm@qnx.com> > “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Dave,

Yes you appear to be correct about the emulation of rdtsc from within an
interrupt, it’s either absent or broken. If ClockCycles() wasn’t intended
to be interrupt safe on machines without a rdtsc instruction then a caveat
should be in the docs.

Yes, the Elan 520 has available other hardware times that I could read as my
timestamp. My times are all relative so that would work. I was trying not
to write any processor dependent code because the code will definitely be
moved to other non-Elan systems, but this is a relatively small thing and
it’s critical to the program’s function so…

Thank you for the comments.

Jason

“David Donohoe” <ddonohoe@qnx.com> wrote in message
news:a67vpt$bbi$1@nntp.qnx.com

Jason Farque <> jasonf@pigging.com> > wrote:
I’m using an AMD Elan 520 cpu and QNX RTP 6.1 and need to grab the
precise
time of entry into a hardware interrupt. The ClockCycles() function
itself
is not callable from within an interrupt so various sources have
recommended
inlining of the assembly to perform the task. Unfortunately, I cannot
wait
for the thread that receives control from the interrupt to use
ClockCycles()
as the result is not consistent enough for our purposes.

I have downloaded this code:

ftp.qnx.com:/usr/free/qnx4/os/utils/examples/Micro_time.tgz

for QNX4, but the included pragma included doesn’t seem to want to
compile
and gives me an error under RTP 6.1:

(.text+0xf): undefined reference to ‘rdtsc64’
(where rdtsc64 is the pragma name)

Does anyone have an inline version of a RDTSC instruction working under
RTP
6.1 that they’d be willing to share?

There are three inline versions of rdtsc in /usr/include/x86/inline.h
for various compilers. However, using rdtsc is no better than calling
ClockCycles directly; look at the imlementation of ClockCycles in
/usr/include/x86/neutrino.h: it basically issues an rdtsc intruction.

On CPUs that do not have the rdtsc instruction, the kernal catches
it as an illegal instruction, and emulates it. This is why it blows
up in an interrupt handler on a CPU without the rdtsc intstruction:
illegal intstructions in a kernal or interrupt context tend to be bad.

It sounds like the docs are incorrect if they indicate that it is
safe to call ClockCycles() in an interrupt handler, either that or
it was supposed to work, but the rdtsc emulation in the kernel is broken.

I still haven’t solved your problem, but if you are only concerned
with a specific system (the AMD Elan one) you could figure out a way
to read a timestamp without using rdtsc; there may be some
timers/counters on the system that will do the trick.

Dave