1 From e04ef476fd330485e5a88c7018d29c55cf411fe2 Mon Sep 17 00:00:00 2001
2 From: Richard W.M. Jones <rjones@redhat.com>
3 Date: Tue, 5 Oct 2010 09:54:10 +0100
4 Subject: [PATCH] Trace reads and writes to qemu block devices.
6 NB: This patch is not suitable for nor intended to go upstream in
9 When qemu opens a block device, this patch creates a trace file
10 in /tmp with a name related to the block device. Reads and writes
11 to the device cause lines to be written to the trace file:
14 W <sector> <nb_sectors>
15 R <sector> <nb_sectors>
17 'S' is the summary line, printed first which just tells you how
18 many sectors are on the device.
20 'W' and 'R' are writes and reads, for the range <sector> through
21 to <sector> + <nb_sectors> - 1.
23 block.c | 29 +++++++++++++++++++++++++++++
25 2 files changed, 32 insertions(+), 0 deletions(-)
27 diff --git a/block.c b/block.c
28 index ebbc376..26ead5b 100644
31 @@ -474,6 +474,23 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename,
35 + /* Open trace file in /tmp based on filename. XXX */
36 + size_t len = strlen (filename);
37 + char *trace_file = qemu_malloc (10 + len);
38 + snprintf (trace_file, 10 + len, "/tmp/%s.qtr", filename);
40 + for (i = 5; i < 5 + len; ++i) {
41 + if (trace_file[i] == '/')
42 + trace_file[i] = '_';
44 + bs->trace_fp = fopen (trace_file, "w");
46 + setlinebuf (bs->trace_fp);
47 + fprintf (bs->trace_fp, "S %" PRIi64 "\n", bs->total_sectors);
49 + perror (trace_file);
53 if (bs->is_temporary) {
55 @@ -665,6 +682,10 @@ void bdrv_close(BlockDriverState *bs)
60 + fclose (bs->trace_fp);
61 + bs->trace_fp = NULL;
63 /* call the change callback */
64 bs->media_changed = 1;
66 @@ -1995,6 +2016,10 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
67 /* Update stats even though technically transfer has not happened. */
68 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
72 + fprintf (bs->trace_fp,
73 + "R %" PRIi64 " %d\n", sector_num, nb_sectors);
77 @@ -2028,6 +2053,10 @@ BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
78 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
79 bs->wr_highest_sector = sector_num + nb_sectors - 1;
83 + fprintf (bs->trace_fp,
84 + "W %" PRIi64 " %d\n", sector_num, nb_sectors);
88 diff --git a/block_int.h b/block_int.h
89 index e8e7156..03e7c9b 100644
92 @@ -178,6 +178,9 @@ struct BlockDriverState {
94 uint64_t wr_highest_sector;
96 + /* Trace to file. */
99 /* Whether the disk can expand beyond total_sectors */