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