1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2015-2022 Free Software Foundation, Inc.
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.
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.
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/>. */
27 /* How many threads fit in the target's thread number space. */
30 /* Number of threads spawned. */
31 unsigned long thread_counter
;
33 /* How long it takes to spawn as many threads as fits in the thread
34 number space. On systems where thread IDs are just monotonically
35 incremented, this is enough for the tid numbers to wrap around. On
36 targets that randomize thread IDs, this is enough time to give each
37 number in the thread number space some chance of reuse. It'll be
38 capped to a lower value if we can't compute it. REUSE_TIME_CAP
39 is the max value, and the default value if ever the program
40 has problem to compute it. */
41 #define REUSE_TIME_CAP 60
42 unsigned int reuse_time
= REUSE_TIME_CAP
;
45 do_nothing_thread_func (void *arg
)
52 check_rc (int rc
, const char *what
)
56 fprintf (stderr
, "unexpected error from %s: %s (%d)\n",
57 what
, strerror (rc
), rc
);
63 spawner_thread_func (void *arg
)
72 rc
= pthread_create (&child
, NULL
, do_nothing_thread_func
, NULL
);
73 check_rc (rc
, "pthread_create");
75 rc
= pthread_join (child
, NULL
);
76 check_rc (rc
, "pthread_join");
82 /* Called after the program is done counting number of spawned threads
83 for a period, to compute REUSE_TIME. */
90 /* Called after enough time has passed for TID reuse to occur. */
93 after_reuse_time (void)
99 /* Get the running system's configured pid_max. */
102 linux_proc_get_pid_max (void)
104 static const char filename
[] ="/proc/sys/kernel/pid_max";
109 file
= fopen (filename
, "r");
112 fprintf (stderr
, "unable to open %s\n", filename
);
116 if (fgets (buf
, sizeof (buf
), file
) != NULL
)
117 retval
= strtol (buf
, NULL
, 10);
126 main (int argc
, char *argv
[])
130 unsigned int reuse_time_raw
= 0;
132 rc
= pthread_create (&child
, NULL
, spawner_thread_func
, NULL
);
133 check_rc (rc
, "pthread_create spawner_thread");
139 tid_max
= linux_proc_get_pid_max ();
141 /* If we don't know how many threads it would take to use the whole
142 number space on this system, just run the test for a bit. */
145 reuse_time_raw
= tid_max
/ ((float) thread_counter
/ COUNT_TIME
) + 0.5;
147 /* Give it a bit more, just in case. */
148 reuse_time
= reuse_time_raw
+ 3;
151 /* 4 seconds were sufficient on the machine this was first observed,
152 an Intel i7-2620M @ 2.70GHz running Linux 3.18.7, with
153 pid_max=32768. Going forward, as machines get faster, this will
154 need less time, unless pid_max is set to a very high number. To
155 avoid unreasonably long test time, cap to an upper bound. */
156 if (reuse_time
> REUSE_TIME_CAP
)
157 reuse_time
= REUSE_TIME_CAP
;
158 printf ("thread_counter=%lu, tid_max = %ld, reuse_time_raw=%u, reuse_time=%u\n",
159 thread_counter
, tid_max
, reuse_time_raw
, reuse_time
);