Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement