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
31 struct trace_info {
32 int bit_field;
33 char *string;
34 };
35
36 int data_is_native = -1;
37
38 #define TRACE_TO_STRING(f) {.bit_field = f, .string = #f}
39 static struct trace_info traces[] = {
40 TRACE_TO_STRING( BLK_TC_READ ),
41 TRACE_TO_STRING( BLK_TC_WRITE ),
42 TRACE_TO_STRING( BLK_TC_FLUSH ),
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 ),
49 TRACE_TO_STRING( BLK_TC_PC ),
50 TRACE_TO_STRING( BLK_TC_AHEAD ),
51 TRACE_TO_STRING( BLK_TC_META ),
52 TRACE_TO_STRING( BLK_TC_DISCARD ),
53 TRACE_TO_STRING( BLK_TC_FUA ),
54 };
55 #define N_TRACES (sizeof(traces) / sizeof(struct trace_info))
56
57 struct act_info {
58 __u32 val;
59 char *string;
60 };
61
62 #define ACT_TO_STRING(f) {.val = f, .string = #f}
63 static struct act_info acts[] = {
64 ACT_TO_STRING( __BLK_TA_QUEUE ),
65 ACT_TO_STRING( __BLK_TA_QUEUE ),
66 ACT_TO_STRING( __BLK_TA_BACKMERGE ),
67 ACT_TO_STRING( __BLK_TA_FRONTMERGE ),
68 ACT_TO_STRING( __BLK_TA_GETRQ ),
69 ACT_TO_STRING( __BLK_TA_SLEEPRQ ),
70 ACT_TO_STRING( __BLK_TA_REQUEUE ),
71 ACT_TO_STRING( __BLK_TA_ISSUE ),
72 ACT_TO_STRING( __BLK_TA_COMPLETE ),
73 ACT_TO_STRING( __BLK_TA_PLUG ),
74 ACT_TO_STRING( __BLK_TA_UNPLUG_IO ),
75 ACT_TO_STRING( __BLK_TA_UNPLUG_TIMER ),
76 ACT_TO_STRING( __BLK_TA_INSERT ),
77 ACT_TO_STRING( __BLK_TA_SPLIT ),
78 ACT_TO_STRING( __BLK_TA_BOUNCE ),
79 ACT_TO_STRING( __BLK_TA_REMAP )
80 };
81 #define N_ACTS (sizeof(acts) / sizeof(struct act_info))
82
act_to_str(__u32 action)83 static char *act_to_str(__u32 action)
84 {
85 static char buf[1024];
86 unsigned int i;
87 unsigned int act = action & 0xffff;
88 unsigned int trace = (action >> BLK_TC_SHIFT) & 0xffff;
89
90 if (act < N_ACTS) {
91 sprintf(buf, "%s ", acts[act].string);
92 for (i = 0; i < N_TRACES; i++)
93 if (trace & (1 << i)) {
94 char buf2[1024];
95 sprintf(buf2, "| %s ", traces[i].string);
96 strcat(buf, buf2);
97 }
98 }
99 else
100 sprintf(buf, "Invalid action=%08x", action);
101
102 return buf;
103 }
104
dump_trace(FILE * ofp,char * prefix,struct blk_io_trace * bit)105 static void dump_trace(FILE *ofp, char *prefix, struct blk_io_trace *bit)
106 {
107 fprintf(ofp, " Dump %s\n", prefix);
108 fprintf(ofp, " %8s: %08x\n", "magic", bit->magic);
109 fprintf(ofp, " %8s: %u\n", "sequence", bit->sequence);
110 fprintf(ofp, " %8s: %llu\n", "time", (unsigned long long) bit->time);
111 fprintf(ofp, " %8s: %llu\n", "sector", (unsigned long long) bit->sector);
112 fprintf(ofp, " %8s: %u\n", "bytes", bit->bytes);
113 fprintf(ofp, " %8s: %s\n", "action", act_to_str(bit->action));
114 fprintf(ofp, " %8s: %u\n", "bytes", bit->bytes);
115 fprintf(ofp, " %8s: %u\n", "cpu", bit->cpu);
116 fprintf(ofp, " %8s: %u\n", "error", bit->error);
117 fprintf(ofp, " %8s: %u\n", "pdu_len", bit->pdu_len);
118 fprintf(ofp, " %8s: (%u,%u)\n\n", "device", MAJOR(bit->device),
119 MINOR(bit->device));
120 }
121
process(FILE ** fp,char * devname,char * file,unsigned int cpu)122 static int process(FILE **fp, char *devname, char *file, unsigned int cpu)
123 {
124 # define SWAP_BITS() do { \
125 if (bit_save) { \
126 struct blk_io_trace *tmp = bit_save; \
127 bit_save = bit; \
128 bit = tmp; \
129 } \
130 else { \
131 bit_save = bit; \
132 bit = malloc(sizeof(struct blk_io_trace)); \
133 } \
134 } while (0)
135
136 # define INC_BAD(str) do { \
137 nbad++; \
138 fprintf(ofp, " ----------------\n"); \
139 if (bit_save) dump_trace(ofp,"seq-1",bit_save); \
140 dump_trace(ofp, str, bit); \
141 SWAP_BITS(); \
142 } while (0)
143
144 size_t n;
145 FILE *ifp, *ofp;
146 __u32 save_device = 0, save_sequence = 0;
147 __u64 save_time = 0;
148 struct blk_io_trace *bit_save = NULL;
149 struct blk_io_trace *bit = malloc(sizeof(struct blk_io_trace));
150 unsigned int ngood = 0;
151 unsigned int nbad = 0;
152 unsigned int nbad_trace = 0, nbad_pdu = 0, nbad_cpu = 0;
153 unsigned int nbad_seq = 0, nbad_dev = 0, nbad_time = 0;
154 char ofname[1024];
155
156 ifp = fopen(file, "r");
157 if (!ifp)
158 return 0;
159
160 sprintf(ofname, "%s.verify.out", devname);
161
162 if (!*fp) {
163 *fp = fopen(ofname, "w");
164 if (*fp == NULL) {
165 fprintf(stderr,"Failed to open %s (%s), skipping\n",
166 ofname, strerror(errno));
167 fclose(ifp);
168 return 0;
169 }
170 fprintf(*fp, "\n---------------\n" );
171 fprintf(*fp, "Verifying %s\n", devname);
172 }
173
174 ofp = *fp;
175 while ((n = fread(bit, sizeof(struct blk_io_trace), 1, ifp)) == 1) {
176 if (ferror(ifp)) {
177 clearerr(ifp);
178 perror("fread");
179 break;
180 }
181 if (data_is_native == -1)
182 check_data_endianness(bit->magic);
183
184 trace_to_cpu(bit);
185
186 if (!CHECK_MAGIC(bit)) {
187 INC_BAD("bad trace");
188 continue;
189 }
190
191 if ((bit->magic & 0xff) != SUPPORTED_VERSION) {
192 fprintf(stderr, "unsupported trace version\n");
193 break;
194 }
195
196 if (bit->pdu_len) {
197 char *pdu_buf;
198
199 pdu_buf = malloc(bit->pdu_len);
200 n = fread(pdu_buf, bit->pdu_len, 1, ifp);
201 if (n == 0) {
202 INC_BAD("bad pdu");
203 nbad_seq++;
204 free(pdu_buf);
205 break;
206 }
207 free(pdu_buf);
208 }
209
210 if (bit->cpu != cpu) {
211 INC_BAD("bad cpu");
212 nbad_cpu++;
213 continue;
214 }
215
216 /*
217 * skip notify traces, they don't have valid sequences
218 */
219 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY))
220 continue;
221
222 if (ngood) {
223 if (bit->sequence <= save_sequence) {
224 INC_BAD("bad seq");
225 nbad_seq++;
226 continue;
227 }
228 else if (bit->time <= save_time) {
229 INC_BAD("time regression");
230 nbad_time++;
231 continue;
232 }
233 else if (bit->device != save_device) {
234 INC_BAD("bad dev");
235 nbad_dev++;
236 continue;
237 }
238 }
239
240 save_sequence = bit->sequence;
241 save_time = bit->time;
242 save_device = bit->device;
243
244 ngood++;
245 SWAP_BITS();
246 }
247
248 if (n == 0 && !feof(ifp))
249 fprintf(stderr,"%s: fread failed %d/%s\n",
250 file, errno, strerror(errno));
251 fclose(ifp);
252
253 fprintf(ofp, " ---------------------\n");
254 fprintf(ofp, " Summary for cpu %d:\n", cpu);
255 fprintf(ofp, " %10d valid + %10d invalid (%5.1f%%) processed\n\n",
256 ngood, nbad,
257 ngood ? 100.0 * (float)ngood / (float)(ngood + nbad) : 0.0);
258
259 if (nbad) {
260 if (nbad_trace)
261 fprintf(ofp, "%8s %d traces\n", "", nbad_trace);
262 if (nbad_trace)
263 fprintf(ofp, "%8s %d pdu\n", "", nbad_pdu);
264 if (nbad_cpu)
265 fprintf(ofp, "%8s %d cpu\n", "", nbad_cpu);
266 if (nbad_seq)
267 fprintf(ofp, "%8s %d seq\n", "", nbad_seq);
268 if (nbad_dev)
269 fprintf(ofp, "%8s %d dev\n", "", nbad_dev);
270 if (nbad_time)
271 fprintf(ofp, "%8s %d time\n", "", nbad_time);
272 fprintf(ofp,"\n");
273 }
274
275 return nbad;
276 }
277
main(int argc,char * argv[])278 int main(int argc, char *argv[])
279 {
280 char *devname;
281 struct stat st;
282 int i, cpu, nbad, rval = 0;
283 FILE *ofp;
284 char *ofname = malloc(1024);
285 char *fname = malloc(1024);
286
287 if (argc < 2) {
288 fprintf(stderr,"FATAL: Need device name(s)\n");
289 fprintf(stderr,"Usage: blkrawverify <dev> [<dev>...]\n");
290 exit(1);
291 }
292
293 for (i = 1; i < argc; i++) {
294 devname = argv[i];
295 sprintf(ofname, "%s.verify.out", devname);
296 ofp = NULL;
297
298 printf("Verifying %s\n", devname); fflush(stdout);
299 for (cpu = 0; ; cpu++) {
300 sprintf(fname, "%s.blktrace.%d", devname, cpu);
301 if (stat(fname, &st) < 0) {
302 if (cpu == 0) {
303 fprintf(stderr, "No tracefiles found for %s\n",
304 devname);
305 rval = 1;
306 }
307 break;
308 }
309 printf(" CPU %d ", cpu); fflush(stdout);
310 nbad = process(&ofp, devname, fname, cpu);
311 if (nbad) {
312 printf("-- %d bad", nbad);
313 rval = 1;
314 }
315 printf("\n");
316 }
317 if (ofp) {
318 fclose(ofp);
319 fprintf(stdout, "Wrote output to %s\n", ofname);
320 }
321 }
322
323 return rval;
324 }
325