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