Don't like btrecord against libaio and librt, as it doesn't use any of their symbols
[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
JA
46 option += 2;
47 if (*option == '\0') {
48 fprintf(stderr,"Bad format specifier - need fmt %s\n", option);
71d5d4c9
JA
49 return 1;
50 }
51
0ac4c20e 52 override_format[spec] = strdup(option);
71d5d4c9
JA
53
54 return 0;
55}
56
768277cc
JA
57static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
58{
59 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
f86990ed 60 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
768277cc
JA
61 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
62 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
711e4d25 63 int m = t->action & BLK_TC_ACT(BLK_TC_META);
768277cc
JA
64 int i = 0;
65
66 if (w)
67 rwbs[i++] = 'W';
d57450af 68 else if (t->bytes)
768277cc 69 rwbs[i++] = 'R';
d57450af
JA
70 else
71 rwbs[i++] = 'N';
f86990ed
NS
72 if (a)
73 rwbs[i++] = 'A';
768277cc
JA
74 if (b)
75 rwbs[i++] = 'B';
76 if (s)
77 rwbs[i++] = 'S';
711e4d25
JA
78 if (m)
79 rwbs[i++] = 'M';
768277cc
JA
80
81 rwbs[i] = '\0';
82}
83
7bd4fd0a
OK
84static const char *
85print_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
63756b50 109static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
1d6376a5 110{
63756b50 111 static char zero[4096];
1d6376a5 112
63756b50 113 return !memcmp(pdu, zero, len);
1d6376a5
JA
114}
115
768277cc
JA
116static 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]);
1d6376a5
JA
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 }
768277cc
JA
139 }
140
141 return p;
142}
143
a8f30e64
JA
144#define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
145
768277cc
JA
146static unsigned int get_pdu_int(struct blk_io_trace *t)
147{
a8f30e64 148 __u64 *val = pdu_start(t);
768277cc
JA
149
150 return be64_to_cpu(*val);
151}
152
a8f30e64
JA
153static 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);
663af819 156 __u64 sector = __r->sector;
a8f30e64
JA
157
158 r->device = be32_to_cpu(__r->device);
97d13fb0 159 r->device_from = be32_to_cpu(__r->device_from);
663af819 160 r->sector = be64_to_cpu(sector);
a8f30e64
JA
161}
162
71d5d4c9
JA
163static 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;
bfc70ad5
JA
185 case 'C': {
186 char *name = find_process_name(t->pid);
187
188 fprintf(ofp, strcat(format, "s"), name);
71d5d4c9 189 break;
bfc70ad5 190 }
71d5d4c9 191 case 'd': {
711e4d25 192 char rwbs[6];
768277cc
JA
193
194 fill_rwbs(rwbs, t);
71d5d4c9
JA
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':
ae957cbc 211 fprintf(ofp, strcat(format, "u"), t_sec(t));
71d5d4c9 212 break;
1c8ca7b5
JA
213 case 'N':
214 fprintf(ofp, strcat(format, "u"), t->bytes);
215 break;
71d5d4c9
JA
216 case 'p':
217 fprintf(ofp, strcat(format, "u"), t->pid);
218 break;
768277cc
JA
219 case 'P': { /* format width ignored */
220 char *p = dump_pdu(pdu_buf, pdu_len);
221 if (p)
222 fprintf(ofp, "%s", p);
71d5d4c9 223 break;
768277cc 224 }
71d5d4c9
JA
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;
7bd4fd0a 245 case 'U':
768277cc 246 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
71d5d4c9 247 break;
7bd4fd0a
OK
248 case 'z':
249 fprintf(ofp, strcat(format, "s"), print_time(t->time));
250 break;
71d5d4c9
JA
251 default:
252 fprintf(ofp,strcat(format, "c"), field);
253 break;
254 }
255}
256
768277cc
JA
257static 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,
71d5d4c9
JA
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
768277cc
JA
284static 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)
71d5d4c9 287{
97d13fb0 288 struct blk_io_trace_remap r = { .device = 0, };
711e4d25 289 char rwbs[6];
bfc70ad5 290 char *name;
71d5d4c9 291
768277cc
JA
292 fill_rwbs(rwbs, t);
293
294 /*
295 * The header is always the same
296 */
97d13fb0
AB
297 if (act[0] == 'A') { /* Remap */
298 get_pdu_remap(t, &r);
299 t->device = r.device_from;
300 }
301
768277cc
JA
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);
71d5d4c9 306
bfc70ad5
JA
307 name = find_process_name(t->pid);
308
768277cc 309 switch (act[0]) {
80c53608 310 case 'R': /* Requeue */
71d5d4c9 311 case 'C': /* Complete */
768277cc
JA
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 {
71d5d4c9 318 if (elapsed != -1ULL) {
d57450af
JA
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);
768277cc 327 } else {
d57450af
JA
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);
768277cc 336 }
71d5d4c9 337 }
71d5d4c9
JA
338 break;
339
340 case 'D': /* Issue */
71d5d4c9 341 case 'I': /* Insert */
71d5d4c9 342 case 'Q': /* Queue */
bd5e71cf 343 case 'B': /* Bounce */
768277cc
JA
344 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
345 char *p;
1c8ca7b5 346 fprintf(ofp, "%u ", t->bytes);
768277cc
JA
347 p = dump_pdu(pdu_buf, pdu_len);
348 if (p)
349 fprintf(ofp, "(%s) ", p);
bfc70ad5 350 fprintf(ofp, "[%s]\n", name);
768277cc
JA
351 } else {
352 if (elapsed != -1ULL) {
d57450af
JA
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);
768277cc 360 } else {
d57450af
JA
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);
768277cc
JA
367 }
368 }
71d5d4c9
JA
369 break;
370
bd5e71cf 371 case 'M': /* Back merge */
71d5d4c9 372 case 'F': /* Front merge */
768277cc
JA
373 case 'G': /* Get request */
374 case 'S': /* Sleep request */
d57450af
JA
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);
71d5d4c9
JA
380 break;
381
382 case 'P': /* Plug */
bfc70ad5 383 fprintf(ofp, "[%s]\n", name);
71d5d4c9
JA
384 break;
385
386 case 'U': /* Unplug IO */
a8f30e64 387 case 'T': /* Unplug timer */
bfc70ad5 388 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
71d5d4c9
JA
389 break;
390
97d13fb0 391 case 'A': /* remap */
a8f30e64 392 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
e9f883ca 393 (unsigned long long) t->sector, t_sec(t),
a8f30e64 394 MAJOR(r.device), MINOR(r.device),
e9f883ca 395 (unsigned long long) r.sector);
a8f30e64 396 break;
a8f30e64 397
71d5d4c9 398 case 'X': /* Split */
768277cc 399 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 400 get_pdu_int(t), name);
71d5d4c9
JA
401 break;
402
403 default:
768277cc
JA
404 fprintf(stderr, "Unknown action %c\n", act[0]);
405 break;
71d5d4c9
JA
406 }
407
71d5d4c9
JA
408}
409
410void 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{
768277cc
JA
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 }
71d5d4c9
JA
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