📄 fsx.c
字号:
exit(1); } for (i = 0, tf = test_files; i < num_test_files; i++, tf++) { tf->path = argv[i]; tf->fd = open(tf->path, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666); if (tf->fd < 0) { prterr(tf->path); exit(91); } } if (quiet || fd_policy == FD_SINGLE) return; for (i = 0, tf = test_files; i < num_test_files; i++, tf++) prt("fd %d: %s\n", i, tf->path);}voidclose_test_files(void){ int i; struct test_file *tf; for (i = 0, tf = test_files; i < num_test_files; i++, tf++) { if (close(tf->fd)) { prterr("close"); report_failure(99); } }}voidcheck_size(void){ struct stat statbuf; off_t size_by_seek; int fd = get_fd(); if (fstat(fd, &statbuf)) { prterr("check_size: fstat"); statbuf.st_size = -1; } size_by_seek = lseek(fd, (off_t)0, SEEK_END); if (file_size != statbuf.st_size || file_size != size_by_seek) { prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n", (unsigned long long)file_size, (unsigned long long)statbuf.st_size, (unsigned long long)size_by_seek); report_failure(120); }}voidcheck_trunc_hack(void){ struct stat statbuf; int fd = get_fd(); ftruncate(fd, (off_t)0); ftruncate(fd, (off_t)100000); if (fstat(fd, &statbuf)) { prterr("trunc_hack: fstat"); statbuf.st_size = -1; } if (statbuf.st_size != (off_t)100000) { prt("no extend on truncate! not posix!\n"); exit(130); } ftruncate(fd, 0);}static char *tf_buf = NULL;static int max_tf_len = 0;voidalloc_tf_buf(void){ char dummy = '\0'; int highest = num_test_files - 1; int len; len = snprintf(&dummy, 0, "%u ", highest); if (len < 1) { prterr("finding max tf_buf"); exit(1); } len++; tf_buf = malloc(len); if (tf_buf == NULL) { prterr("allocating tf_buf"); exit(1); } max_tf_len = snprintf(tf_buf, len, "%u ", highest); if (max_tf_len < 1) { prterr("fiding max_tv_len\n"); exit(1); } if (max_tf_len != len - 1) { warn("snprintf() gave %d instead of %d?\n", max_tf_len, len - 1); exit(1); }}char * fill_tf_buf(struct test_file *tf){ if (tf_buf == NULL) alloc_tf_buf(); sprintf(tf_buf,"%lu ", (unsigned long)(tf - test_files)); return tf_buf;}voidoutput_line(struct test_file *tf, int op, unsigned offset, unsigned size, struct timeval *tv){ char *tf_num = ""; char *ops[] = { [OP_READ] = "read", [OP_WRITE] = "write", [OP_TRUNCATE] = "trunc from", [OP_MAPREAD] = "mapread", [OP_MAPWRITE] = "mapwrite", }; if (fd_policy != FD_SINGLE) tf_num = fill_tf_buf(tf); /* W. */ if (!(!quiet && ((progressinterval && testcalls % progressinterval == 0) || (debug && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))))) return; prt("%06lu %lu.%06lu %.*s%-10s %#08x %s %#08x\t(0x%x bytes)\n", testcalls, tv->tv_sec, tv->tv_usec, max_tf_len, tf_num, ops[op], offset, op == OP_TRUNCATE ? " to " : "thru", offset + size - 1, size);}voiddoread(unsigned offset, unsigned size){ struct timeval t; off_t ret; unsigned iret; struct test_file *tf = get_tf(); int fd = tf->fd; offset -= offset % readbdy; gettimeofday(&t, NULL); if (size == 0) { if (!quiet && testcalls > simulatedopcount) prt("skipping zero size read\n"); log4(OP_SKIPPED, OP_READ, offset, size, &t); return; } if (size + offset > file_size) { if (!quiet && testcalls > simulatedopcount) prt("skipping seek/read past end of file\n"); log4(OP_SKIPPED, OP_READ, offset, size, &t); return; } log4(OP_READ, offset, size, 0, &t); if (testcalls <= simulatedopcount) return; output_line(tf, OP_READ, offset, size, &t); ret = lseek(fd, (off_t)offset, SEEK_SET); if (ret == (off_t)-1) { prterr("doread: lseek"); report_failure(140); } iret = read(fd, temp_buf, size); if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu read done\n", t.tv_sec, t.tv_usec); } if (iret != size) { if (iret == -1) prterr("doread: read"); else prt("short read: 0x%x bytes instead of 0x%x\n", iret, size); report_failure(141); } check_buffers(offset, size);}voiddomapread(unsigned offset, unsigned size){ struct timeval t; unsigned pg_offset; unsigned map_size; char *p; struct test_file *tf = get_tf(); int fd = tf->fd; offset -= offset % readbdy; gettimeofday(&t, NULL); if (size == 0) { if (!quiet && testcalls > simulatedopcount) prt("skipping zero size read\n"); log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t); return; } if (size + offset > file_size) { if (!quiet && testcalls > simulatedopcount) prt("skipping seek/read past end of file\n"); log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t); return; } log4(OP_MAPREAD, offset, size, 0, &t); if (testcalls <= simulatedopcount) return; output_line(tf, OP_MAPREAD, offset, size, &t); pg_offset = offset & page_mask; map_size = pg_offset + size; if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd, (off_t)(offset - pg_offset))) == MAP_FAILED) { prterr("domapread: mmap"); report_failure(190); } if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec); } memcpy(temp_buf, p + pg_offset, size); if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec); } if (munmap(p, map_size) != 0) { prterr("domapread: munmap"); report_failure(191); } if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec); } check_buffers(offset, size);}voidgendata(char *original_buf, char *good_buf, unsigned offset, unsigned size){ while (size--) { good_buf[offset] = testcalls % 256; if (offset % 2) good_buf[offset] += original_buf[offset]; offset++; }}voiddowrite(unsigned offset, unsigned size){ struct timeval t; off_t ret; unsigned iret; struct test_file *tf = get_tf(); int fd = tf->fd; offset -= offset % writebdy; gettimeofday(&t, NULL); if (size == 0) { if (!quiet && testcalls > simulatedopcount) prt("skipping zero size write\n"); log4(OP_SKIPPED, OP_WRITE, offset, size, &t); return; } log4(OP_WRITE, offset, size, file_size, &t); gendata(original_buf, good_buf, offset, size); if (file_size < offset + size) { if (file_size < offset) memset(good_buf + file_size, '\0', offset - file_size); file_size = offset + size; if (lite) { warn("Lite file size bug in fsx!"); report_failure(149); } } if (testcalls <= simulatedopcount) return; output_line(tf, OP_WRITE, offset, size, &t); ret = lseek(fd, (off_t)offset, SEEK_SET); if (ret == (off_t)-1) { prterr("dowrite: lseek"); report_failure(150); } iret = write(fd, good_buf + offset, size); if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu write done\n", t.tv_sec, t.tv_usec); } if (iret != size) { if (iret == -1) prterr("dowrite: write"); else prt("short write: 0x%x bytes instead of 0x%x\n", iret, size); report_failure(151); }}voiddomapwrite(unsigned offset, unsigned size){ struct timeval t; unsigned pg_offset; unsigned map_size; off_t cur_filesize; char *p; struct test_file *tf = get_tf(); int fd = tf->fd; offset -= offset % writebdy; gettimeofday(&t, NULL); if (size == 0) { if (!quiet && testcalls > simulatedopcount) prt("skipping zero size write\n"); log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t); return; } cur_filesize = file_size; log4(OP_MAPWRITE, offset, size, 0, &t); gendata(original_buf, good_buf, offset, size); if (file_size < offset + size) { if (file_size < offset) memset(good_buf + file_size, '\0', offset - file_size); file_size = offset + size; if (lite) { warn("Lite file size bug in fsx!"); report_failure(200); } } if (testcalls <= simulatedopcount) return; output_line(tf, OP_MAPWRITE, offset, size, &t); if (file_size > cur_filesize) { if (ftruncate(fd, file_size) == -1) { prterr("domapwrite: ftruncate"); exit(201); } if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec); } } pg_offset = offset & page_mask; map_size = pg_offset + size; if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED, fd, (off_t)(offset - pg_offset))) == MAP_FAILED) { prterr("domapwrite: mmap"); report_failure(202); } if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec); } memcpy(p + pg_offset, good_buf + offset, size); if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec); } if (msync(p, map_size, 0) != 0) { prterr("domapwrite: msync"); report_failure(203); } if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu msync done\n", t.tv_sec, t.tv_usec); } if (munmap(p, map_size) != 0) { prterr("domapwrite: munmap"); report_failure(204); } if (!quiet && (debug > 1 && (monitorstart == -1 || (offset + size > monitorstart && (monitorend == -1 || offset <= monitorend))))) { gettimeofday(&t, NULL); prt(" %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec); }}voiddotruncate(unsigned size){ struct timeval t; int oldsize = file_size; struct test_file *tf = get_tf(); int fd = tf->fd; size -= size % truncbdy; gettimeofday(&t, NULL); if (size > biggest) { biggest = size; if (!quiet && testcalls > simulatedopcount) prt("truncating to largest ever: 0x%x\n", size); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -