allow for 32/64 bit builds
[tridge/junkcode.git] / fsync.c
1 #define _XOPEN_SOURCE 500
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 #include <fcntl.h>
6 #include <sys/file.h>
7 #include <sys/time.h>
8 #include <time.h>
9 #include <string.h>
10
11 static struct timeval tp1,tp2;
12
13 static void start_timer()
14 {
15         gettimeofday(&tp1,NULL);
16 }
17
18 static double end_timer()
19 {
20         gettimeofday(&tp2,NULL);
21         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
22                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
23 }
24
25 int main(int argc, const char *argv[])
26 {
27         int fd;
28         char *p;
29         size_t size = 4096;
30         const char *fname = "fsync.dat";
31
32         unlink(fname);
33         fd = open(fname, O_RDWR|O_CREAT|O_EXCL, 0600);
34         if (fd == -1) {
35                 perror(argv[1]);
36                 return -1;
37         }
38
39         while (size < 100*1024*1024) {
40                 int count = 0;
41                 ftruncate(fd, size);
42
43                 p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
44                 if (p == (void *)-1) {
45                         perror("mmap");
46                         return -1;
47                 }
48
49                 memset(p, 1, size);
50                 msync(p, size, MS_SYNC);
51                 fsync(fd);
52
53                 start_timer();
54                 while (end_timer() < 2) {
55 #if 0
56                         pwrite(fd, &count, 1, size-1);
57                         fsync(fd);
58 #else
59                         memset(p+size-100, count, 100);
60 //                      msync(p, size, MS_SYNC);
61                         msync(p+size-4096, 4096, MS_SYNC);
62 #endif
63                         count++;
64                 }
65
66                 printf("%7d  %.2f us  (count=%d)\n", 
67                        size, 1.0e6 * end_timer() / count, count);
68
69                 munmap(p, size);
70                 size *= 2;
71         }
72
73         close(fd);
74         unlink(fname);
75
76         return 0;
77 }