summaryrefslogtreecommitdiff
path: root/blkparse_fmt.c
blob: 8fe9c19196c559f527ee02e9373a8f069cc1f6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * This file contains format parsing code for blkparse, allowing you to
 * customize the individual action format and generel output format.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#include "blktrace.h"

#define VALID_SPECS	"ABCDFGMPQRSTUWX"

#define HEADER		"%D %2c %8s %5T.%9t %5p %2a %3d "

static char *override_format[256];

static inline int valid_spec(int spec)
{
	return strchr(VALID_SPECS, spec) != NULL;
}

void set_all_format_specs(char *option)
{
	char *p;

	for (p = VALID_SPECS; *p; p++)
		if (override_format[(int)(*p)] == NULL)
			override_format[(int)(*p)] = strdup(option);
}

int add_format_spec(char *option)
{
	int spec = optarg[0];

	if (!valid_spec(spec)) {
		fprintf(stderr,"Bad format specifier %c\n", spec);
		return 1;
	}
	if (optarg[1] != ',') {
		fprintf(stderr,"Bad format specifier - need ',' %s\n", option);
		return 1;
	}
	option += 2;
	if (*option == '\0') {
		fprintf(stderr,"Bad format specifier - need fmt %s\n", option);
		return 1;
	}

	/*
	 * Set both merges (front and back)
	 */
	if (spec == 'M') {
		override_format['B'] = strdup(option);
		override_format['M'] = strdup(option);
	} else
		override_format[spec] = strdup(option);

	return 0;
}

static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
{
	int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
	int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
	int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
	int i = 0;

	if (w)
		rwbs[i++] = 'W';
	else
		rwbs[i++] = 'R';
	if (b)
		rwbs[i++] = 'B';
	if (s)
		rwbs[i++] = 'S';

	rwbs[i] = '\0';
}

static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
{
	static char zero[4096];

	return !memcmp(pdu, zero, len);
}

static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
{
	static char p[4096];
	int i, len;

	if (!pdu_buf || !pdu_len)
		return NULL;

	for (len = 0, i = 0; i < pdu_len; i++) {
		if (i)
			len += sprintf(p + len, " ");

		len += sprintf(p + len, "%02x", pdu_buf[i]);

		/*
		 * usually dump for cdb dumps where we can see lots of
		 * zeroes, stop when the rest is just zeroes and indicate
		 * so with a .. appended
		 */
		if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
			sprintf(p + len, " ..");
			break;
		}
	}

	return p;
}

#define pdu_start(t)	(((void *) (t) + sizeof(struct blk_io_trace)))

static unsigned int get_pdu_int(struct blk_io_trace *t)
{
	__u64 *val = pdu_start(t);

	return be64_to_cpu(*val);
}

static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
{
	struct blk_io_trace_remap *__r = pdu_start(t);
	__u64 sector = __r->sector;

	r->device = be32_to_cpu(__r->device);
	r->sector = be64_to_cpu(sector);
}

static void print_field(char *act, struct per_cpu_info *pci,
			struct blk_io_trace *t, unsigned long long elapsed,
			int pdu_len, unsigned char *pdu_buf, char field,
			int minus, int has_w, int width)
{
	char format[64];

	if (has_w) {
		if (minus)
			sprintf(format, "%%-%d", width);
		else
			sprintf(format, "%%%d", width);
	} else
		sprintf(format, "%%");

	switch (field) {
	case 'a':
		fprintf(ofp, strcat(format, "s"), act);
		break;
	case 'c':
		fprintf(ofp, strcat(format, "d"), pci->cpu);
		break;
	case 'C':
		fprintf(ofp, strcat(format, "s"), t->comm);
		break;
	case 'd': {
		char rwbs[4];

		fill_rwbs(rwbs, t);
		fprintf(ofp, strcat(format, "s"), rwbs);
		break;
	}
	case 'D':	/* format width ignored */
		fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
		break;
	case 'e':
		fprintf(ofp, strcat(format, "d"), t->error);
		break;
	case 'M':
		fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
		break;
	case 'm':
		fprintf(ofp, strcat(format, "d"), MINOR(t->device));
		break;
	case 'n':
		fprintf(ofp, strcat(format, "u"), t_sec(t));
		break;
	case 'N':
		fprintf(ofp, strcat(format, "u"), t->bytes);
		break;
	case 'p':
		fprintf(ofp, strcat(format, "u"), t->pid);
		break;
	case 'P': { /* format width ignored */
		char *p = dump_pdu(pdu_buf, pdu_len);
		if (p)
			fprintf(ofp, "%s", p);
		break;
	}
	case 's':
		fprintf(ofp, strcat(format, "ld"), t->sequence);
		break;
	case 'S':
		fprintf(ofp, strcat(format, "lu"), t->sector);
		break;
	case 't':
		sprintf(format, "%%0%dlu", has_w ? width : 9);
		fprintf(ofp, format, NANO_SECONDS(t->time));
		break;
	case 'T':
		fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
		break;
	case 'u':
		if (elapsed == -1ULL) {
			fprintf(stderr, "Expecting elapsed value\n");
			exit(1);
		}
		fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
		break;
	case 'U': {
		fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
		break;
	}
	default:
		fprintf(ofp,strcat(format, "c"), field);
		break;
	}
}

static char *parse_field(char *act, struct per_cpu_info *pci,
			 struct blk_io_trace *t, unsigned long long elapsed,
			 int pdu_len, unsigned char *pdu_buf,
			 char *master_format)
{
	int minus = 0;
	int has_w = 0;
	int width = 0;
	char *p = master_format;

	if (*p == '-') {
		minus = 1;
		p++;
	}
	if (isdigit(*p)) {
		has_w = 1;
		do {
			width = (width * 10) + (*p++ - '0');
		} while ((*p) && (isdigit(*p)));
	}
	if (*p) {
		print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
			    minus, has_w, width);
	}
	return p;
}

static void process_default(char *act, struct per_cpu_info *pci,
			    struct blk_io_trace *t, unsigned long long elapsed,
			    int pdu_len, unsigned char *pdu_buf)
{
	char rwbs[4];

	fill_rwbs(rwbs, t);

	/*
	 * The header is always the same
	 */
	fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
		MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
		(int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
		t->pid, act, rwbs);

	switch (act[0]) {
	case 'R':	/* Requeue */
	case 'C': 	/* Complete */
		if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
			char *p = dump_pdu(pdu_buf, pdu_len);
			if (p)
				fprintf(ofp, "(%s) ", p);
			fprintf(ofp, "[%d]\n", t->error);
		} else {
			if (elapsed != -1ULL) {
				fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
					(unsigned long long) t->sector,
					t_sec(t), elapsed, t->error);
			} else {
				fprintf(ofp, "%llu + %u [%d]\n",
					(unsigned long long) t->sector,
					t_sec(t), t->error);
			}
		}
		break;

	case 'D': 	/* Issue */
	case 'I': 	/* Insert */
	case 'Q': 	/* Queue */
	case 'W':	/* Bounce */
		if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
			char *p;
			fprintf(ofp, "%u ", t->bytes);
			p = dump_pdu(pdu_buf, pdu_len);
			if (p)
				fprintf(ofp, "(%s) ", p);
			fprintf(ofp, "[%s]\n", t->comm);
		} else {
			if (elapsed != -1ULL) {
				fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
					(unsigned long long) t->sector,
					t_sec(t), elapsed, t->comm);
			} else {
				fprintf(ofp, "%llu + %u [%s]\n",
					(unsigned long long) t->sector,
					t_sec(t), t->comm);
			}
		}
		break;

	case 'B':	/* Back merge */
	case 'F':	/* Front merge */
	case 'M':	/* Front or back merge */
	case 'G':	/* Get request */
	case 'S':	/* Sleep request */
		fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
			t_sec(t), t->comm);
		break;

	case 'P':	/* Plug */
		fprintf(ofp, "[%s]\n", t->comm);
		break;

	case 'U':	/* Unplug IO */
	case 'T': 	/* Unplug timer */
		fprintf(ofp, "[%s] %u\n", t->comm, get_pdu_int(t));
		break;

	case 'A': {	/* remap */
		struct blk_io_trace_remap r;

		get_pdu_remap(t, &r);
		fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
			(unsigned long long) r.sector, t_sec(t),
			MAJOR(r.device), MINOR(r.device),
			(unsigned long long) t->sector);
		break;
	}
		
	case 'X': 	/* Split */
		fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
			get_pdu_int(t), t->comm);
		break;

	default:
		fprintf(stderr, "Unknown action %c\n", act[0]);
		break;
	}

}

void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
		 unsigned long long elapsed, int pdu_len,
		 unsigned char *pdu_buf)
{
	char *p = override_format[(int) *act];

	if (!p) {
		process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
		return;
	}

	while (*p) {
		switch (*p) {
		case '%': 	/* Field specifier */
			p++;
			if (*p == '%')
				fprintf(ofp, "%c", *p++);
			else if (!*p)
				fprintf(ofp, "%c", '%');
			else
				p = parse_field(act, pci, t, elapsed,
						pdu_len, pdu_buf, p);
			break;
		case '\\': {	/* escape */
			switch (p[1]) {
			case 'b': fprintf(ofp, "\b"); break;
			case 'n': fprintf(ofp, "\n"); break;
			case 'r': fprintf(ofp, "\r"); break;
			case 't': fprintf(ofp, "\t"); break;
			default:
				fprintf(stderr,	
					"Invalid escape char in format %c\n",
					p[1]);
				exit(1);
				/*NOTREACHED*/
			}
			p += 2;
			break;
		}
		default:
			fprintf(ofp, "%c", *p++);
			break;
		}
	}
}