time between two ClockCycles seems not accurate?

I need to get interrupt time in ISR.
So my method is that :

  1. First when program start ,get start_time(nsec) and ClockCycles1 count.
  2. In ISR ,get ClockCycles2 count.
    So the current_time(nsec) = start_time(nsec)
    +1000000000L/SYSPAGE_ENTRY(qtime)->cycles_per_sec
    *(ClockCycles2 -
    ClockCycles1 )
    the interrupt happened about every 5 second.when I get the
    current_time,and convert it into string to print.
    I find a problem: With the time go on (after several minutes),the
    difference between current_time the program and the real clock system is
    more and more.

So I simulate it in a normal program,the problem happened too.

the program is in accessories,named “timecha.c”

Is the problem with ClockCycles()or other else.I can’t understand.


begin 666 Timcha.c
M(VEN8VQU9&4@/’-Y<R]N975T<FEN;RYH/@HC:6YC;‘5D92 :6YT=‘EP97,N
M:#X*(VEN8VQU9&4@/’-T9&EO+F@^"B-I;F-L=61E(#QS=&1L:6(N:#X*(VEN
M8VQU9&4@/’-Y<R]S>7-P86=E+F@^“B-I;F-L=61E(#QT:6UE+F@^”@II;G0@
M;6%I;B@@=F]I9" I"GL*(" @(‘5I;G0V-%]T("!F<W1?8WEC;&4L9G-T7VYS
M96,[(" @(" O+V9I<G-T(&YS96,@86YD(&-Y8VQE+6-O=6YT"B @("!U:6YT
M-C1?=" @8W5R7V-Y8VQE+&-U<E]N<V5C.R @+R]S96-O;F0@;G-E8R!A;F0@
M8WEC;&4M8V]U;G0*“B @(”!S=’)U8W0@=&EM97-P96,@9G-T7W1I;64[+R]F
M:7)S="!T:6UE"B @("!S=’)U8W0@=&EM97-P96,@8W5R7W1I;64[+R]S96-O
M;F0@=&EM90H@(" @<W1R=6-T(‘1I;65S<&5C(’)E86Q?=&EM93LO+R!R96%L
M(’-Y<W1E;2!T:6UE"B (" @("\O9V5T(&9I<G-T(&-Y8VQE+6-O=6YT"B @
M("!F<W1?8WEC;&4@/2!#;&]C:T-Y8VQE<R@I.PH@(" @+R]G970@9FER<W0@
M=&EM90H@(" @8VQO8VM?9V5T=&EM92A#3$]#2U]214%,5$E-12P@)F9S=%]T
M:6UE
3L*(" @("\O9V5T(&9I<G-T(‘1I;64@;G-E8R (" @(&9S=%]N<V5C
M(" @/2!T:6UE<W!E8S)N<V5C
"9F<W1?=&EM92D["@H@“B @(”!W:&EL92@Q
M0H@(" @>PH@(" @+R@9&@<V]M971H:6YG( H@(" @<VQE97 H-2D[“B @
M(” (" @("\O9V5T(&-U<G)E;G0@(’)E86P@<WES=&5M('1I;64(" @(&-L
M;V-K7V=E='1I;64H0TQ/0TM?4D5!3%1)344L)G)E86Q?=&EM92D[“B @(” O
M+R!G970@8W5R<F5N=" @8VQY8VQE+6-O=6YT"B @("!C=7)?8WEC;&4@/2!#
M;&]C:T-Y8VQE<R@@3L(" @("\O9V5T(&-U<G)E;G0@(&YS96,
(" @(&-U
M<E]N<V5C/2@Q,# P,# P,# P3"]365-004=%7T5.5%)9*’%T:6UE2T^8WEC
M;&5S7W!E<E]S96,I
BAC=7)?8WEC;&4M9G-T7V-Y8VQE2MF<W1?;G-E8SL
M(" @("\O8V]N=F5R=" @8W5R<F5N="!N<V5C(‘1O(&-U<G)E;G0@=&EM90H@
M(" @;G-E8S)T:6UE<W!E8R@F8W5R7W1I;64L8W5R7VYS96,I.PH@(" @“B @
M(” O+W!R:6YT(&-U<G)E;G0@=&EM90H@(" @<’)I;G1F*"(@(&-U<G)E;G0@
M=&EM92 @(" @:7,@)7,B+"!C=&EM92@H=&EM95]T(“HI)F-U<E]T:6UE+G1V
M7W-E8RDI.PH@(” @+R]P<FEN="!S>7-T96T@<F5A;"!T:6UE"B @("!P<FEN
M=&8H(B @<WES=&5M(’)E86P@=&EM92!I<R E<UQN(BP@8W1I;64H*'1I;65?
M=" J29R96%L7W1I;64N='9?<V5C2D["B (" @(&9F;'5S:“AS=&1O=70I
E.PH@(”!]“B @( H@(” @<F5T=7)N($58251?4U5#0T534SL
?0``
`
end

I see two things, first sleep 5 last not 5 seconds but
5 seconds + one tick. Check
http://www.qnx.com/developer/articles/index.html?article=oct2300a
for the explanation

The other thing is the calculation of cur_nsec. The operation
should be perform in float or double. Assuming 450Mhz
clock. 1000000000/450000000 = 2.2 which
can’t be represented in an integer formant so it gets truncated
to 2, not good :wink:

Beware, according to the documenation ClockCycles is not safe
to be called from an interrupt
http://www.qnx.com/developer/docs/momentics_nc_docs/neutrino/lib_ref/c/clock
cycles.html
That being said on x86 with Pentium processor and above it’s ok.

“zhz_zhang” <zhz_zhang26@sina.com> wrote in message
news:ag0u4e$9ls$1@inn.qnx.com

I need to get interrupt time in ISR.
So my method is that :

  1. First when program start ,get start_time(nsec) and ClockCycles1
    count.
  2. In ISR ,get ClockCycles2 count.
    So the current_time(nsec) = start_time(nsec)
    +1000000000L/SYSPAGE_ENTRY(qtime)->cycles_per_sec
    *(ClockCycles2 -
    ClockCycles1 )
    the interrupt happened about every 5 second.when I get the
    current_time,and convert it into string to print.
    I find a problem: With the time go on (after several minutes),the
    difference between current_time the program and the real clock system is
    more and more.

So I simulate it in a normal program,the problem happened too.

the program is in accessories,named “timecha.c”

Is the problem with ClockCycles()or other else.I can’t understand.

My Qnx system is 6.1.0A not 6.2,my cpu is PIII 500MHz.
The doc about ClockCycles is safe to be called in an interrupt.
And there is no crash in my interrupt program.

I have slove the problem.You are right,the calculation of cur_nsec
should be perform in float or double.
And I change my program “timcha.c”,to set a temp double argument to get the
cur_nsec.

Now,
The cur_time which the ClockCycles count is almost as same as
the real_time which clock_gettime count with the time go on.


“Mario Charest” postmaster@127.0.0.1 wrote in message
news:ag1g0h$mhl$1@inn.qnx.com

I see two things, first sleep 5 last not 5 seconds but
5 seconds + one tick. Check
http://www.qnx.com/developer/articles/index.html?article=oct2300a
for the explanation

The other thing is the calculation of cur_nsec. The operation
should be perform in float or double. Assuming 450Mhz
clock. 1000000000/450000000 = 2.2 which
can’t be represented in an integer formant so it gets truncated
to 2, not good > :wink:

Beware, according to the documenation ClockCycles is not safe
to be called from an interrupt

http://www.qnx.com/developer/docs/momentics_nc_docs/neutrino/lib_ref/c/clock
cycles.html
That being said on x86 with Pentium processor and above it’s ok.

“zhz_zhang” <zhz_zhang26@sina.com> wrote in message
news:ag3n9l$crk$1@inn.qnx.com

My Qnx system is 6.1.0A not 6.2,my cpu is PIII 500MHz.
The doc about ClockCycles is safe to be called in an interrupt.
And there is no crash in my interrupt program.

This was an error in the 6.1 doc :wink:

I have slove the problem.You are right,the calculation of cur_nsec
should be perform in float or double.
And I change my program “timcha.c”,to set a temp double argument to get
the
cur_nsec.

Now,
The cur_time which the ClockCycles count is almost as same as
the real_time which clock_gettime count with the time go on.


“Mario Charest” postmaster@127.0.0.1 wrote in message
news:ag1g0h$mhl$> 1@inn.qnx.com> …

I see two things, first sleep 5 last not 5 seconds but
5 seconds + one tick. Check
http://www.qnx.com/developer/articles/index.html?article=oct2300a
for the explanation

The other thing is the calculation of cur_nsec. The operation
should be perform in float or double. Assuming 450Mhz
clock. 1000000000/450000000 = 2.2 which
can’t be represented in an integer formant so it gets truncated
to 2, not good > :wink:

Beware, according to the documenation ClockCycles is not safe
to be called from an interrupt


http://www.qnx.com/developer/docs/momentics_nc_docs/neutrino/lib_ref/c/clock
cycles.html
That being said on x86 with Pentium processor and above it’s ok.


\

zhz_zhang <zhz_zhang26@sina.com> wrote:

My Qnx system is 6.1.0A not 6.2,my cpu is PIII 500MHz.
The doc about ClockCycles is safe to be called in an interrupt.

The doc is wrong… ClockCycles() is just USUALLY safe to call
in an interrupt handler. On some hardware (e.g. 486) it is not,
since that doesn’t have any native operation to do the work, so
the kernel has to emulate it… effectively making a kernel call
inside a interrupt handler, which is bad.

On hardware (e.g. Pentium with rdtsc opcode available or
PPC with a decrementor register) this will be safe to use
in an irq handler.

And there is no crash in my interrupt program.

The crash would be of the whole OS if you got this wrong, not
just in the interrupt program – but if it works, you’re ok on
your hardware.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Would you re-post your modified code? I am interested in this also and have
made the changes suggested but I must have missed something because my
results are not the same.
KenR

“zhz_zhang” <zhz_zhang26@sina.com> wrote in message
news:ag3n9l$crk$1@inn.qnx.com

My Qnx system is 6.1.0A not 6.2,my cpu is PIII 500MHz.
The doc about ClockCycles is safe to be called in an interrupt.
And there is no crash in my interrupt program.

I have slove the problem.You are right,the calculation of cur_nsec
should be perform in float or double.
And I change my program “timcha.c”,to set a temp double argument to get
the
cur_nsec.

Now,
The cur_time which the ClockCycles count is almost as same as
the real_time which clock_gettime count with the time go on.


“Mario Charest” postmaster@127.0.0.1 wrote in message
news:ag1g0h$mhl$> 1@inn.qnx.com> …

I see two things, first sleep 5 last not 5 seconds but
5 seconds + one tick. Check
http://www.qnx.com/developer/articles/index.html?article=oct2300a
for the explanation

The other thing is the calculation of cur_nsec. The operation
should be perform in float or double. Assuming 450Mhz
clock. 1000000000/450000000 = 2.2 which
can’t be represented in an integer formant so it gets truncated
to 2, not good > :wink:

Beware, according to the documenation ClockCycles is not safe
to be called from an interrupt


http://www.qnx.com/developer/docs/momentics_nc_docs/neutrino/lib_ref/c/clock
cycles.html
That being said on x86 with Pentium processor and above it’s ok.


\