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 b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
67 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
82 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
87 if (!pdu_buf || !pdu_len)
90 for (len = 0, i = 0; i < pdu_len; i++) {
92 len += sprintf(p + len, " ");
94 len += sprintf(p + len, "%02x", pdu_buf[i]);
100 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
102 static unsigned int get_pdu_int(struct blk_io_trace *t)
104 __u64 *val = pdu_start(t);
106 return be64_to_cpu(*val);
109 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
111 struct blk_io_trace_remap *__r = pdu_start(t);
113 r->device = be32_to_cpu(__r->device);
114 r->sector = be64_to_cpu(__r->sector);
117 static void print_field(char *act, struct per_cpu_info *pci,
118 struct blk_io_trace *t, unsigned long long elapsed,
119 int pdu_len, unsigned char *pdu_buf, char field,
120 int minus, int has_w, int width)
126 sprintf(format, "%%-%d", width);
128 sprintf(format, "%%%d", width);
130 sprintf(format, "%%");
134 fprintf(ofp, strcat(format, "s"), act);
137 fprintf(ofp, strcat(format, "d"), pci->cpu);
140 fprintf(ofp, strcat(format, "s"), t->comm);
146 fprintf(ofp, strcat(format, "s"), rwbs);
149 case 'D': /* format width ignored */
150 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
153 fprintf(ofp, strcat(format, "d"), t->error);
156 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
159 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
162 fprintf(ofp, strcat(format, "u"), t_sec(t));
165 fprintf(ofp, strcat(format, "u"), t->bytes);
168 fprintf(ofp, strcat(format, "u"), t->pid);
170 case 'P': { /* format width ignored */
171 char *p = dump_pdu(pdu_buf, pdu_len);
173 fprintf(ofp, "%s", p);
177 fprintf(ofp, strcat(format, "ld"), t->sequence);
180 fprintf(ofp, strcat(format, "lu"), t->sector);
183 sprintf(format, "%%0%dlu", has_w ? width : 9);
184 fprintf(ofp, format, NANO_SECONDS(t->time));
187 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
190 if (elapsed == -1ULL) {
191 fprintf(stderr, "Expecting elapsed value\n");
194 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
197 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
201 fprintf(ofp,strcat(format, "c"), field);
206 static char *parse_field(char *act, struct per_cpu_info *pci,
207 struct blk_io_trace *t, unsigned long long elapsed,
208 int pdu_len, unsigned char *pdu_buf,
214 char *p = master_format;
223 width = (width * 10) + (*p++ - '0');
224 } while ((*p) && (isdigit(*p)));
227 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
228 minus, has_w, width);
233 static void process_default(char *act, struct per_cpu_info *pci,
234 struct blk_io_trace *t, unsigned long long elapsed,
235 int pdu_len, unsigned char *pdu_buf)
242 * The header is always the same
244 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
245 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
246 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
250 case 'C': /* Complete */
251 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
252 char *p = dump_pdu(pdu_buf, pdu_len);
254 fprintf(ofp, "(%s) ", p);
255 fprintf(ofp, "[%d]\n", t->error);
257 if (elapsed != -1ULL) {
258 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
259 (unsigned long long) t->sector,
260 t_sec(t), elapsed, t->error);
262 fprintf(ofp, "%llu + %u [%d]\n",
263 (unsigned long long) t->sector,
269 case 'D': /* Issue */
270 case 'I': /* Insert */
271 case 'Q': /* Queue */
272 case 'W': /* Bounce */
273 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
275 fprintf(ofp, "%u ", t->bytes);
276 p = dump_pdu(pdu_buf, pdu_len);
278 fprintf(ofp, "(%s) ", p);
279 fprintf(ofp, "[%s]\n", t->comm);
281 if (elapsed != -1ULL) {
282 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
283 (unsigned long long) t->sector,
284 t_sec(t), elapsed, t->comm);
286 fprintf(ofp, "%llu + %u [%s]\n",
287 (unsigned long long) t->sector,
293 case 'B': /* Back merge */
294 case 'F': /* Front merge */
295 case 'M': /* Front or back merge */
296 case 'G': /* Get request */
297 case 'S': /* Sleep request */
298 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
303 fprintf(ofp, "[%s]\n", t->comm);
306 case 'U': /* Unplug IO */
307 case 'T': /* Unplug timer */
308 fprintf(ofp, "[%s] %u\n", t->comm, get_pdu_int(t));
311 case 'A': { /* remap */
312 struct blk_io_trace_remap r;
314 get_pdu_remap(t, &r);
315 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
316 (unsigned long long) r.sector, t_sec(t),
317 MAJOR(r.device), MINOR(r.device),
318 (unsigned long long) t->sector);
322 case 'X': /* Split */
323 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
324 get_pdu_int(t), t->comm);
328 fprintf(stderr, "Unknown action %c\n", act[0]);
334 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
335 unsigned long long elapsed, int pdu_len,
336 unsigned char *pdu_buf)
338 char *p = override_format[(int) *act];
341 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
347 case '%': /* Field specifier */
350 fprintf(ofp, "%c", *p++);
352 fprintf(ofp, "%c", '%');
354 p = parse_field(act, pci, t, elapsed,
355 pdu_len, pdu_buf, p);
357 case '\\': { /* escape */
359 case 'b': fprintf(ofp, "\b"); break;
360 case 'n': fprintf(ofp, "\n"); break;
361 case 'r': fprintf(ofp, "\r"); break;
362 case 't': fprintf(ofp, "\t"); break;
365 "Invalid escape char in format %c\n",
374 fprintf(ofp, "%c", *p++);