f74832d8c7832f2409dd062f6d979f272e68febd
[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 a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
67         int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
68         int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
69         int m = t->action & BLK_TC_ACT(BLK_TC_META);
70         int i = 0;
71
72         if (w)
73                 rwbs[i++] = 'W';
74         else
75                 rwbs[i++] = 'R';
76         if (a)
77                 rwbs[i++] = 'A';
78         if (b)
79                 rwbs[i++] = 'B';
80         if (s)
81                 rwbs[i++] = 'S';
82         if (m)
83                 rwbs[i++] = 'M';
84
85         rwbs[i] = '\0';
86 }
87
88 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
89 {
90         static char zero[4096];
91
92         return !memcmp(pdu, zero, len);
93 }
94
95 static 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]);
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                 }
118         }
119
120         return p;
121 }
122
123 #define pdu_start(t)    (((void *) (t) + sizeof(struct blk_io_trace)))
124
125 static unsigned int get_pdu_int(struct blk_io_trace *t)
126 {
127         __u64 *val = pdu_start(t);
128
129         return be64_to_cpu(*val);
130 }
131
132 static 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);
135         __u64 sector = __r->sector;
136
137         r->device = be32_to_cpu(__r->device);
138         r->device_from = be32_to_cpu(__r->device_from);
139         r->sector = be64_to_cpu(sector);
140 }
141
142 static 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;
164         case 'C': {
165                 char *name = find_process_name(t->pid);
166
167                 fprintf(ofp, strcat(format, "s"), name);
168                 break;
169         }
170         case 'd': {
171                 char rwbs[6];
172
173                 fill_rwbs(rwbs, t);
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':
190                 fprintf(ofp, strcat(format, "u"), t_sec(t));
191                 break;
192         case 'N':
193                 fprintf(ofp, strcat(format, "u"), t->bytes);
194                 break;
195         case 'p':
196                 fprintf(ofp, strcat(format, "u"), t->pid);
197                 break;
198         case 'P': { /* format width ignored */
199                 char *p = dump_pdu(pdu_buf, pdu_len);
200                 if (p)
201                         fprintf(ofp, "%s", p);
202                 break;
203         }
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': {
225                 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
226                 break;
227         }
228         default:
229                 fprintf(ofp,strcat(format, "c"), field);
230                 break;
231         }
232 }
233
234 static 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,
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
261 static 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)
264 {
265         struct blk_io_trace_remap r = { .device = 0, };
266         char rwbs[6];
267         char *name;
268
269         fill_rwbs(rwbs, t);
270
271         /*
272          * The header is always the same
273          */
274         if (act[0] == 'A') {    /* Remap */
275                 get_pdu_remap(t, &r);
276                 t->device = r.device_from;
277         }
278
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);
283
284         name = find_process_name(t->pid);
285
286         switch (act[0]) {
287         case 'R':       /* Requeue */
288         case 'C':       /* Complete */
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 {
295                         if (elapsed != -1ULL) {
296                                 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
297                                         (unsigned long long) t->sector,
298                                         t_sec(t), elapsed, t->error);
299                         } else {
300                                 fprintf(ofp, "%llu + %u [%d]\n",
301                                         (unsigned long long) t->sector,
302                                         t_sec(t), t->error);
303                         }
304                 }
305                 break;
306
307         case 'D':       /* Issue */
308         case 'I':       /* Insert */
309         case 'Q':       /* Queue */
310         case 'W':       /* Bounce */
311                 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
312                         char *p;
313                         fprintf(ofp, "%u ", t->bytes);
314                         p = dump_pdu(pdu_buf, pdu_len);
315                         if (p)
316                                 fprintf(ofp, "(%s) ", p);
317                         fprintf(ofp, "[%s]\n", name);
318                 } else {
319                         if (elapsed != -1ULL) {
320                                 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
321                                         (unsigned long long) t->sector,
322                                         t_sec(t), elapsed, name);
323                         } else {
324                                 fprintf(ofp, "%llu + %u [%s]\n",
325                                         (unsigned long long) t->sector,
326                                         t_sec(t), name);
327                         }
328                 }
329                 break;
330
331         case 'B':       /* Back merge */
332         case 'F':       /* Front merge */
333         case 'M':       /* Front or back merge */
334         case 'G':       /* Get request */
335         case 'S':       /* Sleep request */
336                 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
337                         t_sec(t), name);
338                 break;
339
340         case 'P':       /* Plug */
341                 fprintf(ofp, "[%s]\n", name);
342                 break;
343
344         case 'U':       /* Unplug IO */
345         case 'T':       /* Unplug timer */
346                 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
347                 break;
348
349         case 'A':       /* remap */
350                 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
351                         (unsigned long long) t->sector, t_sec(t),
352                         MAJOR(r.device), MINOR(r.device),
353                         (unsigned long long) r.sector);
354                 break;
355                 
356         case 'X':       /* Split */
357                 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
358                         get_pdu_int(t), name);
359                 break;
360
361         default:
362                 fprintf(stderr, "Unknown action %c\n", act[0]);
363                 break;
364         }
365
366 }
367
368 void 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 {
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         }
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