/* * Copyright 2012, Steven Rostedt */ #include #include #include #include #include #include #include #include #include #include #include #define THREADS 400 //#define MEM (4096ULL*400) #define MEM (10ULL*4096*1024*1024/4) #define nano2sec(nan) (nan / 1000000000ULL) #define nano2ms(nan) (nan / 1000000ULL) #define nano2usec(nan) (nan / 1000ULL) #define usec2nano(sec) (sec * 1000ULL) #define ms2nano(ms) (ms * 1000000ULL) #define sec2nano(sec) (sec * 1000000000ULL) #define sec2usec(sec) (sec * 1000000ULL) static char *data; static pthread_barrier_t start_barrier; static pthread_barrier_t stop_barrier; static void perr(char *fmt, ...) { char buffer[BUFSIZ]; va_list ap; va_start(ap, fmt); vsnprintf(buffer, BUFSIZ, fmt, ap); va_end(ap); perror(buffer); fflush(stderr); exit(-1); } void *func(void *dat) { unsigned long id = (unsigned long)dat; static char x; unsigned long i; pthread_barrier_wait(&start_barrier); for (i = id * 4096; i < MEM; i += 4096 * THREADS) { x = data[i]; } pthread_barrier_wait(&stop_barrier); return NULL; } static unsigned long long get_time(void) { struct timeval tv; unsigned long long time; gettimeofday(&tv, NULL); time = sec2usec(tv.tv_sec); time += tv.tv_usec; return time; } void run_test(int threads) { pthread_t t[threads]; unsigned long long start, end; unsigned long i; for (i=0; i < threads; i++) { if (pthread_create(&t[i], NULL, func, (void *)i)) { perror("pthread_creat"); exit(-1); } } start = get_time(); pthread_barrier_wait(&start_barrier); pthread_barrier_wait(&stop_barrier); end = get_time(); printf("time = %lld usecs\n", end - start); for (i=0; i < threads; i++) { pthread_join(t[i], NULL); } } int main (int argc, char **argv) { int threads = THREADS; int ret; ret = pthread_barrier_init(&start_barrier, NULL, threads + 1); if (ret < 0) perr("pthread_barrier_init"); ret = pthread_barrier_init(&stop_barrier, NULL, threads + 1); if (ret < 0) perr("pthread_barrier_init"); printf("map=%lld\n", MEM); data = mmap(NULL, MEM, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (data == MAP_FAILED) perr("mmap"); run_test(threads); exit(0); return 0; }