2 * by: john stultz (johnstul@us.ibm.com)
3 * (C) Copyright IBM 2004, 2005, 2006, 2012
4 * Licensed under the GPLv2
7 * $ gcc threadtest.c -o threadtest -lrt
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.
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.
24 #include "../kselftest.h"
26 /* serializes shared list access */
27 pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER;
28 /* serializes console output */
29 pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
32 #define MAX_THREADS 128
37 struct timespec global_list[LISTSIZE];
41 void checklist(struct timespec *list, int size)
44 struct timespec *a, *b;
47 for (i = 0; i < size-1; i++) {
51 /* look for any time inconsistencies */
52 if ((b->tv_sec <= a->tv_sec) &&
53 (b->tv_nsec < a->tv_nsec)) {
55 /* flag other threads */
58 /*serialize printing to avoid junky output*/
59 pthread_mutex_lock(&print_lock);
63 for (j = 0; j < size; j++) {
65 printf("---------------\n");
66 printf("%lu:%lu\n", list[j].tv_sec, list[j].tv_nsec);
68 printf("---------------\n");
72 pthread_mutex_unlock(&print_lock);
77 /* The shared thread shares a global list
78 * that each thread fills while holding the lock.
79 * This stresses clock syncronization across cpus.
81 void *shared_thread(void *arg)
84 /* protect the list */
85 pthread_mutex_lock(&list_lock);
87 /* see if we're ready to check the list */
88 if (listcount >= LISTSIZE) {
89 checklist(global_list, LISTSIZE);
92 clock_gettime(CLOCK_MONOTONIC, &global_list[listcount++]);
94 pthread_mutex_unlock(&list_lock);
100 /* Each independent thread fills in its own
101 * list. This stresses clock_gettime() lock contention.
103 void *independent_thread(void *arg)
105 struct timespec my_list[LISTSIZE];
110 for (count = 0; count < LISTSIZE; count++)
111 clock_gettime(CLOCK_MONOTONIC, &my_list[count]);
112 checklist(my_list, LISTSIZE);
117 #define DEFAULT_THREAD_COUNT 8
118 #define DEFAULT_RUNTIME 30
120 int main(int argc, char **argv)
123 time_t start, now, runtime;
125 pthread_t pth[MAX_THREADS];
129 void *(*thread)(void *) = shared_thread;
131 thread_count = DEFAULT_THREAD_COUNT;
132 runtime = DEFAULT_RUNTIME;
134 /* Process arguments */
135 while ((opt = getopt(argc, argv, "t:n:i")) != -1) {
138 runtime = atoi(optarg);
141 thread_count = atoi(optarg);
144 thread = independent_thread;
145 printf("using independent threads\n");
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");
156 if (thread_count > MAX_THREADS)
157 thread_count = MAX_THREADS;
160 setbuf(stdout, NULL);
163 strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&start));
165 printf("Testing consistency with %i threads for %ld seconds: ", thread_count, runtime);
168 for (i = 0; i < thread_count; i++)
169 pthread_create(&pth[i], 0, thread, 0);
171 while (time(&now) < start + runtime) {
175 strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&now));
185 for (i = 0; i < thread_count; i++)
186 pthread_join(pth[i], &tret);
191 return ksft_exit_pass();