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