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);
53 * Set both merges (front and back)
56 override_format['B'] = strdup(option);
57 override_format['M'] = strdup(option);
59 override_format[spec] = strdup(option);
64 static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
66 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
67 int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD);
68 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
69 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
70 int m = t->action & BLK_TC_ACT(BLK_TC_META);
90 print_time(unsigned long long timestamp)
92 static char timebuf[128];
97 sec = abs_start_time.tv_sec + SECONDS(timestamp);
98 nsec = abs_start_time.tv_nsec + NANO_SECONDS(timestamp);
99 if (nsec >= 1000000000) {
104 tm = localtime(&sec);
105 snprintf(timebuf, sizeof(timebuf),
106 "%02u:%02u:%02u.%06lu",
114 static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
116 static char zero[4096];
118 return !memcmp(pdu, zero, len);
121 static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
126 if (!pdu_buf || !pdu_len)
129 for (len = 0, i = 0; i < pdu_len; i++) {
131 len += sprintf(p + len, " ");
133 len += sprintf(p + len, "%02x", pdu_buf[i]);
136 * usually dump for cdb dumps where we can see lots of
137 * zeroes, stop when the rest is just zeroes and indicate
138 * so with a .. appended
140 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
141 sprintf(p + len, " ..");
149 #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
151 static unsigned int get_pdu_int(struct blk_io_trace *t)
153 __u64 *val = pdu_start(t);
155 return be64_to_cpu(*val);
158 static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
160 struct blk_io_trace_remap *__r = pdu_start(t);
161 __u64 sector = __r->sector;
163 r->device = be32_to_cpu(__r->device);
164 r->device_from = be32_to_cpu(__r->device_from);
165 r->sector = be64_to_cpu(sector);
168 static void print_field(char *act, struct per_cpu_info *pci,
169 struct blk_io_trace *t, unsigned long long elapsed,
170 int pdu_len, unsigned char *pdu_buf, char field,
171 int minus, int has_w, int width)
177 sprintf(format, "%%-%d", width);
179 sprintf(format, "%%%d", width);
181 sprintf(format, "%%");
185 fprintf(ofp, strcat(format, "s"), act);
188 fprintf(ofp, strcat(format, "d"), pci->cpu);
191 char *name = find_process_name(t->pid);
193 fprintf(ofp, strcat(format, "s"), name);
200 fprintf(ofp, strcat(format, "s"), rwbs);
203 case 'D': /* format width ignored */
204 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
207 fprintf(ofp, strcat(format, "d"), t->error);
210 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
213 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
216 fprintf(ofp, strcat(format, "u"), t_sec(t));
219 fprintf(ofp, strcat(format, "u"), t->bytes);
222 fprintf(ofp, strcat(format, "u"), t->pid);
224 case 'P': { /* format width ignored */
225 char *p = dump_pdu(pdu_buf, pdu_len);
227 fprintf(ofp, "%s", p);
231 fprintf(ofp, strcat(format, "ld"), t->sequence);
234 fprintf(ofp, strcat(format, "lu"), t->sector);
237 sprintf(format, "%%0%dlu", has_w ? width : 9);
238 fprintf(ofp, format, NANO_SECONDS(t->time));
241 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
244 if (elapsed == -1ULL) {
245 fprintf(stderr, "Expecting elapsed value\n");
248 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
251 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
254 fprintf(ofp, strcat(format, "s"), print_time(t->time));
257 fprintf(ofp,strcat(format, "c"), field);
262 static char *parse_field(char *act, struct per_cpu_info *pci,
263 struct blk_io_trace *t, unsigned long long elapsed,
264 int pdu_len, unsigned char *pdu_buf,
270 char *p = master_format;
279 width = (width * 10) + (*p++ - '0');
280 } while ((*p) && (isdigit(*p)));
283 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
284 minus, has_w, width);
289 static void process_default(char *act, struct per_cpu_info *pci,
290 struct blk_io_trace *t, unsigned long long elapsed,
291 int pdu_len, unsigned char *pdu_buf)
293 struct blk_io_trace_remap r = { .device = 0, };
300 * The header is always the same
302 if (act[0] == 'A') { /* Remap */
303 get_pdu_remap(t, &r);
304 t->device = r.device_from;
307 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
308 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
309 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
312 name = find_process_name(t->pid);
315 case 'R': /* Requeue */
316 case 'C': /* Complete */
317 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
318 char *p = dump_pdu(pdu_buf, pdu_len);
320 fprintf(ofp, "(%s) ", p);
321 fprintf(ofp, "[%d]\n", t->error);
323 if (elapsed != -1ULL) {
324 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
325 (unsigned long long) t->sector,
326 t_sec(t), elapsed, t->error);
328 fprintf(ofp, "%llu + %u [%d]\n",
329 (unsigned long long) t->sector,
335 case 'D': /* Issue */
336 case 'I': /* Insert */
337 case 'Q': /* Queue */
338 case 'B': /* Bounce */
339 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
341 fprintf(ofp, "%u ", t->bytes);
342 p = dump_pdu(pdu_buf, pdu_len);
344 fprintf(ofp, "(%s) ", p);
345 fprintf(ofp, "[%s]\n", name);
347 if (elapsed != -1ULL) {
348 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
349 (unsigned long long) t->sector,
350 t_sec(t), elapsed, name);
352 fprintf(ofp, "%llu + %u [%s]\n",
353 (unsigned long long) t->sector,
359 case 'M': /* Back merge */
360 case 'F': /* Front merge */
361 case 'G': /* Get request */
362 case 'S': /* Sleep request */
363 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
368 fprintf(ofp, "[%s]\n", name);
371 case 'U': /* Unplug IO */
372 case 'T': /* Unplug timer */
373 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
376 case 'A': /* remap */
377 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
378 (unsigned long long) t->sector, t_sec(t),
379 MAJOR(r.device), MINOR(r.device),
380 (unsigned long long) r.sector);
383 case 'X': /* Split */
384 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
385 get_pdu_int(t), name);
389 fprintf(stderr, "Unknown action %c\n", act[0]);
395 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
396 unsigned long long elapsed, int pdu_len,
397 unsigned char *pdu_buf)
399 char *p = override_format[(int) *act];
402 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
408 case '%': /* Field specifier */
411 fprintf(ofp, "%c", *p++);
413 fprintf(ofp, "%c", '%');
415 p = parse_field(act, pci, t, elapsed,
416 pdu_len, pdu_buf, p);
418 case '\\': { /* escape */
420 case 'b': fprintf(ofp, "\b"); break;
421 case 'n': fprintf(ofp, "\n"); break;
422 case 'r': fprintf(ofp, "\r"); break;
423 case 't': fprintf(ofp, "\t"); break;
426 "Invalid escape char in format %c\n",
435 fprintf(ofp, "%c", *p++);