#include #include #include /* #define USE_SPARSE */ // #define INIT_WRITE // #define USE_OSYNC int main() { char zbuffer[8192]; int nbytes; int fd; char *tpath="xlog_testfile"; fd = open(tpath, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); if (fd < 0) exit(1); #ifdef INIT_WRITE #ifdef USE_SPARSE /* the sparse file makes things worse here */ lseek(fd, 16*1024*1024 - 1, SEEK_SET); write(fd, 0, 1); #else memset(zbuffer, '\0', sizeof(zbuffer)); for (nbytes = 0; nbytes < 16*1024*1024; nbytes += sizeof(zbuffer)) { write(fd, zbuffer, sizeof(zbuffer)); } #endif fsync(fd); #endif /* no fsync here, since close does it for us */ /* You think so? */ close (fd); #ifdef USE_OSYNC fd = open(tpath, O_RDWR | O_SYNC, S_IRUSR | S_IWUSR); #else #ifdef USE_ODSYNC fd = open(tpath, O_RDWR | O_DSYNC, S_IRUSR | S_IWUSR); #else fd = open(tpath, O_RDWR , S_IRUSR | S_IWUSR); #endif #endif if (fd < 0) exit(1); memset(zbuffer, 'X', sizeof(zbuffer)); nbytes = 0; do { #ifdef USE_LSEEK lseek(fd, nbytes, SEEK_SET); #endif write(fd, zbuffer, sizeof(zbuffer)); #ifndef USE_OSYNC #ifndef USE_ODSYNC fsync(fd); #endif #endif nbytes += sizeof(zbuffer); } while (nbytes < 16*1024*1024); close(fd); return 0; }