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 "BCDFGMPQRSTU"
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 *optarg)
28 for (p = VALID_SPECS; *p; p++)
29 if (override_format[(int)(*p)] == NULL)
30 override_format[(int)(*p)] = strdup(optarg);
33 int add_format_spec(char *optarg)
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", optarg);
46 if (*optarg == '\0') {
47 fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
52 * Set both merges (front and back)
55 override_format['B'] = strdup(optarg);
56 override_format['M'] = strdup(optarg);
58 override_format[spec] = strdup(optarg);
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 static unsigned int get_pdu_int(struct blk_io_trace *t)
102 __u64 *val = (__u64 *) ((char *) t + sizeof(*t));
104 return be64_to_cpu(*val);
107 static void print_field(char *act, struct per_cpu_info *pci,
108 struct blk_io_trace *t, unsigned long long elapsed,
109 int pdu_len, unsigned char *pdu_buf, char field,
110 int minus, int has_w, int width)
116 sprintf(format, "%%-%d", width);
118 sprintf(format, "%%%d", width);
120 sprintf(format, "%%");
124 fprintf(ofp, strcat(format, "s"), act);
127 fprintf(ofp, strcat(format, "d"), pci->cpu);
130 fprintf(ofp, strcat(format, "s"), t->comm);
136 fprintf(ofp, strcat(format, "s"), rwbs);
139 case 'D': /* format width ignored */
140 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
143 fprintf(ofp, strcat(format, "d"), t->error);
146 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
149 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
152 fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
155 fprintf(ofp, strcat(format, "u"), t->bytes);
158 fprintf(ofp, strcat(format, "u"), t->pid);
160 case 'P': { /* format width ignored */
161 char *p = dump_pdu(pdu_buf, pdu_len);
163 fprintf(ofp, "%s", p);
167 fprintf(ofp, strcat(format, "ld"), t->sequence);
170 fprintf(ofp, strcat(format, "lu"), t->sector);
173 sprintf(format, "%%0%dlu", has_w ? width : 9);
174 fprintf(ofp, format, NANO_SECONDS(t->time));
177 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
180 if (elapsed == -1ULL) {
181 fprintf(stderr, "Expecting elapsed value\n");
184 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
187 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
191 fprintf(ofp,strcat(format, "c"), field);
196 static char *parse_field(char *act, struct per_cpu_info *pci,
197 struct blk_io_trace *t, unsigned long long elapsed,
198 int pdu_len, unsigned char *pdu_buf,
204 char *p = master_format;
213 width = (width * 10) + (*p++ - '0');
214 } while ((*p) && (isdigit(*p)));
217 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
218 minus, has_w, width);
223 static void process_default(char *act, struct per_cpu_info *pci,
224 struct blk_io_trace *t, unsigned long long elapsed,
225 int pdu_len, unsigned char *pdu_buf)
232 * The header is always the same
234 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
235 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
236 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
240 case 'C': /* Complete */
241 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
242 char *p = dump_pdu(pdu_buf, pdu_len);
244 fprintf(ofp, "(%s) ", p);
245 fprintf(ofp, "[%d]\n", t->error);
247 if (elapsed != -1ULL) {
248 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
249 (unsigned long long) t->sector,
250 t->bytes >> 9, elapsed, t->error);
252 fprintf(ofp, "%llu + %u [%d]\n",
253 (unsigned long long) t->sector,
254 t->bytes >> 9, t->error);
259 case 'D': /* Issue */
260 case 'I': /* Insert */
261 case 'Q': /* Queue */
262 case 'W': /* Bounce */
263 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
265 fprintf(ofp, "%u ", t->bytes);
266 p = dump_pdu(pdu_buf, pdu_len);
268 fprintf(ofp, "(%s) ", p);
269 fprintf(ofp, "[%s]\n", t->comm);
271 if (elapsed != -1ULL) {
272 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
273 (unsigned long long) t->sector,
274 t->bytes >> 9, elapsed, t->comm);
276 fprintf(ofp, "%llu + %u [%s]\n",
277 (unsigned long long) t->sector,
278 t->bytes >> 9, t->comm);
283 case 'B': /* Back merge */
284 case 'F': /* Front merge */
285 case 'M': /* Front or back merge */
286 case 'G': /* Get request */
287 case 'S': /* Sleep request */
288 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
289 t->bytes >> 9, t->comm);
293 fprintf(ofp, "[%s]\n", t->comm);
296 case 'U': /* Unplug IO */
297 case 'T': { /* Unplug timer */
298 fprintf(ofp, "[%s] %u\n", t->comm, get_pdu_int(t));
302 case 'X': /* Split */
303 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
304 get_pdu_int(t), t->comm);
308 fprintf(stderr, "Unknown action %c\n", act[0]);
314 void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
315 unsigned long long elapsed, int pdu_len,
316 unsigned char *pdu_buf)
318 char *p = override_format[(int) *act];
321 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
327 case '%': /* Field specifier */
330 fprintf(ofp, "%c", *p++);
332 fprintf(ofp, "%c", '%');
334 p = parse_field(act, pci, t, elapsed,
335 pdu_len, pdu_buf, p);
337 case '\\': { /* escape */
339 case 'b': fprintf(ofp, "\b"); break;
340 case 'n': fprintf(ofp, "\n"); break;
341 case 'r': fprintf(ofp, "\r"); break;
342 case 't': fprintf(ofp, "\t"); break;
345 "Invalid escape char in format %c\n",
354 fprintf(ofp, "%c", *p++);