1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
15 #include <sys/types.h>
18 #define CERROR(fmt, arg...) fprintf(stderr, fmt, ## arg)
20 #define __u64 long long
21 #define cpu_to_le64(v) (v)
22 #define le64_to_cpu(v) (v)
33 #define LPDS sizeof(__u64)
34 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
36 off = cpu_to_le64(off);
38 memcpy(addr, (char *)&off, LPDS);
39 memcpy(addr + LPDS, (char *)&id, LPDS);
41 addr += len - LPDS - LPDS;
42 memcpy(addr, (char *)&off, LPDS);
43 memcpy(addr + LPDS, (char *)&id, LPDS);
48 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
53 ne_off = le64_to_cpu(off);
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);
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);
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);
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);
82 void usage(char *prog)
85 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
90 int main(int argc, char **argv)
94 long long count, last, offset;
103 if (argc < 3 || argc > 6)
106 count = strtoull(argv[2], &end, 0);
108 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
112 if (strchr(argv[3], 'r')) {
116 if (strchr(argv[3], 'w')) {
118 flags = O_RDWR | O_CREAT;
120 if (strchr(argv[3], 'd')) {
127 flags = O_RDWR | O_CREAT | O_DIRECT;
131 pg_vec = strtoul(argv[4], &end, 0);
133 fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
142 objid = strtoull(argv[5], &end, 0);
144 fprintf(stderr, "%s: invalid objid '%s'\n",
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);
156 fd = open(argv[1], flags | O_LARGEFILE);
158 fprintf(stderr, "%s: cannot open %s: %s\n", argv[0],
159 argv[1], strerror(errno));
165 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
166 argv[1], strerror(errno));
170 len = pg_vec * st.st_blksize;
171 last = (long long)count * len;
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));
180 for (offset = 0; offset < last && cmd & WRITE; offset += len) {
183 for (i = 0; i < len; i += st.st_blksize)
184 block_debug_setup(buf + i, st.st_blksize,
187 rc = write(fd, buf, len);
189 for (i = 0; i < len; i += st.st_blksize) {
190 if (block_debug_check("write", buf + i, st.st_blksize,
196 fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
197 argv[0], strerror(errno), rc, len);
202 if (lseek(fd, 0, SEEK_SET) != 0) {
203 fprintf(stderr, "%s: cannot seek %s\n",
204 argv[0], strerror(errno));
208 for (offset = 0; offset < last && cmd & READ; offset += len) {
211 rc = read(fd, buf, len);
213 fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
214 argv[0], strerror(errno), rc, len);
218 for (i = 0; i < len; i += st.st_blksize) {
219 if (block_debug_check("read", buf + i, st.st_blksize,