Created
July 14, 2016 00:07
-
-
Save tdunning/6dfaa070bb9e8e55ed7efa6a70425e18 to your computer and use it in GitHub Desktop.
test of effect of flushing on speed of disk I/O on OSX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <time.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <sys/uio.h> | |
#include <sys/file.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <sys/time.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/resource.h> | |
#define FILE_SIZE (1024 * 1024 * 1024L) | |
#define CHUNK_SIZE (16 * 1024) | |
int simulate_journal_writes(int fd, char* desc, int use_fsync, int use_fdatasync) | |
{ | |
int rc; | |
int pos; | |
struct timeval start, end; | |
long mtime, seconds, useconds; | |
void *buff; | |
posix_memalign(&buff, CHUNK_SIZE, CHUNK_SIZE); | |
int rand_fd = open("/dev/urandom", O_RDONLY); | |
if(rand_fd == -1) | |
{ | |
printf("\nFailed to open random %i", errno); | |
exit(2); | |
} | |
rc = read(rand_fd, buff, CHUNK_SIZE); | |
if(rc != CHUNK_SIZE) | |
{ | |
printf("\nFailed to read random %i", errno); | |
exit(2); | |
} | |
close(rand_fd); | |
gettimeofday(&start, NULL); | |
for (pos=0; pos<FILE_SIZE; pos+=CHUNK_SIZE) | |
{ | |
int write_rc = write(fd, buff, CHUNK_SIZE); | |
if ( write_rc == -1 ) | |
{ | |
printf("\ncan't write %i", errno); | |
exit(2); | |
} | |
if(use_fsync) | |
{ | |
write_rc = fsync(fd); | |
if ( write_rc == -1 ) | |
{ | |
printf("\ncan't fsync %i", errno); | |
exit(2); | |
} | |
} | |
// if(use_fdatasync) | |
// { | |
// write_rc = fdatasync(fd); | |
// if ( write_rc == -1 ) | |
// { | |
// printf("\ncan't fdatasync %i", errno); | |
// exit(2); | |
// } | |
// } | |
} | |
gettimeofday(&end, NULL); | |
seconds = end.tv_sec - start.tv_sec; | |
useconds = end.tv_usec - start.tv_usec; | |
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; | |
printf("Elapsed time %s: %ld milliseconds\n", desc, mtime); | |
free(buff); | |
return 0; | |
} | |
int fake_fallocate(int fd, int length) { | |
fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, length}; | |
// Try to get a continous chunk of disk space | |
int ret = fcntl(fd, F_PREALLOCATE, &store); | |
if(-1 == ret){ | |
return 0; | |
} else { | |
return 0 == ftruncate(fd, length); | |
} | |
} | |
int main() | |
{ | |
int fd; | |
int rc; | |
char* filename = "journal.log"; | |
unlink(filename); | |
fd = open(filename,O_WRONLY | O_CREAT,S_IWUSR | S_IRUSR); | |
if(fd == -1) | |
{ | |
printf("Failed to open file %i", errno); | |
return 1; | |
} | |
rc = fake_fallocate(fd, FILE_SIZE); | |
if(rc == -1) | |
{ | |
printf("Failed to size file %i", errno); | |
return 1; | |
} | |
rc = close(fd); | |
if(rc == -1) | |
{ | |
printf("\nFailed to close file %i", errno); | |
return 1; | |
} | |
printf("\nStarting\n"); | |
fd = open(filename,O_WRONLY | O_CREAT , S_IWUSR | S_IRUSR); | |
simulate_journal_writes(fd, " buffered ", 0, 0 ); | |
close(fd); | |
// fd = open(filename,O_WRONLY | O_CREAT , S_IWUSR | S_IRUSR); | |
// simulate_journal_writes(fd, " buffered + fdatasync ", 0, 1); | |
// close(fd); | |
fd = open(filename,O_WRONLY | O_CREAT , S_IWUSR | S_IRUSR); | |
simulate_journal_writes(fd, " buffered + fsync ", 1, 0); | |
close(fd); | |
fd = open(filename,O_WRONLY | O_CREAT | O_DSYNC, S_IWUSR | S_IRUSR); | |
simulate_journal_writes(fd, " O_DSYNC ",0,0); | |
close(fd); | |
fd = open(filename,O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR); | |
fcntl(fd, F_NOCACHE, 1); | |
simulate_journal_writes(fd, " O_DIRECT ", 0, 0); | |
close(fd); | |
fd = open(filename,O_WRONLY | O_CREAT | O_DSYNC, S_IWUSR | S_IRUSR); | |
fcntl(fd, F_NOCACHE, 1); | |
simulate_journal_writes(fd, " O_DIRECT | O_DSYNC ", 0, 0); | |
close(fd); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment