1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2015-2018 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/>. */
26 /* How many threads fit in the target's thread number space. */
29 /* Number of threads spawned. */
30 unsigned long thread_counter
;
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;
41 do_nothing_thread_func (void *arg
)
48 spawner_thread_func (void *arg
)
57 rc
= pthread_create (&child
, NULL
, do_nothing_thread_func
, NULL
);
60 rc
= pthread_join (child
, NULL
);
67 /* Called after the program is done counting number of spawned threads
68 for a period, to compute REUSE_TIME. */
75 /* Called after enough time has passed for TID reuse to occur. */
78 after_reuse_time (void)
84 /* Get the running system's configured pid_max. */
87 linux_proc_get_pid_max (void)
89 static const char filename
[] ="/proc/sys/kernel/pid_max";
94 file
= fopen (filename
, "r");
97 fprintf (stderr
, "unable to open %s\n", filename
);
101 if (fgets (buf
, sizeof (buf
), file
) != NULL
)
102 retval
= strtol (buf
, NULL
, 10);
111 main (int argc
, char *argv
[])
115 unsigned int reuse_time_raw
= 0;
117 rc
= pthread_create (&child
, NULL
, spawner_thread_func
, NULL
);
124 tid_max
= linux_proc_get_pid_max ();
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. */
130 reuse_time_raw
= tid_max
/ ((float) thread_counter
/ COUNT_TIME
) + 0.5;
132 /* Give it a bit more, just in case. */
133 reuse_time
= reuse_time_raw
+ 3;
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. */
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
);
This page took 0.040399 seconds and 4 git commands to generate.