Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/kernel/kqueue Add support for the NOTE_SECONDS, NOTE_M...



details:   https://anonhg.NetBSD.org/src/rev/9c15fd00fbb4
branches:  trunk
changeset: 989154:9c15fd00fbb4
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Wed Oct 13 04:57:19 2021 +0000

description:
Add support for the NOTE_SECONDS, NOTE_MSECONDS, NOTE_USECONDS,
NOTE_NSECONDS, and NOTE_ABSTIME filter flags to EVFILT_TIMER,
API-compatible with the same in FreeBSD.

diffstat:

 distrib/sets/lists/debug/mi   |    3 +-
 distrib/sets/lists/tests/mi   |    3 +-
 lib/libc/sys/kqueue.2         |   81 +++++++++++-
 sys/kern/kern_event.c         |   90 +++++++++++--
 sys/sys/event.h               |   12 +-
 tests/kernel/kqueue/Makefile  |    3 +-
 tests/kernel/kqueue/t_timer.c |  274 ++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 443 insertions(+), 23 deletions(-)

diffs (truncated from 604 to 300 lines):

diff -r 922ec72669c0 -r 9c15fd00fbb4 distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Wed Oct 13 01:11:29 2021 +0000
+++ b/distrib/sets/lists/debug/mi       Wed Oct 13 04:57:19 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.364 2021/10/10 17:47:38 thorpej Exp $
+# $NetBSD: mi,v 1.365 2021/10/13 04:57:19 thorpej Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib                                      comp-sys-usr            compatdir
 ./usr/lib/i18n/libBIG5_g.a                     comp-c-debuglib         debuglib,compatfile
@@ -1767,6 +1767,7 @@
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc4.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_scan.debug               tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_sig.debug                        tests-kernel-tests      debug,atf,compattestfile
+./usr/libdata/debug/usr/tests/kernel/kqueue/t_timer.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/t_vnode.debug              tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/write/t_fifo.debug         tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/kqueue/write/t_pipe.debug         tests-kernel-tests      debug,atf,compattestfile
diff -r 922ec72669c0 -r 9c15fd00fbb4 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Oct 13 01:11:29 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Oct 13 04:57:19 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1135 2021/10/10 17:47:38 thorpej Exp $
+# $NetBSD: mi,v 1.1136 2021/10/13 04:57:20 thorpej Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2187,6 +2187,7 @@
 ./usr/tests/kernel/kqueue/t_proc4                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_scan                       tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_sig                                tests-kernel-tests      compattestfile,atf
+./usr/tests/kernel/kqueue/t_timer                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/t_vnode                      tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/write                                tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/kqueue/write/Atffile                        tests-kernel-tests      compattestfile,atf
diff -r 922ec72669c0 -r 9c15fd00fbb4 lib/libc/sys/kqueue.2
--- a/lib/libc/sys/kqueue.2     Wed Oct 13 01:11:29 2021 +0000
+++ b/lib/libc/sys/kqueue.2     Wed Oct 13 04:57:19 2021 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: kqueue.2,v 1.53 2020/10/31 14:35:28 christos Exp $
+.\"    $NetBSD: kqueue.2,v 1.54 2021/10/13 04:57:19 thorpej Exp $
 .\"
 .\" Copyright (c) 2000 Jonathan Lemon
 .\" All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.22 2001/06/27 19:55:57 dd Exp $
 .\"
-.Dd October 30, 2020
+.Dd October 11, 2021
 .Dt KQUEUE 2
 .Os
 .Sh NAME
@@ -521,13 +521,72 @@
 .Va ident .
 When adding a timer,
 .Va data
-specifies the timeout period in milliseconds.
-The timer will be periodic unless EV_ONESHOT is specified.
+specifies the timeout period in units described below, or, if
+.Dv NOTE_ABSTIME
+is set in
+.Va fflags ,
+specifies the absolute time at which the timer should fire.
+The timer will repeat unless
+.Dv EV_ONESHOT
+is set in
+.Va flags
+or
+.Dv NOTE_ABSTIME
+is set in
+.Va fflags .
 On return,
 .Va data
 contains the number of times the timeout has expired since the last call to
 .Fn kevent .
-This filter automatically sets the EV_CLEAR flag internally.
+This filter automatically sets
+.Dv EV_CLEAR
+in
+.va flags
+for periodic timers.
+Timers created with
+.Dv NOTE_ABSTIME
+remain activated on the kqueue once the absolute time has passed unless
+.Dv EV_CLEAR
+or
+.Dv EV_ONESHOT
+are also specified.
+.Dv CLOCK_REALTIME
+is the reference clock for timers created with
+.Dv NOTE_ABSTIME.
+.Pp
+The filter accepts the following flags in the
+.Va fflags
+argument:
+.Bl -tag -width XXNOTE_TRACKERR
+.It Dv NOTE_SECONDS
+The timer value in
+.Va data
+is expressed in seconds.
+.It Dv NOTE_MSECONDS
+The timer value in
+.Va data
+is expressed in milliseconds.
+.It Dv NOTE_USECONDS
+The timer value in
+.Va data
+is expressed in microseconds.
+.It Dv NOTE_NSECONDS
+The timer value in
+.Va data
+is expressed in nanoseconds.
+.It Dv NOTE_ABSTIME
+The timer value is an absolute time; see discussion above.
+.El
+.Pp
+Note that
+.Dv NOTE_SECONDS ,
+.Dv NOTE_MSECONDS ,
+.Dv NOTE_USECONDS ,
+and
+.Dv NOTE_NSECONDS
+are mutually exclusive; behavior is undefined if more than one are specified.
+If a timer value unit is not specified, the default is
+.Dv NOTE_MSECONDS .
 .It Dv EVFILT_FS
 Establishes a file system monitor.
 Currently it only monitors file system mount and unmount actions.
@@ -781,3 +840,15 @@
 .Va udata
 type was changed from intptr_t to void * in
 .Nx 10.0 .
+.Pp
+Support for
+.Dv NOTE_SECONDS ,
+.Dv NOTE_MSECONDS ,
+.Dv NOTE_USECONDS ,
+.Dv NOTE_NSECONDS ,
+and
+.Dv NOTE_ABSTIME
+filter flags for
+.Dv EVFILT_TIMER
+was added in
+.Nx 10.0 .
diff -r 922ec72669c0 -r 9c15fd00fbb4 sys/kern/kern_event.c
--- a/sys/kern/kern_event.c     Wed Oct 13 01:11:29 2021 +0000
+++ b/sys/kern/kern_event.c     Wed Oct 13 04:57:19 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_event.c,v 1.131 2021/10/11 01:07:36 thorpej Exp $ */
+/*     $NetBSD: kern_event.c,v 1.132 2021/10/13 04:57:19 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #endif /* _KERNEL_OPT */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.131 2021/10/11 01:07:36 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.132 2021/10/13 04:57:19 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1156,31 +1156,84 @@
 filt_timerexpire(void *knx)
 {
        struct knote *kn = knx;
-       int tticks;
 
        mutex_enter(&kqueue_timer_lock);
        kn->kn_data++;
        knote_activate(kn);
-       if ((kn->kn_flags & EV_ONESHOT) == 0) {
-               tticks = mstohz(kn->kn_sdata);
-               if (tticks <= 0)
-                       tticks = 1;
-               callout_schedule((callout_t *)kn->kn_hook, tticks);
+       if (kn->kn_sdata != (uintptr_t)-1) {
+               KASSERT(kn->kn_sdata > 0 && kn->kn_sdata <= INT_MAX);
+               callout_schedule((callout_t *)kn->kn_hook,
+                   (int)kn->kn_sdata);
        }
        mutex_exit(&kqueue_timer_lock);
 }
 
-/*
- * data contains amount of time to sleep, in milliseconds
- */
 static int
 filt_timerattach(struct knote *kn)
 {
        callout_t *calloutp;
        struct kqueue *kq;
-       int tticks;
+       struct timespec ts;
+       int tticks, flags = 0;
+
+       if (kn->kn_sfflags & ~(NOTE_TIMER_UNITMASK | NOTE_ABSTIME)) {
+               return EINVAL;
+       }
+
+       /*
+        * Convert the event 'data' to a timespec, then convert the
+        * timespec to callout ticks.
+        */
+       switch (kn->kn_sfflags & NOTE_TIMER_UNITMASK) {
+       case NOTE_SECONDS:
+               ts.tv_sec = kn->kn_sdata;
+               ts.tv_nsec = 0;
+               break;
+
+       case NOTE_MSECONDS:             /* == historical value 0 */
+               ts.tv_sec = kn->kn_sdata / 1000;
+               ts.tv_nsec = (kn->kn_sdata % 1000) * 1000000;
+               break;
+
+       case NOTE_USECONDS:
+               ts.tv_sec = kn->kn_sdata / 1000000;
+               ts.tv_nsec = (kn->kn_sdata % 1000000) * 1000;
+               break;
 
-       tticks = mstohz(kn->kn_sdata);
+       case NOTE_NSECONDS:
+               ts.tv_sec = kn->kn_sdata / 1000000000;
+               ts.tv_nsec = kn->kn_sdata % 1000000000;
+               break;
+
+       default:
+               return EINVAL;
+       }
+
+       if (kn->kn_sfflags & NOTE_ABSTIME) {
+               struct timespec deadline = ts;
+
+               /*
+                * Get current time.
+                *
+                * XXX This is CLOCK_REALTIME.  There is no way to
+                * XXX specify CLOCK_MONOTONIC.
+                */
+               nanotime(&ts);
+
+               /* If we're past the deadline, then the event will fire. */
+               if (timespeccmp(&deadline, &ts, <=)) {
+                       kn->kn_data = 1;
+                       return 0;
+               }
+
+               /* Calculate how much time is left. */
+               timespecsub(&deadline, &ts, &ts);
+       } else {
+               /* EV_CLEAR automatically set for relative timers. */
+               flags |= EV_CLEAR;
+       }
+
+       tticks = tstohz(&ts);
 
        /* if the supplied value is under our resolution, use 1 tick */
        if (tticks == 0) {
@@ -1189,6 +1242,15 @@
                tticks = 1;
        }
 
+       if ((kn->kn_flags & EV_ONESHOT) != 0 ||
+           (kn->kn_sfflags & NOTE_ABSTIME) != 0) {
+               /* Timer does not repeat. */
+               kn->kn_sdata = (uintptr_t)-1;
+       } else {
+               KASSERT((uintptr_t)tticks != (uintptr_t)-1);
+               kn->kn_sdata = tticks;
+       }
+
        if (atomic_inc_uint_nv(&kq_ncallouts) >= kq_calloutmax ||
            (calloutp = kmem_alloc(sizeof(*calloutp), KM_NOSLEEP)) == NULL) {
                atomic_dec_uint(&kq_ncallouts);
@@ -1198,7 +1260,7 @@
 
        kq = kn->kn_kq;
        mutex_spin_enter(&kq->kq_lock);
-       kn->kn_flags |= EV_CLEAR;               /* automatically set */
+       kn->kn_flags |= flags;
        kn->kn_hook = calloutp;
        mutex_spin_exit(&kq->kq_lock);
 
diff -r 922ec72669c0 -r 9c15fd00fbb4 sys/sys/event.h
--- a/sys/sys/event.h   Wed Oct 13 01:11:29 2021 +0000
+++ b/sys/sys/event.h   Wed Oct 13 04:57:19 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: event.h,v 1.47 2021/10/11 01:21:28 thorpej Exp $       */
+/*     $NetBSD: event.h,v 1.48 2021/10/13 04:57:19 thorpej Exp $       */
 
 /*-
  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon%FreeBSD.org@localhost>
@@ -160,6 +160,16 @@
 #define        NOTE_TRACKERR   0x00000002U             /* could not track child */
 #define        NOTE_CHILD      0x00000004U             /* am a child process */
 
+/* additional flags for EVFILT_TIMER */
+#define        NOTE_MSECONDS   0x00000000U             /* data is milliseconds */
+#define        NOTE_SECONDS    0x00000001U             /* data is seconds */
+#define        NOTE_USECONDS   0x00000002U             /* data is microseconds */



Home | Main Index | Thread Index | Old Index