U-Boot-V2:Common:Clock Handle case of clock rollover for get_time_ns
Menon
PATCH 2/17 U-Boot-V2:Common:Clock Handle case of clock rollover for get_time_ns
get_time_ns does a simplistic delta = cycle_now - cycle_last. It is possible that the h/w counter reached max and reset back to 0. This patch addresses this issue by checking for rollover condition.
NOTE 1: This does not guarantee that you cannot confuse get_time_ns. You could possibly wait for two reset cycles and then get a messed up value.
To fix that we may need interrupt mode timer tick - something on the lines of jiffies on linux.
NOTE 2: the question of cs->mask is not clear. if the mask is for the tick, then it is better done with (cycle_now & cs->mask) - (cs->cycle_last & cs->mask). Do we need min and max for register read?
--- common/clock.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
common/clock.c +12 -2
@@ -37,14 +37,24 @@uint64_t get_time_ns (void ) {struct clocksource *cs = current_clock; -uint64_t cycle_now, cycle_delta; +uint64_t cycle_now, cycle_delta =0 ;uint64_t ns_offset;/* read clocksource: */ cycle_now = cs->read ();/* calculate the delta since the last call: */ - cycle_delta = (cycle_now - cs->cycle_last) & cs->mask; + +/* Handle rollover case */ +if (cycle_now < cs->cycle_last) { +/* FIXME: I am not convinced about the cs->mask operation.. +* I am assuming cs->mask is max! Probably need to change +* clocksource structure to have min and max +*/ + cycle_delta = cs->mask - (cs->cycle_last & cs->mask); + cs->cycle_last =0 ; + } + cycle_delta += (cycle_now & cs->mask) - (cs->cycle_last & cs->mask);/* convert to nanoseconds: */ ns_offset =cyc2ns (cs, cycle_delta);
PATCH 2/17 U-Boot-V2:Common:Clock Handle case of clock rollover for get_time_ns
Quoted 20 lines · Nishanth
get_time_nsdoes a simplistic delta =cycle_now-cycle_last. It is possible that the h/w counter reached max and reset back to 0. This patch addresses this issue by checking for rollover condition.NOTE 1: This does not guarantee that you cannot confuse
get_time_ns. You could possibly wait for two reset cycles and then get a messed up value.
To fix that we may need interrupt mode timer tick - something on the lines of jiffies on linux.
NOTE 2: the question ofcs->maskis not clear. if the mask is for the tick, then it is better done with (cycle_now&cs->mask) - (cs->cycle_last&cs->mask). Do we need min and max for register read?Signed-off-by Nishanth Menon<[email protected]>--- common/clock.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)Index:
u-boot-v2.git/common/clock.c
===================================================================common/clock.c +0
-0@@ -37,14 +37,24 @@uint64_t get_time_ns (void ) {
common/clock.c line 47
Quoted 10 lines
common/clock.c +1
-2struct clocksource *cs = current_clock; -uint64_t cycle_now, cycle_delta; +uint64_t cycle_now, cycle_delta =0 ;uint64_t ns_offset;/* read clocksource: */ cycle_now = cs->read ();/* calculate the delta since the last call: */ - cycle_delta = (cycle_now - cs->cycle_last) & cs->mask;
Look closer, the rollover case is handled implicitly by the unsigned arithmetics.
You are right, we do not have a possibility to detect a double rollover without interrupts. Normally this is not an issue as this code is used in timeout polling loops where the current time is polled often enough. Anyway, maybe some comment should mention that this function is not useful to measure long periods of time where 'long' is highly architecture specific.
regards,
Sascha
Quoted 15 lines
common/clock.c +11
-0+ +/* Handle rollover case */ +if (cycle_now < cs->cycle_last) { +/* FIXME: I am not convinced about the cs->mask operation.. +* I am assuming cs->mask is max! Probably need to change +* clocksource structure to have min and max +*/ + cycle_delta = cs->mask - (cs->cycle_last & cs->mask); + cs->cycle_last =0 ; + } + cycle_delta += (cycle_now & cs->mask) - (cs->cycle_last & cs->mask);/* convert to nanoseconds: */ ns_offset =cyc2ns (cs, cycle_delta);
Signature · 4 lines
Pengutronix e.K. - Linux Solutions for Science and Industry ----------------------------------------------------------- Kontakt-Informationen finden Sie im Header dieser Mail oder auf der Webseite -> http://www.pengutronix.de/impressum/ <-
Menon
PATCH 2/17 U-Boot-V2:Common:Clock Handle case of clockrollover for get_time_ns
Sascha,
Quoted 11 lines
-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 3:08 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 02/17] U-Boot-V2:Common:Clock Handle case of clockrollover forget_time_nsQuoted 1 line
-
cycle_delta= (cycle_now-cs->cycle_last) &cs->mask;Look closer, the rollover case is handled implicitly by the unsigned arithmetics.
Agreed for logic when mask is the max value attainable.
IMHO, The concept of cs->mask is messy. We do need a min, max and a mask. When:
cycle_now = cs->read(); cs->cycle_last = cycle_now;
(cycle_now - cs->cycle_last) & cs->mask is wrong. Assumptions made:
A) The bits masked out by cs->mask will remain constant. This may not be true.
B) Roll over assume the min is 0 and max is cs->mask. This need not be the case. It would be good to be explicit.
Quoted 8 lines
You are right, we do not have a possibility to detect a double rollover without interrupts. Normally this is not an issue as this code is used in timeout polling loops where the current time is polled often enough. Anyway, maybe some comment should mention that this function is not useful to measure long periods of time where 'long' is highly architecture specific.
Yes, there are indenting issue + no doxygen documentation.. It helps as clock_source is critical implementation required in the system.
Regards,
Nishanth Menon
PATCH 2/17 U-Boot-V2:Common:Clock Handle case of clockrollover for get_time_ns
Quoted 10 lines · Nishanth
Sascha,
Quoted 9 lines
-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 3:08 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 02/17] U-Boot-V2:Common:Clock Handle case of clockrollover forget_time_nsQuoted 1 line
-
cycle_delta= (cycle_now-cs->cycle_last) &cs->mask;
Quoted 7 lines
Quoted 2 lines
Look closer, the rollover case is handled implicitly by the unsigned arithmetics.
Agreed for logic when mask is the max value attainable.
IMHO, The concept ofcs->maskis messy. We do need a min, max and a mask. When:cycle_now = cs->read(); cs->cycle_last = cycle_now;
Now cycle_now == cs->cycle_last...
Quoted 1 line
(
cycle_now-cs->cycle_last) &cs->maskis wrong.
...so 0 & cs->mask = 0
This makes no sense, but this is not the order of execution in current code.
Quoted 2 lines
Assumptions made:
A) The bits masked out bycs->maskwill remain constant. This may not be true.
Eh? That's why they are masked out.
Quoted 2 lines
B) Roll over assume the min is 0 and max is
cs->mask. This need not be the case. It would be good to be explicit.
Do you know any counter that does not start counting from zero? If you have, noone prevents you from substracting the value in your clocksource read function.
Quoted 13 lines
Quoted 8 lines
You are right, we do not have a possibility to detect a double rollover without interrupts. Normally this is not an issue as this code is used in timeout polling loops where the current time is polled often enough. Anyway, maybe some comment should mention that this function is not useful to measure long periods of time where 'long' is highly architecture specific.
Yes, there are indenting issue + no doxygen documentation.. It helps as
clock_sourceis critical implementation required in the system.Regards,
Nishanth Menon
Signature · 4 lines
Pengutronix e.K. - Linux Solutions for Science and Industry ----------------------------------------------------------- Kontakt-Informationen finden Sie im Header dieser Mail oder auf der Webseite -> http://www.pengutronix.de/impressum/ <-
Menon
PATCH 2/17 U-Boot-V2:Common:Clock Handle case ofclockrollover for get_time_ns
Sascha,
Quoted 8 lines
-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 9:15 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 02/17] U-Boot-V2:Common:Clock Handle case ofclockrollover forget_time_ns
Quoted 4 lines
Quoted 2 lines
Assumptions made:
A) The bits masked out bycs->maskwill remain constant. This may not be true.Eh? That's why they are masked out.
For the sake of discussion:
unsigned int now = 0x120;
unsigned int last = 0x128;
unsigned int mask = 0xFFF0;
unsigned int delta1 = now - last;
unsigned int delta2 = (now & mask) - (last & mask);
printf ("delta1=0x%08X maskdelta1=0x%08X delta2=0x%08X\n",delta1, delta1 & mask, delta2);
Output will be:
delta1=0xFFFFFFF8 maskdelta1=0x0000FFF0 delta2=0x00000000
What we will get now is maskdelta1, while delta2 is the right value.
Quoted 5 lines
Quoted 3 lines
B) Roll over assume the min is 0 and max is
cs->mask. This need not be the case. It would be good to be explicit.Do you know any counter that does not start counting from zero? If you
I do not. I am just being a paranoid idiot ;)..
Quoted 2 lines
have, noone prevents you from substracting the value in your clocksource read function.
Max however is assumed to be cs->mask then. We could have timers which can be configured for ticking till a configured max. probably my paranoia kicking in again?
Regards,
Nishanth Menon
PATCH 2/17 U-Boot-V2:Common:Clock Handle case ofclockrollover for get_time_ns
Quoted 21 lines · Nishanth
Sascha,
Quoted 8 lines
-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 9:15 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 02/17] U-Boot-V2:Common:Clock Handle case ofclockrollover forget_time_nsQuoted 4 lines
Quoted 2 lines
Assumptions made:
A) The bits masked out bycs->maskwill remain constant. This may not be true.Eh? That's why they are masked out.
For the sake of discussion:
unsigned int now =0x120;
unsigned int last =0x128;
unsigned int mask =0xFFF0;
unsigned int delta1 = now - last;
unsigned int delta2 = (now & mask) - (last & mask);
printf ("delta1=0x%08X maskdelta1=0x%08X delta2=0x%08X\n",delta1, delta1 & mask, delta2);
Quoted 4 lines
Output will be:
delta1=0xFFFFFFF8 maskdelta1=0x0000FFF0 delta2=0x00000000What we will get now is maskdelta1, while delta2 is the right value.
It's not that I thought of the code by myself, I just looked into the kernel and it's exactly like this in the kernel code. So you either just found a kernel bug or we both understand something wrong.
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
B) Roll over assume the min is 0 and max is
cs->mask. This need not be the case. It would be good to be explicit.Do you know any counter that does not start counting from zero? If you
I do not. I am just being a paranoid idiot ;)..
Quoted 2 lines
have, noone prevents you from substracting the value in your clocksource read function.
Max however is assumed to be
cs->maskthen. We could have timers which can be configured for ticking till a configured max. probably my paranoia kicking in again?
If that's the only possibility to program this timer then go out and kill the chip designer ;)
Sascha
Quoted 4 lines
Regards,
Nishanth Menon
Signature · 4 lines
Pengutronix e.K. - Linux Solutions for Science and Industry ----------------------------------------------------------- Kontakt-Informationen finden Sie im Header dieser Mail oder auf der Webseite -> http://www.pengutronix.de/impressum/ <-
Menon
PATCH 2/17 U-Boot-V2:Common:Clock Handle caseofclockrollover for get_time_ns
Sascha,
Quoted 8 lines
-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 10:14 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 02/17] U-Boot-V2:Common:Clock Handle caseofclockrollover forget_time_ns
Quoted 8 lines
Quoted 4 lines
Output will be:
delta1=0xFFFFFFF8 maskdelta1=0x0000FFF0 delta2=0x00000000What we will get now is maskdelta1, while delta2 is the right value.
It's not that I thought of the code by myself, I just looked into the kernel and it's exactly like this in the kernel code. So you either just found a kernel bug or we both understand something wrong.
Here is a revisit of this patch. The change will ensure cycle_now and last will be masked.
--- common/clock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
common/clock.c +1 -1
@@ -41,7 +41,7 @@uint64_t ns_offset;/* read clocksource: */ - cycle_now = cs->read (); + cycle_now = cs->read () & cs->mask;/* calculate the delta since the last call: */ cycle_delta = (cycle_now - cs->cycle_last) & cs->mask;