I'd like to stop converting the given timespec to ticks and instead use
nanoseconds.  This is part of the ongoing effort to reduce the use of
`hz' through the kernel.

Since I don't know C I'd appreciate any pointer about the checks that
should be added to TIMESPEC_TO_NSEC().

Then the conversions to {t,rw}sleep_nsec(9) become trivial, diff below.

Index: sys/time.h
===================================================================
RCS file: /cvs/src/sys/sys/time.h,v
retrieving revision 1.47
diff -u -p -r1.47 time.h
--- sys/time.h  22 Oct 2019 20:19:41 -0000      1.47
+++ sys/time.h  31 Dec 2019 10:20:26 -0000
@@ -372,6 +372,13 @@ USEC_TO_NSEC(uint64_t microseconds)
        return microseconds * 1000ULL;
 }
 
+static __inline uint64_t
+TIMESPEC_TO_NSEC(const struct timespec *ts)
+{
+       /* XXX overflow? */
+       return SEC_TO_NSEC(ts->tv_sec) + ts->tv_nsec;
+}
+
 #else /* !_KERNEL */
 #include <time.h>
 
Index: kern/kern_synch.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_synch.c,v
retrieving revision 1.155
diff -u -p -r1.155 kern_synch.c
--- kern/kern_synch.c   30 Nov 2019 11:19:17 -0000      1.155
+++ kern/kern_synch.c   31 Dec 2019 10:19:25 -0000
@@ -624,7 +624,7 @@ thrsleep(struct proc *p, struct sys___th
        long ident = (long)SCARG(uap, ident);
        struct timespec *tsp = (struct timespec *)SCARG(uap, tp);
        void *lock = SCARG(uap, lock);
-       uint64_t to_ticks = 0;
+       uint64_t nsecs = INFSLP;
        int abort, error;
        clockid_t clock_id = SCARG(uap, clock_id);
 
@@ -648,10 +648,7 @@ thrsleep(struct proc *p, struct sys___th
                }
 
                timespecsub(tsp, &now, tsp);
-               to_ticks = (uint64_t)hz * tsp->tv_sec +
-                   (tsp->tv_nsec + tick * 1000 - 1) / (tick * 1000) + 1;
-               if (to_ticks > INT_MAX)
-                       to_ticks = INT_MAX;
+               nsecs = TIMESPEC_TO_NSEC(tsp);
        }
 
        p->p_thrslpid = ident;
@@ -675,8 +672,7 @@ thrsleep(struct proc *p, struct sys___th
                void *sleepaddr = &p->p_thrslpid;
                if (ident == -1)
                        sleepaddr = &globalsleepaddr;
-               error = tsleep(sleepaddr, PWAIT|PCATCH, "thrsleep",
-                   (int)to_ticks);
+               error = tsleep_nsec(sleepaddr, PWAIT|PCATCH, "thrsleep", nsecs);
        }
 
 out:
Index: kern/sys_futex.c
===================================================================
RCS file: /cvs/src/sys/kern/sys_futex.c,v
retrieving revision 1.13
diff -u -p -r1.13 sys_futex.c
--- kern/sys_futex.c    10 Jul 2019 15:52:17 -0000      1.13
+++ kern/sys_futex.c    31 Dec 2019 10:19:49 -0000
@@ -213,7 +213,7 @@ futex_wait(uint32_t *uaddr, uint32_t val
 {
        struct proc *p = curproc;
        struct futex *f;
-       uint64_t to_ticks = 0;
+       uint64_t nsecs = INFSLP;
        uint32_t cval;
        int error;
 
@@ -244,17 +244,14 @@ futex_wait(uint32_t *uaddr, uint32_t val
 #endif
                if (ts.tv_sec < 0 || !timespecisvalid(&ts))
                        return EINVAL;
-               to_ticks = (uint64_t)hz * ts.tv_sec +
-                   (ts.tv_nsec + tick * 1000 - 1) / (tick * 1000) + 1;
-               if (to_ticks > INT_MAX)
-                       to_ticks = INT_MAX;
+               nsecs = TIMESPEC_TO_NSEC(&ts);
        }
 
        f = futex_get(uaddr, flags | FT_CREATE);
        TAILQ_INSERT_TAIL(&f->ft_threads, p, p_fut_link);
        p->p_futex = f;
 
-       error = rwsleep(p, &ftlock, PWAIT|PCATCH, "fsleep", (int)to_ticks);
+       error = rwsleep_nsec(p, &ftlock, PWAIT|PCATCH, "fsleep", nsecs);
        if (error == ERESTART)
                error = ECANCELED;
        else if (error == EWOULDBLOCK) {

Reply via email to