#include #include #define __USE_GNU #include #include #include #include #include #define BS (2048*1024) #define BLOCKS (256) #define ALIGN(buf) (char *) (((unsigned long) (buf) + 4095) & ~(4095)) void print_time(struct timeval *s, int memory) { unsigned long ms, mb; struct timeval e; mb = BS * BLOCKS / 1024; gettimeofday(&e, NULL); ms = (e.tv_sec - s->tv_sec) * 1000 + (e.tv_usec - s->tv_usec) / 1000; if (memory) printf("Mem Throughput: %lu MiB/sec\n", mb / ms); else printf("Disk Throughput: %lu MiB/sec\n", mb / ms); } void read_stuff(int fd, char *buffer, int memory) { struct timeval s; int i, ret; gettimeofday(&s, NULL); for (i = 0; i < BLOCKS; i++) { if (memory) lseek(fd, 0, SEEK_SET); ret = read(fd, buffer, BS); if (!ret) break; else if (ret < 0) { perror("read infile"); break; } } print_time(&s, memory); } int main(int argc, char *argv[]) { char *buffer; int fd, seek; if (argc < 2) { printf("%s: \n", argv[0]); return 1; } if (argc == 3) seek = 1; else seek = 0; fd = open(argv[1], O_RDONLY); if (fd == -1) { perror("open"); return 2; } ioctl(fd, BLKFLSBUF, 0); buffer = ALIGN(malloc(BS + 4095)); if (seek) { read_stuff(fd, buffer, 1); read_stuff(fd, buffer, 1); } read_stuff(fd, buffer, 0); return 0; }