Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <assert.h>
  2. #include <fcntl.h>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/time.h>
  7. #include <unistd.h>
  8. #include <vector>
  9.  
  10. const char *filename = "Test-File";
  11. const int block_size = 4 * 1024;
  12. const int file_size = 4 * 1024 * 1024;
  13.  
  14. using namespace std;
  15.  
  16. int pipes[2];
  17. vector<char *> file_data;
  18.  
  19. static int NowUsecs() {
  20. struct timeval tv;
  21. const int err = gettimeofday(&tv, NULL);
  22. assert(err >= 0);
  23. return tv.tv_sec * 1000000LL + tv.tv_usec;
  24. }
  25.  
  26. void CreateData() {
  27. for (int xx = 0; xx < file_size / block_size; ++xx) {
  28. // The data buffer to fill.
  29. char *data = NULL;
  30. assert(posix_memalign(reinterpret_cast<void **>(&data), 4096, block_size) == 0);
  31. file_data.emplace_back(data);
  32. }
  33. }
  34.  
  35. int SpliceWrite(int fd, char *buf, int buf_len) {
  36. int len = buf_len;
  37. struct iovec iov;
  38. iov.iov_base = buf;
  39. iov.iov_len = len;
  40.  
  41. while (len) {
  42. int ret = vmsplice(pipes[1], &iov, 1, SPLICE_F_GIFT);
  43. assert(ret >= 0);
  44. if (!ret)
  45. break;
  46. len -= ret;
  47. if (len) {
  48. auto ptr = static_cast<char *>(iov.iov_base);
  49. ptr += ret;
  50. iov.iov_base = ptr;
  51. iov.iov_len -= ret;
  52. }
  53. }
  54.  
  55. len = buf_len;
  56. while (len) {
  57. int ret = splice(pipes[0], NULL, fd, NULL, len, SPLICE_F_MOVE);
  58. assert(ret >= 0);
  59. if (!ret)
  60. break;
  61.  
  62. len -= ret;
  63. }
  64.  
  65. return 1;
  66. }
  67.  
  68. int WriteToFile(const char *filename, bool use_splice) {
  69. // Open and write to the file.
  70. mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  71. int fd = open(filename, O_CREAT | O_RDWR, mode);
  72. assert(fd >= 0);
  73.  
  74. const int start = NowUsecs();
  75. for (int xx = 0; xx < file_size / block_size; ++xx) {
  76. if (use_splice) {
  77. SpliceWrite(fd, file_data[xx], block_size);
  78. } else {
  79. assert(write(fd, file_data[xx], block_size) == block_size);
  80. }
  81. }
  82. const int time = NowUsecs() - start;
  83.  
  84. // Close file.
  85. assert(close(fd) == 0);
  86.  
  87. return time;
  88. }
  89.  
  90. void ValidateData() {
  91. // Open and read from file.
  92. const int fd = open(filename, O_RDWR);
  93. assert(fd >= 0);
  94.  
  95. char *read_buf = (char *)malloc(block_size);
  96. for (int xx = 0; xx < file_size / block_size; ++xx) {
  97. assert(read(fd, read_buf, block_size) == block_size);
  98. assert(memcmp(read_buf, file_data[xx], block_size) == 0);
  99. }
  100.  
  101. // Close file.
  102. assert(close(fd) == 0);
  103. assert(unlink(filename) == 0);
  104. }
  105.  
  106. int main(int argc, char **argv) {
  107. auto res = pipe(pipes);
  108. assert(res == 0);
  109.  
  110. CreateData();
  111. const int without_splice = WriteToFile(filename, false /* use splice */);
  112. ValidateData();
  113. const int with_splice = WriteToFile(filename, true /* use splice */);
  114. ValidateData();
  115.  
  116. cout << "TIME WITH SPLICE: " << with_splice << endl;
  117. cout << "TIME WITHOUT SPLICE: " << without_splice << endl;
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement