1 /* threadtest.c
2  *		by: john stultz (johnstul@us.ibm.com)
3  *		(C) Copyright IBM 2004, 2005, 2006, 2012
4  *		Licensed under the GPLv2
5  *
6  *  To build:
7  *	$ gcc threadtest.c -o threadtest -lrt
8  *
9  *   This program is free software: you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation, either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  */
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <pthread.h>
24 #ifdef KTEST
25 #include "../kselftest.h"
26 #else
27 static inline int ksft_exit_pass(void)
28 {
29 	exit(0);
30 }
31 static inline int ksft_exit_fail(void)
32 {
33 	exit(1);
34 }
35 #endif
36 
37 
38 /* serializes shared list access */
39 pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER;
40 /* serializes console output */
41 pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
42 
43 
44 #define MAX_THREADS 128
45 #define LISTSIZE 128
46 
47 int done = 0;
48 
49 struct timespec global_list[LISTSIZE];
50 int listcount = 0;
51 
52 
53 void checklist(struct timespec *list, int size)
54 {
55 	int i, j;
56 	struct timespec *a, *b;
57 
58 	/* scan the list */
59 	for (i = 0; i < size-1; i++) {
60 		a = &list[i];
61 		b = &list[i+1];
62 
63 		/* look for any time inconsistencies */
64 		if ((b->tv_sec <= a->tv_sec) &&
65 			(b->tv_nsec < a->tv_nsec)) {
66 
67 			/* flag other threads */
68 			done = 1;
69 
70 			/*serialize printing to avoid junky output*/
71 			pthread_mutex_lock(&print_lock);
72 
73 			/* dump the list */
74 			printf("\n");
75 			for (j = 0; j < size; j++) {
76 				if (j == i)
77 					printf("---------------\n");
78 				printf("%lu:%lu\n", list[j].tv_sec, list[j].tv_nsec);
79 				if (j == i+1)
80 					printf("---------------\n");
81 			}
82 			printf("[FAILED]\n");
83 
84 			pthread_mutex_unlock(&print_lock);
85 		}
86 	}
87 }
88 
89 /* The shared thread shares a global list
90  * that each thread fills while holding the lock.
91  * This stresses clock syncronization across cpus.
92  */
93 void *shared_thread(void *arg)
94 {
95 	while (!done) {
96 		/* protect the list */
97 		pthread_mutex_lock(&list_lock);
98 
99 		/* see if we're ready to check the list */
100 		if (listcount >= LISTSIZE) {
101 			checklist(global_list, LISTSIZE);
102 			listcount = 0;
103 		}
104 		clock_gettime(CLOCK_MONOTONIC, &global_list[listcount++]);
105 
106 		pthread_mutex_unlock(&list_lock);
107 	}
108 	return NULL;
109 }
110 
111 
112 /* Each independent thread fills in its own
113  * list. This stresses clock_gettime() lock contention.
114  */
115 void *independent_thread(void *arg)
116 {
117 	struct timespec my_list[LISTSIZE];
118 	int count;
119 
120 	while (!done) {
121 		/* fill the list */
122 		for (count = 0; count < LISTSIZE; count++)
123 			clock_gettime(CLOCK_MONOTONIC, &my_list[count]);
124 		checklist(my_list, LISTSIZE);
125 	}
126 	return NULL;
127 }
128 
129 #define DEFAULT_THREAD_COUNT 8
130 #define DEFAULT_RUNTIME 30
131 
132 int main(int argc, char **argv)
133 {
134 	int thread_count, i;
135 	time_t start, now, runtime;
136 	char buf[255];
137 	pthread_t pth[MAX_THREADS];
138 	int opt;
139 	void *tret;
140 	int ret = 0;
141 	void *(*thread)(void *) = shared_thread;
142 
143 	thread_count = DEFAULT_THREAD_COUNT;
144 	runtime = DEFAULT_RUNTIME;
145 
146 	/* Process arguments */
147 	while ((opt = getopt(argc, argv, "t:n:i")) != -1) {
148 		switch (opt) {
149 		case 't':
150 			runtime = atoi(optarg);
151 			break;
152 		case 'n':
153 			thread_count = atoi(optarg);
154 			break;
155 		case 'i':
156 			thread = independent_thread;
157 			printf("using independent threads\n");
158 			break;
159 		default:
160 			printf("Usage: %s [-t <secs>] [-n <numthreads>] [-i]\n", argv[0]);
161 			printf("	-t: time to run\n");
162 			printf("	-n: number of threads\n");
163 			printf("	-i: use independent threads\n");
164 			return -1;
165 		}
166 	}
167 
168 	if (thread_count > MAX_THREADS)
169 		thread_count = MAX_THREADS;
170 
171 
172 	setbuf(stdout, NULL);
173 
174 	start = time(0);
175 	strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&start));
176 	printf("%s\n", buf);
177 	printf("Testing consistency with %i threads for %ld seconds: ", thread_count, runtime);
178 
179 	/* spawn */
180 	for (i = 0; i < thread_count; i++)
181 		pthread_create(&pth[i], 0, thread, 0);
182 
183 	while (time(&now) < start + runtime) {
184 		sleep(1);
185 		if (done) {
186 			ret = 1;
187 			strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&now));
188 			printf("%s\n", buf);
189 			goto out;
190 		}
191 	}
192 	printf("[OK]\n");
193 	done = 1;
194 
195 out:
196 	/* wait */
197 	for (i = 0; i < thread_count; i++)
198 		pthread_join(pth[i], &tret);
199 
200 	/* die */
201 	if (ret)
202 		ksft_exit_fail();
203 	return ksft_exit_pass();
204 }
205