Commit 071af0c9 authored by Mark Brown's avatar Mark Brown Committed by Shuah Khan

selftests: timers: Convert posix_timers test to generate KTAP output

Currently the posix_timers test does not produce KTAP output but rather a
custom format. This means that we only get a pass/fail for the suite, not
for each individual test that the suite does. Convert to using the standard
kselftest output functions which result in KTAP output being generated.

As part of this fix the printing of diagnostics in the unlikely event that
the pthread APIs fail, these were using perror() but the API functions
directly return an error code instead of setting errno.
Signed-off-by: default avatarMark Brown <broonie@kernel.org>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 47903c1d
...@@ -76,22 +76,21 @@ static int check_diff(struct timeval start, struct timeval end) ...@@ -76,22 +76,21 @@ static int check_diff(struct timeval start, struct timeval end)
static int check_itimer(int which) static int check_itimer(int which)
{ {
const char *name;
int err; int err;
struct timeval start, end; struct timeval start, end;
struct itimerval val = { struct itimerval val = {
.it_value.tv_sec = DELAY, .it_value.tv_sec = DELAY,
}; };
printf("Check itimer ");
if (which == ITIMER_VIRTUAL) if (which == ITIMER_VIRTUAL)
printf("virtual... "); name = "ITIMER_VIRTUAL";
else if (which == ITIMER_PROF) else if (which == ITIMER_PROF)
printf("prof... "); name = "ITIMER_PROF";
else if (which == ITIMER_REAL) else if (which == ITIMER_REAL)
printf("real... "); name = "ITIMER_REAL";
else
fflush(stdout); return -1;
done = 0; done = 0;
...@@ -104,13 +103,13 @@ static int check_itimer(int which) ...@@ -104,13 +103,13 @@ static int check_itimer(int which)
err = gettimeofday(&start, NULL); err = gettimeofday(&start, NULL);
if (err < 0) { if (err < 0) {
perror("Can't call gettimeofday()\n"); ksft_perror("Can't call gettimeofday()");
return -1; return -1;
} }
err = setitimer(which, &val, NULL); err = setitimer(which, &val, NULL);
if (err < 0) { if (err < 0) {
perror("Can't set timer\n"); ksft_perror("Can't set timer");
return -1; return -1;
} }
...@@ -123,20 +122,18 @@ static int check_itimer(int which) ...@@ -123,20 +122,18 @@ static int check_itimer(int which)
err = gettimeofday(&end, NULL); err = gettimeofday(&end, NULL);
if (err < 0) { if (err < 0) {
perror("Can't call gettimeofday()\n"); ksft_perror("Can't call gettimeofday()");
return -1; return -1;
} }
if (!check_diff(start, end)) ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
printf("[OK]\n");
else
printf("[FAIL]\n");
return 0; return 0;
} }
static int check_timer_create(int which) static int check_timer_create(int which)
{ {
const char *type;
int err; int err;
timer_t id; timer_t id;
struct timeval start, end; struct timeval start, end;
...@@ -144,31 +141,32 @@ static int check_timer_create(int which) ...@@ -144,31 +141,32 @@ static int check_timer_create(int which)
.it_value.tv_sec = DELAY, .it_value.tv_sec = DELAY,
}; };
printf("Check timer_create() ");
if (which == CLOCK_THREAD_CPUTIME_ID) { if (which == CLOCK_THREAD_CPUTIME_ID) {
printf("per thread... "); type = "thread";
} else if (which == CLOCK_PROCESS_CPUTIME_ID) { } else if (which == CLOCK_PROCESS_CPUTIME_ID) {
printf("per process... "); type = "process";
} else {
ksft_print_msg("Unknown timer_create() type %d\n", which);
return -1;
} }
fflush(stdout);
done = 0; done = 0;
err = timer_create(which, NULL, &id); err = timer_create(which, NULL, &id);
if (err < 0) { if (err < 0) {
perror("Can't create timer\n"); ksft_perror("Can't create timer");
return -1; return -1;
} }
signal(SIGALRM, sig_handler); signal(SIGALRM, sig_handler);
err = gettimeofday(&start, NULL); err = gettimeofday(&start, NULL);
if (err < 0) { if (err < 0) {
perror("Can't call gettimeofday()\n"); ksft_perror("Can't call gettimeofday()");
return -1; return -1;
} }
err = timer_settime(id, 0, &val, NULL); err = timer_settime(id, 0, &val, NULL);
if (err < 0) { if (err < 0) {
perror("Can't set timer\n"); ksft_perror("Can't set timer");
return -1; return -1;
} }
...@@ -176,14 +174,12 @@ static int check_timer_create(int which) ...@@ -176,14 +174,12 @@ static int check_timer_create(int which)
err = gettimeofday(&end, NULL); err = gettimeofday(&end, NULL);
if (err < 0) { if (err < 0) {
perror("Can't call gettimeofday()\n"); ksft_perror("Can't call gettimeofday()");
return -1; return -1;
} }
if (!check_diff(start, end)) ksft_test_result(check_diff(start, end) == 0,
printf("[OK]\n"); "timer_create() per %s\n", type);
else
printf("[FAIL]\n");
return 0; return 0;
} }
...@@ -220,25 +216,25 @@ static int check_timer_distribution(void) ...@@ -220,25 +216,25 @@ static int check_timer_distribution(void)
.it_interval.tv_nsec = 1000 * 1000, .it_interval.tv_nsec = 1000 * 1000,
}; };
printf("Check timer_create() per process signal distribution... ");
fflush(stdout);
remain = nthreads + 1; /* worker threads + this thread */ remain = nthreads + 1; /* worker threads + this thread */
signal(SIGALRM, distribution_handler); signal(SIGALRM, distribution_handler);
err = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); err = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
if (err < 0) { if (err < 0) {
perror("Can't create timer\n"); ksft_perror("Can't create timer");
return -1; return -1;
} }
err = timer_settime(id, 0, &val, NULL); err = timer_settime(id, 0, &val, NULL);
if (err < 0) { if (err < 0) {
perror("Can't set timer\n"); ksft_perror("Can't set timer");
return -1; return -1;
} }
for (i = 0; i < nthreads; i++) { for (i = 0; i < nthreads; i++) {
if (pthread_create(&threads[i], NULL, distribution_thread, NULL)) { err = pthread_create(&threads[i], NULL, distribution_thread,
perror("Can't create thread\n"); NULL);
if (err) {
ksft_print_msg("Can't create thread: %s (%d)\n",
strerror(errno), errno);
return -1; return -1;
} }
} }
...@@ -247,25 +243,30 @@ static int check_timer_distribution(void) ...@@ -247,25 +243,30 @@ static int check_timer_distribution(void)
while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); while (__atomic_load_n(&remain, __ATOMIC_RELAXED));
for (i = 0; i < nthreads; i++) { for (i = 0; i < nthreads; i++) {
if (pthread_join(threads[i], NULL)) { err = pthread_join(threads[i], NULL);
perror("Can't join thread\n"); if (err) {
ksft_print_msg("Can't join thread: %s (%d)\n",
strerror(errno), errno);
return -1; return -1;
} }
} }
if (timer_delete(id)) { if (timer_delete(id)) {
perror("Can't delete timer\n"); ksft_perror("Can't delete timer");
return -1; return -1;
} }
printf("[OK]\n"); ksft_test_result_pass("check_timer_distribution\n");
return 0; return 0;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
printf("Testing posix timers. False negative may happen on CPU execution \n"); ksft_print_header();
printf("based timers if other threads run on the CPU...\n"); ksft_set_plan(6);
ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n");
ksft_print_msg("based timers if other threads run on the CPU...\n");
if (check_itimer(ITIMER_VIRTUAL) < 0) if (check_itimer(ITIMER_VIRTUAL) < 0)
return ksft_exit_fail(); return ksft_exit_fail();
...@@ -294,5 +295,5 @@ int main(int argc, char **argv) ...@@ -294,5 +295,5 @@ int main(int argc, char **argv)
if (check_timer_distribution() < 0) if (check_timer_distribution() < 0)
return ksft_exit_fail(); return ksft_exit_fail();
return ksft_exit_pass(); ksft_finished();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment