Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / tests / test_brw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 /* for O_DIRECT */
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <sys/mman.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19
20 #include <liblustre.h>
21
22 #define READ  1
23 #define WRITE 2
24
25 #define LPDS sizeof(__u64)
26 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
27 {
28         off = cpu_to_le64(off);
29         id = cpu_to_le64(id);
30         memcpy(addr, (char *)&off, LPDS);
31         memcpy(addr + LPDS, (char *)&id, LPDS);
32
33         addr += len - LPDS - LPDS;
34         memcpy(addr, (char *)&off, LPDS);
35         memcpy(addr + LPDS, (char *)&id, LPDS);
36
37         return 0;
38 }
39
40 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
41 {
42         __u64 ne_off;
43         int err = 0;
44
45         ne_off = le64_to_cpu(off);
46         id = le64_to_cpu(id);
47         if (memcmp(addr, (char *)&ne_off, LPDS)) {
48                 CERROR("%s: for offset "LPU64" off: "LPX64" != "LPX64"\n",
49                        who, off, *(__u64 *)addr, ne_off);
50                 err = -EINVAL;
51         }
52         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
53                 CERROR("%s: for offset "LPU64" id: "LPX64" != "LPX64"\n",
54                        who, off, *(__u64 *)(addr + LPDS), id);
55                 err = -EINVAL;
56         }
57
58         addr += size - LPDS - LPDS;
59         if (memcmp(addr, (char *)&ne_off, LPDS)) {
60                 CERROR("%s: for offset "LPU64" end off: "LPX64" != "LPX64"\n",
61                        who, off, *(__u64 *)addr, ne_off);
62                 err = -EINVAL;
63         }
64         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
65                 CERROR("%s: for offset "LPU64" end id: "LPX64" != "LPX64"\n",
66                        who, off, *(__u64 *)(addr + LPDS), id);
67                 err = -EINVAL;
68         }
69
70         return err;
71 }
72 #undef LPDS
73
74 void usage(char *prog)
75 {
76         fprintf(stderr,
77                 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
78                 prog);
79         exit(1);
80 }
81
82 int main(int argc, char **argv)
83 {
84         int fd;
85         char *buf;
86         long long count, last, offset;
87         long pg_vec, len;
88         __u64 objid;
89         struct stat st;
90         int flags = 0;
91         int cmd = 0;
92         char *end;
93         int rc;
94
95         if (argc < 3 || argc > 6)
96                 usage(argv[0]);
97
98         count = strtoull(argv[2], &end, 0);
99         if (*end) {
100                 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
101                 usage(argv[0]);
102         }
103         if (argc >= 4) {
104                 if (strchr(argv[3], 'r')) {
105                         cmd = READ;
106                         flags = O_RDONLY;
107                 }
108                 if (strchr(argv[3], 'w')) {
109                         cmd |= WRITE;
110                         flags = O_RDWR | O_CREAT;
111                 }
112                 if (strchr(argv[3], 'd')) {
113 #ifdef O_DIRECT
114                         flags |= O_DIRECT;
115 #else
116                         fprintf(stderr,
117                                 "%s: O_DIRECT not supported in this build\n",
118                                 argv[0]);
119                         exit(1);
120 #endif
121                 }
122                 if (!cmd)
123                         usage(argv[0]);
124         } else {
125                 cmd = READ | WRITE;
126                 flags = O_RDWR | O_CREAT;
127 #ifdef O_DIRECT
128                 flags |= O_DIRECT;
129 #else
130                 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
131                         argv[0]);
132 #endif
133         }
134
135         if (argc >= 5) {
136                 pg_vec = strtoul(argv[4], &end, 0);
137                 if (*end) {
138                         fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
139                                 argv[0], argv[4]);
140                         usage(argv[0]);
141                 }
142         } else {
143                 pg_vec = 16;
144         }
145
146         if (argc >= 6) {
147                 objid = strtoull(argv[5], &end, 0);
148                 if (*end) {
149                         fprintf(stderr, "%s: invalid objid '%s'\n",
150                                 argv[0], argv[5]);
151                         usage(argv[0]);
152                 }
153         } else {
154                 objid = 3;
155         }
156
157         printf("%s: %s on %s(objid "LPX64") for %llux%ld pages \n",
158                argv[0],
159 #ifdef O_DIRECT
160                flags & O_DIRECT ? "directio" : "i/o",
161 #else
162                "i/o",
163 #endif
164                argv[1], objid, count, pg_vec);
165
166         fd = open(argv[1], flags | O_LARGEFILE);
167         if (fd == -1) {
168                 fprintf(stderr, "%s: cannot open %s:  %s\n", argv[0],
169                         argv[1], strerror(errno));
170                 return 3;
171         }
172
173         rc = fstat(fd, &st);
174         if (rc < 0) {
175                 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
176                         argv[1], strerror(errno));
177                 return 4;
178         }
179
180         len = pg_vec * st.st_blksize;
181         last = (long long)count * len;
182
183         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
184         if (buf == MAP_FAILED) {
185                 fprintf(stderr, "%s: no buffer memory %s\n",
186                         argv[0], strerror(errno));
187                 return 2;
188         }
189
190         for (offset = 0; offset < last && cmd & WRITE; offset += len) {
191                 int i;
192
193                 for (i = 0; i < len; i += st.st_blksize)
194                         block_debug_setup(buf + i, st.st_blksize, 
195                                           offset + i, objid);
196
197                 rc = write(fd, buf, len);
198
199                 for (i = 0; i < len; i += st.st_blksize) {
200                         if (block_debug_check("write", buf + i, st.st_blksize,
201                                               offset + i, objid))
202                                 return 10;
203                 }
204
205                 if (rc != len) {
206                         fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
207                                 argv[0], strerror(errno), rc, len);
208                         return 4;
209                 }
210         }
211
212         if (lseek(fd, 0, SEEK_SET) != 0) {
213                 fprintf(stderr, "%s: cannot seek %s\n",
214                         argv[0], strerror(errno));
215                 return 5;
216         }
217
218         for (offset = 0; offset < last && cmd & READ; offset += len) {
219                 int i;
220
221                 rc = read(fd, buf, len);
222                 if (rc != len) {
223                         fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
224                                 argv[0], strerror(errno), rc, len);
225                         return 6;
226                 }
227
228                 for (i = 0; i < len; i += st.st_blksize) {
229                         if (block_debug_check("read", buf + i, st.st_blksize,
230                                               offset + i, objid))
231                                 return 11;
232                 }
233         }
234
235         return 0;
236 }