lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Tests for POSIX timer implementation using process CPU clock. */ |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | |
| 5 | #if _POSIX_THREADS && defined _POSIX_CPUTIME |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <time.h> |
| 12 | #include <pthread.h> |
| 13 | |
| 14 | #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID |
| 15 | #define TEST_CLOCK_MISSING(clock) \ |
| 16 | (setup_test () ? "process CPU clock timer support" : NULL) |
| 17 | |
| 18 | /* This function is intended to rack up both user and system time. */ |
| 19 | static void * |
| 20 | chew_cpu (void *arg) |
| 21 | { |
| 22 | while (1) |
| 23 | { |
| 24 | static volatile char buf[4096]; |
| 25 | for (int i = 0; i < 100; ++i) |
| 26 | for (size_t j = 0; j < sizeof buf; ++j) |
| 27 | buf[j] = 0xaa; |
| 28 | int nullfd = open ("/dev/null", O_WRONLY); |
| 29 | for (int i = 0; i < 100; ++i) |
| 30 | for (size_t j = 0; j < sizeof buf; ++j) |
| 31 | buf[j] = 0xbb; |
| 32 | write (nullfd, (char *) buf, sizeof buf); |
| 33 | close (nullfd); |
| 34 | } |
| 35 | |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | setup_test (void) |
| 41 | { |
| 42 | /* Test timers on our own process CPU clock by having a worker thread |
| 43 | eating CPU. First make sure we can make such timers at all. */ |
| 44 | |
| 45 | timer_t t; |
| 46 | if (timer_create (TEST_CLOCK, NULL, &t) != 0) |
| 47 | { |
| 48 | printf ("timer_create: %m\n"); |
| 49 | return 1; |
| 50 | } |
| 51 | timer_delete (t); |
| 52 | |
| 53 | pthread_t th; |
| 54 | int e = pthread_create (&th, NULL, chew_cpu, NULL); |
| 55 | if (e != 0) |
| 56 | { |
| 57 | printf ("pthread_create: %s\n", strerror (e)); |
| 58 | exit (1); |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | #else |
| 65 | # define TEST_CLOCK_MISSING(clock) "process clocks" |
| 66 | #endif |
| 67 | |
| 68 | #include "tst-timer4.c" |