threadtest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /* serializes shared list access */
  37. pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER;
  38. /* serializes console output */
  39. pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
  40. #define MAX_THREADS 128
  41. #define LISTSIZE 128
  42. int done = 0;
  43. struct timespec global_list[LISTSIZE];
  44. int listcount = 0;
  45. void checklist(struct timespec *list, int size)
  46. {
  47. int i, j;
  48. struct timespec *a, *b;
  49. /* scan the list */
  50. for (i = 0; i < size-1; i++) {
  51. a = &list[i];
  52. b = &list[i+1];
  53. /* look for any time inconsistencies */
  54. if ((b->tv_sec <= a->tv_sec) &&
  55. (b->tv_nsec < a->tv_nsec)) {
  56. /* flag other threads */
  57. done = 1;
  58. /*serialize printing to avoid junky output*/
  59. pthread_mutex_lock(&print_lock);
  60. /* dump the list */
  61. printf("\n");
  62. for (j = 0; j < size; j++) {
  63. if (j == i)
  64. printf("---------------\n");
  65. printf("%lu:%lu\n", list[j].tv_sec, list[j].tv_nsec);
  66. if (j == i+1)
  67. printf("---------------\n");
  68. }
  69. printf("[FAILED]\n");
  70. pthread_mutex_unlock(&print_lock);
  71. }
  72. }
  73. }
  74. /* The shared thread shares a global list
  75. * that each thread fills while holding the lock.
  76. * This stresses clock syncronization across cpus.
  77. */
  78. void *shared_thread(void *arg)
  79. {
  80. while (!done) {
  81. /* protect the list */
  82. pthread_mutex_lock(&list_lock);
  83. /* see if we're ready to check the list */
  84. if (listcount >= LISTSIZE) {
  85. checklist(global_list, LISTSIZE);
  86. listcount = 0;
  87. }
  88. clock_gettime(CLOCK_MONOTONIC, &global_list[listcount++]);
  89. pthread_mutex_unlock(&list_lock);
  90. }
  91. return NULL;
  92. }
  93. /* Each independent thread fills in its own
  94. * list. This stresses clock_gettime() lock contention.
  95. */
  96. void *independent_thread(void *arg)
  97. {
  98. struct timespec my_list[LISTSIZE];
  99. int count;
  100. while (!done) {
  101. /* fill the list */
  102. for (count = 0; count < LISTSIZE; count++)
  103. clock_gettime(CLOCK_MONOTONIC, &my_list[count]);
  104. checklist(my_list, LISTSIZE);
  105. }
  106. return NULL;
  107. }
  108. #define DEFAULT_THREAD_COUNT 8
  109. #define DEFAULT_RUNTIME 30
  110. int main(int argc, char **argv)
  111. {
  112. int thread_count, i;
  113. time_t start, now, runtime;
  114. char buf[255];
  115. pthread_t pth[MAX_THREADS];
  116. int opt;
  117. void *tret;
  118. int ret = 0;
  119. void *(*thread)(void *) = shared_thread;
  120. thread_count = DEFAULT_THREAD_COUNT;
  121. runtime = DEFAULT_RUNTIME;
  122. /* Process arguments */
  123. while ((opt = getopt(argc, argv, "t:n:i")) != -1) {
  124. switch (opt) {
  125. case 't':
  126. runtime = atoi(optarg);
  127. break;
  128. case 'n':
  129. thread_count = atoi(optarg);
  130. break;
  131. case 'i':
  132. thread = independent_thread;
  133. printf("using independent threads\n");
  134. break;
  135. default:
  136. printf("Usage: %s [-t <secs>] [-n <numthreads>] [-i]\n", argv[0]);
  137. printf(" -t: time to run\n");
  138. printf(" -n: number of threads\n");
  139. printf(" -i: use independent threads\n");
  140. return -1;
  141. }
  142. }
  143. if (thread_count > MAX_THREADS)
  144. thread_count = MAX_THREADS;
  145. setbuf(stdout, NULL);
  146. start = time(0);
  147. strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&start));
  148. printf("%s\n", buf);
  149. printf("Testing consistency with %i threads for %ld seconds: ", thread_count, runtime);
  150. /* spawn */
  151. for (i = 0; i < thread_count; i++)
  152. pthread_create(&pth[i], 0, thread, 0);
  153. while (time(&now) < start + runtime) {
  154. sleep(1);
  155. if (done) {
  156. ret = 1;
  157. strftime(buf, 255, "%a, %d %b %Y %T %z", localtime(&now));
  158. printf("%s\n", buf);
  159. goto out;
  160. }
  161. }
  162. printf("[OK]\n");
  163. done = 1;
  164. out:
  165. /* wait */
  166. for (i = 0; i < thread_count; i++)
  167. pthread_join(pth[i], &tret);
  168. /* die */
  169. if (ret)
  170. ksft_exit_fail();
  171. return ksft_exit_pass();
  172. }