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);
83 print_time(unsigned long long timestamp)
85 static char timebuf[128];
90 sec = abs_start_time.tv_sec + SECONDS(timestamp);
91 nsec = abs_start_time.tv_nsec + NANO_SECONDS(timestamp);
92 if (nsec >= 1000000000) {
98 snprintf(timebuf, sizeof(timebuf),
99 "%02u:%02u:%02u.%06lu",
107 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
109 static char zero[4096];
111 return !memcmp(pdu, zero, len);
114 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
119 if (!pdu_buf || !pdu_len)
122 for (len = 0, i = 0; i < pdu_len; i++) {
124 len += sprintf(p + len, " ");
126 len += sprintf(p + len, "%02x", pdu_buf[i]);
129 * usually dump for cdb dumps where we can see lots of
130 * zeroes, stop when the rest is just zeroes and indicate
131 * so with a .. appended
133 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
134 sprintf(p + len, " ..");
142 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
144 static unsigned int get_pdu_int(struct blk_io_trace *t)
146 __u64 *val = pdu_start(t);
148 return be64_to_cpu(*val);
151 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
153 struct blk_io_trace_remap *__r = pdu_start(t);
154 __u64 sector = __r->sector;
156 r->device = be32_to_cpu(__r->device);
157 r->device_from = be32_to_cpu(__r->device_from);
158 r->sector = be64_to_cpu(sector);
161 static void print_field(char *act, struct per_cpu_info *pci,
162 struct blk_io_trace *t, unsigned long long elapsed,
163 int pdu_len, unsigned char *pdu_buf, char field,
164 int minus, int has_w, int width)
170 sprintf(format, "%%-%d", width);
172 sprintf(format, "%%%d", width);
174 sprintf(format, "%%");
178 fprintf(ofp, strcat(format, "s"), act);
181 fprintf(ofp, strcat(format, "d"), pci->cpu);
184 char *name = find_process_name(t->pid);
186 fprintf(ofp, strcat(format, "s"), name);
193 fprintf(ofp, strcat(format, "s"), rwbs);
196 case 'D': /* format width ignored */
197 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
200 fprintf(ofp, strcat(format, "d"), t->error);
203 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
206 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
209 fprintf(ofp, strcat(format, "u"), t_sec(t));
212 fprintf(ofp, strcat(format, "u"), t->bytes);
215 fprintf(ofp, strcat(format, "u"), t->pid);
217 case 'P': { /* format width ignored */
218 char *p = dump_pdu(pdu_buf, pdu_len);
220 fprintf(ofp, "%s", p);
224 fprintf(ofp, strcat(format, "ld"), t->sequence);
227 fprintf(ofp, strcat(format, "lu"), t->sector);
230 sprintf(format, "%%0%dlu", has_w ? width : 9);
231 fprintf(ofp, format, NANO_SECONDS(t->time));
234 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
237 if (elapsed == -1ULL) {
238 fprintf(stderr, "Expecting elapsed value\n");
241 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
244 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
247 fprintf(ofp, strcat(format, "s"), print_time(t->time));
250 fprintf(ofp,strcat(format, "c"), field);
255 static char *parse_field(char *act, struct per_cpu_info *pci,
256 struct blk_io_trace *t, unsigned long long elapsed,
257 int pdu_len, unsigned char *pdu_buf,
263 char *p = master_format;
272 width = (width * 10) + (*p++ - '0');
273 } while ((*p) && (isdigit(*p)));
276 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
277 minus, has_w, width);
282 static void process_default(char *act, struct per_cpu_info *pci,
283 struct blk_io_trace *t, unsigned long long elapsed,
284 int pdu_len, unsigned char *pdu_buf)
286 struct blk_io_trace_remap r = { .device = 0, };
293 * The header is always the same
295 if (act[0] == 'A') { /* Remap */
296 get_pdu_remap(t, &r);
297 t->device = r.device_from;
300 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
301 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
302 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
305 name = find_process_name(t->pid);
308 case 'R': /* Requeue */
309 case 'C': /* Complete */
310 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
311 char *p = dump_pdu(pdu_buf, pdu_len);
313 fprintf(ofp, "(%s) ", p);
314 fprintf(ofp, "[%d]\n", t->error);
316 if (elapsed != -1ULL) {
317 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
318 (unsigned long long) t->sector,
319 t_sec(t), elapsed, t->error);
321 fprintf(ofp, "%llu + %u [%d]\n",
322 (unsigned long long) t->sector,
328 case 'D': /* Issue */
329 case 'I': /* Insert */
330 case 'Q': /* Queue */
331 case 'B': /* Bounce */
332 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
334 fprintf(ofp, "%u ", t->bytes);
335 p = dump_pdu(pdu_buf, pdu_len);
337 fprintf(ofp, "(%s) ", p);
338 fprintf(ofp, "[%s]\n", name);
340 if (elapsed != -1ULL) {
341 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
342 (unsigned long long) t->sector,
343 t_sec(t), elapsed, name);
345 fprintf(ofp, "%llu + %u [%s]\n",
346 (unsigned long long) t->sector,
352 case 'M': /* Back merge */
353 case 'F': /* Front merge */
354 case 'G': /* Get request */
355 case 'S': /* Sleep request */
356 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
361 fprintf(ofp, "[%s]\n", name);
364 case 'U': /* Unplug IO */
365 case 'T': /* Unplug timer */
366 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
369 case 'A': /* remap */
370 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
371 (unsigned long long) t->sector, t_sec(t),
372 MAJOR(r.device), MINOR(r.device),
373 (unsigned long long) r.sector);
376 case 'X': /* Split */
377 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
378 get_pdu_int(t), name);
382 fprintf(stderr, "Unknown action %c\n", act[0]);
388 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
389 unsigned long long elapsed, int pdu_len,
390 unsigned char *pdu_buf)
392 char *p = override_format[(int) *act];
395 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
401 case '%': /* Field specifier */
404 fprintf(ofp, "%c", *p++);
406 fprintf(ofp, "%c", '%');
408 p = parse_field(act, pci, t, elapsed,
409 pdu_len, pdu_buf, p);
411 case '\\': { /* escape */
413 case 'b': fprintf(ofp, "\b"); break;
414 case 'n': fprintf(ofp, "\n"); break;
415 case 'r': fprintf(ofp, "\r"); break;
416 case 't': fprintf(ofp, "\t"); break;
419 "Invalid escape char in format %c\n",
428 fprintf(ofp, "%c", *p++);