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