#include #include #include #include #include #include "sim_pipe.h" #include "rw_threads.h" static int SendRemaining; static int RecvRemaining; static pthread_mutex_t Lock; static pthread_cond_t Wait; static int Go = 0; double MyTime() { struct timeval tv; gettimeofday(&tv,NULL); return((double)(tv.tv_sec)+((double)(tv.tv_usec)/1000000.0)); } int main(int argc, char *argv[]) { int nreaders; int nwriters; int buffsize; int totalsize; int quesize; int i; pthread_t *rthreads; pthread_t *wthreads; double start; double end; void *q; struct ThreadRec *t; int status; int id = 0; if(argc <= 5) { fprintf(stderr, "usage: testpipe nreaders nwriters buffsize quesize totalsize\n"); fflush(stderr); exit(1); } nreaders = atoi(argv[1]); nwriters = atoi(argv[2]); buffsize = atoi(argv[3]); quesize = atoi(argv[4]); totalsize = atoi(argv[5]); pthread_mutex_init(&Lock,NULL); pthread_cond_init(&Wait,NULL); SendRemaining = totalsize; RecvRemaining = totalsize; /* * make a que structure */ q = InitPipe(quesize); /* * get space for reader and writer thread ids */ rthreads = (pthread_t *)malloc(nreaders*sizeof(pthread_t)); if(rthreads == NULL) { exit(1); } wthreads = (pthread_t *)malloc(nwriters*sizeof(pthread_t)); if(wthreads == NULL) { exit(1); } /* * take the lock so none of the threads will run * immediately */ pthread_mutex_lock(&Lock); /* * spawn reader threads first */ id = 0; for(i=0; i < nreaders; i++) { t = (struct ThreadRec *)malloc(sizeof(struct ThreadRec)); if(t == NULL) { exit(1); } t->q = q; t->buffsize = buffsize; t->Lock = &Lock; t->Wait = &Wait; t->SendRemaining = &SendRemaining; t->RecvRemaining = &RecvRemaining; t->Go = &Go; t->me = id; id++; status = pthread_create(&(rthreads[i]), NULL, ReaderThread, (void *)t); if(status != 0) { exit(1); } } /* * now the writers */ for(i=0; i < nwriters; i++) { t = (struct ThreadRec *)malloc(sizeof(struct ThreadRec)); if(t == NULL) { exit(1); } t->q = q; t->buffsize = buffsize; t->Lock = &Lock; t->Wait = &Wait; t->SendRemaining = &SendRemaining; t->RecvRemaining = &RecvRemaining; t->Go = &Go; t->me = id; id++; status = pthread_create(&(wthreads[i]), NULL, WriterThread, (void *)t); if(status != 0) { exit(1); } } /* * start the timer */ start = MyTime(); /* * kick everybody off */ Go = 1; pthread_cond_broadcast(&Wait); pthread_mutex_unlock(&Lock); /* * join with each reader and writer */ for(i=0; i < nreaders; i++) { status = pthread_join(rthreads[i],NULL); if(status != 0) { exit(1); } } for(i=0; i < nwriters; i++) { status = pthread_join(wthreads[i],NULL); if(status != 0) { exit(1); } } /* * stop timer */ end = MyTime(); FreePipe(q); printf("bw: %3.4f MB/s for %d readers, %d writers, %d buffsize, %d quesize %d total size\n", (double)(totalsize)/((end - start)*1000000.0), nreaders, nwriters, buffsize, quesize, totalsize); exit(0); }