Whamcloud - gitweb
b=16488
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 /* for O_DIRECT */
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <sys/mman.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 #include <liblustre.h>
53
54 #define READ  1
55 #define WRITE 2
56
57 #define LPDS sizeof(__u64)
58 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
59 {
60         off = cpu_to_le64(off);
61         id = cpu_to_le64(id);
62         memcpy(addr, (char *)&off, LPDS);
63         memcpy(addr + LPDS, (char *)&id, LPDS);
64
65         addr += len - LPDS - LPDS;
66         memcpy(addr, (char *)&off, LPDS);
67         memcpy(addr + LPDS, (char *)&id, LPDS);
68
69         return 0;
70 }
71
72 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
73 {
74         __u64 ne_off;
75         int err = 0;
76
77         ne_off = le64_to_cpu(off);
78         id = le64_to_cpu(id);
79         if (memcmp(addr, (char *)&ne_off, LPDS)) {
80                 CERROR("%s: for offset "LPU64" off: "LPX64" != "LPX64"\n",
81                        who, off, *(__u64 *)addr, ne_off);
82                 err = -EINVAL;
83         }
84         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
85                 CERROR("%s: for offset "LPU64" id: "LPX64" != "LPX64"\n",
86                        who, off, *(__u64 *)(addr + LPDS), id);
87                 err = -EINVAL;
88         }
89
90         addr += size - LPDS - LPDS;
91         if (memcmp(addr, (char *)&ne_off, LPDS)) {
92                 CERROR("%s: for offset "LPU64" end off: "LPX64" != "LPX64"\n",
93                        who, off, *(__u64 *)addr, ne_off);
94                 err = -EINVAL;
95         }
96         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
97                 CERROR("%s: for offset "LPU64" end id: "LPX64" != "LPX64"\n",
98                        who, off, *(__u64 *)(addr + LPDS), id);
99                 err = -EINVAL;
100         }
101
102         return err;
103 }
104 #undef LPDS
105
106 void usage(char *prog)
107 {
108         fprintf(stderr,
109                 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
110                 prog);
111         exit(1);
112 }
113
114 int main(int argc, char **argv)
115 {
116         int fd;
117         char *buf;
118         long long count, last, offset;
119         long pg_vec, len;
120         __u64 objid;
121         struct stat st;
122         int flags = 0;
123         int cmd = 0;
124         char *end;
125         int rc;
126
127         if (argc < 3 || argc > 6)
128                 usage(argv[0]);
129
130         count = strtoull(argv[2], &end, 0);
131         if (*end) {
132                 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
133                 usage(argv[0]);
134         }
135         if (argc >= 4) {
136                 if (strchr(argv[3], 'r')) {
137                         cmd = READ;
138                         flags = O_RDONLY;
139                 }
140                 if (strchr(argv[3], 'w')) {
141                         cmd |= WRITE;
142                         flags = O_RDWR | O_CREAT;
143                 }
144                 if (strchr(argv[3], 'd')) {
145 #ifdef O_DIRECT
146                         flags |= O_DIRECT;
147 #else
148                         fprintf(stderr,
149                                 "%s: O_DIRECT not supported in this build\n",
150                                 argv[0]);
151                         exit(1);
152 #endif
153                 }
154                 if (!cmd)
155                         usage(argv[0]);
156         } else {
157                 cmd = READ | WRITE;
158                 flags = O_RDWR | O_CREAT;
159 #ifdef O_DIRECT
160                 flags |= O_DIRECT;
161 #else
162                 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
163                         argv[0]);
164 #endif
165         }
166
167         if (argc >= 5) {
168                 pg_vec = strtoul(argv[4], &end, 0);
169                 if (*end) {
170                         fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
171                                 argv[0], argv[4]);
172                         usage(argv[0]);
173                 }
174         } else {
175                 pg_vec = 16;
176         }
177
178         if (argc >= 6) {
179                 objid = strtoull(argv[5], &end, 0);
180                 if (*end) {
181                         fprintf(stderr, "%s: invalid objid '%s'\n",
182                                 argv[0], argv[5]);
183                         usage(argv[0]);
184                 }
185         } else {
186                 objid = 3;
187         }
188
189         printf("%s: %s on %s(objid "LPX64") for %llux%ld pages \n",
190                argv[0],
191 #ifdef O_DIRECT
192                flags & O_DIRECT ? "directio" : "i/o",
193 #else
194                "i/o",
195 #endif
196                argv[1], objid, count, pg_vec);
197
198         fd = open(argv[1], flags | O_LARGEFILE);
199         if (fd == -1) {
200                 fprintf(stderr, "%s: cannot open %s:  %s\n", argv[0],
201                         argv[1], strerror(errno));
202                 return 3;
203         }
204
205         rc = fstat(fd, &st);
206         if (rc < 0) {
207                 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
208                         argv[1], strerror(errno));
209                 return 4;
210         }
211
212         len = pg_vec * st.st_blksize;
213         last = (long long)count * len;
214
215         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
216         if (buf == MAP_FAILED) {
217                 fprintf(stderr, "%s: no buffer memory %s\n",
218                         argv[0], strerror(errno));
219                 return 2;
220         }
221
222         for (offset = 0; offset < last && cmd & WRITE; offset += len) {
223                 int i;
224
225                 for (i = 0; i < len; i += st.st_blksize)
226                         block_debug_setup(buf + i, st.st_blksize, 
227                                           offset + i, objid);
228
229                 rc = write(fd, buf, len);
230
231                 for (i = 0; i < len; i += st.st_blksize) {
232                         if (block_debug_check("write", buf + i, st.st_blksize,
233                                               offset + i, objid))
234                                 return 10;
235                 }
236
237                 if (rc != len) {
238                         fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
239                                 argv[0], strerror(errno), rc, len);
240                         return 4;
241                 }
242         }
243
244         if (lseek(fd, 0, SEEK_SET) != 0) {
245                 fprintf(stderr, "%s: cannot seek %s\n",
246                         argv[0], strerror(errno));
247                 return 5;
248         }
249
250         for (offset = 0; offset < last && cmd & READ; offset += len) {
251                 int i;
252
253                 rc = read(fd, buf, len);
254                 if (rc != len) {
255                         fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
256                                 argv[0], strerror(errno), rc, len);
257                         return 6;
258                 }
259
260                 for (i = 0; i < len; i += st.st_blksize) {
261                         if (block_debug_check("read", buf + i, st.st_blksize,
262                                               offset + i, objid))
263                                 return 11;
264                 }
265         }
266
267         return 0;
268 }