[PATCH] email update
[blktrace.git] / blkparse_fmt.c
CommitLineData
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
17static char *override_format[256];
18
19static inline int valid_spec(int spec)
20{
21 return strchr(VALID_SPECS, spec) != NULL;
22}
23
e820abd7 24void 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 33int 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
63static 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);
711e4d25 69 int m = t->action & BLK_TC_ACT(BLK_TC_META);
768277cc
JA
70 int i = 0;
71
72 if (w)
73 rwbs[i++] = 'W';
74 else
75 rwbs[i++] = 'R';
f86990ed
NS
76 if (a)
77 rwbs[i++] = 'A';
768277cc
JA
78 if (b)
79 rwbs[i++] = 'B';
80 if (s)
81 rwbs[i++] = 'S';
711e4d25
JA
82 if (m)
83 rwbs[i++] = 'M';
768277cc
JA
84
85 rwbs[i] = '\0';
86}
87
63756b50 88static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
1d6376a5 89{
63756b50 90 static char zero[4096];
1d6376a5 91
63756b50 92 return !memcmp(pdu, zero, len);
1d6376a5
JA
93}
94
768277cc
JA
95static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
96{
97 static char p[4096];
98 int i, len;
99
100 if (!pdu_buf || !pdu_len)
101 return NULL;
102
103 for (len = 0, i = 0; i < pdu_len; i++) {
104 if (i)
105 len += sprintf(p + len, " ");
106
107 len += sprintf(p + len, "%02x", pdu_buf[i]);
1d6376a5
JA
108
109 /*
110 * usually dump for cdb dumps where we can see lots of
111 * zeroes, stop when the rest is just zeroes and indicate
112 * so with a .. appended
113 */
114 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
115 sprintf(p + len, " ..");
116 break;
117 }
768277cc
JA
118 }
119
120 return p;
121}
122
a8f30e64
JA
123#define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
124
768277cc
JA
125static unsigned int get_pdu_int(struct blk_io_trace *t)
126{
a8f30e64 127 __u64 *val = pdu_start(t);
768277cc
JA
128
129 return be64_to_cpu(*val);
130}
131
a8f30e64
JA
132static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
133{
134 struct blk_io_trace_remap *__r = pdu_start(t);
663af819 135 __u64 sector = __r->sector;
a8f30e64
JA
136
137 r->device = be32_to_cpu(__r->device);
663af819 138 r->sector = be64_to_cpu(sector);
a8f30e64
JA
139}
140
71d5d4c9
JA
141static void print_field(char *act, struct per_cpu_info *pci,
142 struct blk_io_trace *t, unsigned long long elapsed,
143 int pdu_len, unsigned char *pdu_buf, char field,
144 int minus, int has_w, int width)
145{
146 char format[64];
147
148 if (has_w) {
149 if (minus)
150 sprintf(format, "%%-%d", width);
151 else
152 sprintf(format, "%%%d", width);
153 } else
154 sprintf(format, "%%");
155
156 switch (field) {
157 case 'a':
158 fprintf(ofp, strcat(format, "s"), act);
159 break;
160 case 'c':
161 fprintf(ofp, strcat(format, "d"), pci->cpu);
162 break;
bfc70ad5
JA
163 case 'C': {
164 char *name = find_process_name(t->pid);
165
166 fprintf(ofp, strcat(format, "s"), name);
71d5d4c9 167 break;
bfc70ad5 168 }
71d5d4c9 169 case 'd': {
711e4d25 170 char rwbs[6];
768277cc
JA
171
172 fill_rwbs(rwbs, t);
71d5d4c9
JA
173 fprintf(ofp, strcat(format, "s"), rwbs);
174 break;
175 }
176 case 'D': /* format width ignored */
177 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
178 break;
179 case 'e':
180 fprintf(ofp, strcat(format, "d"), t->error);
181 break;
182 case 'M':
183 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
184 break;
185 case 'm':
186 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
187 break;
188 case 'n':
ae957cbc 189 fprintf(ofp, strcat(format, "u"), t_sec(t));
71d5d4c9 190 break;
1c8ca7b5
JA
191 case 'N':
192 fprintf(ofp, strcat(format, "u"), t->bytes);
193 break;
71d5d4c9
JA
194 case 'p':
195 fprintf(ofp, strcat(format, "u"), t->pid);
196 break;
768277cc
JA
197 case 'P': { /* format width ignored */
198 char *p = dump_pdu(pdu_buf, pdu_len);
199 if (p)
200 fprintf(ofp, "%s", p);
71d5d4c9 201 break;
768277cc 202 }
71d5d4c9
JA
203 case 's':
204 fprintf(ofp, strcat(format, "ld"), t->sequence);
205 break;
206 case 'S':
207 fprintf(ofp, strcat(format, "lu"), t->sector);
208 break;
209 case 't':
210 sprintf(format, "%%0%dlu", has_w ? width : 9);
211 fprintf(ofp, format, NANO_SECONDS(t->time));
212 break;
213 case 'T':
214 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
215 break;
216 case 'u':
217 if (elapsed == -1ULL) {
218 fprintf(stderr, "Expecting elapsed value\n");
219 exit(1);
220 }
221 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
222 break;
223 case 'U': {
768277cc 224 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
71d5d4c9
JA
225 break;
226 }
227 default:
228 fprintf(ofp,strcat(format, "c"), field);
229 break;
230 }
231}
232
768277cc
JA
233static char *parse_field(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,
71d5d4c9
JA
236 char *master_format)
237{
238 int minus = 0;
239 int has_w = 0;
240 int width = 0;
241 char *p = master_format;
242
243 if (*p == '-') {
244 minus = 1;
245 p++;
246 }
247 if (isdigit(*p)) {
248 has_w = 1;
249 do {
250 width = (width * 10) + (*p++ - '0');
251 } while ((*p) && (isdigit(*p)));
252 }
253 if (*p) {
254 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
255 minus, has_w, width);
256 }
257 return p;
258}
259
768277cc
JA
260static void process_default(char *act, struct per_cpu_info *pci,
261 struct blk_io_trace *t, unsigned long long elapsed,
262 int pdu_len, unsigned char *pdu_buf)
71d5d4c9 263{
711e4d25 264 char rwbs[6];
bfc70ad5 265 char *name;
71d5d4c9 266
768277cc
JA
267 fill_rwbs(rwbs, t);
268
269 /*
270 * The header is always the same
271 */
272 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
273 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
274 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
275 t->pid, act, rwbs);
71d5d4c9 276
bfc70ad5
JA
277 name = find_process_name(t->pid);
278
768277cc 279 switch (act[0]) {
80c53608 280 case 'R': /* Requeue */
71d5d4c9 281 case 'C': /* Complete */
768277cc
JA
282 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
283 char *p = dump_pdu(pdu_buf, pdu_len);
284 if (p)
285 fprintf(ofp, "(%s) ", p);
286 fprintf(ofp, "[%d]\n", t->error);
287 } else {
71d5d4c9 288 if (elapsed != -1ULL) {
768277cc
JA
289 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
290 (unsigned long long) t->sector,
ae957cbc 291 t_sec(t), elapsed, t->error);
768277cc
JA
292 } else {
293 fprintf(ofp, "%llu + %u [%d]\n",
294 (unsigned long long) t->sector,
ae957cbc 295 t_sec(t), t->error);
768277cc 296 }
71d5d4c9 297 }
71d5d4c9
JA
298 break;
299
300 case 'D': /* Issue */
71d5d4c9 301 case 'I': /* Insert */
71d5d4c9
JA
302 case 'Q': /* Queue */
303 case 'W': /* Bounce */
768277cc
JA
304 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
305 char *p;
1c8ca7b5 306 fprintf(ofp, "%u ", t->bytes);
768277cc
JA
307 p = dump_pdu(pdu_buf, pdu_len);
308 if (p)
309 fprintf(ofp, "(%s) ", p);
bfc70ad5 310 fprintf(ofp, "[%s]\n", name);
768277cc
JA
311 } else {
312 if (elapsed != -1ULL) {
313 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
314 (unsigned long long) t->sector,
bfc70ad5 315 t_sec(t), elapsed, name);
768277cc
JA
316 } else {
317 fprintf(ofp, "%llu + %u [%s]\n",
318 (unsigned long long) t->sector,
bfc70ad5 319 t_sec(t), name);
768277cc
JA
320 }
321 }
71d5d4c9
JA
322 break;
323
324 case 'B': /* Back merge */
325 case 'F': /* Front merge */
326 case 'M': /* Front or back merge */
768277cc
JA
327 case 'G': /* Get request */
328 case 'S': /* Sleep request */
329 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 330 t_sec(t), name);
71d5d4c9
JA
331 break;
332
333 case 'P': /* Plug */
bfc70ad5 334 fprintf(ofp, "[%s]\n", name);
71d5d4c9
JA
335 break;
336
337 case 'U': /* Unplug IO */
a8f30e64 338 case 'T': /* Unplug timer */
bfc70ad5 339 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
71d5d4c9
JA
340 break;
341
a8f30e64
JA
342 case 'A': { /* remap */
343 struct blk_io_trace_remap r;
344
345 get_pdu_remap(t, &r);
346 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
e9f883ca 347 (unsigned long long) t->sector, t_sec(t),
a8f30e64 348 MAJOR(r.device), MINOR(r.device),
e9f883ca 349 (unsigned long long) r.sector);
a8f30e64
JA
350 break;
351 }
352
71d5d4c9 353 case 'X': /* Split */
768277cc 354 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 355 get_pdu_int(t), name);
71d5d4c9
JA
356 break;
357
358 default:
768277cc
JA
359 fprintf(stderr, "Unknown action %c\n", act[0]);
360 break;
71d5d4c9
JA
361 }
362
71d5d4c9
JA
363}
364
365void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
366 unsigned long long elapsed, int pdu_len,
367 unsigned char *pdu_buf)
368{
768277cc
JA
369 char *p = override_format[(int) *act];
370
371 if (!p) {
372 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
373 return;
374 }
71d5d4c9
JA
375
376 while (*p) {
377 switch (*p) {
378 case '%': /* Field specifier */
379 p++;
380 if (*p == '%')
381 fprintf(ofp, "%c", *p++);
382 else if (!*p)
383 fprintf(ofp, "%c", '%');
384 else
385 p = parse_field(act, pci, t, elapsed,
386 pdu_len, pdu_buf, p);
387 break;
388 case '\\': { /* escape */
389 switch (p[1]) {
390 case 'b': fprintf(ofp, "\b"); break;
391 case 'n': fprintf(ofp, "\n"); break;
392 case 'r': fprintf(ofp, "\r"); break;
393 case 't': fprintf(ofp, "\t"); break;
394 default:
395 fprintf(stderr,
396 "Invalid escape char in format %c\n",
397 p[1]);
398 exit(1);
399 /*NOTREACHED*/
400 }
401 p += 2;
402 break;
403 }
404 default:
405 fprintf(ofp, "%c", *p++);
406 break;
407 }
408 }
409}
410
411