Commit | Line | Data |
---|---|---|
71d5d4c9 JA |
1 | /* |
2 | * This file contains format parsing code for blkparse, allowing you to | |
3 | * customize the individual action format and generel output format. | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <string.h> | |
7 | #include <stdlib.h> | |
8 | #include <unistd.h> | |
9 | #include <ctype.h> | |
10 | ||
11 | #include "blktrace.h" | |
12 | ||
a8f30e64 | 13 | #define VALID_SPECS "ABCDFGMPQRSTUWX" |
71d5d4c9 JA |
14 | |
15 | #define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d " | |
16 | ||
17 | static char *override_format[256]; | |
18 | ||
19 | static inline int valid_spec(int spec) | |
20 | { | |
21 | return strchr(VALID_SPECS, spec) != NULL; | |
22 | } | |
23 | ||
e820abd7 | 24 | void set_all_format_specs(char *option) |
71d5d4c9 JA |
25 | { |
26 | char *p; | |
27 | ||
28 | for (p = VALID_SPECS; *p; p++) | |
29 | if (override_format[(int)(*p)] == NULL) | |
e820abd7 | 30 | override_format[(int)(*p)] = strdup(option); |
71d5d4c9 JA |
31 | } |
32 | ||
e820abd7 | 33 | int add_format_spec(char *option) |
71d5d4c9 JA |
34 | { |
35 | int spec = optarg[0]; | |
36 | ||
37 | if (!valid_spec(spec)) { | |
38 | fprintf(stderr,"Bad format specifier %c\n", spec); | |
39 | return 1; | |
40 | } | |
41 | if (optarg[1] != ',') { | |
e820abd7 | 42 | fprintf(stderr,"Bad format specifier - need ',' %s\n", option); |
71d5d4c9 JA |
43 | return 1; |
44 | } | |
e820abd7 JA |
45 | option += 2; |
46 | if (*option == '\0') { | |
47 | fprintf(stderr,"Bad format specifier - need fmt %s\n", option); | |
71d5d4c9 JA |
48 | return 1; |
49 | } | |
50 | ||
51 | /* | |
52 | * Set both merges (front and back) | |
53 | */ | |
54 | if (spec == 'M') { | |
e820abd7 JA |
55 | override_format['B'] = strdup(option); |
56 | override_format['M'] = strdup(option); | |
71d5d4c9 | 57 | } else |
e820abd7 | 58 | override_format[spec] = strdup(option); |
71d5d4c9 JA |
59 | |
60 | return 0; | |
61 | } | |
62 | ||
768277cc JA |
63 | static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t) |
64 | { | |
65 | int w = t->action & BLK_TC_ACT(BLK_TC_WRITE); | |
f86990ed | 66 | int a = t->action & BLK_TC_ACT(BLK_TC_AHEAD); |
768277cc JA |
67 | int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER); |
68 | int s = t->action & BLK_TC_ACT(BLK_TC_SYNC); | |
69 | int i = 0; | |
70 | ||
71 | if (w) | |
72 | rwbs[i++] = 'W'; | |
73 | else | |
74 | rwbs[i++] = 'R'; | |
f86990ed NS |
75 | if (a) |
76 | rwbs[i++] = 'A'; | |
768277cc JA |
77 | if (b) |
78 | rwbs[i++] = 'B'; | |
79 | if (s) | |
80 | rwbs[i++] = 'S'; | |
81 | ||
82 | rwbs[i] = '\0'; | |
83 | } | |
84 | ||
63756b50 | 85 | static inline int pdu_rest_is_zero(unsigned char *pdu, int len) |
1d6376a5 | 86 | { |
63756b50 | 87 | static char zero[4096]; |
1d6376a5 | 88 | |
63756b50 | 89 | return !memcmp(pdu, zero, len); |
1d6376a5 JA |
90 | } |
91 | ||
768277cc JA |
92 | static char *dump_pdu(unsigned char *pdu_buf, int pdu_len) |
93 | { | |
94 | static char p[4096]; | |
95 | int i, len; | |
96 | ||
97 | if (!pdu_buf || !pdu_len) | |
98 | return NULL; | |
99 | ||
100 | for (len = 0, i = 0; i < pdu_len; i++) { | |
101 | if (i) | |
102 | len += sprintf(p + len, " "); | |
103 | ||
104 | len += sprintf(p + len, "%02x", pdu_buf[i]); | |
1d6376a5 JA |
105 | |
106 | /* | |
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 | |
110 | */ | |
111 | if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) { | |
112 | sprintf(p + len, " .."); | |
113 | break; | |
114 | } | |
768277cc JA |
115 | } |
116 | ||
117 | return p; | |
118 | } | |
119 | ||
a8f30e64 JA |
120 | #define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace))) |
121 | ||
768277cc JA |
122 | static unsigned int get_pdu_int(struct blk_io_trace *t) |
123 | { | |
a8f30e64 | 124 | __u64 *val = pdu_start(t); |
768277cc JA |
125 | |
126 | return be64_to_cpu(*val); | |
127 | } | |
128 | ||
a8f30e64 JA |
129 | static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r) |
130 | { | |
131 | struct blk_io_trace_remap *__r = pdu_start(t); | |
663af819 | 132 | __u64 sector = __r->sector; |
a8f30e64 JA |
133 | |
134 | r->device = be32_to_cpu(__r->device); | |
663af819 | 135 | r->sector = be64_to_cpu(sector); |
a8f30e64 JA |
136 | } |
137 | ||
71d5d4c9 JA |
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) | |
142 | { | |
143 | char format[64]; | |
144 | ||
145 | if (has_w) { | |
146 | if (minus) | |
147 | sprintf(format, "%%-%d", width); | |
148 | else | |
149 | sprintf(format, "%%%d", width); | |
150 | } else | |
151 | sprintf(format, "%%"); | |
152 | ||
153 | switch (field) { | |
154 | case 'a': | |
155 | fprintf(ofp, strcat(format, "s"), act); | |
156 | break; | |
157 | case 'c': | |
158 | fprintf(ofp, strcat(format, "d"), pci->cpu); | |
159 | break; | |
bfc70ad5 JA |
160 | case 'C': { |
161 | char *name = find_process_name(t->pid); | |
162 | ||
163 | fprintf(ofp, strcat(format, "s"), name); | |
71d5d4c9 | 164 | break; |
bfc70ad5 | 165 | } |
71d5d4c9 JA |
166 | case 'd': { |
167 | char rwbs[4]; | |
768277cc JA |
168 | |
169 | fill_rwbs(rwbs, t); | |
71d5d4c9 JA |
170 | fprintf(ofp, strcat(format, "s"), rwbs); |
171 | break; | |
172 | } | |
173 | case 'D': /* format width ignored */ | |
174 | fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device)); | |
175 | break; | |
176 | case 'e': | |
177 | fprintf(ofp, strcat(format, "d"), t->error); | |
178 | break; | |
179 | case 'M': | |
180 | fprintf(ofp, strcat(format, "d"), MAJOR(t->device)); | |
181 | break; | |
182 | case 'm': | |
183 | fprintf(ofp, strcat(format, "d"), MINOR(t->device)); | |
184 | break; | |
185 | case 'n': | |
ae957cbc | 186 | fprintf(ofp, strcat(format, "u"), t_sec(t)); |
71d5d4c9 | 187 | break; |
1c8ca7b5 JA |
188 | case 'N': |
189 | fprintf(ofp, strcat(format, "u"), t->bytes); | |
190 | break; | |
71d5d4c9 JA |
191 | case 'p': |
192 | fprintf(ofp, strcat(format, "u"), t->pid); | |
193 | break; | |
768277cc JA |
194 | case 'P': { /* format width ignored */ |
195 | char *p = dump_pdu(pdu_buf, pdu_len); | |
196 | if (p) | |
197 | fprintf(ofp, "%s", p); | |
71d5d4c9 | 198 | break; |
768277cc | 199 | } |
71d5d4c9 JA |
200 | case 's': |
201 | fprintf(ofp, strcat(format, "ld"), t->sequence); | |
202 | break; | |
203 | case 'S': | |
204 | fprintf(ofp, strcat(format, "lu"), t->sector); | |
205 | break; | |
206 | case 't': | |
207 | sprintf(format, "%%0%dlu", has_w ? width : 9); | |
208 | fprintf(ofp, format, NANO_SECONDS(t->time)); | |
209 | break; | |
210 | case 'T': | |
211 | fprintf(ofp, strcat(format, "d"), SECONDS(t->time)); | |
212 | break; | |
213 | case 'u': | |
214 | if (elapsed == -1ULL) { | |
215 | fprintf(stderr, "Expecting elapsed value\n"); | |
216 | exit(1); | |
217 | } | |
218 | fprintf(ofp, strcat(format, "llu"), elapsed / 1000); | |
219 | break; | |
220 | case 'U': { | |
768277cc | 221 | fprintf(ofp, strcat(format, "u"), get_pdu_int(t)); |
71d5d4c9 JA |
222 | break; |
223 | } | |
224 | default: | |
225 | fprintf(ofp,strcat(format, "c"), field); | |
226 | break; | |
227 | } | |
228 | } | |
229 | ||
768277cc JA |
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, | |
71d5d4c9 JA |
233 | char *master_format) |
234 | { | |
235 | int minus = 0; | |
236 | int has_w = 0; | |
237 | int width = 0; | |
238 | char *p = master_format; | |
239 | ||
240 | if (*p == '-') { | |
241 | minus = 1; | |
242 | p++; | |
243 | } | |
244 | if (isdigit(*p)) { | |
245 | has_w = 1; | |
246 | do { | |
247 | width = (width * 10) + (*p++ - '0'); | |
248 | } while ((*p) && (isdigit(*p))); | |
249 | } | |
250 | if (*p) { | |
251 | print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++, | |
252 | minus, has_w, width); | |
253 | } | |
254 | return p; | |
255 | } | |
256 | ||
768277cc JA |
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) | |
71d5d4c9 | 260 | { |
768277cc | 261 | char rwbs[4]; |
bfc70ad5 | 262 | char *name; |
71d5d4c9 | 263 | |
768277cc JA |
264 | fill_rwbs(rwbs, t); |
265 | ||
266 | /* | |
267 | * The header is always the same | |
268 | */ | |
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), | |
272 | t->pid, act, rwbs); | |
71d5d4c9 | 273 | |
bfc70ad5 JA |
274 | name = find_process_name(t->pid); |
275 | ||
768277cc | 276 | switch (act[0]) { |
80c53608 | 277 | case 'R': /* Requeue */ |
71d5d4c9 | 278 | case 'C': /* Complete */ |
768277cc JA |
279 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) { |
280 | char *p = dump_pdu(pdu_buf, pdu_len); | |
281 | if (p) | |
282 | fprintf(ofp, "(%s) ", p); | |
283 | fprintf(ofp, "[%d]\n", t->error); | |
284 | } else { | |
71d5d4c9 | 285 | if (elapsed != -1ULL) { |
768277cc JA |
286 | fprintf(ofp, "%llu + %u (%8llu) [%d]\n", |
287 | (unsigned long long) t->sector, | |
ae957cbc | 288 | t_sec(t), elapsed, t->error); |
768277cc JA |
289 | } else { |
290 | fprintf(ofp, "%llu + %u [%d]\n", | |
291 | (unsigned long long) t->sector, | |
ae957cbc | 292 | t_sec(t), t->error); |
768277cc | 293 | } |
71d5d4c9 | 294 | } |
71d5d4c9 JA |
295 | break; |
296 | ||
297 | case 'D': /* Issue */ | |
71d5d4c9 | 298 | case 'I': /* Insert */ |
71d5d4c9 JA |
299 | case 'Q': /* Queue */ |
300 | case 'W': /* Bounce */ | |
768277cc JA |
301 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) { |
302 | char *p; | |
1c8ca7b5 | 303 | fprintf(ofp, "%u ", t->bytes); |
768277cc JA |
304 | p = dump_pdu(pdu_buf, pdu_len); |
305 | if (p) | |
306 | fprintf(ofp, "(%s) ", p); | |
bfc70ad5 | 307 | fprintf(ofp, "[%s]\n", name); |
768277cc JA |
308 | } else { |
309 | if (elapsed != -1ULL) { | |
310 | fprintf(ofp, "%llu + %u (%8llu) [%s]\n", | |
311 | (unsigned long long) t->sector, | |
bfc70ad5 | 312 | t_sec(t), elapsed, name); |
768277cc JA |
313 | } else { |
314 | fprintf(ofp, "%llu + %u [%s]\n", | |
315 | (unsigned long long) t->sector, | |
bfc70ad5 | 316 | t_sec(t), name); |
768277cc JA |
317 | } |
318 | } | |
71d5d4c9 JA |
319 | break; |
320 | ||
321 | case 'B': /* Back merge */ | |
322 | case 'F': /* Front merge */ | |
323 | case 'M': /* Front or back merge */ | |
768277cc JA |
324 | case 'G': /* Get request */ |
325 | case 'S': /* Sleep request */ | |
326 | fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector, | |
bfc70ad5 | 327 | t_sec(t), name); |
71d5d4c9 JA |
328 | break; |
329 | ||
330 | case 'P': /* Plug */ | |
bfc70ad5 | 331 | fprintf(ofp, "[%s]\n", name); |
71d5d4c9 JA |
332 | break; |
333 | ||
334 | case 'U': /* Unplug IO */ | |
a8f30e64 | 335 | case 'T': /* Unplug timer */ |
bfc70ad5 | 336 | fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t)); |
71d5d4c9 JA |
337 | break; |
338 | ||
a8f30e64 JA |
339 | case 'A': { /* remap */ |
340 | struct blk_io_trace_remap r; | |
341 | ||
342 | get_pdu_remap(t, &r); | |
343 | fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n", | |
e9f883ca | 344 | (unsigned long long) t->sector, t_sec(t), |
a8f30e64 | 345 | MAJOR(r.device), MINOR(r.device), |
e9f883ca | 346 | (unsigned long long) r.sector); |
a8f30e64 JA |
347 | break; |
348 | } | |
349 | ||
71d5d4c9 | 350 | case 'X': /* Split */ |
768277cc | 351 | fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector, |
bfc70ad5 | 352 | get_pdu_int(t), name); |
71d5d4c9 JA |
353 | break; |
354 | ||
355 | default: | |
768277cc JA |
356 | fprintf(stderr, "Unknown action %c\n", act[0]); |
357 | break; | |
71d5d4c9 JA |
358 | } |
359 | ||
71d5d4c9 JA |
360 | } |
361 | ||
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) | |
365 | { | |
768277cc JA |
366 | char *p = override_format[(int) *act]; |
367 | ||
368 | if (!p) { | |
369 | process_default(act, pci, t, elapsed, pdu_len, pdu_buf); | |
370 | return; | |
371 | } | |
71d5d4c9 JA |
372 | |
373 | while (*p) { | |
374 | switch (*p) { | |
375 | case '%': /* Field specifier */ | |
376 | p++; | |
377 | if (*p == '%') | |
378 | fprintf(ofp, "%c", *p++); | |
379 | else if (!*p) | |
380 | fprintf(ofp, "%c", '%'); | |
381 | else | |
382 | p = parse_field(act, pci, t, elapsed, | |
383 | pdu_len, pdu_buf, p); | |
384 | break; | |
385 | case '\\': { /* escape */ | |
386 | switch (p[1]) { | |
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; | |
391 | default: | |
392 | fprintf(stderr, | |
393 | "Invalid escape char in format %c\n", | |
394 | p[1]); | |
395 | exit(1); | |
396 | /*NOTREACHED*/ | |
397 | } | |
398 | p += 2; | |
399 | break; | |
400 | } | |
401 | default: | |
402 | fprintf(ofp, "%c", *p++); | |
403 | break; | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 |