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);
85 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
87 static char zero[4096];
89 return !memcmp(pdu, zero, len);
92 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
97 if (!pdu_buf || !pdu_len)
100 for (len = 0, i = 0; i < pdu_len; i++) {
102 len += sprintf(p + len, " ");
104 len += sprintf(p + len, "%02x", pdu_buf[i]);
107 * usually dump for cdb dumps where we can see lots of
108 * zeroes, stop when the rest is just zeroes and indicate
109 * so with a .. appended
111 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
112 sprintf(p + len, " ..");
120 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
122 static unsigned int get_pdu_int(struct blk_io_trace *t)
124 __u64 *val = pdu_start(t);
126 return be64_to_cpu(*val);
129 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
131 struct blk_io_trace_remap *__r = pdu_start(t);
132 __u64 sector = __r->sector;
134 r->device = be32_to_cpu(__r->device);
135 r->sector = be64_to_cpu(sector);
138 static void print_field(char *act, struct per_cpu_info *pci,
139 struct blk_io_trace *t, unsigned long long elapsed,
140 int pdu_len, unsigned char *pdu_buf, char field,
141 int minus, int has_w, int width)
147 sprintf(format, "%%-%d", width);
149 sprintf(format, "%%%d", width);
151 sprintf(format, "%%");
155 fprintf(ofp, strcat(format, "s"), act);
158 fprintf(ofp, strcat(format, "d"), pci->cpu);
161 char *name = find_process_name(t->pid);
163 fprintf(ofp, strcat(format, "s"), name);
170 fprintf(ofp, strcat(format, "s"), rwbs);
173 case 'D': /* format width ignored */
174 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
177 fprintf(ofp, strcat(format, "d"), t->error);
180 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
183 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
186 fprintf(ofp, strcat(format, "u"), t_sec(t));
189 fprintf(ofp, strcat(format, "u"), t->bytes);
192 fprintf(ofp, strcat(format, "u"), t->pid);
194 case 'P': { /* format width ignored */
195 char *p = dump_pdu(pdu_buf, pdu_len);
197 fprintf(ofp, "%s", p);
201 fprintf(ofp, strcat(format, "ld"), t->sequence);
204 fprintf(ofp, strcat(format, "lu"), t->sector);
207 sprintf(format, "%%0%dlu", has_w ? width : 9);
208 fprintf(ofp, format, NANO_SECONDS(t->time));
211 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
214 if (elapsed == -1ULL) {
215 fprintf(stderr, "Expecting elapsed value\n");
218 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
221 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
225 fprintf(ofp,strcat(format, "c"), field);
230 static char *parse_field(char *act, struct per_cpu_info *pci,
231 struct blk_io_trace *t, unsigned long long elapsed,
232 int pdu_len, unsigned char *pdu_buf,
238 char *p = master_format;
247 width = (width * 10) + (*p++ - '0');
248 } while ((*p) && (isdigit(*p)));
251 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
252 minus, has_w, width);
257 static void process_default(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)
267 * The header is always the same
269 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
270 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
271 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
274 name = find_process_name(t->pid);
277 case 'R': /* Requeue */
278 case 'C': /* Complete */
279 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
280 char *p = dump_pdu(pdu_buf, pdu_len);
282 fprintf(ofp, "(%s) ", p);
283 fprintf(ofp, "[%d]\n", t->error);
285 if (elapsed != -1ULL) {
286 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
287 (unsigned long long) t->sector,
288 t_sec(t), elapsed, t->error);
290 fprintf(ofp, "%llu + %u [%d]\n",
291 (unsigned long long) t->sector,
297 case 'D': /* Issue */
298 case 'I': /* Insert */
299 case 'Q': /* Queue */
300 case 'W': /* Bounce */
301 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
303 fprintf(ofp, "%u ", t->bytes);
304 p = dump_pdu(pdu_buf, pdu_len);
306 fprintf(ofp, "(%s) ", p);
307 fprintf(ofp, "[%s]\n", name);
309 if (elapsed != -1ULL) {
310 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
311 (unsigned long long) t->sector,
312 t_sec(t), elapsed, name);
314 fprintf(ofp, "%llu + %u [%s]\n",
315 (unsigned long long) t->sector,
321 case 'B': /* Back merge */
322 case 'F': /* Front merge */
323 case 'M': /* Front or back merge */
324 case 'G': /* Get request */
325 case 'S': /* Sleep request */
326 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
331 fprintf(ofp, "[%s]\n", name);
334 case 'U': /* Unplug IO */
335 case 'T': /* Unplug timer */
336 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
339 case 'A': { /* remap */
340 struct blk_io_trace_remap r;
342 get_pdu_remap(t, &r);
343 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
344 (unsigned long long) t->sector, t_sec(t),
345 MAJOR(r.device), MINOR(r.device),
346 (unsigned long long) r.sector);
350 case 'X': /* Split */
351 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
352 get_pdu_int(t), name);
356 fprintf(stderr, "Unknown action %c\n", act[0]);
362 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
363 unsigned long long elapsed, int pdu_len,
364 unsigned char *pdu_buf)
366 char *p = override_format[(int) *act];
369 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
375 case '%': /* Field specifier */
378 fprintf(ofp, "%c", *p++);
380 fprintf(ofp, "%c", '%');
382 p = parse_field(act, pci, t, elapsed,
383 pdu_len, pdu_buf, p);
385 case '\\': { /* escape */
387 case 'b': fprintf(ofp, "\b"); break;
388 case 'n': fprintf(ofp, "\n"); break;
389 case 'r': fprintf(ofp, "\r"); break;
390 case 't': fprintf(ofp, "\t"); break;
393 "Invalid escape char in format %c\n",
402 fprintf(ofp, "%c", *p++);