ksh: count $SECONDS with monotonic clock

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ksh: count $SECONDS with monotonic clock

Scott Cheloha
So that $SECONDS advances uniformly, independent of the system clock.

ok?

--
Scott Cheloha

P.S. Similar change forthcoming for $MAILCHECK.

Index: bin/ksh/var.c
===================================================================
RCS file: /cvs/src/bin/ksh/var.c,v
retrieving revision 1.67
diff -u -p -r1.67 var.c
--- bin/ksh/var.c 9 Apr 2018 17:53:36 -0000 1.67
+++ bin/ksh/var.c 13 Apr 2018 16:01:08 -0000
@@ -1,6 +1,7 @@
 /* $OpenBSD: var.c,v 1.67 2018/04/09 17:53:36 tobias Exp $ */
 
 #include <sys/stat.h>
+#include <sys/time.h>
 
 #include <ctype.h>
 #include <errno.h>
@@ -908,7 +909,7 @@ unspecial(const char *name)
  ktdelete(tp);
 }
 
-static time_t seconds; /* time SECONDS last set */
+static struct timespec seconds; /* time SECONDS last set */
 static int user_lineno; /* what user set $LINENO to */
 
 static void
@@ -921,8 +922,13 @@ getspec(struct tbl *vp)
  * has been set - don't do anything in this case
  * (see initcoms[] in main.c).
  */
- if (vp->flag & ISSET)
- setint(vp, (int64_t)(time(NULL) - seconds));
+ if (vp->flag & ISSET) {
+ struct timespec difference, now;
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ timespecsub(&now, &seconds, &difference);
+ setint(vp, (int64_t)difference.tv_sec);
+ }
  vp->flag |= SPECIAL;
  break;
  case V_RANDOM:
@@ -1036,7 +1042,8 @@ setspec(struct tbl *vp)
  break;
  case V_SECONDS:
  vp->flag &= ~SPECIAL;
- seconds = time(NULL) - intval(vp);
+ clock_gettime(CLOCK_MONOTONIC, &seconds);
+ seconds.tv_sec -= intval(vp);
  vp->flag |= SPECIAL;
  break;
  case V_TMOUT:

Reply | Threaded
Open this post in threaded view
|

Re: ksh: count $SECONDS with monotonic clock

Jeremie Courreges-Anglas-2
On Fri, Apr 13 2018, Scott Cheloha <[hidden email]> wrote:
> So that $SECONDS advances uniformly, independent of the system clock.
>
> ok?

ok jca@

> --
> Scott Cheloha
>
> P.S. Similar change forthcoming for $MAILCHECK.
>
> Index: bin/ksh/var.c
> ===================================================================
> RCS file: /cvs/src/bin/ksh/var.c,v
> retrieving revision 1.67
> diff -u -p -r1.67 var.c
> --- bin/ksh/var.c 9 Apr 2018 17:53:36 -0000 1.67
> +++ bin/ksh/var.c 13 Apr 2018 16:01:08 -0000
> @@ -1,6 +1,7 @@
>  /* $OpenBSD: var.c,v 1.67 2018/04/09 17:53:36 tobias Exp $ */
>  
>  #include <sys/stat.h>
> +#include <sys/time.h>
>  
>  #include <ctype.h>
>  #include <errno.h>
> @@ -908,7 +909,7 @@ unspecial(const char *name)
>   ktdelete(tp);
>  }
>  
> -static time_t seconds; /* time SECONDS last set */
> +static struct timespec seconds; /* time SECONDS last set */
>  static int user_lineno; /* what user set $LINENO to */
>  
>  static void
> @@ -921,8 +922,13 @@ getspec(struct tbl *vp)
>   * has been set - don't do anything in this case
>   * (see initcoms[] in main.c).
>   */
> - if (vp->flag & ISSET)
> - setint(vp, (int64_t)(time(NULL) - seconds));
> + if (vp->flag & ISSET) {
> + struct timespec difference, now;
> +
> + clock_gettime(CLOCK_MONOTONIC, &now);
> + timespecsub(&now, &seconds, &difference);
> + setint(vp, (int64_t)difference.tv_sec);
> + }
>   vp->flag |= SPECIAL;
>   break;
>   case V_RANDOM:
> @@ -1036,7 +1042,8 @@ setspec(struct tbl *vp)
>   break;
>   case V_SECONDS:
>   vp->flag &= ~SPECIAL;
> - seconds = time(NULL) - intval(vp);
> + clock_gettime(CLOCK_MONOTONIC, &seconds);
> + seconds.tv_sec -= intval(vp);
>   vp->flag |= SPECIAL;
>   break;
>   case V_TMOUT:
>

--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply | Threaded
Open this post in threaded view
|

Re: ksh: count $SECONDS with monotonic clock

Todd C. Miller-2
In reply to this post by Scott Cheloha
On Fri, 13 Apr 2018 11:11:04 -0500, Scott Cheloha wrote:

> So that $SECONDS advances uniformly, independent of the system clock.

Why are you including sys/time.h?  For struct timespect you only
need time.h which is already included.

In general, you only need sys/time.h for struct timeval or for some
of the time-related macros.

 - todd

Reply | Threaded
Open this post in threaded view
|

Re: ksh: count $SECONDS with monotonic clock

Jeremie Courreges-Anglas-2
On Fri, Apr 13 2018, "Todd C. Miller" <[hidden email]> wrote:
> On Fri, 13 Apr 2018 11:11:04 -0500, Scott Cheloha wrote:
>
>> So that $SECONDS advances uniformly, independent of the system clock.
>
> Why are you including sys/time.h?  For struct timespect you only
> need time.h which is already included.
>
> In general, you only need sys/time.h for struct timeval or for some
> of the time-related macros.

The diff uses timespecsub:

@@ -921,8 +922,13 @@ getspec(struct tbl *vp)
  * has been set - don't do anything in this case
  * (see initcoms[] in main.c).
  */
- if (vp->flag & ISSET)
- setint(vp, (int64_t)(time(NULL) - seconds));
+ if (vp->flag & ISSET) {
+ struct timespec difference, now;
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ timespecsub(&now, &seconds, &difference);
+ setint(vp, (int64_t)difference.tv_sec);
+ }
  vp->flag |= SPECIAL;
  break;
  case V_RANDOM:


--
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply | Threaded
Open this post in threaded view
|

Re: ksh: count $SECONDS with monotonic clock

Todd C. Miller-2
On Fri, 13 Apr 2018 19:42:51 +0200, Jeremie Courreges-Anglas wrote:

> The diff uses timespecsub:

Ah, OK.  That's fine then.

 - todd