2 * This file contains format parsing code for blkparse, allowing you to
3 * customize the individual action format and generel output format.
14 #define VALID_SPECS "ABCDFGIMPQRSTUWX"
16 #define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
18 static char *override_format[256];
20 static inline int valid_spec(int spec)
22 return strchr(VALID_SPECS, spec) != NULL;
25 void set_all_format_specs(char *option)
29 for (p = VALID_SPECS; *p; p++)
30 if (override_format[(int)(*p)] == NULL)
31 override_format[(int)(*p)] = strdup(option);
34 int add_format_spec(char *option)
38 if (!valid_spec(spec)) {
39 fprintf(stderr,"Bad format specifier %c\n", spec);
42 if (optarg[1] != ',') {
43 fprintf(stderr,"Bad format specifier - need ',' %s\n", option);
48 override_format[spec] = strdup(option);
53 static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
55 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
56 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
57 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
58 int m = t->action & BLK_TC_ACT(BLK_TC_META);
59 int d = t->action & BLK_TC_ACT(BLK_TC_DISCARD);
60 int f = t->action & BLK_TC_ACT(BLK_TC_FLUSH);
61 int u = t->action & BLK_TC_ACT(BLK_TC_FUA);
65 rwbs[i++] = 'F'; /* flush */
77 rwbs[i++] = 'F'; /* fua */
89 print_time(unsigned long long timestamp)
91 static char timebuf[128];
96 sec = abs_start_time.tv_sec + SECONDS(timestamp);
97 nsec = abs_start_time.tv_nsec + NANO_SECONDS(timestamp);
98 if (nsec >= 1000000000) {
103 tm = localtime(&sec);
104 snprintf(timebuf, sizeof(timebuf),
105 "%02u:%02u:%02u.%06lu",
113 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
115 static char zero[4096];
117 return !memcmp(pdu, zero, len);
120 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
125 if (!pdu_buf || !pdu_len)
128 for (len = 0, i = 0; i < pdu_len; i++) {
130 len += sprintf(p + len, " ");
132 len += sprintf(p + len, "%02x", pdu_buf[i]);
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
139 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
140 sprintf(p + len, " ..");
148 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
150 static unsigned int get_pdu_int(struct blk_io_trace *t)
152 __u64 *val = pdu_start(t);
154 return be64_to_cpu(*val);
157 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
159 struct blk_io_trace_remap *__r = pdu_start(t);
160 __u64 sector_from = __r->sector_from;
162 r->device_from = be32_to_cpu(__r->device_from);
163 r->device_to = be32_to_cpu(__r->device_to);
164 r->sector_from = be64_to_cpu(sector_from);
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)
176 sprintf(format, "%%-%d", width);
178 sprintf(format, "%%%d", width);
180 sprintf(format, "%%");
184 fprintf(ofp, strcat(format, "s"), act);
187 fprintf(ofp, strcat(format, "d"), pci->cpu);
190 char *name = find_process_name(t->pid);
192 fprintf(ofp, strcat(format, "s"), name);
199 fprintf(ofp, strcat(format, "s"), rwbs);
202 case 'D': /* format width ignored */
203 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
206 fprintf(ofp, strcat(format, "d"), t->error);
210 u32 ino = 0, gen = 0;
212 if (t->action & __BLK_TA_CGROUP) {
213 struct blk_io_cgroup_payload *cgid =
214 (struct blk_io_cgroup_payload *)pdu_buf;
219 sprintf(cgidstr, "%x,%x", ino, gen);
220 fprintf(ofp, strcat(format, "s"), cgidstr);
224 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
227 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
230 fprintf(ofp, strcat(format, "u"), t_sec(t));
233 fprintf(ofp, strcat(format, "u"), t->bytes);
236 fprintf(ofp, strcat(format, "u"), t->pid);
238 case 'P': { /* format width ignored */
239 char *p = dump_pdu(pdu_buf, pdu_len);
241 fprintf(ofp, "%s", p);
245 fprintf(ofp, strcat(format, "ld"), t->sequence);
248 fprintf(ofp, strcat(format, "lu"), t->sector);
251 sprintf(format, "%%0%dlu", has_w ? width : 9);
252 fprintf(ofp, format, NANO_SECONDS(t->time));
255 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
258 if (elapsed == -1ULL) {
259 fprintf(stderr, "Expecting elapsed value\n");
262 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
265 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
268 fprintf(ofp, strcat(format, "s"), print_time(t->time));
271 fprintf(ofp,strcat(format, "c"), field);
276 static char *parse_field(char *act, struct per_cpu_info *pci,
277 struct blk_io_trace *t, unsigned long long elapsed,
278 int pdu_len, unsigned char *pdu_buf,
279 char *primary_format)
284 char *p = primary_format;
293 width = (width * 10) + (*p++ - '0');
294 } while ((*p) && (isdigit(*p)));
297 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
298 minus, has_w, width);
303 static void process_default(char *act, struct per_cpu_info *pci,
304 struct blk_io_trace *t, unsigned long long elapsed,
305 int pdu_len, unsigned char *pdu_buf)
307 struct blk_io_trace_remap r = { .device_from = 0, };
314 * For remaps we have to modify the device using the remap structure
318 get_pdu_remap(t, &r);
319 t->device = r.device_to;
323 * The header is always the same
325 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
326 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
327 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
330 name = find_process_name(t->pid);
333 case 'R': /* Requeue */
334 case 'C': /* Complete */
335 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
336 char *p = dump_pdu(pdu_buf, pdu_len);
338 fprintf(ofp, "(%s) ", p);
339 fprintf(ofp, "[%d]\n", t->error);
341 if (elapsed != -1ULL) {
343 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
344 (unsigned long long) t->sector,
345 t_sec(t), elapsed, t->error);
347 fprintf(ofp, "%llu (%8llu) [%d]\n",
348 (unsigned long long) t->sector,
352 fprintf(ofp, "%llu + %u [%d]\n",
353 (unsigned long long) t->sector,
356 fprintf(ofp, "%llu [%d]\n",
357 (unsigned long long) t->sector,
363 case 'D': /* Issue */
364 case 'I': /* Insert */
365 case 'Q': /* Queue */
366 case 'B': /* Bounce */
367 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
369 fprintf(ofp, "%u ", t->bytes);
370 p = dump_pdu(pdu_buf, pdu_len);
372 fprintf(ofp, "(%s) ", p);
373 fprintf(ofp, "[%s]\n", name);
375 if (elapsed != -1ULL) {
377 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
378 (unsigned long long) t->sector,
379 t_sec(t), elapsed, name);
381 fprintf(ofp, "(%8llu) [%s]\n", elapsed,
385 fprintf(ofp, "%llu + %u [%s]\n",
386 (unsigned long long) t->sector,
389 fprintf(ofp, "[%s]\n", name);
394 case 'M': /* Back merge */
395 case 'F': /* Front merge */
396 case 'G': /* Get request */
397 case 'S': /* Sleep request */
399 fprintf(ofp, "%llu + %u [%s]\n",
400 (unsigned long long) t->sector, t_sec(t), name);
402 fprintf(ofp, "[%s]\n", name);
406 fprintf(ofp, "[%s]\n", name);
409 case 'U': /* Unplug IO */
410 case 'T': /* Unplug timer */
411 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
414 case 'A': /* remap */
415 get_pdu_remap(t, &r);
416 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
417 (unsigned long long) t->sector, t_sec(t),
418 MAJOR(r.device_from), MINOR(r.device_from),
419 (unsigned long long) r.sector_from);
422 case 'X': /* Split */
423 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
424 get_pdu_int(t), name);
427 case 'm': /* Message */
428 fprintf(ofp, "%*s\n", pdu_len, pdu_buf);
432 fprintf(stderr, "Unknown action %c\n", act[0]);
438 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
439 unsigned long long elapsed, int pdu_len,
440 unsigned char *pdu_buf)
442 char *p = override_format[(int) *act];
445 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
451 case '%': /* Field specifier */
454 fprintf(ofp, "%c", *p++);
456 fprintf(ofp, "%c", '%');
458 p = parse_field(act, pci, t, elapsed,
459 pdu_len, pdu_buf, p);
461 case '\\': { /* escape */
463 case 'b': fprintf(ofp, "\b"); break;
464 case 'n': fprintf(ofp, "\n"); break;
465 case 'r': fprintf(ofp, "\r"); break;
466 case 't': fprintf(ofp, "\t"); break;
469 "Invalid escape char in format %c\n",
478 fprintf(ofp, "%c", *p++);