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