diff mbox series

[v3,1/4] timer: Allow delays with a 32-bit microsecond timer

Message ID 20200710004317.3017137-1-sjg@chromium.org
State Superseded
Headers show
Series [v3,1/4] timer: Allow delays with a 32-bit microsecond timer | expand

Commit Message

Simon Glass July 10, 2020, 12:43 a.m. UTC
The current get_timer_us() uses 64-bit arithmetic on 32-bit machines.
When implementing microsecond-level timeouts, 32-bits is plenty. Add a
new function that uses an unsigned long. On 64-bit machines this is
still 64-bit, but this doesn't introduce a penalty. On 32-bit machines
it is more efficient.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v3:
- Expand the commit message and function comment

 include/time.h | 11 +++++++++++
 lib/time.c     |  5 +++++
 2 files changed, 16 insertions(+)

Comments

Bin Meng July 13, 2020, 2:13 a.m. UTC | #1
On Fri, Jul 10, 2020 at 8:49 AM Simon Glass <sjg at chromium.org> wrote:
>
> The current get_timer_us() uses 64-bit arithmetic on 32-bit machines.
> When implementing microsecond-level timeouts, 32-bits is plenty. Add a
> new function that uses an unsigned long. On 64-bit machines this is
> still 64-bit, but this doesn't introduce a penalty. On 32-bit machines
> it is more efficient.
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
>
> Changes in v3:
> - Expand the commit message and function comment
>
>  include/time.h | 11 +++++++++++
>  lib/time.c     |  5 +++++
>  2 files changed, 16 insertions(+)
>
> diff --git a/include/time.h b/include/time.h
> index e99f9c8012..3f00e68713 100644
> --- a/include/time.h
> +++ b/include/time.h
> @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
>  unsigned long timer_get_us(void);
>  uint64_t get_timer_us(uint64_t base);
>
> +/**
> + * get_timer_us_long() - Get the number of elapsed microseconds
> + *
> + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> + * delays of over an hour. For 64-bit machines it uses a 64-bit value.
> + *
> + *@base: Base time to consider
> + *@return elapsed time since @base
> + */
> +unsigned long get_timer_us_long(unsigned long base);
> +
>  /*
>   * timer_test_add_offset()
>   *
> diff --git a/lib/time.c b/lib/time.c
> index 65db0f6cda..47f8c84327 100644
> --- a/lib/time.c
> +++ b/lib/time.c
> @@ -152,6 +152,11 @@ uint64_t __weak get_timer_us(uint64_t base)
>         return tick_to_time_us(get_ticks()) - base;
>  }
>
> +unsigned long __weak get_timer_us_long(unsigned long base)
> +{
> +       return timer_get_us() - base;
> +}
> +
>  unsigned long __weak notrace timer_get_us(void)
>  {
>         return tick_to_time(get_ticks() * 1000);
> --

Reviewed-by: Bin Meng <bmeng.cn at gmail.com>

Some day we should clean up the timer APIs. We have get_timer_us(),
and now get_timer_us_long(), but we also have timer_get_us() that
returns long value!

Regards,
Bin
Bin Meng July 13, 2020, 2:16 a.m. UTC | #2
On Mon, Jul 13, 2020 at 10:13 AM Bin Meng <bmeng.cn at gmail.com> wrote:
>
> On Fri, Jul 10, 2020 at 8:49 AM Simon Glass <sjg at chromium.org> wrote:
> >
> > The current get_timer_us() uses 64-bit arithmetic on 32-bit machines.
> > When implementing microsecond-level timeouts, 32-bits is plenty. Add a
> > new function that uses an unsigned long. On 64-bit machines this is
> > still 64-bit, but this doesn't introduce a penalty. On 32-bit machines
> > it is more efficient.
> >
> > Signed-off-by: Simon Glass <sjg at chromium.org>
> > ---
> >
> > Changes in v3:
> > - Expand the commit message and function comment
> >
> >  include/time.h | 11 +++++++++++
> >  lib/time.c     |  5 +++++
> >  2 files changed, 16 insertions(+)
> >
> > diff --git a/include/time.h b/include/time.h
> > index e99f9c8012..3f00e68713 100644
> > --- a/include/time.h
> > +++ b/include/time.h
> > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> >  unsigned long timer_get_us(void);
> >  uint64_t get_timer_us(uint64_t base);
> >
> > +/**
> > + * get_timer_us_long() - Get the number of elapsed microseconds
> > + *
> > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > + * delays of over an hour. For 64-bit machines it uses a 64-bit value.
> > + *
> > + *@base: Base time to consider
> > + *@return elapsed time since @base
> > + */
> > +unsigned long get_timer_us_long(unsigned long base);
> > +
> >  /*
> >   * timer_test_add_offset()
> >   *
> > diff --git a/lib/time.c b/lib/time.c
> > index 65db0f6cda..47f8c84327 100644
> > --- a/lib/time.c
> > +++ b/lib/time.c
> > @@ -152,6 +152,11 @@ uint64_t __weak get_timer_us(uint64_t base)
> >         return tick_to_time_us(get_ticks()) - base;
> >  }
> >
> > +unsigned long __weak get_timer_us_long(unsigned long base)
> > +{
> > +       return timer_get_us() - base;
> > +}
> > +
> >  unsigned long __weak notrace timer_get_us(void)
> >  {
> >         return tick_to_time(get_ticks() * 1000);
> > --
>
> Reviewed-by: Bin Meng <bmeng.cn at gmail.com>
>
> Some day we should clean up the timer APIs. We have get_timer_us(),
> and now get_timer_us_long(), but we also have timer_get_us() that
> returns long value!

applied to u-boot-x86, thanks!
Simon Glass July 13, 2020, 2:25 a.m. UTC | #3
Hi Bin,

On Sun, 12 Jul 2020 at 20:13, Bin Meng <bmeng.cn at gmail.com> wrote:
>
> On Fri, Jul 10, 2020 at 8:49 AM Simon Glass <sjg at chromium.org> wrote:
> >
> > The current get_timer_us() uses 64-bit arithmetic on 32-bit machines.
> > When implementing microsecond-level timeouts, 32-bits is plenty. Add a
> > new function that uses an unsigned long. On 64-bit machines this is
> > still 64-bit, but this doesn't introduce a penalty. On 32-bit machines
> > it is more efficient.
> >
> > Signed-off-by: Simon Glass <sjg at chromium.org>
> > ---
> >
> > Changes in v3:
> > - Expand the commit message and function comment
> >
> >  include/time.h | 11 +++++++++++
> >  lib/time.c     |  5 +++++
> >  2 files changed, 16 insertions(+)
> >
> > diff --git a/include/time.h b/include/time.h
> > index e99f9c8012..3f00e68713 100644
> > --- a/include/time.h
> > +++ b/include/time.h
> > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> >  unsigned long timer_get_us(void);
> >  uint64_t get_timer_us(uint64_t base);
> >
> > +/**
> > + * get_timer_us_long() - Get the number of elapsed microseconds
> > + *
> > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > + * delays of over an hour. For 64-bit machines it uses a 64-bit value.
> > + *
> > + *@base: Base time to consider
> > + *@return elapsed time since @base
> > + */
> > +unsigned long get_timer_us_long(unsigned long base);
> > +
> >  /*
> >   * timer_test_add_offset()
> >   *
> > diff --git a/lib/time.c b/lib/time.c
> > index 65db0f6cda..47f8c84327 100644
> > --- a/lib/time.c
> > +++ b/lib/time.c
> > @@ -152,6 +152,11 @@ uint64_t __weak get_timer_us(uint64_t base)
> >         return tick_to_time_us(get_ticks()) - base;
> >  }
> >
> > +unsigned long __weak get_timer_us_long(unsigned long base)
> > +{
> > +       return timer_get_us() - base;
> > +}
> > +
> >  unsigned long __weak notrace timer_get_us(void)
> >  {
> >         return tick_to_time(get_ticks() * 1000);
> > --
>
> Reviewed-by: Bin Meng <bmeng.cn at gmail.com>
>
> Some day we should clean up the timer APIs. We have get_timer_us(),
> and now get_timer_us_long(), but we also have timer_get_us() that
> returns long value!

Yes, but that one returns the monotonic time, so isn't so easy for
timeouts. Perhaps we should remove it since get_timer_us_long(0) does
the same thing?

Regards,
Simon
Bin Meng July 13, 2020, 2:26 a.m. UTC | #4
Hi Simon,

On Mon, Jul 13, 2020 at 10:25 AM Simon Glass <sjg at chromium.org> wrote:
>
> Hi Bin,
>
> On Sun, 12 Jul 2020 at 20:13, Bin Meng <bmeng.cn at gmail.com> wrote:
> >
> > On Fri, Jul 10, 2020 at 8:49 AM Simon Glass <sjg at chromium.org> wrote:
> > >
> > > The current get_timer_us() uses 64-bit arithmetic on 32-bit machines.
> > > When implementing microsecond-level timeouts, 32-bits is plenty. Add a
> > > new function that uses an unsigned long. On 64-bit machines this is
> > > still 64-bit, but this doesn't introduce a penalty. On 32-bit machines
> > > it is more efficient.
> > >
> > > Signed-off-by: Simon Glass <sjg at chromium.org>
> > > ---
> > >
> > > Changes in v3:
> > > - Expand the commit message and function comment
> > >
> > >  include/time.h | 11 +++++++++++
> > >  lib/time.c     |  5 +++++
> > >  2 files changed, 16 insertions(+)
> > >
> > > diff --git a/include/time.h b/include/time.h
> > > index e99f9c8012..3f00e68713 100644
> > > --- a/include/time.h
> > > +++ b/include/time.h
> > > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> > >  unsigned long timer_get_us(void);
> > >  uint64_t get_timer_us(uint64_t base);
> > >
> > > +/**
> > > + * get_timer_us_long() - Get the number of elapsed microseconds
> > > + *
> > > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > > + * delays of over an hour. For 64-bit machines it uses a 64-bit value.
> > > + *
> > > + *@base: Base time to consider
> > > + *@return elapsed time since @base
> > > + */
> > > +unsigned long get_timer_us_long(unsigned long base);
> > > +
> > >  /*
> > >   * timer_test_add_offset()
> > >   *
> > > diff --git a/lib/time.c b/lib/time.c
> > > index 65db0f6cda..47f8c84327 100644
> > > --- a/lib/time.c
> > > +++ b/lib/time.c
> > > @@ -152,6 +152,11 @@ uint64_t __weak get_timer_us(uint64_t base)
> > >         return tick_to_time_us(get_ticks()) - base;
> > >  }
> > >
> > > +unsigned long __weak get_timer_us_long(unsigned long base)
> > > +{
> > > +       return timer_get_us() - base;
> > > +}
> > > +
> > >  unsigned long __weak notrace timer_get_us(void)
> > >  {
> > >         return tick_to_time(get_ticks() * 1000);
> > > --
> >
> > Reviewed-by: Bin Meng <bmeng.cn at gmail.com>
> >
> > Some day we should clean up the timer APIs. We have get_timer_us(),
> > and now get_timer_us_long(), but we also have timer_get_us() that
> > returns long value!
>
> Yes, but that one returns the monotonic time, so isn't so easy for
> timeouts. Perhaps we should remove it since get_timer_us_long(0) does
> the same thing?

Yes, I think so.

Regards,
Bin
diff mbox series

Patch

diff --git a/include/time.h b/include/time.h
index e99f9c8012..3f00e68713 100644
--- a/include/time.h
+++ b/include/time.h
@@ -17,6 +17,17 @@  unsigned long get_timer(unsigned long base);
 unsigned long timer_get_us(void);
 uint64_t get_timer_us(uint64_t base);
 
+/**
+ * get_timer_us_long() - Get the number of elapsed microseconds
+ *
+ * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
+ * delays of over an hour. For 64-bit machines it uses a 64-bit value.
+ *
+ *@base: Base time to consider
+ *@return elapsed time since @base
+ */
+unsigned long get_timer_us_long(unsigned long base);
+
 /*
  * timer_test_add_offset()
  *
diff --git a/lib/time.c b/lib/time.c
index 65db0f6cda..47f8c84327 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -152,6 +152,11 @@  uint64_t __weak get_timer_us(uint64_t base)
 	return tick_to_time_us(get_ticks()) - base;
 }
 
+unsigned long __weak get_timer_us_long(unsigned long base)
+{
+	return timer_get_us() - base;
+}
+
 unsigned long __weak notrace timer_get_us(void)
 {
 	return tick_to_time(get_ticks() * 1000);