|
| 1 | +/* |
| 2 | + *test_fsync.c |
| 3 | + *tests if fsync can be done from another process than the original write |
| 4 | + */ |
| 5 | + |
| 6 | +#include"../../include/pg_config.h" |
| 7 | + |
| 8 | +#include<sys/types.h> |
| 9 | +#include<fcntl.h> |
| 10 | +#include<stdio.h> |
| 11 | +#include<stdlib.h> |
| 12 | +#include<time.h> |
| 13 | +#include<unistd.h> |
| 14 | + |
| 15 | +/* O_SYNC and O_FSYNC are the same */ |
| 16 | +#if defined(O_SYNC) |
| 17 | +#defineOPEN_SYNC_FLAGO_SYNC |
| 18 | +#elif defined(O_FSYNC) |
| 19 | +#defineOPEN_SYNC_FLAGO_FSYNC |
| 20 | +#endif |
| 21 | + |
| 22 | +#if defined(OPEN_SYNC_FLAG) |
| 23 | +#if defined(O_DSYNC)&& (O_DSYNC!=OPEN_SYNC_FLAG) |
| 24 | +#defineOPEN_DATASYNC_FLAGO_DSYNC |
| 25 | +#endif |
| 26 | +#endif |
| 27 | + |
| 28 | +voiddie(char*str); |
| 29 | +voidprint_elapse(structtimevalstart_t,structtimevalelapse_t); |
| 30 | + |
| 31 | +intmain(intargc,char*argv[]) |
| 32 | +{ |
| 33 | +structtimevalstart_t; |
| 34 | +structtimevalelapse_t; |
| 35 | +inttmpfile; |
| 36 | +char*strout="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 37 | + |
| 38 | +printf("Simple write timing:\n"); |
| 39 | +/* write only */ |
| 40 | +gettimeofday(&start_t,NULL); |
| 41 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT))==-1) |
| 42 | +die("can't open /var/tmp/test_fsync.out"); |
| 43 | +write(tmpfile,&strout,200); |
| 44 | +close(tmpfile); |
| 45 | +gettimeofday(&elapse_t,NULL); |
| 46 | +unlink("/var/tmp/test_fsync.out"); |
| 47 | +printf("write "); |
| 48 | +print_elapse(start_t,elapse_t); |
| 49 | +printf("\n\n"); |
| 50 | + |
| 51 | +printf("Compare fsync before and after write's close:\n"); |
| 52 | +/* write, fsync, close */ |
| 53 | +gettimeofday(&start_t,NULL); |
| 54 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT))==-1) |
| 55 | +die("can't open /var/tmp/test_fsync.out"); |
| 56 | +write(tmpfile,&strout,200); |
| 57 | +fsync(tmpfile); |
| 58 | +close(tmpfile); |
| 59 | +gettimeofday(&elapse_t,NULL); |
| 60 | +unlink("/var/tmp/test_fsync.out"); |
| 61 | +printf("write, fsync, close "); |
| 62 | +print_elapse(start_t,elapse_t); |
| 63 | +printf("\n"); |
| 64 | + |
| 65 | +/* write, close, fsync */ |
| 66 | +gettimeofday(&start_t,NULL); |
| 67 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT))==-1) |
| 68 | +die("can't open /var/tmp/test_fsync.out"); |
| 69 | +write(tmpfile,&strout,200); |
| 70 | +close(tmpfile); |
| 71 | +/* reopen file */ |
| 72 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT))==-1) |
| 73 | +die("can't open /var/tmp/test_fsync.out"); |
| 74 | +fsync(tmpfile); |
| 75 | +close(tmpfile); |
| 76 | +gettimeofday(&elapse_t,NULL); |
| 77 | +unlink("/var/tmp/test_fsync.out"); |
| 78 | +printf("write, close, fsync "); |
| 79 | +print_elapse(start_t,elapse_t); |
| 80 | +printf("\n"); |
| 81 | + |
| 82 | +printf("\nTest file sync methods\n"); |
| 83 | + |
| 84 | +#ifdefOPEN_DATASYNC_FLAG |
| 85 | +/* open_dsync, write */ |
| 86 | +gettimeofday(&start_t,NULL); |
| 87 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT |O_DSYNC))==-1) |
| 88 | +die("can't open /var/tmp/test_fsync.out"); |
| 89 | +write(tmpfile,&strout,200); |
| 90 | +close(tmpfile); |
| 91 | +gettimeofday(&elapse_t,NULL); |
| 92 | +unlink("/var/tmp/test_fsync.out"); |
| 93 | +printf("open o_dsync, write "); |
| 94 | +print_elapse(start_t,elapse_t); |
| 95 | +printf("\n"); |
| 96 | +#endif |
| 97 | + |
| 98 | +/* open_fsync, write */ |
| 99 | +gettimeofday(&start_t,NULL); |
| 100 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT |OPEN_SYNC_FLAG))==-1) |
| 101 | +die("can't open /var/tmp/test_fsync.out"); |
| 102 | +write(tmpfile,&strout,200); |
| 103 | +close(tmpfile); |
| 104 | +gettimeofday(&elapse_t,NULL); |
| 105 | +unlink("/var/tmp/test_fsync.out"); |
| 106 | +printf("open o_fsync, write "); |
| 107 | +print_elapse(start_t,elapse_t); |
| 108 | +printf("\n"); |
| 109 | + |
| 110 | +#ifdefHAVE_FDATASYNC |
| 111 | +/* write, fdatasync */ |
| 112 | +gettimeofday(&start_t,NULL); |
| 113 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT))==-1) |
| 114 | +die("can't open /var/tmp/test_fsync.out"); |
| 115 | +write(tmpfile,&strout,200); |
| 116 | +fdatasync(tmpfile); |
| 117 | +close(tmpfile); |
| 118 | +gettimeofday(&elapse_t,NULL); |
| 119 | +unlink("/var/tmp/test_fsync.out"); |
| 120 | +printf("write, fdatasync "); |
| 121 | +print_elapse(start_t,elapse_t); |
| 122 | +printf("\n"); |
| 123 | +#endif |
| 124 | + |
| 125 | +/* write, fsync, close */ |
| 126 | +gettimeofday(&start_t,NULL); |
| 127 | +if ((tmpfile=open("/var/tmp/test_fsync.out",O_RDWR |O_CREAT))==-1) |
| 128 | +die("can't open /var/tmp/test_fsync.out"); |
| 129 | +write(tmpfile,&strout,200); |
| 130 | +fsync(tmpfile); |
| 131 | +close(tmpfile); |
| 132 | +gettimeofday(&elapse_t,NULL); |
| 133 | +unlink("/var/tmp/test_fsync.out"); |
| 134 | +printf("write, fsync, "); |
| 135 | +print_elapse(start_t,elapse_t); |
| 136 | +printf("\n"); |
| 137 | + |
| 138 | +return0; |
| 139 | +} |
| 140 | + |
| 141 | +voidprint_elapse(structtimevalstart_t,structtimevalelapse_t) |
| 142 | +{ |
| 143 | +if (elapse_t.tv_usec<start_t.tv_usec) |
| 144 | +{ |
| 145 | +elapse_t.tv_sec--; |
| 146 | +elapse_t.tv_usec+=1000000; |
| 147 | +} |
| 148 | + |
| 149 | +printf("%ld.%06ld", (long) (elapse_t.tv_sec-start_t.tv_sec), |
| 150 | + (long) (elapse_t.tv_usec-start_t.tv_usec)); |
| 151 | +} |
| 152 | + |
| 153 | +voiddie(char*str) |
| 154 | +{ |
| 155 | +fprintf(stderr,"%s",str); |
| 156 | +exit(1); |
| 157 | +} |