]> Git Repo - binutils.git/blob - gdb/testsuite/gdb.threads/tid-reuse.c
Automatic date update in version.in
[binutils.git] / gdb / testsuite / gdb.threads / tid-reuse.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2015-2022 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 <string.h>
25 #include <limits.h>
26
27 /* How many threads fit in the target's thread number space.  */
28 long tid_max = -1;
29
30 /* Number of threads spawned.  */
31 unsigned long thread_counter;
32
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;
43
44 void *
45 do_nothing_thread_func (void *arg)
46 {
47   usleep (1);
48   return NULL;
49 }
50
51 static void
52 check_rc (int rc, const char *what)
53 {
54   if (rc != 0)
55     {
56       fprintf (stderr, "unexpected error from %s: %s (%d)\n",
57                what, strerror (rc), rc);
58       assert (0);
59     }
60 }
61
62 void *
63 spawner_thread_func (void *arg)
64 {
65   while (1)
66     {
67       pthread_t child;
68       int rc;
69
70       thread_counter++;
71
72       rc = pthread_create (&child, NULL, do_nothing_thread_func, NULL);
73       check_rc (rc, "pthread_create");
74
75       rc = pthread_join (child, NULL);
76       check_rc (rc, "pthread_join");
77     }
78
79   return NULL;
80 }
81
82 /* Called after the program is done counting number of spawned threads
83    for a period, to compute REUSE_TIME.  */
84
85 void
86 after_count (void)
87 {
88 }
89
90 /* Called after enough time has passed for TID reuse to occur.  */
91
92 void
93 after_reuse_time (void)
94 {
95 }
96
97 #ifdef __linux__
98
99 /* Get the running system's configured pid_max.  */
100
101 static int
102 linux_proc_get_pid_max (void)
103 {
104   static const char filename[]  ="/proc/sys/kernel/pid_max";
105   FILE *file;
106   char buf[100];
107   int retval = -1;
108
109   file = fopen (filename, "r");
110   if (file == NULL)
111     {
112       fprintf (stderr, "unable to open %s\n", filename);
113       return -1;
114     }
115
116   if (fgets (buf, sizeof (buf), file) != NULL)
117     retval = strtol (buf, NULL, 10);
118
119   fclose (file);
120   return retval;
121 }
122
123 #endif
124
125 int
126 main (int argc, char *argv[])
127 {
128   pthread_t child;
129   int rc;
130   unsigned int reuse_time_raw = 0;
131
132   rc = pthread_create (&child, NULL, spawner_thread_func, NULL);
133   check_rc (rc, "pthread_create spawner_thread");
134
135 #define COUNT_TIME 2
136   sleep (COUNT_TIME);
137
138 #ifdef __linux__
139   tid_max = linux_proc_get_pid_max ();
140 #endif
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.  */
143   if (tid_max > 0)
144     {
145       reuse_time_raw = tid_max / ((float) thread_counter / COUNT_TIME) + 0.5;
146
147       /* Give it a bit more, just in case.  */
148       reuse_time = reuse_time_raw + 3;
149     }
150
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);
160   after_count ();
161
162   sleep (reuse_time);
163
164   after_reuse_time ();
165   return 0;
166 }
This page took 0.03225 seconds and 4 git commands to generate.