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);
60 int d = t->action & BLK_TC_ACT(BLK_TC_DISCARD);
84 print_time(unsigned long long timestamp)
86 static char timebuf[128];
91 sec = abs_start_time.tv_sec + SECONDS(timestamp);
92 nsec = abs_start_time.tv_nsec + NANO_SECONDS(timestamp);
93 if (nsec >= 1000000000) {
99 snprintf(timebuf, sizeof(timebuf),
100 "%02u:%02u:%02u.%06lu",
108 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
110 static char zero[4096];
112 return !memcmp(pdu, zero, len);
115 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
120 if (!pdu_buf || !pdu_len)
123 for (len = 0, i = 0; i < pdu_len; i++) {
125 len += sprintf(p + len, " ");
127 len += sprintf(p + len, "%02x", pdu_buf[i]);
130 * usually dump for cdb dumps where we can see lots of
131 * zeroes, stop when the rest is just zeroes and indicate
132 * so with a .. appended
134 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
135 sprintf(p + len, " ..");
143 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
145 static unsigned int get_pdu_int(struct blk_io_trace *t)
147 __u64 *val = pdu_start(t);
149 return be64_to_cpu(*val);
152 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
154 struct blk_io_trace_remap *__r = pdu_start(t);
155 __u64 sector_from = __r->sector_from;
157 r->device_from = be32_to_cpu(__r->device_from);
158 r->device_to = be32_to_cpu(__r->device_to);
159 r->sector_from = be64_to_cpu(sector_from);
162 static void print_field(char *act, struct per_cpu_info *pci,
163 struct blk_io_trace *t, unsigned long long elapsed,
164 int pdu_len, unsigned char *pdu_buf, char field,
165 int minus, int has_w, int width)
171 sprintf(format, "%%-%d", width);
173 sprintf(format, "%%%d", width);
175 sprintf(format, "%%");
179 fprintf(ofp, strcat(format, "s"), act);
182 fprintf(ofp, strcat(format, "d"), pci->cpu);
185 char *name = find_process_name(t->pid);
187 fprintf(ofp, strcat(format, "s"), name);
194 fprintf(ofp, strcat(format, "s"), rwbs);
197 case 'D': /* format width ignored */
198 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
201 fprintf(ofp, strcat(format, "d"), t->error);
204 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
207 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
210 fprintf(ofp, strcat(format, "u"), t_sec(t));
213 fprintf(ofp, strcat(format, "u"), t->bytes);
216 fprintf(ofp, strcat(format, "u"), t->pid);
218 case 'P': { /* format width ignored */
219 char *p = dump_pdu(pdu_buf, pdu_len);
221 fprintf(ofp, "%s", p);
225 fprintf(ofp, strcat(format, "ld"), t->sequence);
228 fprintf(ofp, strcat(format, "lu"), t->sector);
231 sprintf(format, "%%0%dlu", has_w ? width : 9);
232 fprintf(ofp, format, NANO_SECONDS(t->time));
235 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
238 if (elapsed == -1ULL) {
239 fprintf(stderr, "Expecting elapsed value\n");
242 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
245 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
248 fprintf(ofp, strcat(format, "s"), print_time(t->time));
251 fprintf(ofp,strcat(format, "c"), field);
256 static char *parse_field(char *act, struct per_cpu_info *pci,
257 struct blk_io_trace *t, unsigned long long elapsed,
258 int pdu_len, unsigned char *pdu_buf,
264 char *p = master_format;
273 width = (width * 10) + (*p++ - '0');
274 } while ((*p) && (isdigit(*p)));
277 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
278 minus, has_w, width);
283 static void process_default(char *act, struct per_cpu_info *pci,
284 struct blk_io_trace *t, unsigned long long elapsed,
285 int pdu_len, unsigned char *pdu_buf)
287 struct blk_io_trace_remap r = { .device_from = 0, };
294 * For remaps we have to modify the device using the remap structure
298 get_pdu_remap(t, &r);
299 t->device = r.device_to;
303 * The header is always the same
305 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
306 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
307 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
310 name = find_process_name(t->pid);
313 case 'R': /* Requeue */
314 case 'C': /* Complete */
315 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
316 char *p = dump_pdu(pdu_buf, pdu_len);
318 fprintf(ofp, "(%s) ", p);
319 fprintf(ofp, "[%d]\n", t->error);
321 if (elapsed != -1ULL) {
323 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
324 (unsigned long long) t->sector,
325 t_sec(t), elapsed, t->error);
327 fprintf(ofp, "%llu (%8llu) [%d]\n",
328 (unsigned long long) t->sector,
332 fprintf(ofp, "%llu + %u [%d]\n",
333 (unsigned long long) t->sector,
336 fprintf(ofp, "%llu [%d]\n",
337 (unsigned long long) t->sector,
343 case 'D': /* Issue */
344 case 'I': /* Insert */
345 case 'Q': /* Queue */
346 case 'B': /* Bounce */
347 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
349 fprintf(ofp, "%u ", t->bytes);
350 p = dump_pdu(pdu_buf, pdu_len);
352 fprintf(ofp, "(%s) ", p);
353 fprintf(ofp, "[%s]\n", name);
355 if (elapsed != -1ULL) {
357 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
358 (unsigned long long) t->sector,
359 t_sec(t), elapsed, name);
361 fprintf(ofp, "(%8llu) [%s]\n", elapsed,
365 fprintf(ofp, "%llu + %u [%s]\n",
366 (unsigned long long) t->sector,
369 fprintf(ofp, "[%s]\n", name);
374 case 'M': /* Back merge */
375 case 'F': /* Front merge */
376 case 'G': /* Get request */
377 case 'S': /* Sleep request */
379 fprintf(ofp, "%llu + %u [%s]\n",
380 (unsigned long long) t->sector, t_sec(t), name);
382 fprintf(ofp, "[%s]\n", name);
386 fprintf(ofp, "[%s]\n", name);
389 case 'U': /* Unplug IO */
390 case 'T': /* Unplug timer */
391 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
394 case 'A': /* remap */
395 get_pdu_remap(t, &r);
396 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
397 (unsigned long long) t->sector, t_sec(t),
398 MAJOR(r.device_from), MINOR(r.device_from),
399 (unsigned long long) r.sector_from);
402 case 'X': /* Split */
403 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
404 get_pdu_int(t), name);
407 case 'm': /* Message */
408 fprintf(ofp, "%*s\n", pdu_len, pdu_buf);
412 fprintf(stderr, "Unknown action %c\n", act[0]);
418 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
419 unsigned long long elapsed, int pdu_len,
420 unsigned char *pdu_buf)
422 char *p = override_format[(int) *act];
425 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
431 case '%': /* Field specifier */
434 fprintf(ofp, "%c", *p++);
436 fprintf(ofp, "%c", '%');
438 p = parse_field(act, pci, t, elapsed,
439 pdu_len, pdu_buf, p);
441 case '\\': { /* escape */
443 case 'b': fprintf(ofp, "\b"); break;
444 case 'n': fprintf(ofp, "\n"); break;
445 case 'r': fprintf(ofp, "\r"); break;
446 case 't': fprintf(ofp, "\t"); break;
449 "Invalid escape char in format %c\n",
458 fprintf(ofp, "%c", *p++);