1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
49 #include <sys/types.h>
52 #include <liblustre.h>
57 #define LPDS sizeof(__u64)
58 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
60 off = cpu_to_le64(off);
62 memcpy(addr, (char *)&off, LPDS);
63 memcpy(addr + LPDS, (char *)&id, LPDS);
65 addr += len - LPDS - LPDS;
66 memcpy(addr, (char *)&off, LPDS);
67 memcpy(addr + LPDS, (char *)&id, LPDS);
72 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
77 ne_off = le64_to_cpu(off);
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);
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);
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);
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);
106 void usage(char *prog)
109 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
114 int main(int argc, char **argv)
118 long long count, last, offset;
127 if (argc < 3 || argc > 6)
130 count = strtoull(argv[2], &end, 0);
132 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
136 if (strchr(argv[3], 'r')) {
140 if (strchr(argv[3], 'w')) {
142 flags = O_RDWR | O_CREAT;
144 if (strchr(argv[3], 'd')) {
149 "%s: O_DIRECT not supported in this build\n",
158 flags = O_RDWR | O_CREAT;
162 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
168 pg_vec = strtoul(argv[4], &end, 0);
170 fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
179 objid = strtoull(argv[5], &end, 0);
181 fprintf(stderr, "%s: invalid objid '%s'\n",
189 printf("%s: %s on %s(objid "LPX64") for %llux%ld pages \n",
192 flags & O_DIRECT ? "directio" : "i/o",
196 argv[1], objid, count, pg_vec);
198 fd = open(argv[1], flags | O_LARGEFILE);
200 fprintf(stderr, "%s: cannot open %s: %s\n", argv[0],
201 argv[1], strerror(errno));
207 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
208 argv[1], strerror(errno));
212 len = pg_vec * st.st_blksize;
213 last = (long long)count * len;
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));
222 for (offset = 0; offset < last && cmd & WRITE; offset += len) {
225 for (i = 0; i < len; i += st.st_blksize)
226 block_debug_setup(buf + i, st.st_blksize,
229 rc = write(fd, buf, len);
231 for (i = 0; i < len; i += st.st_blksize) {
232 if (block_debug_check("write", buf + i, st.st_blksize,
238 fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
239 argv[0], strerror(errno), rc, len);
244 if (lseek(fd, 0, SEEK_SET) != 0) {
245 fprintf(stderr, "%s: cannot seek %s\n",
246 argv[0], strerror(errno));
250 for (offset = 0; offset < last && cmd & READ; offset += len) {
253 rc = read(fd, buf, len);
255 fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
256 argv[0], strerror(errno), rc, len);
260 for (i = 0; i < len; i += st.st_blksize) {
261 if (block_debug_check("read", buf + i, st.st_blksize,