[PATCH] Fixed the Makefile in btt/
[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->sector = be64_to_cpu(sector);
139 }
140
141 static void print_field(char *act, struct per_cpu_info *pci,
142                         struct blk_io_trace *t, unsigned long long elapsed,
143                         int pdu_len, unsigned char *pdu_buf, char field,
144                         int minus, int has_w, int width)
145 {
146         char format[64];
147
148         if (has_w) {
149                 if (minus)
150                         sprintf(format, "%%-%d", width);
151                 else
152                         sprintf(format, "%%%d", width);
153         } else
154                 sprintf(format, "%%");
155
156         switch (field) {
157         case 'a':
158                 fprintf(ofp, strcat(format, "s"), act);
159                 break;
160         case 'c':
161                 fprintf(ofp, strcat(format, "d"), pci->cpu);
162                 break;
163         case 'C': {
164                 char *name = find_process_name(t->pid);
165
166                 fprintf(ofp, strcat(format, "s"), name);
167                 break;
168         }
169         case 'd': {
170                 char rwbs[6];
171
172                 fill_rwbs(rwbs, t);
173                 fprintf(ofp, strcat(format, "s"), rwbs);
174                 break;
175         }
176         case 'D':       /* format width ignored */
177                 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
178                 break;
179         case 'e':
180                 fprintf(ofp, strcat(format, "d"), t->error);
181                 break;
182         case 'M':
183                 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
184                 break;
185         case 'm':
186                 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
187                 break;
188         case 'n':
189                 fprintf(ofp, strcat(format, "u"), t_sec(t));
190                 break;
191         case 'N':
192                 fprintf(ofp, strcat(format, "u"), t->bytes);
193                 break;
194         case 'p':
195                 fprintf(ofp, strcat(format, "u"), t->pid);
196                 break;
197         case 'P': { /* format width ignored */
198                 char *p = dump_pdu(pdu_buf, pdu_len);
199                 if (p)
200                         fprintf(ofp, "%s", p);
201                 break;
202         }
203         case 's':
204                 fprintf(ofp, strcat(format, "ld"), t->sequence);
205                 break;
206         case 'S':
207                 fprintf(ofp, strcat(format, "lu"), t->sector);
208                 break;
209         case 't':
210                 sprintf(format, "%%0%dlu", has_w ? width : 9);
211                 fprintf(ofp, format, NANO_SECONDS(t->time));
212                 break;
213         case 'T':
214                 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
215                 break;
216         case 'u':
217                 if (elapsed == -1ULL) {
218                         fprintf(stderr, "Expecting elapsed value\n");
219                         exit(1);
220                 }
221                 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
222                 break;
223         case 'U': {
224                 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
225                 break;
226         }
227         default:
228                 fprintf(ofp,strcat(format, "c"), field);
229                 break;
230         }
231 }
232
233 static char *parse_field(char *act, struct per_cpu_info *pci,
234                          struct blk_io_trace *t, unsigned long long elapsed,
235                          int pdu_len, unsigned char *pdu_buf,
236                          char *master_format)
237 {
238         int minus = 0;
239         int has_w = 0;
240         int width = 0;
241         char *p = master_format;
242
243         if (*p == '-') {
244                 minus = 1;
245                 p++;
246         }
247         if (isdigit(*p)) {
248                 has_w = 1;
249                 do {
250                         width = (width * 10) + (*p++ - '0');
251                 } while ((*p) && (isdigit(*p)));
252         }
253         if (*p) {
254                 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
255                             minus, has_w, width);
256         }
257         return p;
258 }
259
260 static void process_default(char *act, struct per_cpu_info *pci,
261                             struct blk_io_trace *t, unsigned long long elapsed,
262                             int pdu_len, unsigned char *pdu_buf)
263 {
264         char rwbs[6];
265         char *name;
266
267         fill_rwbs(rwbs, t);
268
269         /*
270          * The header is always the same
271          */
272         fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
273                 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
274                 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
275                 t->pid, act, rwbs);
276
277         name = find_process_name(t->pid);
278
279         switch (act[0]) {
280         case 'R':       /* Requeue */
281         case 'C':       /* Complete */
282                 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
283                         char *p = dump_pdu(pdu_buf, pdu_len);
284                         if (p)
285                                 fprintf(ofp, "(%s) ", p);
286                         fprintf(ofp, "[%d]\n", t->error);
287                 } else {
288                         if (elapsed != -1ULL) {
289                                 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
290                                         (unsigned long long) t->sector,
291                                         t_sec(t), elapsed, t->error);
292                         } else {
293                                 fprintf(ofp, "%llu + %u [%d]\n",
294                                         (unsigned long long) t->sector,
295                                         t_sec(t), t->error);
296                         }
297                 }
298                 break;
299
300         case 'D':       /* Issue */
301         case 'I':       /* Insert */
302         case 'Q':       /* Queue */
303         case 'W':       /* Bounce */
304                 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
305                         char *p;
306                         fprintf(ofp, "%u ", t->bytes);
307                         p = dump_pdu(pdu_buf, pdu_len);
308                         if (p)
309                                 fprintf(ofp, "(%s) ", p);
310                         fprintf(ofp, "[%s]\n", name);
311                 } else {
312                         if (elapsed != -1ULL) {
313                                 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
314                                         (unsigned long long) t->sector,
315                                         t_sec(t), elapsed, name);
316                         } else {
317                                 fprintf(ofp, "%llu + %u [%s]\n",
318                                         (unsigned long long) t->sector,
319                                         t_sec(t), name);
320                         }
321                 }
322                 break;
323
324         case 'B':       /* Back merge */
325         case 'F':       /* Front merge */
326         case 'M':       /* Front or back merge */
327         case 'G':       /* Get request */
328         case 'S':       /* Sleep request */
329                 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
330                         t_sec(t), name);
331                 break;
332
333         case 'P':       /* Plug */
334                 fprintf(ofp, "[%s]\n", name);
335                 break;
336
337         case 'U':       /* Unplug IO */
338         case 'T':       /* Unplug timer */
339                 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
340                 break;
341
342         case 'A': {     /* remap */
343                 struct blk_io_trace_remap r;
344
345                 get_pdu_remap(t, &r);
346                 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
347                         (unsigned long long) t->sector, t_sec(t),
348                         MAJOR(r.device), MINOR(r.device),
349                         (unsigned long long) r.sector);
350                 break;
351         }
352                 
353         case 'X':       /* Split */
354                 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
355                         get_pdu_int(t), name);
356                 break;
357
358         default:
359                 fprintf(stderr, "Unknown action %c\n", act[0]);
360                 break;
361         }
362
363 }
364
365 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
366                  unsigned long long elapsed, int pdu_len,
367                  unsigned char *pdu_buf)
368 {
369         char *p = override_format[(int) *act];
370
371         if (!p) {
372                 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
373                 return;
374         }
375
376         while (*p) {
377                 switch (*p) {
378                 case '%':       /* Field specifier */
379                         p++;
380                         if (*p == '%')
381                                 fprintf(ofp, "%c", *p++);
382                         else if (!*p)
383                                 fprintf(ofp, "%c", '%');
384                         else
385                                 p = parse_field(act, pci, t, elapsed,
386                                                 pdu_len, pdu_buf, p);
387                         break;
388                 case '\\': {    /* escape */
389                         switch (p[1]) {
390                         case 'b': fprintf(ofp, "\b"); break;
391                         case 'n': fprintf(ofp, "\n"); break;
392                         case 'r': fprintf(ofp, "\r"); break;
393                         case 't': fprintf(ofp, "\t"); break;
394                         default:
395                                 fprintf(stderr, 
396                                         "Invalid escape char in format %c\n",
397                                         p[1]);
398                                 exit(1);
399                                 /*NOTREACHED*/
400                         }
401                         p += 2;
402                         break;
403                 }
404                 default:
405                         fprintf(ofp, "%c", *p++);
406                         break;
407                 }
408         }
409 }
410
411