1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017 Red Hat Inc. All Rights Reserved.
7 * Fork N children, each child writes to and reads from its own region of the
8 * same test file, and check if what it reads is what it writes. The test
9 * region is determined by N * blksz. Write and read operation can be either
17 #include <sys/types.h>
27 #define DEF_BLKSZ 4096
31 static void usage(const char *prog)
33 fprintf(stderr, "Usage: %s [-Fhptrwv] [-b blksz] [-n nr_child] [-i iterations] [-o offset] <-f filename>\n", prog);
34 fprintf(stderr, "\t-F\tPreallocate all blocks by writing them before test\n");
35 fprintf(stderr, "\t-p\tPreallocate all blocks using fallocate(2) before test\n");
36 fprintf(stderr, "\t-t\tTruncate test file to largest size before test\n");
37 fprintf(stderr, "\t-r\tDo direct read\n");
38 fprintf(stderr, "\t-w\tDo direct write\n");
39 fprintf(stderr, "\t-v\tBe verbose\n");
40 fprintf(stderr, "\t-h\tshow this help message\n");
44 static int cmpbuf(char *b1, char *b2, int bsize)
48 for (i = 0; i < bsize; i++) {
50 fprintf(stderr, "cmpbuf: offset %d: Expected: 0x%x,"
51 " got 0x%x\n", i, b1[i], b2[i]);
58 static void kill_children(pid_t *pids, int nr_child)
63 for (i = 0; i < nr_child; i++) {
71 static int wait_children(pid_t *pids, int nr_child)
73 int i, status, ret = 0;
76 for (i = 0; i < nr_child; i++) {
80 waitpid(pid, &status, 0);
81 ret += WEXITSTATUS(status);
86 static void dumpbuf(char *buf, int size, int blksz)
90 printf("dumping buffer content\n");
91 for (i = 0; i < size; i++) {
92 if (((i % blksz) == 0) || ((i % 64) == 0))
99 static int run_test(const char *filename, int n_child, int blksz, off_t offset,
100 int nr_iter, int flag_rd, int flag_wr)
109 seekoff = offset + blksz * n_child;
111 page_size = sysconf(_SC_PAGESIZE);
112 ret = posix_memalign((void **)&buf_rd, (size_t)page_size,
113 blksz > page_size ? blksz : (size_t)page_size);
115 fprintf(stderr, "posix_memalign(buf_rd, %d, %d) failed: %d\n",
119 memset(buf_rd, 0, blksz);
120 ret = posix_memalign((void **)&buf_wr, (size_t)page_size,
121 blksz > page_size ? blksz : (size_t)page_size);
123 fprintf(stderr, "posix_memalign(buf_wr, %d, %d) failed: %d\n",
127 memset(buf_wr, 0, blksz);
129 fd_rd = open(filename, flag_rd);
131 perror("open readonly for read");
135 fd_wr = open(filename, flag_wr);
137 perror("open writeonly for direct write");
141 #define log(format, ...) \
143 printf("[%d:%d] ", n_child, i); \
144 printf(format, __VA_ARGS__); \
148 /* seek, write, read and verify */
149 for (i = 0; i < nr_iter; i++) {
150 memset(buf_wr, i + 1, blksz);
151 log("pwrite(fd_wr, %p, %d, %lld)\n", buf_wr, blksz,
152 (long long) seekoff);
153 if (pwrite(fd_wr, buf_wr, blksz, seekoff) != blksz) {
154 perror("direct write");
158 /* make sure buffer write hits disk before direct read */
159 if (!(flag_wr & O_DIRECT)) {
160 if (fsync(fd_wr) < 0) {
161 perror("fsync(fd_wr)");
166 log("pread(fd_rd, %p, %d, %lld)\n", buf_rd, blksz,
167 (long long) seekoff);
168 if (pread(fd_rd, buf_rd, blksz, seekoff) != blksz) {
169 perror("buffer read");
172 if (cmpbuf(buf_wr, buf_rd, blksz) != 0) {
173 fprintf(stderr, "[%d:%d] FAIL - comparison failed, "
174 "offset %d\n", n_child, i, (int)seekoff);
176 dumpbuf(buf_rd, blksz, blksz);
183 int main(int argc, char *argv[])
187 int blksz = DEF_BLKSZ;
189 int flag_rd = O_RDONLY;
190 int flag_wr = O_WRONLY;
197 char *filename = NULL;
199 while ((i = getopt(argc, argv, "b:i:n:f:Fpo:tvrw")) != -1) {
202 if ((blksz = atoi(optarg)) <= 0) {
203 fprintf(stderr, "blksz must be > 0\n");
206 if (blksz % 512 != 0) {
207 fprintf(stderr, "blksz must be multiple of 512\n");
212 if ((nr_iter = atoi(optarg)) <= 0) {
213 fprintf(stderr, "iterations must be > 0\n");
218 if ((nr_child = atoi(optarg)) <= 0) {
219 fprintf(stderr, "no of children must be > 0\n");
242 if ((offset = atol(optarg)) < 0) {
243 fprintf(stderr, "offset must be >= 0\n");
250 case 'h': /* fall through */
256 if (filename == NULL)
258 if (pre_fill && pre_alloc) {
259 fprintf(stderr, "Error: -F and -p are both specified\n");
263 pids = malloc(nr_child * sizeof(pid_t));
265 fprintf(stderr, "failed to malloc memory for pids\n");
268 memset(pids, 0, nr_child * sizeof(pid_t));
270 /* create & truncate testfile first */
271 fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0600);
273 perror("create & truncate testfile");
277 if (do_trunc && (ftruncate(fd, blksz * nr_child) < 0)) {
278 perror("ftruncate failed");
284 buf = malloc(blksz * nr_child);
285 memset(buf, 's', blksz * nr_child);
286 write(fd, buf, blksz * nr_child);
290 fallocate(fd, 0, 0, blksz * nr_child);
296 for (i = 0; i < nr_child; i++) {
300 kill_children(pids, nr_child);
303 } else if (pid == 0) {
305 run_test(filename, i, blksz, offset, nr_iter,
312 ret = wait_children(pids, nr_child);