Created
April 12, 2019 17:20
-
-
Save ajkr/9054e26f76d5fddd40a0c0c76ec0aeda to your computer and use it in GitHub Desktop.
test sync_file_range
This file contains 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 <assert.h> | |
#include <fcntl.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
// compile: gcc ./tmp.c | |
// | |
// run: rm -f ./tmp && strace -Te fdatasync ./a.out <0|1> | |
// | |
// if the fdatasync time is higher when run with 0 vs with 1, sync_file_range | |
// probably works on your filesystem. | |
int main(int argc, char** argv) { | |
assert(argc == 2); | |
bool use_sync_file_range = atoi(argv[1]) > 0; | |
int fd = open("./tmp", O_CREAT | O_WRONLY); | |
assert(fd != -1); | |
char buf[1048576]; | |
for (int i = 0; i < 1024; ++i) { | |
size_t rem = 1048576; | |
while (rem > 0) { | |
ssize_t ret = write(fd, &buf[1048576 - rem], rem); | |
assert(ret > 0); | |
rem -= (size_t)ret; | |
} | |
if (use_sync_file_range) { | |
int ret = sync_file_range(fd, 0, 1048576 * (i + 1), SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); | |
assert(ret == 0); | |
} | |
} | |
int ret = fdatasync(fd); | |
assert(ret == 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment