Guest User

Untitled

a guest
Feb 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <sys/time.h>
  8. #include <sys/mman.h>
  9. #include <sys/stat.h>
  10.  
  11. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  12.  
  13. int main(int argc, const char* argv[])
  14. {
  15. void *buf = NULL;
  16. void *in_file;
  17. void *out_file;
  18. struct timeval start, end;
  19. int input_fd, output_fd;
  20. struct stat st;
  21. uint64_t buf_size, file_size, remain_size, duration, offset, this_copy_size;
  22. char *ptr;
  23.  
  24. if (argc < 3)
  25. {
  26. puts("usage: <in_file> <out_file>");
  27. }
  28.  
  29. input_fd = open(argv[argc-2], O_RDONLY);
  30. output_fd = open(argv[argc-1], O_RDWR | O_CREAT | O_TRUNC, 0666);
  31. fstat(input_fd, &st);
  32. file_size = st.st_size;
  33.  
  34. printf("file_size: %lu\n", file_size);
  35.  
  36. in_file = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
  37.  
  38. gettimeofday(&start, NULL);
  39. buf = calloc(1, file_size);
  40. gettimeofday(&end, NULL);
  41. duration = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
  42. printf("calloc time: %lu(usec)\n", duration);
  43.  
  44. duration = 0;
  45. gettimeofday(&start, NULL);
  46. // read(input_fd, buf, file_size);
  47. memcpy(buf, in_file, file_size);
  48. gettimeofday(&end, NULL);
  49. duration += ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
  50. printf("read time: %lu(usec)\n", duration);
  51.  
  52. if (posix_fallocate(output_fd, 0, file_size))
  53. {
  54. fprintf(stderr, "Failed to create a new file\n");
  55. goto cleanup;
  56. }
  57.  
  58. out_file = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, output_fd, 0);
  59. gettimeofday(&start, NULL);
  60. // write(output_fd, buf, file_size);
  61. memcpy(out_file, buf, file_size);
  62. gettimeofday(&end, NULL);
  63. duration += ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
  64. printf("write time: %lu(usec)\n", duration);
  65.  
  66. gettimeofday(&start, NULL);
  67. munmap(in_file, file_size);
  68. munmap(out_file, file_size);
  69. gettimeofday(&end, NULL);
  70. duration = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
  71. printf("munmap time: %lu(usec)\n", duration);
  72.  
  73. cleanup:
  74. gettimeofday(&start, NULL);
  75. free(buf);
  76. gettimeofday(&end, NULL);
  77. duration = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
  78. printf("free time: %lu(usec)\n", duration);
  79.  
  80. gettimeofday(&start, NULL);
  81. close(input_fd);
  82. close(output_fd);
  83. gettimeofday(&end, NULL);
  84. duration = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
  85. printf("close time: %lu(usec)\n", duration);
  86. return 0;
  87. }
Add Comment
Please, Sign In to add comment