1 #include "buffer.h"
2 #include "scan.h"
3 #include <pthread.h>
4 #include <sys/types.h>
5 #include <sys/mman.h>
6 #include <unistd.h>
7 #include <signal.h>
8 #include <sys/time.h>
9 #include <sys/resource.h>
10 #include <stdlib.h>
11 
mythread(int * pipefd)12 void* mythread(int* pipefd) {
13 //  __libc_write(2,"thread\n",7);
14   write(pipefd[1],".",1);
15   sleep(60*5);
16   return 0;
17 }
18 
main(int argc,char * argv[])19 int main(int argc,char* argv[]) {
20   unsigned long count=1000;
21   struct timeval a,b;
22   unsigned long d;
23 
24 #ifdef RLIMIT_NPROC
25   {
26     struct rlimit rl;
27     rl.rlim_cur=RLIM_INFINITY; rl.rlim_max=RLIM_INFINITY;
28     setrlimit(RLIMIT_NPROC,&rl);
29   }
30 #endif
31 
32   for (;;) {
33     int i;
34     int c=getopt(argc,argv,"hc:");
35     if (c==-1) break;
36     switch (c) {
37     case 'c':
38       i=scan_ulong(optarg,&count);
39       if (i==0 || optarg[i]) {
40 	buffer_puts(buffer_2,"pthreadbench: warning: could not parse count: ");
41 	buffer_puts(buffer_2,optarg+i+1);
42 	buffer_putsflush(buffer_2,"\n");
43       }
44       break;
45     case 'h':
46       buffer_putsflush(buffer_2,
47 		  "usage: pthreadbench [-h] [-c count]\n"
48 		  "\n"
49 		  "\t-h\tprint this help\n"
50 		  "\t-c n\tfork off n children (default: 1000)\n");
51       return 0;
52     }
53   }
54 
55   {
56     unsigned long i;
57     int pfd[2];
58     char buf[100];
59     pthread_t *p=malloc(count*sizeof(pthread_t));
60     if (!p) {
61       buffer_puts(buffer_2,"out of memory!\n");
62       exit(1);
63     }
64     if (pipe(pfd)==-1) {
65       buffer_puts(buffer_2,"pipe failed: ");
66       buffer_puterror(buffer_2);
67       buffer_putnlflush(buffer_2);
68     }
69     for (i=0; i<count; ++i) {
70       int r;
71       gettimeofday(&a,0);
72       switch ((r=pthread_create(p+i,0,(void*(*)(void*))mythread,pfd))) {
73       case 0: /* ok */
74 	break;
75       default:
76 	buffer_puts(buffer_2,"could not create thread: ");
77 	buffer_puterror(buffer_2);
78 	buffer_putsflush(buffer_2,".\n");
79 	exit(1);
80       }
81       if (read(pfd[0],buf,1)!=1) {
82 	buffer_putsflush(buffer_2,"thread did not write into pipe?!\n");
83 	exit(1);
84       }
85       gettimeofday(&b,0);
86       d=(b.tv_sec-a.tv_sec)*1000000;
87       d=d+b.tv_usec-a.tv_usec;
88       buffer_putulong(buffer_1,d);
89       buffer_puts(buffer_1,"\n");
90     }
91     buffer_flush(buffer_1);
92   }
93 
94   return 0;
95 }
96