1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <stdio.h>
20#include <unistd.h>
21#include <stdlib.h>
22#include <sys/time.h>
23#include <pthread.h>
24#include "../kselftest.h"
25
26
27pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER;
28
29pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
30
31
32#define MAX_THREADS 128
33#define LISTSIZE 128
34
35int done = 0;
36
37struct timespec global_list[LISTSIZE];
38int listcount = 0;
39
40
41void checklist(struct timespec *list, int size)
42{
43 int i, j;
44 struct timespec *a, *b;
45
46
47 for (i = 0; i < size-1; i++) {
48 a = &list[i];
49 b = &list[i+1];
50
51
52 if ((b->tv_sec <= a->tv_sec) &&
53 (b->tv_nsec < a->tv_nsec)) {
54
55
56 done = 1;
57
58
59 pthread_mutex_lock(&print_lock);
60
61
62 printf("\n");
63 for (j = 0; j < size; j++) {
64 if (j == i)
65 printf("---------------\n");
66 printf("%lu:%lu\n", list[j].tv_sec, list[j].tv_nsec);
67 if (j == i+1)
68 printf("---------------\n");
69 }
70 printf("[FAILED]\n");
71
72 pthread_mutex_unlock(&print_lock);
73 }
74 }
75}
76
77
78
79
80
81void *shared_thread(void *arg)
82{
83 while (!done) {
84
85 pthread_mutex_lock(&list_lock);
86
87
88 if (listcount >= LISTSIZE) {
89 checklist(global_list, LISTSIZE);
90 listcount = 0;
91 }
92 clock_gettime(CLOCK_MONOTONIC, &global_list[listcount++]);
93
94 pthread_mutex_unlock(&list_lock);
95 }
96 return NULL;
97}
98
99
100
101
102
103void *independent_thread(void *arg)
104{
105 struct timespec my_list[LISTSIZE];
106 int count;
107
108 while (!done) {
109
110 for (count = 0; count < LISTSIZE; count++)
111 clock_gettime(CLOCK_MONOTONIC, &my_list[count]);
112 checklist(my_list, LISTSIZE);
113 }
114 return NULL;
115}
116
117#define DEFAULT_THREAD_COUNT 8
118#define DEFAULT_RUNTIME 30
119
120int main(int argc, char **argv)
121{
122 int thread_count, i;
123 time_t start, now, runtime;
124 char buf[255];
125 pthread_t pth[MAX_THREADS];
126 int opt;
127 void *tret;
128 int ret = 0;
129 void *(*thread)(void *) = shared_thread;
130
131 thread_count = DEFAULT_THREAD_COUNT;
132 runtime = DEFAULT_RUNTIME;
133
134
135 while ((opt = getopt(argc, argv, "t:n:i")) != -1) {
136 switch (opt) {
137 case 't':
138 runtime = atoi(optarg);
139 break;
140 case 'n':
141 thread_count = atoi(optarg);
142 break;
143 case 'i':
144 thread = independent_thread;
145 printf("using independent threads\n");
146 break;
147 default:
148 printf("Usage: %s [-t <secs>] [-n <numthreads>] [-i]\n", argv[0]);
149 printf(" -t: time to run\n");
150 printf(" -n: number of threads\n");
151 printf(" -i: use independent threads\n");
152 return -1;
153 }
154 }
155
156 if (thread_count > MAX_THREADS)
157 thread_count = MAX_THREADS;
158
159
160 setbuf(stdout, NULL);
161
162 start = time(0);
163 strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&start));
164 printf("%s\n", buf);
165 printf("Testing consistency with %i threads for %ld seconds: ", thread_count, runtime);
166 fflush(stdout);
167
168
169 for (i = 0; i < thread_count; i++)
170 pthread_create(&pth[i], 0, thread, 0);
171
172 while (time(&now) < start + runtime) {
173 sleep(1);
174 if (done) {
175 ret = 1;
176 strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&now));
177 printf("%s\n", buf);
178 goto out;
179 }
180 }
181 printf("[OK]\n");
182 done = 1;
183
184out:
185
186 for (i = 0; i < thread_count; i++)
187 pthread_join(pth[i], &tret);
188
189
190 if (ret)
191 ksft_exit_fail();
192 return ksft_exit_pass();
193}
194