Add itimer-related syscall tests

This commit is contained in:
Chen Chengjun
2024-05-31 18:24:20 +08:00
committed by Tate, Hongliang Tian
parent c5ec2e181e
commit d24ddaae66
7 changed files with 170 additions and 0 deletions

View File

@ -22,6 +22,7 @@ TEST_APPS := \
hello_c \
hello_pie \
hello_world \
itimer \
mmap \
mongoose \
network \

View File

@ -0,0 +1,5 @@
# SPDX-License-Identifier: MPL-2.0
include ../test_common.mk
EXTRA_C_FLAGS :=

View File

@ -0,0 +1,91 @@
// SPDX-License-Identifier: MPL-2.0
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
volatile sig_atomic_t counter = 0;
void timer_handler(int signum)
{
counter++;
}
int main()
{
struct itimerval timer;
struct sigaction sa;
int target_count = 3;
struct timespec start_time, end_time;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &timer_handler;
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
// Set the interval to 1.
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 0;
// Start timer.
if (setitimer(ITIMER_REAL, &timer, NULL) == -1) {
perror("Error calling setitimer()");
return EXIT_FAILURE;
}
if (clock_gettime(CLOCK_REALTIME, &start_time) == -1) {
perror("Error calling clock_gettime()");
return EXIT_FAILURE;
}
while (counter < target_count) {
struct itimerval timer_state;
if (getitimer(ITIMER_REAL, &timer_state) == -1) {
perror("Error calling getitimer()");
return EXIT_FAILURE;
}
if (timer_state.it_interval.tv_sec == 1 &&
timer_state.it_value.tv_sec == 0) {
sleep(1);
} else {
perror("Error record time in the timer");
return EXIT_FAILURE;
}
}
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
// Stop timer.
if (setitimer(ITIMER_REAL, &timer, NULL) == -1) {
perror("Error calling setitimer()");
return EXIT_FAILURE;
}
if (clock_gettime(CLOCK_REALTIME, &end_time) == -1) {
perror("Error calling clock_gettime()");
return EXIT_FAILURE;
}
int elapsed_time = (int)(end_time.tv_sec - start_time.tv_sec);
printf("Timer was set to go off every second for a total of %d times.\n",
target_count);
printf("Elapsed time: %d seconds.\n", elapsed_time);
if (elapsed_time == target_count) {
printf("The actual elapsed time matches the expected time.\n");
} else {
printf("There is a discrepancy between actual and expected time.\n");
}
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,61 @@
// SPDX-License-Identifier: MPL-2.0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/syscall.h>
#include <linux/types.h>
#include <string.h>
#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN
static void handler(int sig, siginfo_t *si, void *unused)
{
printf("Caught signal %d\n", sig);
}
int main(int argc, char *argv[])
{
struct sigaction sa;
struct sigevent sev;
timer_t timerid;
struct itimerspec its;
memset(&sa, 0, sizeof(sa));
// Set signal handler
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIG, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
// Create the timer.
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
sev._sigev_un._tid = syscall(SYS_gettid);
if (timer_create(CLOCKID, &sev, &timerid) == -1) {
perror("timer_create");
exit(1);
}
// Enable the timer.
its.it_value.tv_sec = 5;
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
if (timer_settime(timerid, 0, &its, NULL) == -1) {
perror("timer_settime");
exit(1);
}
pause();
return 0;
}

View File

@ -18,6 +18,8 @@ fork_c/fork
getpid/getpid
hello_pie/hello
hello_world/hello_world
itimer/setitimer
itimer/timer_create
mmap/map_shared_anon
pthread/pthread_test
pty/open_pty

View File

@ -29,6 +29,7 @@ TESTS ?= \
statfs_test \
symlink_test \
sync_test \
timers_test \
truncate_test \
uidgid_test \
unlink_test \

View File

@ -0,0 +1,9 @@
TimerTest.ProcessKilledOnCPUSoftLimit
TimerTest.ProcessPingedRepeatedlyAfterCPUSoftLimit
TimerTest.ProcessKilledOnCPUHardLimit
IntervalTimerTest.SingleShotSilent
IntervalTimerTest.PeriodicSilent
IntervalTimerTest.PeriodicThreadDirectedSignal
IntervalTimerTest.RealTimeSignalsAreNotDuplicated
IntervalTimerTest.AlreadyPendingSignal
IntervalTimerTest.IgnoredSignalCountsAsOverrun