Don't like btrecord against libaio and librt, as it doesn't use any of their symbols
[blktrace.git] / blkrawverify.c
CommitLineData
f17c879d
AB
1/*
2 * block queue tracing application
3 *
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <errno.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include "blktrace.h"
30
31struct trace_info {
32 int bit_field;
33 char *string;
34};
35
86368eb5 36int data_is_native = -1;
017d1660 37
f17c879d
AB
38#define TRACE_TO_STRING(f) {.bit_field = f, .string = #f}
39static struct trace_info traces[] = {
40 TRACE_TO_STRING( BLK_TC_READ ),
41 TRACE_TO_STRING( BLK_TC_WRITE ),
42 TRACE_TO_STRING( BLK_TC_BARRIER ),
43 TRACE_TO_STRING( BLK_TC_SYNC ),
44 TRACE_TO_STRING( BLK_TC_QUEUE ),
45 TRACE_TO_STRING( BLK_TC_REQUEUE ),
46 TRACE_TO_STRING( BLK_TC_ISSUE ),
47 TRACE_TO_STRING( BLK_TC_COMPLETE ),
48 TRACE_TO_STRING( BLK_TC_FS ),
f86990ed
NS
49 TRACE_TO_STRING( BLK_TC_PC ),
50 TRACE_TO_STRING( BLK_TC_AHEAD ),
711e4d25 51 TRACE_TO_STRING( BLK_TC_META ),
f17c879d
AB
52};
53#define N_TRACES (sizeof(traces) / sizeof(struct trace_info))
54
55struct act_info {
56 __u32 val;
57 char *string;
58};
59
60#define ACT_TO_STRING(f) {.val = f, .string = #f}
61static struct act_info acts[] = {
62 ACT_TO_STRING( __BLK_TA_QUEUE ),
63 ACT_TO_STRING( __BLK_TA_QUEUE ),
64 ACT_TO_STRING( __BLK_TA_BACKMERGE ),
65 ACT_TO_STRING( __BLK_TA_FRONTMERGE ),
66 ACT_TO_STRING( __BLK_TA_GETRQ ),
67 ACT_TO_STRING( __BLK_TA_SLEEPRQ ),
68 ACT_TO_STRING( __BLK_TA_REQUEUE ),
69 ACT_TO_STRING( __BLK_TA_ISSUE ),
70 ACT_TO_STRING( __BLK_TA_COMPLETE ),
71 ACT_TO_STRING( __BLK_TA_PLUG ),
72 ACT_TO_STRING( __BLK_TA_UNPLUG_IO ),
73 ACT_TO_STRING( __BLK_TA_UNPLUG_TIMER ),
74 ACT_TO_STRING( __BLK_TA_INSERT ),
75 ACT_TO_STRING( __BLK_TA_SPLIT ),
76 ACT_TO_STRING( __BLK_TA_BOUNCE ),
77 ACT_TO_STRING( __BLK_TA_REMAP )
78};
79#define N_ACTS (sizeof(acts) / sizeof(struct act_info))
80
81static char *act_to_str(__u32 action)
82{
83 static char buf[1024];
5be4bdaf
JA
84 unsigned int i;
85 unsigned int act = action & 0xffff;
86 unsigned int trace = (action >> BLK_TC_SHIFT) & 0xffff;
f17c879d
AB
87
88 if (act <= N_ACTS) {
89 sprintf(buf, "%s ", acts[act].string);
90 for (i = 0; i < N_TRACES; i++)
91 if (trace & (1 << i)) {
92 char buf2[1024];
93 sprintf(buf2, "| %s ", traces[i].string);
94 strcat(buf, buf2);
95 }
96 }
97 else
98 sprintf(buf, "Invalid action=%08x", action);
99
100 return buf;
101}
102
103static void dump_trace(FILE *ofp, char *prefix, struct blk_io_trace *bit)
104{
105 fprintf(ofp, " Dump %s\n", prefix);
106 fprintf(ofp, " %8s: %08x\n", "magic", bit->magic);
107 fprintf(ofp, " %8s: %u\n", "sequence", bit->sequence);
c0cb7fab
JA
108 fprintf(ofp, " %8s: %llu\n", "time", (unsigned long long) bit->time);
109 fprintf(ofp, " %8s: %llu\n", "sector", (unsigned long long) bit->sector);
f17c879d
AB
110 fprintf(ofp, " %8s: %u\n", "bytes", bit->bytes);
111 fprintf(ofp, " %8s: %s\n", "action", act_to_str(bit->action));
112 fprintf(ofp, " %8s: %u\n", "bytes", bit->bytes);
113 fprintf(ofp, " %8s: %u\n", "cpu", bit->cpu);
114 fprintf(ofp, " %8s: %u\n", "error", bit->error);
115 fprintf(ofp, " %8s: %u\n", "pdu_len", bit->pdu_len);
116 fprintf(ofp, " %8s: (%u,%u)\n\n", "device", MAJOR(bit->device),
117 MINOR(bit->device));
118}
119
05831aca 120static int process(FILE **fp, char *devname, char *file, unsigned int cpu)
f17c879d
AB
121{
122# define SWAP_BITS() do { \
123 if (bit_save) { \
124 struct blk_io_trace *tmp = bit_save; \
125 bit_save = bit; \
126 bit = tmp; \
127 } \
128 else { \
129 bit_save = bit; \
130 bit = malloc(sizeof(struct blk_io_trace)); \
131 } \
132 } while (0)
133
134# define INC_BAD(str) do { \
135 nbad++; \
136 fprintf(ofp, " ----------------\n"); \
137 if (bit_save) dump_trace(ofp,"seq-1",bit_save); \
138 dump_trace(ofp, str, bit); \
139 SWAP_BITS(); \
140 } while (0)
141
142 size_t n;
05831aca 143 FILE *ifp, *ofp;
f17c879d
AB
144 __u32 save_device = 0, save_sequence = 0;
145 __u64 save_time = 0;
146 struct blk_io_trace *bit_save = NULL;
147 struct blk_io_trace *bit = malloc(sizeof(struct blk_io_trace));
148 unsigned int ngood = 0;
149 unsigned int nbad = 0;
150 unsigned int nbad_trace = 0, nbad_pdu = 0, nbad_cpu = 0;
151 unsigned int nbad_seq = 0, nbad_dev = 0, nbad_time = 0;
05831aca 152 char ofname[1024];
f17c879d
AB
153
154 ifp = fopen(file, "r");
05831aca
JA
155 if (!ifp)
156 return 0;
157
158 sprintf(ofname, "%s.verify.out", devname);
159
160 if (!*fp) {
161 *fp = fopen(ofname, "w");
162 if (*fp == NULL) {
163 fprintf(stderr,"Failed to open %s (%s), skipping\n",
164 ofname, strerror(errno));
165 fclose(ifp);
166 return 0;
167 }
168 fprintf(*fp, "\n---------------\n" );
169 fprintf(*fp, "Verifying %s\n", devname);
170 }
171
172 ofp = *fp;
f17c879d 173 while ((n = fread(bit, sizeof(struct blk_io_trace), 1, ifp)) == 1) {
1452478f
JA
174 if (ferror(ifp)) {
175 clearerr(ifp);
176 perror("fread");
177 break;
178 }
4ff24a34
JA
179 if (data_is_native == -1)
180 check_data_endianness(bit->magic);
181
f17c879d 182 trace_to_cpu(bit);
4ba99bfc
JA
183
184 if (!CHECK_MAGIC(bit)) {
f17c879d
AB
185 INC_BAD("bad trace");
186 continue;
187 }
188
4ba99bfc
JA
189 if ((bit->magic & 0xff) != SUPPORTED_VERSION) {
190 fprintf(stderr, "unsupported trace version\n");
191 break;
192 }
193
f17c879d
AB
194 if (bit->pdu_len) {
195 char *pdu_buf;
196
197 pdu_buf = malloc(bit->pdu_len);
198 n = fread(pdu_buf, bit->pdu_len, 1, ifp);
5be4bdaf 199 if (n == 0) {
f17c879d
AB
200 INC_BAD("bad pdu");
201 nbad_seq++;
202 break;
203 }
204 free(pdu_buf);
205 }
206
207 if (bit->cpu != cpu) {
208 INC_BAD("bad cpu");
209 nbad_cpu++;
210 continue;
211 }
212
bfc70ad5
JA
213 /*
214 * skip notify traces, they don't have valid sequences
215 */
216 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY))
217 continue;
218
f17c879d
AB
219 if (ngood) {
220 if (bit->sequence <= save_sequence) {
221 INC_BAD("bad seq");
222 nbad_seq++;
223 continue;
224 }
225 else if (bit->time <= save_time) {
226 INC_BAD("time regression");
227 nbad_time++;
228 continue;
229 }
230 else if (bit->device != save_device) {
231 INC_BAD("bad dev");
232 nbad_dev++;
233 continue;
234 }
235 }
236
237 save_sequence = bit->sequence;
238 save_time = bit->time;
239 save_device = bit->device;
240
241 ngood++;
242 SWAP_BITS();
243 }
244
05e2c559 245 if (n == 0 && !feof(ifp))
f17c879d
AB
246 fprintf(stderr,"%s: fread failed %d/%s\n",
247 file, errno, strerror(errno));
248 fclose(ifp);
249
250 fprintf(ofp, " ---------------------\n");
251 fprintf(ofp, " Summary for cpu %d:\n", cpu);
252 fprintf(ofp, " %10d valid + %10d invalid (%5.1f%%) processed\n\n",
253 ngood, nbad,
254 ngood ? 100.0 * (float)ngood / (float)(ngood + nbad) : 0.0);
255
256 if (nbad) {
257 if (nbad_trace)
258 fprintf(ofp, "%8s %d traces\n", "", nbad_trace);
259 if (nbad_trace)
260 fprintf(ofp, "%8s %d pdu\n", "", nbad_pdu);
261 if (nbad_cpu)
262 fprintf(ofp, "%8s %d cpu\n", "", nbad_cpu);
263 if (nbad_seq)
264 fprintf(ofp, "%8s %d seq\n", "", nbad_seq);
265 if (nbad_dev)
266 fprintf(ofp, "%8s %d dev\n", "", nbad_dev);
267 if (nbad_time)
268 fprintf(ofp, "%8s %d time\n", "", nbad_time);
269 fprintf(ofp,"\n");
270 }
271
272 return nbad;
273}
274
275int main(int argc, char *argv[])
276{
277 char *devname;
278 struct stat st;
2b9df5a1 279 int i, cpu, nbad, rval = 0;
f17c879d
AB
280 FILE *ofp;
281 char *ofname = malloc(1024);
282 char *fname = malloc(1024);
283
284 if (argc < 2) {
285 fprintf(stderr,"FATAL: Need device name(s)\n");
286 fprintf(stderr,"Usage: blkrawverify <dev> [<dev>...]\n");
287 exit(1);
288 }
289
290 for (i = 1; i < argc; i++) {
291 devname = argv[i];
292 sprintf(ofname, "%s.verify.out", devname);
05831aca 293 ofp = NULL;
f17c879d 294
f17c879d
AB
295 printf("Verifying %s\n", devname); fflush(stdout);
296 for (cpu = 0; ; cpu++) {
297 sprintf(fname, "%s.blktrace.%d", devname, cpu);
298 if (stat(fname, &st) < 0)
299 break;
300 printf(" CPU %d ", cpu); fflush(stdout);
05831aca 301 nbad = process(&ofp, devname, fname, cpu);
2b9df5a1 302 if (nbad) {
f17c879d 303 printf("-- %d bad", nbad);
2b9df5a1
AB
304 rval = 1;
305 }
f17c879d
AB
306 printf("\n");
307 }
05831aca
JA
308 if (ofp) {
309 fclose(ofp);
310 fprintf(stdout, "Wrote output to %s\n", ofname);
311 }
f17c879d
AB
312 }
313
2b9df5a1 314 return rval;
f17c879d 315}