4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
46 #include <sys/types.h>
48 #include <linux/types.h>
53 #define LPDS sizeof(uint64_t)
54 int block_debug_setup(void *addr, int len, uint64_t off, uint64_t id)
58 memcpy(addr, (char *)&off, LPDS);
59 memcpy(addr + LPDS, (char *)&id, LPDS);
61 addr += len - LPDS - LPDS;
62 memcpy(addr, (char *)&off, LPDS);
63 memcpy(addr + LPDS, (char *)&id, LPDS);
68 int block_debug_check(char *who, void *addr, int size, uint64_t off, uint64_t id)
73 ne_off = le64toh(off);
75 if (memcmp(addr, (char *)&ne_off, LPDS)) {
76 fprintf(stderr, "%s: for offset %"PRIu64" off: %"PRIx64" != %"PRIx64"\n",
77 who, off, *(uint64_t *)addr, ne_off);
80 if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
81 fprintf(stderr, "%s: for offset %"PRIu64" id: %"PRIx64" != %"PRIx64"\n",
82 who, off, *(uint64_t *)(addr + LPDS), id);
86 addr += size - LPDS - LPDS;
87 if (memcmp(addr, (char *)&ne_off, LPDS)) {
88 fprintf(stderr, "%s: for offset %"PRIu64" end off: %"PRIx64" != %"PRIx64"\n",
89 who, off, *(uint64_t *)addr, ne_off);
92 if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
93 fprintf(stderr, "%s: for offset %"PRIu64" end id: %"PRIx64" != %"PRIx64"\n",
94 who, off, *(uint64_t *)(addr + LPDS), id);
102 void usage(char *prog)
105 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
110 int main(int argc, char **argv)
114 long long count, last;
116 uint64_t objid, offset;
123 if (argc < 3 || argc > 6)
126 count = strtoull(argv[2], &end, 0);
128 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
132 if (strchr(argv[3], 'r')) {
136 if (strchr(argv[3], 'w')) {
138 flags = O_RDWR | O_CREAT;
140 if (strchr(argv[3], 'd')) {
145 "%s: O_DIRECT not supported in this build\n",
154 flags = O_RDWR | O_CREAT;
158 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
164 pg_vec = strtoul(argv[4], &end, 0);
166 fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
175 objid = strtoull(argv[5], &end, 0);
177 fprintf(stderr, "%s: invalid objid '%s'\n",
185 printf("%s: %s on %s(objid %"PRIx64") for %llux%ld pages \n",
188 flags & O_DIRECT ? "directio" : "i/o",
192 argv[1], objid, count, pg_vec);
194 fd = open(argv[1], flags | O_LARGEFILE);
196 fprintf(stderr, "%s: cannot open %s: %s\n", argv[0],
197 argv[1], strerror(errno));
203 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
204 argv[1], strerror(errno));
208 len = pg_vec * st.st_blksize;
209 last = (long long)count * len;
211 buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
212 if (buf == MAP_FAILED) {
213 fprintf(stderr, "%s: no buffer memory %s\n",
214 argv[0], strerror(errno));
218 for (offset = 0; offset < last && cmd & WRITE; offset += len) {
221 for (i = 0; i < len; i += st.st_blksize)
222 block_debug_setup(buf + i, st.st_blksize,
225 rc = write(fd, buf, len);
227 for (i = 0; i < len; i += st.st_blksize) {
228 if (block_debug_check("write", buf + i, st.st_blksize,
234 fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
235 argv[0], strerror(errno), rc, len);
240 if (lseek(fd, 0, SEEK_SET) != 0) {
241 fprintf(stderr, "%s: cannot seek %s\n",
242 argv[0], strerror(errno));
246 for (offset = 0; offset < last && cmd & READ; offset += len) {
249 rc = read(fd, buf, len);
251 fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
252 argv[0], strerror(errno), rc, len);
256 for (i = 0; i < len; i += st.st_blksize) {
257 if (block_debug_check("read", buf + i, st.st_blksize,