Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <assert.h>
  3. #include <fcntl.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9.  
  10. // compile: gcc ./tmp.c
  11. //
  12. // run: rm -f ./tmp && strace -Te fdatasync ./a.out <0|1>
  13. //
  14. // if the fdatasync time is higher when run with 0 vs with 1, sync_file_range
  15. // probably works on your filesystem.
  16.  
  17. int main(int argc, char** argv) {
  18. assert(argc == 2);
  19. bool use_sync_file_range = atoi(argv[1]) > 0;
  20. int fd = open("./tmp", O_CREAT | O_WRONLY);
  21. assert(fd != -1);
  22. char buf[1048576];
  23. for (int i = 0; i < 1024; ++i) {
  24. size_t rem = 1048576;
  25. while (rem > 0) {
  26. ssize_t ret = write(fd, &buf[1048576 - rem], rem);
  27. assert(ret > 0);
  28. rem -= (size_t)ret;
  29. }
  30. if (use_sync_file_range) {
  31. int ret = sync_file_range(fd, 0, 1048576 * (i + 1), SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
  32. assert(ret == 0);
  33. }
  34. }
  35. int ret = fdatasync(fd);
  36. assert(ret == 0);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement