[PATCH] btt: Fix invalid q2a
[blktrace.git] / blkparse_fmt.c
CommitLineData
71d5d4c9
JA
1/*
2 * This file contains format parsing code for blkparse, allowing you to
3 * customize the individual action format and generel output format.
4 */
5#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <ctype.h>
10
11#include "blktrace.h"
12
a8f30e64 13#define VALID_SPECS "ABCDFGMPQRSTUWX"
71d5d4c9
JA
14
15#define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
16
17static char *override_format[256];
18
19static inline int valid_spec(int spec)
20{
21 return strchr(VALID_SPECS, spec) != NULL;
22}
23
e820abd7 24void set_all_format_specs(char *option)
71d5d4c9
JA
25{
26 char *p;
27
28 for (p = VALID_SPECS; *p; p++)
29 if (override_format[(int)(*p)] == NULL)
e820abd7 30 override_format[(int)(*p)] = strdup(option);
71d5d4c9
JA
31}
32
e820abd7 33int add_format_spec(char *option)
71d5d4c9
JA
34{
35 int spec = optarg[0];
36
37 if (!valid_spec(spec)) {
38 fprintf(stderr,"Bad format specifier %c\n", spec);
39 return 1;
40 }
41 if (optarg[1] != ',') {
e820abd7 42 fprintf(stderr,"Bad format specifier - need ',' %s\n", option);
71d5d4c9
JA
43 return 1;
44 }
e820abd7
JA
45 option += 2;
46 if (*option == '\0') {
47 fprintf(stderr,"Bad format specifier - need fmt %s\n", option);
71d5d4c9
JA
48 return 1;
49 }
50
51 /*
52 * Set both merges (front and back)
53 */
54 if (spec == 'M') {
e820abd7
JA
55 override_format['B'] = strdup(option);
56 override_format['M'] = strdup(option);
71d5d4c9 57 } else
e820abd7 58 override_format[spec] = strdup(option);
71d5d4c9
JA
59
60 return 0;
61}
62
768277cc
JA
63static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
64{
65 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
f86990ed 66 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
768277cc
JA
67 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
68 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
711e4d25 69 int m = t->action & BLK_TC_ACT(BLK_TC_META);
768277cc
JA
70 int i = 0;
71
72 if (w)
73 rwbs[i++] = 'W';
74 else
75 rwbs[i++] = 'R';
f86990ed
NS
76 if (a)
77 rwbs[i++] = 'A';
768277cc
JA
78 if (b)
79 rwbs[i++] = 'B';
80 if (s)
81 rwbs[i++] = 'S';
711e4d25
JA
82 if (m)
83 rwbs[i++] = 'M';
768277cc
JA
84
85 rwbs[i] = '\0';
86}
87
63756b50 88static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
1d6376a5 89{
63756b50 90 static char zero[4096];
1d6376a5 91
63756b50 92 return !memcmp(pdu, zero, len);
1d6376a5
JA
93}
94
768277cc
JA
95static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
96{
97 static char p[4096];
98 int i, len;
99
100 if (!pdu_buf || !pdu_len)
101 return NULL;
102
103 for (len = 0, i = 0; i < pdu_len; i++) {
104 if (i)
105 len += sprintf(p + len, " ");
106
107 len += sprintf(p + len, "%02x", pdu_buf[i]);
1d6376a5
JA
108
109 /*
110 * usually dump for cdb dumps where we can see lots of
111 * zeroes, stop when the rest is just zeroes and indicate
112 * so with a .. appended
113 */
114 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
115 sprintf(p + len, " ..");
116 break;
117 }
768277cc
JA
118 }
119
120 return p;
121}
122
a8f30e64
JA
123#define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
124
768277cc
JA
125static unsigned int get_pdu_int(struct blk_io_trace *t)
126{
a8f30e64 127 __u64 *val = pdu_start(t);
768277cc
JA
128
129 return be64_to_cpu(*val);
130}
131
a8f30e64
JA
132static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
133{
134 struct blk_io_trace_remap *__r = pdu_start(t);
663af819 135 __u64 sector = __r->sector;
a8f30e64
JA
136
137 r->device = be32_to_cpu(__r->device);
97d13fb0 138 r->device_from = be32_to_cpu(__r->device_from);
663af819 139 r->sector = be64_to_cpu(sector);
a8f30e64
JA
140}
141
71d5d4c9
JA
142static void print_field(char *act, struct per_cpu_info *pci,
143 struct blk_io_trace *t, unsigned long long elapsed,
144 int pdu_len, unsigned char *pdu_buf, char field,
145 int minus, int has_w, int width)
146{
147 char format[64];
148
149 if (has_w) {
150 if (minus)
151 sprintf(format, "%%-%d", width);
152 else
153 sprintf(format, "%%%d", width);
154 } else
155 sprintf(format, "%%");
156
157 switch (field) {
158 case 'a':
159 fprintf(ofp, strcat(format, "s"), act);
160 break;
161 case 'c':
162 fprintf(ofp, strcat(format, "d"), pci->cpu);
163 break;
bfc70ad5
JA
164 case 'C': {
165 char *name = find_process_name(t->pid);
166
167 fprintf(ofp, strcat(format, "s"), name);
71d5d4c9 168 break;
bfc70ad5 169 }
71d5d4c9 170 case 'd': {
711e4d25 171 char rwbs[6];
768277cc
JA
172
173 fill_rwbs(rwbs, t);
71d5d4c9
JA
174 fprintf(ofp, strcat(format, "s"), rwbs);
175 break;
176 }
177 case 'D': /* format width ignored */
178 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
179 break;
180 case 'e':
181 fprintf(ofp, strcat(format, "d"), t->error);
182 break;
183 case 'M':
184 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
185 break;
186 case 'm':
187 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
188 break;
189 case 'n':
ae957cbc 190 fprintf(ofp, strcat(format, "u"), t_sec(t));
71d5d4c9 191 break;
1c8ca7b5
JA
192 case 'N':
193 fprintf(ofp, strcat(format, "u"), t->bytes);
194 break;
71d5d4c9
JA
195 case 'p':
196 fprintf(ofp, strcat(format, "u"), t->pid);
197 break;
768277cc
JA
198 case 'P': { /* format width ignored */
199 char *p = dump_pdu(pdu_buf, pdu_len);
200 if (p)
201 fprintf(ofp, "%s", p);
71d5d4c9 202 break;
768277cc 203 }
71d5d4c9
JA
204 case 's':
205 fprintf(ofp, strcat(format, "ld"), t->sequence);
206 break;
207 case 'S':
208 fprintf(ofp, strcat(format, "lu"), t->sector);
209 break;
210 case 't':
211 sprintf(format, "%%0%dlu", has_w ? width : 9);
212 fprintf(ofp, format, NANO_SECONDS(t->time));
213 break;
214 case 'T':
215 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
216 break;
217 case 'u':
218 if (elapsed == -1ULL) {
219 fprintf(stderr, "Expecting elapsed value\n");
220 exit(1);
221 }
222 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
223 break;
224 case 'U': {
768277cc 225 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
71d5d4c9
JA
226 break;
227 }
228 default:
229 fprintf(ofp,strcat(format, "c"), field);
230 break;
231 }
232}
233
768277cc
JA
234static char *parse_field(char *act, struct per_cpu_info *pci,
235 struct blk_io_trace *t, unsigned long long elapsed,
236 int pdu_len, unsigned char *pdu_buf,
71d5d4c9
JA
237 char *master_format)
238{
239 int minus = 0;
240 int has_w = 0;
241 int width = 0;
242 char *p = master_format;
243
244 if (*p == '-') {
245 minus = 1;
246 p++;
247 }
248 if (isdigit(*p)) {
249 has_w = 1;
250 do {
251 width = (width * 10) + (*p++ - '0');
252 } while ((*p) && (isdigit(*p)));
253 }
254 if (*p) {
255 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
256 minus, has_w, width);
257 }
258 return p;
259}
260
768277cc
JA
261static void process_default(char *act, struct per_cpu_info *pci,
262 struct blk_io_trace *t, unsigned long long elapsed,
263 int pdu_len, unsigned char *pdu_buf)
71d5d4c9 264{
97d13fb0 265 struct blk_io_trace_remap r = { .device = 0, };
711e4d25 266 char rwbs[6];
bfc70ad5 267 char *name;
71d5d4c9 268
768277cc
JA
269 fill_rwbs(rwbs, t);
270
271 /*
272 * The header is always the same
273 */
97d13fb0
AB
274 if (act[0] == 'A') { /* Remap */
275 get_pdu_remap(t, &r);
276 t->device = r.device_from;
277 }
278
768277cc
JA
279 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
280 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
281 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
282 t->pid, act, rwbs);
71d5d4c9 283
bfc70ad5
JA
284 name = find_process_name(t->pid);
285
768277cc 286 switch (act[0]) {
80c53608 287 case 'R': /* Requeue */
71d5d4c9 288 case 'C': /* Complete */
768277cc
JA
289 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
290 char *p = dump_pdu(pdu_buf, pdu_len);
291 if (p)
292 fprintf(ofp, "(%s) ", p);
293 fprintf(ofp, "[%d]\n", t->error);
294 } else {
71d5d4c9 295 if (elapsed != -1ULL) {
768277cc
JA
296 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
297 (unsigned long long) t->sector,
ae957cbc 298 t_sec(t), elapsed, t->error);
768277cc
JA
299 } else {
300 fprintf(ofp, "%llu + %u [%d]\n",
301 (unsigned long long) t->sector,
ae957cbc 302 t_sec(t), t->error);
768277cc 303 }
71d5d4c9 304 }
71d5d4c9
JA
305 break;
306
307 case 'D': /* Issue */
71d5d4c9 308 case 'I': /* Insert */
71d5d4c9
JA
309 case 'Q': /* Queue */
310 case 'W': /* Bounce */
768277cc
JA
311 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
312 char *p;
1c8ca7b5 313 fprintf(ofp, "%u ", t->bytes);
768277cc
JA
314 p = dump_pdu(pdu_buf, pdu_len);
315 if (p)
316 fprintf(ofp, "(%s) ", p);
bfc70ad5 317 fprintf(ofp, "[%s]\n", name);
768277cc
JA
318 } else {
319 if (elapsed != -1ULL) {
320 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
321 (unsigned long long) t->sector,
bfc70ad5 322 t_sec(t), elapsed, name);
768277cc
JA
323 } else {
324 fprintf(ofp, "%llu + %u [%s]\n",
325 (unsigned long long) t->sector,
bfc70ad5 326 t_sec(t), name);
768277cc
JA
327 }
328 }
71d5d4c9
JA
329 break;
330
331 case 'B': /* Back merge */
332 case 'F': /* Front merge */
333 case 'M': /* Front or back merge */
768277cc
JA
334 case 'G': /* Get request */
335 case 'S': /* Sleep request */
336 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 337 t_sec(t), name);
71d5d4c9
JA
338 break;
339
340 case 'P': /* Plug */
bfc70ad5 341 fprintf(ofp, "[%s]\n", name);
71d5d4c9
JA
342 break;
343
344 case 'U': /* Unplug IO */
a8f30e64 345 case 'T': /* Unplug timer */
bfc70ad5 346 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
71d5d4c9
JA
347 break;
348
97d13fb0 349 case 'A': /* remap */
a8f30e64 350 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
e9f883ca 351 (unsigned long long) t->sector, t_sec(t),
a8f30e64 352 MAJOR(r.device), MINOR(r.device),
e9f883ca 353 (unsigned long long) r.sector);
a8f30e64 354 break;
a8f30e64 355
71d5d4c9 356 case 'X': /* Split */
768277cc 357 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 358 get_pdu_int(t), name);
71d5d4c9
JA
359 break;
360
361 default:
768277cc
JA
362 fprintf(stderr, "Unknown action %c\n", act[0]);
363 break;
71d5d4c9
JA
364 }
365
71d5d4c9
JA
366}
367
368void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
369 unsigned long long elapsed, int pdu_len,
370 unsigned char *pdu_buf)
371{
768277cc
JA
372 char *p = override_format[(int) *act];
373
374 if (!p) {
375 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
376 return;
377 }
71d5d4c9
JA
378
379 while (*p) {
380 switch (*p) {
381 case '%': /* Field specifier */
382 p++;
383 if (*p == '%')
384 fprintf(ofp, "%c", *p++);
385 else if (!*p)
386 fprintf(ofp, "%c", '%');
387 else
388 p = parse_field(act, pci, t, elapsed,
389 pdu_len, pdu_buf, p);
390 break;
391 case '\\': { /* escape */
392 switch (p[1]) {
393 case 'b': fprintf(ofp, "\b"); break;
394 case 'n': fprintf(ofp, "\n"); break;
395 case 'r': fprintf(ofp, "\r"); break;
396 case 't': fprintf(ofp, "\t"); break;
397 default:
398 fprintf(stderr,
399 "Invalid escape char in format %c\n",
400 p[1]);
401 exit(1);
402 /*NOTREACHED*/
403 }
404 p += 2;
405 break;
406 }
407 default:
408 fprintf(ofp, "%c", *p++);
409 break;
410 }
411 }
412}
413
414