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