Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Fet per Sergi Granell
- * Compilar amb: gcc -Ofast -march=native bw.c -o bw
- * Usage: ./bw [dest file] [size]
- */
- /*
- * No es recomana fer servir gettimeofday()
- * per mesurar temps: https://blog.habets.se/2010/09/gettimeofday-should-never-be-used-to-measure-time
- * Es recomana utilitzar clock_gettime(CLOCK_MONOTONIC, ...);
- * per mesurar intervals de temps en Linux i *BSD.
- */
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <inttypes.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #define BLOCK_SIZE (8 * 1024)
- #define DEFAULT_FILE_SIZE (500 * 1024 * 1024)
- #define DEFAULT_FILE_NAME "bwtest"
- int main(int argc, const char *argv[])
- {
- uint64_t diff;
- uint64_t size;
- uint64_t i;
- struct timespec start, end;
- double seconds;
- const char *file;
- int fd;
- void *buffer;
- if (argc > 1)
- file = argv[1];
- else
- file = DEFAULT_FILE_NAME;
- if (argc > 2)
- size = atoi(argv[2]);
- else
- size = DEFAULT_FILE_SIZE;
- printf("Target file: %s\n", file);
- printf("Target size: %dMB\n", size/(1024*1024));
- fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd < 0) {
- perror("Open error");
- goto err_open;
- }
- buffer = mmap(NULL, BLOCK_SIZE, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- if (!buffer) {
- printf("Could not mmap %d bytes of memory.\n",
- BLOCK_SIZE);
- goto err_mmap;
- }
- /* Trigger page fault to have the pages available */
- memset(buffer, 0, BLOCK_SIZE);
- clock_gettime(CLOCK_MONOTONIC, &start);
- for (i = 0; i < size; i += BLOCK_SIZE) {
- write(fd, buffer, BLOCK_SIZE);
- }
- /* Flush data to disc */
- fsync(fd);
- clock_gettime(CLOCK_MONOTONIC, &end);
- close(fd);
- munmap(buffer, BLOCK_SIZE);
- diff = 1000000000L * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
- seconds = diff/(double)1000000000L;
- printf("Elapsed time: %.9fs\n", seconds);
- printf("Bandwidth: %fMB/s\n", (size/(1024*1024))/seconds);
- return 0;
- err_mmap:
- close(fd);
- err_open:
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement