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