tid-reuse.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* This testcase is part of GDB, the GNU debugger.
  2. Copyright 2015-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #define _GNU_SOURCE
  14. #include <assert.h>
  15. #include <pthread.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. /* How many threads fit in the target's thread number space. */
  22. long tid_max = -1;
  23. /* Number of threads spawned. */
  24. unsigned long thread_counter;
  25. /* How long it takes to spawn as many threads as fits in the thread
  26. number space. On systems where thread IDs are just monotonically
  27. incremented, this is enough for the tid numbers to wrap around. On
  28. targets that randomize thread IDs, this is enough time to give each
  29. number in the thread number space some chance of reuse. It'll be
  30. capped to a lower value if we can't compute it. REUSE_TIME_CAP
  31. is the max value, and the default value if ever the program
  32. has problem to compute it. */
  33. #define REUSE_TIME_CAP 60
  34. unsigned int reuse_time = REUSE_TIME_CAP;
  35. void *
  36. do_nothing_thread_func (void *arg)
  37. {
  38. usleep (1);
  39. return NULL;
  40. }
  41. static void
  42. check_rc (int rc, const char *what)
  43. {
  44. if (rc != 0)
  45. {
  46. fprintf (stderr, "unexpected error from %s: %s (%d)\n",
  47. what, strerror (rc), rc);
  48. assert (0);
  49. }
  50. }
  51. void *
  52. spawner_thread_func (void *arg)
  53. {
  54. while (1)
  55. {
  56. pthread_t child;
  57. int rc;
  58. thread_counter++;
  59. rc = pthread_create (&child, NULL, do_nothing_thread_func, NULL);
  60. check_rc (rc, "pthread_create");
  61. rc = pthread_join (child, NULL);
  62. check_rc (rc, "pthread_join");
  63. }
  64. return NULL;
  65. }
  66. /* Called after the program is done counting number of spawned threads
  67. for a period, to compute REUSE_TIME. */
  68. void
  69. after_count (void)
  70. {
  71. }
  72. /* Called after enough time has passed for TID reuse to occur. */
  73. void
  74. after_reuse_time (void)
  75. {
  76. }
  77. #ifdef __linux__
  78. /* Get the running system's configured pid_max. */
  79. static int
  80. linux_proc_get_pid_max (void)
  81. {
  82. static const char filename[] ="/proc/sys/kernel/pid_max";
  83. FILE *file;
  84. char buf[100];
  85. int retval = -1;
  86. file = fopen (filename, "r");
  87. if (file == NULL)
  88. {
  89. fprintf (stderr, "unable to open %s\n", filename);
  90. return -1;
  91. }
  92. if (fgets (buf, sizeof (buf), file) != NULL)
  93. retval = strtol (buf, NULL, 10);
  94. fclose (file);
  95. return retval;
  96. }
  97. #endif
  98. int
  99. main (int argc, char *argv[])
  100. {
  101. pthread_t child;
  102. int rc;
  103. unsigned int reuse_time_raw = 0;
  104. rc = pthread_create (&child, NULL, spawner_thread_func, NULL);
  105. check_rc (rc, "pthread_create spawner_thread");
  106. #define COUNT_TIME 2
  107. sleep (COUNT_TIME);
  108. #ifdef __linux__
  109. tid_max = linux_proc_get_pid_max ();
  110. #endif
  111. /* If we don't know how many threads it would take to use the whole
  112. number space on this system, just run the test for a bit. */
  113. if (tid_max > 0)
  114. {
  115. reuse_time_raw = tid_max / ((float) thread_counter / COUNT_TIME) + 0.5;
  116. /* Give it a bit more, just in case. */
  117. reuse_time = reuse_time_raw + 3;
  118. }
  119. /* 4 seconds were sufficient on the machine this was first observed,
  120. an Intel i7-2620M @ 2.70GHz running Linux 3.18.7, with
  121. pid_max=32768. Going forward, as machines get faster, this will
  122. need less time, unless pid_max is set to a very high number. To
  123. avoid unreasonably long test time, cap to an upper bound. */
  124. if (reuse_time > REUSE_TIME_CAP)
  125. reuse_time = REUSE_TIME_CAP;
  126. printf ("thread_counter=%lu, tid_max = %ld, reuse_time_raw=%u, reuse_time=%u\n",
  127. thread_counter, tid_max, reuse_time_raw, reuse_time);
  128. after_count ();
  129. sleep (reuse_time);
  130. after_reuse_time ();
  131. return 0;
  132. }