use dprintf(1,..) instead of buffered stderr
[libc-test] / src / thread / bench.c
1 #define _XOPEN_SOURCE 700
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <pthread.h>
6 #include <unistd.h>
7 #include "test.h"
8
9 void *emptyfunc(void *dummy) {
10         return 0;
11 }
12
13 void bench_pthread_createjoin_serial1(int N) {
14         int i;
15         pthread_t td;
16         void *dummy;
17
18         for (i=0; i<N; i++) {
19                 pthread_create(&td, 0, emptyfunc, 0);
20                 pthread_join(td, &dummy);
21         }
22 }
23
24 void bench_pthread_createjoin_serial2(int N) {
25         int i, j;
26         pthread_t td[50];
27         void *dummy;
28
29         for (j=0; j<N/(sizeof td/sizeof *td); j++) {
30                 for (i=0; i<sizeof td/sizeof *td; i++)
31                         pthread_create(td+i, 0, emptyfunc, 0);
32                 for (i=0; i<sizeof td/sizeof *td; i++)
33                         pthread_join(td[i], &dummy);
34         }
35 }
36
37 void bench_pthread_create_serial1(int N) {
38         int i,j;
39         pthread_attr_t attr;
40         pthread_t td[50];
41         void *dummy;
42
43         pthread_attr_init(&attr);
44         pthread_attr_setstacksize(&attr, 16384);
45         for (j=0; j<N/(sizeof td/sizeof *td); j++) {
46                 for (i=0; i<sizeof td/sizeof *td; i++)
47                         pthread_create(td+i, &attr, emptyfunc, 0);
48                 stop_timer();
49                 for (i=0; i<sizeof td/sizeof *td; i++)
50                         pthread_join(td[i], &dummy);
51                 start_timer();
52         }
53 }
54
55 static int lockN;
56 void *lockunlock(void *mut)
57 {
58         int i;
59         for (i=0; i<lockN; i++) {
60                 pthread_mutex_lock(mut);
61                 pthread_mutex_unlock(mut);
62         }
63         return 0;
64 }
65
66 void bench_pthread_uselesslock(int N) {
67         pthread_t td;
68         pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
69         void *dummy;
70
71         lockN = N;
72         pthread_create(&td, 0, lockunlock, &mut);
73         pthread_join(td, &dummy);
74 }
75
76 void bench_pthread_createjoin_min1(int N) {
77         size_t i;
78         pthread_t td;
79         pthread_attr_t a;
80         void *dummy;
81
82         pthread_attr_init(&a);
83         pthread_attr_setstacksize(&a, sysconf(_SC_PAGESIZE));
84         pthread_attr_setguardsize(&a, 0);
85         for (i=0; i<N; i++) {
86                 pthread_create(&td, &a, emptyfunc, 0);
87                 pthread_join(td, &dummy);
88         }
89 }
90
91 void bench_pthread_createjoin_min2(int N) {
92         int i, j;
93         pthread_t td[50];
94         pthread_attr_t a;
95         void *dummy;
96
97         pthread_attr_init(&a);
98         pthread_attr_setstacksize(&a, sysconf(_SC_PAGESIZE));
99         pthread_attr_setguardsize(&a, 0);
100         for (j=0; j<N/(sizeof td/sizeof *td); j++) {
101                 for (i=0; i<sizeof td/sizeof *td; i++)
102                         pthread_create(td+i, &a, emptyfunc, 0);
103                 for (i=0; i<sizeof td/sizeof *td; i++)
104                         pthread_join(td[i], &dummy);
105         }
106 }