Merge branch 'add-P'
[blktrace.git] / blkparse_fmt.c
CommitLineData
71d5d4c9
JA
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>
7bd4fd0a 10#include <time.h>
71d5d4c9
JA
11
12#include "blktrace.h"
13
4c446dc0 14#define VALID_SPECS "ABCDFGIMPQRSTUWX"
71d5d4c9
JA
15
16#define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
17
18static char *override_format[256];
19
20static inline int valid_spec(int spec)
21{
22 return strchr(VALID_SPECS, spec) != NULL;
23}
24
e820abd7 25void set_all_format_specs(char *option)
71d5d4c9
JA
26{
27 char *p;
28
29 for (p = VALID_SPECS; *p; p++)
30 if (override_format[(int)(*p)] == NULL)
e820abd7 31 override_format[(int)(*p)] = strdup(option);
71d5d4c9
JA
32}
33
e820abd7 34int add_format_spec(char *option)
71d5d4c9
JA
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] != ',') {
e820abd7 43 fprintf(stderr,"Bad format specifier - need ',' %s\n", option);
71d5d4c9
JA
44 return 1;
45 }
e820abd7 46 option += 2;
71d5d4c9 47
0ac4c20e 48 override_format[spec] = strdup(option);
71d5d4c9
JA
49
50 return 0;
51}
52
768277cc
JA
53static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
54{
55 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
f86990ed 56 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
768277cc
JA
57 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
58 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
711e4d25 59 int m = t->action & BLK_TC_ACT(BLK_TC_META);
37081bf5 60 int d = t->action & BLK_TC_ACT(BLK_TC_DISCARD);
768277cc
JA
61 int i = 0;
62
37081bf5
DW
63 if (d)
64 rwbs[i++] = 'D';
65 else if (w)
768277cc 66 rwbs[i++] = 'W';
d57450af 67 else if (t->bytes)
768277cc 68 rwbs[i++] = 'R';
d57450af
JA
69 else
70 rwbs[i++] = 'N';
f86990ed
NS
71 if (a)
72 rwbs[i++] = 'A';
768277cc
JA
73 if (b)
74 rwbs[i++] = 'B';
75 if (s)
76 rwbs[i++] = 'S';
711e4d25
JA
77 if (m)
78 rwbs[i++] = 'M';
768277cc
JA
79
80 rwbs[i] = '\0';
81}
82
7bd4fd0a
OK
83static const char *
84print_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
63756b50 108static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
1d6376a5 109{
63756b50 110 static char zero[4096];
1d6376a5 111
63756b50 112 return !memcmp(pdu, zero, len);
1d6376a5
JA
113}
114
768277cc
JA
115static 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]);
1d6376a5
JA
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 }
768277cc
JA
138 }
139
140 return p;
141}
142
a8f30e64
JA
143#define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
144
768277cc
JA
145static unsigned int get_pdu_int(struct blk_io_trace *t)
146{
a8f30e64 147 __u64 *val = pdu_start(t);
768277cc
JA
148
149 return be64_to_cpu(*val);
150}
151
a8f30e64
JA
152static 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);
663af819 155 __u64 sector = __r->sector;
a8f30e64
JA
156
157 r->device = be32_to_cpu(__r->device);
97d13fb0 158 r->device_from = be32_to_cpu(__r->device_from);
663af819 159 r->sector = be64_to_cpu(sector);
a8f30e64
JA
160}
161
71d5d4c9
JA
162static 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;
bfc70ad5
JA
184 case 'C': {
185 char *name = find_process_name(t->pid);
186
187 fprintf(ofp, strcat(format, "s"), name);
71d5d4c9 188 break;
bfc70ad5 189 }
71d5d4c9 190 case 'd': {
711e4d25 191 char rwbs[6];
768277cc
JA
192
193 fill_rwbs(rwbs, t);
71d5d4c9
JA
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':
ae957cbc 210 fprintf(ofp, strcat(format, "u"), t_sec(t));
71d5d4c9 211 break;
1c8ca7b5
JA
212 case 'N':
213 fprintf(ofp, strcat(format, "u"), t->bytes);
214 break;
71d5d4c9
JA
215 case 'p':
216 fprintf(ofp, strcat(format, "u"), t->pid);
217 break;
768277cc
JA
218 case 'P': { /* format width ignored */
219 char *p = dump_pdu(pdu_buf, pdu_len);
220 if (p)
221 fprintf(ofp, "%s", p);
71d5d4c9 222 break;
768277cc 223 }
71d5d4c9
JA
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;
7bd4fd0a 244 case 'U':
768277cc 245 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
71d5d4c9 246 break;
7bd4fd0a
OK
247 case 'z':
248 fprintf(ofp, strcat(format, "s"), print_time(t->time));
249 break;
71d5d4c9
JA
250 default:
251 fprintf(ofp,strcat(format, "c"), field);
252 break;
253 }
254}
255
768277cc
JA
256static 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,
71d5d4c9
JA
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
768277cc
JA
283static 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)
71d5d4c9 286{
97d13fb0 287 struct blk_io_trace_remap r = { .device = 0, };
711e4d25 288 char rwbs[6];
bfc70ad5 289 char *name;
71d5d4c9 290
768277cc
JA
291 fill_rwbs(rwbs, t);
292
293 /*
294 * The header is always the same
295 */
97d13fb0
AB
296 if (act[0] == 'A') { /* Remap */
297 get_pdu_remap(t, &r);
298 t->device = r.device_from;
299 }
300
768277cc
JA
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);
71d5d4c9 305
bfc70ad5
JA
306 name = find_process_name(t->pid);
307
768277cc 308 switch (act[0]) {
80c53608 309 case 'R': /* Requeue */
71d5d4c9 310 case 'C': /* Complete */
768277cc
JA
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 {
71d5d4c9 317 if (elapsed != -1ULL) {
d57450af
JA
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);
768277cc 326 } else {
d57450af
JA
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);
768277cc 335 }
71d5d4c9 336 }
71d5d4c9
JA
337 break;
338
339 case 'D': /* Issue */
71d5d4c9 340 case 'I': /* Insert */
71d5d4c9 341 case 'Q': /* Queue */
bd5e71cf 342 case 'B': /* Bounce */
768277cc
JA
343 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
344 char *p;
1c8ca7b5 345 fprintf(ofp, "%u ", t->bytes);
768277cc
JA
346 p = dump_pdu(pdu_buf, pdu_len);
347 if (p)
348 fprintf(ofp, "(%s) ", p);
bfc70ad5 349 fprintf(ofp, "[%s]\n", name);
768277cc
JA
350 } else {
351 if (elapsed != -1ULL) {
d57450af
JA
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);
768277cc 359 } else {
d57450af
JA
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);
768277cc
JA
366 }
367 }
71d5d4c9
JA
368 break;
369
bd5e71cf 370 case 'M': /* Back merge */
71d5d4c9 371 case 'F': /* Front merge */
768277cc
JA
372 case 'G': /* Get request */
373 case 'S': /* Sleep request */
d57450af
JA
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);
71d5d4c9
JA
379 break;
380
381 case 'P': /* Plug */
bfc70ad5 382 fprintf(ofp, "[%s]\n", name);
71d5d4c9
JA
383 break;
384
385 case 'U': /* Unplug IO */
a8f30e64 386 case 'T': /* Unplug timer */
bfc70ad5 387 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
71d5d4c9
JA
388 break;
389
97d13fb0 390 case 'A': /* remap */
a8f30e64 391 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
e9f883ca 392 (unsigned long long) t->sector, t_sec(t),
a8f30e64 393 MAJOR(r.device), MINOR(r.device),
e9f883ca 394 (unsigned long long) r.sector);
a8f30e64 395 break;
1a15f6a8 396
71d5d4c9 397 case 'X': /* Split */
768277cc 398 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 399 get_pdu_int(t), name);
71d5d4c9
JA
400 break;
401
1a15f6a8
AB
402 case 'm': /* Message */
403 fprintf(ofp, "%*s\n", pdu_len, pdu_buf);
404 break;
405
71d5d4c9 406 default:
768277cc
JA
407 fprintf(stderr, "Unknown action %c\n", act[0]);
408 break;
71d5d4c9
JA
409 }
410
71d5d4c9
JA
411}
412
413void 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{
768277cc
JA
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 }
71d5d4c9
JA
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