btrfs/220: fix how we tests for mount options
[xfstests-dev.git] / src / dio-interleaved.c
1 #ifndef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #endif
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13
14 static pthread_barrier_t barrier;
15
16 static unsigned long extent_size;
17 static unsigned long num_extents;
18
19 struct dio_thread_data {
20         int fd;
21         int thread_id;
22 };
23
24 static void *dio_thread(void *arg)
25 {
26         struct dio_thread_data *data = arg;
27         off_t off;
28         ssize_t ret;
29         void *buf;
30
31         if ((errno = posix_memalign(&buf, extent_size / 2, extent_size / 2))) {
32                 perror("malloc");
33                 return NULL;
34         }
35         memset(buf, 0, extent_size / 2);
36
37         off = (num_extents - 1) * extent_size;
38         if (data->thread_id)
39                 off += extent_size / 2;
40         while (off >= 0) {
41                 pthread_barrier_wait(&barrier);
42
43                 ret = pread(data->fd, buf, extent_size / 2, off);
44                 if (ret == -1)
45                         perror("pread");
46
47                 off -= extent_size;
48         }
49
50         free(buf);
51         return NULL;
52 }
53
54 int main(int argc, char **argv)
55 {
56         struct dio_thread_data data[2];
57         pthread_t thread;
58         int fd;
59
60         if (argc != 4) {
61                 fprintf(stderr, "usage: %s EXTENT_SIZE NUM_EXTENTS PATH\n",
62                         argv[0]);
63                 return EXIT_FAILURE;
64         }
65
66         extent_size = strtoul(argv[1], NULL, 0);
67         num_extents = strtoul(argv[2], NULL, 0);
68
69         errno = pthread_barrier_init(&barrier, NULL, 2);
70         if (errno) {
71                 perror("pthread_barrier_init");
72                 return EXIT_FAILURE;
73         }
74
75         fd = open(argv[3], O_RDONLY | O_DIRECT);
76         if (fd == -1) {
77                 perror("open");
78                 return EXIT_FAILURE;
79         }
80
81         data[0].fd = fd;
82         data[0].thread_id = 0;
83         errno = pthread_create(&thread, NULL, dio_thread, &data[0]);
84         if (errno) {
85                 perror("pthread_create");
86                 close(fd);
87                 return EXIT_FAILURE;
88         }
89
90         data[1].fd = fd;
91         data[1].thread_id = 1;
92         dio_thread(&data[1]);
93
94         pthread_join(thread, NULL);
95
96         close(fd);
97         return EXIT_SUCCESS;
98 }