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