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);
47 if (*option == '\0') {
48 fprintf(stderr,"Bad format specifier - need fmt %s\n", option);
52 override_format[spec] = strdup(option);
57 static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
59 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
60 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
61 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
62 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
63 int m = t->action & BLK_TC_ACT(BLK_TC_META);
85 print_time(unsigned long long timestamp)
87 static char timebuf[128];
92 sec = abs_start_time.tv_sec + SECONDS(timestamp);
93 nsec = abs_start_time.tv_nsec + NANO_SECONDS(timestamp);
94 if (nsec >= 1000000000) {
100 snprintf(timebuf, sizeof(timebuf),
101 "%02u:%02u:%02u.%06lu",
109 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
111 static char zero[4096];
113 return !memcmp(pdu, zero, len);
116 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
121 if (!pdu_buf || !pdu_len)
124 for (len = 0, i = 0; i < pdu_len; i++) {
126 len += sprintf(p + len, " ");
128 len += sprintf(p + len, "%02x", pdu_buf[i]);
131 * usually dump for cdb dumps where we can see lots of
132 * zeroes, stop when the rest is just zeroes and indicate
133 * so with a .. appended
135 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
136 sprintf(p + len, " ..");
144 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
146 static unsigned int get_pdu_int(struct blk_io_trace *t)
148 __u64 *val = pdu_start(t);
150 return be64_to_cpu(*val);
153 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
155 struct blk_io_trace_remap *__r = pdu_start(t);
156 __u64 sector = __r->sector;
158 r->device = be32_to_cpu(__r->device);
159 r->device_from = be32_to_cpu(__r->device_from);
160 r->sector = be64_to_cpu(sector);
163 static void print_field(char *act, struct per_cpu_info *pci,
164 struct blk_io_trace *t, unsigned long long elapsed,
165 int pdu_len, unsigned char *pdu_buf, char field,
166 int minus, int has_w, int width)
172 sprintf(format, "%%-%d", width);
174 sprintf(format, "%%%d", width);
176 sprintf(format, "%%");
180 fprintf(ofp, strcat(format, "s"), act);
183 fprintf(ofp, strcat(format, "d"), pci->cpu);
186 char *name = find_process_name(t->pid);
188 fprintf(ofp, strcat(format, "s"), name);
195 fprintf(ofp, strcat(format, "s"), rwbs);
198 case 'D': /* format width ignored */
199 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
202 fprintf(ofp, strcat(format, "d"), t->error);
205 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
208 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
211 fprintf(ofp, strcat(format, "u"), t_sec(t));
214 fprintf(ofp, strcat(format, "u"), t->bytes);
217 fprintf(ofp, strcat(format, "u"), t->pid);
219 case 'P': { /* format width ignored */
220 char *p = dump_pdu(pdu_buf, pdu_len);
222 fprintf(ofp, "%s", p);
226 fprintf(ofp, strcat(format, "ld"), t->sequence);
229 fprintf(ofp, strcat(format, "lu"), t->sector);
232 sprintf(format, "%%0%dlu", has_w ? width : 9);
233 fprintf(ofp, format, NANO_SECONDS(t->time));
236 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
239 if (elapsed == -1ULL) {
240 fprintf(stderr, "Expecting elapsed value\n");
243 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
246 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
249 fprintf(ofp, strcat(format, "s"), print_time(t->time));
252 fprintf(ofp,strcat(format, "c"), field);
257 static char *parse_field(char *act, struct per_cpu_info *pci,
258 struct blk_io_trace *t, unsigned long long elapsed,
259 int pdu_len, unsigned char *pdu_buf,
265 char *p = master_format;
274 width = (width * 10) + (*p++ - '0');
275 } while ((*p) && (isdigit(*p)));
278 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
279 minus, has_w, width);
284 static void process_default(char *act, struct per_cpu_info *pci,
285 struct blk_io_trace *t, unsigned long long elapsed,
286 int pdu_len, unsigned char *pdu_buf)
288 struct blk_io_trace_remap r = { .device = 0, };
295 * The header is always the same
297 if (act[0] == 'A') { /* Remap */
298 get_pdu_remap(t, &r);
299 t->device = r.device_from;
302 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
303 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
304 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
307 name = find_process_name(t->pid);
310 case 'R': /* Requeue */
311 case 'C': /* Complete */
312 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
313 char *p = dump_pdu(pdu_buf, pdu_len);
315 fprintf(ofp, "(%s) ", p);
316 fprintf(ofp, "[%d]\n", t->error);
318 if (elapsed != -1ULL) {
320 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
321 (unsigned long long) t->sector,
322 t_sec(t), elapsed, t->error);
324 fprintf(ofp, "%llu (%8llu) [%d]\n",
325 (unsigned long long) t->sector,
329 fprintf(ofp, "%llu + %u [%d]\n",
330 (unsigned long long) t->sector,
333 fprintf(ofp, "%llu [%d]\n",
334 (unsigned long long) t->sector,
340 case 'D': /* Issue */
341 case 'I': /* Insert */
342 case 'Q': /* Queue */
343 case 'B': /* Bounce */
344 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
346 fprintf(ofp, "%u ", t->bytes);
347 p = dump_pdu(pdu_buf, pdu_len);
349 fprintf(ofp, "(%s) ", p);
350 fprintf(ofp, "[%s]\n", name);
352 if (elapsed != -1ULL) {
354 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
355 (unsigned long long) t->sector,
356 t_sec(t), elapsed, name);
358 fprintf(ofp, "(%8llu) [%s]\n", elapsed,
362 fprintf(ofp, "%llu + %u [%s]\n",
363 (unsigned long long) t->sector,
366 fprintf(ofp, "[%s]\n", name);
371 case 'M': /* Back merge */
372 case 'F': /* Front merge */
373 case 'G': /* Get request */
374 case 'S': /* Sleep request */
376 fprintf(ofp, "%llu + %u [%s]\n",
377 (unsigned long long) t->sector, t_sec(t), name);
379 fprintf(ofp, "[%s]\n", name);
383 fprintf(ofp, "[%s]\n", name);
386 case 'U': /* Unplug IO */
387 case 'T': /* Unplug timer */
388 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
391 case 'A': /* remap */
392 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
393 (unsigned long long) t->sector, t_sec(t),
394 MAJOR(r.device), MINOR(r.device),
395 (unsigned long long) r.sector);
398 case 'X': /* Split */
399 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
400 get_pdu_int(t), name);
404 fprintf(stderr, "Unknown action %c\n", act[0]);
410 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
411 unsigned long long elapsed, int pdu_len,
412 unsigned char *pdu_buf)
414 char *p = override_format[(int) *act];
417 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
423 case '%': /* Field specifier */
426 fprintf(ofp, "%c", *p++);
428 fprintf(ofp, "%c", '%');
430 p = parse_field(act, pci, t, elapsed,
431 pdu_len, pdu_buf, p);
433 case '\\': { /* escape */
435 case 'b': fprintf(ofp, "\b"); break;
436 case 'n': fprintf(ofp, "\n"); break;
437 case 'r': fprintf(ofp, "\r"); break;
438 case 't': fprintf(ofp, "\t"); break;
441 "Invalid escape char in format %c\n",
450 fprintf(ofp, "%c", *p++);