Home | History | Annotate | Download | only in src
      1 // Author: Suleiman Souhlal (suleiman (at) google.com)
      2 
      3 #include <stdio.h>
      4 #include <err.h>
      5 #include <stdint.h>
      6 #include <sys/types.h>
      7 #include <sys/stat.h>
      8 #include <sys/mman.h>
      9 #include <fcntl.h>
     10 
     11 #define O_NOATIME     01000000
     12 
     13 inline uint64_t
     14 rdtsc(void)
     15 {
     16 	int64_t tsc;
     17 
     18 	__asm __volatile("rdtsc" : "=A" (tsc));
     19 	return (tsc);
     20 }
     21 
     22 int
     23 main(int argc, char **argv)
     24 {
     25 	struct stat st;
     26 	uint64_t e, s, t;
     27 	char *p, q;
     28 	long i;
     29 	int fd;
     30 
     31 	if (argc < 2) {
     32 		printf("Usage: %s <file>\n", argv[0]);
     33 		return (1);
     34 	}
     35 
     36 	if ((fd = open(argv[1], O_RDWR | O_NOATIME)) < 0)
     37 		err(1, "open");
     38 
     39 	if (fstat(fd, &st) < 0)
     40 		err(1, "fstat");
     41 
     42 	p = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     43 
     44 	t = 0;
     45 	for (i = 0; i < 1000; i++) {
     46 		*p = 0;
     47 		msync(p, 4096, MS_SYNC);
     48 		s = rdtsc();
     49 		*p = 0;
     50 		__asm __volatile(""::: "memory");
     51 		e = rdtsc();
     52 		if (argc > 2)
     53 			printf("%d: %lld cycles %jd %jd\n", i, e - s, (intmax_t)s, (intmax_t)e);
     54 		t += e - s;
     55 	}
     56 
     57 	printf("average time: %lld cycles\n", t / 1000);
     58 
     59 	return (0);
     60 }
     61