Advertisement
xerpi

CASO - Bandwidth

Mar 14th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. /*
  2.  * Fet per Sergi Granell
  3.  * Compilar amb: gcc -Ofast -march=native bw.c -o bw
  4.  * Usage: ./bw [dest file] [size]
  5.  */
  6.  
  7. /*
  8.  * No es recomana fer servir gettimeofday()
  9.  * per mesurar temps: https://blog.habets.se/2010/09/gettimeofday-should-never-be-used-to-measure-time
  10.  * Es recomana utilitzar clock_gettime(CLOCK_MONOTONIC, ...);
  11.  * per mesurar intervals de temps en Linux i *BSD.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <inttypes.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/mman.h>
  24. #include <fcntl.h>
  25.  
  26. #define BLOCK_SIZE      (8 * 1024)
  27. #define DEFAULT_FILE_SIZE   (500 * 1024 * 1024)
  28. #define DEFAULT_FILE_NAME   "bwtest"
  29.  
  30. int main(int argc, const char *argv[])
  31. {
  32.     uint64_t diff;
  33.     uint64_t size;
  34.     uint64_t i;
  35.     struct timespec start, end;
  36.     double seconds;
  37.     const char *file;
  38.     int fd;
  39.     void *buffer;
  40.  
  41.     if (argc > 1)
  42.         file = argv[1];
  43.     else
  44.         file = DEFAULT_FILE_NAME;
  45.  
  46.     if (argc > 2)
  47.         size = atoi(argv[2]);
  48.     else
  49.         size = DEFAULT_FILE_SIZE;
  50.  
  51.     printf("Target file: %s\n", file);
  52.     printf("Target size: %dMB\n", size/(1024*1024));
  53.  
  54.     fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  55.     if (fd < 0) {
  56.         perror("Open error");
  57.         goto err_open;
  58.     }
  59.  
  60.     buffer = mmap(NULL, BLOCK_SIZE, PROT_READ | PROT_WRITE,
  61.         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  62.  
  63.     if (!buffer) {
  64.         printf("Could not mmap %d bytes of memory.\n",
  65.             BLOCK_SIZE);
  66.         goto err_mmap;
  67.     }
  68.  
  69.     /* Trigger page fault to have the pages available */
  70.     memset(buffer, 0, BLOCK_SIZE);
  71.  
  72.     clock_gettime(CLOCK_MONOTONIC, &start);
  73.  
  74.     for (i = 0; i < size; i += BLOCK_SIZE) {
  75.         write(fd, buffer, BLOCK_SIZE);
  76.     }
  77.  
  78.     /* Flush data to disc */
  79.     fsync(fd);
  80.  
  81.     clock_gettime(CLOCK_MONOTONIC, &end);
  82.  
  83.     close(fd);
  84.  
  85.     munmap(buffer, BLOCK_SIZE);
  86.  
  87.     diff = 1000000000L * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
  88.     seconds = diff/(double)1000000000L;
  89.  
  90.     printf("Elapsed time: %.9fs\n", seconds);
  91.     printf("Bandwidth: %fMB/s\n", (size/(1024*1024))/seconds);
  92.  
  93.     return 0;
  94.  
  95. err_mmap:
  96.     close(fd);
  97. err_open:
  98.     return -1;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement