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);
209 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
212 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
215 fprintf(ofp, strcat(format, "u"), t_sec(t));
218 fprintf(ofp, strcat(format, "u"), t->bytes);
221 fprintf(ofp, strcat(format, "u"), t->pid);
223 case 'P': { /* format width ignored */
224 char *p = dump_pdu(pdu_buf, pdu_len);
226 fprintf(ofp, "%s", p);
230 fprintf(ofp, strcat(format, "ld"), t->sequence);
233 fprintf(ofp, strcat(format, "lu"), t->sector);
236 sprintf(format, "%%0%dlu", has_w ? width : 9);
237 fprintf(ofp, format, NANO_SECONDS(t->time));
240 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
243 if (elapsed == -1ULL) {
244 fprintf(stderr, "Expecting elapsed value\n");
247 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
250 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
253 fprintf(ofp, strcat(format, "s"), print_time(t->time));
256 fprintf(ofp,strcat(format, "c"), field);
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,
269 char *p = master_format;
278 width = (width * 10) + (*p++ - '0');
279 } while ((*p) && (isdigit(*p)));
282 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
283 minus, has_w, width);
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)
292 struct blk_io_trace_remap r = { .device_from = 0, };
299 * For remaps we have to modify the device using the remap structure
303 get_pdu_remap(t, &r);
304 t->device = r.device_to;
308 * The header is always the same
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),
315 name = find_process_name(t->pid);
318 case 'R': /* Requeue */
319 case 'C': /* Complete */
320 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
321 char *p = dump_pdu(pdu_buf, pdu_len);
323 fprintf(ofp, "(%s) ", p);
324 fprintf(ofp, "[%d]\n", t->error);
326 if (elapsed != -1ULL) {
328 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
329 (unsigned long long) t->sector,
330 t_sec(t), elapsed, t->error);
332 fprintf(ofp, "%llu (%8llu) [%d]\n",
333 (unsigned long long) t->sector,
337 fprintf(ofp, "%llu + %u [%d]\n",
338 (unsigned long long) t->sector,
341 fprintf(ofp, "%llu [%d]\n",
342 (unsigned long long) t->sector,
348 case 'D': /* Issue */
349 case 'I': /* Insert */
350 case 'Q': /* Queue */
351 case 'B': /* Bounce */
352 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
354 fprintf(ofp, "%u ", t->bytes);
355 p = dump_pdu(pdu_buf, pdu_len);
357 fprintf(ofp, "(%s) ", p);
358 fprintf(ofp, "[%s]\n", name);
360 if (elapsed != -1ULL) {
362 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
363 (unsigned long long) t->sector,
364 t_sec(t), elapsed, name);
366 fprintf(ofp, "(%8llu) [%s]\n", elapsed,
370 fprintf(ofp, "%llu + %u [%s]\n",
371 (unsigned long long) t->sector,
374 fprintf(ofp, "[%s]\n", name);
379 case 'M': /* Back merge */
380 case 'F': /* Front merge */
381 case 'G': /* Get request */
382 case 'S': /* Sleep request */
384 fprintf(ofp, "%llu + %u [%s]\n",
385 (unsigned long long) t->sector, t_sec(t), name);
387 fprintf(ofp, "[%s]\n", name);
391 fprintf(ofp, "[%s]\n", name);
394 case 'U': /* Unplug IO */
395 case 'T': /* Unplug timer */
396 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
399 case 'A': /* remap */
400 get_pdu_remap(t, &r);
401 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
402 (unsigned long long) t->sector, t_sec(t),
403 MAJOR(r.device_from), MINOR(r.device_from),
404 (unsigned long long) r.sector_from);
407 case 'X': /* Split */
408 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
409 get_pdu_int(t), name);
412 case 'm': /* Message */
413 fprintf(ofp, "%*s\n", pdu_len, pdu_buf);
417 fprintf(stderr, "Unknown action %c\n", act[0]);
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)
427 char *p = override_format[(int) *act];
430 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
436 case '%': /* Field specifier */
439 fprintf(ofp, "%c", *p++);
441 fprintf(ofp, "%c", '%');
443 p = parse_field(act, pci, t, elapsed,
444 pdu_len, pdu_buf, p);
446 case '\\': { /* escape */
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;
454 "Invalid escape char in format %c\n",
463 fprintf(ofp, "%c", *p++);