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