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