#include #include #include #include #include #include #define COUNT 1024 #define BUFSIZE (1024*1024) char *buf; void read_file(int fd) { int i; if (ftruncate(fd, BUFSIZE*COUNT) < 0) { perror("truncate"); exit(1); } for (i = 0; i < COUNT; i++) { if (read(fd, buf, BUFSIZE) != BUFSIZE) { perror("read"); exit(1); } } } int main(int argc, char **argv) { int fd; int fcount, i, stages; char fname[128]; struct timeval start, end; if (argc != 4) { fprintf(stderr, "Usage: trunc-bench \n"); return 1; } fcount = strtol(argv[2], NULL, 0); stages = strtol(argv[3], NULL, 0); buf = malloc(BUFSIZE); if (!buf) { fprintf(stderr, "Cannot allocate write buffer.\n"); return 1; } memset(buf, 'a', BUFSIZE); if (stages & 1) { fprintf(stderr, "Creating files...\n"); for (i = 0; i < fcount; i++ ) { sprintf(fname, "%s%d", argv[1], i); fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644); if (fd < 0) { perror("open"); return 1; } read_file(fd); close(fd); } } if (stages & 2) { fprintf(stderr, "Removing files...\n"); gettimeofday(&start, NULL); for (i = 0; i < fcount; i++ ) { sprintf(fname, "%s%d", argv[1], i); truncate(fname, 0); } gettimeofday(&end, NULL); printf("%lu us\n", (end.tv_sec - start.tv_sec) * 1000000UL + (end.tv_usec - start.tv_usec)); } fprintf(stderr, "Done.\n"); return 0; }