2 * This file contains format parsing code for blkparse, allowing you to
3 * customize the individual action format and generel output format.
13 #define VALID_SPECS "ABCDFGMPQRSTUWX"
15 #define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
17 static char *override_format[256];
19 static inline int valid_spec(int spec)
21 return strchr(VALID_SPECS, spec) != NULL;
24 void set_all_format_specs(char *option)
28 for (p = VALID_SPECS; *p; p++)
29 if (override_format[(int)(*p)] == NULL)
30 override_format[(int)(*p)] = strdup(option);
33 int add_format_spec(char *option)
37 if (!valid_spec(spec)) {
38 fprintf(stderr,"Bad format specifier %c\n", spec);
41 if (optarg[1] != ',') {
42 fprintf(stderr,"Bad format specifier - need ',' %s\n", option);
46 if (*option == '\0') {
47 fprintf(stderr,"Bad format specifier - need fmt %s\n", option);
52 * Set both merges (front and back)
55 override_format['B'] = strdup(option);
56 override_format['M'] = strdup(option);
58 override_format[spec] = strdup(option);
63 static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
65 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
66 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
67 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
68 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
69 int m = t->action & BLK_TC_ACT(BLK_TC_META);
88 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
90 static char zero[4096];
92 return !memcmp(pdu, zero, len);
95 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
100 if (!pdu_buf || !pdu_len)
103 for (len = 0, i = 0; i < pdu_len; i++) {
105 len += sprintf(p + len, " ");
107 len += sprintf(p + len, "%02x", pdu_buf[i]);
110 * usually dump for cdb dumps where we can see lots of
111 * zeroes, stop when the rest is just zeroes and indicate
112 * so with a .. appended
114 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
115 sprintf(p + len, " ..");
123 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
125 static unsigned int get_pdu_int(struct blk_io_trace *t)
127 __u64 *val = pdu_start(t);
129 return be64_to_cpu(*val);
132 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
134 struct blk_io_trace_remap *__r = pdu_start(t);
135 __u64 sector = __r->sector;
137 r->device = be32_to_cpu(__r->device);
138 r->device_from = be32_to_cpu(__r->device_from);
139 r->sector = be64_to_cpu(sector);
142 static void print_field(char *act, struct per_cpu_info *pci,
143 struct blk_io_trace *t, unsigned long long elapsed,
144 int pdu_len, unsigned char *pdu_buf, char field,
145 int minus, int has_w, int width)
151 sprintf(format, "%%-%d", width);
153 sprintf(format, "%%%d", width);
155 sprintf(format, "%%");
159 fprintf(ofp, strcat(format, "s"), act);
162 fprintf(ofp, strcat(format, "d"), pci->cpu);
165 char *name = find_process_name(t->pid);
167 fprintf(ofp, strcat(format, "s"), name);
174 fprintf(ofp, strcat(format, "s"), rwbs);
177 case 'D': /* format width ignored */
178 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
181 fprintf(ofp, strcat(format, "d"), t->error);
184 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
187 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
190 fprintf(ofp, strcat(format, "u"), t_sec(t));
193 fprintf(ofp, strcat(format, "u"), t->bytes);
196 fprintf(ofp, strcat(format, "u"), t->pid);
198 case 'P': { /* format width ignored */
199 char *p = dump_pdu(pdu_buf, pdu_len);
201 fprintf(ofp, "%s", p);
205 fprintf(ofp, strcat(format, "ld"), t->sequence);
208 fprintf(ofp, strcat(format, "lu"), t->sector);
211 sprintf(format, "%%0%dlu", has_w ? width : 9);
212 fprintf(ofp, format, NANO_SECONDS(t->time));
215 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
218 if (elapsed == -1ULL) {
219 fprintf(stderr, "Expecting elapsed value\n");
222 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
225 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
229 fprintf(ofp,strcat(format, "c"), field);
234 static char *parse_field(char *act, struct per_cpu_info *pci,
235 struct blk_io_trace *t, unsigned long long elapsed,
236 int pdu_len, unsigned char *pdu_buf,
242 char *p = master_format;
251 width = (width * 10) + (*p++ - '0');
252 } while ((*p) && (isdigit(*p)));
255 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
256 minus, has_w, width);
261 static void process_default(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)
265 struct blk_io_trace_remap r = { .device = 0, };
272 * The header is always the same
274 if (act[0] == 'A') { /* Remap */
275 get_pdu_remap(t, &r);
276 t->device = r.device_from;
279 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
280 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
281 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
284 name = find_process_name(t->pid);
287 case 'R': /* Requeue */
288 case 'C': /* Complete */
289 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
290 char *p = dump_pdu(pdu_buf, pdu_len);
292 fprintf(ofp, "(%s) ", p);
293 fprintf(ofp, "[%d]\n", t->error);
295 if (elapsed != -1ULL) {
296 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
297 (unsigned long long) t->sector,
298 t_sec(t), elapsed, t->error);
300 fprintf(ofp, "%llu + %u [%d]\n",
301 (unsigned long long) t->sector,
307 case 'D': /* Issue */
308 case 'I': /* Insert */
309 case 'Q': /* Queue */
310 case 'W': /* Bounce */
311 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
313 fprintf(ofp, "%u ", t->bytes);
314 p = dump_pdu(pdu_buf, pdu_len);
316 fprintf(ofp, "(%s) ", p);
317 fprintf(ofp, "[%s]\n", name);
319 if (elapsed != -1ULL) {
320 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
321 (unsigned long long) t->sector,
322 t_sec(t), elapsed, name);
324 fprintf(ofp, "%llu + %u [%s]\n",
325 (unsigned long long) t->sector,
331 case 'B': /* Back merge */
332 case 'F': /* Front merge */
333 case 'M': /* Front or back merge */
334 case 'G': /* Get request */
335 case 'S': /* Sleep request */
336 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
341 fprintf(ofp, "[%s]\n", name);
344 case 'U': /* Unplug IO */
345 case 'T': /* Unplug timer */
346 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
349 case 'A': /* remap */
350 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
351 (unsigned long long) t->sector, t_sec(t),
352 MAJOR(r.device), MINOR(r.device),
353 (unsigned long long) r.sector);
356 case 'X': /* Split */
357 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
358 get_pdu_int(t), name);
362 fprintf(stderr, "Unknown action %c\n", act[0]);
368 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
369 unsigned long long elapsed, int pdu_len,
370 unsigned char *pdu_buf)
372 char *p = override_format[(int) *act];
375 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
381 case '%': /* Field specifier */
384 fprintf(ofp, "%c", *p++);
386 fprintf(ofp, "%c", '%');
388 p = parse_field(act, pci, t, elapsed,
389 pdu_len, pdu_buf, p);
391 case '\\': { /* escape */
393 case 'b': fprintf(ofp, "\b"); break;
394 case 'n': fprintf(ofp, "\n"); break;
395 case 'r': fprintf(ofp, "\r"); break;
396 case 't': fprintf(ofp, "\t"); break;
399 "Invalid escape char in format %c\n",
408 fprintf(ofp, "%c", *p++);