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