Skip to content

Commit

Permalink
test/zdtm: add a new test to check non-periodic timers
Browse files Browse the repository at this point in the history
It creates a few timers with log expiration intervals, waites for C/R
and check that timers are armed and their intervals have been restored.
  • Loading branch information
avagin committed Jan 17, 2025
1 parent 167dcc9 commit c02e313
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TST_NOFILE := \
sse20 \
mprotect00 \
timers \
timers01 \
timerfd \
unbound_sock \
sched_prio00 \
Expand Down
74 changes: 74 additions & 0 deletions test/zdtm/static/timers01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>

#include "zdtmtst.h"

const char *test_doc = "Checks non-periodic timers\n";
const char *test_author = "Andrei Vagin <avagin@gmail.com>";

static struct {
const int timer_type;
const int signal;
volatile sig_atomic_t count;
} timer_tests[] = {
/* from slowest to fastest */
{ ITIMER_VIRTUAL, SIGVTALRM },
{ ITIMER_PROF, SIGPROF },
{ ITIMER_REAL, SIGALRM },
};

#define NUM_TIMERS (sizeof(timer_tests) / sizeof(timer_tests[0]))

Check warning on line 21 in test/zdtm/static/timers01.c

View workflow job for this annotation

GitHub Actions / build

#define TIMER_TIMEOUT 3600
#define TIMER_ALLOWED_DELTA 300

static void setup_timers(void)
{
int i;
struct itimerval tv = {
.it_interval = { .tv_sec = 0, .tv_usec = 0 },
.it_value = { .tv_sec = TIMER_TIMEOUT, .tv_usec = 0 },
};

for (i = 0; i < NUM_TIMERS; i++) {
if (setitimer(timer_tests[i].timer_type, &tv, NULL) < 0) {
pr_perror("can't set timer %d", i);
exit(1);
}
}
}

static void check_timers(void)
{
int i;

for (i = 0; i < NUM_TIMERS; i++) {
struct itimerval tv = {};

if (getitimer(timer_tests[i].timer_type, &tv)) {
pr_perror("gettimer");
exit(1);
}
if (tv.it_value.tv_sec > TIMER_TIMEOUT ||
tv.it_value.tv_sec < TIMER_TIMEOUT - TIMER_ALLOWED_DELTA) {
fail("%ld isn't in [%d, %d]", (long)tv.it_value.tv_sec,
TIMER_TIMEOUT,

Check warning on line 55 in test/zdtm/static/timers01.c

View workflow job for this annotation

GitHub Actions / build

TIMER_TIMEOUT - TIMER_ALLOWED_DELTA);
exit(1);
}
}
pass();
}

int main(int argc, char **argv)
{
test_init(argc, argv);

setup_timers();

test_daemon();
test_waitsig();

check_timers();
return 0;
}

0 comments on commit c02e313

Please sign in to comment.