#include #include #include #include #include #include #include #include #include #define nthreads 48 #define pagecount 16384 #define nrounds 1000 #define nchunks 20 #define PAGE_SIZE 4096 int fd; unsigned long v; void *threadf(void *ptr) { unsigned long *addr[nchunks]; int i, j, k; struct timespec ts = {0}; int ret; ts.tv_nsec = random() % 1024; for (j = 0; j < nrounds; j++) { for (i = 0; i < nchunks; i++) { addr[i] = mmap(NULL, PAGE_SIZE * pagecount, PROT_READ, MAP_SHARED, fd, i * PAGE_SIZE); if (addr[i] == MAP_FAILED) { fprintf(stderr, "mmap\n"); exit(1); } } nanosleep(&ts, NULL); for (i = 0; i < nchunks; i++) { v += *addr[i]; } nanosleep(&ts, NULL); for (i = 0; i < nchunks; i++) { munmap(addr[i], PAGE_SIZE * pagecount); } } } int main(int argc, char *argv[]) { pthread_t thr[nthreads]; int i; srandom(time(NULL)); if (argc < 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } fd = open(argv[1], O_RDONLY); if (fd < 0) { fprintf(stderr, "open\n"); exit(1); } for (i = 0; i < nthreads; i++) { if(pthread_create(&thr[i], NULL, threadf, NULL)) { fprintf(stderr, "pthread_create\n"); exit(1); } } for (i = 0; i < nthreads; i++) { if(pthread_join(thr[i], NULL)) { fprintf(stderr, "pthread_join\n"); exit(1); } } return 0; }