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