1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
34 #include <sys/param.h>
35 #include <sys/types.h>
38 # define MAX_THREADS 100000
40 #ifndef DEFAULT_THREADS
41 # define DEFAULT_THREADS 50
45 #define OPT_TO_THREAD 300
46 #define OPT_TO_PROCESS 301
47 #define OPT_SYNC_SIGNAL 302
48 #define OPT_SYNC_JOIN 303
49 #define OPT_TOPLEVEL 304
52 static const struct argp_option options
[] =
54 { NULL
, 0, NULL
, 0, "\
55 This is a test for threads so we allow ther user to selection the number of \
56 threads which are used at any one time. Independently the total number of \
57 rounds can be selected. This is the total number of threads which will have \
58 run when the process terminates:" },
59 { "threads", 't', "NUMBER", 0, "Number of threads used at once" },
60 { "starts", 's', "NUMBER", 0, "Total number of working threads" },
61 { "toplevel", OPT_TOPLEVEL
, "NUMBER", 0,
62 "Number of toplevel threads which start the other threads; this \
63 implies --sync-join" },
65 { NULL
, 0, NULL
, 0, "\
66 Each thread can do one of two things: sleep or do work. The latter is 100% \
67 CPU bound. The work load is the probability a thread does work. All values \
68 from zero to 100 (inclusive) are valid. How often each thread repeats this \
69 can be determined by the number of rounds. The work cost determines how long \
70 each work session (not sleeping) takes. If it is zero a thread would \
71 effectively nothing. By setting the number of rounds to zero the thread \
72 does no work at all and pure thread creation times can be measured." },
73 { "workload", 'w', "PERCENT", 0, "Percentage of time spent working" },
74 { "workcost", 'c', "NUMBER", 0,
75 "Factor in the cost of each round of working" },
76 { "rounds", 'r', "NUMBER", 0, "Number of rounds each thread runs" },
78 { NULL
, 0, NULL
, 0, "\
79 There are a number of different methods how thread creation can be \
80 synchronized. Synchronization is necessary since the number of concurrently \
81 running threads is limited." },
82 { "sync-signal", OPT_SYNC_SIGNAL
, NULL
, 0,
83 "Synchronize using a signal (default)" },
84 { "sync-join", OPT_SYNC_JOIN
, NULL
, 0, "Synchronize using pthread_join" },
86 { NULL
, 0, NULL
, 0, "\
87 One parameter for each threads execution is the size of the stack. If this \
88 parameter is not used the system's default stack size is used. If many \
89 threads are used the stack size should be chosen quite small." },
90 { "stacksize", 'S', "BYTES", 0, "Size of threads stack" },
91 { "guardsize", 'g', "BYTES", 0,
92 "Size of stack guard area; must fit into the stack" },
94 { NULL
, 0, NULL
, 0, "Signal options:" },
95 { "to-thread", OPT_TO_THREAD
, NULL
, 0, "Send signal to main thread" },
96 { "to-process", OPT_TO_PROCESS
, NULL
, 0,
97 "Send signal to process (default)" },
99 { NULL
, 0, NULL
, 0, "Administrative options:" },
100 { "progress", 'p', NULL
, 0, "Show signs of progress" },
101 { "timing", 'T', NULL
, 0,
102 "Measure time from startup to the last thread finishing" },
103 { NULL
, 0, NULL
, 0, NULL
}
106 /* Prototype for option handler. */
107 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
109 /* Data structure to communicate with argp functions. */
110 static struct argp argp
=
116 static unsigned long int threads
= DEFAULT_THREADS
;
117 static unsigned long int workload
= 75;
118 static unsigned long int workcost
= 20;
119 static unsigned long int rounds
= 10;
120 static long int starts
= 5000;
121 static unsigned long int stacksize
;
122 static long int guardsize
= -1;
123 static bool progress
;
125 static bool to_thread
;
126 static unsigned long int toplevel
= 1;
129 static long int running
;
130 static pthread_mutex_t running_mutex
= PTHREAD_MUTEX_INITIALIZER
;
133 static pthread_t tmain
;
136 static struct timespec start_time
;
139 static pthread_mutex_t sum_mutex
= PTHREAD_MUTEX_INITIALIZER
;
150 /* We use 64bit values for the times. */
151 typedef unsigned long long int hp_timing_t
;
154 /* Attributes for all created threads. */
155 static pthread_attr_t attr
;
162 unsigned int state
= (unsigned long int) arg
;
164 for (i
= 0; i
< rounds
; ++i
)
166 /* Determine what to do. */
169 /* Uniform distribution. */
171 rnum
= rand_r (&state
);
172 while (rnum
>= UINT_MAX
- (UINT_MAX
% 100));
179 int a
[4] = { i
, rnum
, i
+ rnum
, rnum
- i
};
182 write (STDERR_FILENO
, "c", 1);
184 for (j
= 0; j
< workcost
; ++j
)
188 a
[2] += a
[1] ^ 0x3423423;
192 pthread_mutex_lock (&sum_mutex
);
193 sum
+= a
[0] + a
[1] + a
[2] + a
[3];
194 pthread_mutex_unlock (&sum_mutex
);
202 tv
.tv_nsec
= 10000000;
205 write (STDERR_FILENO
, "w", 1);
207 nanosleep (&tv
, NULL
);
216 thread_function (void *arg
)
220 pthread_mutex_lock (&running_mutex
);
221 if (--running
<= 0 && starts
<= 0)
225 write (STDERR_FILENO
, "\n", 1);
229 struct timespec end_time
;
231 if (clock_gettime (cl
, &end_time
) == 0)
233 end_time
.tv_sec
-= start_time
.tv_sec
;
234 end_time
.tv_nsec
-= start_time
.tv_nsec
;
235 if (end_time
.tv_nsec
< 0)
237 end_time
.tv_nsec
+= 1000000000;
241 printf ("\nRuntime: %lu.%09lu seconds\n",
242 (unsigned long int) end_time
.tv_sec
,
243 (unsigned long int) end_time
.tv_nsec
);
247 printf ("Result: %08x\n", sum
);
251 pthread_mutex_unlock (&running_mutex
);
253 if (sync_method
== sync_signal
)
256 /* This code sends a signal to the main thread. */
257 pthread_kill (tmain
, SIGUSR1
);
259 /* Use this code to test sending a signal to the process. */
264 write (STDERR_FILENO
, "f", 1);
273 unsigned int threads
;
278 start_threads (void *arg
)
280 struct start_info
*si
= arg
;
281 unsigned int starts
= si
->starts
;
282 pthread_t ths
[si
->threads
];
283 unsigned int state
= starts
;
289 write (STDERR_FILENO
, "T", 1);
291 memset (ths
, '\0', sizeof (pthread_t
) * si
->threads
);
297 /* Wait for the threads in the order they were created. */
298 err
= pthread_join (ths
[i
], NULL
);
300 error (EXIT_FAILURE
, err
, "cannot join thread");
303 write (STDERR_FILENO
, "f", 1);
306 err
= pthread_create (&ths
[i
], &attr
, work
,
307 (void *) (rand_r (&state
) + starts
+ i
));
310 error (EXIT_FAILURE
, err
, "cannot start thread");
313 write (STDERR_FILENO
, "t", 1);
315 if (++i
== si
->threads
)
324 err
= pthread_join (ths
[i
], NULL
);
326 error (EXIT_FAILURE
, err
, "cannot join thread");
329 write (STDERR_FILENO
, "f", 1);
332 if (++i
== si
->threads
)
338 write (STDERR_FILENO
, "F", 1);
345 main (int argc
, char *argv
[])
350 pthread_t
*ths
= NULL
;
355 /* Parse and process arguments. */
356 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
358 if (sync_method
== sync_join
)
360 ths
= (pthread_t
*) calloc (threads
, sizeof (pthread_t
));
362 error (EXIT_FAILURE
, errno
,
363 "cannot allocate memory for thread descriptor array");
373 if (toplevel
> threads
)
375 printf ("resetting number of toplevel threads to %lu to not surpass number to concurrent threads\n",
382 if (clock_getcpuclockid (0, &cl
) != 0
383 || clock_gettime (cl
, &start_time
) != 0)
387 /* We need this later. */
389 tmain
= pthread_self ();
391 /* We use signal SIGUSR1 for communication between the threads and
392 the main thread. We only want sychronous notification. */
393 if (sync_method
== sync_signal
)
396 sigaddset (&ss
, SIGUSR1
);
397 if (sigprocmask (SIG_BLOCK
, &ss
, NULL
) != 0)
398 error (EXIT_FAILURE
, errno
, "cannot set signal mask");
401 /* Create the thread attributes. */
402 pthread_attr_init (&attr
);
404 /* If the user provided a stack size use it. */
406 && pthread_attr_setstacksize (&attr
, stacksize
) != 0)
407 puts ("could not set stack size; will use default");
408 /* And stack guard size. */
410 && pthread_attr_setguardsize (&attr
, guardsize
) != 0)
411 puts ("invalid stack guard size; will use default");
413 /* All threads are created detached if we are not using pthread_join
415 if (sync_method
!= sync_join
)
416 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
418 if (sync_method
== sync_signal
)
423 bool do_wait
= false;
425 pthread_mutex_lock (&running_mutex
);
429 do_wait
= ++running
>= threads
&& starts
> 0;
431 pthread_mutex_unlock (&running_mutex
);
437 write (STDERR_FILENO
, "t", 1);
439 err
= pthread_create (&ths
[empty
], &attr
, thread_function
,
442 error (EXIT_FAILURE
, err
, "cannot start thread %lu", starts
);
448 sigwaitinfo (&ss
, NULL
);
451 /* Do nothing anymore. On of the threads will terminate the program. */
453 sigdelset (&ss
, SIGINT
);
459 pthread_t ths
[toplevel
];
460 struct start_info si
[toplevel
];
463 for (i
= 0; i
< toplevel
; ++i
)
465 unsigned int child_starts
= starts
/ (toplevel
- i
);
466 unsigned int child_threads
= threads
/ (toplevel
- i
);
469 si
[i
].starts
= child_starts
;
470 si
[i
].threads
= child_threads
;
472 err
= pthread_create (&ths
[i
], &attr
, start_threads
, &si
[i
]);
474 error (EXIT_FAILURE
, err
, "cannot start thread");
476 starts
-= child_starts
;
477 threads
-= child_threads
;
480 for (i
= 0; i
< toplevel
; ++i
)
482 int err
= pthread_join (ths
[i
], NULL
);
485 error (EXIT_FAILURE
, err
, "cannot join thread");
490 write (STDERR_FILENO
, "\n", 1);
494 struct timespec end_time
;
496 if (clock_gettime (cl
, &end_time
) == 0)
498 end_time
.tv_sec
-= start_time
.tv_sec
;
499 end_time
.tv_nsec
-= start_time
.tv_nsec
;
500 if (end_time
.tv_nsec
< 0)
502 end_time
.tv_nsec
+= 1000000000;
506 printf ("\nRuntime: %lu.%09lu seconds\n",
507 (unsigned long int) end_time
.tv_sec
,
508 (unsigned long int) end_time
.tv_nsec
);
512 printf ("Result: %08x\n", sum
);
522 /* Handle program arguments. */
524 parse_opt (int key
, char *arg
, struct argp_state
*state
)
526 unsigned long int num
;
532 num
= strtoul (arg
, NULL
, 0);
533 if (num
<= MAX_THREADS
)
537 number of threads limited to %u; recompile with a higher limit if necessary",
542 num
= strtoul (arg
, NULL
, 0);
546 puts ("workload must be between 0 and 100 percent");
550 workcost
= strtoul (arg
, NULL
, 0);
554 rounds
= strtoul (arg
, NULL
, 0);
558 starts
= strtoul (arg
, NULL
, 0);
562 num
= strtoul (arg
, NULL
, 0);
563 if (num
>= PTHREAD_STACK_MIN
)
566 printf ("minimum stack size is %d\n", PTHREAD_STACK_MIN
);
570 snum
= strtol (arg
, NULL
, 0);
572 printf ("invalid guard size %s\n", arg
);
593 case OPT_SYNC_SIGNAL
:
594 sync_method
= sync_signal
;
598 sync_method
= sync_join
;
602 num
= strtoul (arg
, NULL
, 0);
603 if (num
< MAX_THREADS
)
607 number of threads limited to %u; recompile with a higher limit if necessary",
609 sync_method
= sync_join
;
613 return ARGP_ERR_UNKNOWN
;
623 /* We read the information from the /proc filesystem. It contains at
628 We search for this line and convert the number in an integer. */
629 static hp_timing_t result
;
632 /* If this function was called before, we know the result. */
636 fd
= open ("/proc/cpuinfo", O_RDONLY
);
637 if (__builtin_expect (fd
!= -1, 1))
639 /* XXX AFAIK the /proc filesystem can generate "files" only up
640 to a size of 4096 bytes. */
644 n
= read (fd
, buf
, sizeof buf
);
645 if (__builtin_expect (n
, 1) > 0)
647 char *mhz
= memmem (buf
, n
, "cpu MHz", 7);
649 if (__builtin_expect (mhz
!= NULL
, 1))
651 char *endp
= buf
+ n
;
652 int seen_decpoint
= 0;
655 /* Search for the beginning of the string. */
656 while (mhz
< endp
&& (*mhz
< '0' || *mhz
> '9') && *mhz
!= '\n')
659 while (mhz
< endp
&& *mhz
!= '\n')
661 if (*mhz
>= '0' && *mhz
<= '9')
664 result
+= *mhz
- '0';
668 else if (*mhz
== '.')
674 /* Compensate for missing digits at the end. */
675 while (ndigits
++ < 6)
688 clock_getcpuclockid (pid_t pid
, clockid_t
*clock_id
)
690 /* We don't allow any process ID but our own. */
691 if (pid
!= 0 && pid
!= getpid ())
694 #ifdef CLOCK_PROCESS_CPUTIME_ID
695 /* Store the number. */
696 *clock_id
= CLOCK_PROCESS_CPUTIME_ID
;
700 /* We don't have a timer for that. */
706 #define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
708 /* Get current value of CLOCK and store it in TP. */
710 clock_gettime (clockid_t clock_id
, struct timespec
*tp
)
716 case CLOCK_PROCESS_CPUTIME_ID
:
719 static hp_timing_t freq
;
722 /* Get the current counter. */
727 freq
= get_clockfreq ();
732 /* Compute the seconds. */
733 tp
->tv_sec
= tsc
/ freq
;
735 /* And the nanoseconds. This computation should be stable until
736 we get machines with about 16GHz frequency. */
737 tp
->tv_nsec
= ((tsc
% freq
) * UINT64_C (1000000000)) / freq
;