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