-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrecord.c
744 lines (641 loc) · 16.8 KB
/
btrecord.c
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* Blktrace record utility - Convert binary trace data into bunches of IOs
*
* Copyright (C) 2007 Alan D. Brunelle <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
static char build_date[] = __DATE__ " at "__TIME__;
#include "Python.h"
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdarg.h>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <getopt.h>
#include "list.h"
#include "btrecord.h"
int data_is_native; // Indicates whether to swap
LIST_HEAD(input_files); // List of all input files
static char *idir = "."; // Input directory base
static char *odir = "."; // Output directory base
static char *obase = "replay"; // Output file base
static __u64 max_bunch_tm = (10 * 1000 * 1000); // 10 milliseconds
static __u64 max_pkts_per_bunch = 8; // Default # of pkts per bunch
static int verbose = 0; // Boolean: output stats
static int find_traces = 0; // Boolean: Find traces in dir
static char usage_str[] = \
"\n" \
"\t[ -d <dir> : --input-directory=<dir> ] Default: .\n" \
"\t[ -D <dir> : --output-directory=<dir>] Default: .\n" \
"\t[ -F : --find-traces ] Default: Off\n" \
"\t[ -h : --help ] Default: Off\n" \
"\t[ -m <nsec> : --max-bunch-time=<nsec> ] Default: 10 msec\n" \
"\t[ -M <pkts> : --max-pkts=<pkts> ] Default: 8\n" \
"\t[ -o <base> : --output-base=<base> ] Default: replay\n" \
"\t[ -v : --verbose ] Default: Off\n" \
"\t[ -V : --version ] Default: Off\n" \
"\t<dev>... Default: None\n" \
"\n";
#define S_OPTS "d:D:Fhm:M:o:vV"
static struct option l_opts[] = {
{
.name = "input-directory",
.has_arg = required_argument,
.flag = NULL,
.val = 'd'
},
{
.name = "output-directory",
.has_arg = required_argument,
.flag = NULL,
.val = 'D'
},
{
.name = "find-traces",
.has_arg = no_argument,
.flag = NULL,
.val = 'F'
},
{
.name = "help",
.has_arg = no_argument,
.flag = NULL,
.val = 'h'
},
{
.name = "max-bunch-time",
.has_arg = required_argument,
.flag = NULL,
.val = 'm'
},
{
.name = "max_pkts",
.has_arg = required_argument,
.flag = NULL,
.val = 'M'
},
{
.name = "output-base",
.has_arg = required_argument,
.flag = NULL,
.val = 'o'
},
{
.name = "verbose",
.has_arg = no_argument,
.flag = NULL,
.val = 'v'
},
{
.name = "version",
.has_arg = no_argument,
.flag = NULL,
.val = 'V'
},
{
.name = NULL
}
};
#define ERR_ARGS 1
#define ERR_SYSCALL 2
static inline void fatal(const char *errstring, const int exitval,
const char *fmt, ...)
{
va_list ap;
if (errstring)
perror(errstring);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(exitval);
/*NOTREACHED*/
}
#if 0
/**
* match - Return true if this trace is a proper QUEUE transaction
* @action: Action field from trace
*/
static inline int match(__u32 action)
{
return ((action & 0xffff) == __BLK_TA_QUEUE) &&
(action & BLK_TC_ACT(BLK_TC_QUEUE));
}
#endif
/**
* usage - Display usage string and version
*/
static void usage(void)
{
fprintf(stderr, "Usage: btrecord -- version %s\n%s",
my_btversion, usage_str);
}
/**
* write_file_hdr - Seek to and write btrecord file header
* @stream: Output file information
* @hdr: Header to write
*/
static void write_file_hdr(struct io_stream *stream, struct io_file_hdr *hdr)
{
hdr->version = mk_btversion(btver_mjr, btver_mnr, btver_sub);
if (verbose) {
fprintf(stderr, "\t%s: %llx %llx %llx %llx\n",
stream->file_name,
(long long unsigned)hdr->version,
(long long unsigned)hdr->genesis,
(long long unsigned)hdr->nbunches,
(long long unsigned)hdr->total_pkts);
}
fseek(stream->ofp, 0, SEEK_SET);
if (fwrite(hdr, sizeof(*hdr), 1, stream->ofp) != 1) {
fatal(stream->file_name, ERR_SYSCALL, "Hdr write failed\n");
/*NOTREACHED*/
}
}
/**
* io_bunch_create - Allocate & initialize an io_bunch
* @io_stream: IO stream being added to
* @pre_stall: Amount of time that this bunch should be delayed by
* @start_time: Records current start
*/
static inline void io_bunch_create(struct io_stream *stream, __u64 start_time)
{
struct io_bunch *cur = malloc(sizeof(*cur));
memset(cur, 0, sizeof(*cur));
cur->hdr.npkts = 0;
cur->hdr.time_stamp = stream->start_time = start_time;
stream->cur = cur;
}
/**
* io_bunch_add - Add an IO to the current bunch of IOs
* @stream: Per-output file stream information
* @spec: IO trace specification
*
* Returns update bunch information
*/
static void io_bunch_add(struct io_stream *stream, struct io_spec *spec)
{
struct io_bunch *cur = stream->cur;
struct io_pkt iop = {
.sector = spec->sector,
.nbytes = spec->bytes,
.rw = spec->rw
};
assert(cur != NULL);
assert(cur->hdr.npkts < BT_MAX_PKTS);
assert(stream->last_time == 0 || stream->last_time <= spec->time);
cur->pkts[cur->hdr.npkts++] = iop; // Struct copy
stream->last_time = spec->time;
}
#if 0
/**
* rem_input_file - Release resources associated with an input file
* @iip: Per-input file information
*/
static void rem_input_file(struct ifile_info *iip)
{
list_del(&iip->head);
close(iip->ifd);
free(iip->file_name);
free(iip->devnm);
free(iip);
}
#endif
/**
* __add_input_file - Allocate and initialize per-input file structure
* @cpu: CPU for this file
* @devnm: Device name for this file
* @file_name: Fully qualifed input file name
*/
static void __add_input_file(int cpu, char *devnm, char *file_name)
{
struct ifile_info *iip = malloc(sizeof(*iip));
iip->cpu = cpu;
iip->tpkts = 0;
iip->genesis = 0;
iip->devnm = strdup(devnm);
iip->file_name = strdup(file_name);
iip->ifd = open(file_name, O_RDONLY);
if (iip->ifd < 0) {
fatal(file_name, ERR_ARGS, "Unable to open\n");
/*NOTREACHED*/
}
list_add_tail(&iip->head, &input_files);
}
/**
* add_input_file - Set up the input file name
* @devnm: Device name to use
*/
static void add_input_file(char *devnm)
{
struct list_head *p;
int cpu, found = 0;
__list_for_each(p, &input_files) {
struct ifile_info *iip = list_entry(p, struct ifile_info, head);
if (strcmp(iip->devnm, devnm) == 0)
return;
}
for (cpu = 0; ; cpu++) {
char full_name[MAXPATHLEN];
sprintf(full_name, "%s/%s.blktrace.%d", idir, devnm, cpu);
if (access(full_name, R_OK) != 0)
break;
__add_input_file(cpu, devnm, full_name);
found++;
}
if (!found) {
fatal(NULL, ERR_ARGS, "No traces found for %s\n", devnm);
/*NOTREACHED*/
}
}
static void find_input_files(char *idir)
{
struct dirent *ent;
DIR *dir = opendir(idir);
if (dir == NULL) {
fatal(idir, ERR_ARGS, "Unable to open %s\n", idir);
/*NOTREACHED*/
}
while ((ent = readdir(dir)) != NULL) {
char *p, *dsf = malloc(256);
if (strstr(ent->d_name, ".blktrace.") == NULL)
continue;
dsf = strdup(ent->d_name);
p = index(dsf, '.');
assert(p != NULL);
*p = '\0';
add_input_file(dsf);
free(dsf);
}
closedir(dir);
}
/**
* handle_args - Parse passed in argument list
* @argc: Number of arguments in argv
* @argv: Arguments passed in
*
* Does rudimentary parameter verification as well.
*/
void handle_args(int argc, char *argv[])
{
int c;
while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
switch (c) {
case 'd':
idir = optarg;
if (access(idir, R_OK | X_OK) != 0) {
fatal(idir, ERR_ARGS,
"Invalid input directory specified\n");
/*NOTREACHED*/
}
break;
case 'D':
odir = optarg;
if (access(odir, R_OK | X_OK) != 0) {
fatal(odir, ERR_ARGS,
"Invalid output directory specified\n");
/*NOTREACHED*/
}
break;
case 'F':
find_traces = 1;
break;
case 'h':
usage();
exit(0);
/*NOTREACHED*/
case 'm':
max_bunch_tm = (__u64)atoll(optarg);
if (max_bunch_tm < 1) {
fprintf(stderr, "Invalid bunch time %llu\n",
(unsigned long long)max_bunch_tm);
exit(ERR_ARGS);
/*NOTREACHED*/
}
break;
case 'M':
max_pkts_per_bunch = (__u64)atoll(optarg);
if (!((1 <= max_pkts_per_bunch) &&
(max_pkts_per_bunch < 513))) {
fprintf(stderr, "Invalid max pkts %llu\n",
(unsigned long long)max_pkts_per_bunch);
exit(ERR_ARGS);
/*NOTREACHED*/
}
break;
case 'o':
obase = optarg;
break;
case 'V':
fprintf(stderr, "btrecord -- version %s\n",
my_btversion);
fprintf(stderr, " Built on %s\n", build_date);
exit(0);
/*NOTREACHED*/
case 'v':
verbose++;
break;
default:
usage();
fatal(NULL, ERR_ARGS, "Invalid command line\n");
/*NOTREACHED*/
}
}
while (optind < argc)
add_input_file(argv[optind++]);
if (find_traces)
find_input_files(idir);
if (list_len(&input_files) == 0) {
fatal(NULL, ERR_ARGS, "Missing required input file name(s)\n");
/*NOTREACHED*/
}
}
#if 0
/**
* next_io - Retrieve next Q trace from input stream
* @iip: Per-input file information
* @spec: IO specifier for trace
*
* Returns 0 on end of file, 1 if valid data returned.
*/
static int next_io(struct ifile_info *iip, struct io_spec *spec)
{
ssize_t ret;
__u32 action;
__u16 pdu_len;
struct blk_io_trace t;
again:
ret = read(iip->ifd, &t, sizeof(t));
if (ret < 0) {
fatal(iip->file_name, ERR_SYSCALL, "Read failed\n");
/*NOTREACHED*/
}
else if (ret == 0)
return 0;
else if (ret < (ssize_t)sizeof(t)) {
fprintf(stderr, "WARNING: Short read on %s (%d)\n",
iip->file_name, (int)ret);
return 0;
}
if (data_is_native == -1)
check_data_endianness(t.magic);
assert(data_is_native >= 0);
if (data_is_native) {
spec->time = t.time;
spec->sector = t.sector;
spec->bytes = t.bytes;
action = t.action;
pdu_len = t.pdu_len;
}
else {
spec->time = be64_to_cpu(t.time);
spec->sector = be64_to_cpu(t.sector);
spec->bytes = be32_to_cpu(t.bytes);
action = be32_to_cpu(t.action);
pdu_len = be16_to_cpu(t.pdu_len);
}
if (pdu_len) {
char buf[pdu_len];
ret = read(iip->ifd, buf, pdu_len);
if (ret < 0) {
fatal(iip->file_name, ERR_SYSCALL, "Read PDU failed\n");
/*NOTREACHED*/
}
else if (ret < (ssize_t)pdu_len) {
fprintf(stderr, "WARNING: Short PDU read on %s (%d)\n",
iip->file_name, (int)ret);
return 0;
}
}
iip->tpkts++;
if (!match(action))
goto again;
spec->rw = (action & BLK_TC_ACT(BLK_TC_READ)) ? 1 : 0;
if (verbose > 1)
fprintf(stderr, "%2d: %10llu+%10llu (%d) @ %10llx\n",
iip->cpu, (long long unsigned)spec->sector,
(long long unsigned)spec->bytes / 512LLU,
spec->rw, (long long unsigned)spec->time);
if (iip->genesis == 0) {
iip->genesis = spec->time;
if (verbose > 1)
fprintf(stderr, "\tSetting new genesis: %llx(%d)\n",
(long long unsigned)iip->genesis, iip->cpu);
}
else if (iip->genesis > spec->time)
fatal(NULL, ERR_SYSCALL,
"Time inversion? %llu ... %llu\n",
(long long unsigned )iip->genesis,
(long long unsigned )spec->time);
return 1;
}
#endif
/**
* bunch_output_hdr - Output bunch header
*/
static inline void bunch_output_hdr(struct io_stream *stream)
{
struct io_bunch_hdr *hdrp = &stream->cur->hdr;
assert(0 < hdrp->npkts && hdrp->npkts <= BT_MAX_PKTS);
if (fwrite(hdrp, sizeof(struct io_bunch_hdr), 1, stream->ofp) != 1) {
fatal(stream->file_name, ERR_SYSCALL, "fwrite(hdr) failed\n");
/*NOTREACHED*/
}
if (verbose) {
__u64 off = hdrp->time_stamp - stream->iip->genesis;
assert(stream->vfp);
fprintf(stream->vfp, "------------------\n");
fprintf(stream->vfp, "%4llu.%09llu %3llu\n",
(unsigned long long)off / (1000 * 1000 * 1000),
(unsigned long long)off % (1000 * 1000 * 1000),
(unsigned long long)hdrp->npkts);
fprintf(stream->vfp, "------------------\n");
}
}
/**
* bunch_output_pkt - Output IO packets
*/
static inline void bunch_output_pkts(struct io_stream *stream)
{
struct io_pkt *p = stream->cur->pkts;
size_t npkts = stream->cur->hdr.npkts;
assert(0 < npkts && npkts <= BT_MAX_PKTS);
if (fwrite(p, sizeof(struct io_pkt), npkts, stream->ofp) != npkts) {
fatal(stream->file_name, ERR_SYSCALL, "fwrite(pkts) failed\n");
/*NOTREACHED*/
}
if (verbose) {
size_t i;
assert(stream->vfp);
for (i = 0; i < npkts; i++, p++)
fprintf(stream->vfp, "\t%1d %10llu\t%10llu\n",
p->rw,
(unsigned long long)p->sector,
(unsigned long long)p->nbytes / 512);
}
}
/**
* stream_flush - Flush current bunch of IOs out to the output stream
* @stream: Per-output file stream information
*/
static void stream_flush(struct io_stream *stream)
{
struct io_bunch *cur = stream->cur;
if (cur) {
if (cur->hdr.npkts) {
assert(cur->hdr.npkts <= BT_MAX_PKTS);
bunch_output_hdr(stream);
bunch_output_pkts(stream);
stream->bunches++;
stream->pkts += cur->hdr.npkts;
}
free(cur);
}
}
/**
* bunch_done - Returns true if current bunch is either full, or next IO is late
* @stream: Output stream information
* @spec: IO trace specification
*/
static inline int bunch_done(struct io_stream *stream, struct io_spec *spec)
{
if (stream->cur->hdr.npkts >= max_pkts_per_bunch)
return 1;
if ((spec->time - stream->start_time) > max_bunch_tm)
return 1;
return 0;
}
/**
* stream_add_io - Add an IO trace to the current stream
* @stream: Output stream information
* @spec: IO trace specification
*/
void stream_add_io(struct io_stream *stream, struct io_spec *spec)
{
if (stream->cur == NULL)
io_bunch_create(stream, spec->time);
else if (bunch_done(stream, spec)) {
stream_flush(stream);
io_bunch_create(stream, spec->time);
}
io_bunch_add(stream, spec);
}
/**
* stream_open - Open output stream for specified input stream
* @iip: Per-input file information
*/
struct io_stream *stream_open(struct ifile_info *iip)
{
char ofile_name[MAXPATHLEN];
struct io_stream *stream = malloc(sizeof(*stream));
struct io_file_hdr io_file_hdr = {
.genesis = 0,
.nbunches = 0,
.total_pkts = 0
};
memset(stream, 0, sizeof(*stream));
sprintf(ofile_name, "%s/%s.%s.%d", odir, iip->devnm, obase, iip->cpu);
stream->ofp = fopen(ofile_name, "w");
if (!stream->ofp) {
fatal(ofile_name, ERR_SYSCALL, "Open failed\n");
/*NOTREACHED*/
}
stream->iip = iip;
stream->cur = NULL;
stream->bunches = stream->pkts = 0;
stream->last_time = 0;
stream->file_name = strdup(ofile_name);
write_file_hdr(stream, &io_file_hdr);
if (verbose) {
char vfile_name[MAXPATHLEN];
sprintf(vfile_name, "%s/%s.%s.%d.rec", odir, iip->devnm,
obase, iip->cpu);
stream->vfp = fopen(vfile_name, "w");
if (!stream->vfp) {
fatal(vfile_name, ERR_SYSCALL, "Open failed\n");
/*NOTREACHED*/
}
stream->vfn = strdup(vfile_name);
}
data_is_native = -1;
return stream;
}
/**
* stream_close - Release resources associated with an output stream
* @stream: Stream to release
*/
void stream_close(struct io_stream *stream)
{
struct io_file_hdr io_file_hdr = {
.genesis = stream->iip->genesis,
.nbunches = stream->bunches,
.total_pkts = stream->pkts
};
stream_flush(stream);
write_file_hdr(stream, &io_file_hdr);
fclose(stream->ofp);
if (verbose && stream->bunches) {
fprintf(stderr,
"%s:%d: %llu pkts (tot), %llu pkts (replay), "
"%llu bunches, %.1lf pkts/bunch\n",
stream->iip->devnm, stream->iip->cpu,
(unsigned long long)stream->iip->tpkts,
(unsigned long long)stream->pkts,
(unsigned long long)stream->bunches,
(double)(stream->pkts) / (double)(stream->bunches));
fclose(stream->vfp);
free(stream->vfn);
}
free(stream->file_name);
free(stream);
}
#if 0
/**
* process - Process one input file to an output file
* @iip: Per-input file information
*/
static void process(struct ifile_info *iip)
{
struct io_spec spec;
struct io_stream *stream;
stream = stream_open(iip);
while (next_io(iip, &spec))
stream_add_io(stream, &spec);
stream_close(stream);
rem_input_file(iip);
}
/**
* main -
* @argc: Number of arguments
* @argv: Array of arguments
*/
int main(int argc, char *argv[])
{
struct list_head *p, *q;
handle_args(argc, argv);
list_for_each_safe(p, q, &input_files)
process(list_entry(p, struct ifile_info, head));
return 0;
}
#endif