#include #include #include #include #include #include #include #include #include // Uncomment to use select inside the loop of each of the threads #define SLEEP_TYPE 1 // Uncomment to use poll inside the loop of each of the threads //#define SLEEP_TYPE 2 // Uncomment to use usleep inside the loop of each of the threads //#define SLEEP_TYPE 3 struct thread_info { int sleep_time; int num_iterations; int work_size; }; void *do_test(void *arg) { const struct thread_info *tinfo = (struct thread_info *)arg; const int sleep_time = tinfo->sleep_time; const int num_iterations = tinfo->num_iterations; const int work_size = tinfo->work_size; #if SLEEP_TYPE == 1 // Data for calling select struct timeval ts; #elif SLEEP_TYPE == 2 // Data for calling poll struct pollfd pfd; #endif // Data for doing work int *buf; int pseed; int inum, bnum; // Data for tracking the time struct timeval before, after; long long *diff; buf = calloc(work_size, sizeof(int)); diff = malloc(sizeof(unsigned long long)); #if SLEEP_TYPE == 2 // Initialize the poll data pfd.fd = 0; pfd.events = 0; #endif // Get the time before starting the processing gettimeofday(&before, NULL); // Do the requested number of iterations for (inum=0; inum) pseed = 1; for (bnum=0; bnum \n", argv[0]); return -1; } struct thread_info tinfo; int s, tnum, num_threads; pthread_attr_t attr; pthread_t *threads; long long *res; long long *times; // Get the parameters tinfo.sleep_time = atoi(argv[1]); tinfo.num_iterations = atoi(argv[2]); tinfo.work_size = atoi(argv[3]) * 1024; num_threads = atoi(argv[4]); // Initialize the thread creation attributes s = pthread_attr_init(&attr); if (s != 0) { printf("Error initializing thread attributes\n"); return -2; } // Allocate the memory to track the threads threads = calloc(num_threads, sizeof(pthread_t)); times = calloc(num_threads, sizeof(unsigned long long)); if (threads == NULL) { printf("Error allocating memory to track threads\n"); return -3; } // Start all of the threads for (tnum=0; tnum max_time) max_time = times[tnum]; avg_time += (times[tnum] - avg_time) / (float)(tnum + 1); } // Calculate the standard deviation of the time float stddev_time = 0; if (num_threads > 1) { for (tnum=0; tnum