#include #include #include #include #define NR_THREADS 1 #define ITERS 1000000 #define HEAPSIZE (4*1024) static void *old_thread(void *heap) { int i; for (i = 0; i < ITERS; i++) { char *mem = heap; if (mprotect(heap, HEAPSIZE, PROT_READ|PROT_WRITE) == -1) perror("mprotect"), exit(1); *mem = i; if (mmap(heap, HEAPSIZE, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == MAP_FAILED) perror("mmap"), exit(1); } return NULL; } static void old_test(void) { void *heap; pthread_t pt[NR_THREADS]; int i; heap = mmap(NULL, NR_THREADS*HEAPSIZE, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); if (heap == MAP_FAILED) perror("mmap"), exit(1); for (i = 0; i < NR_THREADS; i++) { if (pthread_create(&pt[i], NULL, old_thread, heap + i*HEAPSIZE) == -1) perror("pthread_create"), exit(1); } for (i = 0; i < NR_THREADS; i++) { if (pthread_join(pt[i], NULL) == -1) perror("pthread_join"), exit(1); } if (munmap(heap, NR_THREADS*HEAPSIZE) == -1) perror("munmap"), exit(1); } static void *new_thread(void *heap) { int i; for (i = 0; i < ITERS; i++) { char *mem = heap; *mem = i; if (madvise(heap, HEAPSIZE, MADV_DONTNEED) == -1) perror("madvise"), exit(1); } return NULL; } static void new_test(void) { void *heap; pthread_t pt[NR_THREADS]; int i; heap = mmap(NULL, HEAPSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); if (heap == MAP_FAILED) perror("mmap"), exit(1); for (i = 0; i < NR_THREADS; i++) { if (pthread_create(&pt[i], NULL, new_thread, heap + i*HEAPSIZE) == -1) perror("pthread_create"), exit(1); } for (i = 0; i < NR_THREADS; i++) { if (pthread_join(pt[i], NULL) == -1) perror("pthread_join"), exit(1); } if (munmap(heap, HEAPSIZE) == -1) perror("munmap"), exit(1); } int main(void) { old_test(); exit(0); }