Home | History | Annotate | Line # | Download | only in kqueue
t_timer.c revision 1.1
      1  1.1  thorpej /* $NetBSD: t_timer.c,v 1.1 2021/10/13 04:57:19 thorpej Exp $ */
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  *
     16  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  thorpej  */
     28  1.1  thorpej 
     29  1.1  thorpej #include <sys/cdefs.h>
     30  1.1  thorpej __RCSID("$NetBSD: t_timer.c,v 1.1 2021/10/13 04:57:19 thorpej Exp $");
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/types.h>
     33  1.1  thorpej #include <sys/event.h>
     34  1.1  thorpej #include <errno.h>
     35  1.1  thorpej #include <stdio.h>
     36  1.1  thorpej #include <stdlib.h>
     37  1.1  thorpej #include <time.h>
     38  1.1  thorpej #include <unistd.h>
     39  1.1  thorpej 
     40  1.1  thorpej #include <atf-c.h>
     41  1.1  thorpej 
     42  1.1  thorpej ATF_TC(basic_timer);
     43  1.1  thorpej ATF_TC_HEAD(basic_timer, tc)
     44  1.1  thorpej {
     45  1.1  thorpej 	atf_tc_set_md_var(tc, "descr",
     46  1.1  thorpej 	    "tests basic EVFILT_TIMER functionality");
     47  1.1  thorpej }
     48  1.1  thorpej 
     49  1.1  thorpej #define	TIME1		1000		/* 1000ms -> 1s */
     50  1.1  thorpej #define	TIME1_COUNT	5
     51  1.1  thorpej #define	TIME2		6000		/* 6000ms -> 6s */
     52  1.1  thorpej 
     53  1.1  thorpej #define	TIME1_TOTAL_SEC	((TIME1 * TIME1_COUNT) / 1000)
     54  1.1  thorpej #define	TIME2_TOTAL_SEC	(TIME2 / 1000)
     55  1.1  thorpej 
     56  1.1  thorpej ATF_TC_BODY(basic_timer, tc)
     57  1.1  thorpej {
     58  1.1  thorpej 	struct kevent event[2];
     59  1.1  thorpej 	int ntimer1 = 0, ntimer2 = 0;
     60  1.1  thorpej 	struct timespec ots, ts;
     61  1.1  thorpej 	int kq;
     62  1.1  thorpej 
     63  1.1  thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
     64  1.1  thorpej 
     65  1.1  thorpej 	EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, TIME1, NULL);
     66  1.1  thorpej 	EV_SET(&event[1], 2, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, TIME2, NULL);
     67  1.1  thorpej 
     68  1.1  thorpej 	ATF_REQUIRE(kevent(kq, event, 2, NULL, 0, NULL) == 0);
     69  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ots) == 0);
     70  1.1  thorpej 
     71  1.1  thorpej 	for (;;) {
     72  1.1  thorpej 		ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, NULL) == 1);
     73  1.1  thorpej 		ATF_REQUIRE(event[0].filter == EVFILT_TIMER);
     74  1.1  thorpej 		ATF_REQUIRE(event[0].ident == 1 ||
     75  1.1  thorpej 			    event[0].ident == 2);
     76  1.1  thorpej 		if (event[0].ident == 1) {
     77  1.1  thorpej 			ATF_REQUIRE(ntimer1 < TIME1_COUNT);
     78  1.1  thorpej 			if (++ntimer1 == TIME1_COUNT) {
     79  1.1  thorpej 				/*
     80  1.1  thorpej 				 * Make sure TIME1_TOTAL_SEC seconds have
     81  1.1  thorpej 				 * elapsed, allowing for a little slop.
     82  1.1  thorpej 				 */
     83  1.1  thorpej 				ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC,
     84  1.1  thorpej 				    &ts) == 0);
     85  1.1  thorpej 				timespecsub(&ts, &ots, &ts);
     86  1.1  thorpej 				ATF_REQUIRE(ts.tv_sec ==
     87  1.1  thorpej 					    (TIME1_TOTAL_SEC - 1) ||
     88  1.1  thorpej 				    ts.tv_sec == TIME1_TOTAL_SEC);
     89  1.1  thorpej 				if (ts.tv_sec == TIME1_TOTAL_SEC - 1) {
     90  1.1  thorpej 					ATF_REQUIRE(ts.tv_nsec >=
     91  1.1  thorpej 					    900000000);
     92  1.1  thorpej 				}
     93  1.1  thorpej 				EV_SET(&event[0], 1, EVFILT_TIMER, EV_DELETE,
     94  1.1  thorpej 				    0, 0, NULL);
     95  1.1  thorpej 				ATF_REQUIRE(kevent(kq, event, 1, NULL, 0,
     96  1.1  thorpej 				    NULL) == 0);
     97  1.1  thorpej 			}
     98  1.1  thorpej 		} else {
     99  1.1  thorpej 			ATF_REQUIRE(ntimer1 == TIME1_COUNT);
    100  1.1  thorpej 			ATF_REQUIRE(ntimer2 == 0);
    101  1.1  thorpej 			ntimer2++;
    102  1.1  thorpej 			/*
    103  1.1  thorpej 			 * Make sure TIME2_TOTAL_SEC seconds have
    104  1.1  thorpej 			 * elapsed, allowing for a little slop.
    105  1.1  thorpej 			 */
    106  1.1  thorpej 			ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC,
    107  1.1  thorpej 			    &ts) == 0);
    108  1.1  thorpej 			timespecsub(&ts, &ots, &ts);
    109  1.1  thorpej 			ATF_REQUIRE(ts.tv_sec ==
    110  1.1  thorpej 				    (TIME2_TOTAL_SEC - 1) ||
    111  1.1  thorpej 			    ts.tv_sec == TIME2_TOTAL_SEC);
    112  1.1  thorpej 			if (ts.tv_sec == TIME2_TOTAL_SEC - 1) {
    113  1.1  thorpej 				ATF_REQUIRE(ts.tv_nsec >= 900000000);
    114  1.1  thorpej 			}
    115  1.1  thorpej 			EV_SET(&event[0], 2, EVFILT_TIMER, EV_DELETE,
    116  1.1  thorpej 			    0, 0, NULL);
    117  1.1  thorpej 			ATF_REQUIRE_ERRNO(ENOENT,
    118  1.1  thorpej 			    kevent(kq, event, 1, NULL, 0, NULL) == -1);
    119  1.1  thorpej 			break;
    120  1.1  thorpej 		}
    121  1.1  thorpej 	}
    122  1.1  thorpej 
    123  1.1  thorpej 	/*
    124  1.1  thorpej 	 * Now block in kqueue for TIME2_TOTAL_SEC, and ensure we
    125  1.1  thorpej 	 * don't receive any new events.
    126  1.1  thorpej 	 */
    127  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ots) == 0);
    128  1.1  thorpej 	ts.tv_sec = TIME2_TOTAL_SEC;
    129  1.1  thorpej 	ts.tv_nsec = 0;
    130  1.1  thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 0);
    131  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
    132  1.1  thorpej 	timespecsub(&ts, &ots, &ts);
    133  1.1  thorpej 	ATF_REQUIRE(ts.tv_sec == (TIME2_TOTAL_SEC - 1) ||
    134  1.1  thorpej 	    ts.tv_sec == TIME2_TOTAL_SEC ||
    135  1.1  thorpej 	    ts.tv_sec == (TIME2_TOTAL_SEC + 1));
    136  1.1  thorpej 	if (ts.tv_sec == TIME2_TOTAL_SEC - 1) {
    137  1.1  thorpej 		ATF_REQUIRE(ts.tv_nsec >= 900000000);
    138  1.1  thorpej 	} else if (ts.tv_sec == TIME2_TOTAL_SEC + 1) {
    139  1.1  thorpej 		ATF_REQUIRE(ts.tv_nsec < 500000000);
    140  1.1  thorpej 	}
    141  1.1  thorpej }
    142  1.1  thorpej 
    143  1.1  thorpej ATF_TC(count_expirations);
    144  1.1  thorpej ATF_TC_HEAD(count_expirations, tc)
    145  1.1  thorpej {
    146  1.1  thorpej 	atf_tc_set_md_var(tc, "descr",
    147  1.1  thorpej 	    "tests counting timer expirations");
    148  1.1  thorpej }
    149  1.1  thorpej 
    150  1.1  thorpej ATF_TC_BODY(count_expirations, tc)
    151  1.1  thorpej {
    152  1.1  thorpej 	struct kevent event[1];
    153  1.1  thorpej 	struct timespec ts = { 0, 0 };
    154  1.1  thorpej 	struct timespec sleepts;
    155  1.1  thorpej 	int kq;
    156  1.1  thorpej 
    157  1.1  thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    158  1.1  thorpej 
    159  1.1  thorpej 	EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD, 0, TIME1, NULL);
    160  1.1  thorpej 	ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
    161  1.1  thorpej 
    162  1.1  thorpej 	/* Sleep a little longer to mitigate timing jitter. */
    163  1.1  thorpej 	sleepts.tv_sec = TIME1_TOTAL_SEC;
    164  1.1  thorpej 	sleepts.tv_nsec = 500000000;
    165  1.1  thorpej 	ATF_REQUIRE(nanosleep(&sleepts, NULL) == 0);
    166  1.1  thorpej 
    167  1.1  thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
    168  1.1  thorpej 	ATF_REQUIRE(event[0].ident == 1);
    169  1.1  thorpej 	ATF_REQUIRE(event[0].data == TIME1_COUNT ||
    170  1.1  thorpej 		    event[0].data == TIME1_COUNT + 1);
    171  1.1  thorpej }
    172  1.1  thorpej 
    173  1.1  thorpej ATF_TC(abstime);
    174  1.1  thorpej ATF_TC_HEAD(abstime, tc)
    175  1.1  thorpej {
    176  1.1  thorpej 	atf_tc_set_md_var(tc, "descr",
    177  1.1  thorpej 	    "tests timers with NOTE_ABSTIME");
    178  1.1  thorpej }
    179  1.1  thorpej 
    180  1.1  thorpej ATF_TC_BODY(abstime, tc)
    181  1.1  thorpej {
    182  1.1  thorpej 	struct kevent event[1];
    183  1.1  thorpej 	struct timespec ts, ots;
    184  1.1  thorpej 	time_t seconds;
    185  1.1  thorpej 	int kq;
    186  1.1  thorpej 
    187  1.1  thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    188  1.1  thorpej 
    189  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ots) == 0);
    190  1.1  thorpej 	ATF_REQUIRE(ots.tv_sec < INTPTR_MAX - TIME1_TOTAL_SEC);
    191  1.1  thorpej 
    192  1.1  thorpej 	seconds = ots.tv_sec + TIME1_TOTAL_SEC;
    193  1.1  thorpej 	if (ots.tv_nsec >= 500000000) {
    194  1.1  thorpej 		seconds++;
    195  1.1  thorpej 	}
    196  1.1  thorpej 
    197  1.1  thorpej 	EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD,
    198  1.1  thorpej 	    NOTE_ABSTIME | NOTE_SECONDS, seconds, NULL);
    199  1.1  thorpej 	ATF_REQUIRE(kevent(kq, event, 1, event, 1, NULL) == 1);
    200  1.1  thorpej 
    201  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts) == 0);
    202  1.1  thorpej 	timespecsub(&ts, &ots, &ts);
    203  1.1  thorpej 
    204  1.1  thorpej 	/*
    205  1.1  thorpej 	 * We're not going for precision here; just verify that it was
    206  1.1  thorpej 	 * delivered anywhere between 4.5-6.whatever seconds later.
    207  1.1  thorpej 	 */
    208  1.1  thorpej 	ATF_REQUIRE(ts.tv_sec >= 4 && ts.tv_sec <= 6);
    209  1.1  thorpej 	if (ts.tv_sec == 4) {
    210  1.1  thorpej 		ATF_REQUIRE(ts.tv_nsec >= 500000000);
    211  1.1  thorpej 	}
    212  1.1  thorpej 
    213  1.1  thorpej 	ts.tv_sec = 0;
    214  1.1  thorpej 	ts.tv_nsec = 0;
    215  1.1  thorpej 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &ts) == 1);
    216  1.1  thorpej }
    217  1.1  thorpej 
    218  1.1  thorpej #define	PREC_TIMEOUT_SEC	2
    219  1.1  thorpej 
    220  1.1  thorpej static void
    221  1.1  thorpej do_test_timer_units(const char *which, uint32_t fflag, int64_t data)
    222  1.1  thorpej {
    223  1.1  thorpej 	struct kevent event[1];
    224  1.1  thorpej 	struct timespec ts, ots;
    225  1.1  thorpej 	int kq;
    226  1.1  thorpej 
    227  1.1  thorpej 	ATF_REQUIRE((kq = kqueue()) >= 0);
    228  1.1  thorpej 
    229  1.1  thorpej 	EV_SET(&event[0], 1, EVFILT_TIMER, EV_ADD | EV_ONESHOT,
    230  1.1  thorpej 	    fflag, data, NULL);
    231  1.1  thorpej 
    232  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ots) == 0);
    233  1.1  thorpej 	ATF_REQUIRE(kevent(kq, event, 1, event, 1, NULL) == 1);
    234  1.1  thorpej 	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
    235  1.1  thorpej 
    236  1.1  thorpej 	timespecsub(&ts, &ots, &ts);
    237  1.1  thorpej 	ATF_REQUIRE_MSG(ts.tv_sec == (PREC_TIMEOUT_SEC - 1) ||
    238  1.1  thorpej 		    ts.tv_sec == PREC_TIMEOUT_SEC,
    239  1.1  thorpej 		    "units '%s' failed [sec]", which);
    240  1.1  thorpej 	if (ts.tv_sec == PREC_TIMEOUT_SEC - 1) {
    241  1.1  thorpej 		ATF_REQUIRE_MSG(ts.tv_nsec >= 900000000,
    242  1.1  thorpej 		"units '%s' failed [nsec]", which);
    243  1.1  thorpej 	}
    244  1.1  thorpej 
    245  1.1  thorpej 	(void)close(kq);
    246  1.1  thorpej }
    247  1.1  thorpej 
    248  1.1  thorpej #define	test_timer_units(fflag, data)				\
    249  1.1  thorpej 	do_test_timer_units(#fflag, fflag, data)
    250  1.1  thorpej 
    251  1.1  thorpej ATF_TC(timer_units);
    252  1.1  thorpej ATF_TC_HEAD(timer_units, tc)
    253  1.1  thorpej {
    254  1.1  thorpej 	atf_tc_set_md_var(tc, "descr",
    255  1.1  thorpej 	    "tests timers with NOTE_* units modifiers");
    256  1.1  thorpej }
    257  1.1  thorpej 
    258  1.1  thorpej ATF_TC_BODY(timer_units, tc)
    259  1.1  thorpej {
    260  1.1  thorpej 	test_timer_units(NOTE_SECONDS,  PREC_TIMEOUT_SEC);
    261  1.1  thorpej 	test_timer_units(NOTE_MSECONDS, PREC_TIMEOUT_SEC * 1000);
    262  1.1  thorpej 	test_timer_units(NOTE_USECONDS, PREC_TIMEOUT_SEC * 1000000);
    263  1.1  thorpej 	test_timer_units(NOTE_NSECONDS, PREC_TIMEOUT_SEC * 1000000000);
    264  1.1  thorpej }
    265  1.1  thorpej 
    266  1.1  thorpej ATF_TP_ADD_TCS(tp)
    267  1.1  thorpej {
    268  1.1  thorpej 	ATF_TP_ADD_TC(tp, basic_timer);
    269  1.1  thorpej 	ATF_TP_ADD_TC(tp, count_expirations);
    270  1.1  thorpej 	ATF_TP_ADD_TC(tp, abstime);
    271  1.1  thorpej 	ATF_TP_ADD_TC(tp, timer_units);
    272  1.1  thorpej 
    273  1.1  thorpej 	return atf_no_error();
    274  1.1  thorpej }
    275