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