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 | #include <support/xunistd.h> |
15 | |
16 | #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID |
17 | #define TEST_CLOCK_MISSING(clock) \ |
18 | (setup_test () ? "process CPU clock timer support" : NULL) |
19 | |
20 | /* This function is intended to rack up both user and system time. */ |
21 | static void * |
22 | chew_cpu (void *arg) |
23 | { |
24 | while (1) |
25 | { |
26 | static volatile char buf[4096]; |
27 | for (int i = 0; i < 100; ++i) |
28 | for (size_t j = 0; j < sizeof buf; ++j) |
29 | buf[j] = 0xaa; |
30 | int nullfd = open (file: "/dev/null" , O_WRONLY); |
31 | for (int i = 0; i < 100; ++i) |
32 | for (size_t j = 0; j < sizeof buf; ++j) |
33 | buf[j] = 0xbb; |
34 | xwrite (nullfd, (char *) buf, sizeof buf); |
35 | close (fd: nullfd); |
36 | } |
37 | |
38 | return NULL; |
39 | } |
40 | |
41 | static int |
42 | setup_test (void) |
43 | { |
44 | /* Test timers on our own process CPU clock by having a worker thread |
45 | eating CPU. First make sure we can make such timers at all. */ |
46 | |
47 | timer_t t; |
48 | if (timer_create (TEST_CLOCK, NULL, timerid: &t) != 0) |
49 | { |
50 | printf (format: "timer_create: %m\n" ); |
51 | return 1; |
52 | } |
53 | timer_delete (timerid: t); |
54 | |
55 | pthread_t th; |
56 | int e = pthread_create (newthread: &th, NULL, start_routine: chew_cpu, NULL); |
57 | if (e != 0) |
58 | { |
59 | printf (format: "pthread_create: %s\n" , strerror (errnum: e)); |
60 | exit (1); |
61 | } |
62 | |
63 | return 0; |
64 | } |
65 | |
66 | #else |
67 | # define TEST_CLOCK_MISSING(clock) "process clocks" |
68 | #endif |
69 | |
70 | #include "tst-timer4.c" |
71 | |