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 b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
58 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
59 int m = t->action & BLK_TC_ACT(BLK_TC_META);
81 print_time(unsigned long long timestamp)
83 static char timebuf[128];
88 sec = abs_start_time.tv_sec + SECONDS(timestamp);
89 nsec = abs_start_time.tv_nsec + NANO_SECONDS(timestamp);
90 if (nsec >= 1000000000) {
96 snprintf(timebuf, sizeof(timebuf),
97 "%02u:%02u:%02u.%06lu",
105 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
107 static char zero[4096];
109 return !memcmp(pdu, zero, len);
112 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
117 if (!pdu_buf || !pdu_len)
120 for (len = 0, i = 0; i < pdu_len; i++) {
122 len += sprintf(p + len, " ");
124 len += sprintf(p + len, "%02x", pdu_buf[i]);
127 * usually dump for cdb dumps where we can see lots of
128 * zeroes, stop when the rest is just zeroes and indicate
129 * so with a .. appended
131 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
132 sprintf(p + len, " ..");
140 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
142 static unsigned int get_pdu_int(struct blk_io_trace *t)
144 __u64 *val = pdu_start(t);
146 return be64_to_cpu(*val);
149 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
151 struct blk_io_trace_remap *__r = pdu_start(t);
152 __u64 sector = __r->sector;
154 r->device = be32_to_cpu(__r->device);
155 r->device_from = be32_to_cpu(__r->device_from);
156 r->sector = be64_to_cpu(sector);
159 static void print_field(char *act, struct per_cpu_info *pci,
160 struct blk_io_trace *t, unsigned long long elapsed,
161 int pdu_len, unsigned char *pdu_buf, char field,
162 int minus, int has_w, int width)
168 sprintf(format, "%%-%d", width);
170 sprintf(format, "%%%d", width);
172 sprintf(format, "%%");
176 fprintf(ofp, strcat(format, "s"), act);
179 fprintf(ofp, strcat(format, "d"), pci->cpu);
182 char *name = find_process_name(t->pid);
184 fprintf(ofp, strcat(format, "s"), name);
191 fprintf(ofp, strcat(format, "s"), rwbs);
194 case 'D': /* format width ignored */
195 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
198 fprintf(ofp, strcat(format, "d"), t->error);
201 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
204 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
207 fprintf(ofp, strcat(format, "u"), t_sec(t));
210 fprintf(ofp, strcat(format, "u"), t->bytes);
213 fprintf(ofp, strcat(format, "u"), t->pid);
215 case 'P': { /* format width ignored */
216 char *p = dump_pdu(pdu_buf, pdu_len);
218 fprintf(ofp, "%s", p);
222 fprintf(ofp, strcat(format, "ld"), t->sequence);
225 fprintf(ofp, strcat(format, "lu"), t->sector);
228 sprintf(format, "%%0%dlu", has_w ? width : 9);
229 fprintf(ofp, format, NANO_SECONDS(t->time));
232 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
235 if (elapsed == -1ULL) {
236 fprintf(stderr, "Expecting elapsed value\n");
239 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
242 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
245 fprintf(ofp, strcat(format, "s"), print_time(t->time));
248 fprintf(ofp,strcat(format, "c"), field);
253 static char *parse_field(char *act, struct per_cpu_info *pci,
254 struct blk_io_trace *t, unsigned long long elapsed,
255 int pdu_len, unsigned char *pdu_buf,
261 char *p = master_format;
270 width = (width * 10) + (*p++ - '0');
271 } while ((*p) && (isdigit(*p)));
274 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
275 minus, has_w, width);
280 static void process_default(char *act, struct per_cpu_info *pci,
281 struct blk_io_trace *t, unsigned long long elapsed,
282 int pdu_len, unsigned char *pdu_buf)
284 struct blk_io_trace_remap r = { .device = 0, };
291 * The header is always the same
293 if (act[0] == 'A') { /* Remap */
294 get_pdu_remap(t, &r);
295 t->device = r.device_from;
298 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
299 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
300 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
303 name = find_process_name(t->pid);
306 case 'R': /* Requeue */
307 case 'C': /* Complete */
308 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
309 char *p = dump_pdu(pdu_buf, pdu_len);
311 fprintf(ofp, "(%s) ", p);
312 fprintf(ofp, "[%d]\n", t->error);
314 if (elapsed != -1ULL) {
316 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
317 (unsigned long long) t->sector,
318 t_sec(t), elapsed, t->error);
320 fprintf(ofp, "%llu (%8llu) [%d]\n",
321 (unsigned long long) t->sector,
325 fprintf(ofp, "%llu + %u [%d]\n",
326 (unsigned long long) t->sector,
329 fprintf(ofp, "%llu [%d]\n",
330 (unsigned long long) t->sector,
336 case 'D': /* Issue */
337 case 'I': /* Insert */
338 case 'Q': /* Queue */
339 case 'B': /* Bounce */
340 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
342 fprintf(ofp, "%u ", t->bytes);
343 p = dump_pdu(pdu_buf, pdu_len);
345 fprintf(ofp, "(%s) ", p);
346 fprintf(ofp, "[%s]\n", name);
348 if (elapsed != -1ULL) {
350 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
351 (unsigned long long) t->sector,
352 t_sec(t), elapsed, name);
354 fprintf(ofp, "(%8llu) [%s]\n", elapsed,
358 fprintf(ofp, "%llu + %u [%s]\n",
359 (unsigned long long) t->sector,
362 fprintf(ofp, "[%s]\n", name);
367 case 'M': /* Back merge */
368 case 'F': /* Front merge */
369 case 'G': /* Get request */
370 case 'S': /* Sleep request */
372 fprintf(ofp, "%llu + %u [%s]\n",
373 (unsigned long long) t->sector, t_sec(t), name);
375 fprintf(ofp, "[%s]\n", name);
379 fprintf(ofp, "[%s]\n", name);
382 case 'U': /* Unplug IO */
383 case 'T': /* Unplug timer */
384 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
387 case 'A': /* remap */
388 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
389 (unsigned long long) t->sector, t_sec(t),
390 MAJOR(r.device), MINOR(r.device),
391 (unsigned long long) r.sector);
394 case 'X': /* Split */
395 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
396 get_pdu_int(t), name);
400 fprintf(stderr, "Unknown action %c\n", act[0]);
406 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
407 unsigned long long elapsed, int pdu_len,
408 unsigned char *pdu_buf)
410 char *p = override_format[(int) *act];
413 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
419 case '%': /* Field specifier */
422 fprintf(ofp, "%c", *p++);
424 fprintf(ofp, "%c", '%');
426 p = parse_field(act, pci, t, elapsed,
427 pdu_len, pdu_buf, p);
429 case '\\': { /* escape */
431 case 'b': fprintf(ofp, "\b"); break;
432 case 'n': fprintf(ofp, "\n"); break;
433 case 'r': fprintf(ofp, "\r"); break;
434 case 't': fprintf(ofp, "\t"); break;
437 "Invalid escape char in format %c\n",
446 fprintf(ofp, "%c", *p++);