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